c - Using getaddrinfo not giving desired output -


extern "c" int rtinet_lookup( const char * host, char * address, unsigned int port)   {   struct addrinfo hints, *res;   int errcode;   char addrstr[inet6_addrstrlen];   void *ptr;    memset (&hints, 0, sizeof (hints));   hints.ai_family = af_unspec;   hints.ai_socktype = sock_stream;   hints.ai_protocol = ipproto_tcp;   hints.ai_flags |= ai_canonname;    errcode = getaddrinfo (host, "port", &hints, &res);   /*if (errcode != 0)     {       perror ("getaddrinfo");       return -1;     }*/    info(str("host: %s", host));   while (res)     {       inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr,inet6_addrstrlen);       info(str("inside while condition res "));       switch (res->ai_family)         {         case af_inet:           ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;           break;         case af_inet6:           ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;           break;         }       inet_ntop (res->ai_family, ptr, addrstr,inet6_addrstrlen);       info(str("ipv%d address: %s (%s)\n", res->ai_family == af_inet6 ? 6 : 4,               addrstr, res->ai_canonname));       res = res->ai_next;     }info(str("ipv%d address: %s (%s)\n", res->ai_family == af_inet6 ? 6 : 4,               addrstr, res->ai_canonname));    return 1; } 

in code, it's not going in while part res null maybe. can point out error here or correct way getaddrinfo used?

the 1 error see in code "port" parameter being passed getaddrinfo. not intended, , fail unless system have service named "port" defined.

either pass null service name , override port in resultant address, or pass string containing name or decimal representation of port. (in latter case, may want set ai_numericserv flag in hints).

as side note, code isn't calling freeaddrinfo, leaking memory.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -