|
ginnOS
The reference: libc, the libraries and the kernel
|
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). | |
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.
| #define ICMP_HEADER_SIZE 8 |
| #define DATAGRAM_MAX 65507 |
| 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.
make a new socket and its open file.
| type | SOCK_STREAM or SOCK_DGRAM. |
| protocol | 0, or IPPROTO_TCP for a stream; IPPROTO_UDP or IPPROTO_ICMP (ping) for datagrams. |
| flags | O_NONBLOCK or 0. |
| 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.
| flags | O_NONBLOCK or 0. |
| 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 | ( | 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).
| 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).
| to | where it goes, or NULL for the connected address (a stream does not use it). |
| flags | MSG_DONTWAIT. |
| 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).
| from | receives the sender (may be NULL). |
| flags | MSG_PEEK: leave it in the queue. |
| full | receives the datagram's whole length (may be NULL). |
| 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 | ( | 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).
| flags | O_NONBLOCK or 0, for the new file. |
| peer | receives the address of the other end. |