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 {
4040 return error.InvalidIPAddressFormat;
4141 }
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
5843 pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {
5944 switch (family) {
6045 os.AF_INET => return parseIp4(name, port),
......@@ -230,6 +215,21 @@ pub const Address = extern union {
230215 };
231216 }
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
233233 /// Returns the port in native endian.
234234 pub fn getPort(self: Address) u16 {
235235 const big_endian_port = switch (self.any.family) {
......@@ -1306,7 +1306,7 @@ pub const StreamServer = struct {
13061306 pub fn listen(self: *StreamServer, address: Address) !void {
13071307 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
13081308 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);
13101310 self.sockfd = sockfd;
13111311 errdefer {
13121312 os.close(sockfd);