C 语言中带有 getaddrinfo() 的 Unix 套接字

发布于 2024-11-27 14:37:31 字数 424 浏览 2 评论 0原文

有谁知道是否可以在 C (AF_UNIX) 中将 getaddrinfo 与 unix 套接字一起使用。我尝试了一些方法,但无法使其发挥作用。 这基本上就是我正在尝试的:

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
}

我的问题是如何填充节点和服务字段,以防可以将其与 unix 套接字一起使用。
提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花辞树 2024-12-04 14:37:31

getaddrinfo() 的某些实现支持 AF_UNIX 套接字地址,但出于安全考虑,它们不再这样做。

无论如何,您实际上并不需要一个函数来“查找”AF_UNIX 地址 - 如果您知道套接字路径,那么您可以将其直接复制到 sockaddr_un 中足够的尺寸。 AF_UNIX 地址没有解析步骤 - 套接字名称​​是套接字地址。

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.

甜柠檬 2024-12-04 14:37:31

来自 man 3 getaddrinfo

hints 参数指定首选套接字类型或协议。 NULL 提示指定任何网络地址或协议都是可接受的。如果此参数不为 NULL,则它指向一个 addrinfo 结构,其 ai_family、ai_socktype 和 ai_protocol 成员指定首选套接字类型。 ai_family 中的 AF_UNSPEC 指定任何协议族(例如 IPv4 或 IPv6)。 ai_socktype 或 ai_protocol 中的 0 指定也可以接受任何套接字类型或协议。 ai_flags 成员指定其他选项,定义如下。多个标志是通过对它们进行逻辑“或”运算来指定的。 hins 参数中的所有其他成员必须包含 0 或空指针。

所以看起来你需要传入 hints 参数来告诉它你想要一个 unix 套接字。

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.

烟火散人牵绊 2024-12-04 14:37:31

晚了很多,但只是为了记录,你尝试过这个 -
struct addrinfo 提示,*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 ->;是要连接的主机名或地址。此处使用 NULL 的调用会放置本地主机的地址,在这种情况下您指定hints.ai_flags=AI_PASSIVE。
parm2 ->;是预期服务的名称(http、ftp 等)或套接字所用于的该服务的相应端口号

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文