简体   繁体   中英

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 sockets.
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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM