Skip to content

Commit c24d818

Browse files
committed
further bind updates
1 parent 1be74a3 commit c24d818

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

src/safeposix/syscalls/net_calls.rs

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::safeposix::filesystem::*;
1212
use crate::safeposix::net::*;
1313

1414
impl Cage {
15-
//Initializes a socket description and sets the necessary flags
15+
//Initializes a socket file descriptor and sets the necessary flags
1616
fn _socket_initializer(
1717
&self,
1818
domain: i32,
@@ -55,9 +55,7 @@ impl Cage {
5555
return fd;
5656
}
5757
let fdoption = &mut *guardopt.unwrap();
58-
//Insert the sockfd as the FileDescriptor inside fdoption
59-
//This specifies that the fd entry in the fd table points to a Socket
60-
//with properties outlined in SocketDesc within the FileDescriptor enum
58+
//Insert the sockfd into the FileDescriptor option inside the fd table
6159
let _insertval = fdoption.insert(sockfd);
6260
return fd;
6361
}
@@ -196,7 +194,7 @@ impl Cage {
196194
//Create a new socket, as no sockhandle exists
197195
//Socket creation rarely fails except for invalid parameters or extremely low-resources conditions
198196
//Upon failure, process will panic
199-
// ** What domains does lind satisfy???? ** //
197+
//Lind handles IPv4, IPv6, and Unix for domains
200198
let thissock =
201199
interface::Socket::new(sockhandle.domain, sockhandle.socktype, sockhandle.protocol);
202200

@@ -216,7 +214,7 @@ impl Cage {
216214
//Failure occured upon setting a socket option
217215
//Possible failures can be read at the man page linked above
218216
//
219-
// **Possibly add errors instead of having a single panick for all errors ??? ** //
217+
// TODO: Possibly add errors instead of having a single panic for all errors ??? ** //
220218
if sockret < 0 {
221219
panic!("Cannot handle failure in setsockopt on socket creation");
222220
}
@@ -227,15 +225,70 @@ impl Cage {
227225
};
228226
}
229227

230-
//we assume we've converted into a RustSockAddr in the dispatcher
231-
//bind a name to a socket - assign the address specified by localaddr to the
232-
//socket referred to by the file descriptor fd
228+
//TODO: bind_syscall can be merged with bind_inner to be one function as this is a remnant
229+
//from a previous refactor
230+
231+
/// ### Description
232+
///
233+
/// `bind_syscall` - when a socket is created with socket_syscall, it exists in a name
234+
/// space (address family) but has no address assigned to it. bind_syscall
235+
/// assigns the address specified by localaddr to the socket referred to
236+
/// by the file descriptor fd.
237+
///
238+
/// ### Arguments
239+
///
240+
/// it accepts two parameters:
241+
/// * `fd` - an open file descriptor
242+
/// * `localaddr` - the address to bind to the socket referred to by fd
243+
///
244+
/// ### Returns
245+
///
246+
/// On success, zero is returned. On error, -errno is returned, and
247+
/// errno is set to indicate the error.
248+
///
249+
/// ### Errors
250+
///
251+
/// * EACCES - The address is protected, and the user is not the
252+
/// superuser.
253+
/// * EADDRINUSE - The given address is already in use.
254+
/// * EADDRINUSE - (Internet domain sockets) The port number was specified as
255+
/// zero in the socket address structure, but, upon attempting
256+
/// to bind to an ephemeral port, it was determined that all
257+
/// port numbers in the ephemeral port range are currently in
258+
/// use. See the discussion of
259+
/// /proc/sys/net/ipv4/ip_local_port_range ip(7).
260+
/// * EBADF - sockfd is not a valid file descriptor.
261+
/// * EINVAL - The socket is already bound to an address.
262+
/// * EINVAL - addrlen is wrong, or addr is not a valid address for this
263+
/// socket's domain.
264+
/// * ENOTSOCK - The file descriptor sockfd does not refer to a socket.
265+
///
266+
/// The following errors are specific to UNIX domain (AF_UNIX)
267+
/// sockets:
268+
///
269+
/// * EACCES - Search permission is denied on a component of the path
270+
/// prefix. (See also path_resolution(7).)
271+
/// * EADDRNOTAVAIL - A nonexistent interface was requested or the requested
272+
/// address was not local.
273+
/// * EFAULT - addr points outside the user's accessible address space.
274+
/// * ELOOP - Too many symbolic links were encountered in resolving
275+
/// addr.
276+
/// * ENAMETOOLONG - addr is too long.
277+
/// * ENOENT - A component in the directory prefix of the socket pathname
278+
/// does not exist.
279+
/// * ENOMEM - Insufficient kernel memory was available.
280+
/// * ENOTDIR - A component of the path prefix is not a directory.
281+
/// * EROFS - The socket inode would reside on a read-only filesystem.
282+
///
283+
// ** should this comment be left ?? : we assume we've converted into a RustSockAddr in the dispatcher
233284
pub fn bind_syscall(&self, fd: i32, localaddr: &interface::GenSockaddr) -> i32 {
234285
self.bind_inner(fd, localaddr, false)
235286
}
236287

237-
//Based on the domain of the socket, bind the socket with a local address and
238-
//insert a cloned local address in the socket handle
288+
//Direct to appropriate helper function based on the domain of the socket
289+
//to bind socket with local address
290+
//For INET sockets, create and bind the inner socket which is a kernel socket noted by raw_sys_fd
291+
//For Unix sockets, setup unix info field and bind to local address
239292
//On success, zero is returned. On error, -errno is returned, and
240293
//errno is set to indicate the error.
241294
fn bind_inner_socket(
@@ -314,13 +367,9 @@ impl Cage {
314367
let newinodenum = FS_METADATA
315368
.nextinode
316369
.fetch_add(1, interface::RustAtomicOrdering::Relaxed);
317-
//fetch_add returns the previous value, which is the inode number we want,
318-
//while incrementing the nextinode value as well
319-
//Specifically, the order argument Relaxed allows reordering and allows
320-
//operations to appear out of order when viewed by other threads.
321-
//It provides no synchronization or guarantees about the visibility of other concurrent operations.
322-
//To learn more about atomic variables and operations, access the following link,
323-
//https://medium.com/@teamcode20233/understanding-the-atomicusize-in-std-sync-atomic-rust-tutorial-b3b43c77a2b
370+
//fetch_add returns the previous value, which is the inode number we want, while incrementing the nextinode value as well
371+
//The ordering argument Relaxed guarantees the memory location is atomic, which is all that is neccessary for a counter
372+
//Read more at https://stackoverflow.com/questions/30407121/which-stdsyncatomicordering-to-use
324373

325374
let newinode;
326375

0 commit comments

Comments
 (0)