您的位置:首页 > 大数据 > 人工智能

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

2014-05-08 10:12 295 查看
/article/2249925.html

[cpp]
view plaincopyprint?

/* Structure to contain information about address of a service provider. */

struct addrinfo
{
int ai_flags; /* Input flags. */

int ai_family;
/* Protocol family for socket. */

int ai_socktype; /* Socket type. */

int ai_protocol;
/* Protocol for socket. */
socklen_t ai_addrlen; /* Length of socket address. */

struct sockaddr *ai_addr;
/* Socket address for socket. */

char *ai_canonname;
/* Canonical name for service location. */

struct addrinfo *ai_next;
/* Pointer to next in list. */

};

/* Structure to contain information about address of a service provider.  */
struct addrinfo
{
int ai_flags;			/* Input flags.  */
int ai_family;		/* Protocol family for socket.  */
int ai_socktype;		/* Socket type.  */
int ai_protocol;		/* Protocol for socket.  */
socklen_t ai_addrlen;		/* Length of socket address.  */
struct sockaddr *ai_addr;	/* Socket address for socket.  */
char *ai_canonname;		/* Canonical name for service location.  */
struct addrinfo *ai_next;	/* Pointer to next in list.  */
};


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

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

[cpp]
view plaincopyprint?

int getaddrinfo(const
char *node, const
char *service, const
struct addrinfo *hints, struct addrinfo **res);

void freeaddrinfo(struct addrinfo *res);

const char *gai_strerror(int errcode);

int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
void freeaddrinfo(struct addrinfo *res);
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 plaincopyprint?

int getnameinfo(const
struct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen,
char *serv, size_t servlen,
int flags);

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 plaincopyprint?

/* Structure to contain information about address of a service provider. */
struct addrinfo
{
int ai_flags; /* Input flags. */
int ai_family; /* Protocol family for socket. */

int ai_socktype; /* Socket type. */
int ai_protocol; /* Protocol for socket. */

socklen_t ai_addrlen; /* Length of socket address. */
struct sockaddr *ai_addr;/* Socket address for socket. */

char *ai_canonname;
/* Canonical name for service location. */

struct addrinfo *ai_next;/* Pointer to next in list. */

};

/* Structure to contain information about address of a service provider.  */
struct addrinfo
{
int ai_flags;			/* Input flags.  */
int ai_family;		/* Protocol family for socket.  */
int ai_socktype;		/* Socket type.  */
int ai_protocol;		/* Protocol for socket.  */
socklen_t ai_addrlen;		/* Length of socket address.  */
struct sockaddr *ai_addr;	/* Socket address for socket.  */
char *ai_canonname;		/* Canonical name for service location.  */
struct addrinfo *ai_next;	/* Pointer to next in list.  */
};


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

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

[cpp]
view plaincopyprint?

int getaddrinfo(constchar *node,
constchar *service,
conststruct addrinfo *hints,
struct addrinfo **res);
void freeaddrinfo(struct addrinfo *res);
const char *gai_strerror(int errcode);

int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
void freeaddrinfo(struct addrinfo *res);
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 plaincopyprint?

int getnameinfo(conststruct sockaddr *sa, socklen_t salen,
char *host, size_t hostlen,char *serv,
size_t servlen,int flags);

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 plaincopyprint?

/**
* getaddrinfo()
* OS: Ubuntu 11.04 Server

* This example is form APUE-2e
*/
#include "print_ai.h"

#include <stdio.h>
#include <stdlib.h>

#include <netdb.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])

{
struct addrinfo *ailist = NULL;
struct addrinfo hint;
int err;

if(argc != 3)

{
printf("usage: %s nodename service\n", argv[0]);
exit(1);
}

hint.ai_flags = AI_CANONNAME; // AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV
hint.ai_family = 0;
hint.ai_socktype = 0;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_addr = NULL;
hint.ai_canonname = NULL;
hint.ai_next = NULL;

if( (err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0 )
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
exit(EXIT_FAILURE);
}

print_ai(ailist);

freeaddrinfo(ailist);

return 0;
}
/*
$ ./a.out baidu.com nfs
flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host: baidu.com
address: 220.181.111.86
port: 2049 # NFS程序常运行于这个端口

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 220.181.111.86
port: 2049

flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host:
address: 123.125.114.144
port: 2049

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 123.125.114.144
port: 2049

flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host:
address: 220.181.111.85
port: 2049

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 220.181.111.85
port: 2049

-------------------------------
$ ./a.out localhost nfs
flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host: localhost
address: 127.0.0.1
port: 2049

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 127.0.0.1
port: 2049

-------------------------------the function of AI_NUMERICHOST and AI_NUMERICSERV
hint.ai_flags = AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV
$ ./a.out baidu.com nfs

getaddrinfo: Name or service not known

$ ./a.out 220.181.111.85 nfs
getaddrinfo: Name or service not known

$ ./a.out 220.181.111.85 2049

flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
family: inet
socket type: stream
protocol: IPPROTO_TCP
host: 220.181.111.85
address: 220.181.111.85

port: 2049

flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 220.181.111.85

port: 2049

flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
family: inet
socket type: raw
protocol: default
host:
address: 220.181.111.85

port: 2049

-------------------------------
FILES: /etc/gai.conf
*/

/**
* getaddrinfo()
* OS: Ubuntu 11.04 Server
* This example is form APUE-2e
*/
#include	"print_ai.h"
#include	<stdio.h>
#include	<stdlib.h>
#include	<netdb.h>
#include	<arpa/inet.h>

int main(int argc, char *argv[])
{
struct addrinfo *ailist = NULL;
struct addrinfo hint;
int err;

if(argc != 3)
{
printf("usage: %s nodename service\n", argv[0]);
exit(1);
}

hint.ai_flags = AI_CANONNAME;	// AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV
hint.ai_family = 0;
hint.ai_socktype = 0;
hint.ai_protocol = 0;
hint.ai_addrlen = 0;
hint.ai_addr = NULL;
hint.ai_canonname = NULL;
hint.ai_next = NULL;

if( (err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0 )
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
exit(EXIT_FAILURE);
}

print_ai(ailist);

freeaddrinfo(ailist);

return 0;
}
/*
$ ./a.out baidu.com nfs
flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host: baidu.com
address: 220.181.111.86
port: 2049				# NFS程序常运行于这个端口

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 220.181.111.86
port: 2049

flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host:
address: 123.125.114.144
port: 2049

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 123.125.114.144
port: 2049

flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host:
address: 220.181.111.85
port: 2049

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 220.181.111.85
port: 2049

-------------------------------
$ ./a.out localhost nfs
flags: AI_CANONNAME
family: inet
socket type: stream
protocol: IPPROTO_TCP
host: localhost
address: 127.0.0.1
port: 2049

flags: AI_CANONNAME
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 127.0.0.1
port: 2049

-------------------------------the function of AI_NUMERICHOST and AI_NUMERICSERV
hint.ai_flags = AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV
$ ./a.out baidu.com nfs
getaddrinfo: Name or service not known

$ ./a.out 220.181.111.85 nfs
getaddrinfo: Name or service not known

$ ./a.out 220.181.111.85 2049
flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
family: inet
socket type: stream
protocol: IPPROTO_TCP
host: 220.181.111.85
address: 220.181.111.85
port: 2049

flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
family: inet
socket type: datagram
protocol: IPPROTO_TCP
host:
address: 220.181.111.85
port: 2049

flags: AI_CANONNAME AI_NUMERICHOST AI_NUMERICSERV
family: inet
socket type: raw
protocol: default
host:
address: 220.181.111.85
port: 2049

-------------------------------
FILES: /etc/gai.conf
*/


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

print_ai.h

[cpp]
view plaincopyprint?

#ifndef _PRINT_AI_H
#define _PRINT_AI_H

#include <sys/socket.h>

#include <netdb.h>

extern void print_ai(struct addrinfo *ailist);
extern void print_flags(conststruct addrinfo *aip);

extern void print_family(conststruct addrinfo *aip);

extern void print_socktype(conststruct addrinfo *aip);

extern void print_protocol(conststruct addrinfo *aip);

#endif /* _PRINT_AI_H */

#ifndef	_PRINT_AI_H
#define	_PRINT_AI_H

#include	<sys/socket.h>
#include	<netdb.h>

extern void print_ai(struct addrinfo *ailist);
extern void print_flags(const struct addrinfo *aip);
extern void print_family(const struct addrinfo *aip);
extern void print_socktype(const struct addrinfo *aip);
extern void print_protocol(const struct addrinfo *aip);

#endif	/* _PRINT_AI_H */

print_ai.c

[cpp]
view plaincopyprint?

#include "print_ai.h"
#include <stdio.h>
#include <arpa/inet.h>

void print_ai(struct addrinfo *ailist)
{
struct addrinfo *aip = NULL;
struct sockaddr_in *sin_p = NULL; // socket inet address pointer

char addr_in_p[INET_ADDRSTRLEN];
const char *p = NULL;

for(aip = ailist; aip != NULL; aip = aip->ai_next)
{
printf("flags: ");
print_flags(aip);
printf("\n");

printf("family: ");
print_family(aip);
printf("\n");

printf("socket type: ");

print_socktype(aip);
printf("\n");

printf("protocol: ");

print_protocol(aip);
printf("\n");

printf("host: %s\n", aip->ai_canonname ? aip->ai_canonname :"");

if(aip -> ai_family == AF_INET)
{
sin_p = (struct sockaddr_in *)aip->ai_addr;
p = inet_ntop(AF_INET, &sin_p->sin_addr, addr_in_p, INET_ADDRSTRLEN);
printf("address: %s\n", p ? addr_in_p :"unknown");

printf("port: %d\n", ntohs(sin_p->sin_port)); // Note:

}

printf("\n");
}
}

void print_flags(conststruct addrinfo *aip)

{
if(aip->ai_flags == 0)
{
printf("0");
}
else
{
if(aip->ai_flags & AI_PASSIVE)
{
printf("AI_PASSIVE ");

}
if(aip->ai_flags & AI_CANONNAME)
{
printf("AI_CANONNAME ");
}
if(aip->ai_flags & AI_NUMERICHOST)
{
printf("AI_NUMERICHOST ");
}
#if defined(AI_V4MAPPED)

if(aip->ai_flags & AI_V4MAPPED)
{
printf("AI_V4MAPPED ");
}
#endif
#if defined(AI_ALL)
if(aip->ai_flags & AI_ALL)
{
printf("AI_ALL ");
}
#endif
#if defined(AI_ADDRCONFIG)

if(aip->ai_flags & AI_ADDRCONFIG)
{
printf("AI_ADDRCONFIG ");
}
#endif
#if defined(AI_NUMERICSERV)

if(aip->ai_flags & AI_NUMERICSERV)
{
printf("AI_NUMERICSERV ");
}
#endif
}
}

void print_family(conststruct addrinfo *aip)

{
switch(aip->ai_family)
{
case AF_INET:

printf("inet");
break;
case AF_INET6:
printf("inet6");
break;
case AF_UNIX:

printf("unix");
break;
case AF_UNSPEC:
printf("unspecified");
break;
default:
printf("unknown");
}
}

void print_socktype(conststruct addrinfo *aip)

{
switch(aip->ai_socktype)

{
case SOCK_STREAM:
printf("stream");
break;
case SOCK_DGRAM:

printf("datagram");
break;
case SOCK_SEQPACKET:
printf("seqpacket");
break;
case SOCK_RAW:

printf("raw");
break;
default:
printf("unknown (%d)", aip->ai_socktype);
}
}

void print_protocol(conststruct addrinfo *aip)

{
switch(aip->ai_protocol)
{
case 0:
printf("default");
break;
case IPPROTO_TCP:
printf("IPPROTO_TCP");
break;
case IPPROTO_UDP:

printf("IPPROTO_UDP");

break;
case IPPROTO_RAW:
printf("IPPROTO_RAW");
break;
default:
printf("unknown (%d)", aip->ai_protocol);
}
}

#include	"print_ai.h"
#include	<stdio.h>
#include	<arpa/inet.h>

void print_ai(struct addrinfo *ailist)
{
struct addrinfo		*aip = NULL;
struct sockaddr_in	*sin_p = NULL;	// socket inet address pointer
char 				addr_in_p[INET_ADDRSTRLEN];
const char 			*p = NULL;

for(aip = ailist; aip != NULL; aip = aip->ai_next)
{
printf("flags: ");
print_flags(aip);
printf("\n");

printf("family: ");
print_family(aip);
printf("\n");

printf("socket type: ");
print_socktype(aip);
printf("\n");

printf("protocol: ");
print_protocol(aip);
printf("\n");

printf("host: %s\n", aip->ai_canonname ? aip->ai_canonname : "");

if(aip -> ai_family == AF_INET)
{
sin_p = (struct sockaddr_in *)aip->ai_addr;
p = inet_ntop(AF_INET, &sin_p->sin_addr, addr_in_p, INET_ADDRSTRLEN);
printf("address: %s\n", p ? addr_in_p : "unknown");
printf("port: %d\n", ntohs(sin_p->sin_port));	// Note:
}

printf("\n");
}
}

void print_flags(const struct addrinfo *aip)
{
if(aip->ai_flags == 0)
{
printf("0");
}
else
{
if(aip->ai_flags & AI_PASSIVE)
{
printf("AI_PASSIVE ");
}
if(aip->ai_flags & AI_CANONNAME)
{
printf("AI_CANONNAME ");
}
if(aip->ai_flags & AI_NUMERICHOST)
{
printf("AI_NUMERICHOST ");
}
#if defined(AI_V4MAPPED)
if(aip->ai_flags & AI_V4MAPPED)
{
printf("AI_V4MAPPED ");
}
#endif
#if defined(AI_ALL)
if(aip->ai_flags & AI_ALL)
{
printf("AI_ALL ");
}
#endif
#if defined(AI_ADDRCONFIG)
if(aip->ai_flags & AI_ADDRCONFIG)
{
printf("AI_ADDRCONFIG ");
}
#endif
#if defined(AI_NUMERICSERV)
if(aip->ai_flags & AI_NUMERICSERV)
{
printf("AI_NUMERICSERV ");
}
#endif
}
}

void print_family(const struct addrinfo *aip)
{
switch(aip->ai_family)
{
case AF_INET:
printf("inet");
break;
case AF_INET6:
printf("inet6");
break;
case AF_UNIX:
printf("unix");
break;
case AF_UNSPEC:
printf("unspecified");
break;
default:
printf("unknown");
}
}

void print_socktype(const struct addrinfo *aip)
{
switch(aip->ai_socktype)
{
case SOCK_STREAM:
printf("stream");
break;
case SOCK_DGRAM:
printf("datagram");
break;
case SOCK_SEQPACKET:
printf("seqpacket");
break;
case SOCK_RAW:
printf("raw");
break;
default:
printf("unknown (%d)", aip->ai_socktype);
}
}

void print_protocol(const struct addrinfo *aip)
{
switch(aip->ai_protocol)
{
case 0:
printf("default");
break;
case IPPROTO_TCP:
printf("IPPROTO_TCP");
break;
case IPPROTO_UDP:
printf("IPPROTO_UDP");
break;
case IPPROTO_RAW:
printf("IPPROTO_RAW");
break;
default:
printf("unknown (%d)", aip->ai_protocol);
}
}


[cpp]
view plaincopyprint?

/**
* getnameinfo()
* OS: Ubuntu 11.04 Server

* for other examples, see man getnameinfo page
*/
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(int argc,char *argv[])

{
struct sockaddr_in sa_in;
char host[NI_MAXHOST], service[NI_MAXSERV];
int flags;

int err;

sa_in.sin_family = AF_INET; // IPv4
sa_in.sin_port = htons(2049); // the port of NFS
inet_pton(AF_INET, "127.0.0.1", &sa_in.sin_addr.s_addr); // "220.181.111.86" is the address of baidu.com
flags = 0; // NI_NUMERICHOST | NI_NUMERICSERV

err = getnameinfo((struct sockaddr *)(&sa_in),sizeof(struct sockaddr),
host, sizeof(host), service,sizeof(service), flags);

if(err != 0)

{
gai_strerror(err);
exit(EXIT_FAILURE);
}
printf("host=%s, serv=%s\n", host, service);

return 0;
}
/*
$ ./a.out
host=localhost, serv=nfs

$ ./a.out
host=220.181.111.86, serv=nfs

*/

/**
* getnameinfo()
* OS: Ubuntu 11.04 Server
* for other examples, see man getnameinfo page
*/
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
struct sockaddr_in sa_in;
char host[NI_MAXHOST], service[NI_MAXSERV];
int flags;
int err;

sa_in.sin_family = AF_INET;	// IPv4
sa_in.sin_port = htons(2049);	// the port of NFS
inet_pton(AF_INET, "127.0.0.1", &sa_in.sin_addr.s_addr);	// "220.181.111.86" is the address of baidu.com
flags = 0;	// NI_NUMERICHOST | NI_NUMERICSERV

err = getnameinfo((struct sockaddr *)(&sa_in), sizeof(struct sockaddr),
host, sizeof(host), service, sizeof(service), flags);
if(err != 0)
{
gai_strerror(err);
exit(EXIT_FAILURE);
}
printf("host=%s, serv=%s\n", host, service);

return 0;
}
/*
$ ./a.out
host=localhost, serv=nfs

$ ./a.out
host=220.181.111.86, serv=nfs
*/


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

APUE-2e

Linux man pages
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐