16617cf57SGeorge V. Neville-Neil.\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $ 26617cf57SGeorge V. Neville-Neil.\" $OpenBSD: getaddrinfo.3,v 1.35 2004/12/21 03:40:31 jaredy Exp $ 36617cf57SGeorge V. Neville-Neil.\" 46617cf57SGeorge V. Neville-Neil.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") 56617cf57SGeorge V. Neville-Neil.\" Copyright (C) 2000, 2001 Internet Software Consortium. 66617cf57SGeorge V. Neville-Neil.\" 76617cf57SGeorge V. Neville-Neil.\" Permission to use, copy, modify, and distribute this software for any 86617cf57SGeorge V. Neville-Neil.\" purpose with or without fee is hereby granted, provided that the above 96617cf57SGeorge V. Neville-Neil.\" copyright notice and this permission notice appear in all copies. 106617cf57SGeorge V. Neville-Neil.\" 116617cf57SGeorge V. Neville-Neil.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 126617cf57SGeorge V. Neville-Neil.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 136617cf57SGeorge V. Neville-Neil.\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 146617cf57SGeorge V. Neville-Neil.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 156617cf57SGeorge V. Neville-Neil.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 166617cf57SGeorge V. Neville-Neil.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 176617cf57SGeorge V. Neville-Neil.\" PERFORMANCE OF THIS SOFTWARE. 186617cf57SGeorge V. Neville-Neil.\" 194a3c598fSFernando Apesteguía.Dd June 27, 2022 206617cf57SGeorge V. Neville-Neil.Dt GETADDRINFO 3 216617cf57SGeorge V. Neville-Neil.Os 226617cf57SGeorge V. Neville-Neil.Sh NAME 236617cf57SGeorge V. Neville-Neil.Nm getaddrinfo , 246617cf57SGeorge V. Neville-Neil.Nm freeaddrinfo 256617cf57SGeorge V. Neville-Neil.Nd socket address structure to host and service name 266617cf57SGeorge V. Neville-Neil.Sh SYNOPSIS 27580dbd65SJoel Dahl.In sys/types.h 28580dbd65SJoel Dahl.In sys/socket.h 29580dbd65SJoel Dahl.In netdb.h 306617cf57SGeorge V. Neville-Neil.Ft int 31f789cb82SRuslan Ermilov.Fo getaddrinfo 32f789cb82SRuslan Ermilov.Fa "const char *hostname" "const char *servname" 33f789cb82SRuslan Ermilov.Fa "const struct addrinfo *hints" "struct addrinfo **res" 34f789cb82SRuslan Ermilov.Fc 356617cf57SGeorge V. Neville-Neil.Ft void 366617cf57SGeorge V. Neville-Neil.Fn freeaddrinfo "struct addrinfo *ai" 376617cf57SGeorge V. Neville-Neil.Sh DESCRIPTION 386617cf57SGeorge V. Neville-NeilThe 396617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 406617cf57SGeorge V. Neville-Neilfunction is used to get a list of 416617cf57SGeorge V. Neville-Neiladdresses and port numbers for host 426617cf57SGeorge V. Neville-Neil.Fa hostname 436617cf57SGeorge V. Neville-Neiland service 446617cf57SGeorge V. Neville-Neil.Fa servname . 456617cf57SGeorge V. Neville-NeilIt is a replacement for and provides more flexibility than the 466617cf57SGeorge V. Neville-Neil.Xr gethostbyname 3 476617cf57SGeorge V. Neville-Neiland 486617cf57SGeorge V. Neville-Neil.Xr getservbyname 3 496617cf57SGeorge V. Neville-Neilfunctions. 506617cf57SGeorge V. Neville-Neil.Pp 516617cf57SGeorge V. Neville-NeilThe 526617cf57SGeorge V. Neville-Neil.Fa hostname 536617cf57SGeorge V. Neville-Neiland 546617cf57SGeorge V. Neville-Neil.Fa servname 556617cf57SGeorge V. Neville-Neilarguments are either pointers to NUL-terminated strings or the null pointer. 566617cf57SGeorge V. Neville-NeilAn acceptable value for 576617cf57SGeorge V. Neville-Neil.Fa hostname 586617cf57SGeorge V. Neville-Neilis either a valid host name or a numeric host address string consisting 59bb957021SHiroki Satoof a dotted decimal IPv4 address, 60bb957021SHiroki Satoan IPv6 address, 61bb957021SHiroki Satoor a UNIX-domain address. 626617cf57SGeorge V. Neville-NeilThe 636617cf57SGeorge V. Neville-Neil.Fa servname 646617cf57SGeorge V. Neville-Neilis either a decimal port number or a service name listed in 656617cf57SGeorge V. Neville-Neil.Xr services 5 . 666617cf57SGeorge V. Neville-NeilAt least one of 676617cf57SGeorge V. Neville-Neil.Fa hostname 686617cf57SGeorge V. Neville-Neiland 696617cf57SGeorge V. Neville-Neil.Fa servname 706617cf57SGeorge V. Neville-Neilmust be non-null. 716617cf57SGeorge V. Neville-Neil.Pp 726617cf57SGeorge V. Neville-Neil.Fa hints 736617cf57SGeorge V. Neville-Neilis an optional pointer to a 746617cf57SGeorge V. Neville-Neil.Li struct addrinfo , 756617cf57SGeorge V. Neville-Neilas defined by 766617cf57SGeorge V. Neville-Neil.Aq Pa netdb.h : 776617cf57SGeorge V. Neville-Neil.Bd -literal 786617cf57SGeorge V. Neville-Neilstruct addrinfo { 799b9a5278SSergey Kandaurov int ai_flags; /* AI_PASSIVE, AI_CANONNAME, .. */ 809b9a5278SSergey Kandaurov int ai_family; /* AF_xxx */ 819b9a5278SSergey Kandaurov int ai_socktype; /* SOCK_xxx */ 829b9a5278SSergey Kandaurov int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ 839b9a5278SSergey Kandaurov socklen_t ai_addrlen; /* length of ai_addr */ 849b9a5278SSergey Kandaurov char *ai_canonname; /* canonical name for hostname */ 859b9a5278SSergey Kandaurov struct sockaddr *ai_addr; /* binary address */ 869b9a5278SSergey Kandaurov struct addrinfo *ai_next; /* next structure in linked list */ 876617cf57SGeorge V. Neville-Neil}; 886617cf57SGeorge V. Neville-Neil.Ed 896617cf57SGeorge V. Neville-Neil.Pp 906617cf57SGeorge V. Neville-NeilThis structure can be used to provide hints concerning the type of socket 916617cf57SGeorge V. Neville-Neilthat the caller supports or wishes to use. 926617cf57SGeorge V. Neville-NeilThe caller can supply the following structure elements in 936617cf57SGeorge V. Neville-Neil.Fa hints : 946617cf57SGeorge V. Neville-Neil.Bl -tag -width "ai_socktypeXX" 956617cf57SGeorge V. Neville-Neil.It Fa ai_family 96b1f9559aSHajimu UMEMOTOThe address family that should be used. 976617cf57SGeorge V. Neville-NeilWhen 986617cf57SGeorge V. Neville-Neil.Fa ai_family 996617cf57SGeorge V. Neville-Neilis set to 100b1f9559aSHajimu UMEMOTO.Dv AF_UNSPEC , 101b1f9559aSHajimu UMEMOTOit means the caller will accept any address family supported by the 1026617cf57SGeorge V. Neville-Neiloperating system. 1036617cf57SGeorge V. Neville-Neil.It Fa ai_socktype 1046617cf57SGeorge V. Neville-NeilDenotes the type of socket that is wanted: 1056617cf57SGeorge V. Neville-Neil.Dv SOCK_STREAM , 1066617cf57SGeorge V. Neville-Neil.Dv SOCK_DGRAM , 107bb957021SHiroki Sato.Dv SOCK_SEQPACKET , 1086617cf57SGeorge V. Neville-Neilor 1096617cf57SGeorge V. Neville-Neil.Dv SOCK_RAW . 1106617cf57SGeorge V. Neville-NeilWhen 1116617cf57SGeorge V. Neville-Neil.Fa ai_socktype 1126617cf57SGeorge V. Neville-Neilis zero the caller will accept any socket type. 1136617cf57SGeorge V. Neville-Neil.It Fa ai_protocol 1146617cf57SGeorge V. Neville-NeilIndicates which transport protocol is desired, 115bb957021SHiroki Sato.Dv IPPROTO_UDP , 116bb957021SHiroki Sato.Dv IPPROTO_TCP , 117bb957021SHiroki Sato.Dv IPPROTO_SCTP , 1186617cf57SGeorge V. Neville-Neilor 119bb957021SHiroki Sato.Dv IPPROTO_UDPLITE . 1206617cf57SGeorge V. Neville-NeilIf 1216617cf57SGeorge V. Neville-Neil.Fa ai_protocol 1226617cf57SGeorge V. Neville-Neilis zero the caller will accept any protocol. 1236617cf57SGeorge V. Neville-Neil.It Fa ai_flags 124eb1db428SRemko LodderThe 1256617cf57SGeorge V. Neville-Neil.Fa ai_flags 126eb1db428SRemko Lodderfield to which the 127eb1db428SRemko Lodder.Fa hints 128eb1db428SRemko Lodderparameter points shall be set to zero 129eb1db428SRemko Lodderor be the bitwise-inclusive OR of one or more of the values 130eb1db428SRemko Lodder.Dv AI_ADDRCONFIG , 131b848a37dSHajimu UMEMOTO.Dv AI_ALL , 132eb1db428SRemko Lodder.Dv AI_CANONNAME , 133eb1db428SRemko Lodder.Dv AI_NUMERICHOST , 134b848a37dSHajimu UMEMOTO.Dv AI_NUMERICSERV , 135b848a37dSHajimu UMEMOTO.Dv AI_PASSIVE 136eb1db428SRemko Lodderand 137b848a37dSHajimu UMEMOTO.Dv AI_V4MAPPED . 138bb957021SHiroki SatoFor a UNIX-domain address, 139bb957021SHiroki Sato.Fa ai_flags 140bb957021SHiroki Satois ignored. 1416617cf57SGeorge V. Neville-Neil.Bl -tag -width "AI_CANONNAMEXX" 142eb1db428SRemko Lodder.It Dv AI_ADDRCONFIG 143eb1db428SRemko LodderIf the 144eb1db428SRemko Lodder.Dv AI_ADDRCONFIG 145eb1db428SRemko Lodderbit is set, IPv4 addresses shall be returned only if 146eb1db428SRemko Lodderan IPv4 address is configured on the local system, 147eb1db428SRemko Lodderand IPv6 addresses shall be returned only if 148eb1db428SRemko Lodderan IPv6 address is configured on the local system. 149b848a37dSHajimu UMEMOTO.It Dv AI_ALL 150b848a37dSHajimu UMEMOTOIf the 151b848a37dSHajimu UMEMOTO.Dv AI_ALL 152b848a37dSHajimu UMEMOTOflag is used with the 153b848a37dSHajimu UMEMOTO.Dv AI_V4MAPPED 154b848a37dSHajimu UMEMOTOflag, then 155b848a37dSHajimu UMEMOTO.Fn getaddrinfo 156b848a37dSHajimu UMEMOTOshall return all matching IPv6 and IPv4 addresses. 157b848a37dSHajimu UMEMOTO.Pp 158b848a37dSHajimu UMEMOTOFor example, when using the DNS, queries are made for both AAAA records and A records, and 159b848a37dSHajimu UMEMOTO.Fn getaddrinfo 160b848a37dSHajimu UMEMOTOreturns the combined results of both queries. 161b848a37dSHajimu UMEMOTOAny IPv4 addresses found are returned as IPv4-mapped IPv6 addresses. 162b848a37dSHajimu UMEMOTO.Pp 163b848a37dSHajimu UMEMOTOThe 164b848a37dSHajimu UMEMOTO.Dv AI_ALL 165b848a37dSHajimu UMEMOTOflag without the 166b848a37dSHajimu UMEMOTO.Dv AI_V4MAPPED 167b848a37dSHajimu UMEMOTOflag is ignored. 1686617cf57SGeorge V. Neville-Neil.It Dv AI_CANONNAME 1696617cf57SGeorge V. Neville-NeilIf the 1706617cf57SGeorge V. Neville-Neil.Dv AI_CANONNAME 1716617cf57SGeorge V. Neville-Neilbit is set, a successful call to 1726617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 1736617cf57SGeorge V. Neville-Neilwill return a NUL-terminated string containing the canonical name 1746617cf57SGeorge V. Neville-Neilof the specified hostname in the 1756617cf57SGeorge V. Neville-Neil.Fa ai_canonname 1766617cf57SGeorge V. Neville-Neilelement of the first 1776617cf57SGeorge V. Neville-Neil.Li addrinfo 1786617cf57SGeorge V. Neville-Neilstructure returned. 1796617cf57SGeorge V. Neville-Neil.It Dv AI_NUMERICHOST 1806617cf57SGeorge V. Neville-NeilIf the 1816617cf57SGeorge V. Neville-Neil.Dv AI_NUMERICHOST 1826617cf57SGeorge V. Neville-Neilbit is set, it indicates that 1836617cf57SGeorge V. Neville-Neil.Fa hostname 1846617cf57SGeorge V. Neville-Neilshould be treated as a numeric string defining an IPv4 or IPv6 address 1856617cf57SGeorge V. Neville-Neiland no name resolution should be attempted. 186eb1db428SRemko Lodder.It Dv AI_NUMERICSERV 187eb1db428SRemko LodderIf the 188eb1db428SRemko Lodder.Dv AI_NUMERICSERV 189eb1db428SRemko Lodderbit is set, 190eb1db428SRemko Lodderthen a non-null 191eb1db428SRemko Lodder.Fa servname 192eb1db428SRemko Lodderstring supplied shall be a numeric port string. 193eb1db428SRemko LodderOtherwise, an 194eb1db428SRemko Lodder.Dv EAI_NONAME 195eb1db428SRemko Loddererror shall be returned. 196eb1db428SRemko LodderThis bit shall prevent any type of name resolution service 197eb1db428SRemko Lodder(for example, NIS+) from being invoked. 1986617cf57SGeorge V. Neville-Neil.It Dv AI_PASSIVE 1996617cf57SGeorge V. Neville-NeilIf the 2006617cf57SGeorge V. Neville-Neil.Dv AI_PASSIVE 2016617cf57SGeorge V. Neville-Neilbit is set it indicates that the returned socket address structure 2026617cf57SGeorge V. Neville-Neilis intended for use in a call to 2036617cf57SGeorge V. Neville-Neil.Xr bind 2 . 2046617cf57SGeorge V. Neville-NeilIn this case, if the 2056617cf57SGeorge V. Neville-Neil.Fa hostname 2066617cf57SGeorge V. Neville-Neilargument is the null pointer, then the IP address portion of the 2076617cf57SGeorge V. Neville-Neilsocket address structure will be set to 2086617cf57SGeorge V. Neville-Neil.Dv INADDR_ANY 2096617cf57SGeorge V. Neville-Neilfor an IPv4 address or 2106617cf57SGeorge V. Neville-Neil.Dv IN6ADDR_ANY_INIT 2116617cf57SGeorge V. Neville-Neilfor an IPv6 address. 2126617cf57SGeorge V. Neville-Neil.Pp 2136617cf57SGeorge V. Neville-NeilIf the 2146617cf57SGeorge V. Neville-Neil.Dv AI_PASSIVE 2156617cf57SGeorge V. Neville-Neilbit is not set, the returned socket address structure will be ready 2166617cf57SGeorge V. Neville-Neilfor use in a call to 2176617cf57SGeorge V. Neville-Neil.Xr connect 2 2186617cf57SGeorge V. Neville-Neilfor a connection-oriented protocol or 2196617cf57SGeorge V. Neville-Neil.Xr connect 2 , 2206617cf57SGeorge V. Neville-Neil.Xr sendto 2 , 2216617cf57SGeorge V. Neville-Neilor 2226617cf57SGeorge V. Neville-Neil.Xr sendmsg 2 2236617cf57SGeorge V. Neville-Neilif a connectionless protocol was chosen. 2246617cf57SGeorge V. Neville-NeilThe 2256617cf57SGeorge V. Neville-Neil.Tn IP 2266617cf57SGeorge V. Neville-Neiladdress portion of the socket address structure will be set to the 2276617cf57SGeorge V. Neville-Neilloopback address if 2286617cf57SGeorge V. Neville-Neil.Fa hostname 2296617cf57SGeorge V. Neville-Neilis the null pointer and 2306617cf57SGeorge V. Neville-Neil.Dv AI_PASSIVE 2316617cf57SGeorge V. Neville-Neilis not set. 232b848a37dSHajimu UMEMOTO.It Dv AI_V4MAPPED 233b848a37dSHajimu UMEMOTOIf the 234b848a37dSHajimu UMEMOTO.Dv AI_V4MAPPED 235b848a37dSHajimu UMEMOTOflag is specified along with an ai_family of 236b848a37dSHajimu UMEMOTO.Dv AF_INET6 , 237b848a37dSHajimu UMEMOTOthen 238b848a37dSHajimu UMEMOTO.Fn getaddrinfo 239b848a37dSHajimu UMEMOTOshall return IPv4-mapped IPv6 addresses on finding no matching IPv6 addresses ( 240b848a37dSHajimu UMEMOTO.Fa ai_addrlen 241b848a37dSHajimu UMEMOTOshall be 16). 242b848a37dSHajimu UMEMOTO.Pp 243b848a37dSHajimu UMEMOTOFor example, when using the DNS, if no AAAA records are found then a query is made for A records and any found are returned as IPv4-mapped IPv6 addresses. 244b848a37dSHajimu UMEMOTO.Pp 245b848a37dSHajimu UMEMOTOThe 246b848a37dSHajimu UMEMOTO.Dv AI_V4MAPPED 247b848a37dSHajimu UMEMOTOflag shall be ignored unless 248b848a37dSHajimu UMEMOTO.Fa ai_family 249b848a37dSHajimu UMEMOTOequals 250b848a37dSHajimu UMEMOTO.Dv AF_INET6 . 2516617cf57SGeorge V. Neville-Neil.El 2526617cf57SGeorge V. Neville-Neil.El 2536617cf57SGeorge V. Neville-Neil.Pp 2546617cf57SGeorge V. Neville-NeilAll other elements of the 2556617cf57SGeorge V. Neville-Neil.Li addrinfo 2566617cf57SGeorge V. Neville-Neilstructure passed via 2576617cf57SGeorge V. Neville-Neil.Fa hints 2586617cf57SGeorge V. Neville-Neilmust be zero or the null pointer. 2596617cf57SGeorge V. Neville-Neil.Pp 2606617cf57SGeorge V. Neville-NeilIf 2616617cf57SGeorge V. Neville-Neil.Fa hints 2626617cf57SGeorge V. Neville-Neilis the null pointer, 2636617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 2646617cf57SGeorge V. Neville-Neilbehaves as if the caller provided a 2656617cf57SGeorge V. Neville-Neil.Li struct addrinfo 2666617cf57SGeorge V. Neville-Neilwith 2676617cf57SGeorge V. Neville-Neil.Fa ai_family 2686617cf57SGeorge V. Neville-Neilset to 269b1f9559aSHajimu UMEMOTO.Dv AF_UNSPEC 2706617cf57SGeorge V. Neville-Neiland all other elements set to zero or 2716617cf57SGeorge V. Neville-Neil.Dv NULL . 2726617cf57SGeorge V. Neville-Neil.Pp 2736617cf57SGeorge V. Neville-NeilAfter a successful call to 2746617cf57SGeorge V. Neville-Neil.Fn getaddrinfo , 2756617cf57SGeorge V. Neville-Neil.Fa *res 2766617cf57SGeorge V. Neville-Neilis a pointer to a linked list of one or more 2776617cf57SGeorge V. Neville-Neil.Li addrinfo 2786617cf57SGeorge V. Neville-Neilstructures. 2796617cf57SGeorge V. Neville-NeilThe list can be traversed by following the 2806617cf57SGeorge V. Neville-Neil.Fa ai_next 2816617cf57SGeorge V. Neville-Neilpointer in each 2826617cf57SGeorge V. Neville-Neil.Li addrinfo 2836617cf57SGeorge V. Neville-Neilstructure until a null pointer is encountered. 284816c67baSJason HelfmanEach returned 285816c67baSJason Helfman.Li addrinfo 286816c67baSJason Helfmanstructure contains three members that are suitable for a call to 287816c67baSJason Helfman.Xr socket 2 : 2886617cf57SGeorge V. Neville-Neil.Fa ai_family , 2896617cf57SGeorge V. Neville-Neil.Fa ai_socktype , 2906617cf57SGeorge V. Neville-Neiland 291816c67baSJason Helfman.Fa ai_protocol . 2926617cf57SGeorge V. Neville-NeilFor each 2936617cf57SGeorge V. Neville-Neil.Li addrinfo 2946617cf57SGeorge V. Neville-Neilstructure in the list, the 2956617cf57SGeorge V. Neville-Neil.Fa ai_addr 2966617cf57SGeorge V. Neville-Neilmember points to a filled-in socket address structure of length 2976617cf57SGeorge V. Neville-Neil.Fa ai_addrlen . 2986617cf57SGeorge V. Neville-Neil.Pp 2996617cf57SGeorge V. Neville-NeilThis implementation of 3006617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 3016617cf57SGeorge V. Neville-Neilallows numeric IPv6 address notation with scope identifier, 3024e4f91b8SHiroki Satoas documented in chapter 11 of RFC 4007. 3036617cf57SGeorge V. Neville-NeilBy appending the percent character and scope identifier to addresses, 3046617cf57SGeorge V. Neville-Neilone can fill the 3056617cf57SGeorge V. Neville-Neil.Li sin6_scope_id 3066617cf57SGeorge V. Neville-Neilfield for addresses. 3076617cf57SGeorge V. Neville-NeilThis would make management of scoped addresses easier 3086617cf57SGeorge V. Neville-Neiland allows cut-and-paste input of scoped addresses. 3096617cf57SGeorge V. Neville-Neil.Pp 3106617cf57SGeorge V. Neville-NeilAt this moment the code supports only link-local addresses with the format. 3116617cf57SGeorge V. Neville-NeilThe scope identifier is hardcoded to the name of the hardware interface 3126617cf57SGeorge V. Neville-Neilassociated 3136617cf57SGeorge V. Neville-Neilwith the link 3146617cf57SGeorge V. Neville-Neil.Po 3156617cf57SGeorge V. Neville-Neilsuch as 3166617cf57SGeorge V. Neville-Neil.Li ne0 3176617cf57SGeorge V. Neville-Neil.Pc . 3186617cf57SGeorge V. Neville-NeilAn example is 3196617cf57SGeorge V. Neville-Neil.Dq Li fe80::1%ne0 , 3206617cf57SGeorge V. Neville-Neilwhich means 3216617cf57SGeorge V. Neville-Neil.Do 3226617cf57SGeorge V. Neville-Neil.Li fe80::1 3236617cf57SGeorge V. Neville-Neilon the link associated with the 3246617cf57SGeorge V. Neville-Neil.Li ne0 3256617cf57SGeorge V. Neville-Neilinterface 3266617cf57SGeorge V. Neville-Neil.Dc . 3276617cf57SGeorge V. Neville-Neil.Pp 3286617cf57SGeorge V. Neville-NeilThe current implementation assumes a one-to-one relationship between 3296617cf57SGeorge V. Neville-Neilthe interface and link, which is not necessarily true from the specification. 3306617cf57SGeorge V. Neville-Neil.Pp 3316617cf57SGeorge V. Neville-NeilAll of the information returned by 3326617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 3336617cf57SGeorge V. Neville-Neilis dynamically allocated: the 3346617cf57SGeorge V. Neville-Neil.Li addrinfo 3356617cf57SGeorge V. Neville-Neilstructures themselves as well as the socket address structures and 3366617cf57SGeorge V. Neville-Neilthe canonical host name strings included in the 3376617cf57SGeorge V. Neville-Neil.Li addrinfo 3386617cf57SGeorge V. Neville-Neilstructures. 3396617cf57SGeorge V. Neville-Neil.Pp 3406617cf57SGeorge V. Neville-NeilMemory allocated for the dynamically allocated structures created by 3416617cf57SGeorge V. Neville-Neila successful call to 3426617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 3436617cf57SGeorge V. Neville-Neilis released by the 3446617cf57SGeorge V. Neville-Neil.Fn freeaddrinfo 3456617cf57SGeorge V. Neville-Neilfunction. 3466617cf57SGeorge V. Neville-NeilThe 3476617cf57SGeorge V. Neville-Neil.Fa ai 3486617cf57SGeorge V. Neville-Neilpointer should be a 3496617cf57SGeorge V. Neville-Neil.Li addrinfo 3506617cf57SGeorge V. Neville-Neilstructure created by a call to 3516617cf57SGeorge V. Neville-Neil.Fn getaddrinfo . 352587e285eSKonstantin Belousov.Sh IMPLEMENTATION NOTES 353587e285eSKonstantin BelousovThe behavior of 354acc3f068SMaxim Konovalov.Li freeaddrinfo(NULL) 355587e285eSKonstantin Belousovis left unspecified by both 356587e285eSKonstantin Belousov.St -susv4 357587e285eSKonstantin Belousovand 358587e285eSKonstantin Belousov.Dv "RFC 3493" . 359587e285eSKonstantin BelousovThe current implementation ignores a 360587e285eSKonstantin Belousov.Dv NULL 361587e285eSKonstantin Belousovargument for compatibility with programs that rely on the implementation 362587e285eSKonstantin Belousovdetails of other operating systems. 3636617cf57SGeorge V. Neville-Neil.Sh RETURN VALUES 3646617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 3656617cf57SGeorge V. Neville-Neilreturns zero on success or one of the error codes listed in 3666617cf57SGeorge V. Neville-Neil.Xr gai_strerror 3 3676617cf57SGeorge V. Neville-Neilif an error occurs. 3686617cf57SGeorge V. Neville-Neil.Sh EXAMPLES 3696617cf57SGeorge V. Neville-NeilThe following code tries to connect to 3706617cf57SGeorge V. Neville-Neil.Dq Li www.kame.net 3716617cf57SGeorge V. Neville-Neilservice 3726617cf57SGeorge V. Neville-Neil.Dq Li http 3736617cf57SGeorge V. Neville-Neilvia a stream socket. 3746617cf57SGeorge V. Neville-NeilIt loops through all the addresses available, regardless of address family. 3756617cf57SGeorge V. Neville-NeilIf the destination resolves to an IPv4 address, it will use an 3766617cf57SGeorge V. Neville-Neil.Dv AF_INET 3776617cf57SGeorge V. Neville-Neilsocket. 3786617cf57SGeorge V. Neville-NeilSimilarly, if it resolves to IPv6, an 3796617cf57SGeorge V. Neville-Neil.Dv AF_INET6 3806617cf57SGeorge V. Neville-Neilsocket is used. 3816617cf57SGeorge V. Neville-NeilObserve that there is no hardcoded reference to a particular address family. 3826617cf57SGeorge V. Neville-NeilThe code works even if 3836617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 3846617cf57SGeorge V. Neville-Neilreturns addresses that are not IPv4/v6. 3856617cf57SGeorge V. Neville-Neil.Bd -literal -offset indent 3866617cf57SGeorge V. Neville-Neilstruct addrinfo hints, *res, *res0; 3876617cf57SGeorge V. Neville-Neilint error; 3886617cf57SGeorge V. Neville-Neilint s; 3896617cf57SGeorge V. Neville-Neilconst char *cause = NULL; 3906617cf57SGeorge V. Neville-Neil 3916617cf57SGeorge V. Neville-Neilmemset(&hints, 0, sizeof(hints)); 392b1f9559aSHajimu UMEMOTOhints.ai_family = AF_UNSPEC; 3936617cf57SGeorge V. Neville-Neilhints.ai_socktype = SOCK_STREAM; 3946617cf57SGeorge V. Neville-Neilerror = getaddrinfo("www.kame.net", "http", &hints, &res0); 3956617cf57SGeorge V. Neville-Neilif (error) { 3966617cf57SGeorge V. Neville-Neil errx(1, "%s", gai_strerror(error)); 3976617cf57SGeorge V. Neville-Neil /* NOTREACHED */ 3986617cf57SGeorge V. Neville-Neil} 3996617cf57SGeorge V. Neville-Neils = -1; 4006617cf57SGeorge V. Neville-Neilfor (res = res0; res; res = res->ai_next) { 4016617cf57SGeorge V. Neville-Neil s = socket(res->ai_family, res->ai_socktype, 4026617cf57SGeorge V. Neville-Neil res->ai_protocol); 4036617cf57SGeorge V. Neville-Neil if (s < 0) { 4046617cf57SGeorge V. Neville-Neil cause = "socket"; 4056617cf57SGeorge V. Neville-Neil continue; 4066617cf57SGeorge V. Neville-Neil } 4076617cf57SGeorge V. Neville-Neil 4086617cf57SGeorge V. Neville-Neil if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { 4096617cf57SGeorge V. Neville-Neil cause = "connect"; 4106617cf57SGeorge V. Neville-Neil close(s); 4116617cf57SGeorge V. Neville-Neil s = -1; 4126617cf57SGeorge V. Neville-Neil continue; 4136617cf57SGeorge V. Neville-Neil } 4146617cf57SGeorge V. Neville-Neil 4156617cf57SGeorge V. Neville-Neil break; /* okay we got one */ 4166617cf57SGeorge V. Neville-Neil} 4176617cf57SGeorge V. Neville-Neilif (s < 0) { 4186617cf57SGeorge V. Neville-Neil err(1, "%s", cause); 4196617cf57SGeorge V. Neville-Neil /* NOTREACHED */ 4206617cf57SGeorge V. Neville-Neil} 4216617cf57SGeorge V. Neville-Neilfreeaddrinfo(res0); 4226617cf57SGeorge V. Neville-Neil.Ed 4236617cf57SGeorge V. Neville-Neil.Pp 4246617cf57SGeorge V. Neville-NeilThe following example tries to open a wildcard listening socket onto service 4256617cf57SGeorge V. Neville-Neil.Dq Li http , 4266617cf57SGeorge V. Neville-Neilfor all the address families available. 4276617cf57SGeorge V. Neville-Neil.Bd -literal -offset indent 4286617cf57SGeorge V. Neville-Neilstruct addrinfo hints, *res, *res0; 4296617cf57SGeorge V. Neville-Neilint error; 4306617cf57SGeorge V. Neville-Neilint s[MAXSOCK]; 4316617cf57SGeorge V. Neville-Neilint nsock; 4326617cf57SGeorge V. Neville-Neilconst char *cause = NULL; 4336617cf57SGeorge V. Neville-Neil 4346617cf57SGeorge V. Neville-Neilmemset(&hints, 0, sizeof(hints)); 435b1f9559aSHajimu UMEMOTOhints.ai_family = AF_UNSPEC; 4366617cf57SGeorge V. Neville-Neilhints.ai_socktype = SOCK_STREAM; 4376617cf57SGeorge V. Neville-Neilhints.ai_flags = AI_PASSIVE; 4386617cf57SGeorge V. Neville-Neilerror = getaddrinfo(NULL, "http", &hints, &res0); 4396617cf57SGeorge V. Neville-Neilif (error) { 4406617cf57SGeorge V. Neville-Neil errx(1, "%s", gai_strerror(error)); 4416617cf57SGeorge V. Neville-Neil /* NOTREACHED */ 4426617cf57SGeorge V. Neville-Neil} 4436617cf57SGeorge V. Neville-Neilnsock = 0; 4446617cf57SGeorge V. Neville-Neilfor (res = res0; res && nsock < MAXSOCK; res = res->ai_next) { 4456617cf57SGeorge V. Neville-Neil s[nsock] = socket(res->ai_family, res->ai_socktype, 4466617cf57SGeorge V. Neville-Neil res->ai_protocol); 4476617cf57SGeorge V. Neville-Neil if (s[nsock] < 0) { 4486617cf57SGeorge V. Neville-Neil cause = "socket"; 4496617cf57SGeorge V. Neville-Neil continue; 4506617cf57SGeorge V. Neville-Neil } 4516617cf57SGeorge V. Neville-Neil 4526617cf57SGeorge V. Neville-Neil if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) { 4536617cf57SGeorge V. Neville-Neil cause = "bind"; 4546617cf57SGeorge V. Neville-Neil close(s[nsock]); 4556617cf57SGeorge V. Neville-Neil continue; 4566617cf57SGeorge V. Neville-Neil } 4576617cf57SGeorge V. Neville-Neil (void) listen(s[nsock], 5); 4586617cf57SGeorge V. Neville-Neil 4596617cf57SGeorge V. Neville-Neil nsock++; 4606617cf57SGeorge V. Neville-Neil} 4616617cf57SGeorge V. Neville-Neilif (nsock == 0) { 4626617cf57SGeorge V. Neville-Neil err(1, "%s", cause); 4636617cf57SGeorge V. Neville-Neil /* NOTREACHED */ 4646617cf57SGeorge V. Neville-Neil} 4656617cf57SGeorge V. Neville-Neilfreeaddrinfo(res0); 4666617cf57SGeorge V. Neville-Neil.Ed 4676617cf57SGeorge V. Neville-Neil.Sh SEE ALSO 4686617cf57SGeorge V. Neville-Neil.Xr bind 2 , 4696617cf57SGeorge V. Neville-Neil.Xr connect 2 , 4706617cf57SGeorge V. Neville-Neil.Xr send 2 , 4716617cf57SGeorge V. Neville-Neil.Xr socket 2 , 4726617cf57SGeorge V. Neville-Neil.Xr gai_strerror 3 , 4736617cf57SGeorge V. Neville-Neil.Xr gethostbyname 3 , 4746617cf57SGeorge V. Neville-Neil.Xr getnameinfo 3 , 4756617cf57SGeorge V. Neville-Neil.Xr getservbyname 3 , 4766617cf57SGeorge V. Neville-Neil.Xr resolver 3 , 477bb957021SHiroki Sato.Xr inet 4 , 478bb957021SHiroki Sato.Xr inet6 4 , 479bb957021SHiroki Sato.Xr unix 4 , 4806617cf57SGeorge V. Neville-Neil.Xr hosts 5 , 4816617cf57SGeorge V. Neville-Neil.Xr resolv.conf 5 , 4826617cf57SGeorge V. Neville-Neil.Xr services 5 , 483*9cb98ab7SJose Luis Duran.Xr hostname 7 , 484*9cb98ab7SJose Luis Duran.Xr ip6addrctl 8 4856617cf57SGeorge V. Neville-Neil.Rs 4866617cf57SGeorge V. Neville-Neil.%A R. Gilligan 4876617cf57SGeorge V. Neville-Neil.%A S. Thomson 4886617cf57SGeorge V. Neville-Neil.%A J. Bound 4896617cf57SGeorge V. Neville-Neil.%A J. McCann 4906617cf57SGeorge V. Neville-Neil.%A W. Stevens 4916617cf57SGeorge V. Neville-Neil.%T Basic Socket Interface Extensions for IPv6 4926617cf57SGeorge V. Neville-Neil.%R RFC 3493 4936617cf57SGeorge V. Neville-Neil.%D February 2003 4946617cf57SGeorge V. Neville-Neil.Re 4956617cf57SGeorge V. Neville-Neil.Rs 4966617cf57SGeorge V. Neville-Neil.%A S. Deering 4976617cf57SGeorge V. Neville-Neil.%A B. Haberman 4986617cf57SGeorge V. Neville-Neil.%A T. Jinmei 4996617cf57SGeorge V. Neville-Neil.%A E. Nordmark 5006617cf57SGeorge V. Neville-Neil.%A B. Zill 5016617cf57SGeorge V. Neville-Neil.%T "IPv6 Scoped Address Architecture" 5024e4f91b8SHiroki Sato.%R RFC 4007 5034e4f91b8SHiroki Sato.%D March 2005 5046617cf57SGeorge V. Neville-Neil.Re 5056617cf57SGeorge V. Neville-Neil.Rs 5066617cf57SGeorge V. Neville-Neil.%A Craig Metz 5076617cf57SGeorge V. Neville-Neil.%T Protocol Independence Using the Sockets API 5086617cf57SGeorge V. Neville-Neil.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference" 5096617cf57SGeorge V. Neville-Neil.%D June 2000 5106617cf57SGeorge V. Neville-Neil.Re 5116617cf57SGeorge V. Neville-Neil.Sh STANDARDS 5126617cf57SGeorge V. Neville-NeilThe 5136617cf57SGeorge V. Neville-Neil.Fn getaddrinfo 5146617cf57SGeorge V. Neville-Neilfunction is defined by the 515eb1db428SRemko Lodder.St -p1003.1-2004 516eb1db428SRemko Lodderspecification and documented in 5176617cf57SGeorge V. Neville-Neil.Dv "RFC 3493" , 5186617cf57SGeorge V. Neville-Neil.Dq Basic Socket Interface Extensions for IPv6 . 519