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