X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=net.c;h=e01af24be27c49a00a60a61a98448bc3d82046f0;hb=501b83a39828c9d3db5498c7352a2e5b60175bba;hp=672e09e971717a742eb85b9c57b05f661f0b97dd;hpb=631fb4db2cea7bc44e91631957db8cd852fe75ee;p=paraslash.git diff --git a/net.c b/net.c index 672e09e9..e01af24b 100644 --- a/net.c +++ b/net.c @@ -10,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" @@ -180,6 +168,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. + */ +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. * @@ -268,9 +286,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; } @@ -289,7 +307,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; @@ -299,7 +317,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); } @@ -783,25 +801,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); @@ -856,14 +870,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 */ @@ -877,6 +890,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; } @@ -901,7 +918,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)