C++ - How to get IP and port from struct sockaddr

I am trying to develop a mail milter with libmilter. Therefor I have to define some callback functions. Currently I struggle with the connect() function. The declaration looks like this:

sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, struct sockaddr *hostaddr);

When a client connects to the milter, this callback is called. Now I would like to log the hostname, IP and port. For the hostname this is easy. But I can not get it to work with the hostaddr.

I found some similar articles here on StackOverflow, but none worked for me. It looks like I have to convert the structure to either sockaddr_in or sockaddr_in6, but I get compiler problems like "member access into incomplete type 'struct sockaddr_in'"

This is what I tried:

struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
string ipandport;

switch (hostaddr->sa_family) {
    case AF_INET:
        sin = (struct sockaddr_in *) hostaddr;
        unsigned char *ip = (unsigned char *)&sin->sin_addr.s_addr;
        break;
    case AF_INET6:
        // TODO
        break;
    default:
        ipandport = "unknown";
}

I never thought that it could be so hard to get such trivial information :-)

Upvotes: 5

Views: 23045

Answers (4)

Stian Skjelstad
Stian Skjelstad

Reputation: 2335

non-compile tested example for IPv4. IPv6 is almost the same, just adjust the constant-defintions used (read the inet_ntop man page)

#include <arpa/inet.h>

void print_ipv4(struct sockaddr *s)
{
 struct sockaddr_in *sin = (struct sockaddr_in *)s;
 char ip[INET_ADDRSTRLEN];
 uint16_t port;
 
 inet_ntop (AF_INET, sin->sin_addr, ip, sizeof (ip));
 port = htons (sin->sin_port);
 
 printf ("host %s:%d\n", ip, port);
}

Upvotes: 4

saeed_m
saeed_m

Reputation: 175

struct sockaddr_in  client, server;
SOCKET s , new_socket;
s = socket(AF_INET , SOCK_STREAM , 0 )

server.sin_family       = AF_INET;
server.sin_addr.s_addr  = INADDR_ANY;
server.sin_port         = htons( 8888 );

bind(s ,(struct sockaddr *)&server , sizeof(server))
//pending connection in queue is 1
listen(s , 1);

new_socket = accept(s , (struct sockaddr *)&client, &c);
printf ("client remote port: %i\n", ntohs(client.sin_port) );

Upvotes: 0

fadedbee
fadedbee

Reputation: 44807

inet_ntop is needed to convert from binary to string, not inet_pton.

$ man inet_ntop
...
This  function  converts  the  network address structure src in the af address family into a character string.  The
resulting string is copied to the buffer pointed to by dst, which must be a non-null pointer.  The caller specifies
the number of bytes available in this buffer in the argument size.

Example:

#include <arpa/inet.h>
...
char address[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &my_sockaddr_in.sin_addr, address, sizeof(address));

Upvotes: 4

I found a different answer. After posting here, I googled again and found this, which finally works for me:

sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, struct sockaddr *hostaddr) {
assert(ctx != NULL);
assert(hostaddr != NULL);

string ipandport;

char clienthost[NI_MAXHOST];
char clientport[NI_MAXSERV];
int result = getnameinfo(hostaddr, sizeof(*hostaddr),
                         clienthost, sizeof(clienthost),
                         clientport, sizeof (clientport),
                         NI_NUMERICHOST | NI_NUMERICSERV);

if(result != 0)
    ipandport = "unknown";
else
{
    if (hostaddr->sa_family == AF_INET)
        ipandport = string(clienthost) + ":" + string(clientport);
    else
        ipandport = "[" + string(clienthost) + "]:" + string(clientport);
}

if (hostname == nullptr)
    hostname = const_cast<char *>("unknown");

Client *con = new Client(string(hostname), ipandport);

// Store new client data
smfi_setpriv(ctx, static_cast<void *>(con));

cout << "id=" << con->getId()
        << " connect from " << con->getHostname()
        << " IP and port " << con->getIPandPort() << endl;

return SMFIS_CONTINUE;
}

Upvotes: 1

Related Questions