KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 sys/socket.h
4 Copyright (C) 2006, 2010, 2012, 2017 Lawrence Sebald
5
6*/
7
8/** \file sys/socket.h
9 \brief Main sockets header.
10 \ingroup networking_sockets
11
12 This file contains the standard definitions (as directed by the POSIX 2008
13 spec) for socket-related functionality in the AF_INET and AF_INET6 address
14 families. This does not include anything related to UNIX domain sockets
15 and is not guaranteed to have everything that one might have in a
16 fully-standards compliant implementation of the POSIX spec.
17
18 \author Lawrence Sebald
19*/
20
21#ifndef __SYS_SOCKET_H
22#define __SYS_SOCKET_H
23
24#include <sys/cdefs.h>
25#include <sys/types.h>
26#include <sys/uio.h>
27
28__BEGIN_DECLS
29
30/** \defgroup networking_sockets Sockets
31 \brief POSIX Sockets Interface for IPv4 and IPv6
32 Address Families
33 \ingroup networking
34
35 @{
36*/
37
38/** \brief Socket length type. */
39typedef __uint32_t socklen_t;
40
41/** \brief Socket address family type. */
42typedef __uint8_t sa_family_t;
43
44/** \brief Socket address structure.
45 \headerfile sys/socket.h
46*/
47struct sockaddr {
48 /** \brief Address family. */
50 /** \brief Address data. */
51 char sa_data[];
52};
53
54/** \brief Size of the struct sockaddr_storage.
55 The size here is chosen for compatibility with anything that may expect the
56 struct sockaddr_storage to be of size 128. Technically, since there are no
57 current plans to support AF_UNIX sockets, this could be smaller, but most
58 implementations make it 128 bytes.
59*/
60#define _SS_MAXSIZE 128
61
62/** \brief Desired alignment of struct sockaddr_storage. */
63#define _SS_ALIGNSIZE (sizeof(__uint64_t))
64
65/** \brief First padding size used within struct sockaddr_storage. */
66#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
67
68/** \brief Second padding size used within struct sockaddr_storage. */
69#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t) + \
70 _SS_PAD1SIZE + _SS_ALIGNSIZE))
71
72/** \brief Socket address structure of appropriate size to hold any supported
73 socket type's addresses.
74 \headerfile sys/socket.h
75*/
77 /** \brief Address family. */
79
80 /** \brief First padding field. */
82
83 /** \brief Used to force alignment. */
84 __uint64_t _ss_align;
85
86 /** \brief Second padding field to fill up the space required. */
88};
89
90/** \brief Datagram socket type.
91
92 This socket type specifies that the socket in question transmits datagrams
93 that may or may not be reliably transmitted. With IP, this implies using UDP
94 as the underlying protocol.
95*/
96#define SOCK_DGRAM 1
97
98/** \brief Stream socket type.
99
100 This socket type specifies that the socket in question acts like a stream
101 or pipe between the two endpoints. Sockets of this type can be assumed to be
102 reliable -- unless an error is returned, all packets will be received at the
103 other end in the order they are sent. With IP, this implies using TCP as the
104 underlying protocol.
105*/
106#define SOCK_STREAM 2
107
108/** \brief Socket-level option setting.
109
110 This constant should be used with the setsockopt() or getsockopt() function
111 to represent that options should be accessed at the socket level, not the
112 protocol level.
113*/
114#define SOL_SOCKET 1
115
116/** @} */
117
118/** \defgroup so_opts Options
119 \brief Socket-level options
120 \ingroup networking_sockets
121
122 These are the various socket-level options that can be accessed with the
123 setsockopt() and getsockopt() functions for the SOL_SOCKET level value.
124
125 Not all of these are currently supported, but they are listed for
126 completeness.
127
128 \see ipv6_opts
129 \see ipv4_opts
130 \see udp_opts
131
132 @{
133*/
134#define SO_ACCEPTCONN 1 /**< \brief Socket is accepting connections (get) */
135#define SO_BROADCAST 2 /**< \brief Support broadcasting (get/set) */
136#define SO_DEBUG 3 /**< \brief Record debugging info (get/set) */
137#define SO_DONTROUTE 4 /**< \brief Do not route packets (get/set) */
138#define SO_ERROR 5 /**< \brief Retrieve error status (get) */
139#define SO_KEEPALIVE 6 /**< \brief Send keepalive messages (get/set) */
140#define SO_LINGER 7 /**< \brief Socket lingers on close (get/set) */
141#define SO_OOBINLINE 8 /**< \brief OOB data is inline (get/set) */
142#define SO_RCVBUF 9 /**< \brief Receive buffer size (get/set) */
143#define SO_RCVLOWAT 10 /**< \brief Receive low-water mark (get/set) */
144#define SO_RCVTIMEO 11 /**< \brief Receive timeout value (get/set) */
145#define SO_REUSEADDR 12 /**< \brief Reuse local addresses (get/set) */
146#define SO_SNDBUF 13 /**< \brief Send buffer size (get/set) */
147#define SO_SNDLOWAT 14 /**< \brief Send low-water mark (get/set) */
148#define SO_SNDTIMEO 15 /**< \brief Send timeout value (get/set) */
149#define SO_TYPE 16 /**< \brief Socket type (get) */
150/** @} */
151
152/** \defgroup msg_flags Message Flags
153 \brief Socket message flags
154 \ingroup networking_sockets
155
156 The following flags can be used with the recv(), recvfrom(), send(),
157 and sendto() functions as the flags parameter.
158
159 Note that not all of these are currently supported, but they are listed for
160 completeness. Those that are unsupported have (U) at the end of their
161 description. Also, for the time being, the supported flags are only
162 supported for TCP.
163
164 @{
165*/
166#define MSG_CTRUNC 0x01 /**< \brief Control data truncated (U) */
167#define MSG_DONTROUTE 0x02 /**< \brief Send without routing (U) */
168#define MSG_EOR 0x04 /**< \brief Terminate a record (U) */
169#define MSG_OOB 0x08 /**< \brief Out-of-band data (U) */
170#define MSG_PEEK 0x10 /**< \brief Leave received data in queue */
171#define MSG_TRUNC 0x20 /**< \brief Normal data truncated (U) */
172#define MSG_WAITALL 0x40 /**< \brief Attempt to fill read buffer */
173#define MSG_DONTWAIT 0x80 /**< \brief Make this call non-blocking (non-standard) */
174/** @} */
175
176/** \addtogroup networking_sockets
177 @{
178*/
179
180/** \brief Unspecified address family. */
181#define AF_UNSPEC 0
182
183/** \brief Internet domain sockets for use with IPv4 addresses. */
184#define AF_INET 1
185
186/** \brief Internet domain sockets for use with IPv6 addresses. */
187#define AF_INET6 2
188
189/** \brief Unspecified protocol family. */
190#define PF_UNSPEC AF_UNSPEC
191
192/** \brief Protocol family for Internet domain sockets (IPv4). */
193#define PF_INET AF_INET
194
195/** \brief Protocol family for Internet domain sockets (IPv6). */
196#define PF_INET6 AF_INET6
197
198/** \brief Disable further receive operations. */
199#define SHUT_RD 0x00000001
200
201/** \brief Disable further send operations. */
202#define SHUT_WR 0x00000002
203
204/** \brief Disable further send and receive operations. */
205#define SHUT_RDWR (SHUT_RD | SHUT_WR)
206
207/** \brief Maximum backlog for a listening socket. */
208#define SOMAXCONN 32
209
210/** \brief Accept a new connection on a socket.
211
212 This function extracts the first connection on the queue of connections of
213 the specified socket, creating a new socket with the same protocol and
214 address family as that socket for communication with the extracted
215 connection.
216
217 \param socket A socket created with socket() that has been bound to an
218 address with bind() and is listening for connections
219 after a call to listen().
220 \param address A pointer to a sockaddr structure where the address of
221 the connecting socket will be returned (can be NULL).
222 \param address_len A pointer to a socklen_t which specifies the amount of
223 space in address on input, and the amount used of the
224 space on output.
225
226 \return On success, the non-negative file descriptor of the
227 new connection, otherwise -1 and errno will be set to
228 the appropriate error value.
229*/
230int accept(int socket, struct sockaddr *address, socklen_t *address_len);
231
232/** \brief Bind a name to a socket.
233
234 This function assigns the socket to a unique name (address).
235
236 \param socket A socket that is to be bound.
237 \param address A pointer to a sockaddr structure where the name to be
238 assigned to the socket resides.
239 \param address_len The length of the address structure.
240
241 \retval 0 On success.
242 \retval -1 On error, sets errno as appropriate.
243*/
244int bind(int socket, const struct sockaddr *address, socklen_t address_len);
245
246/** \brief Connect a socket.
247
248 This function attempts to make a connection to a resource on a connection-
249 mode socket, or sets/resets the peer address on a connectionless one.
250
251 \param socket A socket that is to be connected.
252 \param address A pointer to a sockaddr structure where the name of the
253 peer resides.
254 \param address_len The length of the address structure.
255
256 \retval 0 On success.
257 \retval -1 On error, sets errno as appropriate.
258*/
259int connect(int socket, const struct sockaddr *address, socklen_t address_len);
260
261/** \brief Listen for socket connections and set the queue length.
262
263 This function marks a connection-mode socket for incoming connections.
264
265 \param socket A connection-mode socket to listen on.
266 \param backlog The number of queue entries.
267
268 \retval 0 On success.
269 \retval -1 On error, sets errno as appropriate.
270*/
271int listen(int socket, int backlog);
272
273/** \brief Receive a message on a connected socket.
274
275 This function receives messages from the peer on a connected socket.
276
277 \param socket The socket to receive on.
278 \param buffer A pointer to a buffer to store the message in.
279 \param length The length of the buffer.
280 \param flags The type of message reception. Set to 0 for now.
281
282 \return On success, the length of the message in bytes. If no
283 messages are available, and the socket has been shut
284 down, 0. On error, -1, and sets errno as appropriate.
285*/
286ssize_t recv(int socket, void *buffer, size_t length, int flags);
287
288/** \brief Receive a message on a socket.
289
290 This function receives messages from a peer on a (usually connectionless)
291 socket.
292
293 \param socket The socket to receive on.
294 \param buffer A pointer to a buffer to store the message in.
295 \param length The length of the buffer.
296 \param flags The type of message reception. Set to 0 for now.
297 \param address A pointer to a sockaddr structure to store the peer's
298 name in.
299 \param address_len A pointer to the length of the address structure on
300 input, the number of bytes used on output.
301
302 \return On success, the length of the message in bytes. If no
303 messages are available, and the socket has been shut
304 down, 0. On error, -1, and sets errno as appropriate.
305*/
306ssize_t recvfrom(int socket, void *buffer, size_t length, int flags,
307 struct sockaddr *address, socklen_t *address_len);
308
309/** \brief Send a message on a connected socket.
310
311 This function sends messages to the peer on a connected socket.
312
313 \param socket The socket to send on.
314 \param message A pointer to a buffer with the message to send.
315 \param length The length of the message.
316 \param flags The type of message transmission. Set to 0 for now.
317
318 \return On success, the number of bytes sent. On error, -1,
319 and sets errno as appropriate.
320*/
321ssize_t send(int socket, const void *message, size_t length, int flags);
322
323/** \brief Send a message on a socket.
324
325 This function sends messages to the peer on a (usually connectionless)
326 socket. If used on a connection-mode socket, this function may change the
327 peer that the socket is connected to, or it may simply return error.
328
329 \param socket The socket to send on.
330 \param message A pointer to a buffer with the message to send.
331 \param length The length of the message.
332 \param flags The type of message transmission. Set to 0 for now.
333 \param dest_addr A pointer to a sockaddr structure with the peer's name.
334 \param dest_len The length of dest_addr, in bytes.
335
336 \return On success, the number of bytes sent. On error, -1,
337 and sets errno as appropriate.
338*/
339ssize_t sendto(int socket, const void *message, size_t length, int flags,
340 const struct sockaddr *dest_addr, socklen_t dest_len);
341
342/** \brief Shutdown socket send and receive operations.
343
344 This function closes a specific socket for the set of specified operations.
345
346 \param socket The socket to shutdown.
347 \param how The type of shutdown.
348
349 \retval 0 On success.
350 \retval -1 On error, sets errno as appropriate.
351
352 \see SHUT_RD
353 \see SHUT_WR
354 \see SHUT_RDWR
355*/
356int shutdown(int socket, int how);
357
358/** \brief Create an endpoint for communications.
359
360 This function creates an unbound socket for communications with the
361 specified parameters.
362
363 \param domain The domain to create the socket in (i.e, AF_INET).
364 \param type The type of socket to be created (i.e, SOCK_DGRAM).
365 \param protocol The protocol to use with the socket. May be 0 to allow
366 a default to be used.
367
368 \return A non-negative file descriptor on success. -1 on error,
369 and sets errno as appropriate.
370*/
371int socket(int domain, int type, int protocol);
372
373/** \brief Get socket name.
374
375 This function returns the locally bound address information for a specified
376 socket.
377
378 \param socket The socket to get the name of.
379 \param name Pointer to a sockaddr structure which will hold the
380 resulting address information.
381 \param name_len The amount of space pointed to by name, in bytes.
382 On return, this is set to the actual size of the
383 returned address information.
384
385 \retval -1 On error, sets errno as appropriate.
386 \retval 0 On success.
387*/
388int getsockname(int socket, struct sockaddr *name, socklen_t *name_len);
389
390/**
391 \brief Get the name of the connected peer socket.
392
393 This function retrieves the address of the peer connected to the socket
394 specified by the socket descriptor. The address is returned in the buffer
395 pointed to by the name parameter, and the actual length of the address is
396 returned in the name_len parameter.
397
398 \param socket A socket that is already connected to a peer.
399 \param name A pointer to a sockaddr structure where the peer
400 address will be stored.
401 \param name_len A pointer to a socklen_t variable that specifies the
402 length of the address structure.
403 On return, it will contain the actual size of the
404 address returned.
405
406 \retval 0 On success.
407 \retval -1 On error, sets errno as appropriate, such as:
408 - EBADF: The socket argument is not a valid file
409 descriptor.
410 - EFAULT: The addr argument points to memory not in a
411 valid part of the process address space.
412 - ENOBUFS: Insufficient resources were available in the
413 system to perform the operation.
414 - ENOTCONN: The socket is not connected.
415 - ENOTSOCK: The socket argument does not refer to a
416 socket.
417 - EOPNOTSUPP: The socket does not support getpeername.
418*/
419int getpeername(int socket, struct sockaddr *__RESTRICT name,
420 socklen_t *__RESTRICT name_len);
421
422/** \brief Get socket options.
423
424 This function retrieves options associated with a socket. This function
425 shall attempt to retrieve the specified option (at the specified level) from
426 the given socket.
427
428 \param socket The socket to get options for.
429 \param level The protocol level to get options at.
430 \param option_name The option to look up.
431 \param option_value Storage for the value of the option.
432 \param option_len The length of option_value on call, and the real
433 option length (if less than the original value)
434 on return.
435
436 \return Zero on success. -1 on error, and sets errno as
437 appropriate.
438
439 \see so_opts
440 \see ipv4_opts
441 \see ipv6_opts
442 \see udp_opts
443*/
444int getsockopt(int socket, int level, int option_name, void *option_value,
445 socklen_t *option_len);
446
447/** \brief Set socket options.
448
449 This function sets options associated with a socket. This function shall
450 attempt to set the specified option (at the specified level) from the given
451 socket.
452
453 \param socket The socket to set options for.
454 \param level The protocol level to set options at.
455 \param option_name The option to set.
456 \param option_value The value to set for the option.
457 \param option_len The length of option_value in bytes.
458
459 \return Zero on success. -1 on error, and sets errno as
460 appropriate.
461
462 \see so_opts
463 \see ipv4_opts
464 \see ipv6_opts
465 \see udp_opts
466*/
467int setsockopt(int socket, int level, int option_name, const void *option_value,
468 socklen_t option_len);
469
470/** @} */
471
472__END_DECLS
473
474#endif /* __SYS_SOCKET_H */
int shutdown(int socket, int how)
Shutdown socket send and receive operations.
ssize_t recv(int socket, void *buffer, size_t length, int flags)
Receive a message on a connected socket.
#define _SS_PAD2SIZE
Second padding size used within struct sockaddr_storage.
Definition socket.h:69
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 send(int socket, const void *message, size_t length, int flags)
Send a message on a connected socket.
__uint8_t sa_family_t
Socket address family type.
Definition socket.h:42
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.
#define _SS_PAD1SIZE
First padding size used within struct sockaddr_storage.
Definition socket.h:66
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.
#define __RESTRICT
Definition cdefs.h:176
Socket address structure of appropriate size to hold any supported socket type's addresses.
Definition socket.h:76
sa_family_t ss_family
Address family.
Definition socket.h:78
__uint64_t _ss_align
Used to force alignment.
Definition socket.h:84
char _ss_pad2[_SS_PAD2SIZE]
Second padding field to fill up the space required.
Definition socket.h:87
char _ss_pad1[_SS_PAD1SIZE]
First padding field.
Definition socket.h:81
Socket address structure.
Definition socket.h:47
char sa_data[]
Address data.
Definition socket.h:51
sa_family_t sa_family
Address family.
Definition socket.h:49
Header for terminal control operations.