-/*
- * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/** \file net.c Networking-related helper functions. */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
-
-/* 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 <regex.h>
#include "error.h"
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.
+ */
+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.
*
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
*/
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;
}
* 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) {
return fd;
}
+/**
+ * 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);
+}
+
/**
* Determine IPv4/v6 socket address length.
* \param sa Container of IPv4 or IPv6 address.
* 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 */
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;
}
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)