主机名、服务与地址的映射——getaddrinfo(),getnameinfo(),gai_strerror()

http://blog.csdn.net/duyiwuer2009/article/details/7875206

 


[cpp] view plain copy print ?
  1. /* Structure to contain information about address of a service provider.  */ 
  2. struct addrinfo 
  3.   int ai_flags;         /* Input flags.  */ 
  4.   int ai_family;        /* Protocol family for socket.  */ 
  5.   int ai_socktype;      /* Socket type.  */ 
  6.   int ai_protocol;      /* Protocol for socket.  */ 
  7.   socklen_t ai_addrlen;     /* Length of socket address.  */ 
  8.   struct sockaddr *ai_addr; /* Socket address for socket.  */ 
  9.   char *ai_canonname;       /* Canonical name for service location.  */ 
  10.   struct addrinfo *ai_next; /* Pointer to next in list.  */ 
  11. }; 

----------getaddrinfo()

The getaddrinfo function allows us to map a host name and a service name to an address.(APUE-2e)

[cpp] view plain copy print ?
  1. int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res); 
  2. void freeaddrinfo(struct addrinfo *res); 
  3. const char *gai_strerror(int errcode); 

Note:

(1) getaddrinfo() sets res to point to a dynamically-allocated linked list of  addrinfo  structures, linked by the ai_next member.  There are several reasons why the linked list may have more than one addrinfo structure, including:  if  the  network host is multi-homed; or if the same service is available from multiple socket  protocols  (one SOCK_STREAM address and another SOCK_DGRAM address, for example).

(2) If getaddrinfo fails, we can't use perror or strerror to generate an error message. Instead, we need to callgai_strerrorto convert the error code returned into an error message.

下面的 getnameinfo 也用 gai_strerror 收集错误信息。

(3) 注意addrinfo.ai_flags AI_*的含义(见例子)

(4) getaddrinfo() returns 0 if it succeeds, or one of the following nonzero error codes: <see man page>

这也是不能用 perror or strerror 处理的原因,因为它没有用 errno(#include <errno.h>) 作为错误代码

----------getnameinfo()

The getnameinfo function converts an address into a host name and a service name.

[cpp] view plain copy print ?
  1. int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); 

Note:

(1) The  sa  argument is a pointer to a generic socket address structure (of type sockaddr_in or sockaddr_in6) of size salen thatholds the input IP address and port number

(2) addrinfo.ai_flags getnameinfo的参数 flags 可以同时设置多个标志,按位与(|);如果取一个标志,要按位或(&),下面的例子中有这两种用法。(Multiple flags are specified by bitwise OR-ing them together.)

 

[cpp] view plain copy print ?
  1. /* Structure to contain information about address of a service provider.  */ 
  2. struct addrinfo 
  3.   int ai_flags;         /* Input flags.  */ 
  4.   int ai_family;       /* Protocol family for socket.  */ 
  5.   int ai_socktype;      /* Socket type.  */ 
  6.   int ai_protocol;     /* Protocol for socket.  */ 
  7.   socklen_t ai_addrlen;     /* Length of socket address.  */ 
  8.   struct sockaddr *ai_addr;/* Socket address for socket.  */ 
  9.   char *ai_canonname;       /* Canonical name for service location.  */ 
  10.   struct addrinfo *ai_next;/* Pointer to next in list.  */ 
  11. }; 

----------getaddrinfo()

The getaddrinfo function allows us to map a host name and a service name to an address.(APUE-2e)

[cpp] view plain copy print ?
  1. int getaddrinfo(constchar *node, constchar *service, conststruct addrinfo *hints, struct addrinfo **res); 
  2. void freeaddrinfo(struct addrinfo *res); 
  3. const char *gai_strerror(int errcode); 

Note:

(1) getaddrinfo() sets res to point to a dynamically-allocated linked list of  addrinfo  structures, linked by the ai_next member. There are several reasons why the linked list may have more than one addrinfo structure, including:  if  the  network host is multi-homed; or if the same service is available from multiple socket  protocols  (one SOCK_STREAM address and another SOCK_DGRAM address, for example).

(2) If getaddrinfo fails, we can't use perror or strerror to generate an error message. Instead, we need to callgai_strerrorto convert the error code returned into an error message.

下面的 getnameinfo 也用 gai_strerror收集错误信息。

(3) 注意addrinfo.ai_flags AI_*的含义(见例子)

(4) getaddrinfo() returns 0 if it succeeds, or one of the following nonzero error codes: <see man page>

这也是不能用 perror or strerror 处理的原因,因为它没有用 errno(#include <errno.h>) 作为错误代码

----------getnameinfo()

The getnameinfo function converts an address into a host name and a service name.

[cpp] view plain copy print ?
  1. int getnameinfo(conststruct sockaddr *sa, socklen_t salen, char *host, size_t hostlen,char *serv, size_t servlen,int flags); 

Note:

(1) The  sa  argument is a pointer to a generic socket address structure (of type sockaddr_in or sockaddr_in6) of size salen thatholds the input IP address and port number

(2) addrinfo.ai_flags getnameinfo的参数flags 可以同时设置多个标志,按位与(|);如果取一个标志,要按位或(&),下面的例子中有这两种用法。(Multiple flags are specified by bitwise OR-ing them together.)

[cpp] view plain copy print ?
  1. /**
  2. * getaddrinfo()
  3. * OS: Ubuntu 11.04 Server
  4. * This example is form APUE-2e
  5. */ 
  6. #include    "print_ai.h" 
  7. #include    <stdio.h> 
  8. #include    <stdlib.h> 
  9. #include    <netdb.h> 
  10. #include    <arpa/inet.h> 
  11.  
  12.  
  13. int main(int argc,char *argv[]) 
  14.     struct addrinfo *ailist = NULL; 
  15.     struct addrinfo hint; 
  16.     int err; 
  17.      
  18.     if(argc != 3) 
  19.     { 
  20.         printf("usage: %s nodename service\n", argv[0]); 
  21.         exit(1); 
  22.     } 
  23.      
  24.     hint.ai_flags = AI_CANONNAME;   // AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV 
  25.     hint.ai_family = 0; 
  26.     hint.ai_socktype = 0; 
  27.     hint.ai_protocol = 0; 
  28.     hint.ai_addrlen = 0; 
  29.     hint.ai_addr = NULL; 
  30.     hint.ai_canonname = NULL; 
  31.     hint.ai_next = NULL; 
  32.  
  33.  
  34.     if( (err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0 ) 
  35.     { 
  36.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err)); 
  37.         exit(EXIT_FAILURE); 
  38.     } 
  39.      
  40.     print_ai(ailist); 
  41.      
  42.     freeaddrinfo(ailist); 
  43.      
  44.     return 0; 
  45. /*
  46. $ ./a.out baidu.com nfs
  47. flags: AI_CANONNAME
  48. family: inet
  49. socket type: stream
  50. protocol: IPPROTO_TCP
  51. host: baidu.com
  52. address: 220.181.111.86
  53. port: 2049              # NFS程序常运行于这个端口
  54. flags: AI_CANONNAME
  55. family: inet
  56. socket type: datagram
  57. protocol: IPPROTO_TCP
  58. host:
  59. address: 220.181.111.86
  60. port: 2049
  61. flags: AI_CANONNAME
  62. family: inet
  63. socket type: stream
  64. protocol: IPPROTO_TCP
  65. host:
  66. address: 123.125.114.144
  67. port: 2049
  68. flags: AI_CANONNAME
  69. family: inet
  70. socket type: datagram
  71. protocol: IPPROTO_TCP
  72. host:
  73. address: 123.125.114.144
  74. port: 2049
  75. flags: AI_CANONNAME
  76. family: inet
  77. socket type: stream
  78. protocol: IPPROTO_TCP
  79. host:
  80. address: 220.181.111.85
  81. port: 2049
  82. flags: AI_CANONNAME
  83. family: inet
  84. socket type: datagram
  85. protocol: IPPROTO_TCP
  86. host:
  87. address: 220.181.111.85
  88. port: 2049
  89. -------------------------------
  90. $ ./a.out localhost nfs
  91. flags: AI_CANONNAME
  92. family: inet
  93. socket type: stream
  94. protocol: IPPROTO_TCP
  95. host: localhost
  96. address: 127.0.0.1
  97. port: 2049
  98. flags: AI_CANONNAME
  99. family: inet
  100. socket type: datagram
  101. protocol: IPPROTO_TCP
  102. host:
  103. address: 127.0.0.1
  104. port: 2049
  105. -------------------------------the function of AI_NUMERICHOST and AI_NUMERICSERV
  106. hint.ai_flags = AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV
  107. $ ./a.out baidu.com nfs
  108. getaddrinfo: Name or service not known
  109. $ ./a.out 220.181.111.85 nfs
  110. getaddrinfo: Name or service not known
  111. $ ./a.out 220.181.111.85 2049
  112. flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
  113. family: inet
  114. socket type: stream
  115. protocol: IPPROTO_TCP
  116. host: 220.181.111.85
  117. address: 220.181.111.85
  118. port: 2049
  119. flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
  120. family: inet
  121. socket type: datagram
  122. protocol: IPPROTO_TCP
  123. host:
  124. address: 220.181.111.85
  125. port: 2049
  126. flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
  127. family: inet
  128. socket type: raw
  129. protocol: default
  130. host:
  131. address: 220.181.111.85
  132. port: 2049
  133. -------------------------------
  134. FILES: /etc/gai.conf
  135. */ 


----------打印 addrinfo 的函数——print_ai

print_ai.h

[cpp] view plain copy print ?
  1. #ifndef _PRINT_AI_H 
  2. #define _PRINT_AI_H 
  3.  
  4. #include    <sys/socket.h> 
  5. #include    <netdb.h> 
  6.  
  7. extern void print_ai(struct addrinfo *ailist); 
  8. extern void print_flags(conststruct addrinfo *aip); 
  9. extern void print_family(conststruct addrinfo *aip); 
  10. extern void print_socktype(conststruct addrinfo *aip); 
  11. extern void print_protocol(conststruct addrinfo *aip); 
  12.  
  13. #endif  /* _PRINT_AI_H */ 

print_ai.c

[cpp] view plain copy print ?
  1. #include    "print_ai.h" 
  2. #include    <stdio.h> 
  3. #include    <arpa/inet.h> 
  4.  
  5. void print_ai(struct addrinfo *ailist) 
  6.     struct addrinfo     *aip = NULL; 
  7.     struct sockaddr_in  *sin_p = NULL; // socket inet address pointer 
  8.     char                addr_in_p[INET_ADDRSTRLEN]; 
  9.     const char          *p = NULL; 
  10.  
  11.     for(aip = ailist; aip != NULL; aip = aip->ai_next) 
  12.     { 
  13.         printf("flags: "); 
  14.         print_flags(aip); 
  15.         printf("\n"); 
  16.          
  17.         printf("family: "); 
  18.         print_family(aip); 
  19.         printf("\n"); 
  20.          
  21.         printf("socket type: "); 
  22.         print_socktype(aip); 
  23.         printf("\n"); 
  24.          
  25.         printf("protocol: "); 
  26.         print_protocol(aip); 
  27.         printf("\n"); 
  28.          
  29.         printf("host: %s\n", aip->ai_canonname ? aip->ai_canonname :""); 
  30.          
  31.         if(aip -> ai_family == AF_INET) 
  32.         { 
  33.             sin_p = (struct sockaddr_in *)aip->ai_addr; 
  34.             p = inet_ntop(AF_INET, &sin_p->sin_addr, addr_in_p, INET_ADDRSTRLEN); 
  35.             printf("address: %s\n", p ? addr_in_p :"unknown"); 
  36.             printf("port: %d\n", ntohs(sin_p->sin_port));   // Note:  
  37.         } 
  38.          
  39.         printf("\n"); 
  40.     } 
  41.  
  42. void print_flags(conststruct addrinfo *aip) 
  43.     if(aip->ai_flags == 0) 
  44.     { 
  45.         printf("0"); 
  46.     } 
  47.     else 
  48.     { 
  49.         if(aip->ai_flags & AI_PASSIVE) 
  50.         { 
  51.             printf("AI_PASSIVE "); 
  52.         } 
  53.         if(aip->ai_flags & AI_CANONNAME) 
  54.         { 
  55.             printf("AI_CANONNAME "); 
  56.         } 
  57.         if(aip->ai_flags & AI_NUMERICHOST) 
  58.         { 
  59.             printf("AI_NUMERICHOST "); 
  60.         } 
  61. #if defined(AI_V4MAPPED) 
  62.         if(aip->ai_flags & AI_V4MAPPED) 
  63.         { 
  64.             printf("AI_V4MAPPED "); 
  65.         } 
  66. #endif 
  67. #if defined(AI_ALL) 
  68.         if(aip->ai_flags & AI_ALL) 
  69.         { 
  70.             printf("AI_ALL "); 
  71.         } 
  72. #endif 
  73. #if defined(AI_ADDRCONFIG) 
  74.         if(aip->ai_flags & AI_ADDRCONFIG) 
  75.         { 
  76.             printf("AI_ADDRCONFIG "); 
  77.         } 
  78. #endif 
  79. #if defined(AI_NUMERICSERV) 
  80.         if(aip->ai_flags & AI_NUMERICSERV) 
  81.         { 
  82.             printf("AI_NUMERICSERV "); 
  83.         } 
  84. #endif 
  85.     } 
  86.  
  87. void print_family(conststruct addrinfo *aip) 
  88.     switch(aip->ai_family) 
  89.     { 
  90.     case AF_INET: 
  91.         printf("inet"); 
  92.         break
  93.     case AF_INET6: 
  94.         printf("inet6"); 
  95.         break
  96.     case AF_UNIX: 
  97.         printf("unix"); 
  98.         break
  99.     case AF_UNSPEC: 
  100.         printf("unspecified"); 
  101.         break
  102.     default
  103.         printf("unknown"); 
  104.     } 
  105.  
  106. void print_socktype(conststruct addrinfo *aip) 
  107.     switch(aip->ai_socktype) 
  108.     { 
  109.     case SOCK_STREAM: 
  110.         printf("stream"); 
  111.         break
  112.     case SOCK_DGRAM: 
  113.         printf("datagram"); 
  114.         break
  115.     case SOCK_SEQPACKET: 
  116.         printf("seqpacket"); 
  117.         break
  118.     case SOCK_RAW: 
  119.         printf("raw"); 
  120.         break
  121.     default
  122.         printf("unknown (%d)", aip->ai_socktype); 
  123.     } 
  124.  
  125. void print_protocol(conststruct addrinfo *aip) 
  126.     switch(aip->ai_protocol) 
  127.     { 
  128.     case 0: 
  129.         printf("default"); 
  130.         break
  131.     case IPPROTO_TCP: 
  132.         printf("IPPROTO_TCP"); 
  133.         break
  134.     case IPPROTO_UDP: 
  135.         printf("IPPROTO_UDP"); 
  136.         break
  137.     case IPPROTO_RAW: 
  138.         printf("IPPROTO_RAW"); 
  139.         break
  140.     default
  141.         printf("unknown (%d)", aip->ai_protocol); 
  142.     } 


[cpp] view plain copy print ?
  1. /**
  2. * getnameinfo()
  3. * OS: Ubuntu 11.04 Server
  4. * for other examples, see man getnameinfo page
  5. */ 
  6. #include <stdio.h> 
  7. #include <stdlib.h> 
  8. #include <netdb.h> 
  9. #include <arpa/inet.h> 
  10.  
  11. int main(int argc,char *argv[]) 
  12.     struct sockaddr_in sa_in; 
  13.     char host[NI_MAXHOST], service[NI_MAXSERV]; 
  14.     int flags; 
  15.     int err; 
  16.      
  17.     sa_in.sin_family = AF_INET; // IPv4 
  18.     sa_in.sin_port = htons(2049);   // the port of NFS 
  19.     inet_pton(AF_INET, "127.0.0.1", &sa_in.sin_addr.s_addr);   // "220.181.111.86" is the address of baidu.com 
  20.     flags = 0;  // NI_NUMERICHOST | NI_NUMERICSERV 
  21.      
  22.     err = getnameinfo((struct sockaddr *)(&sa_in),sizeof(struct sockaddr), 
  23.                     host, sizeof(host), service,sizeof(service), flags); 
  24.     if(err != 0) 
  25.     { 
  26.         gai_strerror(err); 
  27.         exit(EXIT_FAILURE); 
  28.     } 
  29.     printf("host=%s, serv=%s\n", host, service); 
  30.      
  31.     return 0; 
  32. /*
  33. $ ./a.out
  34. host=localhost, serv=nfs
  35. $ ./a.out
  36. host=220.181.111.86, serv=nfs
  37. */ 


 

----------参考资料

APUE-2e

Linux man pages

 

你可能感兴趣的:(主机名、服务与地址的映射——getaddrinfo(),getnameinfo(),gai_strerror())