开发者

Unix sockets with getaddrinfo() in C

Does anyone know if it's possible to use getaddrinfo with unix sockets in C (AF_UNIX). I've tried a couple of things but I can't make it work. This is basically what I'm trying:

struct addrinfo hints, *res;

memset(&hints, 0, sizeof(hints));
hints.ai_family   = AF_UNIX;
hints.ai_socktype = SOCK_STREAM;
if(getaddrinfo("What should I put here?", "What should I put here?", &hints, &res) != 0){
    //do sth about
}

My question is how to fill the node and service fields, in case that is possible to use it with unix so开发者_运维技巧ckets.

Thanks in advance.


Some implementations of getaddrinfo() have supported AF_UNIX socket addresses, but they no longer do so due to security concerns.

You don't really need a function to "look up" an AF_UNIX address anyway - if you know the socket path, then you can just copy it straight into a sockaddr_un of sufficient size. There's no resolution step for AF_UNIX addresses - the socket name is the socket address.


From man 3 getaddrinfo:

The hints parameter specifies the preferred socket type, or protocol. A NULL hints specifies that any network address or protocol is acceptable. If this parameter is not NULL it points to an addrinfo structure whose ai_family, ai_socktype, and ai_protocol members specify the preferred socket type. AF_UNSPEC in ai_family specifies any protocol family (either IPv4 or IPv6, for example). 0 in ai_socktype or ai_protocol specifies that any socket type or protocol is acceptable as well. The ai_flags member specifies additional options, defined below. Multiple flags are specified by logically OR-ing them together. All the other members in the hints parameter must contain either 0, or a null pointer.

So it looks like you need to pass in the hints parameter to tell it you want a unix socket.


A lot late, but just for the records, did you try this - struct addrinfo hints, *res;

memset(&hints, 0, sizeof(hints));
hints.ai_family   = AF_UNSPEC;   //is the address family(IPV4 or 6) or UNSPEC for either
hints.ai_socktype = SOCK_STREAM; 
if(getaddrinfo(parm1, parm2, &hints, &res) != 0){
    //do sth about
}

parm1 -> is the host name or address to connect to. A call with NULL here puts the address of local host, in which case you specify hints.ai_flags=AI_PASSIVE. parm2 -> is the name of the intended service (http, ftp etc) or the corresponding port number for that service the socket is used for

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜