authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-04 14:52:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-04 14:52:15-07:00
logf2bc5ccbc2f322b625719d374bbb09a4335c1bd1
treef36d534f21e3414b5ee496d06aa6d130dc0fbfc9
parent66ed7a5eb51580fb8f02e16f16ccad31abe0bcc1

more networking standard library code


2 files changed, 214 insertions(+), 6 deletions(-)

std/linux.zig+159-5
......@@ -80,9 +80,9 @@ const SIG_BLOCK = 0;
8080const SIG_UNBLOCK = 1;
8181const SIG_SETMASK = 2;
8282
83const SOCK_STREAM = 1;
84const SOCK_DGRAM = 2;
85const SOCK_RAW = 3;
83pub const SOCK_STREAM = 1;
84pub const SOCK_DGRAM = 2;
85pub const SOCK_RAW = 3;
8686pub const SOCK_RDM = 4;
8787pub const SOCK_SEQPACKET = 5;
8888pub const SOCK_DCCP = 6;
......@@ -91,6 +91,135 @@ pub const SOCK_CLOEXEC = 0o2000000;
9191pub const SOCK_NONBLOCK = 0o4000;
9292
9393
94pub const PROTO_ip = 0o000;
95pub const PROTO_icmp = 0o001;
96pub const PROTO_igmp = 0o002;
97pub const PROTO_ggp = 0o003;
98pub const PROTO_ipencap = 0o004;
99pub const PROTO_st = 0o005;
100pub const PROTO_tcp = 0o006;
101pub const PROTO_egp = 0o010;
102pub const PROTO_pup = 0o014;
103pub const PROTO_udp = 0o021;
104pub const PROTO_hmp = 0o024;
105pub const PROTO_xns_idp = 0o026;
106pub const PROTO_rdp = 0o033;
107pub const PROTO_iso_tp4 = 0o035;
108pub const PROTO_xtp = 0o044;
109pub const PROTO_ddp = 0o045;
110pub const PROTO_idpr_cmtp = 0o046;
111pub const PROTO_ipv6 = 0o051;
112pub const PROTO_ipv6_route = 0o053;
113pub const PROTO_ipv6_frag = 0o054;
114pub const PROTO_idrp = 0o055;
115pub const PROTO_rsvp = 0o056;
116pub const PROTO_gre = 0o057;
117pub const PROTO_esp = 0o062;
118pub const PROTO_ah = 0o063;
119pub const PROTO_skip = 0o071;
120pub const PROTO_ipv6_icmp = 0o072;
121pub const PROTO_ipv6_nonxt = 0o073;
122pub const PROTO_ipv6_opts = 0o074;
123pub const PROTO_rspf = 0o111;
124pub const PROTO_vmtp = 0o121;
125pub const PROTO_ospf = 0o131;
126pub const PROTO_ipip = 0o136;
127pub const PROTO_encap = 0o142;
128pub const PROTO_pim = 0o147;
129pub const PROTO_raw = 0o377;
130
131pub const PF_UNSPEC = 0;
132pub const PF_LOCAL = 1;
133pub const PF_UNIX = PF_LOCAL;
134pub const PF_FILE = PF_LOCAL;
135pub const PF_INET = 2;
136pub const PF_AX25 = 3;
137pub const PF_IPX = 4;
138pub const PF_APPLETALK = 5;
139pub const PF_NETROM = 6;
140pub const PF_BRIDGE = 7;
141pub const PF_ATMPVC = 8;
142pub const PF_X25 = 9;
143pub const PF_INET6 = 10;
144pub const PF_ROSE = 11;
145pub const PF_DECnet = 12;
146pub const PF_NETBEUI = 13;
147pub const PF_SECURITY = 14;
148pub const PF_KEY = 15;
149pub const PF_NETLINK = 16;
150pub const PF_ROUTE = PF_NETLINK;
151pub const PF_PACKET = 17;
152pub const PF_ASH = 18;
153pub const PF_ECONET = 19;
154pub const PF_ATMSVC = 20;
155pub const PF_RDS = 21;
156pub const PF_SNA = 22;
157pub const PF_IRDA = 23;
158pub const PF_PPPOX = 24;
159pub const PF_WANPIPE = 25;
160pub const PF_LLC = 26;
161pub const PF_IB = 27;
162pub const PF_MPLS = 28;
163pub const PF_CAN = 29;
164pub const PF_TIPC = 30;
165pub const PF_BLUETOOTH = 31;
166pub const PF_IUCV = 32;
167pub const PF_RXRPC = 33;
168pub const PF_ISDN = 34;
169pub const PF_PHONET = 35;
170pub const PF_IEEE802154 = 36;
171pub const PF_CAIF = 37;
172pub const PF_ALG = 38;
173pub const PF_NFC = 39;
174pub const PF_VSOCK = 40;
175pub const PF_MAX = 41;
176
177pub const AF_UNSPEC = PF_UNSPEC;
178pub const AF_LOCAL = PF_LOCAL;
179pub const AF_UNIX = AF_LOCAL;
180pub const AF_FILE = AF_LOCAL;
181pub const AF_INET = PF_INET;
182pub const AF_AX25 = PF_AX25;
183pub const AF_IPX = PF_IPX;
184pub const AF_APPLETALK = PF_APPLETALK;
185pub const AF_NETROM = PF_NETROM;
186pub const AF_BRIDGE = PF_BRIDGE;
187pub const AF_ATMPVC = PF_ATMPVC;
188pub const AF_X25 = PF_X25;
189pub const AF_INET6 = PF_INET6;
190pub const AF_ROSE = PF_ROSE;
191pub const AF_DECnet = PF_DECnet;
192pub const AF_NETBEUI = PF_NETBEUI;
193pub const AF_SECURITY = PF_SECURITY;
194pub const AF_KEY = PF_KEY;
195pub const AF_NETLINK = PF_NETLINK;
196pub const AF_ROUTE = PF_ROUTE;
197pub const AF_PACKET = PF_PACKET;
198pub const AF_ASH = PF_ASH;
199pub const AF_ECONET = PF_ECONET;
200pub const AF_ATMSVC = PF_ATMSVC;
201pub const AF_RDS = PF_RDS;
202pub const AF_SNA = PF_SNA;
203pub const AF_IRDA = PF_IRDA;
204pub const AF_PPPOX = PF_PPPOX;
205pub const AF_WANPIPE = PF_WANPIPE;
206pub const AF_LLC = PF_LLC;
207pub const AF_IB = PF_IB;
208pub const AF_MPLS = PF_MPLS;
209pub const AF_CAN = PF_CAN;
210pub const AF_TIPC = PF_TIPC;
211pub const AF_BLUETOOTH = PF_BLUETOOTH;
212pub const AF_IUCV = PF_IUCV;
213pub const AF_RXRPC = PF_RXRPC;
214pub const AF_ISDN = PF_ISDN;
215pub const AF_PHONET = PF_PHONET;
216pub const AF_IEEE802154 = PF_IEEE802154;
217pub const AF_CAIF = PF_CAIF;
218pub const AF_ALG = PF_ALG;
219pub const AF_NFC = PF_NFC;
220pub const AF_VSOCK = PF_VSOCK;
221pub const AF_MAX = PF_MAX;
222
94223/// Get the errno from a syscall return value, or 0 for no error.
95224pub fn get_errno(r: isize) -> isize {
96225 if (r > -4096) -r else 0
......@@ -185,10 +314,35 @@ fn restore_signals(set: &sigset_t) {
185314
186315pub type sa_family_t = u16;
187316pub type socklen_t = u32;
317pub type in_addr_t = u32;
318
319export struct in_addr {
320 s_addr: in_addr_t,
321}
188322
189323export struct sockaddr {
190 sa_family: sa_family_t,
191 sa_data: [14]u8,
324 family: sa_family_t,
325 port: u16,
326 data: [12]u8,
327}
328
329export struct sockaddr_in {
330 family: sa_family_t,
331 port: u16,
332 addr: in_addr,
333 zero: [8]u8,
334}
335
336export struct sockaddr_in6 {
337 family: sa_family_t,
338 port: u16,
339 flowinfo: u32,
340 addr: in6_addr,
341 scope_id: u32,
342}
343
344export struct in6_addr {
345 addr: [16]u8,
192346}
193347
194348export struct iovec {
std/net.zig+55-1
......@@ -1,4 +1,58 @@
11const linux = @import("linux.zig");
2const errno = @import("errno.zig");
23
3pub fn open(hostname: []const u8) -> %void {
4pub error SigInterrupt;
5pub error Unexpected;
6pub error Io;
7
8struct Connection {
9 socket_fd: i32,
10
11 pub fn close(c: Connection) -> %void {
12 switch (linux.get_errno(linux.close(c.socket_fd))) {
13 0 => return,
14 errno.EBADF => unreachable{},
15 errno.EINTR => return error.SigInterrupt,
16 errno.EIO => return error.Io,
17 else => return error.Unexpected,
18 }
19 }
20}
21
22struct Address {
23 addr: linux.sockaddr,
24}
25
26pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address {
27 unreachable{} // TODO
28}
29
30pub fn connect_addr(addr: &Address, port: u16) -> %Connection {
31 addr.addr.port = port;
32
33 const socket_ret = linux.socket(linux.PF_INET, linux.SOCK_STREAM, linux.PROTO_tcp);
34 const socket_err = linux.get_errno(socket_ret);
35 if (socket_err > 0) {
36 // TODO figure out possible errors from socket()
37 return error.Unexpected;
38 }
39 const socket_fd = i32(socket_ret);
40
41 const connect_err = linux.get_errno(linux.connect(socket_fd, &addr.addr, @sizeof(linux.sockaddr)));
42 if (connect_err > 0) {
43 // TODO figure out possible errors from connect()
44 return error.Unexpected;
45 }
46
47 return Connection {
48 .socket_fd = socket_fd,
49 };
50}
51
52pub fn connect(hostname: []const u8, port: u16) -> %Connection {
53 var addrs_buf: [1]Address = undefined;
54 const addrs_slice = %return lookup(hostname, addrs_buf);
55 const main_addr = &addrs_slice[0];
56
57 return connect_addr(main_addr, port);
458}