struct addrinfo结构体


表头文件: #include<netdb.h>

struct addrinfo
{
int ai_flags;
int ai_family; //AF_INET,AF_INET6,UNIX etc
int ai_socktype; //STREAM,DATAGRAM,RAW
int ai_protocol; //IPPROTO_IP, IPPROTO_IPV4, IPPROTO_IPV6 etc
size_t ai_addrlen;//length of ai_addr
char* ai_canonname; //full hostname
struct sockaddr* ai_addr; //addr of host
struct addrinfo* ai_next;
}

value of ai_falgs:
AI_PASSIVE: Socket address is intended for `bind'.
AI_CANONNAME:Request for canonical name.
AI_NUMERICHOST: Don't use name resolution.
AI_V4MAPPED: IPv4 mapped addresses are acceptable.
AI_ALL: Return IPv4 mapped and IPv6 addresses.
AI_ADDRCONFIG:Use configuration of this host to choose

定义函数:
int getaddrinfo( const char *hostname, const char *service, const struct addrinfo *hints,
struct addrinfo **result );

函数说明:
      getaddrinfo函数能够处理名字到地址以及服务到端口这两种转换,返回的是一个sockaddr 结构的链而 不是一个地址清单。它具有协议无关性。
      hostname:一个主机名或者地址串(IPv4的点分十进制串或者IPv6的16进制串)
      service:一个服务名或者10进制端口号数串。 【帮客之家 http://www.bkjia.com 】
       hints:可以是一个空指针,也可以是一个指向某个addrinfo结构的指针,调用者在这个结构中填入关于期望返回的信息类型的暗示。举例来说:如果指定的服务既支持TCP也支持UDP,那么调用者可以把hints结构中的ai_socktype成员设置成SOCK_DGRAM使得返回的仅仅是适用于数据报套接口的信息。返回0: 成功,返回非0: 出错。

定义函数:const char *gai_strerror( int error );
函数说明:
      该函数以getaddrinfo返回的非0错误值的名字和含义为他的唯一参数,返回一个指向对应的出错信息串的指针。

定义函数: void freeaddrinfo( struct addrinfo *ai );
函数说明:
       由getaddrinfo返回的所有存储空间都是动态获取的,这些存储空间必须通过调用freeaddrinfo返回给系统。

例子:

  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <stdlib.h>   
  4. #include <netdb.h>   
  5. #include <sys/types.h>   
  6. #include <sys/socket.h>   
  7. #include <arpa/inet.h>   
  8.   
  9. int  
  10. lookup_host (const char *host)  
  11. {  
  12.   struct addrinfo hints, *res;  
  13.   int errcode;  
  14.   char addrstr[100];  
  15.   void *ptr;  
  16.   
  17.   memset (&hints, 0, sizeof (hints));  
  18.   hints.ai_family = PF_UNSPEC;  
  19.   hints.ai_socktype = SOCK_STREAM;  
  20.   hints.ai_flags |= AI_CANONNAME;  
  21.   
  22.   errcode = getaddrinfo (host, NULL, &hints, &res);  
  23.   if (errcode != 0)  
  24.     {  
  25.       perror ("getaddrinfo");  
  26.       return -1;  
  27.     }  
  28.   
  29.   printf ("Host: %s\n", host);  
  30.   while (res)  
  31.     {  
  32.       inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr, 100);  
  33.   
  34.       switch (res->ai_family)  
  35.         {  
  36.         case AF_INET:  
  37.           ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;  
  38.           break;  
  39.         case AF_INET6:  
  40.           ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;  
  41.           break;  
  42.         }  
  43.       inet_ntop (res->ai_family, ptr, addrstr, 100);  
  44.       printf ("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,  
  45.               addrstr, res->ai_canonname);  
  46.       res = res->ai_next;  
  47.     }  
  48.   
  49.   return 0;  
  50. }  
  51.   
  52. int  
  53. main (int argc, char *argv[])  
  54. {  
  55.   if (argc < 2)  
  56.     exit (1);  
  57.   return lookup_host (argv[1]);  
  58. }  

相关内容