KallistiOS  2.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
socket.h
Go to the documentation of this file.
1 /* KallistiOS 2.0.0
2 
3  sys/socket.h
4  Copyright (C)2006, 2010, 2012 Lawrence Sebald
5 
6 */
7 
8 /** \file sys/socket.h
9  \brief Main sockets header.
10 
11  This file contains the standard definitions (as directed by the POSIX 2008
12  standard) for socket-related functionality in the AF_INET address family.
13  This does not include anything related to AF_INET6 (as IPv6 is not currently
14  implemented in KOS) nor UNIX domain sockets, and is not guaranteed to have
15  everything that one might have in a fully-standard compliant implementation
16  of the POSIX standard.
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 
27 __BEGIN_DECLS
28 
29 /** \brief Socket length type. */
30 typedef __uint32_t socklen_t;
31 
32 /** \brief Socket address family type. */
33 typedef __uint8_t sa_family_t;
34 
35 /** \brief Socket address structure.
36  \headerfile sys/socket.h
37 */
38 struct sockaddr {
39  /** \brief Address family. */
41  /** \brief Address data. */
42  char sa_data[];
43 };
44 
45 /** \brief Size of the struct sockaddr_storage.
46  The size here is chosen for compatibility with anything that may expect the
47  struct sockaddr_storage to be of size 128. Technically, since there are no
48  current plans to support AF_UNIX sockets, this could be smaller, but most
49  implementations make it 128 bytes.
50 */
51 #define _SS_MAXSIZE 128
52 
53 /** \brief Desired alignment of struct sockaddr_storage. */
54 #define _SS_ALIGNSIZE (sizeof(__uint64_t))
55 
56 /** \brief First padding size used within struct sockaddr_storage. */
57 #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
58 
59 /** \brief Second padding size used within struct sockaddr_storage. */
60 #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t) + \
61  _SS_PAD1SIZE + _SS_ALIGNSIZE))
62 
63 /** \brief Socket address structure of appropriate size to hold any supported
64  socket type's addresses.
65  \headerfile sys/socket.h
66 */
68  /** \brief Address family. */
70 
71  /** \brief First padding field. */
73 
74  /** \brief Used to force alignment. */
75  __uint64_t _ss_align;
76 
77  /** \brief Second padding field to fill up the space required. */
79 };
80 
81 /** \brief Datagram socket type.
82 
83  This socket type specifies that the socket in question transmits datagrams
84  that may or may not be reliably transmitted. With IP, this implies using UDP
85  as the underlying protocol.
86 */
87 #define SOCK_DGRAM 1
88 
89 /** \brief Stream socket type.
90 
91  This socket type specifies that the socket in question acts like a stream
92  or pipe between the two endpoints. Sockets of this type can be assumed to be
93  reliable -- unless an error is returned, all packets will be received at the
94  other end in the order they are sent. With IP, this implies using TCP as the
95  underlying protocol.
96 */
97 #define SOCK_STREAM 2
98 
99 /** \brief Socket-level option setting.
100 
101  This constant should be used with the setsockopt() or getsockopt() function
102  to represent that options should be accessed at the socket level, not the
103  protocol level.
104 */
105 #define SOL_SOCKET 1
106 
107 /** \defgroup so_opts Socket-level options
108 
109  These are the various socket-level options that can be accessed with the
110  setsockopt() and getsockopt() functions for the SOL_SOCKET level value.
111 
112  Not all of these are currently supported, but they are listed for
113  completeness.
114 
115  \see ipv6_opts
116 
117  @{
118 */
119 #define SO_ACCEPTCONN 1 /**< \brief Socket is accepting connections (get) */
120 #define SO_BROADCAST 2 /**< \brief Support broadcasting (get/set) */
121 #define SO_DEBUG 3 /**< \brief Record debugging info (get/set) */
122 #define SO_DONTROUTE 4 /**< \brief Do not route packets (get/set) */
123 #define SO_ERROR 5 /**< \brief Retrieve error status (get) */
124 #define SO_KEEPALIVE 6 /**< \brief Send keepalive messages (get/set) */
125 #define SO_LINGER 7 /**< \brief Socket lingers on close (get/set) */
126 #define SO_OOBINLINE 8 /**< \brief OOB data is inline (get/set) */
127 #define SO_RCVBUF 9 /**< \brief Receive buffer size (get/set) */
128 #define SO_RCVLOWAT 10 /**< \brief Receive low-water mark (get/set) */
129 #define SO_RCVTIMEO 11 /**< \brief Receive timeout value (get/set) */
130 #define SO_REUSEADDR 12 /**< \brief Reuse local addresses (get/set) */
131 #define SO_SNDBUF 13 /**< \brief Send buffer size (get/set) */
132 #define SO_SNDLOWAT 14 /**< \brief Send low-water mark (get/set) */
133 #define SO_SNDTIMEO 15 /**< \brief Send timeout value (get/set) */
134 #define SO_TYPE 16 /**< \brief Socket type (get) */
135 /** @} */
136 
137 /** \defgroup msg_flags Socket message flags
138 
139  The following flags can be used with the recv(), recvfrom(), send(),
140  and sendto() functions as the flags parameter.
141 
142  Note that not all of these are currently supported, but they are listed for
143  completeness. Those that are unsupported have (U) at the end of their
144  description. Also, for the time being, the supported flags are only
145  supported for TCP.
146 
147  @{
148 */
149 #define MSG_CTRUNC 0x01 /**< \brief Control data truncated (U) */
150 #define MSG_DONTROUTE 0x02 /**< \brief Send without routing (U) */
151 #define MSG_EOR 0x04 /**< \brief Terminate a record (U) */
152 #define MSG_OOB 0x08 /**< \brief Out-of-band data (U) */
153 #define MSG_PEEK 0x10 /**< \brief Leave received data in queue */
154 #define MSG_TRUNC 0x20 /**< \brief Normal data truncated (U) */
155 #define MSG_WAITALL 0x40 /**< \brief Attempt to fill read buffer */
156 #define MSG_DONTWAIT 0x80 /**< \brief Make this call non-blocking (non-standard) */
157 /** @} */
158 
159 /** \brief Internet domain sockets for use with IPv4 addresses. */
160 #define AF_INET 1
161 
162 /** \brief Internet domain sockets for use with IPv6 addresses. */
163 #define AF_INET6 2
164 
165 /** \brief Protocol family for Internet domain sockets (IPv4). */
166 #define PF_INET AF_INET
167 
168 /** \brief Protocol family for Internet domain sockets (IPv6). */
169 #define PF_INET6 AF_INET6
170 
171 /** \brief Disable furhter receive operations. */
172 #define SHUT_RD 0x00000001
173 
174 /** \brief Disable further send operations. */
175 #define SHUT_WR 0x00000002
176 
177 /** \brief Disable further send and receive operations. */
178 #define SHUT_RDWR (SHUT_RD | SHUT_WR)
179 
180 /** \brief Maximum backlog for a listening socket. */
181 #define SOMAXCONN 32
182 
183 /** \brief Accept a new connection on a socket.
184 
185  This function extracts the first connection on the queue of connections of
186  the specified socket, creating a new socket with the same protocol and
187  address family as that socket for communication with the extracted
188  connection.
189 
190  \param socket A socket created with socket() that has been bound to an
191  address with bind() and is listening for connections
192  after a call to listen().
193  \param address A pointer to a sockaddr structure where the address of
194  the connecting socket will be returned (can be NULL).
195  \param address_len A pointer to a socklen_t which specifies the amount of
196  space in address on input, and the amount used of the
197  space on output.
198  \return On success, the non-negative file descriptor of the
199  new connection, otherwise -1 and errno will be set to
200  the appropriate error value.
201 */
202 int accept(int socket, struct sockaddr *address, socklen_t *address_len);
203 
204 /** \brief Bind a name to a socket.
205 
206  This function assigns the socket to a unique name (address).
207 
208  \param socket A socket that is to be bound.
209  \param address A pointer to a sockaddr structure where the name to be
210  assigned to the socket resides.
211  \param address_len The length of the address structure.
212  \retval 0 On success.
213  \retval -1 On error, sets errno as appropriate.
214 */
215 int bind(int socket, const struct sockaddr *address, socklen_t address_len);
216 
217 /** \brief Connect a socket.
218 
219  This function attempts to make a connection to a resource on a connection-
220  mode socket, or sets/resets the peer address on a connectionless one.
221 
222  \param socket A socket that is to be connected.
223  \param address A pointer to a sockaddr structure where the name of the
224  peer resides.
225  \param address_len The length of the address structure.
226  \retval 0 On success.
227  \retval -1 On error, sets errno as appropriate.
228 */
229 int connect(int socket, const struct sockaddr *address, socklen_t address_len);
230 
231 /** \brief Listen for socket connections and set the queue length.
232 
233  This function marks a connection-mode socket for incoming connections.
234 
235  \param socket A connection-mode socket to listen on.
236  \param backlog The number of queue entries.
237  \retval 0 On success.
238  \retval -1 On error, sets errno as appropriate.
239 */
240 int listen(int socket, int backlog);
241 
242 /** \brief Receive a message on a connected socket.
243 
244  This function receives messages from the peer on a connected socket.
245 
246  \param socket The socket to receive on.
247  \param buffer A pointer to a buffer to store the message in.
248  \param length The length of the buffer.
249  \param flags The type of message reception. Set to 0 for now.
250  \return On success, the length of the message in bytes. If no
251  messages are available, and the socket has been shut
252  down, 0. On error, -1, and sets errno as appropriate.
253 */
254 ssize_t recv(int socket, void *buffer, size_t length, int flags);
255 
256 /** \brief Receive a message on a socket.
257 
258  This function receives messages from a peer on a (usually connectionless)
259  socket.
260 
261  \param socket The socket to receive on.
262  \param buffer A pointer to a buffer to store the message in.
263  \param length The length of the buffer.
264  \param flags The type of message reception. Set to 0 for now.
265  \param address A pointer to a sockaddr structure to store the peer's
266  name in.
267  \param address_len A pointer to the length of the address structure on
268  input, the number of bytes used on output.
269  \return On success, the length of the message in bytes. If no
270  messages are available, and the socket has been shut
271  down, 0. On error, -1, and sets errno as appropriate.
272 */
273 ssize_t recvfrom(int socket, void *buffer, size_t length, int flags,
274  struct sockaddr *address, socklen_t *address_len);
275 
276 /** \brief Send a message on a connected socket.
277 
278  This function sends messages to the peer on a connected socket.
279 
280  \param socket The socket to send on.
281  \param message A pointer to a buffer with the message to send.
282  \param length The length of the message.
283  \param flags The type of message transmission. Set to 0 for now.
284  \return On success, the number of bytes sent. On error, -1,
285  and sets errno as appropriate.
286 */
287 ssize_t send(int socket, const void *message, size_t length, int flags);
288 
289 /** \brief Send a message on a socket.
290 
291  This function sends messages to the peer on a (usually connectionless)
292  socket. If used on a connection-mode socket, this function may change the
293  peer that the socket is connected to, or it may simply return error.
294 
295  \param socket The socket to send on.
296  \param message A pointer to a buffer with the message to send.
297  \param length The length of the message.
298  \param flags The type of message transmission. Set to 0 for now.
299  \param dest_addr A pointer to a sockaddr structure with the peer's name.
300  \param dest_len The length of dest_addr, in bytes.
301  \return On success, the number of bytes sent. On error, -1,
302  and sets errno as appropriate.
303 */
304 ssize_t sendto(int socket, const void *message, size_t length, int flags,
305  const struct sockaddr *dest_addr, socklen_t dest_len);
306 
307 /** \brief Shutdown socket send and receive operations.
308 
309  This function closes a specific socket for the set of specified operations.
310 
311  \param socket The socket to shutdown.
312  \param how The type of shutdown.
313  \retval 0 On success.
314  \retval -1 On error, sets errno as appropriate.
315  \see SHUT_RD
316  \see SHUT_WR
317  \see SHUT_RDWR
318 */
319 int shutdown(int socket, int how);
320 
321 /** \brief Create an endpoint for communications.
322 
323  This function creates an unbound socket for communications with the
324  specified parameters.
325 
326  \param domain The domain to create the socket in (i.e, AF_INET).
327  \param type The type of socket to be created (i.e, SOCK_DGRAM).
328  \param protocol The protocol to use with the socket. May be 0 to allow
329  a default to be used.
330  \return A non-negative file descriptor on success. -1 on error,
331  and sets errno as appropriate.
332 */
333 int socket(int domain, int type, int protocol);
334 
335 /** \brief Get socket options.
336 
337  This function retrieves options associated with a socket. This function
338  shall attempt to retrieve the specified option (at the specified level) from
339  the given socket.
340 
341  \param socket The socket to get options for.
342  \param level The protocol level to get options at.
343  \param option_name The option to look up.
344  \param option_value Storage for the value of the option.
345  \param option_len The length of option_value on call, and the real
346  option length (if less than the original value)
347  on return.
348  \return Zero on success. -1 on error, and sets errno as
349  appropriate.
350 
351  \see so_opts
352 */
353 int getsockopt(int socket, int level, int option_name, void *option_value,
354  socklen_t *option_len);
355 
356 /** \brief Set socket options.
357 
358  This function sets options associated with a socket. This function shall
359  attempt to set the specified option (at the specified level) from the given
360  socket.
361 
362  \param socket The socket to set options for.
363  \param level The protocol level to set options at.
364  \param option_name The option to set.
365  \param option_value The value to set for the option.
366  \param option_len The length of option_value in bytes.
367  \return Zero on success. -1 on error, and sets errno as
368  appropriate.
369 
370  \see so_opts
371 */
372 int setsockopt(int socket, int level, int option_name, const void *option_value,
373  socklen_t option_len);
374 
375 __END_DECLS
376 
377 #endif /* __SYS_SOCKET_H */