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

IPv4 sockets: UDP and ICMP echo here, and the calls of TCP sockets go to tcp_socket.c (see inet_socket.h). More...

#include "inet_internal.h"
#include "byteorder.h"
#include "checksum.h"
#include "common/memory.h"
#include "icmp.h"
#include "ipv4.h"
#include "kernel/errno.h"
#include "kernel/memory/heap.h"
#include "kernel/process/process.h"
#include "kernel/signal/signal.h"
#include "kernel/sync/wait_queue.h"
#include "kernel/usermode/uaccess.h"
#include "kernel/vfs/file.h"
#include "kernel/vfs/poll.h"
#include "kernel/vfs/stat.h"
#include "netif.h"
#include "udp.h"

Macros

#define ICMP_HEADER_SIZE   8
#define DATAGRAM_MAX   65507

Functions

inet_socket_t * inet_socket_of (file_t *file)
 the socket of an open file, or NULL if the file is not an IPv4 socket.
int32_t inet_socket_create (uint32_t type, uint32_t protocol, uint32_t flags, file_t **out)
 make a new socket and its open file.
int32_t inet_socket_alloc (uint8_t protocol, uint32_t flags, file_t **out, inet_socket_t **socket)
 make a socket and its file, on the list of all sockets.
bool inet_bind_any_port (inet_socket_t *s)
 give a socket a free port (net_lock held).
int32_t inet_socket_bind (inet_socket_t *s, const ksockaddr_in_t *addr)
 give a socket a local address and port (port 0: one not in use).
int32_t inet_socket_connect (file_t *file, const ksockaddr_in_t *addr)
 a datagram socket: set the address datagrams go to and the only one they come from; AF_UNSPEC (family 0) takes the connection away.
int32_t inet_socket_send (file_t *file, const kiovec_t *iov, uint32_t iov_count, const ksockaddr_in_t *to, uint32_t flags)
 send one datagram from user memory, or data on a stream (as much as fits; it blocks for room unless the file is non-blocking).
int32_t inet_socket_receive (file_t *file, const kiovec_t *iov, uint32_t iov_count, ksockaddr_in_t *from, uint32_t flags, bool dontwait, uint32_t *full)
 receive one datagram into user memory (blocks unless the file is non-blocking or dontwait).
int32_t inet_socket_name (inet_socket_t *s, bool peer, ksockaddr_in_t *out)
 the local address (peer false) or the connected one (peer true).
int32_t inet_socket_set_option (inet_socket_t *s, int32_t level, int32_t name, int32_t value)
 setsockopt and getsockopt (an int value).
int32_t inet_socket_get_option (inet_socket_t *s, int32_t level, int32_t name, int32_t *value)
int32_t inet_socket_shutdown (inet_socket_t *s, int32_t how)
 shutdown: SHUT_RD, SHUT_WR or SHUT_RDWR.
bool inet_socket_deliver_udp (uint32_t dst, uint16_t dst_port, uint32_t src, uint16_t src_port, const uint8_t *data, uint32_t length)
 give a received UDP datagram to the socket it is for (net_lock held).
void inet_socket_deliver_echo (uint32_t src, uint16_t id, const uint8_t *message, uint32_t length)
 give a received echo reply to the ICMP socket with its identifier (net_lock held).
int32_t inet_socket_listen (inet_socket_t *s, uint32_t backlog)
 a stream socket takes connections.
int32_t inet_socket_accept (file_t *file, uint32_t flags, file_t **out, ksockaddr_in_t *peer)
 take the next connection of a listening stream socket (blocks unless the file is non-blocking).

Detailed Description

IPv4 sockets: UDP and ICMP echo here, and the calls of TCP sockets go to tcp_socket.c (see inet_socket.h).

all sockets are on one list, under net_lock, so a received datagram finds its socket there. data from user memory is copied into the kernel before the lock is taken, and to user memory after it is let go.

Macro Definition Documentation

◆ ICMP_HEADER_SIZE

#define ICMP_HEADER_SIZE   8

◆ DATAGRAM_MAX

#define DATAGRAM_MAX   65507

Function Documentation

◆ inet_socket_of()

inet_socket_t * inet_socket_of ( file_t * file)

the socket of an open file, or NULL if the file is not an IPv4 socket.

◆ inet_socket_create()

int32_t inet_socket_create ( uint32_t type,
uint32_t protocol,
uint32_t flags,
struct file ** out )

make a new socket and its open file.

Parameters
typeSOCK_STREAM or SOCK_DGRAM.
protocol0, or IPPROTO_TCP for a stream; IPPROTO_UDP or IPPROTO_ICMP (ping) for datagrams.
flagsO_NONBLOCK or 0.
Returns
0, or -EPROTONOSUPPORT, -ENOMEM.

◆ inet_socket_alloc()

int32_t inet_socket_alloc ( uint8_t protocol,
uint32_t flags,
struct file ** out,
inet_socket_t ** socket )

make a socket and its file, on the list of all sockets.

Parameters
flagsO_NONBLOCK or 0.
Returns
0, or -ENOMEM.

◆ inet_bind_any_port()

bool inet_bind_any_port ( inet_socket_t * s)

give a socket a free port (net_lock held).

Returns
false if all ports are taken.

◆ inet_socket_bind()

int32_t inet_socket_bind ( inet_socket_t * s,
const ksockaddr_in_t * addr )

give a socket a local address and port (port 0: one not in use).

Returns
0, or -EADDRINUSE, -EADDRNOTAVAIL, -EINVAL.

◆ inet_socket_connect()

int32_t inet_socket_connect ( struct file * file,
const ksockaddr_in_t * addr )

a datagram socket: set the address datagrams go to and the only one they come from; AF_UNSPEC (family 0) takes the connection away.

a stream: connect (blocks for the handshake unless the file is non-blocking).

Returns
0, or -EAFNOSUPPORT, -ENETUNREACH; for a stream also -ECONNREFUSED, -ETIMEDOUT, -EINPROGRESS, -EALREADY, -EISCONN, -EINTR.

◆ inet_socket_send()

int32_t inet_socket_send ( struct file * file,
const kiovec_t * iov,
uint32_t iov_count,
const ksockaddr_in_t * to,
uint32_t flags )

send one datagram from user memory, or data on a stream (as much as fits; it blocks for room unless the file is non-blocking).

Parameters
towhere it goes, or NULL for the connected address (a stream does not use it).
flagsMSG_DONTWAIT.
Returns
the bytes sent, or -EDESTADDRREQ, -EMSGSIZE, -ENETUNREACH, -EACCES (broadcast without SO_BROADCAST), -ENOBUFS, -ENOMEM, -EPIPE (shut down for writing), -EFAULT.

◆ inet_socket_receive()

int32_t inet_socket_receive ( struct file * file,
const kiovec_t * iov,
uint32_t iov_count,
ksockaddr_in_t * from,
uint32_t flags,
bool dontwait,
uint32_t * full )

receive one datagram into user memory (blocks unless the file is non-blocking or dontwait).

a datagram longer than the buffers is cut; the rest of it is gone. a stream gives the bytes it has (0 at the end of the stream; -ECONNRESET once after a reset).

Parameters
fromreceives the sender (may be NULL).
flagsMSG_PEEK: leave it in the queue.
fullreceives the datagram's whole length (may be NULL).
Returns
the bytes received, or -EAGAIN, -ERESTARTSYS, -EFAULT; 0 when shut down for reading and the queue is empty.

◆ inet_socket_name()

int32_t inet_socket_name ( inet_socket_t * s,
bool peer,
ksockaddr_in_t * out )

the local address (peer false) or the connected one (peer true).

Returns
0, or -ENOTCONN.

◆ inet_socket_set_option()

int32_t inet_socket_set_option ( inet_socket_t * s,
int32_t level,
int32_t name,
int32_t value )

setsockopt and getsockopt (an int value).

Returns
0, or -ENOPROTOOPT.

◆ inet_socket_get_option()

int32_t inet_socket_get_option ( inet_socket_t * s,
int32_t level,
int32_t name,
int32_t * value )

◆ inet_socket_shutdown()

int32_t inet_socket_shutdown ( inet_socket_t * s,
int32_t how )

shutdown: SHUT_RD, SHUT_WR or SHUT_RDWR.

Returns
0, or -EINVAL.

◆ inet_socket_deliver_udp()

bool inet_socket_deliver_udp ( uint32_t dst,
uint16_t dst_port,
uint32_t src,
uint16_t src_port,
const uint8_t * data,
uint32_t length )

give a received UDP datagram to the socket it is for (net_lock held).

Returns
false if no socket takes it.

◆ inet_socket_deliver_echo()

void inet_socket_deliver_echo ( uint32_t src,
uint16_t id,
const uint8_t * message,
uint32_t length )

give a received echo reply to the ICMP socket with its identifier (net_lock held).

◆ inet_socket_listen()

int32_t inet_socket_listen ( inet_socket_t * s,
uint32_t backlog )

a stream socket takes connections.

Returns
0, or -EOPNOTSUPP (a datagram socket), -EINVAL (connected).

◆ inet_socket_accept()

int32_t inet_socket_accept ( struct file * file,
uint32_t flags,
struct file ** out,
ksockaddr_in_t * peer )

take the next connection of a listening stream socket (blocks unless the file is non-blocking).

Parameters
flagsO_NONBLOCK or 0, for the new file.
peerreceives the address of the other end.
Returns
0, or -EAGAIN, -EINVAL (not listening), -EOPNOTSUPP, -ENOMEM, -ERESTARTSYS.