The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
inet_socket.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file inet_socket.h
5 * @brief IPv4 sockets: TCP (SOCK_STREAM), UDP (SOCK_DGRAM) and ICMP echo
6 * (SOCK_DGRAM with IPPROTO_ICMP, for ping, as Linux's ping sockets).
7 *
8 * a TCP socket has a tcp_conn_t (tcp.h): its connection, or its listener.
9 * a datagram socket keeps the datagrams it receives in a queue, up to
10 * INET_RECEIVE_MAX bytes; more are dropped. an ICMP socket sends an echo
11 * request that the program writes (type, code, then the data), with the
12 * identifier set to the socket's port and the checksum filled in, and it
13 * receives the echo replies with that identifier.
14 *
15 * the layouts below are the user ABI (<netinet/in.h> and <sys/socket.h>
16 * in libc); they are the same as Linux's.
17 */
18
19#include "common/error.h"
20#include "common/stdint.h"
22
23struct file;
24
25#define AF_INET 2
26#define SOCK_DGRAM 2
27
28#define IPPROTO_IP 0
29#define IPPROTO_ICMP 1
30#define IPPROTO_TCP 6
31#define IPPROTO_UDP 17
32
33// setsockopt and getsockopt at SOL_SOCKET
34#define SO_REUSEADDR 2
35#define SO_TYPE 3
36#define SO_ERROR 4
37#define SO_BROADCAST 6
38#define SO_SNDBUF 7
39#define SO_RCVBUF 8
40#define SO_KEEPALIVE 9
41
42// setsockopt and getsockopt at IPPROTO_TCP
43#define TCP_NODELAY 1
44#define TCP_KEEPCNT 6
45
46#define FIONREAD 0x541B // ioctl: the bytes of the next datagram, or a stream's bytes
47
48#define MSG_PEEK 0x0002
49#define MSG_TRUNC 0x0020
50
51#define SHUT_RD 0
52#define SHUT_WR 1
53#define SHUT_RDWR 2
54
55#define INET_RECEIVE_MAX (64 * 1024)
56#define INET_PORT_FIRST 49152 // the ports given to sockets that do not bind one
57
58typedef struct
59{
61 uint16_t sin_port; // network byte order
62 uint32_t sin_addr; // network byte order
65
67
68/**
69 * make a new socket and its open file.
70 * @param type SOCK_STREAM or SOCK_DGRAM.
71 * @param protocol 0, or IPPROTO_TCP for a stream; IPPROTO_UDP or
72 * IPPROTO_ICMP (ping) for datagrams.
73 * @param flags O_NONBLOCK or 0.
74 * @return 0, or -EPROTONOSUPPORT, -ENOMEM.
75 */
77 struct file **out);
78
79/**
80 * the socket of an open file, or NULL if the file is not an IPv4 socket.
81 */
83
84/**
85 * give a socket a local address and port (port 0: one not in use).
86 * @return 0, or -EADDRINUSE, -EADDRNOTAVAIL, -EINVAL.
87 */
89
90/**
91 * a datagram socket: set the address datagrams go to and the only one
92 * they come from; AF_UNSPEC (family 0) takes the connection away.
93 * a stream: connect (blocks for the handshake unless the file is
94 * non-blocking).
95 * @return 0, or -EAFNOSUPPORT, -ENETUNREACH; for a stream also
96 * -ECONNREFUSED, -ETIMEDOUT, -EINPROGRESS, -EALREADY, -EISCONN,
97 * -EINTR.
98 */
100
101/**
102 * a stream socket takes connections.
103 * @return 0, or -EOPNOTSUPP (a datagram socket), -EINVAL (connected).
104 */
106
107/**
108 * take the next connection of a listening stream socket (blocks unless
109 * the file is non-blocking).
110 * @param flags O_NONBLOCK or 0, for the new file.
111 * @param peer receives the address of the other end.
112 * @return 0, or -EAGAIN, -EINVAL (not listening), -EOPNOTSUPP, -ENOMEM,
113 * -ERESTARTSYS.
114 */
115int32_t inet_socket_accept(struct file *file, uint32_t flags, struct file **out,
116 ksockaddr_in_t *peer);
117
118/**
119 * send one datagram from user memory, or data on a stream (as much as
120 * fits; it blocks for room unless the file is non-blocking).
121 * @param to where it goes, or NULL for the connected address (a stream
122 * does not use it).
123 * @param flags MSG_DONTWAIT.
124 * @return the bytes sent, or -EDESTADDRREQ, -EMSGSIZE, -ENETUNREACH,
125 * -EACCES (broadcast without SO_BROADCAST), -ENOBUFS, -ENOMEM,
126 * -EPIPE (shut down for writing), -EFAULT.
127 */
128int32_t inet_socket_send(struct file *file, const kiovec_t *iov, uint32_t iov_count,
129 const ksockaddr_in_t *to, uint32_t flags);
130
131/**
132 * receive one datagram into user memory (blocks unless the file is
133 * non-blocking or dontwait). a datagram longer than the buffers is cut;
134 * the rest of it is gone. a stream gives the bytes it has (0 at the end
135 * of the stream; -ECONNRESET once after a reset).
136 * @param from receives the sender (may be NULL).
137 * @param flags MSG_PEEK: leave it in the queue.
138 * @param full receives the datagram's whole length (may be NULL).
139 * @return the bytes received, or -EAGAIN, -ERESTARTSYS, -EFAULT; 0 when
140 * shut down for reading and the queue is empty.
141 */
142int32_t inet_socket_receive(struct file *file, const kiovec_t *iov, uint32_t iov_count,
143 ksockaddr_in_t *from, uint32_t flags, bool dontwait,
144 uint32_t *full);
145
146/**
147 * the local address (peer false) or the connected one (peer true).
148 * @return 0, or -ENOTCONN.
149 */
151
152/**
153 * setsockopt and getsockopt (an int value).
154 * @return 0, or -ENOPROTOOPT.
155 */
158
159/**
160 * shutdown: SHUT_RD, SHUT_WR or SHUT_RDWR.
161 * @return 0, or -EINVAL.
162 */
164
165/**
166 * give a received UDP datagram to the socket it is for (net_lock held).
167 * @return false if no socket takes it.
168 */
169bool inet_socket_deliver_udp(uint32_t dst, uint16_t dst_port, uint32_t src, uint16_t src_port,
170 const uint8_t *data, uint32_t length);
171
172/**
173 * give a received echo reply to the ICMP socket with its identifier
174 * (net_lock held).
175 */
176void inet_socket_deliver_echo(uint32_t src, uint16_t id, const uint8_t *message,
177 uint32_t length);
uint8_t flags
Definition idt.c:3
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 t...
Definition inet_socket.c:266
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...
Definition inet_socket.c:192
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).
Definition inet_socket.c:654
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).
Definition inet_socket.c:682
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).
Definition inet_socket.c:796
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.
Definition inet_socket.c:42
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).
Definition inet_socket.c:466
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).
Definition inet_socket.c:372
int32_t inet_socket_get_option(inet_socket_t *s, int32_t level, int32_t name, int32_t *value)
Definition inet_socket.c:526
int32_t inet_socket_shutdown(inet_socket_t *s, int32_t how)
shutdown: SHUT_RD, SHUT_WR or SHUT_RDWR.
Definition inet_socket.c:576
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).
Definition inet_socket.c:497
int32_t inet_socket_listen(inet_socket_t *s, uint32_t backlog)
a stream socket takes connections.
Definition inet_socket.c:791
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.
Definition inet_socket.c:37
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).
Definition inet_socket.c:154
struct inet_socket inet_socket_t
Definition inet_socket.h:66
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
unsigned short uint16_t
Definition stdint.h:13
int int32_t
Definition stdint.h:14
an open-file description.
Definition file.h:64
Definition inet_internal.h:23
uint8_t protocol
Definition inet_internal.h:25
Definition unix_socket.h:49
Definition inet_socket.h:59
uint16_t sin_family
Definition inet_socket.h:60
uint16_t sin_port
Definition inet_socket.h:61
uint32_t sin_addr
Definition inet_socket.h:62
uint8_t sin_zero[8]
Definition inet_socket.h:63
Unix domain stream sockets, with file descriptor passing.