diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-08-20 19:09:52 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-08-20 19:09:52 -0400 |
| commit | 5f3d59f0ac78e01bd50419dab54d7fcbae15f17c (patch) | |
| tree | 59313f6debbbaafc18dafc5dfed6a479cede8cef /std/net.zig | |
| parent | c39bb3ebc49096af45f3a69d4742e5f4d50cab62 (diff) | |
| parent | 3b5a8858c29582daf37856534abe150b568a7bb7 (diff) | |
| download | zig-5f3d59f0ac78e01bd50419dab54d7fcbae15f17c.tar.gz zig-5f3d59f0ac78e01bd50419dab54d7fcbae15f17c.zip | |
Merge branch 'master' into llvm9
Diffstat (limited to 'std/net.zig')
| -rw-r--r-- | std/net.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/std/net.zig b/std/net.zig index efcbf7000d..be9d18056c 100644 --- a/std/net.zig +++ b/std/net.zig @@ -215,3 +215,33 @@ test "std.net.parseIp6" { assert(addr.addr[1] == 0x01); assert(addr.addr[2] == 0x00); } + +pub fn connectUnixSocket(path: []const u8) !std.fs.File { + const opt_non_block = if (std.event.Loop.instance != null) os.SOCK_NONBLOCK else 0; + const sockfd = try os.socket( + os.AF_UNIX, + os.SOCK_STREAM | os.SOCK_CLOEXEC | opt_non_block, + 0, + ); + errdefer os.close(sockfd); + + var sock_addr = os.sockaddr{ + .un = os.sockaddr_un{ + .family = os.AF_UNIX, + .path = undefined, + }, + }; + + if (path.len > @typeOf(sock_addr.un.path).len) return error.NameTooLong; + mem.copy(u8, sock_addr.un.path[0..], path); + const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len); + if (std.event.Loop.instance) |loop| { + try os.connect_async(sockfd, &sock_addr, size); + try loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); + try os.getsockoptError(sockfd); + } else { + try os.connect(sockfd, &sock_addr, size); + } + + return std.fs.File.openHandle(sockfd); +} |
