gpt4 book ai didi

C gethostbyname 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:15 24 4
gpt4 key购买 nike

我正在尝试修改 Linux tracepath (traceroute) 程序以使其处理三个不同的 ip 地址。这三个 ip 地址是:

  1. "11.11.11.11";
  2. "144.133.133.133";
  3. “202.202.202.202”;

应该将它们转换为套接字地址结构和无符号整数值等,然后再转换回字符串,但我一直遇到段错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/types.h>
#include <linux/errqueue.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
//#include <resolv.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <getopt.h>

char target_ip_string[INET_ADDRSTRLEN] = {0}; // first address written out string
char alternate_ip_1[INET_ADDRSTRLEN] = {0}; // second address written out string
char alternate_ip_2[INET_ADDRSTRLEN] = {0}; // third address written out string

struct sockaddr_in target_host; // first address
struct sockaddr_in target_alt1; // second address
struct sockaddr_in target_alt2; // third address

/**
* This example should convert three distict ip addresses from three distinct
* strings to three distinct integer values.
*/
int
main(int argc, char **argv) {
setvbuf(stdout, NULL, _IONBF, 0); // auto flushing.

struct hostent *hostname_target_entry;
struct hostent *hostname_alt1_entry;
struct hostent *hostname_alt2_entry;

target_host.sin_family = AF_INET;
target_alt1.sin_family = AF_INET;
target_alt2.sin_family = AF_INET;

char * target_hostname = (char *) "11.11.11.11";
char * alternate_ip_1_local = (char *) "144.133.133.133";
char * alternate_ip_2_local = (char *) "202.202.202.202";

printf("The first ip is: %s \n", target_hostname);
printf("The second ip is: %s \n", alternate_ip_1_local);
printf("The third ip is: %s \n\n", alternate_ip_2_local);

hostname_target_entry = gethostbyname(target_hostname);
hostname_alt1_entry = gethostbyname(alternate_ip_1_local);
hostname_alt2_entry = gethostbyname(alternate_ip_2_local);

memcpy(&target_host.sin_addr, hostname_target_entry->h_addr_list[0], 4);
memcpy(&target_alt1.sin_addr, hostname_alt1_entry->h_addr_list[0], 4);
memcpy(&target_alt2.sin_addr, hostname_alt2_entry->h_addr_list[0], 4);

// These should be three different ip addresses
printf("First ip as an integer: %u \n", target_host.sin_addr.s_addr); // Why are these all the same?
printf("Second ip as an integer: %u \n", target_alt1.sin_addr.s_addr); // Why are these all the same?
printf("Third ip as an integer: %u \n\n", target_alt2.sin_addr.s_addr); // Why are these all the same?

printf("No seg fault yet. \n\n");

// This conversion should yield "11.11.11.11"
const char * conversion1 = inet_ntop(AF_INET, &(target_host.sin_addr), target_ip_string, INET_ADDRSTRLEN);

printf("Result of inet_ntop: %s \n", conversion1); // Why is this "202.202.202.202" and not "11.11.11.11"?

printf("Seg fault here. \n\n");

// This conversion should yield "144.133.133.133".
const char * conversion2 = inet_ntop(AF_INET, &(target_alt1.sin_addr), alternate_ip_1, INET_ADDRSTRLEN);

// This conversion should yield "202.202.202.202".
const char * conversion3 = inet_ntop(AF_INET, &(target_alt2.sin_addr), alternate_ip_2, INET_ADDRSTRLEN);

return 0;
}

终端输出:

The first ip is: 11.11.11.11 
The second ip is: 144.133.133.133
The third ip is: 202.202.202.202

First ip as an integer: 3402287818
Second ip as an integer: 3402287818
Third ip as an integer: 3402287818

No seg fault yet.

Result of inet_ntop: 202.202.202.202
Seg fault here.

为什么我在 string 和 hostent/sockaddr_in 之间的转换总是失败?

最佳答案

gethostbyname 函数返回一个指向静态数据的指针,因此如果您不复制给定调用的结果,它将被下一次调用覆盖。

由于您只转换 IP 地址而不转换主机名,因此最好改用 inet_addr:

target_host.sin_addr.s_addr = inet_addr("11.11.11.11");
target_alt1.sin_addr.s_addr = inet_addr("144.133.133.133");
target_alt2.sin_addr.s_addr = inet_addr("202.202.202.202");

关于C gethostbyname 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32429682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com