getaddrinfo: filtering by service

This is a discussion on getaddrinfo: filtering by service within the C Programming forums, part of the General Programming Boards category; I supposed that passing "80"/"host" as service parameter to getaddrinfo will return web-socket on host. But instead of that (after ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    51

    getaddrinfo: filtering by service

    I supposed that passing "80"/"host" as service parameter to getaddrinfo
    will return web-socket on host. But instead of that (after passing
    "www.google.com" as host) i obtained structure with filled ai_addr substructure (as sockaddr_in *) with port=20480. Full code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <errno.h>
    #include <string.h>
    #include <netinet/in.h>
    extern int errno;
    extern int h_errno;
    
    #define arrsize(x) sizeof(x)/sizeof(x[0])
    static struct addrinfo *pp=NULL;
    static struct addrinfo **ServerSocketAddresses=&pp;//&pp;
    
    static void print_error(const char *file, const int line, const char *func, const char *lib_fn, 
      const char *err_msg)
    {
      fprintf(stderr, "Program fails in %s:%s:%d:\n  after calling of %s: %s\n",
        file, func, line, lib_fn, err_msg);
      exit(1);
    }
    
    #define wrap_print_error(fn, err_msg) print_error(__FILE__, __LINE__, __FUNCTION__ , fn, err_msg)
    #define wrap_print_sys_error(fn) wrap_print_error(fn, strerror(errno))
    #define wrap_print_net_error(fn, errc) wrap_print_error(fn, gai_strerror(errc))
    
    static struct addrinfo *get_server_info(const char *host, const char *port)
    {
      int l_error;
      struct addrinfo *OneSocketAddr;
      /*
      struct addrinfo hint;
      //
      hint.ai_family=AF_UNSPEC;
      hint.ai_socktype=SOCK_STREAM;
      hint.ai_flags=(AI_V4MAPPED | AI_ADDRCONFIG);
      */
      l_error=getaddrinfo(host, port, NULL, ServerSocketAddresses);
      if (l_error<0)
      {
        if (l_error==EAI_SYSTEM)
          wrap_print_sys_error("getaddrinfo: system error: ");
        else
          wrap_print_net_error("getaddrinfo: ", l_error);
        return NULL;
      }
      OneSocketAddr=*ServerSocketAddresses;
      while(OneSocketAddr)
      {
        if ((OneSocketAddr->ai_family==AF_INET)&&(OneSocketAddr->ai_socktype==SOCK_STREAM)&&
          (OneSocketAddr->ai_addr))
          break;
        OneSocketAddr=OneSocketAddr->ai_next;
      }
      if (!OneSocketAddr)
      {
        fprintf(stderr, "No appropriate sockets on server available\n");
        return NULL;
      }
      return OneSocketAddr;
    }
    
    int main (const int argc, const char **argv)
    {
      int fd_socket=0, res=1;
      struct addrinfo *server_addr;
      struct sockaddr_in *dummy;//=(struct sockaddr_in *)server_addr;
      //
      if (argc!=3)
      {
        fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
        goto l_err;
      }
      server_addr=get_server_info(argv[1], argv[2]);
      if (server_addr)
      {
        fprintf(stderr, "found socket with options %d\n", server_addr->ai_flags);
      }
      else
        goto l_err; // errors already are handled
      dummy=(struct sockaddr_in *)server_addr->ai_addr;
      fd_socket=socket(AF_INET, SOCK_STREAM, server_addr->ai_protocol);
      if (fd_socket<0)
        wrap_print_sys_error("socket");
      //connect
      if (connect(fd_socket, server_addr->ai_addr, server_addr->ai_addrlen))
      {
        wrap_print_sys_error("connect");
        goto l_err;
      }
      {
        const char *data="GET www.google.com/ HTTP/1.0\n\n";//"Hello, server!";
        int rc;
        //
        rc = write(fd_socket, data, arrsize(data)*sizeof(char));
        if (rc<0)
        {
          wrap_print_sys_error("write");
          goto l_err;
        }
      }
      // respond
      while (1)
      {
        char buf[32];
        int rc;
        //
        rc=read(fd_socket, buf, arrsize(buf)-1);
        if (rc<0)
        {
          wrap_print_sys_error("read");
          goto l_err;
        }
        else if (rc==0) // read EOF
          break;
        else
        {
          buf[rc]='\0';
          fprintf(stdout, "%s", buf);
        }
      }
      fprintf(stdout, "\n");
      res=0;
    l_err:
      if (fd_socket)
        close(fd_socket);
      if (ServerSocketAddresses)
        freeaddrinfo(*ServerSocketAddresses);
      return res;
    }
    Program was executed as a.out google.com 80
    But others seem to not have such problems.
    After bit googling found that, a scary one
    Should i think i have trojan on my kubuntu or everything is much simpler as it use to be?

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    51
    Everything is OK.
    p htons(80)
    $1 = 20480

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getaddrinfo : who is right?
    By icegood in forum C Programming
    Replies: 2
    Last Post: 12-12-2012, 06:27 PM
  2. ip address using getaddrinfo()
    By ueg1990 in forum C Programming
    Replies: 4
    Last Post: 07-07-2012, 07:54 PM
  3. getaddrinfo() can't resolve hostname
    By chaos5687 in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 10:15 PM
  4. is getaddrinfo really necessary?
    By sunjayc99 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-28-2008, 04:14 PM
  5. getaddrinfo()
    By Cactus_Hugger in forum Windows Programming
    Replies: 1
    Last Post: 07-24-2006, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21