socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-15 Freie Universit├Ąt Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
29 #ifndef SYS_SOCKET_H
30 #define SYS_SOCKET_H
31 
32 #ifdef CPU_NATIVE
33 /* Ignore Linux definitions in native */
34 #define _BITS_SOCKADDR_H 1
35 #define __SOCKADDR_COMMON(sa_prefix) \
36  sa_family_t sa_prefix##family
37 
38 #define __SOCKADDR_COMMON_SIZE (sizeof (unsigned short int))
39 #endif
40 
41 #include <stdalign.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <sys/uio.h>
45 
46 #include "architecture.h"
47 #include "net/af.h"
48 #include "sys/bytes.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
57 #ifndef SOCKET_POOL_SIZE
58 #ifdef MODULE_SOCK_TCP
59 #define SOCKET_POOL_SIZE (6) /* define enough for accepted sockets */
60 #else
61 #define SOCKET_POOL_SIZE (4)
62 #endif
63 #endif
64 
69 #ifndef SOCKET_TCP_QUEUE_SIZE
70 #ifdef MODULE_SOCK_TCP
71 #define SOCKET_TCP_QUEUE_SIZE (2)
72 #else
73 #define SOCKET_TCP_QUEUE_SIZE (0)
74 #endif
75 #endif
76 
84 #define SOCKADDR_MAX_DATA_LEN (26)
85 
90 #define SOCK_DGRAM (1)
91 #define SOCK_RAW (2)
92 #define SOCK_SEQPACKET (3)
93 #define SOCK_STREAM (4)
96 #define SOL_SOCKET (-1)
103 #define SO_ACCEPTCONN (0)
104 #define SO_BROADCAST (1)
105 #define SO_DEBUG (2)
106 #define SO_DONTROUTE (3)
107 #define SO_ERROR (4)
108 #define SO_KEEPALIVE (5)
109 #define SO_LINGER (6)
110 #define SO_OOBINLINE (7)
111 #define SO_RCVBUF (8)
112 #define SO_RCVLOWAT (9)
113 #define SO_RCVTIMEO (10)
114 #define SO_REUSEADDR (11)
115 #define SO_SNDBUF (12)
116 #define SO_SNDLOWAT (13)
117 #define SO_SNDTIMEO (14)
118 #define SO_TYPE (15)
121 typedef unsigned short sa_family_t;
129 struct sockaddr {
130  alignas(uint32_t) sa_family_t sa_family;
132 };
133 
142  alignas(uint32_t) sa_family_t ss_family;
144 };
145 
191 int accept(int socket, struct sockaddr *__restrict address,
192  socklen_t *__restrict address_len);
193 
218 int bind(int socket, const struct sockaddr *address,
219  socklen_t address_len);
220 
248 int connect(int socket, const struct sockaddr *address, socklen_t address_len);
249 
273 int getpeername(int socket, struct sockaddr *__restrict address,
274  socklen_t *__restrict address_len);
275 
299 int getsockname(int socket, struct sockaddr *__restrict address,
300  socklen_t *__restrict address_len);
301 
323 int listen(int socket, int backlog);
324 
359 ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags,
360  struct sockaddr *__restrict address,
361  socklen_t *__restrict address_len);
362 
386 static inline ssize_t recv(int socket, void *buffer, size_t length, int flags)
387 {
388  return recvfrom(socket, buffer, length, flags, NULL, NULL);
389 }
390 
432 ssize_t sendto(int socket, const void *buffer, size_t length, int flags,
433  const struct sockaddr *address, socklen_t address_len);
434 
456 static inline ssize_t send(int socket, const void *buffer, size_t length,
457  int flags)
458 {
459  return sendto(socket, buffer, length, flags, NULL, 0);
460 }
461 
483 int socket(int domain, int type, int protocol);
484 
489 static inline int getsockopt(int socket, int level, int option_name, void *option_value,
490  socklen_t *option_len)
491 {
492  (void)socket;
493  (void)level;
494  (void)option_name;
495  (void)option_value;
496  (void)option_len;
497  return -1;
498 }
499 
500 int setsockopt(int socket, int level, int option_name, const void *option_value,
501  socklen_t option_len);
502 
505 #ifdef __cplusplus
506 }
507 #endif
508 
512 #endif /* SYS_SOCKET_H */
Global UNIX address family definitions.
Platform-independent access to architecture details.
System-internal byte operations.
size_t socklen_t
socket address length
Definition: bytes.h:31
unsigned short sa_family_t
address family type
Definition: socket.h:121
static ssize_t send(int socket, const void *buffer, size_t length, int flags)
Send a message on a socket.
Definition: socket.h:456
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
static ssize_t recv(int socket, void *buffer, size_t length, int flags)
Receive a message from a connected socket.
Definition: socket.h:386
int accept(int socket, struct sockaddr *__restrict address, socklen_t *__restrict address_len)
Accept a new connection on a socket.
int getpeername(int socket, struct sockaddr *__restrict address, socklen_t *__restrict address_len)
Get the name of the peer socket.
int listen(int socket, int backlog)
Listen for socket connections and limit the queue of incoming connections.
int getsockname(int socket, struct sockaddr *__restrict address, socklen_t *__restrict address_len)
Get the socket name.
static int getsockopt(int socket, int level, int option_name, void *option_value, socklen_t *option_len)
Definition: socket.h:489
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 communication.
#define SOCKADDR_MAX_DATA_LEN
Maximum data length for a socket address.
Definition: socket.h:84
ssize_t recvfrom(int socket, void *__restrict buffer, size_t length, int flags, struct sockaddr *__restrict address, socklen_t *__restrict address_len)
Receive a message from a socket.
ssize_t sendto(int socket, const void *buffer, size_t length, int flags, const struct sockaddr *address, socklen_t address_len)
Send a message on a socket.
Implementation based socket address table.
Definition: socket.h:141
sa_family_t ss_family
Address family.
Definition: socket.h:142
uint8_t ss_data[SOCKADDR_MAX_DATA_LEN]
Socket address.
Definition: socket.h:143
Used to define the socket address.
Definition: socket.h:129
char sa_data[SOCKADDR_MAX_DATA_LEN]
Socket address (variable length data)
Definition: socket.h:131
sa_family_t sa_family
Address family.
Definition: socket.h:130
libc header for scatter/gather I/O