tags:

views:

11

answers:

2

Hi.

Is it possible to decipher the hostname given the sockaddr_in strucure?

struct sockaddr_in {
    short            sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr   sin_addr;     // see struct in_addr, below
    char             sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};

I'd tried printing out "udp_server.sin_addr.s_addr" (where udp_server is of type struct sockaddr_in) and it printed me a number.

I believe the s_addr variable is converted to network long? How do I convert it into a readable format so I can determine the host?

+1  A: 

There's a number of functions for transforming the number you mentioned (a network address in long format) to human-readable addresses.

Take a look at inet_pton() and inet_ntop() (Presentation to Number, Number to Presentation). Those are AF_INET-specific, and work for both IPv4 and IPv6 if I'm not mistaken.

Suerte!

Santiago Lezica
+1  A: 

Yes, it's the IP address in network byte order. Use the inet_ntoa(3) or inet_ntop(3) to get back a string representation of the IP.

Nikolai N Fetissov