KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
fs_socket.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 kos/fs_socket.h
4 Copyright (C) 2006, 2009, 2010, 2012, 2013 Lawrence Sebald
5
6*/
7
8/** \file kos/fs_socket.h
9 \brief Definitions for a sockets "filesystem".
10 \ingroup vfs_sockets
11
12 This file provides definitions to support the BSD-sockets-like filesystem
13 in KallistiOS. Technically, this filesystem mounts itself on /sock, but it
14 doesn't export any files there, so that point is largely irrelevant. The
15 filesystem is designed to be extensible, making it possible to add
16 additional socket family handlers at runtime. Currently, the kernel only
17 implements UDP sockets over IPv4 and IPv6, but as mentioned, this can be
18 extended in a fairly straightforward manner. In general, as a user of
19 KallistiOS (someone not interested in adding additional socket family
20 drivers), there's very little in this file that will be of interest.
21
22 \author Lawrence Sebald
23*/
24
25#ifndef __KOS_FS_SOCKET_H
26#define __KOS_FS_SOCKET_H
27
28#include <kos/cdefs.h>
29
30__BEGIN_DECLS
31
32#include <kos/fs.h>
33#include <kos/net.h>
34#include <sys/queue.h>
35#include <sys/socket.h>
36#include <stdint.h>
37
38/** \defgroup vfs_sockets Sockets
39 \brief VFS Driver for adding a BSD-sockets-like
40 filesystem.
41 \ingroup vfs_drivers
42*/
43
44struct fs_socket_proto;
45
46/** \brief Internal representation of a socket for fs_socket.
47 \ingroup vfs_sockets
48
49 This structure is the internal representation of a socket "file" that is
50 used within fs_socket. A normal user will never deal with this structure
51 directly (only protocol handlers and fs_socket itself ever sees this
52 structure directly).
53
54 \headerfile kos/fs_socket.h
55*/
56typedef struct net_socket {
57 /** \cond */
58 /* List handle */
59 LIST_ENTRY(net_socket) sock_list;
60 /** \endcond */
61
62 /** \brief File handle from the VFS layer. */
64
65 /** \brief The protocol handler for this socket. */
66 struct fs_socket_proto *protocol;
67
68 /** \brief Protocol-specific data. */
69 void *data;
71
72/** \brief Internal sockets protocol handler.
73 \ingroup vfs_sockets
74
75 This structure is a protocol handler used within fs_socket. Each protocol
76 that is supported has one of these registered for it within the kernel.
77 Generally, users will not come in contact with this structure (unless you're
78 planning on writing a protocol handler), and it can generally be ignored.
79
80 For a complete list of appropriate errno values to return from any functions
81 that are in here, take a look at the Single Unix Specification (aka, the
82 POSIX spec), specifically the page about sys/socket.h (and all the functions
83 that it defines, which is available at
84 http://www.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html .
85
86 \headerfile kos/fs_socket.h
87*/
88typedef struct fs_socket_proto {
89 /** \brief Entry into the global list of protocols.
90
91 Contrary to what Doxygen might think, this is <b>NOT</b> a function.
92 This should be initialized with the FS_SOCKET_PROTO_ENTRY macro before
93 adding the protocol to the kernel with fs_socket_proto_add().
94 */
95 TAILQ_ENTRY(fs_socket_proto) entry;
96
97 /** \brief Domain of support for this protocol handler.
98
99 This field determines which sockets domain this protocol handler
100 actually supports. This corresponds with the domain argument of the
101 ::socket() function.
102 */
104
105 /** \brief Type of support for this protocol handler.
106
107 This field determines which types of sockets that this protocol handler
108 pays attention to. This corresponds with the type argument of the
109 ::socket() function.
110 */
111 int type;
112
113 /** \brief Protocol of support for this protocol handler.
114
115 This field determines the protocol that this protocol handler actually
116 pays attention to. This corresponds with the protocol argument of the
117 ::socket() function.
118 */
120
121 /** \brief Create a new socket for the protocol.
122
123 This function must create a new socket, initializing any data that the
124 protocol might need for the socket, based on the parameters passed in.
125 The socket passed in is already initialized prior to the handler being
126 called, and will be cleaned up by fs_socket if an error is returned from
127 the handler (a return value of -1).
128
129 \param s The socket structure to initialize
130 \param domain Domain of the socket
131 \param type Type of the socket
132 \param protocol Protocol of the socket
133 \retval -1 On error (errno should be set appropriately)
134 \retval 0 On success
135 */
136 int (*socket)(net_socket_t *s, int domain, int type, int protocol);
137
138 /** \brief Close a socket that was created with the protocol.
139
140 This function must do any work required to close a socket and destroy
141 it. This function will be called when a socket requests to be closed
142 with the close system call. There are no errors defined for this
143 function.
144
145 \param s The socket to close
146 */
147 void (*close)(net_socket_t *hnd);
148
149 /** \brief Accept a connection on a socket created with the protocol.
150
151 This function should implement the ::accept() system call for the
152 protocol. The semantics are exactly as expected for that function.
153
154 \param s The socket to accept a connection on
155 \param addr The address of the incoming connection
156 \param alen The length of the address
157 \return A newly created socket for the incoming connection
158 or -1 on error (with errno set appropriately)
159 */
160 int (*accept)(net_socket_t *s, struct sockaddr *addr, socklen_t *alen);
161
162 /** \brief Bind a socket created with the protocol to an address.
163
164 This function should implement the ::bind() system call for the
165 protocol. The semantics are exactly as expected for that function.
166
167 \param s The socket to bind to the address
168 \param addr The address to bind to
169 \param alen The length of the address
170 \retval -1 On error (set errno appropriately)
171 \retval 0 On success
172 */
173 int (*bind)(net_socket_t *s, const struct sockaddr *addr, socklen_t alen);
174
175 /** \brief Connect a socket created with the protocol to a remote system.
176
177 This function should implement the ::connect() system call for the
178 protocol. The semantics are exactly as expected for that function.
179
180 \param s The socket to connect with
181 \param addr The address to connect to
182 \param alen The length of the address
183 \retval -1 On error (with errno set appropriately)
184 \retval 0 On success
185 */
186 int (*connect)(net_socket_t *s, const struct sockaddr *addr,
187 socklen_t alen);
188
189 /** \brief Listen for incoming connections on a socket created with the
190 protocol.
191
192 This function should implement the ::listen() system call for the
193 protocol. The semantics are exactly as expected for that function.
194
195 \param s The socket to listen on
196 \param backlog The number of connections to queue
197 \retval -1 On error (with errno set appropriately)
198 \retval 0 On success
199 */
200 int (*listen)(net_socket_t *s, int backlog);
201
202 /** \brief Receive data on a socket created with the protocol.
203
204 This function should implement the ::recvfrom() system call for the
205 protocol. The semantics are exactly as expected for that function. Also,
206 this function should implement the ::recv() system call, which will
207 call this function with NULL for addr and alen.
208
209 \param s The socket to receive data on
210 \param buffer The buffer to save data in
211 \param len The length of the buffer
212 \param flags Flags to the function
213 \param addr Space to store the address that data came from (NULL
214 if this was called by ::recv())
215 \param alen Space to store the length of the address (NULL if
216 this was called by ::recv())
217 \retval -1 On error (set errno appropriately)
218 \retval 0 No outstanding data and the peer has disconnected
219 cleanly
220 \retval n The number of bytes received (may be less than len)
221 */
222 ssize_t (*recvfrom)(net_socket_t *s, void *buffer, size_t len, int flags,
223 struct sockaddr *addr, socklen_t *alen);
224
225 /** \brief Send data on a socket created with the protocol.
226
227 This function should implement the ::sendto() system call for the
228 protocol. The semantics are exactly as expected for that function. Also,
229 this function should implement the ::send() system call, which will
230 call this function with NULL for addr and 0 for alen.
231
232 \param s The socket to send data on
233 \param msg The data to send
234 \param len The length of data to send
235 \param flags Flags to the function
236 \param addr The address to send data to (NULL if this was called
237 by ::send())
238 \param alen The length of the address (0 if this was called by
239 ::send())
240 \retval -1 On error (set errno appropriately)
241 \retval n The number of bytes actually sent (may be less than
242 len)
243 */
244 ssize_t (*sendto)(net_socket_t *s, const void *msg, size_t len, int flags,
245 const struct sockaddr *addr, socklen_t alen);
246
247 /** \brief Shut down a socket created with the protocol.
248
249 This function should implement the ::shutdown() system call for the
250 protocol. The semantics are exactly as expected for that function.
251
252 \param s The socket to shut down
253 \param how What should be shut down on the socket
254 \retval -1 On error (set errno appropriately)
255 \retval 0 On success
256 */
257 int (*shutdownsock)(net_socket_t *s, int how);
258
259 /** \brief Input a packet into a protocol.
260
261 This function should read in the packet specified by the arguments and
262 sort out what exactly to do with it. This usually involves checking if
263 there is an open socket with the source address and adding it to a
264 packet queue if there is.
265
266 \param src The interface the packet was input on
267 \param domain The low-level protocol used (AF_INET or AF_INET6)
268 \param hdr The low-level protocol header
269 \param data The packet itself, including any protocol headers,
270 but not any from lower-level protocols
271 \param size The size of the packet, not including any lower-
272 level protocol headers
273 \retval -1 On error (the packet is discarded)
274 \retval 0 On success
275 */
276 int (*input)(netif_t *src, int domain, const void *hdr, const uint8_t *data,
277 size_t size);
278
279 /** \brief Get socket options.
280
281 This function should implement the ::getsockopt() system call for the
282 given protocol. The semantics are exactly as defined for that function.
283
284 Currently all options (regardless of level) are passed onto the
285 protocol handler.
286
287 \param s The socket to get options for.
288 \param level The protocol level to get options at.
289 \param option_name The option to look up.
290 \param option_value Storage for the value of the option.
291 \param option_len The length of option_value on call, and the real
292 option length (if less than the original value)
293 on return.
294 \retval -1 On error (set errno appropriately).
295 \retval 0 On success.
296 */
297 int (*getsockopt)(net_socket_t *s, int level, int option_name,
298 void *option_value, socklen_t *option_len);
299
300 /** \brief Set socket options.
301
302 This function should implement the ::setsockopt() system call for the
303 given protocol. The semantics are exactly as defined for that function.
304
305 Currently all options (regardless of level) are passed onto the
306 protocol handler.
307
308 \param s The socket to set options for.
309 \param level The protocol level to set options at.
310 \param option_name The option to set.
311 \param option_value The value to set for the option.
312 \param option_len The length of the option_value value.
313 \retval -1 On error (set errno appropriately).
314 \retval 0 On success.
315 */
316 int (*setsockopt)(net_socket_t *s, int level, int option_name,
317 const void *option_value, socklen_t option_len);
318
319 /** \brief Get socket name.
320
321 This function should implement the ::getsockname() system call for the
322 given protocol. The semantics are exactly as defined for that function.
323
324 Currently all options (regardless of level) are passed onto the
325 protocol handler.
326
327 \param s The socket to get the name of.
328 \param name Pointer to a sockaddr structure which will hold
329 the resulting address information.
330 \param name_len The amount of space pointed to by name, in
331 bytes. On return, this is set to the actual size
332 of the returned address information.
333 \retval -1 On error (with errno set appropriately).
334 \retval 0 On success.
335 */
336 int (*getsockname)(net_socket_t *s, struct sockaddr *name, socklen_t *name_len);
337
338 /** \brief Get the name of the peer connected to a socket created with the
339 protocol.
340
341 This function should implement the ::getpeername() system call for the
342 protocol. The semantics are exactly as expected for that function.
343
344 \param s The socket from which to get the peer address.
345 \param name Pointer to a sockaddr structure which will hold
346 the resulting address information.
347 \param name_len The amount of space pointed to by name, in
348 bytes. On return, this is set to the actual size
349 of the returned address information.
350 \retval -1 On error (with errno set appropriately).
351 \retval 0 On success.
352 */
353 int (*getpeername)(net_socket_t *s, struct sockaddr *name, socklen_t *name_len);
354
355 /** \brief Manipulate file options.
356
357 This function should implement the fcntl() system call for the given
358 protocol. The semantics are exactly as defined for that function.
359
360 \param s The socket to manipulate.
361 \param cmd The fcntl command to run.
362 \param ap Arguments to the command.
363 \retval -1 On error (generally, set errno appropriately).
364 */
365 int (*fcntl)(net_socket_t *s, int cmd, va_list ap);
366
367 /** \brief Poll for events.
368
369 This function should check the given socket for any events that may have
370 already occurred that are specified. This is used to back the ::poll()
371 system call. This function should not block to wait for any events. This
372 function may be called in an interrupt.
373
374 \param s The socket to poll.
375 \param events The events to check for.
376 \retval A mask of any of the events specified that are
377 currently true in the socket. 0 if none are true.
378 */
379 short (*poll)(net_socket_t *s, short events);
381
382/** \brief Initializer for the entry field in the fs_socket_proto_t struct.
383 \ingroup vfs_sockets
384*/
385#define FS_SOCKET_PROTO_ENTRY { NULL, NULL }
386
387/* \cond */
388/* Init/shutdown */
389int fs_socket_init(void);
390int fs_socket_shutdown(void);
391/* \endcond */
392
393/** \brief Open a socket without calling the protocol initializer.
394 \ingroup vfs_sockets
395
396 This function creates a new socket, but does not call the protocol's
397 socket() function. This is meant to be used for things like accepting an
398 incoming connection, where calling the regular socket initializer could
399 cause issues. You shouldn't really have any need to call this function
400 unless you are implementing a new protocol handler.
401
402 \param proto The protocol to use for the socket.
403
404 \return The newly created socket on success, NULL on failure.
405
406 \par Error Conditions:
407 \em EWOULDBLOCK - if the function would block in an IRQ \n
408 \em ENOMEM - out of memory \n
409 \em EMFILE - too many files open
410*/
412
413/** \defgroup sock_flags Flags
414 \brief Flags for Socket VFS
415 \ingroup vfs_sockets
416
417 These are the available flags defined for sockets.
418
419 Every flag after FS_SOCKET_FAM_MAX is for internal-use only, and should
420 never be passed into any functions.
421 @{
422*/
423#define FS_SOCKET_NONBLOCK 0x00000001 /** \brief Non-blocking operations */
424#define FS_SOCKET_V6ONLY 0x00000002 /** \brief IPv6 Only */
425
426#define FS_SOCKET_GEN_MAX 0x00008000 /** \brief Maximum generic flag */
427#define FS_SOCKET_FAM_MAX 0x00800000 /** \brief Maximum family flag */
428/** @} */
429
430/** \brief Input a packet into some socket family handler.
431 \ingroup vfs_sockets
432
433 This function is used by the lower-level network protocol handlers to input
434 packets for further processing by upper-level protocols. This will call the
435 input function on the family handler, if one is found.
436
437 \param src The network interface the packet came in on
438 \param domain The low-level protocol used (AF_INET or AF_INET6)
439 \param protocol The upper-level protocol that we're looking for
440 \param hdr The low-level protocol header
441 \param data The upper-level packet, without any lower-level protocol
442 headers, but with the upper-level ones intact
443 \param size The size of the packet (the data parameter)
444
445 \retval -2 The protocol is not known
446 \retval -1 Protocol-level error processing packet
447 \retval 0 On success
448*/
449int fs_socket_input(netif_t *src, int domain, int protocol, const void *hdr,
450 const uint8_t *data, size_t size);
451
452/** \brief Add a new protocol for use with fs_socket.
453 \ingroup vfs_sockets
454
455 This function registers a protocol handler with fs_socket for use when
456 creating and using sockets. This protocol handler must implement all of the
457 functions in the fs_socket_proto_t structure. See the code in
458 kos/kernel/net/net_udp.c for an example of how to do this.
459
460 \warning
461 This function is NOT safe to call inside an interrupt.
462
463 \param proto The new protocol handler to register
464
465 \retval 0 On success (no error conditions are currently defined)
466*/
468
469/** \brief Unregister a protocol from fs_socket.
470 \ingroup vfs_sockets
471
472 This function does the exact opposite of fs_socket_proto_add, and removes
473 a protocol from use with fs_socket.
474
475 \note
476 It is the programmer's responsibility to make sure that no sockets are
477 still around that are registered with the protocol to be removed (as
478 they will not work properly once the handler has been removed).
479
480 \param proto The protocol handler to remove
481
482 \retval -1 On error (This function does not directly change errno)
483 \retval 0 On success
484*/
486
487__END_DECLS
488
489#endif /* __KOS_FS_SOCKET_H */
490
Various common macros used throughout the codebase.
Virtual filesystem support.
int accept(int socket, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
Set socket options.
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
int getsockname(int socket, struct sockaddr *name, socklen_t *name_len)
Get socket name.
__uint32_t socklen_t
Socket length type.
Definition socket.h:39
int getsockopt(int socket, int level, int option_name, void *option_value, socklen_t *option_len)
Get socket options.
ssize_t sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
Send a message on a socket.
int listen(int socket, int backlog)
Listen for socket connections and set the queue length.
ssize_t recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)
Receive a message on a socket.
int connect(int socket, const struct sockaddr *address, socklen_t address_len)
Connect a socket.
int socket(int domain, int type, int protocol)
Create an endpoint for communications.
int getpeername(int socket, struct sockaddr *__RESTRICT name, socklen_t *__RESTRICT name_len)
Get the name of the connected peer socket.
int poll(struct pollfd fds[], nfds_t nfds, int timeout)
Poll a group of file descriptors for activity.
int file_t
File descriptor type.
Definition fs.h:94
int fs_socket_proto_remove(fs_socket_proto_t *proto)
Unregister a protocol from fs_socket.
net_socket_t * fs_socket_open_sock(fs_socket_proto_t *proto)
Open a socket without calling the protocol initializer.
int fs_socket_proto_add(fs_socket_proto_t *proto)
Add a new protocol for use with fs_socket.
int fs_socket_input(netif_t *src, int domain, int protocol, const void *hdr, const uint8_t *data, size_t size)
Input a packet into some socket family handler.
Network support.
Main sockets header.
Internal sockets protocol handler.
Definition fs_socket.h:88
int type
Type of support for this protocol handler.
Definition fs_socket.h:111
int domain
Domain of support for this protocol handler.
Definition fs_socket.h:103
int protocol
Protocol of support for this protocol handler.
Definition fs_socket.h:119
TAILQ_ENTRY(fs_socket_proto) entry
Entry into the global list of protocols.
Internal representation of a socket for fs_socket.
Definition fs_socket.h:56
struct fs_socket_proto * protocol
The protocol handler for this socket.
Definition fs_socket.h:66
file_t fd
File handle from the VFS layer.
Definition fs_socket.h:63
void * data
Protocol-specific data.
Definition fs_socket.h:69
Structure describing one usable network device.
Definition net.h:53
Socket address structure.
Definition socket.h:47