Make recv_pattern() use the nonblock API.
[paraslash.git] / net.h
1 /*
2 * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 #include <stdbool.h>
7
8 /** \file net.h exported symbols from net.c */
9
10 /**
11 * the buffer size of the sun_path component of struct sockaddr_un
12 *
13 * While glibc doesn't define \p UNIX_PATH_MAX, it
14 * documents it has being limited to 108 bytes.
15 */
16 #ifndef UNIX_PATH_MAX
17 #define UNIX_PATH_MAX 108
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_AVAILABLE_CCIDS
40 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 /**< List of supported CCIDs. */
41 #endif
42
43 #ifndef DCCP_SOCKOPT_CCID
44 #define DCCP_SOCKOPT_CCID 13 /**< Sets both TX/RX CCID. */
45 #endif
46
47 #ifndef DCCP_SOCKOPT_TX_CCID
48 #define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */
49 #endif
50
51 /**
52 * Flowopts: Transport-layer independent encapsulation of socket options
53 * that need to be registered prior to setting up a connection.
54 */
55 struct flowopts;
56
57 extern struct flowopts *flowopt_new(void);
58 extern void flowopt_add(struct flowopts *fo, int level, int opt,
59 char *name, const void *val, int len);
60 extern void flowopt_add_bool(struct flowopts *fo, int lev, int opt,
61 char *optname, bool on_or_off);
62 /** Flowopt shortcut macros */
63 #define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len)
64 #define OPT_ENABLE(fo, lev, opt) flowopt_add_bool(fo, lev, opt, #opt, 1)
65 #define OPT_DISABLE(fo, lev, opt) flowopt_add_bool(fo, lev, opt, #opt, 0)
66
67 /**
68 * Functions to parse and validate (parts of) URLs.
69 */
70 extern char *parse_cidr(const char *cidr,
71 char *addr, ssize_t addrlen, int32_t *netmask);
72 extern char *parse_url(const char *url,
73 char *host, ssize_t hostlen, int32_t *port);
74 extern const char *stringify_port(int port, const char *transport);
75 /**
76 * Ensure that string conforms to the IPv4 address format.
77 *
78 * \param address The address string to check.
79 *
80 * \return 1 if \a address conforms to the IPv4 address format, else 0.
81 */
82 _static_inline_ bool is_valid_ipv4_address(const char *address)
83 {
84 struct in_addr test_it;
85
86 return inet_pton(AF_INET, address, &test_it) != 0;
87 }
88
89 /**
90 * Ensure that string conforms to IPv6 address format.
91 *
92 * \param address The address string to check.
93 *
94 * \return 1 if string has a valid IPv6 address syntax, 0 if not.
95 * \sa RFC 4291
96 */
97 _static_inline_ bool is_valid_ipv6_address(const char *address)
98 {
99 struct in6_addr test_it;
100
101 return inet_pton(AF_INET6, address, &test_it) != 0;
102 }
103
104 /**
105 * Generic socket creation (passive and active sockets).
106 */
107 extern int makesock(unsigned l4type, bool passive,
108 const char *host, uint16_t port_number,
109 struct flowopts *fo);
110
111 static inline int para_connect_simple(unsigned l4type,
112 const char *host, uint16_t port)
113 {
114 return makesock(l4type, 0, host, port, NULL);
115 }
116
117 extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
118
119 /**
120 * Functions to support listening sockets.
121 */
122 /** How many pending connections queue of a listening server will hold. */
123 #define BACKLOG 10
124 extern int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo);
125
126 static inline int para_listen_simple(unsigned l4type, uint16_t port)
127 {
128 return para_listen(l4type, port, NULL);
129 }
130
131 /** Pretty-printing of IPv4/6 socket addresses */
132 extern char *local_name(int sockfd);
133 extern char *remote_name(int sockfd);
134
135 int send_bin_buffer(int, const char *, size_t);
136 int send_buffer(int, const char *);
137 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...);
138
139 int recv_bin_buffer(int fd, char *buf, size_t size);
140 int recv_buffer(int fd, char *buf, size_t size);
141
142 int para_accept(int, void *addr, socklen_t size);
143 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
144 mode_t mode);
145 int create_remote_socket(const char *name);
146 int recv_cred_buffer(int, char *, size_t);
147 ssize_t send_cred_buffer(int, char*);
148
149 /**
150 * Functions and definitions to support \p IPPROTO_DCCP
151 */
152 /** Hardcoded maximum number of separate CCID modules compiled into a host */
153 #define DCCP_MAX_HOST_CCIDS 20
154 extern int dccp_available_ccids(uint8_t **ccid_array);