Date: Wed, 18 Jan 2012 07:01:22 -0600 From: Martin McCormick <martin@dc.cis.okstate.edu> To: questions@freebsd.org Subject: Sample getaddrinfo Code Compiles in Linux but not FreeBSD. Message-ID: <201201181301.q0ID1Mss037383@x.it.okstate.edu>
next in thread | raw e-mail | index | archive | help
Here is a sample program kindly provided in the Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hall The code is said to be in the public domain so it is posted here as it compiles and runs perfectly under Linux but fails in two places with the following errors: I named it nsl.c. nsl.c: In function 'main': nsl.c:38: error: dereferencing pointer to incomplete type nsl.c:42: error: dereferencing pointer to incomplete type You will see that in both places, the code was performing the same operation of assigning a value to a pointer so I am suspecting a prototyping issue but am not sure and hope someone can help me cut through the forest a little more quickly. He did provide suggestions for users of Sunos who have reported errors, but for FreeBSD, the errors did not change. Here is the sample code with the two error-generating lines marked. #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> int main(int argc, char *argv[]) { struct addrinfo hints, *res, *p; int status; char ipstr[INET6_ADDRSTRLEN]; if (argc != 2) { fprintf(stderr,"usage: showip hostname\n"); return 1; } memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version hints.ai_socktype = SOCK_STREAM; if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return 2; } printf("IP addresses for %s:\n\n", argv[1]); for(p = res;p != NULL; p = p->ai_next) { void *addr; char *ipver; // get the pointer to the address itself, // different fields in IPv4 and IPv6: if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr);/*error*/ ipver = "IPv4"; } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr);/*error*/ ipver = "IPv6"; } // convert the IP to a string and print it: inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); printf(" %s: %s\n", ipver, ipstr); } freeaddrinfo(res); // free the linked list return 0; }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201201181301.q0ID1Mss037383>