aboutsummaryrefslogtreecommitdiff
path: root/core/net/socket_linux.odin
blob: dcc48d3fa1bf204bed8d30412189f5242ab91fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// +build linux
/*
	Copyright 2022 Tetralux        <tetraluxonpc@gmail.com>
	Copyright 2022 Colin Davidson  <colrdavidson@gmail.com>
	Copyright 2022 Jeroen van Rijn <nom@duclavier.com>.
	Made available under Odin's BSD-3 license.

	List of contributors:
		Tetralux:        Initial implementation
		Colin Davidson:  Linux platform code, OSX platform code, Odin-native DNS resolver
		Jeroen van Rijn: Cross platform unification, code style, documentation
*/

/*
	Package net implements cross-platform Berkeley Sockets, DNS resolution and associated procedures.
	For other protocols and their features, see subdirectories of this package.
*/
package net

import "core:c"
import "core:os"
import "core:time"

Platform_Socket :: os.Socket

Create_Socket_Error :: enum c.int {
	Family_Not_Supported_For_This_Socket = c.int(os.EAFNOSUPPORT),
	No_Socket_Descriptors_Available = c.int(os.EMFILE),
	No_Buffer_Space_Available = c.int(os.ENOBUFS),
	No_Memory_Available_Available = c.int(os.ENOMEM),
	Protocol_Unsupported_By_System = c.int(os.EPROTONOSUPPORT),
	Wrong_Protocol_For_Socket = c.int(os.EPROTONOSUPPORT),
	Family_And_Socket_Type_Mismatch = c.int(os.EPROTONOSUPPORT),
}

create_socket :: proc(family: Address_Family, protocol: Socket_Protocol) -> (socket: Any_Socket, err: Network_Error) {
	c_type, c_protocol, c_family: int

	switch family {
	case .IP4:  c_family = os.AF_INET
	case .IP6:  c_family = os.AF_INET6
	case:
		unreachable()
	}

	switch protocol {
	case .TCP:  c_type = os.SOCK_STREAM; c_protocol = os.IPPROTO_TCP
	case .UDP:  c_type = os.SOCK_DGRAM;  c_protocol = os.IPPROTO_UDP
	case:
		unreachable()
	}

	sock, ok := os.socket(c_family, c_type, c_protocol)
	if ok != os.ERROR_NONE {
		err = Create_Socket_Error(ok)
		return
	}

	switch protocol {
	case .TCP:  return TCP_Socket(sock), nil
	case .UDP:  return UDP_Socket(sock), nil
	case:
		unreachable()
	}
}


Dial_Error :: enum c.int {
	Port_Required = -1,

	Address_In_Use = c.int(os.EADDRINUSE),
	In_Progress = c.int(os.EINPROGRESS),
	Cannot_Use_Any_Address = c.int(os.EADDRNOTAVAIL),
	Wrong_Family_For_Socket = c.int(os.EAFNOSUPPORT),
	Refused = c.int(os.ECONNREFUSED),
	Is_Listening_Socket = c.int(os.EACCES),
	Already_Connected = c.int(os.EISCONN),
	Network_Unreachable = c.int(os.ENETUNREACH), // Device is offline
	Host_Unreachable = c.int(os.EHOSTUNREACH), // Remote host cannot be reached
	No_Buffer_Space_Available = c.int(os.ENOBUFS),
	Not_Socket = c.int(os.ENOTSOCK),
	Timeout = c.int(os.ETIMEDOUT),
	Would_Block = c.int(os.EWOULDBLOCK), // TODO: we may need special handling for this; maybe make a socket a struct with metadata?
}

dial_tcp_from_endpoint :: proc(endpoint: Endpoint, options := default_tcp_options) -> (skt: TCP_Socket, err: Network_Error) {
	if endpoint.port == 0 {
		return 0, .Port_Required
	}

	family := family_from_endpoint(endpoint)
	sock := create_socket(family, .TCP) or_return
	skt = sock.(TCP_Socket)

	// NOTE(tetra): This is so that if we crash while the socket is open, we can
	// bypass the cooldown period, and allow the next run of the program to
	// use the same address immediately.
	_ = set_option(skt, .Reuse_Address, true)

	sockaddr := endpoint_to_sockaddr(endpoint)
	res := os.connect(Platform_Socket(skt), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
	if res != os.ERROR_NONE {
		err = Dial_Error(res)
		return
	}

	if options.no_delay {
		_ = set_option(sock, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
	}

	return
}


Bind_Error :: enum c.int {
	// Another application is currently bound to this endpoint.
	Address_In_Use = c.int(os.EADDRINUSE),
	// The address is not a local address on this machine.
	Given_Nonlocal_Address = c.int(os.EADDRNOTAVAIL),
	// To bind a UDP socket to the broadcast address, the appropriate socket option must be set.
	Broadcast_Disabled = c.int(os.EACCES),
	// The address family of the address does not match that of the socket.
	Address_Family_Mismatch = c.int(os.EFAULT),
	// The socket is already bound to an address.
	Already_Bound = c.int(os.EINVAL),
	// There are not enough ephemeral ports available.
	No_Ports_Available = c.int(os.ENOBUFS),
}

bind :: proc(skt: Any_Socket, ep: Endpoint) -> (err: Network_Error) {
	sockaddr := endpoint_to_sockaddr(ep)
	s := any_socket_to_socket(skt)
	res := os.bind(Platform_Socket(s), (^os.SOCKADDR)(&sockaddr), size_of(sockaddr))
	if res != os.ERROR_NONE {
		err = Bind_Error(res)
	}
	return
}


// This type of socket becomes bound when you try to send data.
// This is likely what you want if you want to send data unsolicited.
//
// This is like a client TCP socket, except that it can send data to any remote endpoint without needing to establish a connection first.
make_unbound_udp_socket :: proc(family: Address_Family) -> (skt: UDP_Socket, err: Network_Error) {
	sock := create_socket(family, .UDP) or_return
	skt = sock.(UDP_Socket)
	return
}

// This type of socket is bound immediately, which enables it to receive data on the port.
// Since it's UDP, it's also able to send data without receiving any first.
//
// This is like a listening TCP socket, except that data packets can be sent and received without needing to establish a connection first.
//
// The bound_address is the address of the network interface that you want to use, or a loopback address if you don't care which to use.
make_bound_udp_socket :: proc(bound_address: Address, port: int) -> (skt: UDP_Socket, err: Network_Error) {
	skt = make_unbound_udp_socket(family_from_address(bound_address)) or_return
	bind(skt, {bound_address, port}) or_return
	return
}



Listen_Error :: enum c.int {
	Address_In_Use = c.int(os.EADDRINUSE),
	Already_Connected = c.int(os.EISCONN),
	No_Socket_Descriptors_Available = c.int(os.EMFILE),
	No_Buffer_Space_Available = c.int(os.ENOBUFS),
	Nonlocal_Address = c.int(os.EADDRNOTAVAIL),
	Not_Socket = c.int(os.ENOTSOCK),
	Listening_Not_Supported_For_This_Socket = c.int(os.EOPNOTSUPP),
}

listen_tcp :: proc(interface_endpoint: Endpoint, backlog := 1000) -> (skt: TCP_Socket, err: Network_Error) {
	assert(backlog > 0 && i32(backlog) < max(i32))

	family := family_from_endpoint(interface_endpoint)
	sock := create_socket(family, .TCP) or_return
	skt = sock.(TCP_Socket)

	// NOTE(tetra): This is so that if we crash while the socket is open, we can
	// bypass the cooldown period, and allow the next run of the program to
	// use the same address immediately.
	//
	// TODO(tetra, 2022-02-15): Confirm that this doesn't mean other processes can hijack the address!
	set_option(sock, .Reuse_Address, true) or_return

	bind(sock, interface_endpoint) or_return

	res := os.listen(Platform_Socket(skt), backlog)
	if res != os.ERROR_NONE {
		err = Listen_Error(res)
		return
	}

	return
}



Accept_Error :: enum c.int {
	Not_Listening = c.int(os.EINVAL),
	No_Socket_Descriptors_Available_For_Client_Socket = c.int(os.EMFILE),
	No_Buffer_Space_Available = c.int(os.ENOBUFS),
	Not_Socket = c.int(os.ENOTSOCK),
	Not_Connection_Oriented_Socket = c.int(os.EOPNOTSUPP),
	Would_Block = c.int(os.EWOULDBLOCK), // TODO: we may need special handling for this; maybe make a socket a struct with metadata?
}

accept_tcp :: proc(sock: TCP_Socket, options := default_tcp_options) -> (client: TCP_Socket, source: Endpoint, err: Network_Error) {
	sockaddr: os.SOCKADDR_STORAGE_LH
	sockaddrlen := c.int(size_of(sockaddr))

	client_sock, ok := os.accept(Platform_Socket(sock), cast(^os.SOCKADDR) &sockaddr, &sockaddrlen)
	if ok != os.ERROR_NONE {
		err = Accept_Error(ok)
		return
	}
	client = TCP_Socket(client_sock)
	source = sockaddr_to_endpoint(&sockaddr)
	if options.no_delay {
		_ = set_option(client, .TCP_Nodelay, true) // NOTE(tetra): Not vital to succeed; error ignored
	}
	return
}



close :: proc(skt: Any_Socket) {
	s := any_socket_to_socket(skt)
	os.close(os.Handle(Platform_Socket(s)))
}



TCP_Recv_Error :: enum c.int {
	Shutdown = c.int(os.ESHUTDOWN),
	Not_Connected = c.int(os.ENOTCONN),
	Connection_Broken = c.int(os.ENETRESET),
	Not_Socket = c.int(os.ENOTSOCK),
	Aborted = c.int(os.ECONNABORTED),
	Connection_Closed = c.int(os.ECONNRESET), // TODO(tetra): Determine when this is different from the syscall returning n=0 and maybe normalize them?
	Offline = c.int(os.ENETDOWN),
	Host_Unreachable = c.int(os.EHOSTUNREACH),
	Interrupted = c.int(os.EINTR),
	Timeout = c.int(os.EWOULDBLOCK), // NOTE: No, really. Presumably this means something different for nonblocking sockets...
}

recv_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_read: int, err: Network_Error) {
	if len(buf) <= 0 {
		return
	}
	res, ok := os.recv(Platform_Socket(skt), buf, 0)
	if ok != os.ERROR_NONE {
		err = TCP_Recv_Error(ok)
		return
	}
	return int(res), nil
}

UDP_Recv_Error :: enum c.int {
	// The buffer is too small to fit the entire message, and the message was truncated.
	// When this happens, the rest of message is lost.
	Buffer_Too_Small = c.int(os.EMSGSIZE),
	// The so-called socket is not an open socket.
	Not_Socket = c.int(os.ENOTSOCK),
	// The so-called socket is, in fact, not even a valid descriptor.
	Not_Descriptor = c.int(os.EBADF),
	// The buffer did not point to a valid location in memory.
	Bad_Buffer = c.int(os.EFAULT),
	// A signal occurred before any data was transmitted.
	// See signal(7).
	Interrupted = c.int(os.EINTR),
	// The send timeout duration passed before all data was received.
	// See Socket_Option.Receive_Timeout.
	Timeout = c.int(os.EWOULDBLOCK), // NOTE: No, really. Presumably this means something different for nonblocking sockets...
	// The socket must be bound for this operation, but isn't.
	Socket_Not_Bound = c.int(os.EINVAL),
}

recv_udp :: proc(skt: UDP_Socket, buf: []byte) -> (bytes_read: int, remote_endpoint: Endpoint, err: Network_Error) {
	if len(buf) <= 0 {
		return
	}

	from: os.SOCKADDR_STORAGE_LH = ---
	fromsize := c.int(size_of(from))

	// NOTE(tetra): On Linux, if the buffer is too small to fit the entire datagram payload, the rest is silently discarded,
	// and no error is returned.
	// However, if you pass MSG_TRUNC here, 'res' will be the size of the incoming message, rather than how much was read.
	// We can use this fact to detect this condition and return .Buffer_Too_Small.
	res, ok := os.recvfrom(Platform_Socket(skt), buf, os.MSG_TRUNC, cast(^os.SOCKADDR) &from, &fromsize)
	if ok != os.ERROR_NONE {
		err = UDP_Recv_Error(ok)
		return
	}

	bytes_read = int(res)
	remote_endpoint = sockaddr_to_endpoint(&from)

	if bytes_read > len(buf) {
		// NOTE(tetra): The buffer has been filled, with a partial message.
		bytes_read = len(buf)
		err = .Buffer_Too_Small
	}

	return
}

recv :: proc{recv_tcp, recv_udp}



// TODO
TCP_Send_Error :: enum c.int {
	Aborted = c.int(os.ECONNABORTED), // TODO(tetra): merge with other errors?
	Connection_Closed = c.int(os.ECONNRESET),
	Not_Connected = c.int(os.ENOTCONN),
	Shutdown = c.int(os.ESHUTDOWN),
	// The send queue was full.
	// This is usually a transient issue.
	//
	// This also shouldn't normally happen on Linux, as data is dropped if it
	// doesn't fit in the send queue.
	No_Buffer_Space_Available = c.int(os.ENOBUFS),
	Offline = c.int(os.ENETDOWN),
	Host_Unreachable = c.int(os.EHOSTUNREACH),
	// A signal occurred before any data was transmitted.
	// See signal(7).
	Interrupted = c.int(os.EINTR),
	// The send timeout duration passed before all data was sent.
	// See Socket_Option.Send_Timeout.
	Timeout = c.int(os.EWOULDBLOCK), // NOTE: No, really. Presumably this means something different for nonblocking sockets...
	// The so-called socket is not an open socket.
	Not_Socket = c.int(os.ENOTSOCK),
}

// Repeatedly sends data until the entire buffer is sent.
// If a send fails before all data is sent, returns the amount
// sent up to that point.
send_tcp :: proc(skt: TCP_Socket, buf: []byte) -> (bytes_written: int, err: Network_Error) {
	for bytes_written < len(buf) {
		limit := min(int(max(i32)), len(buf) - bytes_written)
		remaining := buf[bytes_written:][:limit]
		res, ok := os.send(Platform_Socket(skt), remaining, 0)
		if ok != os.ERROR_NONE {
			err = TCP_Send_Error(ok)
			return
		}
		bytes_written += int(res)
	}
	return
}

// TODO
UDP_Send_Error :: enum c.int {
	// The message is too big. No data was sent.
	Message_Too_Long = c.int(os.EMSGSIZE),
	// TODO: not sure what the exact circumstances for this is yet
	Network_Unreachable = c.int(os.ENETUNREACH),
	// There are no more emphemeral outbound ports available to bind the socket to, in order to send.
	No_Outbound_Ports_Available = c.int(os.EAGAIN),
	// The send timeout duration passed before all data was sent.
	// See Socket_Option.Send_Timeout.
	Timeout = c.int(os.EWOULDBLOCK), // NOTE: No, really. Presumably this means something different for nonblocking sockets...
	// The so-called socket is not an open socket.
	Not_Socket = c.int(os.ENOTSOCK),
	// The so-called socket is, in fact, not even a valid descriptor.
	Not_Descriptor = c.int(os.EBADF),
	// The buffer did not point to a valid location in memory.
	Bad_Buffer = c.int(os.EFAULT),
	// A signal occurred before any data was transmitted.
	// See signal(7).
	Interrupted = c.int(os.EINTR),
	// The send queue was full.
	// This is usually a transient issue.
	//
	// This also shouldn't normally happen on Linux, as data is dropped if it
	// doesn't fit in the send queue.
	No_Buffer_Space_Available = c.int(os.ENOBUFS),
	// No memory was available to properly manage the send queue.
	No_Memory_Available = c.int(os.ENOMEM),
}

// Sends a single UDP datagram packet.
//
// Datagrams are limited in size; attempting to send more than this limit at once will result in a Message_Too_Long error.
// UDP packets are not guarenteed to be received in order.
send_udp :: proc(skt: UDP_Socket, buf: []byte, to: Endpoint) -> (bytes_written: int, err: Network_Error) {
	toaddr := endpoint_to_sockaddr(to)
	res, os_err := os.sendto(Platform_Socket(skt), buf, 0, cast(^os.SOCKADDR) &toaddr, size_of(toaddr))
	if os_err != os.ERROR_NONE {
		err = UDP_Send_Error(os_err)
		return
	}
	bytes_written = int(res)
	return
}

send :: proc{send_tcp, send_udp}




Shutdown_Manner :: enum c.int {
	Receive = c.int(os.SHUT_RD),
	Send = c.int(os.SHUT_WR),
	Both = c.int(os.SHUT_RDWR),
}

Shutdown_Error :: enum c.int {
	Aborted = c.int(os.ECONNABORTED),
	Reset = c.int(os.ECONNRESET),
	Offline = c.int(os.ENETDOWN),
	Not_Connected = c.int(os.ENOTCONN),
	Not_Socket = c.int(os.ENOTSOCK),
	Invalid_Manner = c.int(os.EINVAL),
}

shutdown :: proc(skt: Any_Socket, manner: Shutdown_Manner) -> (err: Network_Error) {
	s := any_socket_to_socket(skt)
	res := os.shutdown(Platform_Socket(s), int(manner))
	if res != os.ERROR_NONE {
		return Shutdown_Error(res)
	}
	return
}




Socket_Option :: enum c.int {
	Reuse_Address = c.int(os.SO_REUSEADDR),
	Keep_Alive = c.int(os.SO_KEEPALIVE),
	Out_Of_Bounds_Data_Inline = c.int(os.SO_OOBINLINE),
	TCP_Nodelay = c.int(os.TCP_NODELAY),

	Linger = c.int(os.SO_LINGER),

	Receive_Buffer_Size = c.int(os.SO_RCVBUF),
	Send_Buffer_Size = c.int(os.SO_SNDBUF),
	Receive_Timeout = c.int(os.SO_RCVTIMEO_NEW),
	Send_Timeout = c.int(os.SO_SNDTIMEO_NEW),
}

Socket_Option_Error :: enum c.int {
	Offline = c.int(os.ENETDOWN),
	Timeout_When_Keepalive_Set = c.int(os.ENETRESET),
	Invalid_Option_For_Socket = c.int(os.ENOPROTOOPT),
	Reset_When_Keepalive_Set = c.int(os.ENOTCONN),
	Not_Socket = c.int(os.ENOTSOCK),
}

set_option :: proc(s: Any_Socket, option: Socket_Option, value: any, loc := #caller_location) -> Network_Error {
	level := os.SOL_SOCKET if option != .TCP_Nodelay else os.IPPROTO_TCP

	// NOTE(tetra, 2022-02-15): On Linux, you cannot merely give a single byte for a bool;
	//  it _has_ to be a b32.
	//  I haven't tested if you can give more than that.
	bool_value: b32
	int_value: i32
	timeval_value: os.Timeval

	ptr: rawptr
	len: os.socklen_t

	switch option {
	case
		.Reuse_Address,
		.Keep_Alive,
		.Out_Of_Bounds_Data_Inline,
		.TCP_Nodelay:
		// TODO: verify whether these are options or not on Linux
		// .Broadcast,
		// .Conditional_Accept,
		// .Dont_Linger:
			switch x in value {
			case bool, b8:
				x2 := x
				bool_value = b32((^bool)(&x2)^)
			case b16:
				bool_value = b32(x)
			case b32:
				bool_value = b32(x)
			case b64:
				bool_value = b32(x)
			case:
				panic("set_option() value must be a boolean here", loc)
			}
			ptr = &bool_value
			len = size_of(bool_value)
	case
		.Linger,
		.Send_Timeout,
		.Receive_Timeout:
			t, ok := value.(time.Duration)
			if !ok do panic("set_option() value must be a time.Duration here", loc)

			nanos := time.duration_nanoseconds(t)
			timeval_value.nanoseconds = int(nanos % 1e9)
			timeval_value.seconds = (nanos - i64(timeval_value.nanoseconds)) / 1e9

			ptr = &timeval_value
			len = size_of(timeval_value)
	case
		.Receive_Buffer_Size,
		.Send_Buffer_Size:
			// TODO: check for out of range values and return .Value_Out_Of_Range?
			switch i in value {
			case i8, u8:   i2 := i; int_value = os.socklen_t((^u8)(&i2)^)
			case i16, u16: i2 := i; int_value = os.socklen_t((^u16)(&i2)^)
			case i32, u32: i2 := i; int_value = os.socklen_t((^u32)(&i2)^)
			case i64, u64: i2 := i; int_value = os.socklen_t((^u64)(&i2)^)
			case i128, u128: i2 := i; int_value = os.socklen_t((^u128)(&i2)^)
			case int, uint: i2 := i; int_value = os.socklen_t((^uint)(&i2)^)
			case:
				panic("set_option() value must be an integer here", loc)
			}
			ptr = &int_value
			len = size_of(int_value)
	}

	skt := any_socket_to_socket(s)
	res := os.setsockopt(Platform_Socket(skt), int(level), int(option), ptr, len)
	if res != os.ERROR_NONE {
		return Socket_Option_Error(res)
	}

	return nil
}