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

IPv4 sockets: TCP (SOCK_STREAM), UDP (SOCK_DGRAM) and ICMP echo (SOCK_DGRAM with IPPROTO_ICMP, for ping, as Linux's ping sockets). More...

#include "common/error.h"
#include "common/stdint.h"
#include "kernel/ipc/unix_socket.h"

Go to the source code of this file.

Data Structures

struct  ksockaddr_in_t

Macros

#define AF_INET   2
#define SOCK_DGRAM   2
#define IPPROTO_IP   0
#define IPPROTO_ICMP   1
#define IPPROTO_TCP   6
#define IPPROTO_UDP   17
#define SO_REUSEADDR   2
#define SO_TYPE   3
#define SO_ERROR   4
#define SO_BROADCAST   6
#define SO_SNDBUF   7
#define SO_RCVBUF   8
#define SO_KEEPALIVE   9
#define TCP_NODELAY   1
#define TCP_KEEPCNT   6
#define FIONREAD   0x541B
#define MSG_PEEK   0x0002
#define MSG_TRUNC   0x0020
#define SHUT_RD   0
#define SHUT_WR   1
#define SHUT_RDWR   2
#define INET_RECEIVE_MAX   (64 * 1024)
#define INET_PORT_FIRST   49152

Typedefs

typedef struct inet_socket inet_socket_t

Functions

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.
inet_socket_t * inet_socket_of (struct file *file)
 the socket of an open file, or NULL if the file is not an IPv4 socket.
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 (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.
int32_t inet_socket_listen (inet_socket_t *s, uint32_t backlog)
 a stream socket takes connections.
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).
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).
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).
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).

Detailed Description

IPv4 sockets: TCP (SOCK_STREAM), UDP (SOCK_DGRAM) and ICMP echo (SOCK_DGRAM with IPPROTO_ICMP, for ping, as Linux's ping sockets).

a TCP socket has a tcp_conn_t (tcp.h): its connection, or its listener. a datagram socket keeps the datagrams it receives in a queue, up to INET_RECEIVE_MAX bytes; more are dropped. an ICMP socket sends an echo request that the program writes (type, code, then the data), with the identifier set to the socket's port and the checksum filled in, and it receives the echo replies with that identifier.

the layouts below are the user ABI (<netinet/in.h> and <sys/socket.h> in libc); they are the same as Linux's.

Macro Definition Documentation

◆ AF_INET

#define AF_INET   2

◆ SOCK_DGRAM

#define SOCK_DGRAM   2

◆ IPPROTO_IP

#define IPPROTO_IP   0

◆ IPPROTO_ICMP

#define IPPROTO_ICMP   1

◆ IPPROTO_TCP

#define IPPROTO_TCP   6

◆ IPPROTO_UDP

#define IPPROTO_UDP   17

◆ SO_REUSEADDR

#define SO_REUSEADDR   2

◆ SO_TYPE

#define SO_TYPE   3

◆ SO_ERROR

#define SO_ERROR   4

◆ SO_BROADCAST

#define SO_BROADCAST   6

◆ SO_SNDBUF

#define SO_SNDBUF   7

◆ SO_RCVBUF

#define SO_RCVBUF   8

◆ SO_KEEPALIVE

#define SO_KEEPALIVE   9

◆ TCP_NODELAY

#define TCP_NODELAY   1

◆ TCP_KEEPCNT

#define TCP_KEEPCNT   6

◆ FIONREAD

#define FIONREAD   0x541B

◆ MSG_PEEK

#define MSG_PEEK   0x0002

◆ MSG_TRUNC

#define MSG_TRUNC   0x0020

◆ SHUT_RD

#define SHUT_RD   0

◆ SHUT_WR

#define SHUT_WR   1

◆ SHUT_RDWR

#define SHUT_RDWR   2

◆ INET_RECEIVE_MAX

#define INET_RECEIVE_MAX   (64 * 1024)

◆ INET_PORT_FIRST

#define INET_PORT_FIRST   49152

Typedef Documentation

◆ inet_socket_t

typedef struct inet_socket inet_socket_t

Function Documentation

◆ 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_of()

inet_socket_t * inet_socket_of ( struct file * file)

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

◆ 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_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.

◆ 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).