Home > Enterprise >  inet_ntop always returns the same IP
inet_ntop always returns the same IP

Time:02-17

Spending way too much time trying to figure out why inet_ntop is always returning the same IP address of 2.0.19.86 inside of my barebones C UDP socket program.

Here is the code:


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

#define SERVERPORT "4950"    // the port users will be connecting to

int main(int argc, char *argv[])
{
    int sock;
    struct addrinfo addr_type, *server_info, *p;
    int err;
    int numbytes;
    

    if (argc != 3) {
        fprintf(stderr,"usage: talker hostname message\n");
        exit(1);
    }

    //Specify type of response we want to git
    memset(&addr_type, 0, sizeof addr_type);
    addr_type.ai_family = AF_INET; // set to AF_INET to use IPv4
    addr_type.ai_socktype = SOCK_DGRAM;


    //Get the address info (like IP address) and store in server_info struct
    if ((err = getaddrinfo(argv[1], SERVERPORT, &addr_type, &server_info)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
        return 1;
    }

    // There might be multiple IP addresses...loop through and use the first one that works
    for(p = server_info; p != NULL; p = p->ai_next) {
        if ((sock = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("Error when creating socket");
            continue;
        }

        break;
    }

    if (p == NULL) {
        fprintf(stderr, "Client failed to create socket\n");
        return 2;
    }
    
    char s[INET_ADDRSTRLEN];
    inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
    printf("sending to %s....\n",s);

    if ((numbytes = sendto(sock, argv[2], strlen(argv[2]), 0,
             p->ai_addr, p->ai_addrlen)) == -1) {
        perror("Error sending message");
        exit(1);
    }


    printf("client sent %d bytes to %s\n", numbytes, argv[1]);


    freeaddrinfo(server_info);
    close(sock);

    return 0;
}

The lines I am particularly stuck on is:

    char s[INET_ADDRSTRLEN];
    inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
    printf("sending to %s....\n",s);

For example I run the program with ./client www.google.com hello and get the following:

sending to 2.0.19.86....
client sent 5 bytes to www.google.com

I run the program again with ./client localhost hello and inet_ntop still returns the same IP.

sending to 2.0.19.86....
client sent 5 bytes to localhost

No errors are being thrown when I am creating the socket, and the message sends successfully when I send it to the receiving program over localhost, why is inet_ntop still outputting this weird address?

CodePudding user response:

In your call to inet_ntop:

inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);

You're not passing in the correct structure. When AF_INET is passed as the first argument, the second argument should have type struct in_addr *, not struct sockaddr_in *.

You need to call out the sin_addr member which is of this type.

inet_ntop(AF_INET, &((struct sockaddr_in *)p->ai_addr)->sin_addr, s, sizeof s);
  • Related