C++ -如何从struct sockaddr中获取IP和端口

dvtswwa3  于 2023-01-06  发布在  其他
关注(0)|答案(4)|浏览(249)

我正在尝试用libmilter开发一个邮件过滤器,为此我必须定义一些回调函数,目前我正在纠结于connect()函数,声明如下:

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

当一个客户端连接到milter时,这个回调函数被调用。现在我想记录主机名,IP和端口。对于主机名来说这很容易。但是我不能让它和hostaddr一起工作。
我在StackOverflow上找到了一些类似的文章,但是没有一篇适合我。看起来我必须将结构转换为sockaddr_in或sockaddr_in6,但是我遇到了编译器问题,比如"成员访问不完整的类型'struct sockaddr_in'"
我是这么试的:

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";
}

我从来没有想过,得到这么琐碎的信息会这么难:-)

t9aqgxwy

t9aqgxwy1#

从二进制转换为字符串需要inet_ntop,而不是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.

示例:

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

p1tboqfb2#

IPv4的非编译测试示例。IPv6几乎相同,只是调整了所使用的常量定义(请阅读inet_ntop手册页)

#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);
}
k4emjkb1

k4emjkb13#

我发现了一个不同的答案。在这里发帖后,我再次谷歌搜索,发现了这个,它终于对我起作用了:

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;
}
ilmyapht

ilmyapht4#

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) );

相关问题