X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=net.c;h=91200fc040bcfebafccf9e737fb65514af9ff8f2;hp=a58d84ef1c6345dcef41ca5b3ba76d3ed602cb7b;hb=refs%2Fheads%2Fpu;hpb=80541d0f045e1ed57332800eff9832e0a5b72ddf diff --git a/net.c b/net.c index a58d84ef..9b362442 100644 --- a/net.c +++ b/net.c @@ -1,8 +1,4 @@ -/* - * Copyright (C) 2005 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ +/* Copyright (C) 2005 Andre Noll , see file COPYING. */ /** \file net.c Networking-related helper functions. */ @@ -14,18 +10,6 @@ #include #include #include - -/* At least NetBSD needs these. */ -#ifndef AI_V4MAPPED -#define AI_V4MAPPED 0 -#endif -#ifndef AI_ALL -#define AI_ALL 0 -#endif -#ifndef AI_ADDRCONFIG -#define AI_ADDRCONFIG 0 -#endif - #include #include "error.h" @@ -34,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. * @@ -74,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; @@ -93,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: * @@ -184,6 +175,36 @@ failed: return NULL; } +/** + * Pretty-print a host/port pair. + * + * \param url NULL, or any string accepted by \ref parse_url(). + * \param default_port Applies if url has no port. + * + * If the url argument is NULL, the function returns the string + * 0.0.0.0:default_port. Otherwise it calls \ref parse_url() to check the + * syntax of the input string given by url. On errors the string "?" is + * returned. Otherwise, if url contains a port, a copy of url is returned. If + * no port was supplied, a colon and the default port are appended to url. + * + * \return In all cases the returned string is a allocated with malloc(3) and + * has to be freed by the caller. + */ +__malloc char *format_url(const char *url, int default_port) +{ + char host[MAX_HOSTLEN]; + int url_port; + + if (!url) + return make_message("0.0.0.0:%d", default_port); + if (!parse_url(url, host, sizeof(host), &url_port)) + return make_message("?"); + if (url_port < 0) + return make_message("%s:%d", url, default_port); + else + return para_strdup(url); +} + /** * Stringify port number, resolve into service name where defined. * @@ -191,7 +212,7 @@ failed: * \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) { @@ -210,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) { @@ -227,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) { @@ -259,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; }; @@ -272,9 +297,9 @@ struct flowopts { */ struct flowopts *flowopt_new(void) { - struct flowopts *new = para_malloc(sizeof(*new)); + struct flowopts *new = alloc(sizeof(*new)); - INIT_LIST_HEAD(&new->sockopts); + init_list_head(&new->sockopts); return new; } @@ -293,7 +318,7 @@ struct flowopts *flowopt_new(void) void flowopt_add(struct flowopts *fo, int lev, int opt, const char *name, const void *val, int len) { - struct pre_conn_opt *new = para_malloc(sizeof(*new)); + struct pre_conn_opt *new = alloc(sizeof(*new)); new->sock_option = opt; new->sock_level = lev; @@ -303,7 +328,7 @@ void flowopt_add(struct flowopts *fo, int lev, int opt, new->opt_val = NULL; new->opt_len = 0; } else { - new->opt_val = para_malloc(len); + new->opt_val = alloc(len); new->opt_len = len; memcpy(new->opt_val, val, len); } @@ -311,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; @@ -429,15 +454,20 @@ int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai, for (; ai; ai = ai->ai_next) { int fd; ret = socket(ai->ai_family, sock_type(l4type), l4type); - if (ret < 0) + if (ret < 0) { + PARA_NOTICE_LOG("socket(): %s\n", strerror(errno)); continue; + } fd = ret; flowopt_setopts(fd, fo); if (!passive) { - if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) - return fd; - close(fd); - continue; + if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) { + PARA_NOTICE_LOG("connect(): %s\n", + strerror(errno)); + close(fd); + continue; + } + return fd; } /* * Reuse the address on passive sockets to avoid failure on @@ -446,10 +476,12 @@ int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai, */ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { + PARA_NOTICE_LOG("setsockopt(): %s\n", strerror(errno)); close(fd); continue; } if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) { + PARA_NOTICE_LOG("bind(): %s\n", strerror(errno)); close(fd); continue; } @@ -488,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); } @@ -499,18 +531,28 @@ int makesock(unsigned l4type, bool passive, const char *host, uint16_t port_numb * Create a passive / listening socket. * * \param l4type The transport-layer type (\p IPPROTO_xxx). - * \param port The decimal port number to listen on. - * \param fo Flowopts (if any) to set before starting to listen. + * \param addr Passed to \ref parse_url() if not NULL. + * \param port Ignored if addr contains a port number. * * \return Positive integer (socket descriptor) on success, negative value * otherwise. * * \sa \ref makesock(), ip(7), ipv6(7), bind(2), listen(2). */ -int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo) +int para_listen(unsigned l4type, const char *addr, uint16_t port) { - int ret, fd = makesock(l4type, 1, NULL, port, fo); - + char host[MAX_HOSTLEN]; + int ret, fd, addr_port; + + if (addr) { + if (!parse_url(addr, host, sizeof(host), &addr_port)) + return -ERRNO_TO_PARA_ERROR(EINVAL); + if (addr_port > 0) + port = addr_port; + addr = host; + } + fd = makesock(l4type, true /* passive */, addr, port, + NULL /* no flowopts */); if (fd > 0) { ret = listen(fd, BACKLOG); if (ret < 0) { @@ -525,10 +567,22 @@ int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo) } /** - * Determine IPv4/v6 socket address length. - * \param sa Container of IPv4 or IPv6 address. - * \return Address-family dependent address length. + * Create a socket which listens on all network addresses. + * + * \param l4type See \ref para_listen(). + * \param port See \ref para_listen(). + * + * This is a simple wrapper for \ref para_listen() which passes a NULL pointer + * as the address information. + * + * \return See \ref para_listen(). */ +int para_listen_simple(unsigned l4type, uint16_t port) +{ + return para_listen(l4type, NULL, port); +} + +/* 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); @@ -538,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; @@ -546,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. */ @@ -570,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. @@ -585,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; @@ -754,25 +808,21 @@ int recv_buffer(int fd, char *buf, size_t size) * Wrapper around the accept system call. * * \param fd The listening socket. - * \param rfds An optional fd_set pointer. * \param addr Structure which is filled in with the address of the peer socket. * \param size Should contain the size of the structure pointed to by \a addr. * \param new_fd Result pointer. * - * Accept incoming connections on \a addr, retry if interrupted. If \a rfds is - * not \p NULL, return 0 if \a fd is not set in \a rfds without calling accept(). + * Accept incoming connections on addr, retry if interrupted. * * \return Negative on errors, zero if no connections are present to be accepted, * one otherwise. * * \sa accept(2). */ -int para_accept(int fd, fd_set *rfds, void *addr, socklen_t size, int *new_fd) +int para_accept(int fd, void *addr, socklen_t size, int *new_fd) { int ret; - if (rfds && !FD_ISSET(fd, rfds)) - return 0; do ret = accept(fd, (struct sockaddr *) addr, &size); while (ret < 0 && errno == EINTR); @@ -786,6 +836,10 @@ int para_accept(int fd, fd_set *rfds, 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. @@ -800,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; @@ -818,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. * @@ -827,14 +893,13 @@ int dccp_available_ccids(uint8_t **ccid_array) * The first call to this function tries to bind a socket to the abstract name * space. The result of this test is stored in a static variable. Subsequent * calls read this variable and create abstract sockets on systems that support - * them. + * them. If a NULL pointer is passed as the name, the function only + * initializes the static variable. */ static int init_unix_addr(struct sockaddr_un *u, const char *name) { static int use_abstract; - if (strlen(name) + 1 >= UNIX_PATH_MAX) - return -E_NAME_TOO_LONG; memset(u->sun_path, 0, UNIX_PATH_MAX); u->sun_family = PF_UNIX; if (use_abstract == 0) { /* executed only once */ @@ -848,6 +913,10 @@ static int init_unix_addr(struct sockaddr_un *u, const char *name) PARA_NOTICE_LOG("%susing abstract socket namespace\n", use_abstract == 1? "" : "not "); } + if (!name) + return 0; + if (strlen(name) + 1 >= UNIX_PATH_MAX) + return -E_NAME_TOO_LONG; strcpy(u->sun_path + (use_abstract == 1? 1 : 0), name); return 1; } @@ -872,7 +941,7 @@ int create_local_socket(const char *name) int fd, ret; ret = init_unix_addr(&unix_addr, name); - if (ret < 0) + if (ret <= 0) /* error, or name was NULL */ return ret; ret = socket(PF_UNIX, SOCK_STREAM, 0); if (ret < 0)