AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
sys_net.c File Reference

System calls on sockets: making them, binding, listening, accepting and connecting, sending and receiving, their names and options. More...

#include "syscalls.h"

Go to the source code of this file.

Data Structures

union  ksockaddr

Functions

long sys_socket (int domain, int type, int protocol)
 Implements socket(): makes a socket, open on a new descriptor.
static int sockaddr_copyin (union ksockaddr *k, const struct sockaddr *uaddr, socklen_t len)
 Copies an address of len bytes from user memory.
static int sockaddr_room (socklen_t *ulen, socklen_t *klen)
 Reads how much room the user's buffer has for an address going out, into *klen, at most the kernel copy's size.
static int sockaddr_copyout (struct sockaddr *uaddr, socklen_t *ulen, const union ksockaddr *k, socklen_t klen, socklen_t room)
 Copies an address out to user memory, as much of it as fits in room, and sets *ulen to its full size klen.
long sys_accept (int fd, const struct sockaddr *addr, socklen_t *len)
 Implements accept(): takes a connection from a listening socket, open on a new descriptor.
long sys_bind (int fd, const struct sockaddr *addr, socklen_t len)
 Implements bind(): gives a socket its local address.
long sys_connect (int fd, const struct sockaddr *addr, socklen_t len)
 Implements connect(): connects a socket to a peer.
long sys_listen (int fd, int backlog)
 Implements listen(): makes a socket accept connections.
long sys_send (struct aquila_socket_io_args *us)
 Implements send() and sendto().
long sys_recv (struct aquila_socket_io_args *us)
 Implements recv() and recvfrom().
static long socket_name (int fd, struct sockaddr *addr, socklen_t *len, int peer)
 Gets a socket's local address or its peer's, for getsockname() and getpeername(), and copies it out.
long sys_getsockname (int fd, struct sockaddr *addr, socklen_t *len)
 Implements getsockname(): a socket's local address.
long sys_getpeername (int fd, struct sockaddr *addr, socklen_t *len)
 Implements getpeername(): the address of a socket's peer.
long sys_getsockopt (int fd, int level, int name, void *val, socklen_t *len)
 Implements getsockopt(): reads a socket option.
long sys_setsockopt (int fd, int level, int name, const void *val, socklen_t len)
 Implements setsockopt(): sets a socket option.
long sys_shutdown (int fd, int how)
 Implements shutdown(): shuts down reading, writing or both on a socket, which stays open.

Detailed Description

System calls on sockets: making them, binding, listening, accepting and connecting, sending and receiving, their names and options.

The work is done by net/socket.c and the domains' code; these calls copy the arguments in and the results out.

Definition in file sys_net.c.

Function Documentation

◆ sockaddr_copyin()

int sockaddr_copyin ( union ksockaddr * k,
const struct sockaddr * uaddr,
socklen_t len )
static

Copies an address of len bytes from user memory.

Returns 0, -EINVAL if it's too big, or -EFAULT.

Definition at line 58 of file sys_net.c.

References sockaddr_copyin().

Referenced by sockaddr_copyin(), sys_bind(), sys_connect(), and sys_send().

◆ sockaddr_copyout()

int sockaddr_copyout ( struct sockaddr * uaddr,
socklen_t * ulen,
const union ksockaddr * k,
socklen_t klen,
socklen_t room )
static

Copies an address out to user memory, as much of it as fits in room, and sets *ulen to its full size klen.

Returns 0 or -EFAULT.

Definition at line 79 of file sys_net.c.

References MIN, and sockaddr_copyout().

Referenced by sockaddr_copyout(), socket_name(), sys_accept(), and sys_recv().

◆ sockaddr_room()

int sockaddr_room ( socklen_t * ulen,
socklen_t * klen )
static

Reads how much room the user's buffer has for an address going out, into *klen, at most the kernel copy's size.

Returns 0 or -EFAULT.

Definition at line 68 of file sys_net.c.

References MIN, and sockaddr_room().

Referenced by sockaddr_room(), socket_name(), sys_accept(), and sys_recv().

◆ socket_name()

long socket_name ( int fd,
struct sockaddr * addr,
socklen_t * len,
int peer )
static

Gets a socket's local address or its peer's, for getsockname() and getpeername(), and copies it out.

Definition at line 365 of file sys_net.c.

References fd_file(), memset(), ksockaddr::sa, sockaddr_copyout(), sockaddr_room(), socket_getname(), and socket_name().

Referenced by socket_name(), sys_getpeername(), and sys_getsockname().

◆ sys_accept()

long sys_accept ( int fd,
const struct sockaddr * addr,
socklen_t * len )

Implements accept(): takes a connection from a listening socket, open on a new descriptor.

Parameters
fdthe listening socket
addrwhere to put the peer's address, or NULL
lenthe room at addr, set to the address's size
Returns
the connection's descriptor, or a negative errno: -EBADF, -EMFILE, -ENOMEM, -EAGAIN, -EINVAL, -EFAULT

Definition at line 97 of file sys_net.c.

References curproc, fd_file(), file_new(), kfree(), LOG_DEBUG, memset(), NULL, proc_fd_close(), proc_fd_get(), proc_fd_install(), proc_fd_release(), ksockaddr::sa, sockaddr_copyout(), sockaddr_room(), socket_accept(), and sys_accept().

Referenced by sys_accept().

◆ sys_bind()

long sys_bind ( int fd,
const struct sockaddr * addr,
socklen_t len )

Implements bind(): gives a socket its local address.

Parameters
fdthe socket
addrthe address
lenits size
Returns
0, or a negative errno: -EBADF, -EADDRINUSE, -EADDRNOTAVAIL, -EACCES, -EINVAL, -EFAULT

Definition at line 161 of file sys_net.c.

References fd_file(), LOG_DEBUG, memset(), ksockaddr::sa, sockaddr_copyin(), socket_bind(), and sys_bind().

Referenced by sys_bind().

◆ sys_connect()

long sys_connect ( int fd,
const struct sockaddr * addr,
socklen_t len )

Implements connect(): connects a socket to a peer.

Parameters
fdthe socket
addrthe peer's address
lenits size
Returns
0, or a negative errno: -EBADF, -ECONNREFUSED, -EINPROGRESS, -ENETUNREACH, -ETIMEDOUT, -EFAULT

Definition at line 192 of file sys_net.c.

References fd_file(), LOG_DEBUG, memset(), ksockaddr::sa, sockaddr_copyin(), socket_connect(), and sys_connect().

Referenced by sys_connect().

◆ sys_getpeername()

long sys_getpeername ( int fd,
struct sockaddr * addr,
socklen_t * len )

Implements getpeername(): the address of a socket's peer.

Parameters
fdthe socket
addrwhere to put the address
lenthe room at addr, set to the address's size
Returns
0, or a negative errno: -EBADF, -ENOTCONN, -EFAULT

Definition at line 412 of file sys_net.c.

References LOG_DEBUG, socket_name(), and sys_getpeername().

Referenced by sys_getpeername().

◆ sys_getsockname()

long sys_getsockname ( int fd,
struct sockaddr * addr,
socklen_t * len )

Implements getsockname(): a socket's local address.

Parameters
fdthe socket
addrwhere to put the address
lenthe room at addr, set to the address's size
Returns
0, or a negative errno: -EBADF, -ENOTSOCK, -EFAULT

Definition at line 398 of file sys_net.c.

References LOG_DEBUG, socket_name(), and sys_getsockname().

Referenced by sys_getsockname().

◆ sys_getsockopt()

long sys_getsockopt ( int fd,
int level,
int name,
void * val,
socklen_t * len )

Implements getsockopt(): reads a socket option.

Parameters
fdthe socket
levelSOL_SOCKET, or a protocol's level
namethe option
valwhere to put its value
lenthe room at val, set to the value's size
Returns
0, or a negative errno: -EBADF, -ENOPROTOOPT, -EINVAL, -EFAULT

Definition at line 429 of file sys_net.c.

References fd_file(), LOG_DEBUG, memset(), MIN, socket_getsockopt(), and sys_getsockopt().

Referenced by sys_getsockopt().

◆ sys_listen()

long sys_listen ( int fd,
int backlog )

Implements listen(): makes a socket accept connections.

Parameters
fdthe socket
backloghow many connections may wait to be accepted
Returns
0, or a negative errno: -EBADF, -EINVAL, -EOPNOTSUPP

Definition at line 221 of file sys_net.c.

References fd_file(), LOG_DEBUG, socket_listen(), and sys_listen().

Referenced by sys_listen().

◆ sys_recv()

long sys_recv ( struct aquila_socket_io_args * us)

Implements recv() and recvfrom().

Parameters
usthe arguments, in user memory: the socket, the buffer, the flags, and where recvfrom() puts the sender's address
Returns
how many bytes were received (0 at the end), or a negative errno: -EBADF, -EFAULT, -EAGAIN, -ENOTCONN, ...

Definition at line 313 of file sys_net.c.

References fd_file(), fd::flags, iobuf_init(), LOG_DEBUG, memset(), NULL, ksockaddr::sa, sockaddr_copyout(), sockaddr_room(), socket_recv(), sys_recv(), and uaccess_check().

Referenced by sys_recv().

◆ sys_send()

long sys_send ( struct aquila_socket_io_args * us)

Implements send() and sendto().

A send on a connection that can't send anymore also raises SIGPIPE, unless MSG_NOSIGNAL is set.

Parameters
usthe arguments, in user memory: the socket, the data, the flags, and sendto()'s destination
Returns
how many bytes were sent, or a negative errno: -EBADF, -EFAULT, -EPIPE, -EAGAIN, -EMSGSIZE, ...

Definition at line 249 of file sys_net.c.

References curproc, fd_file(), fd::flags, iobuf_init(), LOG_DEBUG, memset(), NULL, ksockaddr::sa, signal_proc_send(), sockaddr_copyin(), socket_send(), sys_send(), and uaccess_check().

Referenced by sys_send().

◆ sys_setsockopt()

long sys_setsockopt ( int fd,
int level,
int name,
const void * val,
socklen_t len )

Implements setsockopt(): sets a socket option.

Parameters
fdthe socket
levelSOL_SOCKET, or a protocol's level
namethe option
valits value
lenthe value's size
Returns
0, or a negative errno: -EBADF, -ENOPROTOOPT, -EINVAL, -EPERM, -EFAULT

Definition at line 470 of file sys_net.c.

References fd_file(), LOG_DEBUG, MIN, socket_setsockopt(), and sys_setsockopt().

Referenced by sys_setsockopt().

◆ sys_shutdown()

long sys_shutdown ( int fd,
int how )

Implements shutdown(): shuts down reading, writing or both on a socket, which stays open.

Parameters
fdthe socket
howSHUT_RD, SHUT_WR or SHUT_RDWR
Returns
0, or a negative errno: -EBADF, -ENOTSOCK, -ENOTCONN, -EINVAL

Definition at line 501 of file sys_net.c.

References fd_file(), LOG_DEBUG, socket_shutdown_dir(), and sys_shutdown().

Referenced by sys_shutdown().

◆ sys_socket()

long sys_socket ( int domain,
int type,
int protocol )

Implements socket(): makes a socket, open on a new descriptor.

Parameters
domainAF_UNIX or AF_INET
typeSOCK_STREAM or SOCK_DGRAM
protocolthe protocol, or 0 for the type's default
Returns
the descriptor, or a negative errno: -EMFILE, -ENOMEM, -EAFNOSUPPORT, -EPROTONOSUPPORT

Definition at line 19 of file sys_net.c.

References curproc, file_new(), kfree(), LOG_DEBUG, proc_fd_get(), proc_fd_install(), proc_fd_release(), socket_create(), and sys_socket().

Referenced by sys_socket().