66bf875568eadd9fe07587e31edc5539e472575d
[paraslash.git] / net.h
1 /*
2  * Copyright (C) 2006-2009 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 /** \cond Userland defines for Linux DCCP support. */
21 #ifndef IPPROTO_DCCP
22 #define IPPROTO_DCCP    33      /* IANA assigned value */
23 #define SOCK_DCCP       6       /* Linux socket type   */
24 #define SOL_DCCP        269     /* Linux socket level  */
25 /* Per-connection CCID support (since v2.6.30-rc1) */
26 #define DCCP_SOCKOPT_AVAILABLE_CCIDS    12      /* List of supported CCIDs */
27 #define DCCP_SOCKOPT_CCID               13      /* Sets both TX/RX CCID    */
28 #define DCCP_SOCKOPT_TX_CCID            14      /* Set/get the TX CCID     */
29 #define DCCP_SOCKOPT_RX_CCID            15      /* Set/get the RX CCID     */
30 #endif
31 /** \endcond */
32
33
34 /**
35  * Functions to parse and validate (parts of) URLs.
36  */
37 extern char *parse_cidr(const char *cidr,
38                         char *addr, ssize_t addrlen, int32_t *netmask);
39 extern char *parse_url(const char *url,
40                        char *host, ssize_t hostlen, int32_t *port);
41 extern const char *stringify_port(int port, const char *transport);
42 /**
43  * Ensure that string conforms to the IPv4 address format.
44  *
45  * \param address The address string to check.
46  *
47  * \return 1 if \a address conforms to the IPv4 address format, else 0.
48  */
49 _static_inline_ bool is_valid_ipv4_address(const char *address)
50 {
51         struct in_addr test_it;
52
53         return inet_pton(AF_INET, address, &test_it) != 0;
54 }
55
56 /**
57  * Ensure that string conforms to IPv6 address format.
58  *
59  * \param address The address string to check.
60  *
61  * \return 1 if string has a valid IPv6 address syntax, 0 if not.
62  * \sa RFC 4291
63  */
64 _static_inline_ bool is_valid_ipv6_address(const char *address)
65 {
66         struct in6_addr test_it;
67
68         return inet_pton(AF_INET6, address, &test_it) != 0;
69 }
70
71 /**
72  * Generic socket creation (passive and active sockets).
73  */
74 extern int makesock(unsigned l3type, unsigned l4type, int passive,
75                     const char *host, unsigned short port_number);
76 extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
77
78 /**
79  * Functions to support listening sockets.
80  */
81 /** How many pending connections queue of a listening server will hold. */
82 #define BACKLOG 10
83 extern int para_listen(unsigned l3type, unsigned l4type, unsigned short port);
84
85 /** Pretty-printing of IPv4/6 socket addresses */
86 extern char *local_name(int sockfd);
87 extern char *remote_name(int sockfd);
88
89 int send_bin_buffer(int, const char *, size_t);
90 int send_buffer(int, const char *);
91 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...);
92
93 int recv_bin_buffer(int fd, char *buf, size_t size);
94 int recv_buffer(int fd, char *buf, size_t size);
95
96 int para_accept(int, void *addr, socklen_t size);
97 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
98         mode_t mode);
99 int create_remote_socket(const char *name);
100 int recv_cred_buffer(int, char *, size_t);
101 ssize_t send_cred_buffer(int, char*);
102 int recv_pattern(int fd, const char *pattern, size_t bufsize);
103
104 /**
105  * Functions and definitions to support \p IPPROTO_DCCP
106  */
107 /** Hardcoded maximum number of separate CCID modules compiled into a host */
108 #define DCCP_MAX_HOST_CCIDS     20
109 extern const uint8_t *dccp_available_ccids(uint8_t *ccids, uint8_t *nccids);