Merge branch 't/mood_fix'
[paraslash.git] / net.h
1 /*
2 * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file net.h exported symbols from net.c */
7
8 /**
9 * The buffer size of the sun_path component of struct sockaddr_un.
10 *
11 * While glibc doesn't define \p UNIX_PATH_MAX, it documents it has being
12 * limited to 108 bytes. On NetBSD it is only 104 bytes though. We trust \p
13 * UNIX_PATH_MAX if it is defined and use the size of the ->sun_path member
14 * otherwise. This should be safe everywhere.
15 */
16 #ifndef UNIX_PATH_MAX
17 #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path))
18 #endif
19
20 /* Userland defines for Linux DCCP support. */
21
22 #ifndef IPPROTO_DCCP
23 #define IPPROTO_DCCP 33 /**< IANA assigned value. */
24 #endif
25
26 #ifndef SOCK_DCCP
27 #define SOCK_DCCP 6 /**< Linux socket type. */
28 #endif
29
30 #ifndef DCCP_SOCKOPT_RX_CCID
31 /** Per-connection CCID support (set/get the RX CCID, since v2.6.30-rc1). */
32 #define DCCP_SOCKOPT_RX_CCID 15
33 #endif
34
35 #ifndef SOL_DCCP
36 #define SOL_DCCP 269 /**< Linux socket level. */
37 #endif
38
39 #ifndef DCCP_SOCKOPT_GET_CUR_MPS
40 #define DCCP_SOCKOPT_GET_CUR_MPS 5 /**< Max packet size, RFC 4340, 14. */
41 #endif
42
43 #ifndef DCCP_SOCKOPT_AVAILABLE_CCIDS
44 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 /**< List of supported CCIDs. */
45 #endif
46
47 #ifndef DCCP_SOCKOPT_CCID
48 #define DCCP_SOCKOPT_CCID 13 /**< Sets both TX/RX CCID. */
49 #endif
50
51 #ifndef DCCP_SOCKOPT_TX_CCID
52 #define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */
53 #endif
54
55 /**
56 * Flowopts: Transport-layer independent encapsulation of socket options
57 * that need to be registered prior to setting up a connection.
58 */
59 struct flowopts;
60
61 extern struct flowopts *flowopt_new(void);
62 extern void flowopt_add(struct flowopts *fo, int level, int opt,
63 const char *name, const void *val, int len);
64 extern void flowopt_add_bool(struct flowopts *fo, int lev, int opt,
65 const char *optname, bool on_or_off);
66 /** Flowopt shortcut macros */
67 #define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len)
68 #define OPT_ENABLE(fo, lev, opt) flowopt_add_bool(fo, lev, opt, #opt, 1)
69 #define OPT_DISABLE(fo, lev, opt) flowopt_add_bool(fo, lev, opt, #opt, 0)
70
71 /**
72 * Functions to parse and validate (parts of) URLs.
73 */
74 extern char *parse_cidr(const char *cidr,
75 char *addr, ssize_t addrlen, int32_t *netmask);
76 extern char *parse_url(const char *url,
77 char *host, ssize_t hostlen, int32_t *port);
78 extern const char *stringify_port(int port, const char *transport);
79 /**
80 * Ensure that string conforms to the IPv4 address format.
81 *
82 * \param address The address string to check.
83 *
84 * \return 1 if \a address conforms to the IPv4 address format, else 0.
85 */
86 _static_inline_ bool is_valid_ipv4_address(const char *address)
87 {
88 struct in_addr test_it;
89
90 return inet_pton(AF_INET, address, &test_it) != 0;
91 }
92
93 /**
94 * Ensure that string conforms to IPv6 address format.
95 *
96 * \param address The address string to check.
97 *
98 * \return 1 if string has a valid IPv6 address syntax, 0 if not.
99 * \sa RFC 4291
100 */
101 _static_inline_ bool is_valid_ipv6_address(const char *address)
102 {
103 struct in6_addr test_it;
104
105 return inet_pton(AF_INET6, address, &test_it) != 0;
106 }
107
108 /**
109 * Generic socket creation (passive and active sockets).
110 */
111 extern int makesock(unsigned l4type, bool passive,
112 const char *host, uint16_t port_number,
113 struct flowopts *fo);
114
115 static inline int para_connect_simple(unsigned l4type,
116 const char *host, uint16_t port)
117 {
118 return makesock(l4type, 0, host, port, NULL);
119 }
120
121 extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
122
123 /**
124 * Functions to support listening sockets.
125 */
126 /** How many pending connections queue of a listening server will hold. */
127 #define BACKLOG 10
128 extern int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo);
129
130 static inline int para_listen_simple(unsigned l4type, uint16_t port)
131 {
132 return para_listen(l4type, port, NULL);
133 }
134
135 /** Pretty-printing of IPv4/6 socket addresses */
136 extern char *local_name(int sockfd);
137 extern char *remote_name(int sockfd);
138
139 /**
140 * Determining maximum payload (packet) size
141 */
142 extern int generic_max_transport_msg_size(int sockfd);
143
144 int recv_bin_buffer(int fd, char *buf, size_t size);
145 int recv_buffer(int fd, char *buf, size_t size);
146
147 int para_accept(int fd, fd_set *rfds, void *addr, socklen_t size, int *new_fd);
148 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
149 mode_t mode);
150 int connect_local_socket(const char *name);
151 int recv_cred_buffer(int, char *, size_t);
152 ssize_t send_cred_buffer(int, char*);
153
154 /**
155 * Functions and definitions to support \p IPPROTO_DCCP
156 */
157 /** Estimated worst-case length of a DCCP header including options. */
158 #define DCCP_MAX_HEADER 128
159 /** Hardcoded maximum number of separate CCID modules compiled into a host. */
160 #define DCCP_MAX_HOST_CCIDS 20
161 extern int dccp_available_ccids(uint8_t **ccid_array);