1f2ebc6b011aec8b0b888fc71c2a92ae9804b1fb
[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 #endif
26 /** \endcond */
27
28
29 /**
30  * Functions to parse and validate (parts of) URLs.
31  */
32 extern char *parse_cidr(const char *cidr,
33                         char *addr, ssize_t addrlen, int32_t *netmask);
34 extern char *parse_url(const char *url,
35                        char *host, ssize_t hostlen, int32_t *port);
36 extern const char *stringify_port(int port, const char *transport);
37 /**
38  * Ensure that string conforms to the IPv4 address format.
39  *
40  * \param address The address string to check.
41  *
42  * \return 1 if \a address conforms to the IPv4 address format, else 0.
43  */
44 _static_inline_ bool is_valid_ipv4_address(const char *address)
45 {
46         struct in_addr test_it;
47
48         return inet_pton(AF_INET, address, &test_it) != 0;
49 }
50
51 /**
52  * Ensure that string conforms to IPv6 address format.
53  *
54  * \param address The address string to check.
55  *
56  * \return 1 if string has a valid IPv6 address syntax, 0 if not.
57  * \sa RFC 4291
58  */
59 _static_inline_ bool is_valid_ipv6_address(const char *address)
60 {
61         struct in6_addr test_it;
62
63         return inet_pton(AF_INET6, address, &test_it) != 0;
64 }
65
66 /**
67  * Generic socket creation (passive and active sockets).
68  */
69 extern int makesock(unsigned l3type, unsigned l4type, int passive,
70                     const char *host, unsigned short port_number);
71 extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
72
73 /**
74  * Functions to support listening sockets.
75  */
76 /** How many pending connections queue of a listening server will hold. */
77 #define BACKLOG 10
78 extern int para_listen(unsigned l3type, unsigned l4type, unsigned short port);
79
80 /** Pretty-printing of IPv4/6 socket addresses */
81 extern char *local_name(int sockfd);
82 extern char *remote_name(int sockfd);
83
84 int send_bin_buffer(int, const char *, size_t);
85 int send_buffer(int, const char *);
86 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...);
87
88 int recv_bin_buffer(int fd, char *buf, size_t size);
89 int recv_buffer(int fd, char *buf, size_t size);
90
91 int para_accept(int, void *addr, socklen_t size);
92 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
93         mode_t mode);
94 int create_remote_socket(const char *name);
95 int recv_cred_buffer(int, char *, size_t);
96 ssize_t send_cred_buffer(int, char*);
97 int recv_pattern(int fd, const char *pattern, size_t bufsize);