authorgravatar for git@l4.pmLuna <git@l4.pm> 2019-11-09 12:51:33-03:00
committergravatar for git@l4.pmLuna <git@l4.pm> 2019-11-09 12:51:33-03:00
log05ae21b78ed58e621fd2c10456a3f100a8107e3a
tree88bec9dbb50e353c3f9e36b2503497abf36fa9a9
parentf4d8dc278b312fc3eccf33a37cfe89c7c012d6fd

make StreamServer.listen family-agnostic

- rename Address.parseUnix to Address.initUnix

1 files changed, 16 insertions(+), 16 deletions(-)

lib/std/net.zig+16-16
...@@ -40,21 +40,6 @@ pub const Address = extern union {...@@ -40,21 +40,6 @@ pub const Address = extern union {
40 return error.InvalidIPAddressFormat;40 return error.InvalidIPAddressFormat;
41 }41 }
4242
43 pub fn parseUnix(path: []const u8) !Address {
44 var sock_addr = os.sockaddr_un{
45 .family = os.AF_UNIX,
46 .path = undefined,
47 };
48
49 // this enables us to have the proper length of the socket in getOsSockLen
50 mem.zero(&sock_addr.path);
51
52 if (path.len > sock_addr.path.len) return error.NameTooLong;
53 mem.copy(u8, &sock_addr.path, path);
54
55 return Address{ .un = sock_addr };
56 }
57
58 pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {43 pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {
59 switch (family) {44 switch (family) {
60 os.AF_INET => return parseIp4(name, port),45 os.AF_INET => return parseIp4(name, port),
...@@ -230,6 +215,21 @@ pub const Address = extern union {...@@ -230,6 +215,21 @@ pub const Address = extern union {
230 };215 };
231 }216 }
232217
218 pub fn initUnix(path: []const u8) !Address {
219 var sock_addr = os.sockaddr_un{
220 .family = os.AF_UNIX,
221 .path = undefined,
222 };
223
224 // this enables us to have the proper length of the socket in getOsSockLen
225 mem.zero(&sock_addr.path);
226
227 if (path.len > sock_addr.path.len) return error.NameTooLong;
228 mem.copy(u8, &sock_addr.path, path);
229
230 return Address{ .un = sock_addr };
231 }
232
233 /// Returns the port in native endian.233 /// Returns the port in native endian.
234 pub fn getPort(self: Address) u16 {234 pub fn getPort(self: Address) u16 {
235 const big_endian_port = switch (self.any.family) {235 const big_endian_port = switch (self.any.family) {
...@@ -1306,7 +1306,7 @@ pub const StreamServer = struct {...@@ -1306,7 +1306,7 @@ pub const StreamServer = struct {
1306 pub fn listen(self: *StreamServer, address: Address) !void {1306 pub fn listen(self: *StreamServer, address: Address) !void {
1307 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;1307 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
1308 const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;1308 const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;
1309 const sockfd = try os.socket(os.AF_INET, sock_flags, os.IPPROTO_TCP);1309 const sockfd = try os.socket(address.any.family, sock_flags, os.IPPROTO_TCP);
1310 self.sockfd = sockfd;1310 self.sockfd = sockfd;
1311 errdefer {1311 errdefer {
1312 os.close(sockfd);1312 os.close(sockfd);