2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 /** \file net.h exported symbols from net.c */
9 * The buffer size of the sun_path component of struct sockaddr_un.
11 * While glibc doesn't define \p UNIX_PATH_MAX, it documents it has being
12 * limited to 108 bytes. On NetBSD it is only 104 bytes though. We trust \p
13 * UNIX_PATH_MAX if it is defined and use the size of the ->sun_path member
14 * otherwise. This should be safe everywhere.
17 #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path))
20 /* Userland defines for Linux DCCP support. */
23 #define IPPROTO_DCCP 33 /**< IANA assigned value. */
27 #define SOCK_DCCP 6 /**< Linux socket type. */
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
36 #define SOL_DCCP 269 /**< Linux socket level. */
39 #ifndef DCCP_SOCKOPT_GET_CUR_MPS
40 #define DCCP_SOCKOPT_GET_CUR_MPS 5 /**< Max packet size, RFC 4340, 14. */
43 #ifndef DCCP_SOCKOPT_AVAILABLE_CCIDS
44 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 /**< List of supported CCIDs. */
47 #ifndef DCCP_SOCKOPT_CCID
48 #define DCCP_SOCKOPT_CCID 13 /**< Sets both TX/RX CCID. */
51 #ifndef DCCP_SOCKOPT_TX_CCID
52 #define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */
56 * Flowopts: Transport-layer independent encapsulation of socket options
57 * that need to be registered prior to setting up a connection.
61 extern struct flowopts *flowopt_new(void);
62 extern void flowopt_add(struct flowopts *fo, int level, int opt,
63 const char *name, const void *val, int len);
64 void flowopt_cleanup(struct flowopts *fo);
65 /** Flowopt shortcut macros */
66 #define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len)
69 * Functions to parse and validate (parts of) URLs.
71 extern char *parse_cidr(const char *cidr,
72 char *addr, ssize_t addrlen, int32_t *netmask);
73 extern char *parse_url(const char *url,
74 char *host, ssize_t hostlen, int32_t *port);
75 extern const char *stringify_port(int port, const char *transport);
77 * Ensure that string conforms to the IPv4 address format.
79 * \param address The address string to check.
81 * \return 1 if \a address conforms to the IPv4 address format, else 0.
83 _static_inline_ bool is_valid_ipv4_address(const char *address)
85 struct in_addr test_it;
87 return inet_pton(AF_INET, address, &test_it) != 0;
91 * Ensure that string conforms to IPv6 address format.
93 * \param address The address string to check.
95 * \return 1 if string has a valid IPv6 address syntax, 0 if not.
98 _static_inline_ bool is_valid_ipv6_address(const char *address)
100 struct in6_addr test_it;
102 return inet_pton(AF_INET6, address, &test_it) != 0;
105 int lookup_address(unsigned l4type, bool passive, const char *host,
106 int port_number, struct addrinfo **result);
109 * Generic socket creation (passive and active sockets).
111 int makesock(unsigned l4type, bool passive, const char *host,
112 uint16_t port_number, struct flowopts *fo);
114 int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai,
115 struct flowopts *fo);
117 static inline int para_connect_simple(unsigned l4type,
118 const char *host, uint16_t port)
120 return makesock(l4type, 0, host, port, NULL);
123 void extract_v4_addr(const struct sockaddr_storage *ss, struct in_addr *ia);
124 bool sockaddr_equal(const struct sockaddr *sa1, const struct sockaddr *sa2);
127 * Functions to support listening sockets.
129 /** How many pending connections queue of a listening server will hold. */
131 extern int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo);
133 static inline int para_listen_simple(unsigned l4type, uint16_t port)
135 return para_listen(l4type, port, NULL);
138 /** Pretty-printing of IPv4/6 socket addresses */
139 extern char *remote_name(int sockfd);
142 * Determining maximum payload (packet) size
144 extern int generic_max_transport_msg_size(int sockfd);
146 int recv_bin_buffer(int fd, char *buf, size_t size);
147 int recv_buffer(int fd, char *buf, size_t size);
149 int para_accept(int fd, fd_set *rfds, void *addr, socklen_t size, int *new_fd);
150 int create_local_socket(const char *name, mode_t mode);
151 int connect_local_socket(const char *name);
152 int recv_cred_buffer(int, char *, size_t);
153 ssize_t send_cred_buffer(int, char*);
156 * Functions and definitions to support \p IPPROTO_DCCP
158 /** Estimated worst-case length of a DCCP header including options. */
159 #define DCCP_MAX_HEADER 128
160 /** Hardcoded maximum number of separate CCID modules compiled into a host. */
161 #define DCCP_MAX_HOST_CCIDS 20
162 extern int dccp_available_ccids(uint8_t **ccid_array);