开发者

getnameinfo prototype asks for sockaddr not sockaddr_in?

The getnameinfo prototype asks for sockaddr but I have only seen examples using sockaddr_in. Can this example be re-written for sockaddr ? sin_family becomes sa_family but what about sin_port and sin_addr ? How are they included in sa_data ?

struct sockaddr{
    unsigned short  sa_family;
    char            sa_data[14];
};

struct sockaddr_in{
    short           sin_family;
    unsigned short  sin_port;
    struct in_addr  sin_addr;
    char            sin_zero[8];
};


struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family  开发者_运维问答    = AF_INET;
sin.sin_addr.s_addr = inet_addr(IPvar);
sin.sin_port        = 0; // If 0, port is chosen by system

getnameinfo( (struct sockaddr *)&sin, sizeof(sin), buffervar, sizeof(buffervar), NULL, 0, 0);


struct sockaddr is a "super-class" of the concrete protocol address structures like struct sockaddr_in, struct sockaddr_in6, and struct sockaddr_un, etc. The getnameinfo(3) dispatches to a specific execution path based of the address family (the sa_family member.)

As far as memory is concerned - the three members of struct sockaddr_in are overlaid with the struct sockaddr's sa_data member. Take a look at Chapter 3 of the UnP book.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜