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

Sockets: what every domain's sockets share. More...

#include <core/system.h>
#include <core/string.h>
#include <ds/queue.h>
#include <aquila/errno.h>
#include <net/socket.h>
#include <aquila/fcntl.h>

Go to the source code of this file.

Functions

int socket_create (struct file *file, int domain, int type, int protocol)
 Makes a socket of a domain in an open file, for socket().
int socket_accept (struct file *file, struct file *conn, const struct sockaddr *addr, socklen_t *len)
 Accepts a connection on a listening socket, for accept().
int socket_bind (struct file *file, const struct sockaddr *addr, socklen_t len)
 Gives a socket its local address, for bind().
int socket_connect (struct file *file, const struct sockaddr *addr, socklen_t len)
 Connects a socket to a peer, for connect().
int socket_listen (struct file *file, int backlog)
 Makes a socket listen for connections, for listen().
ssize_t socket_send (struct file *file, struct iobuf *io, int flags, const struct sockaddr *to, socklen_t tolen)
 Sends data on a socket, for send(), sendto() and write().
ssize_t socket_recv (struct file *file, struct iobuf *io, int flags, struct sockaddr *from, socklen_t *fromlen)
 Receives data from a socket, for recv(), recvfrom() and read().
int socket_can_read (struct file *file, size_t len)
 Says whether a socket has something to read, for select() and poll().
struct waitq * socket_wait_queue (struct file *file, int write)
 Finds the wait queue that a socket's readers (or writers) sleep on, for select() and poll() to register.
int socket_can_write (struct file *file, size_t len)
 Says whether a socket has room to write, for select() and poll().
int socket_shutdown_dir (struct file *file, int how)
 Shuts down reading, writing or both on a socket, for shutdown().
int socket_shutdown (struct file *file, int how)
 Releases a socket when its open file is closed (its last descriptor went).
int socket_getname (struct file *file, struct sockaddr *addr, socklen_t *len, int peer)
 Gets a socket's local address, or its peer's, for getsockname() and getpeername().
static int sockopt_int (void *val, socklen_t *len, int v)
 Sets an option's value to the int v.
int socket_getsockopt (struct file *file, int level, int name, void *val, socklen_t *len)
 Reads a socket option, for getsockopt().
int socket_setsockopt (struct file *file, int level, int name, const void *val, socklen_t len)
 Sets a socket option, for setsockopt().
int socket_ioctl (struct file *file, int request, void *argp)
 Handles an ioctl() on a socket, by passing it to the protocol.

Detailed Description

Sockets: what every domain's sockets share.

A socket is an open file with FILE_SOCKET set. The system calls on sockets come here, which checks that the file is one, then passes the call to its domain's operations (struct sock_ops): Unix sockets (net/unix/unix.c) or Internet ones (net/inet/socket.c).

Definition in file socket.c.

Function Documentation

◆ socket_accept()

int socket_accept ( struct file * file,
struct file * conn,
const struct sockaddr * addr,
socklen_t * len )

Accepts a connection on a listening socket, for accept().

Parameters
filethe listening socket
connthe open file that becomes the connection's socket
addrwhere to put the peer's address, in user memory, or NULL
lenits size, in user memory
Returns
0, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 59 of file socket.c.

References FILE_SOCKET, file::flags, and socket_accept().

Referenced by socket_accept(), and sys_accept().

◆ socket_bind()

int socket_bind ( struct file * file,
const struct sockaddr * addr,
socklen_t len )

Gives a socket its local address, for bind().

Parameters
filethe socket
addrthe address, in the kernel's memory
lenits size
Returns
0, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 83 of file socket.c.

References FILE_SOCKET, file::flags, and socket_bind().

Referenced by socket_bind(), and sys_bind().

◆ socket_can_read()

int socket_can_read ( struct file * file,
size_t len )

Says whether a socket has something to read, for select() and poll().

Parameters
filethe socket
lenhow much a read would want
Returns
non-zero if a read wouldn't wait, 0 if it would, or a negative errno

Definition at line 189 of file socket.c.

References FILE_SOCKET, file::flags, and socket_can_read().

Referenced by socket_can_read(), and vfs_file_can_read().

◆ socket_can_write()

int socket_can_write ( struct file * file,
size_t len )

Says whether a socket has room to write, for select() and poll().

Parameters
filethe socket
lenhow much a write would want
Returns
non-zero if a write wouldn't wait, 0 if it would, or a negative errno

Definition at line 225 of file socket.c.

References FILE_SOCKET, file::flags, and socket_can_write().

Referenced by socket_can_write(), and vfs_file_can_write().

◆ socket_connect()

int socket_connect ( struct file * file,
const struct sockaddr * addr,
socklen_t len )

Connects a socket to a peer, for connect().

Parameters
filethe socket
addrthe peer's address, in the kernel's memory
lenits size
Returns
0, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 102 of file socket.c.

References FILE_SOCKET, file::flags, and socket_connect().

Referenced by socket_connect(), and sys_connect().

◆ socket_create()

int socket_create ( struct file * file,
int domain,
int type,
int protocol )

Makes a socket of a domain in an open file, for socket().

Parameters
filethe open file that becomes the socket
domainAF_UNIX or AF_INET
typethe socket's type (SOCK_STREAM, SOCK_DGRAM)
protocolthe protocol, or 0 for the type's default
Returns
0, -EAFNOSUPPORT for another domain, or the domain's error

Definition at line 27 of file socket.c.

References file::flags, socket_create(), socket_inet_create(), and socket_unix_create().

Referenced by socket_create(), and sys_socket().

◆ socket_getname()

int socket_getname ( struct file * file,
struct sockaddr * addr,
socklen_t * len,
int peer )

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

Parameters
filethe socket
addrwhere to put the address, in the kernel's memory
lenits size, set to the address's
peer0 for the local address, 1 for the peer's
Returns
0, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 289 of file socket.c.

References FILE_SOCKET, file::flags, and socket_getname().

Referenced by socket_getname(), and socket_name().

◆ socket_getsockopt()

int socket_getsockopt ( struct file * file,
int level,
int name,
void * val,
socklen_t * len )

Reads a socket option, for getsockopt().

The protocol is asked first; SO_TYPE, and SO_ERROR for a protocol that keeps no errors, are answered here.

Parameters
filethe socket
levelSOL_SOCKET, or a protocol's level
namethe option
valwhere to put its value, in the kernel's memory
lenits size, set to the value's
Returns
0, -ENOPROTOOPT for an unknown option, or another negative errno

Definition at line 325 of file socket.c.

References FILE_SOCKET, file::flags, sock_ops::getsockopt, socket_getsockopt(), and sockopt_int().

Referenced by socket_getsockopt(), and sys_getsockopt().

◆ socket_ioctl()

int socket_ioctl ( struct file * file,
int request,
void * argp )

Handles an ioctl() on a socket, by passing it to the protocol.

Parameters
filethe socket
requestthe request
argpits argument, in user memory
Returns
what the protocol returns, or -ENOTTY if it has no ioctl()

Definition at line 399 of file socket.c.

References FILE_SOCKET, file::flags, and socket_ioctl().

Referenced by socket_ioctl(), and sys_ioctl().

◆ socket_listen()

int socket_listen ( struct file * file,
int backlog )

Makes a socket listen for connections, for listen().

Parameters
filethe socket
backloghow many connections may wait to be accepted
Returns
0, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 120 of file socket.c.

References FILE_SOCKET, file::flags, and socket_listen().

Referenced by socket_listen(), and sys_listen().

◆ socket_recv()

ssize_t socket_recv ( struct file * file,
struct iobuf * io,
int flags,
struct sockaddr * from,
socklen_t * fromlen )

Receives data from a socket, for recv(), recvfrom() and read().

Parameters
filethe socket
iowhere to put the data, in user memory
flagsMSG_* flags
fromwhere to put the sender's address, or NULL
fromlenits size
Returns
how many bytes were received (0 at the end), or a negative errno

Definition at line 169 of file socket.c.

References FILE_SOCKET, file::flags, and socket_recv().

Referenced by socket_recv(), sys_recv(), and vfs_file_read().

◆ socket_send()

ssize_t socket_send ( struct file * file,
struct iobuf * io,
int flags,
const struct sockaddr * to,
socklen_t tolen )

Sends data on a socket, for send(), sendto() and write().

Parameters
filethe socket
iothe data, in user memory
flagsMSG_* flags
tothe destination, for a datagram socket, or NULL
tolenits size
Returns
how many bytes were sent, or a negative errno

Definition at line 141 of file socket.c.

References FILE_SOCKET, file::flags, and socket_send().

Referenced by socket_send(), sys_send(), and vfs_file_write().

◆ socket_setsockopt()

int socket_setsockopt ( struct file * file,
int level,
int name,
const void * val,
socklen_t len )

Sets a socket option, for setsockopt().

The protocol is asked first; a few common SOL_SOCKET options are accepted here without effect.

Parameters
filethe socket
levelSOL_SOCKET, or a protocol's level
namethe option
valits value, in the kernel's memory
lenits size
Returns
0, -ENOPROTOOPT for an unknown option, or another negative errno

Definition at line 361 of file socket.c.

References FILE_SOCKET, file::flags, sock_ops::setsockopt, and socket_setsockopt().

Referenced by socket_setsockopt(), and sys_setsockopt().

◆ socket_shutdown()

int socket_shutdown ( struct file * file,
int how )

Releases a socket when its open file is closed (its last descriptor went).

Parameters
filethe socket
howpassed on to the protocol
Returns
0, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 267 of file socket.c.

References FILE_SOCKET, file::flags, and socket_shutdown().

Referenced by socket_shutdown(), and vfs_file_close().

◆ socket_shutdown_dir()

int socket_shutdown_dir ( struct file * file,
int how )

Shuts down reading, writing or both on a socket, for shutdown().

The socket stays open.

Parameters
filethe socket
howSHUT_RD, SHUT_WR or SHUT_RDWR
Returns
0, -EINVAL for a bad how, -ENOTSOCK, -EOPNOTSUPP, or the protocol's error

Definition at line 245 of file socket.c.

References FILE_SOCKET, file::flags, and socket_shutdown_dir().

Referenced by socket_shutdown_dir(), and sys_shutdown().

◆ socket_wait_queue()

struct waitq * socket_wait_queue ( struct file * file,
int write )

Finds the wait queue that a socket's readers (or writers) sleep on, for select() and poll() to register.

Parameters
filethe socket
write0 for readers, 1 for writers
Returns
the wait queue, or NULL if the protocol can't say, in which case any wakeup counts

Definition at line 209 of file socket.c.

References NULL, and socket_wait_queue().

Referenced by socket_wait_queue(), and vfs_file_wait_queue().

◆ sockopt_int()

int sockopt_int ( void * val,
socklen_t * len,
int v )
static

Sets an option's value to the int v.

Returns 0, or -EINVAL if *len has no room for an int.

Definition at line 302 of file socket.c.

References sockopt_int().

Referenced by socket_getsockopt(), and sockopt_int().