AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1#ifndef _NET_SOCKET_H
2#define _NET_SOCKET_H
3
4#include <core/system.h>
5#include <fs/vfs.h>
6#include <fs/iobuf.h>
7
8#include <aquila/socket.h>
9
10/*
11 * Sockets (net/socket.c): what every kind of socket provides, and the
12 * operations that the system calls use, whatever the socket's domain.
13 */
14
16struct socket {
17 int domain;
18 int type;
20
21 /* its domain's operations */
22 struct sock_ops *ops;
23
24 /* the domain's own data */
25 void *p;
26};
27
29struct sock_ops {
30 int (*accept) (struct file *socket, struct file *conn, const struct sockaddr *addr, socklen_t *len);
31 int (*bind) (struct file *socket, const struct sockaddr *addr, socklen_t len);
32 int (*connect)(struct file *socket, const struct sockaddr *addr, socklen_t len);
33 int (*listen) (struct file *socket, int backlog);
34
35 /* recvfrom() and sendto(): `io->resid` bytes through the iobuf;
36 * `from` and `to` (in kernel memory) are NULL if the caller doesn't
37 * want, or doesn't give, an address */
38 ssize_t (*recv)(struct file *socket, struct iobuf *io, int flags,
39 struct sockaddr *from, socklen_t *fromlen);
40 ssize_t (*send)(struct file *socket, struct iobuf *io, int flags,
41 const struct sockaddr *to, socklen_t tolen);
42
43 /* gets its address, or its peer's */
44 int (*getname)(struct file *socket, struct sockaddr *addr, socklen_t *len, int peer);
45
46 /* options of a level other than SOL_SOCKET (the protocol's), and the
47 * SOL_SOCKET options that the socket layer doesn't handle itself */
48 int (*getsockopt)(struct file *socket, int level, int name, void *val, socklen_t *len);
49 int (*setsockopt)(struct file *socket, int level, int name, const void *val, socklen_t len);
50
51 /* ioctl(): FIONREAD, and the protocol's own (the interfaces') */
52 int (*ioctl)(struct file *socket, int request, void *argp);
53
54 /* whether a read, or a write, wouldn't wait */
55 int (*can_read)(struct file *socket, size_t len);
56 int (*can_write)(struct file *socket, size_t len);
57
58 /* the wait queue a reader (or writer) sleeps on until it can; poll()
59 * waits on it too */
60 struct waitq *(*wait_queue)(struct file *socket, int write);
61
62 /* the last descriptor was closed */
63 int (*shutdown) (struct file *socket, int how);
64
65 /* shutdown(): no more reading or writing (SHUT_*); it stays open */
66 int (*shut) (struct file *socket, int how);
67};
68
69/* An open file's flag: it's a socket. */
70#define FILE_SOCKET 0x80000000
71
72/* The sockets' operations, whatever their domain (net/socket.c). */
73int socket_create(struct file *file, int domain, int type, int protocol);
74int socket_bind(struct file *file, const struct sockaddr *addr, socklen_t len);
75int socket_accept(struct file *file, struct file *conn, const struct sockaddr *addr, socklen_t *len);
76int socket_connect(struct file *file, const struct sockaddr *addr, socklen_t len);
77int socket_listen(struct file *file, int backlog);
78ssize_t socket_send(struct file *file, struct iobuf *io, int flags, const struct sockaddr *to, socklen_t tolen);
79ssize_t socket_recv(struct file *file, struct iobuf *io, int flags, struct sockaddr *from, socklen_t *fromlen);
80int socket_getname(struct file *file, struct sockaddr *addr, socklen_t *len, int peer);
81int socket_getsockopt(struct file *file, int level, int name, void *val, socklen_t *len);
82int socket_setsockopt(struct file *file, int level, int name, const void *val, socklen_t len);
83int socket_ioctl(struct file *file, int request, void *argp);
84int socket_can_read(struct file *file, size_t len);
85int socket_can_write(struct file *file, size_t len);
86struct waitq *socket_wait_queue(struct file *file, int write);
87int socket_shutdown(struct file *file, int how);
88int socket_shutdown_dir(struct file *file, int how);
89
90/* AF_UNIX */
91int socket_unix_create(struct file *file, int domain, int type, int protocol);
92
93/* AF_INET */
94int socket_inet_create(struct file *file, int domain, int type, int protocol);
95int socket_tcp_create(struct file *file, int domain, int type, int protocol);
96
97#endif /* ! _NET_SOCKET_H */
int socket_listen(struct file *file, int backlog)
Makes a socket listen for connections, for listen().
Definition socket.c:120
int socket_inet_create(struct file *file, int domain, int type, int protocol)
Makes an AF_INET socket: a TCP, UDP or ping socket.
Definition socket.c:230
int socket_shutdown_dir(struct file *file, int how)
Shuts down reading, writing or both on a socket, for shutdown().
Definition socket.c:245
int socket_can_write(struct file *file, size_t len)
Says whether a socket has room to write, for select() and poll().
Definition socket.c:225
int socket_getsockopt(struct file *file, int level, int name, void *val, socklen_t *len)
Reads a socket option, for getsockopt().
Definition socket.c:325
int socket_connect(struct file *file, const struct sockaddr *addr, socklen_t len)
Connects a socket to a peer, for connect().
Definition socket.c:102
int socket_setsockopt(struct file *file, int level, int name, const void *val, socklen_t len)
Sets a socket option, for setsockopt().
Definition socket.c:361
int socket_can_read(struct file *file, size_t len)
Says whether a socket has something to read, for select() and poll().
Definition socket.c:189
struct waitq * socket_wait_queue(struct file *file, int write)
Finds the wait queue that a socket's readers (or writers) sleep on, for select() and poll() to regist...
Definition socket.c:209
int socket_create(struct file *file, int domain, int type, int protocol)
Makes a socket of a domain in an open file, for socket().
Definition socket.c:27
int socket_tcp_create(struct file *file, int domain, int type, int protocol)
Makes a TCP socket, with its connection, CLOSED.
Definition tcp_socket.c:490
int socket_unix_create(struct file *file, int domain, int type, int protocol)
Makes an AF_UNIX socket, for socket().
Definition unix.c:68
int socket_accept(struct file *file, struct file *conn, const struct sockaddr *addr, socklen_t *len)
Accepts a connection on a listening socket, for accept().
Definition socket.c:59
int socket_ioctl(struct file *file, int request, void *argp)
Handles an ioctl() on a socket, by passing it to the protocol.
Definition socket.c:399
int socket_shutdown(struct file *file, int how)
Releases a socket when its open file is closed (its last descriptor went).
Definition socket.c:267
ssize_t socket_recv(struct file *file, struct iobuf *io, int flags, struct sockaddr *from, socklen_t *fromlen)
Receives data from a socket, for recv(), recvfrom() and read().
Definition socket.c:169
int socket_getname(struct file *file, struct sockaddr *addr, socklen_t *len, int peer)
Gets a socket's local address, or its peer's, for getsockname() and getpeername().
Definition socket.c:289
ssize_t socket_send(struct file *file, struct iobuf *io, int flags, const struct sockaddr *to, socklen_t tolen)
Sends data on a socket, for send(), sendto() and write().
Definition socket.c:141
int socket_bind(struct file *file, const struct sockaddr *addr, socklen_t len)
Gives a socket its local address, for bind().
Definition socket.c:83
An open file (POSIX's "open file description", fs/file.c).
Definition vfs.h:157
int flags
Definition vfs.h:164
An iobuf.
Definition iobuf.h:35
A domain's socket operations.
Definition socket.h:29
int(*) connect(struct file *socket, const struct sockaddr *addr, socklen_t len)
Definition socket.h:32
int(*) getname(struct file *socket, struct sockaddr *addr, socklen_t *len, int peer)
Definition socket.h:44
int(*) bind(struct file *socket, const struct sockaddr *addr, socklen_t len)
Definition socket.h:31
int(*) shutdown(struct file *socket, int how)
Definition socket.h:63
ssize_t(*) send(struct file *socket, struct iobuf *io, int flags, const struct sockaddr *to, socklen_t tolen)
Definition socket.h:40
int(*) shut(struct file *socket, int how)
Definition socket.h:66
int(*) can_write(struct file *socket, size_t len)
Definition socket.h:56
int(*) accept(struct file *socket, struct file *conn, const struct sockaddr *addr, socklen_t *len)
Definition socket.h:30
int(*) listen(struct file *socket, int backlog)
Definition socket.h:33
int(*) setsockopt(struct file *socket, int level, int name, const void *val, socklen_t len)
Definition socket.h:49
int(*) can_read(struct file *socket, size_t len)
Definition socket.h:55
ssize_t(*) recv(struct file *socket, struct iobuf *io, int flags, struct sockaddr *from, socklen_t *fromlen)
Definition socket.h:38
int(*) getsockopt(struct file *socket, int level, int name, void *val, socklen_t *len)
Definition socket.h:48
int(*) ioctl(struct file *socket, int request, void *argp)
Definition socket.h:52
A socket.
Definition socket.h:16
int domain
Definition socket.h:17
void * p
Definition socket.h:25
int type
Definition socket.h:18
struct sock_ops * ops
Definition socket.h:22
int protocol
Definition socket.h:19
Definition waitq.h:19
long int ssize_t
Definition types.h:12