Linux下获得主机与域名-gethostbyname和gethostbyaddr

来源:互联网 发布:sqlserver 定义表变量 编辑:程序博客网 时间:2024/05/16 17:40

1.数据结构hostentservent:

   struct hostent{

   char *h_name; /* official domain name of host */

   char **h_aliases; /* null-terminated array of domain names */

   int h_addrtype; /* host address type (AF_INET) */

   int h_length; /* length of an address, in bytes */

   char **h_addr_list; /* null-terminated array of in_addr structs */

   };

   #define h_addr h_addr_list[0]

这里是这个数据结构的详细资料:

char *h_name :表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com

char **h_aliases: 表示的是主机的别名。www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。

int h_addrtype :表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是ipv6(AF_INET6)

int h_length :表示的是主机ip地址的长度

int **h_addr_lisst: 表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()

结构体servent

typedef struct servent {
char FAR* s_name; //
正规的服务名
char FAR FAR** s_aliases;
short s_port; //
连接到该服务时需要用到的端口号
char FAR* s_proto;
} servent;

2.取得主机名与域名

2.1 gethostname() : 返回本地主机的标准主机名。

原型如下:

#include <unistd.h>

int gethostname(char *name, size_t len);

参数说明:

这个函数需要两个参数:

接收缓冲区name,其长度必须为len字节或是更长

接收缓冲区name的最大长度

返回值:

如果函数成功,则返回0。如果发生错误则返回-1。错误号存放在外部变量errno中。

2.2 getdomainname(2)函数:可以允许程序获得程序正运行的主机的NIS域名。

原型如下:

#include <unistd.h>

int getdomainname(char *name,size_t len);

参数说明:

这个函数的用法也gethostname相同。

2.3 测试getdomainname与gethostname函数

下面这个程序演示了这两个函数的用法。这个程序只是简单的调用这两个函数并报告其结果。

/*gethostn.c

*

* Example of gethostname(2):

*/

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>

int main(int argc,char **argv)

{

int z;

char buf[32];

z = gethostname(buf,sizeof buf);

if(z==-1)

{

fprintf(stderr,"%s:gethostname(2)\n",strerror(errno));

exit(1);

}

printf("host name = '%s'\n",buf);

z = getdomainname(buf,sizeof buf);

if(z==-1)

{

fprintf(stderr,"%s:getdomainname(2)\n",strerror(errno));

exit(1);

}

printf("domain name = '%s'\n",buf);

return 0;

}

这个程序的运行结果如下:

$ ./gethostn

host name = 'tux'

domain name = ''

在了解了如何获得本地系统的信息以后,现在我们就可以将我们的注意力转移到解析远程主机名上了。

3.gethostbyaddr():

struct hostent *gethostbyaddr(const char *name)

这个函数,传入值是IP地址(注意,这里不是简单的字符串,需要先将字符串形式的IP地址由inet_aton转化一下),然后经过函数处理,将结果由返回值传出。返回值是一个hostent结构.因为有了hosten这个传出的结构,我们可以在这个结构里面找到我们想需要的信息。

下面的是例程。编译后只需在命令行输入./a.out 202.102.14.141IP地址)就可以看结果了。

#include <netdb.h>

#include <sys/socket.h>

int main(int argc, char **argv)

{

char *ptr,**pptr;

struct hostent *hptr;

char str[32];

char ipaddr[16];

struct in_addr *hipaddr;

/* 取得命令后第一个参数,即要解析的IP地址 */

ptr = argv[1];

/* 调用inet_aton()ptr就是以字符串存放的地方的指针,hipaddrin_addr形式的地址 */

if(!inet_aton(ptr,hipaddr))

{

printf("inet_aton error\n");

return 1;

}

/* 调用gethostbyaddr()。调用结果都存在hptr */

if( (hptr = gethostbyaddr(hipaddr, 4, AF_INET) ) == NULL )

{

printf("gethostbyaddr error for addr:%s\n", ptr);

return 1; /* 如果调用gethostbyaddr发生错误,返回1 */

}

/* 将主机的规范名打出来 */

printf("official hostname:%s\n",hptr->h_name);

/* 主机可能有多个别名,将所有别名分别打出来 */

for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)

printf(" alias:%s\n",*pptr);

/* 根据地址类型,将地址打出来 */

switch(hptr->h_addrtype)

{

case AF_INET:

case AF_INET6:

pptr=hptr->h_addr_list;

/* 将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 */

for(;*pptr!=NULL;pptr++)

printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

break;

default:

printf("unknown address type\n");

break;

}

return 0;

}

4.gethostbyname():

使用这个东西,首先要包含2个头文件:

#include <netdb.h>

#include <sys/socket.h>

struct hostent *gethostbyname(const char *name);

这个函数的传入值是域名或者主机名,例如"www.google.com","wpc"等等。

传出值,是一个hostent的结构(如下)。如果函数调用失败,将返回NULL

struct hostent {

char *h_name;

char **h_aliases;

int h_addrtype;

int h_length;

char **h_addr_list;

};(对它的解释见1)

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)

这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。

这个函数,其实就是返回指向dst的一个指针。如果函数调用错误,返回值是NULL

下面是例程,有详细的注释。

#include <netdb.h>

#include <sys/socket.h>

int main(int argc, char **argv)

{

char *ptr,**pptr;

struct hostent *hptr;

char str[32];

/* 取得命令后第一个参数,即要解析的域名或主机名 */

ptr = argv[1];

/* 调用gethostbyname()。调用结果都存在hptr */

if( (hptr = gethostbyname(ptr) ) == NULL )

{

printf("gethostbyname error for host:%s\n", ptr);

return 0; /* 如果调用gethostbyname发生错误,返回1 */

}

/* 将主机的规范名打出来 */

printf("official hostname:%s\n",hptr->h_name);

/* 主机可能有多个别名,将所有别名分别打出来 */

for(pptr = hptr->h_aliases; *pptr != NULL; pptr++)

printf(" alias:%s\n",*pptr);

/* 根据地址类型,将地址打出来 */

switch(hptr->h_addrtype)

{

case AF_INET:

case AF_INET6:

pptr=hptr->h_addr_list;

/* 将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 */

for(;*pptr!=NULL;pptr++)

printf(" address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));

break;

default:

printf("unknown address type\n");

break;

}

return 0;

}

原创粉丝点击