docs/189268: 3 getaddrinfo(3) - hostanme="localhost", but it returns IN_ADDR_ANY (0.0.0.0)

Dreamcat4 dreamcat4 at gmail.com
Sat May 3 07:50:01 UTC 2014


The following reply was made to PR docs/189268; it has been noted by GNATS.

From: Dreamcat4 <dreamcat4 at gmail.com>
To: bug-followup at freebsd.org, dreamcat4 at gmail.com
Cc:  
Subject: Re: docs/189268: 3 getaddrinfo(3) - hostanme="localhost",
 but it returns IN_ADDR_ANY (0.0.0.0)
Date: Sat, 3 May 2014 08:44:57 +0100

 --001a1133059e7da3f704f87a150e
 Content-Type: text/plain; charset=UTF-8
 
 getaddrinfo() returns a list of results. So I have updated my test program
 to walk the entire list. It now prints two results for PF_UNSPEC
 
 getaddrinfo ~/ root~# gcc test.c -o test && ./test
 0.0.0.0
 127.0.0.1
 getaddrinfo ~/ root~#
 
 Where the first result in the list is supposed to be the ipv6's "::1" (but
 isn't).
 
 Anyway heres the new version of the test.c
 
 getaddrinfo ~/ root~# cat test.c
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 
 /* man getaddrinfo */
 
 int main()
 {
   struct addrinfo *addrinfo = NULL;
   struct addrinfo *rp = NULL;
   struct addrinfo hints;
   struct addrinfo res;
   int err;
 
   memset((void*)&hints, 0, sizeof(hints));
   hints.ai_family   = PF_UNSPEC;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags    = AI_PASSIVE;
 
   err = getaddrinfo("localhost", "18083", &hints, &addrinfo);
 
   if (addrinfo)
   {
     for (rp = addrinfo; rp != NULL; rp = rp->ai_next)
     {
       printf("%s\n", inet_ntoa( ((struct
 sockaddr_in*)rp->ai_addr)->sin_addr ));
     }
 
     freeaddrinfo(addrinfo);
     return(0);
   }
 
   if (err || !addrinfo)
   {
     printf("getaddrinfo failed with code %i.\n",err);
     return(1);
   }
 
   printf("end_main()\n");
   return (0);
 
 }
 
 getaddrinfo ~/ root~# gcc test.c -o test && ./test
 0.0.0.0
 127.0.0.1
 getaddrinfo ~/ root~#
 
 --001a1133059e7da3f704f87a150e
 Content-Type: text/html; charset=UTF-8
 Content-Transfer-Encoding: quoted-printable
 
 <div dir=3D"ltr">getaddrinfo() returns a list of results. So I have updated=
  my test program to walk the entire list. It now prints two results for PF_=
 UNSPEC<div><br></div><div><div><div>getaddrinfo ~/ root~# gcc test.c -o tes=
 t && ./test</div>
 
 <div>0.0.0.0</div><div>127.0.0.1</div><div>getaddrinfo ~/ root~#=C2=A0</div=
 ></div></div><div><br></div><div>Where the first result in the list is supp=
 osed to be the ipv6's "::1" (but isn't).</div><div><br></=
 div>
 
 <div>Anyway heres the new version of the test.c</div><div><br></div><div><d=
 iv>getaddrinfo ~/ root~# cat test.c=C2=A0</div><div>#include <string.h&g=
 t;</div><div>#include <stdio.h></div><div>#include <stdlib.h></=
 div>
 
 <div>#include <sys/types.h></div><div>#include <sys/socket.h></=
 div><div>#include <netdb.h></div><div>#include <netinet/in.h></=
 div><div>#include <arpa/inet.h></div><div><br></div><div><br></div>
 
 <div>/* man getaddrinfo */</div><div><br></div><div>int main()</div><div>{<=
 /div><div>=C2=A0 struct addrinfo *addrinfo =3D NULL;</div><div>=C2=A0 struc=
 t addrinfo *rp =3D NULL;</div><div>=C2=A0 struct addrinfo hints;</div><div>=
 =C2=A0 struct addrinfo res;</div>
 
 <div>=C2=A0 int err;</div><div><br></div><div>=C2=A0 memset((void*)&hin=
 ts, 0, sizeof(hints));</div><div>=C2=A0 hints.ai_family =C2=A0 =3D PF_UNSPE=
 C;</div><div>=C2=A0 hints.ai_socktype =3D SOCK_STREAM;</div><div>=C2=A0 hin=
 ts.ai_flags =C2=A0 =C2=A0=3D AI_PASSIVE;</div>
 
 <div><br></div><div>=C2=A0 err =3D getaddrinfo("localhost", &quot=
 ;18083", &hints, &addrinfo);</div><div><br></div><div>=C2=A0 i=
 f (addrinfo)</div><div>=C2=A0 {=C2=A0</div><div>=C2=A0 =C2=A0 for (rp =3D a=
 ddrinfo; rp !=3D NULL; rp =3D rp->ai_next)</div>
 
 <div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 printf("%s\n"=
 , inet_ntoa( ((struct sockaddr_in*)rp->ai_addr)->sin_addr ));</div><d=
 iv>=C2=A0 =C2=A0 }</div><div><br></div><div>=C2=A0 =C2=A0 freeaddrinfo(addr=
 info);</div><div>=C2=A0 =C2=A0 return(0);</div><div>
 
 =C2=A0 }</div><div><br></div><div>=C2=A0 if (err || !addrinfo)</div><div>=
 =C2=A0 {</div><div>=C2=A0 =C2=A0 printf("getaddrinfo failed with code =
 %i.\n",err);</div><div>=C2=A0 =C2=A0 return(1);</div><div>=C2=A0 }</di=
 v><div><br></div><div>=C2=A0 printf("end_main()\n");</div>
 
 <div>=C2=A0 return (0);</div><div><br></div><div>}</div><div><br></div><div=
 >getaddrinfo ~/ root~# gcc test.c -o test && ./test</div><div>0.0.0=
 .0</div><div>127.0.0.1</div><div>getaddrinfo ~/ root~#=C2=A0</div></div><di=
 v><br>
 
 </div></div>
 
 --001a1133059e7da3f704f87a150e--


More information about the freebsd-doc mailing list