| ... | ... | @@ -22,16 +22,31 @@ pub const Address = struct { |
| 22 | 22 | os_addr: OsAddress, |
| 23 | 23 | |
| 24 | 24 | pub fn initIp4(ip4: u32, _port: u16) Address { |
| 25 | | return Address{ |
| 26 | | .os_addr = os.sockaddr{ |
| 27 | | .in = os.sockaddr_in{ |
| 28 | | .family = os.AF_INET, |
| 29 | | .port = mem.nativeToBig(u16, _port), |
| 30 | | .addr = ip4, |
| 31 | | .zero = [_]u8{0} ** 8, |
| 25 | switch (builtin.os) { |
| 26 | .macosx, .ios, .watchos, .tvos, |
| 27 | .freebsd, .netbsd => return Address{ |
| 28 | .os_addr = os.sockaddr{ |
| 29 | .in = os.sockaddr_in{ |
| 30 | .len = @sizeOf(os.sockaddr_in), |
| 31 | .family = os.AF_INET, |
| 32 | .port = mem.nativeToBig(u16, _port), |
| 33 | .addr = ip4, |
| 34 | .zero = [_]u8{0} ** 8, |
| 35 | }, |
| 32 | 36 | }, |
| 33 | 37 | }, |
| 34 | | }; |
| 38 | .linux => return Address{ |
| 39 | .os_addr = os.sockaddr{ |
| 40 | .in = os.sockaddr_in{ |
| 41 | .family = os.AF_INET, |
| 42 | .port = mem.nativeToBig(u16, _port), |
| 43 | .addr = ip4, |
| 44 | .zero = [_]u8{0} ** 8, |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | else => @compileError("Address.initIp4 not implemented for this platform"), |
| 49 | } |
| 35 | 50 | } |
| 36 | 51 | |
| 37 | 52 | pub fn initIp6(ip6: Ip6Addr, _port: u16) Address { |
| ... | ... | @@ -286,6 +301,23 @@ test "std.net.parseIp6" { |
| 286 | 301 | std.testing.expect(mem.eql(u8, "[ff01::fb]:80", printed)); |
| 287 | 302 | } |
| 288 | 303 | |
| 304 | fn testIp4s(ips: []const []const u8) void { |
| 305 | var buffer : [18]u8 = undefined; |
| 306 | for (ips) |ip| { |
| 307 | var addr = Address.initIp4(parseIp4(ip) catch unreachable, 0); |
| 308 | var newIp = std.fmt.bufPrint(buffer[0..], "{}", addr) catch unreachable; |
| 309 | std.testing.expect(std.mem.eql(u8, ip, newIp[0..newIp.len - 2])); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | test "std.net.ip4s" { |
| 314 | testIp4s(([_][]const u8 { |
| 315 | "0.0.0.0" , |
| 316 | "255.255.255.255" , |
| 317 | "1.2.3.4", |
| 318 | "123.255.0.91", |
| 319 | })[0..]); |
| 320 | } |
| 289 | 321 | pub fn connectUnixSocket(path: []const u8) !fs.File { |
| 290 | 322 | const opt_non_block = if (std.event.Loop.instance != null) os.SOCK_NONBLOCK else 0; |
| 291 | 323 | const sockfd = try os.socket( |