linux--IP地址和域名之间的转换

目录

利用域名获取IP地址

利用IP地址获取域名

利用域名获取IP地址

#include <netdb.h>

struct hostent* gethostbyname(const char* hostname);

成功时返回hostent结构体地址,失败时返回NULL指针。

host结构体定义如下: 

struct hostent
{
    char * h_name; //official name
    char ** h_aliases; //alias list
    int h_addrtype; //host address type
    int h_length; //address length
    char ** h_addr_list; //address list
}

 h_addr_list: 通过此变量以整数形式保存域名对应的IP地址。另外,用户较多的网站有可能分配多个IP给同一域名,利用多个服务器进行负载均衡。此时同样可以通过此变量获取IP地址信息

/*gethostbyname.c*/
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

void error_handling(char *message);

int main(int argc, char* argv[])
{
    int i = 0;
    struct hostent* host;
    if(argc != 2)
    {
        printf("Usage: %s <addr>\n", argv[0]);
        exit(1);
    }

    host = gethostbyname(argv[1]);
    if(host == NULL)
    {
        error_handling("gethost...error");
    }
    printf("Official name: %s\n", host->h_name);
    printf("address length: %d\n", host->h_length);

    for(i = 0; host->h_aliases[i]; i++)
    {
        printf("Aliases %d: %s\n", i + 1, host->h_aliases[i]);
    }

    printf("Address type: %s\n", (host->h_addrtype == AF_INET) ? "AF_INET" : "AF_INET6");

    for( i = 0; host->h_addr_list[i]; i++)
    {
        printf("IP addr %d: %s\n", i + 1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
    }

    return 0;
}

void error_handling(char* message)
{
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}

运行结果:

mali@mali:~/code$ gcc gethostbyname.c -o gethostbyname
mali@mali:~/code$ ./gethostbyname www.taobao.com
Official name: www.taobao.com.danuoyi.tbcache.com
address length: 4
Aliases 1: www.taobao.com
Address type: AF_INET
IP addr 1: 101.89.125.220
IP addr 2: 180.163.39.80
IP addr 3: 101.89.125.219
IP addr 4: 180.163.39.79
mali@mali:~/code$ ./gethostbyname www.baidu.com
Official name: www.a.shifen.com
address length: 4
Aliases 1: www.baidu.com
Address type: AF_INET
IP addr 1: 180.101.49.12
IP addr 2: 180.101.49.11

 

利用IP地址获取域名

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux C 语言中,可以使用 getaddrinfo 函数将域名转换IP 地址。 具体实现步骤如下: 1. 引入头文件: ``` #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> ``` 2. 定义 getaddrinfo() 函数所需的结构体: ``` struct addrinfo hints, *res; int status; char ipstr[INET6_ADDRSTRLEN]; ``` 3. 初始化 hints 结构体: ``` memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET 或 AF_INET6 均可 hints.ai_socktype = SOCK_STREAM; // TCP ``` 4. 调用 getaddrinfo() 函数: ``` status = getaddrinfo("www.example.com", NULL, &hints, &res); ``` 其中 "www.example.com" 是需要转换域名。 5. 遍历返回的地址列表,并将其转换IP 地址: ``` struct addrinfo *p; void *addr; for(p = res; p != NULL; p = p->ai_next) { if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr); } // 将 IP 地址转换为字符串 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); } ``` 其中,inet_ntop() 函数将 IP 地址转换为字符串形式,ipstr 变量即为转换后的 IP 地址。 6. 释放内存: ``` freeaddrinfo(res); ``` 完整代码如下: ``` #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res; int status; char ipstr[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; status = getaddrinfo("www.example.com", NULL, &hints, &res); if (status != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } struct addrinfo *p; void *addr; for(p = res; p != NULL; p = p->ai_next) { if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr); } inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); printf("IP address: %s\n", ipstr); } freeaddrinfo(res); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值