My Kernel v0.1.0
socket.h
1
10#ifndef KERNEL_SOCKET_H
11#define KERNEL_SOCKET_H
12
13#include <kernel/net.h>
14#include <kernel/spinlock.h>
15#include <kernel/vfs.h>
16
17#include <libalgo/queue.h>
18#include <utils/container_of.h>
19
20struct packet;
21
26};
27
29struct socket {
30 struct file *file;
31 const struct socket_protocol *proto;
34 void *data;
37};
38
40static inline bool socket_mode_is_connection(enum socket_type socket_type)
41{
42 return socket_type == SOCK_STREAM;
43}
44
46static inline void socket_lock(struct socket *socket)
47{
48 spinlock_acquire(&socket->lock);
49}
50
52static inline void socket_unlock(struct socket *socket)
53{
55}
56
64 struct socket socket;
65 struct vnode vnode;
66};
67
69static inline struct socket *socket_from_vnode(struct vnode *vnode)
70{
72 vnode);
73 return &socket_node->socket;
74}
75
77static inline struct vnode *socket_node(struct socket *socket)
78{
80 socket);
81 return &socket_node->vnode;
82}
83
85struct socket *socket_alloc(void);
86
93error_t socket_init(struct socket *socket, int domain, int type, int proto);
94
98error_t socket_enqueue_packet(struct socket *socket, struct packet *packet);
99
104
114 enum communication_domain domain;
115 node_t this;
119 error_t (*socket_init)(struct socket *, int type, int proto);
120};
121
123error_t socket_domain_register(struct socket_domain *);
124
126struct socket_protocol_ops {
128 error_t (*init)(struct socket *);
130 error_t (*bind)(struct socket *, struct sockaddr *addr, socklen_t addrlen);
132 error_t (*connect)(struct socket *, struct sockaddr *addr,
133 socklen_t addrlen);
135 ssize_t (*sendmsg)(struct socket *, const struct msghdr *, int flags);
137 ssize_t (*recvmsg)(struct socket *, struct msghdr *, int flags);
138};
139
141struct socket_protocol {
142 int proto;
143 enum socket_type type;
144 const struct socket_protocol_ops *ops;
145};
146
147#endif /* KERNEL_SOCKET_H */
148
static ALWAYS_INLINE void spinlock_release(spinlock_t *lock)
Release a spinlock for others to take it.
Definition: spinlock.h:90
socket_state
Socket connection state.
Definition: socket.h:23
struct socket * socket_alloc(void)
Allocate and initialize a new socket.
Definition: socket.c:33
error_t socket_domain_register(struct socket_domain *)
Register a new domain of sockets.
Definition: socket.c:17
static struct socket * socket_from_vnode(struct vnode *vnode)
Definition: socket.h:69
static bool socket_mode_is_connection(enum socket_type socket_type)
Check whether the socket is connection oriented (TCP, ...)
Definition: socket.h:40
error_t socket_enqueue_packet(struct socket *socket, struct packet *packet)
Place a packet inside the socket's receive queue.
Definition: socket.c:46
error_t socket_init(struct socket *socket, int domain, int type, int proto)
Initialize a socket's underlying protocol.
Definition: socket.c:26
struct packet * socket_dequeue_packet(struct socket *socket)
Retreive one packet from the socket's receive queue.
Definition: socket.c:55
@ SOCKET_DISCONNECTED
Definition: socket.h:24
@ SOCKET_CONNECTED
Definition: socket.h:25
#define container_of(_ptr, _struct, _field)
Cast a member of a structure out to the containing structure.
Definition: container_of.h:12
Opened file description.
Definition: file.h:29
The head of a doubly linked list.
Definition: linked_list.h:43
Intrusive doubly-linked list node.
Definition: linked_list.h:27
A network packet.
Definition: packet.h:51
Socket communication domain.
Definition: socket.h:113
error_t(* socket_init)(struct socket *, int type, int proto)
Match socket with its protocol, and initialize necessary per-domain data.
Definition: socket.h:119
enum communication_domain domain
Definition: socket.h:114
Socket node.
Definition: socket.h:63
struct vnode vnode
Definition: socket.h:65
struct socket socket
Definition: socket.h:64
A BSD socket.
Definition: socket.h:29
queue_t rx_packets
Definition: socket.h:35
enum socket_state state
Definition: socket.h:32
spinlock_t lock
Definition: socket.h:33
const struct socket_protocol * proto
Definition: socket.h:31
void * data
Definition: socket.h:34
struct file * file
Definition: socket.h:30
spinlock_t rx_lock
Definition: socket.h:36
Spinlock.
Definition: spinlock.h:29
represents a single virtual node
Definition: vfs.h:205