From: Andre Noll Date: Mon, 7 Nov 2022 16:34:31 +0000 (+0100) Subject: Merge topic branch t/net into master X-Git-Tag: v0.7.2~20 X-Git-Url: http://git.tuebingen.mpg.de/?a=commitdiff_plain;h=68c0deb1ae25af923fde02952159c1129a281813;hp=be6cdae0466199a60c3e3e749a2b331c762dc6fc;p=paraslash.git Merge topic branch t/net into master A moderately sized series which contains a bunch of simple cleanups for net.c and net.h. * refs/heads/t/net: net: De-doxify static functions. net: Refer to correct man page in stringify_port(). net: Pass true/false instead of 0/1 to makesock(). net: Demote log level of error message in makesock(). net: Rename para_connect_simple() -> para_connect(). net: Make is_valid_ipv{4,6}_address() local to net.c. net: Remove IPPROTO_DCCP define. net: Make single-use macros local. net: Combine documentation of struct flowopts. net: Drop extern keyword of function declarations. --- diff --git a/NEWS.md b/NEWS.md index 5a00175c..c21cc858 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,15 @@ NEWS ==== +------------------------------------------ +0.7.2 (to be announced) "optical friction" +------------------------------------------ + +- Minor cleanup of the net subsystem. + +Downloads: +[tarball](./releases/paraslash-git.tar.xz) + -------------------------------------- 0.7.1 (2022-10-03) "digital spindrift" -------------------------------------- @@ -27,6 +36,7 @@ usual mix of bug fixes and minor improvements not mentioned here. requires support from the compiler, the oldest supported gcc version has been bumped to gcc-5.4 (released in 2015). +Downloads: [tarball](./releases/paraslash-0.7.1.tar.xz), [signature](./releases/paraslash-0.7.1.tar.xz.asc) diff --git a/client_common.c b/client_common.c index 64f6c676..95e59fd2 100644 --- a/client_common.c +++ b/client_common.c @@ -490,7 +490,7 @@ int client_connect(struct client_task *ct, struct sched *s, PARA_NOTICE_LOG("connecting %s:%u\n", host, port); ct->scc.fd = -1; - ret = para_connect_simple(IPPROTO_TCP, host, port); + ret = para_connect(IPPROTO_TCP, host, port); if (ret < 0) return ret; ct->scc.fd = ret; diff --git a/dccp_recv.c b/dccp_recv.c index fe2f7abf..0b20bcc8 100644 --- a/dccp_recv.c +++ b/dccp_recv.c @@ -27,6 +27,10 @@ #include "net.h" #include "fd.h" +#ifndef DCCP_SOCKOPT_CCID +#define DCCP_SOCKOPT_CCID 13 /**< Sets both TX/RX CCID. */ +#endif + static void dccp_recv_close(struct receiver_node *rn) { if (rn->fd > 0) @@ -59,6 +63,9 @@ static int dccp_recv_ccid_support_check(const struct lls_parse_result *lpr) return 1; } +/** Flowopt shortcut */ +#define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len) + static int dccp_recv_open(struct receiver_node *rn) { struct lls_parse_result *lpr = rn->lpr; @@ -83,7 +90,7 @@ static int dccp_recv_open(struct receiver_node *rn) OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i); } - fd = makesock(IPPROTO_DCCP, 0, host, port, fo); + fd = makesock(IPPROTO_DCCP, false, host, port, fo); flowopt_cleanup(fo); free(ccids); if (fd < 0) diff --git a/dccp_send.c b/dccp_send.c index 15a361ba..6182c964 100644 --- a/dccp_send.c +++ b/dccp_send.c @@ -45,6 +45,10 @@ static void dccp_pre_monitor(struct sched *s) sched_monitor_readfd(dss->listen_fds[n], s); } +#ifndef DCCP_SOCKOPT_TX_CCID +#define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */ +#endif + /** * Query the TX CCID used on the sender-client half connection. * \param sockfd Server socket descriptor to query (after accept(2)). @@ -89,6 +93,13 @@ static void dccp_shutdown(void) free_sender_status(dss); } +#ifndef DCCP_SOCKOPT_GET_CUR_MPS +#define DCCP_SOCKOPT_GET_CUR_MPS 5 /**< Max packet size, RFC 4340, 14. */ +#endif + +/** Estimated worst-case length of a DCCP header including options. */ +#define DCCP_MAX_HEADER 128 + /** * Obtain current MPS according to RFC 4340, sec. 14. */ static int dccp_init_fec(struct sender_client *sc) { diff --git a/http_recv.c b/http_recv.c index 5aafacb8..32c9e7b9 100644 --- a/http_recv.c +++ b/http_recv.c @@ -150,7 +150,7 @@ static int http_recv_open(struct receiver_node *rn) struct lls_parse_result *lpr = rn->lpr; const char *r_i = RECV_CMD_OPT_STRING_VAL(HTTP, HOST, lpr); uint32_t r_p = RECV_CMD_OPT_UINT32_VAL(HTTP, PORT, lpr); - int fd, ret = para_connect_simple(IPPROTO_TCP, r_i, r_p); + int fd, ret = para_connect(IPPROTO_TCP, r_i, r_p); if (ret < 0) return ret; diff --git a/net.c b/net.c index e01af24b..a24081f5 100644 --- a/net.c +++ b/net.c @@ -18,6 +18,13 @@ #include "list.h" #include "fd.h" +/* Whether the given address conforms to the IPv4 address format. */ +static inline bool is_valid_ipv4_address(const char *address) +{ + struct in_addr test_it; + return inet_pton(AF_INET, address, &test_it) != 0; +} + /** * Parse and validate IPv4 address/netmask string. * @@ -58,13 +65,6 @@ failed: return NULL; } - -/** - * Match string as a candidate IPv4 address. - * - * \param address The string to match. - * \return True if \a address has "dot-quad" format. - */ static bool is_v4_dot_quad(const char *address) { bool result; @@ -77,6 +77,13 @@ static bool is_v4_dot_quad(const char *address) return result; } +/* Whether a string conforms to IPv6 address format (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; +} + /** * Perform basic syntax checking on the host-part of an URL: * @@ -205,7 +212,7 @@ char *format_url(const char *url, int default_port) * \param transport Transport protocol name (e.g. "udp", "tcp"), or NULL. * \return Pointer to static result buffer. * - * \sa getservent(3), services(5), nsswitch.conf(5). + * \sa getservbyport(3), services(5), nsswitch.conf(5). */ const char *stringify_port(int port, const char *transport) { @@ -224,12 +231,13 @@ const char *stringify_port(int port, const char *transport) return service; } -/** - * Determine the socket type for a given layer-4 protocol. - * - * \param l4type The symbolic name of the transport-layer protocol. - * - * \sa ip(7), socket(2). +#ifndef SOCK_DCCP +#define SOCK_DCCP 6 /**< Linux socket type. */ +#endif + +/* + * Determine the socket type, given the symbolic name of the transport-layer + * protocol. See ip(7), socket(2). */ static inline int sock_type(const unsigned l4type) { @@ -241,9 +249,7 @@ static inline int sock_type(const unsigned l4type) return -1; /* not supported here */ } -/** - * Pretty-print transport-layer name. - */ +/* Pretty-print transport-layer name. */ static const char *layer4_name(const unsigned l4type) { switch (l4type) { @@ -273,7 +279,12 @@ struct pre_conn_opt { struct list_head node; /**< FIFO, as sockopt order matters. */ }; -/** FIFO list of pre-connection socket options to be set */ +/** + * List of pre-connection socket options to be set. + * + * This list contains transport-layer independent encapsulation of socket + * options that need to be registered prior to setting up a connection. + */ struct flowopts { struct list_head sockopts; }; @@ -325,7 +336,7 @@ void flowopt_add(struct flowopts *fo, int lev, int opt, list_add_tail(&new->node, &fo->sockopts); } -/** Set the entire bunch of pre-connection options at once. */ +/* Set the entire bunch of pre-connection options at once. */ static void flowopt_setopts(int sockfd, struct flowopts *fo) { struct pre_conn_opt *pc; @@ -509,7 +520,7 @@ int makesock(unsigned l4type, bool passive, const char *host, uint16_t port_numb if (ai) freeaddrinfo(ai); if (ret < 0) { - PARA_ERROR_LOG("can not create %s socket %s#%d.\n", + PARA_NOTICE_LOG("can not create %s socket %s#%d.\n", layer4_name(l4type), host? host : (passive? "[loopback]" : "[localhost]"), port_number); } @@ -571,11 +582,7 @@ int para_listen_simple(unsigned l4type, uint16_t port) return para_listen(l4type, NULL, port); } -/** - * Determine IPv4/v6 socket address length. - * \param sa Container of IPv4 or IPv6 address. - * \return Address-family dependent address length. - */ +/* Compute the address-family dependent address length of an IPv4/v6 socket. */ static socklen_t salen(const struct sockaddr *sa) { assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6); @@ -585,7 +592,7 @@ static socklen_t salen(const struct sockaddr *sa) : sizeof(struct sockaddr_in); } -/** True if @ss holds a v6-mapped-v4 address (RFC 4291, 2.5.5.2) */ +/* True if ss holds a v6-mapped-v4 address (RFC 4291, 2.5.5.2) */ static bool SS_IS_ADDR_V4MAPPED(const struct sockaddr_storage *ss) { const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss; @@ -593,10 +600,10 @@ static bool SS_IS_ADDR_V4MAPPED(const struct sockaddr_storage *ss) return ss->ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&ia6->sin6_addr); } -/** +/* * Process IPv4/v6 address, turn v6-mapped-v4 address into normal IPv4 address. - * \param ss Container of IPv4/6 address. - * \return Pointer to normalized address (may be static storage). + * ss: Container of IPv4/6 address. + * Returns: Pointer to normalized address (may be static storage). * * \sa RFC 3493. */ @@ -617,7 +624,7 @@ normalize_ip_address(const struct sockaddr_storage *ss) return (const struct sockaddr *)ss; } -/** +/* * Generic/fallback MTU values * * These are taken from RFC 1122, RFC 2460, and RFC 5405. @@ -632,7 +639,7 @@ static inline int generic_mtu(const int af_type) return af_type == AF_INET6 ? 1280 : 576; } -/** Crude approximation of IP header overhead - neglecting options. */ +/* Crude approximation of IP header overhead - neglecting options. */ static inline int estimated_header_overhead(const int af_type) { return af_type == AF_INET6 ? 40 : 20; @@ -829,6 +836,10 @@ int para_accept(int fd, void *addr, socklen_t size, int *new_fd) return -ERRNO_TO_PARA_ERROR(errno); } +#ifndef DCCP_SOCKOPT_AVAILABLE_CCIDS +#define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 /**< List of supported CCIDs. */ +#endif + /** * Probe the list of DCCP CCIDs configured on this host. * \param ccid_array Pointer to return statically allocated array in. @@ -843,7 +854,7 @@ int dccp_available_ccids(uint8_t **ccid_array) socklen_t nccids = sizeof(ccids); int ret, fd; - ret = fd = makesock(IPPROTO_DCCP, 1, NULL, 0, NULL); + ret = fd = makesock(IPPROTO_DCCP, true /* passive */, NULL, 0, NULL); if (ret < 0) return ret; @@ -861,6 +872,18 @@ int dccp_available_ccids(uint8_t **ccid_array) return nccids; } +/** + * The buffer size of the sun_path component of struct sockaddr_un. + * + * While glibc doesn't define UNIX_PATH_MAX, it documents it has being limited + * to 108 bytes. On NetBSD it is only 104 bytes though. We trust UNIX_PATH_MAX + * if it is defined and use the size of the ->sun_path member otherwise. This + * should be safe everywhere. + */ +#ifndef UNIX_PATH_MAX +#define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path)) +#endif + /* * Prepare a structure for AF_UNIX socket addresses. * diff --git a/net.h b/net.h index fd89dc5d..d206881c 100644 --- a/net.h +++ b/net.h @@ -1,106 +1,32 @@ /* Copyright (C) 2006 Andre Noll , see file COPYING. */ /** \file net.h exported symbols from net.c */ -/** - * The buffer size of the sun_path component of struct sockaddr_un. - * - * While glibc doesn't define \p UNIX_PATH_MAX, it documents it has being - * limited to 108 bytes. On NetBSD it is only 104 bytes though. We trust \p - * UNIX_PATH_MAX if it is defined and use the size of the ->sun_path member - * otherwise. This should be safe everywhere. - */ -#ifndef UNIX_PATH_MAX -#define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path)) -#endif - /* Userland defines for Linux DCCP support. */ -#ifndef IPPROTO_DCCP -#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_GET_CUR_MPS -#define DCCP_SOCKOPT_GET_CUR_MPS 5 /**< Max packet size, RFC 4340, 14. */ -#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 - -#ifndef DCCP_SOCKOPT_TX_CCID -#define DCCP_SOCKOPT_TX_CCID 14 /**< Set/get the TX CCID. */ -#endif - /** The maximum length of the host component in an URL. */ #define MAX_HOSTLEN 256 -/** - * Flowopts: Transport-layer independent encapsulation of socket options - * that need to be registered prior to setting up a connection. - */ +/* Opaque, only known to net.c. */ struct flowopts; -extern struct flowopts *flowopt_new(void); -extern void flowopt_add(struct flowopts *fo, int level, int opt, +struct flowopts *flowopt_new(void); +void flowopt_add(struct flowopts *fo, int level, int opt, const char *name, const void *val, int len); void flowopt_cleanup(struct flowopts *fo); -/** Flowopt shortcut macros */ -#define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len) /** * 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); +char *parse_cidr(const char *cidr, + char *addr, ssize_t addrlen, int32_t *netmask); +char *parse_url(const char *url, + char *host, ssize_t hostlen, int32_t *port); char *format_url(const char *url, int default_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; -} +const char *stringify_port(int port, const char *transport); int lookup_address(unsigned l4type, bool passive, const char *host, int port_number, struct addrinfo **result); @@ -114,10 +40,9 @@ int makesock(unsigned l4type, bool passive, const char *host, int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai, struct flowopts *fo); -static inline int para_connect_simple(unsigned l4type, - const char *host, uint16_t port) +static inline int para_connect(unsigned l4type, const char *host, uint16_t port) { - return makesock(l4type, 0, host, port, NULL); + return makesock(l4type, false, host, port, NULL); } void extract_v4_addr(const struct sockaddr_storage *ss, struct in_addr *ia); @@ -133,12 +58,12 @@ int para_listen(unsigned l4type, const char *addr, uint16_t port); int para_listen_simple(unsigned l4type, uint16_t port); /** Pretty-printing of IPv4/6 socket addresses */ -extern char *remote_name(int sockfd); +char *remote_name(int sockfd); /** * Determining maximum payload (packet) size */ -extern int generic_max_transport_msg_size(int sockfd); +int generic_max_transport_msg_size(int sockfd); int recv_bin_buffer(int fd, char *buf, size_t size); int recv_buffer(int fd, char *buf, size_t size); @@ -152,8 +77,6 @@ ssize_t send_cred_buffer(int, char*); /** * Functions and definitions to support \p IPPROTO_DCCP */ -/** Estimated worst-case length of a DCCP header including options. */ -#define DCCP_MAX_HEADER 128 /** 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); +int dccp_available_ccids(uint8_t **ccid_array); diff --git a/udp_recv.c b/udp_recv.c index 8d1274bc..365b6863 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -168,7 +168,7 @@ static int udp_recv_open(struct receiver_node *rn) uint32_t port = RECV_CMD_OPT_UINT32_VAL(UDP, PORT, lpr); int ret; - ret = makesock(IPPROTO_UDP, 1, host, port, NULL); + ret = makesock(IPPROTO_UDP, true /* passive */, host, port, NULL); if (ret < 0) return ret; rn->fd = ret; diff --git a/udp_send.c b/udp_send.c index 28947987..fe001025 100644 --- a/udp_send.c +++ b/udp_send.c @@ -187,7 +187,7 @@ static int udp_resolve_target(const char *url, struct sender_command_data *scd) return ret; port = scd->port > 0 ? scd->port : OPT_UINT32_VAL(UDP_DEFAULT_PORT); - ret = para_connect_simple(IPPROTO_UDP, scd->host, port); + ret = para_connect(IPPROTO_UDP, scd->host, port); if (ret < 0) return ret; @@ -336,7 +336,7 @@ static int udp_com_add(struct sender_command_data *scd) sc->private_data = ut; sc->fd = -1; - ret = para_connect_simple(IPPROTO_UDP, scd->host, scd->port); + ret = para_connect(IPPROTO_UDP, scd->host, scd->port); if (ret < 0) goto err; sc->fd = ret;