3

I have a Raspberry Pi which has name resolution issues with Python's getaddrinfo. I traced the source code, perhaps wrongly, to the C function gethostbyaddr. So now I am trying to create a simple test to see what this function returns. Sockets programming, and C, is way over my head, but my attempt is:

#include <sys/socket.h>
#include <string.h>
#include <stdio.h>

static struct gai_afd {
    int a_af;
    int a_addrlen;
    int a_socklen;
    int a_off;
    const char *a_addrany;
    const char *a_loopback;
};

int main()
{
  struct hostent *hp;
  struct gai_afd *gai_afd;
  hp = gethostbyaddr("google.com", gai_afd->a_addrlen, AF_INET);
}

Compiling using gcc gives two warnings:

warning: useless storage class specifier in empty declaration [enabled by default]
In function ‘main’: warning: assignment makes pointer from integer without a cast [enabled by default]

Running a.out gives Segmentation fault.

What must I change to make the above work?

My objective is to find out why getaddrinfo is unable to resolve google.com when ping works fine on this very same machine. The trouble I am facing is here.

0

2 Answers 2

2

Pointers are not inited.

At least

int main()
{
  struct hostent *hp;
  struct gai_afd *gai_afd = malloc(sizeof(gai_afd));

  // ...

}

This is a little example to find infos:

#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
    int i;
    struct hostent *he, *inner_he;
    struct in_addr **addr_list;
    unsigned long ip;
    char *addressString;

    if (argc != 2) {
        fprintf(stderr,"usage: ghbn hostname\n");
        return 1;
    }

    if ((he = gethostbyname(argv[1])) == NULL) {  // get the host info
        herror("gethostbyname");
        return 2;
    }

    // print information about this host:
    printf("Official name is: %s\n", he->h_name);
    addr_list = (struct in_addr **)he->h_addr_list;
    for(i = 0; addr_list[i] != NULL; i++)
    {
        addressString = inet_ntoa(*addr_list[i]);

        printf("    IP addresse %d: %s \n", i, addressString);

        ip = inet_addr(addressString);

        inner_he = gethostbyaddr((const char *)&ip, sizeof(ip), AF_INET);
        if (inner_he != NULL)
            printf("Host name: %s\n", inner_he->h_name);
    }
    printf("\n");

    return 0;
}

You can launch it passing www.usa.com and output will be:

Official name is: www.usa.com
    IP addresse 0: 69.10.42.209 
Host name: lawyer.com
Sign up to request clarification or add additional context in comments.

Comments

0

You're not allocating any memory for your structs, and dereference an unintialised pointer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.