]> git.tuebingen.mpg.de Git - paraslash.git/blob - net.h
net: Combine documentation of struct flowopts.
[paraslash.git] / net.h
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2 /** \file net.h exported symbols from net.c */
3
4 /**
5  * The buffer size of the sun_path component of struct sockaddr_un.
6  *
7  * While glibc doesn't define \p UNIX_PATH_MAX, it documents it has being
8  * limited to 108 bytes. On NetBSD it is only 104 bytes though. We trust \p
9  * UNIX_PATH_MAX if it is defined and use the size of the ->sun_path member
10  * otherwise. This should be safe everywhere.
11  */
12 #ifndef UNIX_PATH_MAX
13 #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path))
14 #endif
15
16 /* Userland defines for Linux DCCP support. */
17
18 #ifndef IPPROTO_DCCP
19 #define IPPROTO_DCCP 33 /**< IANA assigned value. */
20 #endif
21
22 #ifndef SOCK_DCCP
23 #define SOCK_DCCP 6 /**< Linux socket type. */
24 #endif
25
26 #ifndef DCCP_SOCKOPT_RX_CCID
27 /** Per-connection CCID support (set/get the RX CCID, since v2.6.30-rc1). */
28 #define DCCP_SOCKOPT_RX_CCID 15
29 #endif
30
31 #ifndef SOL_DCCP
32 #define SOL_DCCP 269 /**< Linux socket level. */
33 #endif
34
35 #ifndef DCCP_SOCKOPT_GET_CUR_MPS
36 #define DCCP_SOCKOPT_GET_CUR_MPS  5 /**< Max packet size, RFC 4340, 14. */
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 /** The maximum length of the host component in an URL. */
52 #define MAX_HOSTLEN 256
53
54 /* Opaque, only known to net.c. */
55 struct flowopts;
56
57 struct flowopts *flowopt_new(void);
58 void flowopt_add(struct flowopts *fo, int level, int opt,
59                 const char *name, const void *val, int len);
60 void flowopt_cleanup(struct flowopts *fo);
61 /** Flowopt shortcut macros */
62 #define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len)
63
64 /**
65  * Functions to parse and validate (parts of) URLs.
66  */
67 char *parse_cidr(const char *cidr,
68                 char *addr, ssize_t addrlen, int32_t *netmask);
69 char *parse_url(const char *url,
70                 char *host, ssize_t hostlen, int32_t *port);
71 char *format_url(const char *url, int default_port);
72 const char *stringify_port(int port, const char *transport);
73
74 /**
75  * Ensure that string conforms to the IPv4 address format.
76  *
77  * \param address The address string to check.
78  *
79  * \return 1 if \a address conforms to the IPv4 address format, else 0.
80  */
81 _static_inline_ bool is_valid_ipv4_address(const char *address)
82 {
83         struct in_addr test_it;
84
85         return inet_pton(AF_INET, address, &test_it) != 0;
86 }
87
88 /**
89  * Ensure that string conforms to IPv6 address format.
90  *
91  * \param address The address string to check.
92  *
93  * \return 1 if string has a valid IPv6 address syntax, 0 if not.
94  * \sa RFC 4291.
95  */
96 _static_inline_ bool is_valid_ipv6_address(const char *address)
97 {
98         struct in6_addr test_it;
99
100         return inet_pton(AF_INET6, address, &test_it) != 0;
101 }
102
103 int lookup_address(unsigned l4type, bool passive, const char *host,
104                 int port_number, struct addrinfo **result);
105
106 /**
107  * Generic socket creation (passive and active sockets).
108  */
109 int makesock(unsigned l4type, bool passive, const char *host,
110                 uint16_t port_number, struct flowopts *fo);
111
112 int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai,
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 void extract_v4_addr(const struct sockaddr_storage *ss, struct in_addr *ia);
122 bool sockaddr_equal(const struct sockaddr *sa1, const struct sockaddr *sa2);
123
124 /**
125  * Functions to support listening sockets.
126  */
127 /** How many pending connections queue of a listening server will hold. */
128 #define BACKLOG 10
129
130 int para_listen(unsigned l4type, const char *addr, uint16_t port);
131 int para_listen_simple(unsigned l4type, uint16_t port);
132
133 /** Pretty-printing of IPv4/6 socket addresses */
134 char *remote_name(int sockfd);
135
136 /**
137  * Determining maximum payload (packet) size
138  */
139 int generic_max_transport_msg_size(int sockfd);
140
141 int recv_bin_buffer(int fd, char *buf, size_t size);
142 int recv_buffer(int fd, char *buf, size_t size);
143
144 int para_accept(int fd, void *addr, socklen_t size, int *new_fd);
145 int create_local_socket(const char *name);
146 int connect_local_socket(const char *name);
147 int recv_cred_buffer(int, char *, size_t);
148 ssize_t send_cred_buffer(int, char*);
149
150 /**
151  * Functions and definitions to support \p IPPROTO_DCCP
152  */
153 /** Estimated worst-case length of a DCCP header including options. */
154 #define DCCP_MAX_HEADER         128
155 /** Hardcoded maximum number of separate CCID modules compiled into a host. */
156 #define DCCP_MAX_HOST_CCIDS     20
157 int dccp_available_ccids(uint8_t **ccid_array);