Michael Lafayette
Michael Lafayette

Reputation: 3072

C gethostbyname Seg Fault

I am trying to modify the Linux tracepath (traceroute) program to make it handle three different ip addresses. The three ip addresses are:

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

It is supposed to convert them to socket address structs and unsigned integer values and the like and also back into strings, but I keep getting seg fault.

#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;
}

Terminal output:

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. 

Why does my conversion between string and hostent / sockaddr_in and back keep failing?

Upvotes: 0

Views: 636

Answers (2)

Davislor
Davislor

Reputation: 15144

Here you go. The lines that use gethostbyname() are both corrected and commented out with #if 0. (Edited to check for buffer overflows that “can’t happen.”)

#define _POSIX_C_SOURCE     200809L
#define _XOPEN_SOURCE       700
#define _ISOC99_SOURCE
#define _ISOC11_SOURCE

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.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_storage target_host; // first address
struct sockaddr_storage target_alt1; // second address
struct sockaddr_storage target_alt2; // third address

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

#if 0
    struct hostent hostname_target_entry;
    struct hostent hostname_alt1_entry;
    struct hostent hostname_alt2_entry;
    const struct hostent* p;
#endif

    struct addrinfo *hostaddr_temp = NULL;

    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);

#if 0 /* Simple fix, but not thread-safe. */
    p = gethostbyname(target_hostname);
    memcpy( &hostname_target_entry, p, sizeof(struct hostent) );
    p = gethostbyname(alternate_ip_1_local);
    memcpy( &hostname_alt1_entry, p, sizeof(struct hostent) );
    p = gethostbyname(alternate_ip_2_local);
    memcpy( &hostname_alt2_entry, p, sizeof(struct hostent) );
#endif


/* The correct fix.
 */
/* Change AF_INET to AF_UNSPECIFIED to support IPv6: */
    static const struct addrinfo hints = { .ai_flags = AI_NUMERICHOST, .ai_family = AF_INET };

    if ( 0 == getaddrinfo( target_hostname, NULL, &hints, &hostaddr_temp ) ) {
/* The POSIX standard "guarantees" that a sockaddr_storage will always
 * be big enough to hold any socket address, so this should never cause a
 * buffer overflow, but ....
 */
      assert( sizeof(target_host) >= hostaddr_temp[0].ai_addrlen );
      memcpy ( &target_host, hostaddr_temp[0].ai_addr, hostaddr_temp[0].ai_addrlen );
      freeaddrinfo(hostaddr_temp);
      assert( AF_INET == target_host.ss_family );
    }
    else
      perror("getaddrinfo");

    if ( 0 == getaddrinfo( alternate_ip_1_local, NULL, &hints, &hostaddr_temp ) ) {
      assert( sizeof(target_alt1) >= hostaddr_temp[0].ai_addrlen );
      memcpy ( &target_alt1, hostaddr_temp[0].ai_addr, hostaddr_temp[0].ai_addrlen );
      freeaddrinfo(hostaddr_temp);
      assert( AF_INET == target_alt1.ss_family );
    }
    else
      perror("getaddrinfo");

    if ( 0 == getaddrinfo( alternate_ip_2_local, NULL, &hints, &hostaddr_temp ) ) {
      assert( sizeof(target_alt2) >= hostaddr_temp[0].ai_addrlen );
      memcpy ( &target_alt2, hostaddr_temp[0].ai_addr, hostaddr_temp[0].ai_addrlen );
      freeaddrinfo(hostaddr_temp);
      assert( AF_INET == target_alt2.ss_family );
    }
    else
      perror("getaddrinfo");

    hostaddr_temp = NULL;

    // These should be three different ip addresses
    const in_addr_t* const host_ipv4 = &((struct sockaddr_in*)(&target_host))->sin_addr.s_addr;
    const in_addr_t* const alt1_ipv4 = &((struct sockaddr_in*)(&target_alt1))->sin_addr.s_addr;
    const in_addr_t* const alt2_ipv4 = &((struct sockaddr_in*)(&target_alt2))->sin_addr.s_addr;

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

    // This conversion should yield "11.11.11.11"
    const char * conversion1 = inet_ntop(AF_INET, host_ipv4, target_ip_string, INET_ADDRSTRLEN);
    printf("Result of inet_ntop: %s \n", conversion1);

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

    // This conversion should yield "144.133.133.133".
    const char * conversion2 = inet_ntop(AF_INET, alt1_ipv4, alternate_ip_1, INET_ADDRSTRLEN);
    printf("Result of inet_ntop: %s \n", conversion2);

    // This conversion should yield "202.202.202.202".
    const char * conversion3 = inet_ntop(AF_INET, alt2_ipv4, alternate_ip_2, INET_ADDRSTRLEN);
    printf("Result of inet_ntop: %s \n", conversion3);


    return 0;
}

Upvotes: 0

dbush
dbush

Reputation: 223927

The gethostbyname function returns a pointer to static data, so if you don't copy the results of a given call, it will get overwritten by the next call.

Since you're only converting IP addresses and not hostnames, you're better off using inet_addr instead:

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");

Upvotes: 2

Related Questions