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