Get address family from socket. Linux

前端 未结 2 1768
轮回少年
轮回少年 2021-01-19 15:47

I want to write a C++ wrapper for the Linux Socket API. In the socket() ctor the adress family (AF) is requested. I dont want to require the AF in the connect() signature ag

相关标签:
2条回答
  • 2021-01-19 16:34

    POSIX-standard getsockname will help:

    int getsockname(int socket, struct sockaddr *restrict address,
           socklen_t *restrict address_len);
    

    it will fill in given struct sockaddr (upon success):

    struct sockaddr {
        unsigned short    sa_family;  // <- that's what you looking for
        char              sa_data[14];  
    }
    
    0 讨论(0)
  • 2021-01-19 16:45

    Damn. Searched about half an hour. Now after posting this question I found immiediately the answer.

    getsockopt([...]) with option SO_DOMAIN (see socket options)

    0 讨论(0)
提交回复
热议问题