在编程中,知道域名是不能直接访问主机,需要将其转换为IP地址。 但是在某些业务需求中需要将一个IP地址转换为域名。 Linux下,IP与域名的相互转换的C语言实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

//根据域名返回IP
void getIpByHostName(char *hostname);
//根据IP返回域名
void getHontNameByIp(char *ip);

/**
* 域名结构体
*/
// typedef struct hostent {
// char * h_name; //正式的主机名称
// char ** h_aliases; //主机的别名
// int h_addrtype; //主机名的类型
// int h_length; //地址的长度
// char ** h_addr_list; //从域名服务器获取的主机地址
// } hostent;

void getIpByHostName(char *hostname) {
struct hostent * host; //定义hostent指针
struct in_addr in;
struct sockaddr_in addr_in;
extern int h_errno;

if((host = gethostbyname(hostname))!=NULL) {
memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4); //复制主机地址、
in.s_addr=addr_in.sin_addr.s_addr;
printf("host name is %s\n", hostname);
printf("IP length: %d\n", host->h_length);
printf("Type: %d\n", host->h_addrtype);
printf("IP : %s\n", inet_ntoa(in));
}
else {
printf("host name is :%s\n",hostname);
printf("error : %d\n",h_errno);
printf("%s\n", hstrerror(h_errno));
}
}

void getHontNameByIp(char *ip) {
struct hostent *host;
struct in_addr in;
struct sockaddr_in addr_in;
extern int h_errno;

if((host=gethostbyaddr(ip, sizeof(ip), AF_INET))!=NULL){
memcpy(&addr_in.sin_addr.s_addr, host->h_addr, 4);
in.s_addr = addr_in.sin_addr.s_addr;
printf("Host Name is %s\n", host->h_name);
printf("Ip length %d\n", host->h_length);
printf("Type is %d\n", host->h_addrtype);
printf("Ip is %s\n", inet_ntoa(in));
}
else {
printf("error %d\n", h_errno);
printf("%s\n", hstrerror(h_errno));
}
}

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

char hostname1[] = "www.ansore.net"; //定义一个存在的域名
char hostname2[] = "www.123qwdferwerfsdfataewfar.net"; //定义一个不存在的域名
char ip[] = "139.129.35.50";

getIpByHostName(hostname1);
printf("--------------------------------------------------------\n");
getIpByHostName(hostname2);
printf("--------------------------------------------------------\n");
getHontNameByIp(ip);

return 0;
}