From owner-freebsd-questions@FreeBSD.ORG Wed Jan 18 15:20:17 2012 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0078C106566C for ; Wed, 18 Jan 2012 15:20:17 +0000 (UTC) (envelope-from andreev.peter@gmail.com) Received: from mail-lpp01m010-f54.google.com (mail-lpp01m010-f54.google.com [209.85.215.54]) by mx1.freebsd.org (Postfix) with ESMTP id 7B2D68FC13 for ; Wed, 18 Jan 2012 15:20:16 +0000 (UTC) Received: by lahe6 with SMTP id e6so1310895lah.13 for ; Wed, 18 Jan 2012 07:20:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=I0dah3k91e0FSsaRtYovTYIW6EPc9B/DwTFlCxlrWCI=; b=rUivkJypk01KJ7UGSD2SsQ9ZUOCzK1UR2h/2NJm1vK7khXVXza1xbFS7wdQd8tjjc+ aCVJrVvhQY+AM6x4W+JdqCsrh8JvHCCrS6exQjNUeftNHobaHJbzhiPq74KVGvLmhJPu OO8eFHZ5S6ITTUVPaJgVD2w3mr7yW25+euELU= MIME-Version: 1.0 Received: by 10.112.42.170 with SMTP id p10mr5538176lbl.30.1326898532670; Wed, 18 Jan 2012 06:55:32 -0800 (PST) Received: by 10.112.65.174 with HTTP; Wed, 18 Jan 2012 06:55:32 -0800 (PST) In-Reply-To: <201201181301.q0ID1Mss037383@x.it.okstate.edu> References: <201201181301.q0ID1Mss037383@x.it.okstate.edu> Date: Wed, 18 Jan 2012 18:55:32 +0400 Message-ID: From: Peter Andreev To: Martin McCormick Content-Type: text/plain; charset=ISO-8859-1 X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: questions@freebsd.org Subject: Re: Sample getaddrinfo Code Compiles in Linux but not FreeBSD. X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Jan 2012 15:20:17 -0000 2012/1/18 Martin McCormick > 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 > #include > #include > #include > #include > #include > > 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; > } > #include > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to " > freebsd-questions-unsubscribe@freebsd.org" > -- -- AP