posix-socket 0.2.0

thin wrapper around POSIX sockets
Documentation
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
533
534
535
536
537
538
539
540
use filedesc::FileDesc;
use std::io::{IoSlice, IoSliceMut};
use std::os::raw::{c_int, c_void};
use std::os::unix::io::{RawFd, AsRawFd, IntoRawFd, FromRawFd};

use crate::AsSocketAddress;
use crate::ancillary::SocketAncillary;

/// A POSIX socket.
pub struct Socket<Address> {
	fd: FileDesc,
	_address: std::marker::PhantomData<fn() -> Address>,
}

#[cfg(not(any(target_os = "apple", target_os = "solaris")))]
mod extra_flags {
	pub const SENDMSG: std::os::raw::c_int = libc::MSG_NOSIGNAL;
	pub const RECVMSG: std::os::raw::c_int = libc::MSG_CMSG_CLOEXEC;
}

#[cfg(any(target_os = "apple", target_os = "solaris"))]
mod extra_flags {
	pub const SENDMSG: std::os::raw::c_int = 0;
	pub const RECVMSG: std::os::raw::c_int = 0;
}

impl<Address: AsSocketAddress> Socket<Address> {
	/// Wrap a file descriptor in a Socket.
	///
	/// On Apple systems, this sets the SO_NOSIGPIPE option to prevent SIGPIPE signals.
	fn wrap(fd: FileDesc) -> std::io::Result<Self> {
		let wrapped = Self {
			fd,
			_address: std::marker::PhantomData,
		};

		#[cfg(target_os = "apple")]
		wrapped.set_option(libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1 as c_int)?;

		Ok(wrapped)
	}

	/// Create a new socket with the specified type and protocol.
	///
	/// The domain is taken from the `Address` type.
	///
	/// The created socket has the `close-on-exec` flag set.
	/// The flag will be set atomically when the socket is created if the platform supports it.
	///
	/// See `man socket` for more information.
	pub fn new(kind: c_int, protocol: c_int) -> std::io::Result<Self>
	where
		Address: crate::SpecificSocketAddress,
	{
		Self::new_generic(Address::static_family() as c_int, kind, protocol)
	}

	/// Create a new socket with the specified domain, type and protocol.
	///
	/// Unless you are working with generic socket addresses,
	/// you should normally prefer `Self::new`.
	///
	/// The created socket has the `close-on-exec` flag set.
	/// The flag will be set atomically when the socket is created if the platform supports it.
	///
	/// See `man socket` for more information.
	pub fn new_generic(domain: c_int, kind: c_int, protocol: c_int) -> std::io::Result<Self> {
		socket(domain, kind | libc::SOCK_CLOEXEC, protocol)
			.or_else(|e| {
				// Fall back to setting close-on-exec after creation if SOCK_CLOEXEC is not supported.
				if e.raw_os_error() == Some(libc::EINVAL) {
					let fd = socket(domain, kind, protocol)?;
					fd.set_close_on_exec(true)?;
					Ok(fd)
				} else {
					Err(e)
				}
			})
			.and_then(Self::wrap)
	}

	/// Create a connected pair of socket with the specified type and protocol.
	///
	/// The domain is taken from the `Address` type.
	///
	/// The created sockets have the `close-on-exec` flag set.
	/// The flag will be set atomically when the sockets are created if the platform supports it.
	///
	/// See `man socketpair` and `man socket` for more information.
	pub fn pair(kind: c_int, protocol: c_int) -> std::io::Result<(Self, Self)>
	where
		Address: crate::SpecificSocketAddress,
	{
		Self::pair_generic(Address::static_family() as c_int, kind, protocol)
	}

	/// Create a connected pair of socket with the specified domain, type and protocol.
	///
	/// Unless you are working with generic socket addresses,
	/// you should normally prefer `Self::pair`.
	///
	/// The created sockets have the `close-on-exec` flag set.
	/// The flag will be set atomically when the sockets are created if the platform supports it.
	///
	/// See `man socketpair` and `man socket` for more information.
	pub fn pair_generic(domain: c_int, kind: c_int, protocol: c_int) -> std::io::Result<(Self, Self)> {
		socketpair(domain, kind | libc::SOCK_CLOEXEC, protocol)
			.or_else(|e| {
				// Fall back to setting close-on-exec after creation if SOCK_CLOEXEC is not supported.
				if e.raw_os_error() == Some(libc::EINVAL) {
					let (a, b) = socketpair(domain, kind, protocol)?;
					a.set_close_on_exec(true)?;
					b.set_close_on_exec(true)?;
					Ok((a, b))
				} else {
					Err(e)
				}
			})
			.and_then(|(a, b)| {
				Ok((Self::wrap(a)?, Self::wrap(b)?))
			})
	}

	/// Try to clone the socket.
	///
	/// This is implemented by duplicating the file descriptor.
	/// The returned [`Socket`] refers to the same kernel object.
	///
	/// The underlying file descriptor of the new socket will have the `close-on-exec` flag set.
	/// If the platform supports it, the flag will be set atomically when the file descriptor is duplicated.
	pub fn try_clone(&self) -> std::io::Result<Self> {
		Ok(Self {
			fd: self.fd.duplicate()?,
			_address: std::marker::PhantomData,
		})
	}

	/// Wrap a raw file descriptor in a [`Socket`].
	///
	/// This function sets no flags or options on the file descriptor or socket.
	/// It is your own responsibility to make sure the close-on-exec flag is already set,
	/// and that the `SO_NOSIGPIPE` option is set on Apple platforms.
	pub unsafe fn from_raw_fd(fd: RawFd) -> Self {
		Self {
			fd: FileDesc::from_raw_fd(fd),
			_address: std::marker::PhantomData,
		}
	}

	/// Get the raw file descriptor.
	///
	/// This function does not release ownership of the underlying file descriptor.
	/// The file descriptor will still be closed when the [`FileDesc`] is dropped.
	pub fn as_raw_fd(&self) -> RawFd {
		self.fd.as_raw_fd()
	}

	/// Release and get the raw file descriptor.
	///
	/// This function releases ownership of the underlying file descriptor.
	/// The file descriptor will not be closed.
	pub fn into_raw_fd(self) -> RawFd {
		self.fd.into_raw_fd()
	}

	/// Set a socket option.
	///
	/// See `man setsockopt` for more information.
	fn set_option<T: Copy>(&self, level: c_int, option: c_int, value: T) -> std::io::Result<()> {
		unsafe {
			let value = &value as *const T as *const c_void;
			let length = std::mem::size_of::<T>() as libc::socklen_t;
			check_ret(libc::setsockopt(self.as_raw_fd(), level, option, value, length))?;
			Ok(())
		}
	}

	/// Get the value of a socket option.
	///
	/// See `man getsockopt` for more information.
	fn get_option<T: Copy>(&self, level: c_int, option: c_int) -> std::io::Result<T> {
		unsafe {
			let mut output = std::mem::MaybeUninit::zeroed();
			let output_ptr = output.as_mut_ptr() as *mut c_void;
			let mut length = std::mem::size_of::<T>() as libc::socklen_t;
			check_ret(libc::getsockopt(self.as_raw_fd(), level, option, output_ptr, &mut length))?;
			assert_eq!(length, std::mem::size_of::<T>() as libc::socklen_t);
			Ok(output.assume_init())
		}
	}

	/// Put the socket in blocking or non-blocking mode.
	pub fn set_nonblocking(&self, non_blocking: bool) -> std::io::Result<()> {
		self.set_option(libc::SOL_SOCKET, libc::O_NONBLOCK, bool_to_c_int(non_blocking))
	}

	/// Check if the socket in blocking or non-blocking mode.
	pub fn get_nonblocking(&self) -> std::io::Result<bool> {
		let raw: c_int = self.get_option(libc::SOL_SOCKET, libc::O_NONBLOCK)?;
		Ok(raw != 0)
	}

	/// Gets the value of the SO_ERROR option on this socket.
	///
	/// This will retrieve the stored error in the underlying socket, clearing the field in the process.
	/// This can be useful for checking errors between calls.
	pub fn take_error(&self) -> std::io::Result<Option<std::io::Error>> {
		let raw: c_int = self.get_option(libc::SOL_SOCKET, libc::SO_ERROR)?;
		if raw == 0 {
			Ok(None)
		} else {
			Ok(Some(std::io::Error::from_raw_os_error(raw)))
		}
	}

	/// Get the local address the socket is bound to.
	pub fn local_addr(&self) -> std::io::Result<Address> {
		unsafe {
			let mut address = std::mem::MaybeUninit::<Address>::zeroed();
			let mut len = Address::max_len();
			check_ret(libc::getsockname(self.as_raw_fd(), Address::as_sockaddr_mut(&mut address), &mut len))?;
			Address::finalize(address, len)
		}
	}

	/// Get the remote address the socket is connected to.
	pub fn peer_addr(&self) -> std::io::Result<Address> {
		unsafe {
			let mut address = std::mem::MaybeUninit::<Address>::zeroed();
			let mut len = Address::max_len();
			check_ret(libc::getpeername(self.as_raw_fd(), Address::as_sockaddr_mut(&mut address), &mut len))?;
			Address::finalize(address, len)
		}
	}

	/// Connect the socket to a remote address.
	///
	/// It depends on the exact socket type what it means to connect the socket.
	/// See `man connect` for more information.
	pub fn connect(&self, address: &Address) -> std::io::Result<()> {
		unsafe {
			check_ret(libc::connect(self.as_raw_fd(), address.as_sockaddr(), address.len()))?;
			Ok(())
		}
	}

	/// Bind the socket to a local address.
	///
	/// It depends on the exact socket type what it means to bind the socket.
	/// See `man bind` for more information.
	pub fn bind(&self, address: &Address) -> std::io::Result<()> {
		unsafe {
			check_ret(libc::bind(self.as_raw_fd(), address.as_sockaddr(), address.len()))?;
			Ok(())
		}
	}

	/// Put the socket in listening mode, ready to accept connections.
	///
	/// Once the socket is in listening mode,
	/// new connections can be accepted with [`accept()`](Socket::accept).
	///
	/// Not all socket types can be put into listening mode.
	/// See `man listen` for more information.
	pub fn listen(&self, backlog: c_int) -> std::io::Result<()> {
		unsafe {
			check_ret(libc::listen(self.as_raw_fd(), backlog))?;
			Ok(())
		}
	}

	/// Accept a new connection on the socket.
	///
	/// The socket must have been put in listening mode
	/// with a call to [`listen()`](Socket::listen).
	///
	/// Not all socket types can be put into listening mode or accept connections.
	/// See `man listen` for more information.
	pub fn accept(&self) -> std::io::Result<(Self, Address)> {
		unsafe {
			let mut address = std::mem::MaybeUninit::zeroed();
			let mut len = Address::max_len();
			let fd = check_ret(libc::accept4(self.as_raw_fd(), Address::as_sockaddr_mut(&mut address), &mut len, libc::SOCK_CLOEXEC))?;
			let socket = Self::wrap(FileDesc::from_raw_fd(fd))?;
			let address = Address::finalize(address, len)?;
			Ok((socket, address))
		}
	}

	/// Send data over the socket to the connected peer.
	///
	/// Returns the number of transferred bytes, or an error.
	///
	/// See `man send` for more information.
	pub fn send(&self, data: &[u8], flags: c_int) -> std::io::Result<usize> {
		unsafe {
			let data_ptr = data.as_ptr() as *const c_void;
			let transferred = check_ret_isize(libc::send(self.as_raw_fd(), data_ptr, data.len(), flags | extra_flags::SENDMSG))?;
			Ok(transferred as usize)
		}
	}

	/// Send data over the socket to the specified address.
	///
	/// This function is only valid for connectionless protocols such as UDP or unix datagram sockets.
	///
	/// Returns the number of transferred bytes, or an error.
	///
	/// See `man sendto` for more information.
	pub fn send_to(&self, data: &[u8], address: &Address, flags: c_int) -> std::io::Result<usize> {
		unsafe {
			let data_ptr = data.as_ptr() as *const c_void;
			let transferred = check_ret_isize(libc::sendto(
				self.as_raw_fd(),
				data_ptr,
				data.len(),
				flags | extra_flags::SENDMSG,
				address.as_sockaddr(), address.len()
			))?;
			Ok(transferred as usize)
		}
	}

	/// Send a message over the socket to the connected peer.
	///
	/// Returns the number of transferred bytes, or an error.
	///
	/// See `man sendmsg` for more information.
	pub fn send_msg(&self, data: &[IoSlice], cdata: Option<&[u8]>, flags: c_int) -> std::io::Result<usize> {
		unsafe {
			let mut header = std::mem::zeroed::<libc::msghdr>();
			header.msg_iov = data.as_ptr() as *mut libc::iovec;
			header.msg_iovlen = data.len();
			header.msg_control = cdata.map(|x| x.as_ptr()).unwrap_or(std::ptr::null()) as *mut c_void;
			header.msg_controllen = cdata.map(|x| x.len()).unwrap_or(0);

			let ret = check_ret_isize(libc::sendmsg(self.as_raw_fd(), &header, flags | extra_flags::SENDMSG))?;
			Ok(ret as usize)
		}
	}

	/// Send a message over the socket to the specified address.
	///
	/// This function is only valid for connectionless protocols such as UDP or unix datagram sockets.
	///
	/// Returns the number of transferred bytes, or an error.
	///
	/// See `man sendmsg` for more information.
	pub fn send_msg_to(&self, address: &Address, data: &[IoSlice], cdata: Option<&[u8]>, flags: c_int) -> std::io::Result<usize> {
		unsafe {
			let mut header = std::mem::zeroed::<libc::msghdr>();
			header.msg_name = address.as_sockaddr() as *mut c_void;
			header.msg_namelen = address.len();
			header.msg_iov = data.as_ptr() as *mut libc::iovec;
			header.msg_iovlen = data.len();
			header.msg_control = cdata.map(|x| x.as_ptr()).unwrap_or(std::ptr::null()) as *mut c_void;
			header.msg_controllen = cdata.map(|x| x.len()).unwrap_or(0);

			let ret = check_ret_isize(libc::sendmsg(self.as_raw_fd(), &header, flags | extra_flags::SENDMSG))?;
			Ok(ret as usize)
		}
	}

	/// Receive a data on the socket from the connected peer.
	///
	/// Returns the number of transferred bytes, or an error.
	///
	/// See `man recv` for more information.
	pub fn recv(&self, buffer: &mut [u8], flags: c_int) -> std::io::Result<usize> {
		unsafe {
			let buffer_ptr = buffer.as_mut_ptr() as *mut c_void;
			let transferred = check_ret_isize(libc::recv(self.as_raw_fd(), buffer_ptr, buffer.len(), flags | extra_flags::RECVMSG))?;
			Ok(transferred as usize)
		}
	}

	/// Receive a data on the socket.
	///
	/// Returns the address of the sender and the number of transferred bytes, or an error.
	///
	/// See `man recvfrom` for more information.
	pub fn recv_from(&self, buffer: &mut [u8], flags: c_int) -> std::io::Result<(Address, usize)> {
		unsafe {
			let buffer_ptr = buffer.as_mut_ptr() as *mut c_void;
			let mut address = std::mem::MaybeUninit::zeroed();
			let mut address_len = Address::max_len();
			let transferred = check_ret_isize(libc::recvfrom(
				self.as_raw_fd(),
				buffer_ptr,
				buffer.len(),
				flags,
				Address::as_sockaddr_mut(&mut address),
				&mut address_len
			))?;

			let address = Address::finalize(address, address_len)?;
			Ok((address, transferred as usize))
		}
	}

	/// Receive a message on the socket from the connected peer.
	///
	/// If the call succeeds, the function returns a tuple with:
	///   * the number of transferred bytes
	///   * the number of transferred control message bytes
	///   * the reception flags
	///
	/// See `man recvmsg` for more information.
	pub fn recv_msg(&self, data: &[IoSliceMut], cdata: &mut SocketAncillary, flags: c_int) -> std::io::Result<(usize, c_int)> {
		let (cdata_buf, cdata_len) = if cdata.capacity() == 0 {
			(std::ptr::null_mut(), 0)
		} else {
			(cdata.buffer.as_mut_ptr(), cdata.capacity())
		};

		unsafe {
			let mut header = std::mem::zeroed::<libc::msghdr>();
			header.msg_iov = data.as_ptr() as *mut libc::iovec;
			header.msg_iovlen = data.len();
			header.msg_control = cdata_buf as *mut c_void;
			header.msg_controllen = cdata_len;

			let ret = check_ret_isize(libc::recvmsg(self.as_raw_fd(), &mut header, flags | extra_flags::RECVMSG))?;

			cdata.length = header.msg_controllen as usize;
			cdata.truncated = header.msg_flags & libc::MSG_CTRUNC != 0;
			Ok((ret as usize, header.msg_flags))
		}
	}

	/// Receive a message on the socket from any address.
	///
	/// If the call succeeds, the function returns a tuple with:
	///   * the address of the sender
	///   * the number of transferred bytes
	///   * the number of transferred control message bytes
	///   * the reception flags
	///
	/// See `man recvmsg` for more information.
	pub fn recv_msg_from(&self, data: &[IoSliceMut], cdata: &mut SocketAncillary, flags: c_int) -> std::io::Result<(Address, usize, c_int)> {
		let (cdata_buf, cdata_len) = if cdata.capacity() == 0 {
			(std::ptr::null_mut(), 0)
		} else {
			(cdata.buffer.as_mut_ptr(), cdata.capacity())
		};

		unsafe {
			let mut address = std::mem::MaybeUninit::zeroed();
			let mut header = std::mem::zeroed::<libc::msghdr>();
			header.msg_name = Address::as_sockaddr_mut(&mut address) as *mut c_void;
			header.msg_namelen = Address::max_len();
			header.msg_iov = data.as_ptr() as *mut libc::iovec;
			header.msg_iovlen = data.len();
			header.msg_control = cdata_buf as *mut c_void;
			header.msg_controllen = cdata_len;

			let ret = check_ret_isize(libc::recvmsg(self.as_raw_fd(), &mut header, flags | extra_flags::RECVMSG))?;
			let address = Address::finalize(address, header.msg_namelen)?;
			cdata.length = header.msg_controllen as usize;
			cdata.truncated = header.msg_flags & libc::MSG_CTRUNC != 0;
			Ok((address, ret as usize, header.msg_flags))
		}
	}
}

impl<Address: AsSocketAddress> FromRawFd for Socket<Address> {
	unsafe fn from_raw_fd(fd: RawFd) -> Self {
		Self::from_raw_fd(fd)
	}
}

impl<Address: AsSocketAddress> AsRawFd for Socket<Address> {
	fn as_raw_fd(&self) -> RawFd {
		self.as_raw_fd()
	}
}

impl<Address: AsSocketAddress> AsRawFd for &'_ Socket<Address> {
	fn as_raw_fd(&self) -> RawFd {
		(*self).as_raw_fd()
	}
}

impl<Address: AsSocketAddress> IntoRawFd for Socket<Address> {
	fn into_raw_fd(self) -> RawFd {
		self.into_raw_fd()
	}
}

/// Wrap the return value of a libc function in an [`std::io::Result`].
///
/// If the return value is -1, [`last_os_error()`](std::io::Error::last_os_error) is returned.
/// Otherwise, the return value is returned wrapped as [`Ok`].
fn check_ret(ret: c_int) -> std::io::Result<c_int> {
	if ret == -1 {
		Err(std::io::Error::last_os_error())
	} else {
		Ok(ret)
	}
}

/// Wrap the return value of a libc function in an [`std::io::Result`].
///
/// If the return value is -1, [`last_os_error()`](std::io::Error::last_os_error) is returned.
/// Otherwise, the return value is returned wrapped as [`Ok`].
fn check_ret_isize(ret: isize) -> std::io::Result<isize> {
	if ret == -1 {
		Err(std::io::Error::last_os_error())
	} else {
		Ok(ret)
	}
}

/// Create a socket and wrap the created file descriptor.
fn socket(domain: c_int, kind: c_int, protocol: c_int) -> std::io::Result<FileDesc> {
	unsafe {
		let fd = check_ret(libc::socket(domain, kind, protocol))?;
		Ok(FileDesc::from_raw_fd(fd))
	}
}

/// Create a socket pair and wrap the created file descriptors.
fn socketpair(domain: c_int, kind: c_int, protocol: c_int) -> std::io::Result<(FileDesc, FileDesc)> {
	unsafe {
		let mut fds = [0; 2];
		check_ret(libc::socketpair(domain, kind, protocol, fds.as_mut_ptr()))?;
		Ok((
			FileDesc::from_raw_fd(fds[0]),
			FileDesc::from_raw_fd(fds[1]),
		))
	}
}

fn bool_to_c_int(value: bool) -> c_int {
	if value {
		1
	} else {
		0
	}
}