From owner-freebsd-hackers@FreeBSD.ORG Tue May 3 16:32:30 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5D7E916A4CE for ; Tue, 3 May 2005 16:32:30 +0000 (GMT) Received: from cheer.mahoroba.org (gw4.mahoroba.org [218.45.22.175]) by mx1.FreeBSD.org (Postfix) with ESMTP id D197243D78 for ; Tue, 3 May 2005 16:32:26 +0000 (GMT) (envelope-from ume@mahoroba.org) Received: from lyrics.mahoroba.org (ume@lyrics.mahoroba.org [IPv6:3ffe:501:185b:8010:280:88ff:fe03:4841]) (user=ume mech=CRAM-MD5 bits=0)j43GW4L7029034 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 4 May 2005 01:32:08 +0900 (JST) (envelope-from ume@mahoroba.org) Date: Wed, 04 May 2005 01:32:04 +0900 Message-ID: From: Hajimu UMEMOTO To: Sergey In-Reply-To: <1908103559.20050503183910@intercollab.net> References: <1908103559.20050503183910@intercollab.net> User-Agent: xcite1.38> Wanderlust/2.15.1 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.7 (=?ISO-8859-4?Q?Sanj=F2?=) APEL/10.6 Emacs/22.0.50 (i386-unknown-freebsd5.4) MULE/5.0 (SAKAKI) X-Operating-System: FreeBSD 5.4-STABLE X-PGP-Key: http://www.imasy.or.jp/~ume/publickey.asc X-PGP-Fingerprint: 1F00 0B9E 2164 70FC 6DC5 BF5F 04E9 F086 BF90 71FE Organization: Internet Mutual Aid Society, YOKOHAMA MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII X-Greylist: Sender succeded SMTP AUTH authentication, not delayed by milter-greylist-2.0b5 (cheer.mahoroba.org [IPv6:3ffe:501:185b:8010::1]); Wed, 04 May 2005 01:32:08 +0900 (JST) X-Virus-Scanned: by amavisd-new X-Virus-Status: Clean X-Spam-Status: No, score=-5.7 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.0.2 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on cheer.mahoroba.org cc: freebsd-hackers@freebsd.org Subject: Re: Can't get correct address in string form X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 May 2005 16:32:30 -0000 Hi, >>>>> On Tue, 3 May 2005 18:39:10 +0300 >>>>> Sergey said: fenix> //get local address - local address is 192.168.0.250 fenix> if ((gaierr = getaddrinfo(argv[1], argv[2], &hints, &localaddr)) != 0) fenix> errx(1, "%s port %s: %s", argv[1], argv[2], gai_strerror(gaierr)); You need to initialize hints before calling getaddrinfo(3). fenix> inet_ntop(la->ai_family, la->ai_addr->sa_data, buf, sizeof(struct sockaddr)); The usage of inet_ntop(3) is wrong. It should be something like: #include #include #include #include #include #include #include #include int main(int argc, char **argv) { struct addrinfo hints, *la, *localaddr; char buf[46]; int gaierr=0; //get local address - local address is 192.168.0.250 bzero(&hints, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((gaierr = getaddrinfo(argv[1], argv[2], &hints, &localaddr)) != 0) errx(1, "%s port %s: %s", argv[1], argv[2], gai_strerror(gaierr)); for (la = localaddr; la; la = la->ai_next) { switch (la->ai_family) { case AF_INET: inet_ntop(la->ai_family, &((struct sockaddr_in *)la->ai_addr)->sin_addr, buf, sizeof(buf)); break; case AF_INET6: inet_ntop(la->ai_family, &((struct sockaddr_in6 *)la->ai_addr)->sin6_addr, buf, sizeof(buf)); break; default: continue; } fprintf(stderr, "Address: %s\n", buf); } return 0; } You can use getnameinfo(3) to simplify usage of inet_ntop(3). Sincerely, -- Hajimu UMEMOTO @ Internet Mutual Aid Society Yokohama, Japan ume@mahoroba.org ume@{,jp.}FreeBSD.org http://www.imasy.org/~ume/