Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 3 May 2014 07:50:00 GMT
From:      Dreamcat4 <dreamcat4@gmail.com>
To:        freebsd-doc@FreeBSD.org
Subject:   Re: docs/189268: 3 getaddrinfo(3) - hostanme=&quot;localhost&quot;, but it returns IN_ADDR_ANY (0.0.0.0)
Message-ID:  <201405030750.s437o0AU014902@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR docs/189268; it has been noted by GNATS.

From: Dreamcat4 <dreamcat4@gmail.com>
To: bug-followup@freebsd.org, dreamcat4@gmail.com
Cc:  
Subject: Re: docs/189268: 3 getaddrinfo(3) - hostanme=&quot;localhost&quot;,
 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 &amp;&amp; ./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&#39;s &quot;::1&quot; (but isn&#39;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 &lt;string.h&g=
 t;</div><div>#include &lt;stdio.h&gt;</div><div>#include &lt;stdlib.h&gt;</=
 div>
 
 <div>#include &lt;sys/types.h&gt;</div><div>#include &lt;sys/socket.h&gt;</=
 div><div>#include &lt;netdb.h&gt;</div><div>#include &lt;netinet/in.h&gt;</=
 div><div>#include &lt;arpa/inet.h&gt;</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*)&amp;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(&quot;localhost&quot;, &quot=
 ;18083&quot;, &amp;hints, &amp;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-&gt;ai_next)</div>
 
 <div>=C2=A0 =C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 printf(&quot;%s\n&quot;=
 , inet_ntoa( ((struct sockaddr_in*)rp-&gt;ai_addr)-&gt;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(&quot;getaddrinfo failed with code =
 %i.\n&quot;,err);</div><div>=C2=A0 =C2=A0 return(1);</div><div>=C2=A0 }</di=
 v><div><br></div><div>=C2=A0 printf(&quot;end_main()\n&quot;);</div>
 
 <div>=C2=A0 return (0);</div><div><br></div><div>}</div><div><br></div><div=
 >getaddrinfo ~/ root~# gcc test.c -o test &amp;&amp; ./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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201405030750.s437o0AU014902>