The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
sys_socket.c File Reference

Macros

#define MAX_IOV   16
 the socket syscalls: Unix domain stream sockets (kernel/ipc/unix_socket.h) and IPv4 sockets (kernel/net/inet_socket.h).
#define MAX_CONTROL   256
#define CMSG_ALIGN_K(n)
#define CMSG_HDR_SIZE   CMSG_ALIGN_K(sizeof(kcmsghdr_t))

Functions

syscall_ret_t sys_socket (struct registers *regs)
 SYS_socket: make an unconnected socket.
syscall_ret_t sys_socketpair (struct registers *regs)
 SYS_socketpair: make two connected sockets.
syscall_ret_t sys_bind (struct registers *regs)
 SYS_bind: give a socket a name.
syscall_ret_t sys_listen (struct registers *regs)
 SYS_listen: let a bound socket take connections.
syscall_ret_t sys_connect (struct registers *regs)
 SYS_connect: connect to the listening socket with a name.
syscall_ret_t sys_accept (struct registers *regs)
 SYS_accept: take the next connection of a listening socket (accept4).
syscall_ret_t sys_sendmsg (struct registers *regs)
 SYS_sendmsg: send data, and pass open files with SCM_RIGHTS.
syscall_ret_t sys_recvmsg (struct registers *regs)
 SYS_recvmsg: receive data, and the files passed with it.
syscall_ret_t sys_getsockname (struct registers *regs)
 SYS_getsockname: the local address of a socket.
syscall_ret_t sys_getpeername (struct registers *regs)
 SYS_getpeername: the address a socket is connected to.
syscall_ret_t sys_setsockopt (struct registers *regs)
 SYS_setsockopt: set a socket option (an int value).
syscall_ret_t sys_getsockopt (struct registers *regs)
 SYS_getsockopt: read a socket option (an int value).
syscall_ret_t sys_shutdown (struct registers *regs)
 SYS_shutdown: stop the reading or the writing on a socket.

Macro Definition Documentation

◆ MAX_IOV

#define MAX_IOV   16

the socket syscalls: Unix domain stream sockets (kernel/ipc/unix_socket.h) and IPv4 sockets (kernel/net/inet_socket.h).

the user structs (sockaddr_un, sockaddr_in, msghdr, iovec, cmsghdr) have the Linux layouts. this file copies them in, checks the user ranges, and turns fds into open files and back; the socket code does the rest. each call sees which kind of socket the fd is and calls that kind's code.

a known limit: a socket passed over itself (or in a ring of sockets that are only open in each other's queues) is never freed. Linux has a collector for this; we do not.

◆ MAX_CONTROL

#define MAX_CONTROL   256

◆ CMSG_ALIGN_K

#define CMSG_ALIGN_K ( n)
Value:
(((n) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
__SIZE_TYPE__ size_t
Definition types.h:14

◆ CMSG_HDR_SIZE

#define CMSG_HDR_SIZE   CMSG_ALIGN_K(sizeof(kcmsghdr_t))

Function Documentation

◆ sys_socket()

syscall_ret_t sys_socket ( struct registers * regs)

SYS_socket: make an unconnected socket.

args: domain (AF_UNIX or AF_INET), type (SOCK_STREAM; for AF_INET also SOCK_DGRAM; with SOCK_NONBLOCK and SOCK_CLOEXEC), protocol (0; for AF_INET also IPPROTO_TCP, IPPROTO_UDP or IPPROTO_ICMP). returns the new fd, or -EAFNOSUPPORT, -EPROTONOSUPPORT, -ENOMEM, -EMFILE.

◆ sys_socketpair()

syscall_ret_t sys_socketpair ( struct registers * regs)

SYS_socketpair: make two connected sockets.

args: domain, type, protocol (as for socket), int sv[2]. returns 0, or a negative errno.

◆ sys_bind()

syscall_ret_t sys_bind ( struct registers * regs)

SYS_bind: give a socket a name.

args: fd, const struct sockaddr_un *, addrlen. returns 0, or -EBADF, -ENOTSOCK, -EINVAL, -EADDRINUSE, -EFAULT.

◆ sys_listen()

syscall_ret_t sys_listen ( struct registers * regs)

SYS_listen: let a bound socket take connections.

args: fd, backlog (AF_UNIX: 0 or more than 16 means 16; AF_INET: at most TCP_BACKLOG_MAX). returns 0, or -EBADF, -ENOTSOCK, -EINVAL, -EOPNOTSUPP (a datagram socket).

◆ sys_connect()

syscall_ret_t sys_connect ( struct registers * regs)

SYS_connect: connect to the listening socket with a name.

args: fd, const struct sockaddr_un *, addrlen. returns 0, or -ECONNREFUSED (no one listens there, or the backlog is full), -EISCONN, -EBADF, -ENOTSOCK, -EINVAL, -EFAULT.

◆ sys_accept()

syscall_ret_t sys_accept ( struct registers * regs)

SYS_accept: take the next connection of a listening socket (accept4).

args: fd, struct sockaddr * (or NULL), socklen_t *addrlen (or NULL), flags (SOCK_NONBLOCK, SOCK_CLOEXEC). AF_INET: the peer's sockaddr_in. AF_UNIX: the peer has no name, so only sun_family is filled in and *addrlen becomes 2. returns the new fd, or -EAGAIN, -EINVAL, -EBADF, -ENOTSOCK, -EFAULT, -EMFILE, -ENOMEM.

◆ sys_sendmsg()

syscall_ret_t sys_sendmsg ( struct registers * regs)

SYS_sendmsg: send data, and pass open files with SCM_RIGHTS.

args: fd, const struct msghdr *, flags (MSG_DONTWAIT, MSG_NOSIGNAL). the files go with the first byte sent, so a message that passes files must have some data. msg_name is ignored (the socket is connected). returns the bytes sent, or -EPIPE (and SIGPIPE, unless MSG_NOSIGNAL), -EAGAIN, -ENOTCONN, -EBADF, -ENOTSOCK, -EINVAL, -EFAULT, -ENOMEM.

◆ sys_recvmsg()

syscall_ret_t sys_recvmsg ( struct registers * regs)

SYS_recvmsg: receive data, and the files passed with it.

args: fd, struct msghdr *, flags (MSG_DONTWAIT, MSG_CMSG_CLOEXEC). the files come as one SCM_RIGHTS control message. files that do not fit in msg_control (or in the fd table) are closed, and msg_flags gets MSG_CTRUNC. msg_namelen becomes 0 (the peer has no name). returns the bytes received (0 at the end of the stream), or -EAGAIN, -ENOTCONN, -EBADF, -ENOTSOCK, -EINVAL, -EFAULT.

◆ sys_getsockname()

syscall_ret_t sys_getsockname ( struct registers * regs)

SYS_getsockname: the local address of a socket.

args: fd, struct sockaddr *, socklen_t *addrlen. returns 0, or -EBADF, -ENOTSOCK, -EFAULT.

◆ sys_getpeername()

syscall_ret_t sys_getpeername ( struct registers * regs)

SYS_getpeername: the address a socket is connected to.

args: fd, struct sockaddr *, socklen_t *addrlen. returns 0, or -ENOTCONN, -EBADF, -ENOTSOCK, -EFAULT.

◆ sys_setsockopt()

syscall_ret_t sys_setsockopt ( struct registers * regs)

SYS_setsockopt: set a socket option (an int value).

args: fd, level, name, const void *value, socklen_t length. a Unix socket takes the SOL_SOCKET options and keeps none of them. returns 0, or -ENOPROTOOPT, -EINVAL, -EBADF, -ENOTSOCK, -EFAULT.

◆ sys_getsockopt()

syscall_ret_t sys_getsockopt ( struct registers * regs)

SYS_getsockopt: read a socket option (an int value).

args: fd, level, name, void *value, socklen_t *length. returns 0, or -ENOPROTOOPT, -EINVAL, -EBADF, -ENOTSOCK, -EFAULT.

◆ sys_shutdown()

syscall_ret_t sys_shutdown ( struct registers * regs)

SYS_shutdown: stop the reading or the writing on a socket.

args: fd, how (SHUT_RD, SHUT_WR, SHUT_RDWR). returns 0, or -EINVAL, -EOPNOTSUPP (a Unix socket), -EBADF, -ENOTSOCK.