559:
into text strings consisting of the hostname or, if the address cannot be resolved into a name, a textual IP address representation, as well as the service port name or number. The function prototype is specified as follows:
389:
can be either a domain name, such as "example.com", an address string, such as "127.0.0.1", or NULL, in which case the address 0.0.0.0 or 127.0.0.1 is assigned depending on the hints flags.
1301:
Stevens R., Fenner, Rudoff UNIX® Network
Programming Volume 1, Third Edition: The Sockets Networking API. Publisher: Addison-Wesley Professional. Pub. Date: November 14, 2003 p. 256
81:. It is the recommended interface for name resolution in building protocol independent applications and for transitioning legacy IPv4 code to the IPv6 Internet.
423:
structure with the information requested after successful completion of the function. The function returns 0 upon success and non-zero error value if it fails.
495:
value while in others it is the opposite, so both need to be defined with equivalent values for the code to be work across multiple platforms.
1311:
395:
can be a port number passed as string, such as "80", or a service name, e.g. "echo". In the latter case a typical implementation uses
67:
1323:
427:
Although implementations vary among platforms, the function first attempts to obtain a port number usually by branching on
679:
name is returned. In this example, the domain name is printed three times, once for each of the three results obtained.
1374:
1369:
73:
getaddrinfo and getnameinfo are inverse functions of each other. They are network protocol agnostic, and support both
32:
1364:
300:
1379:
105:
28:
1340:
1259:
1312:
https://opensource.apple.com/source/passwordserver_sasl/passwordserver_sasl-14/cyrus_sasl/lib/getaddrinfo.c
108:
data structure used to represent addresses and hostnames within the networking API is the following:
671:
on each result to return the canonical name for the address. In general, this produces the original
89:
92:
is commonly used on Unix-like systems and affects most implementation of this pair as it did with
479:
in one of these conditions and also adds the port retrieved at the beginning to it. Finally, the
306:
of struct addrinfo structures. The function prototype for this function is specified as follows:
85:
1284:
507:. As the result of the latter is a linked list of addrinfo structures starting at the address
93:
59:
555:
converts the internal binary representation of an IP address in the form of a pointer to a
1236:
1224:
1358:
84:
Internally, the functions may use a variety of resolution methods not limited to the
279:
field. These are set to the same value when the structure is created with function
58:
between human-readable text representations and structured binary formats for the
1349:
303:
47:
296:
55:
487:
structure. In some implementations, such as the Unix version for Mac OS, the
253:
17:
431:. If the string value is a number, it converts it to an integer and calls
672:
292:
51:
1264:
The Open Group Base
Specifications Issue 7, 2018 edition (POSIX.1-2017)
1227:
technique, that may allow execution of arbitrary code by the attacker.
675:, unless the particular address has multiple names, in which case the
1220:
63:
483:
parameter is dereferenced to make it point to a newly allocated
291:
getaddrinfo() converts human-readable text strings representing
78:
74:
1324:"CVE-2015-7547: Glibc getaddrinfo stack-based buffer overflow"
252:
structure. If the types are incompatible, e.g., on a 64-bit
1341:
freeaddrinfo and getaddrinfo specifications in POSIX.1-2017
1219:
On
February 16, 2016, a security bug was announced in the
941:/* loop over all returned results and do inverse lookup */
1345:
62:'s networking API. Both functions are contained in the
780:/* resolve the domain name into a list of addresses */
1260:"freeaddrinfo, getaddrinfo - get address information"
1254:
1252:
503:
This function frees the memory allocated by function
1350:
RFC 3493, Basic Socket
Interface Extensions for IPv6
515:loops through the list and frees each one in turn.
447:as the second parameter to that function. Then, if
244:and programmers often pass the address to the
1310:Hajimu UMEMOTO getaddrinfo.c Accessed from:
413:structure with the type of service requested.
264:is 4 bytes, then run-time errors may result.
8:
667:into its list of addresses and then calls
1285:"nss - man pages section 5: File Formats"
1223:implementation of getaddrinfo(), using a
403:to resolve the service to a port number.
212:/* this struct can form a linked list */
1248:
475:structure filled with the appropriate
455:resolves it, or otherwise the address
240:, require the parameter to have type
7:
435:. If it is a service name, such as
443:, using the protocol derived from
419:is a pointer that points to a new
267:The structure contains structures
220:In some older systems the type of
25:
232:. Most socket functions, such as
94:their BSD-socket era predecessors
68:application programming interface
439:, the service is looked up with
451:is given (not NULL), a call to
1105:"error in getnameinfo: %s
899:"error in getaddrinfo: %s
471:otherwise. It allocated a new
1:
796:"www.example.com"
663:to resolve the domain name
659:The following example uses
1396:
717:#define NI_MAXHOST 1025
409:can be either NULL or an
283:in some implementations.
681:
562:
517:
308:
110:
872:"getaddrinfo"
489:hints->ai_protocol
493:hints->ai_socktype
445:hints->ai_socktype
301:dynamically allocated
714:#ifndef NI_MAXHOST
711:<sys/socket.h>
705:<netinet/in.h>
194:/* canonical name */
90:Name Service Switch
1375:Domain Name System
1370:Network addressing
1165:"hostname: %s
461:hints->ai_flags
399:to query the file
86:Domain Name System
1365:Internet Protocol
16:(Redirected from
1387:
1328:
1327:
1320:
1314:
1308:
1302:
1299:
1293:
1292:
1281:
1275:
1274:
1272:
1271:
1266:. The Open Group
1256:
1210:
1207:
1204:
1201:
1198:
1197:
1194:
1191:
1188:
1184:
1181:
1178:
1175:
1172:
1169:
1166:
1163:
1160:
1157:
1154:
1151:
1148:
1145:
1142:
1139:
1136:
1133:
1130:
1127:
1124:
1121:
1118:
1115:
1112:
1109:
1106:
1103:
1100:
1097:
1094:
1091:
1088:
1085:
1082:
1079:
1076:
1073:
1070:
1069:
1066:
1063:
1060:
1057:
1054:
1051:
1048:
1045:
1042:
1039:
1036:
1033:
1030:
1027:
1024:
1021:
1018:
1015:
1012:
1009:
1006:
1002:
999:
996:
993:
990:
987:
984:
981:
978:
975:
972:
969:
966:
963:
960:
957:
954:
951:
948:
945:
942:
939:
936:
933:
930:
927:
924:
921:
918:
915:
912:
909:
906:
903:
900:
897:
894:
891:
888:
885:
882:
879:
876:
873:
870:
867:
864:
861:
858:
855:
852:
849:
846:
843:
840:
837:
834:
831:
828:
825:
822:
821:
818:
815:
812:
809:
806:
803:
800:
797:
794:
791:
788:
785:
781:
778:
775:
772:
769:
766:
763:
760:
757:
754:
751:
748:
745:
742:
739:
736:
733:
730:
727:
724:
721:
718:
715:
712:
709:
706:
703:
700:
697:
694:
693:<stdlib.h>
691:
688:
685:
650:
647:
644:
641:
638:
635:
632:
629:
626:
623:
620:
617:
614:
611:
608:
605:
602:
599:
596:
593:
590:
587:
584:
581:
578:
575:
572:
569:
566:
542:
539:
536:
533:
530:
527:
524:
521:
381:
378:
375:
372:
369:
366:
363:
360:
357:
354:
351:
348:
345:
342:
339:
336:
333:
330:
327:
324:
321:
318:
315:
312:
216:
213:
210:
207:
204:
201:
198:
195:
192:
189:
186:
183:
180:
177:
174:
171:
168:
165:
162:
159:
156:
153:
150:
147:
144:
141:
138:
135:
132:
129:
126:
123:
120:
117:
114:
60:operating system
21:
1395:
1394:
1390:
1389:
1388:
1386:
1385:
1384:
1380:C POSIX library
1355:
1354:
1337:
1332:
1331:
1322:
1321:
1317:
1309:
1305:
1300:
1296:
1289:docs.oracle.com
1283:
1282:
1278:
1269:
1267:
1258:
1257:
1250:
1245:
1237:Network address
1233:
1225:buffer overflow
1217:
1212:
1211:
1208:
1205:
1202:
1199:
1195:
1192:
1189:
1186:
1185:
1182:
1179:
1176:
1173:
1170:
1167:
1164:
1161:
1158:
1155:
1152:
1149:
1146:
1143:
1140:
1137:
1134:
1131:
1128:
1125:
1122:
1119:
1116:
1113:
1110:
1107:
1104:
1101:
1098:
1095:
1092:
1089:
1086:
1083:
1080:
1077:
1074:
1071:
1067:
1064:
1061:
1058:
1055:
1052:
1049:
1046:
1043:
1040:
1037:
1034:
1031:
1028:
1025:
1022:
1019:
1016:
1013:
1010:
1007:
1004:
1003:
1000:
997:
994:
991:
988:
985:
982:
979:
976:
973:
970:
967:
964:
961:
958:
955:
952:
949:
946:
943:
940:
937:
934:
931:
928:
925:
922:
919:
916:
913:
910:
907:
904:
901:
898:
895:
892:
889:
886:
883:
880:
877:
874:
871:
868:
865:
862:
859:
856:
853:
850:
847:
844:
841:
838:
835:
832:
829:
826:
823:
819:
816:
813:
810:
807:
804:
801:
798:
795:
792:
789:
786:
783:
782:
779:
776:
773:
770:
767:
764:
761:
758:
755:
752:
749:
746:
743:
740:
737:
734:
731:
728:
725:
722:
719:
716:
713:
710:
707:
704:
701:
699:<netdb.h>
698:
695:
692:
689:
687:<stdio.h>
686:
683:
665:www.example.com
657:
652:
651:
648:
645:
642:
639:
636:
633:
630:
627:
624:
621:
618:
615:
612:
609:
606:
603:
600:
597:
594:
591:
588:
585:
582:
579:
576:
573:
570:
567:
564:
557:struct sockaddr
549:
544:
543:
540:
537:
534:
531:
528:
525:
522:
519:
501:
453:gethostbyname()
441:getservbyname()
397:getservbyname()
383:
382:
379:
376:
373:
370:
367:
364:
361:
358:
355:
352:
349:
346:
343:
340:
337:
334:
331:
328:
325:
322:
319:
316:
313:
310:
289:
260:is 8 bytes and
248:element of the
218:
217:
214:
211:
208:
205:
202:
199:
196:
193:
190:
187:
184:
181:
178:
175:
172:
169:
166:
163:
160:
157:
154:
151:
148:
145:
142:
139:
136:
133:
130:
127:
124:
121:
118:
115:
112:
102:
100:struct addrinfo
23:
22:
15:
12:
11:
5:
1393:
1391:
1383:
1382:
1377:
1372:
1367:
1357:
1356:
1353:
1352:
1347:
1336:
1335:External links
1333:
1330:
1329:
1315:
1303:
1294:
1276:
1247:
1246:
1244:
1241:
1240:
1239:
1232:
1229:
1216:
1213:
682:
656:
653:
563:
548:
545:
518:
513:freeaddrinfo()
500:
499:freeaddrinfo()
497:
491:overrides the
425:
424:
417:
414:
407:
404:
393:
390:
387:
309:
288:
285:
111:
101:
98:
24:
14:
13:
10:
9:
6:
4:
3:
2:
1392:
1381:
1378:
1376:
1373:
1371:
1368:
1366:
1363:
1362:
1360:
1351:
1348:
1346:
1342:
1339:
1338:
1334:
1325:
1319:
1316:
1313:
1307:
1304:
1298:
1295:
1290:
1286:
1280:
1277:
1265:
1261:
1255:
1253:
1249:
1242:
1238:
1235:
1234:
1230:
1228:
1226:
1222:
1214:
680:
678:
674:
670:
669:getnameinfo()
666:
662:
661:getaddrinfo()
654:
561:
558:
554:
553:getnameinfo()
551:The function
547:getnameinfo()
546:
516:
514:
510:
506:
505:getaddrinfo()
498:
496:
494:
490:
486:
482:
478:
474:
470:
466:
462:
458:
454:
450:
446:
442:
438:
434:
430:
422:
418:
415:
412:
408:
405:
402:
401:/etc/services
398:
394:
391:
388:
385:
384:
307:
305:
302:
298:
294:
287:getaddrinfo()
286:
284:
282:
278:
275:with its own
274:
270:
265:
263:
259:
256:system where
255:
251:
247:
243:
239:
238:getpeername()
235:
231:
227:
223:
109:
107:
99:
97:
95:
91:
87:
82:
80:
76:
71:
69:
65:
61:
57:
53:
49:
45:
44:
43:getnameinfo()
39:
38:
37:getaddrinfo()
34:
30:
29:C programming
19:
1344:
1318:
1306:
1297:
1288:
1279:
1268:. Retrieved
1263:
1218:
1187:freeaddrinfo
1153:'\0'
1117:gai_strerror
932:EXIT_FAILURE
911:gai_strerror
676:
668:
664:
660:
658:
556:
552:
550:
523:freeaddrinfo
512:
508:
504:
502:
492:
488:
484:
480:
476:
472:
468:
464:
460:
459:is used, if
456:
452:
448:
444:
440:
436:
432:
428:
426:
420:
410:
400:
396:
297:IP addresses
290:
280:
276:
272:
268:
266:
261:
257:
249:
245:
242:socklen_t *
241:
237:
233:
229:
225:
221:
219:
188:ai_canonname
103:
83:
72:
56:IP addresses
48:domain names
42:
41:
36:
35:
26:
1011:getnameinfo
790:getaddrinfo
568:getnameinfo
477:sockaddr_in
314:getaddrinfo
304:linked list
281:getaddrinfo
228:instead of
152:ai_protocol
143:ai_socktype
88:(DNS). The
18:Getnameinfo
1359:Categories
1270:2022-03-05
1243:References
1047:NI_MAXHOST
1035:ai_addrlen
857:EAI_SYSTEM
465:AI_PASSIVE
463:is set to
246:ai_addrlen
222:ai_addrlen
161:ai_addrlen
677:canonical
592:socklen_t
469:127.0.0.1
293:hostnames
277:sa_family
269:ai_family
262:socklen_t
254:Solaris 9
230:socklen_t
158:socklen_t
134:ai_family
66:standard
52:hostnames
33:functions
1231:See also
1215:Security
1177:hostname
1147:hostname
1129:continue
1041:hostname
998:hostname
759:addrinfo
744:addrinfo
708:#include
702:#include
696:#include
690:#include
684:#include
673:hostname
580:sockaddr
532:addrinfo
485:addrinfo
473:addrinfo
449:hostname
421:addrinfo
411:addrinfo
386:hostname
371:addrinfo
356:addrinfo
329:hostname
273:sockaddr
250:addrinfo
234:accept()
200:addrinfo
170:sockaddr
125:ai_flags
116:addrinfo
46:convert
1093:fprintf
1023:ai_addr
986:ai_next
887:fprintf
655:Example
637:servlen
616:hostlen
457:0.0.0.0
433:htons()
429:service
392:service
344:service
299:into a
206:ai_next
176:ai_addr
70:(API).
31:, the
1200:return
1193:result
1171:"
1159:printf
1111:"
1099:stderr
956:result
905:"
893:stderr
866:perror
817:result
756:struct
750:result
741:struct
720:#endif
634:size_t
613:size_t
577:struct
529:struct
467:, and
368:struct
353:struct
258:size_t
226:size_t
197:struct
167:struct
113:struct
54:, and
1221:glibc
1123:error
1078:error
1032:->
1020:->
1005:error
983:->
917:error
851:error
830:error
814:&
784:error
774:error
646:flags
595:salen
574:const
481:**res
406:hints
362:hints
350:const
335:const
320:const
64:POSIX
1053:NULL
995:char
968:NULL
926:exit
881:else
808:NULL
802:NULL
732:void
726:main
628:serv
622:char
607:host
601:char
520:void
338:char
323:char
271:and
236:and
182:char
104:The
79:IPv6
77:and
75:IPv4
40:and
1126:));
1029:res
1017:res
980:res
974:res
962:res
950:res
944:for
920:));
771:int
765:res
723:int
643:int
565:int
437:www
416:res
377:res
311:int
295:or
224:is
149:int
140:int
131:int
122:int
27:In
1361::
1343:.
1287:.
1262:.
1251:^
1196:);
1180:);
1168:\n
1150:!=
1138:if
1108:\n
1081:!=
1072:if
1068:);
965:!=
935:);
902:\n
875:);
854:==
845:if
833:!=
824:if
820:);
649:);
586:sa
541:);
538:ai
511:,
509:ai
380:);
374:**
215:};
96:.
50:,
1326:.
1291:.
1273:.
1209:}
1206:;
1203:0
1190:(
1183:}
1174:,
1162:(
1156:)
1144:*
1141:(
1135:}
1132:;
1120:(
1114:,
1102:,
1096:(
1090:{
1087:)
1084:0
1075:(
1065:0
1062:,
1059:0
1056:,
1050:,
1044:,
1038:,
1026:,
1014:(
1008:=
1001:;
992:{
989:)
977:=
971:;
959:;
953:=
947:(
938:}
929:(
923:}
914:(
908:,
896:,
890:(
884:{
878:}
869:(
863:{
860:)
848:(
842:{
839:)
836:0
827:(
811:,
805:,
799:,
793:(
787:=
777:;
768:;
762:*
753:;
747:*
738:{
735:)
729:(
640:,
631:,
625:*
619:,
610:,
604:*
598:,
589:,
583:*
571:(
535:*
526:(
365:,
359:*
347:,
341:*
332:,
326:*
317:(
209:;
203:*
191:;
185:*
179:;
173:*
164:;
155:;
146:;
137:;
128:;
119:{
106:C
20:)
Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.