0

I am trying to find my source's IP yet it won't work.

void getSourceIp(struct hostent *sourceHost, struct ip *ipStruct)
{
    char sourceName[100];

    if (gethostname(sourceName,sizeof(sourceName)) < 0)
    {
        perror("Error in function gethostname().\n");
        exit(EXIT_FAILURE);
    }

    if ((sourceHost = gethostbyname(sourceName)) == NULL)
    {
        std::cout << "The source " << sourceName << " is unknown.\n";
        exit(EXIT_FAILURE);
    }

    ipStruct->ip_src = (*(struct in_addr *) sourceHost->h_addr_list);
    std::cout << "IP Address: " << inet_ntoa(ipStruct->ip_src);
}

MAIN function:

int main(int argc, char *argv[])
{
    struct hostent *sourceHostent       = NULL;
    struct hostent *destinationHostent  = NULL;
    struct ip *ip                       = NULL;

    getSourceIp(sourceHostent,ip);
    return 0;
}

The output I get is "The source macbook is unknown."

Teodora
  • 687
  • 2
  • 11
  • 21
  • 1
    "gethostbyname() not working" - probably it is, but your expectations don't match its behavior. –  Nov 23 '13 at 19:25
  • what do you mean? @H2CO3 – Teodora Nov 23 '13 at 19:27
  • sorry, I forgot to finish my comment... >.< Silly me. –  Nov 23 '13 at 19:28
  • No worries. Haha, nicely put, well it sure isn't working because I have checked with the debugger and the sourceHost structure remains NULL after the function call. Am I missing something? – Teodora Nov 23 '13 at 19:31
  • that doesn't mean that "they are not working". Inspect what kind of error occurred using the `herror()` or `hstrerror()` functions. –  Nov 23 '13 at 19:37
  • 1
    Does it exist a line within `/etc/hosts` with the name "macbook"? Try with a fully qualified name, like macbook.localnet or something like that. Try also with a known hostname, like "stackoverflow.com" and see if it can resolve it. – mcleod_ideafix Nov 23 '13 at 19:40
  • the herror is failed: Unknown host. I have checked /etc/hosts and there is no "macbook". What I don't get is why since I got the name using gethostname() function ... – Teodora Nov 23 '13 at 19:48
  • 2
    The name returned by `gethostbyname` is likely not in your DNS. You should look into using `getifaddrs` instead. – Holly Nov 23 '13 at 19:54
  • Additionally, you have some bugs in your code. Your `getSourceIp` won't pass the value out through the pointers like you expect. – Holly Nov 23 '13 at 19:55

1 Answers1

2

As I mentioned in the comments on the question, you're method has several issues. The first is that it that the results are not passed back to the calling function. You need to use a double pointer to do this.

Next, the value returned from gethostbyname is allocated in static memory, so it can be overwritten the next time you call the function. You need to copy the result into your own memory. This is non-trivial because you need to deep copy it and not just malloc(sizeof(struct hosting)). The complexity of doing this is why gethostbyname is depreciated. Depending on your target platform, there are much better options to do DNS lookup.

Even if the lookup succeeded in your code, you will get a SEGFAULT. You are passing the value NULL for ipStruct and then attempting to dereference and write to it. If you dereference a NULL pointer, you're going to have a bad time. You should spend some time to understand memory management in C using malloc and free.

Without seeing more code, I'm not sure what you're trying to do with your struct ip. It looks like you're trying to get one or more IP addresses of the host as char *, but you've missed that target by a considerable amount. I can offer a bit more help if you can elaborate on the intent.

This last piece is stylistic, but doing assignments in if statements is error prone and a generally bad idea. You should get out of the habit of doing it.

Holly
  • 5,270
  • 1
  • 24
  • 27
  • I am trying to implement ping. I have to get the source and destination IP addresses and send some ICMP echo requests. Thank you so much. Oh and I'm using a macbook. – Teodora Nov 23 '13 at 20:28
  • Why are you implementing Ping? It already exists. Is there something that you are attempting to do differently or is this a learning exercise? If it is a learning exercise, what constraints are on the solution (UI, libraries available, supported platforms, etc)? – Holly Nov 23 '13 at 20:36
  • It's my computer network's homework. The available libraries are the C/C++ ones, it should work on my mac for now. – Teodora Nov 23 '13 at 20:38
  • The final application should be similar to the mtr command in linux yet for now I'm just trying to send some icmp requests. I'm writing in Qt but I'm not allowed to use Qt libraries, not even for the sockets. I will be using raw ones and in the end I will also make a UI for the program. – Teodora Nov 23 '13 at 20:42
  • 1
    If the topic is something that interests you, there is an amazing book by [Stevens](http://www.amazon.com/Unix-Network-Programming-Volume-Networking/dp/0131411551/ref=sr_1_1?ie=UTF8&qid=1385239333&sr=8-1&keywords=Network+programming+by+stevens). – Holly Nov 23 '13 at 20:43
  • To implement PING, you do not need to know your current IP address. You can use `INADDR_ANY` with your `bind`. – Holly Nov 23 '13 at 20:46