Use non-blocking API for signal handling.
[paraslash.git] / net.h
diff --git a/net.h b/net.h
index c4412b2fe1040f26f96b023c168b2aa96f50e3c3..457c24dc92380fca0e8af10c9e63781246c58604 100644 (file)
--- a/net.h
+++ b/net.h
@@ -1,8 +1,9 @@
 /*
- * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
+#include <stdbool.h>
 
 /** \file net.h exported symbols from net.c */
 
 #define UNIX_PATH_MAX 108
 #endif
 
-/** \cond Userland defines for Linux DCCP support. */
+/* Userland defines for Linux DCCP support. */
+
 #ifndef IPPROTO_DCCP
-#define IPPROTO_DCCP   33      /**< IANA assigned value */
-#define SOCK_DCCP      6       /**< Linux socket type   */
-#define SOL_DCCP        269    /**< Linux socket level  */
+#define IPPROTO_DCCP 33 /**< IANA assigned value. */
+#endif
+
+#ifndef SOCK_DCCP
+#define SOCK_DCCP 6 /**< Linux socket type. */
+#endif
+
+#ifndef DCCP_SOCKOPT_RX_CCID
+/** Per-connection CCID support (set/get the RX CCID, since v2.6.30-rc1). */
+#define DCCP_SOCKOPT_RX_CCID 15
+#endif
+
+#ifndef SOL_DCCP
+#define SOL_DCCP 269 /**< Linux socket level. */
+#endif
+
+#ifndef DCCP_SOCKOPT_AVAILABLE_CCIDS
+#define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 /**< List of supported CCIDs. */
+#endif
+
+#ifndef DCCP_SOCKOPT_CCID
+#define DCCP_SOCKOPT_CCID 13 /**< Sets both TX/RX CCID. */
 #endif
-/** \endcond */
+
+#ifndef DCCP_SOCKOPT_TX_CCID
+#define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */
+#endif
+
+/**
+ * Flowopts: Transport-layer independent encapsulation of socket options
+ *           that need to be registered prior to setting up a connection.
+ */
+struct flowopts;
+
+extern struct flowopts *flowopt_new(void);
+extern void flowopt_add(struct flowopts *fo, int level, int opt,
+                       char *name, const void *val, int len);
+extern void flowopt_add_bool(struct flowopts *fo, int lev, int opt,
+                            char *optname, bool on_or_off);
+/** Flowopt shortcut macros */
+#define OPT_ADD(fo, lev, opt, val, len)        flowopt_add(fo, lev, opt, #opt, val, len)
+#define OPT_ENABLE(fo, lev, opt)       flowopt_add_bool(fo, lev, opt, #opt, 1)
+#define OPT_DISABLE(fo, lev, opt)      flowopt_add_bool(fo, lev, opt, #opt, 0)
+
+/**
+ * Functions to parse and validate (parts of) URLs.
+ */
+extern char *parse_cidr(const char *cidr,
+                       char *addr, ssize_t addrlen, int32_t *netmask);
+extern char *parse_url(const char *url,
+                      char *host, ssize_t hostlen, int32_t *port);
+extern const char *stringify_port(int port, const char *transport);
+/**
+ * Ensure that string conforms to the IPv4 address format.
+ *
+ * \param address The address string to check.
+ *
+ * \return 1 if \a address conforms to the IPv4 address format, else 0.
+ */
+_static_inline_ bool is_valid_ipv4_address(const char *address)
+{
+       struct in_addr test_it;
+
+       return inet_pton(AF_INET, address, &test_it) != 0;
+}
+
+/**
+ * Ensure that string conforms to IPv6 address format.
+ *
+ * \param address The address string to check.
+ *
+ * \return 1 if string has a valid IPv6 address syntax, 0 if not.
+ * \sa RFC 4291
+ */
+_static_inline_ bool is_valid_ipv6_address(const char *address)
+{
+       struct in6_addr test_it;
+
+       return inet_pton(AF_INET6, address, &test_it) != 0;
+}
 
 /**
  * Generic socket creation (passive and active sockets).
  */
-extern int makesock(unsigned l3type, unsigned l4type, int passive,
-                   const char *host, unsigned short port_number);
+extern int makesock(unsigned l4type, bool passive,
+                   const char *host, uint16_t port_number,
+                   struct flowopts *fo);
+
+static inline int para_connect_simple(unsigned l4type,
+                                     const char *host, uint16_t port)
+{
+       return makesock(l4type, 0, host, port, NULL);
+}
+
+extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
 
 /**
  * Functions to support listening sockets.
  */
 /** How many pending connections queue of a listening server will hold. */
 #define BACKLOG        10
-extern int para_listen(unsigned l3type, unsigned l4type, unsigned short port);
+extern int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo);
+
+static inline int para_listen_simple(unsigned l4type, uint16_t port)
+{
+       return para_listen(l4type, port, NULL);
+}
 
 /** Pretty-printing of IPv4/6 socket addresses */
-extern char *host_and_port(struct sockaddr *sa, socklen_t len);
 extern char *local_name(int sockfd);
 extern char *remote_name(int sockfd);
 
-/** used to crypt the communication between para_server and para_client */
-typedef void crypt_function(unsigned long len,
-       const unsigned char *indata, unsigned char *outdata, void *private_data);
-
-int send_buffer(int, const char *);
 int send_bin_buffer(int, const char *, size_t);
+int send_buffer(int, const char *);
 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...);
-int recv_buffer(int fd, char *buf, size_t size);
+
 int recv_bin_buffer(int fd, char *buf, size_t size);
-int para_accept(int, void *addr, socklen_t size);
+int recv_buffer(int fd, char *buf, size_t size);
+
+int para_accept(int fd, fd_set *rfds, void *addr, socklen_t size, int *new_fd);
 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
        mode_t mode);
 int create_remote_socket(const char *name);
 int recv_cred_buffer(int, char *, size_t);
 ssize_t send_cred_buffer(int, char*);
-int recv_pattern(int fd, const char *pattern, size_t bufsize);
-void enable_crypt(int fd, crypt_function *recv_f, crypt_function *send_f,
-       void *private_data);
-void disable_crypt(int fd);
+
+/**
+ * Functions and definitions to support \p IPPROTO_DCCP
+ */
+/** Hardcoded maximum number of separate CCID modules compiled into a host */
+#define DCCP_MAX_HOST_CCIDS    20
+extern int dccp_available_ccids(uint8_t **ccid_array);