2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009-2010 The FreeBSD Foundation
|
2011-05-20 11:14:05 +00:00
|
|
|
* Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
|
2010-02-18 23:16:19 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Pawel Jakub Dawidek under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h> /* MAXHOSTNAMELEN */
|
2011-03-22 16:21:11 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2010-04-29 21:55:20 +00:00
|
|
|
#include <fcntl.h>
|
2010-02-18 23:16:19 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "pjdlog.h"
|
|
|
|
#include "proto_impl.h"
|
2010-04-29 21:55:20 +00:00
|
|
|
#include "subr.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
#define TCP_CTX_MAGIC 0x7c41c
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx {
|
2010-02-18 23:16:19 +00:00
|
|
|
int tc_magic;
|
2011-05-20 11:14:05 +00:00
|
|
|
struct sockaddr_storage tc_sa;
|
2010-02-18 23:16:19 +00:00
|
|
|
int tc_fd;
|
|
|
|
int tc_side;
|
2011-05-20 11:09:02 +00:00
|
|
|
#define TCP_SIDE_CLIENT 0
|
|
|
|
#define TCP_SIDE_SERVER_LISTEN 1
|
|
|
|
#define TCP_SIDE_SERVER_WORK 2
|
2010-02-18 23:16:19 +00:00
|
|
|
};
|
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
static int tcp_connect_wait(void *ctx, int timeout);
|
|
|
|
static void tcp_close(void *ctx);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Function converts the given string to unsigned number.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
numfromstr(const char *str, intmax_t minnum, intmax_t maxnum, intmax_t *nump)
|
|
|
|
{
|
|
|
|
intmax_t digit, num;
|
|
|
|
|
|
|
|
if (str[0] == '\0')
|
|
|
|
goto invalid; /* Empty string. */
|
|
|
|
num = 0;
|
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
if (*str < '0' || *str > '9')
|
|
|
|
goto invalid; /* Non-digit character. */
|
|
|
|
digit = *str - '0';
|
|
|
|
if (num > num * 10 + digit)
|
|
|
|
goto invalid; /* Overflow. */
|
|
|
|
num = num * 10 + digit;
|
|
|
|
if (num > maxnum)
|
|
|
|
goto invalid; /* Too big. */
|
|
|
|
}
|
|
|
|
if (num < minnum)
|
|
|
|
goto invalid; /* Too small. */
|
|
|
|
*nump = num;
|
|
|
|
return (0);
|
|
|
|
invalid:
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:14:05 +00:00
|
|
|
tcp_addr(const char *addr, int defport, struct sockaddr_storage *sap)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:14:05 +00:00
|
|
|
char iporhost[MAXHOSTNAMELEN], portstr[6];
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res;
|
2010-02-18 23:16:19 +00:00
|
|
|
const char *pp;
|
2011-05-20 11:14:05 +00:00
|
|
|
intmax_t port;
|
2010-02-18 23:16:19 +00:00
|
|
|
size_t size;
|
2011-05-20 11:14:05 +00:00
|
|
|
int error;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
if (addr == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
bzero(&hints, sizeof(hints));
|
|
|
|
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
if (strncasecmp(addr, "tcp4://", 7) == 0) {
|
|
|
|
addr += 7;
|
|
|
|
hints.ai_family = PF_INET;
|
|
|
|
} else if (strncasecmp(addr, "tcp6://", 7) == 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
addr += 7;
|
2011-05-20 11:14:05 +00:00
|
|
|
hints.ai_family = PF_INET6;
|
|
|
|
} else if (strncasecmp(addr, "tcp://", 6) == 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
addr += 6;
|
2011-05-20 11:14:05 +00:00
|
|
|
} else {
|
2010-08-05 17:57:59 +00:00
|
|
|
/*
|
2011-05-20 11:09:02 +00:00
|
|
|
* Because TCP is the default assume IP or host is given without
|
2010-08-05 17:57:59 +00:00
|
|
|
* prefix.
|
|
|
|
*/
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
/*
|
|
|
|
* Extract optional port.
|
|
|
|
* There are three cases to consider.
|
|
|
|
* 1. hostname with port, eg. freefall.freebsd.org:8457
|
|
|
|
* 2. IPv4 address with port, eg. 192.168.0.101:8457
|
|
|
|
* 3. IPv6 address with port, eg. [fe80::1]:8457
|
|
|
|
* We discover IPv6 address by checking for two colons and if port is
|
|
|
|
* given, the address has to start with [.
|
|
|
|
*/
|
|
|
|
pp = NULL;
|
|
|
|
if (strchr(addr, ':') != strrchr(addr, ':')) {
|
|
|
|
if (addr[0] == '[')
|
|
|
|
pp = strrchr(addr, ':');
|
|
|
|
} else {
|
|
|
|
pp = strrchr(addr, ':');
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
if (pp == NULL) {
|
|
|
|
/* Port not given, use the default. */
|
2011-05-20 11:14:05 +00:00
|
|
|
port = defport;
|
2010-02-18 23:16:19 +00:00
|
|
|
} else {
|
|
|
|
if (numfromstr(pp + 1, 1, 65535, &port) < 0)
|
|
|
|
return (errno);
|
|
|
|
}
|
2011-05-20 11:14:05 +00:00
|
|
|
(void)snprintf(portstr, sizeof(portstr), "%jd", (intmax_t)port);
|
2010-02-18 23:16:19 +00:00
|
|
|
/* Extract host name or IP address. */
|
|
|
|
if (pp == NULL) {
|
|
|
|
size = sizeof(iporhost);
|
|
|
|
if (strlcpy(iporhost, addr, size) >= size)
|
|
|
|
return (ENAMETOOLONG);
|
2011-05-20 11:14:05 +00:00
|
|
|
} else if (addr[0] == '[' && pp[-1] == ']') {
|
|
|
|
size = (size_t)(pp - addr - 2 + 1);
|
|
|
|
if (size > sizeof(iporhost))
|
|
|
|
return (ENAMETOOLONG);
|
|
|
|
(void)strlcpy(iporhost, addr + 1, size);
|
2010-02-18 23:16:19 +00:00
|
|
|
} else {
|
|
|
|
size = (size_t)(pp - addr + 1);
|
|
|
|
if (size > sizeof(iporhost))
|
|
|
|
return (ENAMETOOLONG);
|
2010-08-16 21:59:56 +00:00
|
|
|
(void)strlcpy(iporhost, addr, size);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
2011-05-20 11:14:05 +00:00
|
|
|
|
|
|
|
error = getaddrinfo(iporhost, portstr, &hints, &res);
|
|
|
|
if (error != 0) {
|
|
|
|
pjdlog_debug(1, "getaddrinfo(%s, %s) failed: %s.", iporhost,
|
|
|
|
portstr, gai_strerror(error));
|
2010-02-18 23:16:19 +00:00
|
|
|
return (EINVAL);
|
2011-05-20 11:14:05 +00:00
|
|
|
}
|
|
|
|
if (res == NULL)
|
|
|
|
return (ENOENT);
|
|
|
|
|
|
|
|
memcpy(sap, res->ai_addr, res->ai_addrlen);
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_setup_new(const char *addr, int side, void **ctxp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx;
|
2011-02-01 07:58:43 +00:00
|
|
|
int ret, nodelay;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(addr != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(side == TCP_SIDE_CLIENT ||
|
|
|
|
side == TCP_SIDE_SERVER_LISTEN);
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(ctxp != NULL);
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
tctx = malloc(sizeof(*tctx));
|
|
|
|
if (tctx == NULL)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
/* Parse given address. */
|
2011-05-20 11:14:05 +00:00
|
|
|
if ((ret = tcp_addr(addr, PROTO_TCP_DEFAULT_PORT, &tctx->tc_sa)) != 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
free(tctx);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
|
2011-02-02 15:53:09 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
tctx->tc_fd = socket(tctx->tc_sa.ss_family, SOCK_STREAM, 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (tctx->tc_fd == -1) {
|
|
|
|
ret = errno;
|
|
|
|
free(tctx);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
|
2011-03-21 08:54:59 +00:00
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
/* Socket settings. */
|
2011-02-01 07:58:43 +00:00
|
|
|
nodelay = 1;
|
|
|
|
if (setsockopt(tctx->tc_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
|
|
|
|
sizeof(nodelay)) == -1) {
|
2011-02-02 15:53:09 +00:00
|
|
|
pjdlog_errno(LOG_WARNING, "Unable to set TCP_NOELAY");
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tctx->tc_side = side;
|
2011-05-20 11:09:02 +00:00
|
|
|
tctx->tc_magic = TCP_CTX_MAGIC;
|
2010-02-18 23:16:19 +00:00
|
|
|
*ctxp = tctx;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2011-02-02 15:53:09 +00:00
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_setup_wrap(int fd, int side, void **ctxp)
|
2011-02-02 15:53:09 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx;
|
2011-02-02 15:53:09 +00:00
|
|
|
|
|
|
|
PJDLOG_ASSERT(fd >= 0);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(side == TCP_SIDE_CLIENT ||
|
|
|
|
side == TCP_SIDE_SERVER_WORK);
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(ctxp != NULL);
|
|
|
|
|
|
|
|
tctx = malloc(sizeof(*tctx));
|
|
|
|
if (tctx == NULL)
|
|
|
|
return (errno);
|
|
|
|
|
|
|
|
tctx->tc_fd = fd;
|
2011-05-20 11:14:05 +00:00
|
|
|
tctx->tc_sa.ss_family = AF_UNSPEC;
|
2011-02-02 15:53:09 +00:00
|
|
|
tctx->tc_side = side;
|
2011-05-20 11:09:02 +00:00
|
|
|
tctx->tc_magic = TCP_CTX_MAGIC;
|
2011-02-02 15:53:09 +00:00
|
|
|
*ctxp = tctx;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-02-18 23:16:19 +00:00
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_client(const char *srcaddr, const char *dstaddr, void **ctxp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx;
|
2011-05-20 11:14:05 +00:00
|
|
|
struct sockaddr_storage sa;
|
2011-03-21 08:54:59 +00:00
|
|
|
int ret;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
ret = tcp_setup_new(dstaddr, TCP_SIDE_CLIENT, ctxp);
|
2011-03-21 08:54:59 +00:00
|
|
|
if (ret != 0)
|
|
|
|
return (ret);
|
|
|
|
tctx = *ctxp;
|
|
|
|
if (srcaddr == NULL)
|
|
|
|
return (0);
|
2011-05-20 11:14:05 +00:00
|
|
|
ret = tcp_addr(srcaddr, 0, &sa);
|
2011-03-21 08:54:59 +00:00
|
|
|
if (ret != 0) {
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_close(tctx);
|
2011-03-21 08:54:59 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2011-05-20 11:14:05 +00:00
|
|
|
if (bind(tctx->tc_fd, (struct sockaddr *)&sa, sa.ss_len) < 0) {
|
2011-03-21 08:54:59 +00:00
|
|
|
ret = errno;
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_close(tctx);
|
2011-03-21 08:54:59 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
return (0);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_connect(void *ctx, int timeout)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx = ctx;
|
2011-02-02 15:46:28 +00:00
|
|
|
int error, flags;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
|
|
|
PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_CLIENT);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
|
2011-02-02 15:46:28 +00:00
|
|
|
PJDLOG_ASSERT(timeout >= -1);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2010-04-29 21:55:20 +00:00
|
|
|
flags = fcntl(tctx->tc_fd, F_GETFL);
|
|
|
|
if (flags == -1) {
|
|
|
|
KEEP_ERRNO(pjdlog_common(LOG_DEBUG, 1, errno,
|
|
|
|
"fcntl(F_GETFL) failed"));
|
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
/*
|
2010-08-27 13:54:17 +00:00
|
|
|
* We make socket non-blocking so we can handle connection timeout
|
|
|
|
* manually.
|
2010-04-29 21:55:20 +00:00
|
|
|
*/
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(tctx->tc_fd, F_SETFL, flags) == -1) {
|
|
|
|
KEEP_ERRNO(pjdlog_common(LOG_DEBUG, 1, errno,
|
|
|
|
"fcntl(F_SETFL, O_NONBLOCK) failed"));
|
2010-02-18 23:16:19 +00:00
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
if (connect(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
|
|
|
|
tctx->tc_sa.ss_len) == 0) {
|
2011-02-02 15:46:28 +00:00
|
|
|
if (timeout == -1)
|
|
|
|
return (0);
|
2010-04-29 21:55:20 +00:00
|
|
|
error = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (errno != EINPROGRESS) {
|
|
|
|
error = errno;
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno, "connect() failed");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-02-02 15:46:28 +00:00
|
|
|
if (timeout == -1)
|
|
|
|
return (0);
|
2011-05-20 11:09:02 +00:00
|
|
|
return (tcp_connect_wait(ctx, timeout));
|
2011-02-02 15:46:28 +00:00
|
|
|
done:
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
if (fcntl(tctx->tc_fd, F_SETFL, flags) == -1) {
|
|
|
|
if (error == 0)
|
|
|
|
error = errno;
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno,
|
|
|
|
"fcntl(F_SETFL, ~O_NONBLOCK) failed");
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_connect_wait(void *ctx, int timeout)
|
2011-02-02 15:46:28 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx = ctx;
|
2011-02-02 15:46:28 +00:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set fdset;
|
|
|
|
socklen_t esize;
|
|
|
|
int error, flags, ret;
|
|
|
|
|
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
|
|
|
PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_CLIENT);
|
2011-02-02 15:46:28 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
|
|
|
PJDLOG_ASSERT(timeout >= 0);
|
|
|
|
|
2011-02-02 15:42:00 +00:00
|
|
|
tv.tv_sec = timeout;
|
2010-04-29 21:55:20 +00:00
|
|
|
tv.tv_usec = 0;
|
|
|
|
again:
|
|
|
|
FD_ZERO(&fdset);
|
2011-03-22 10:39:34 +00:00
|
|
|
FD_SET(tctx->tc_fd, &fdset);
|
2010-04-29 21:55:20 +00:00
|
|
|
ret = select(tctx->tc_fd + 1, NULL, &fdset, NULL, &tv);
|
|
|
|
if (ret == 0) {
|
|
|
|
error = ETIMEDOUT;
|
|
|
|
goto done;
|
|
|
|
} else if (ret == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
error = errno;
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno, "select() failed");
|
|
|
|
goto done;
|
|
|
|
}
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(ret > 0);
|
|
|
|
PJDLOG_ASSERT(FD_ISSET(tctx->tc_fd, &fdset));
|
2010-04-29 21:55:20 +00:00
|
|
|
esize = sizeof(error);
|
|
|
|
if (getsockopt(tctx->tc_fd, SOL_SOCKET, SO_ERROR, &error,
|
|
|
|
&esize) == -1) {
|
|
|
|
error = errno;
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno,
|
|
|
|
"getsockopt(SO_ERROR) failed");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (error != 0) {
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, error,
|
|
|
|
"getsockopt(SO_ERROR) returned error");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
done:
|
2011-02-02 15:46:28 +00:00
|
|
|
flags = fcntl(tctx->tc_fd, F_GETFL);
|
|
|
|
if (flags == -1) {
|
|
|
|
if (error == 0)
|
|
|
|
error = errno;
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno, "fcntl(F_GETFL) failed");
|
|
|
|
return (error);
|
|
|
|
}
|
2010-04-29 21:55:20 +00:00
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
if (fcntl(tctx->tc_fd, F_SETFL, flags) == -1) {
|
|
|
|
if (error == 0)
|
|
|
|
error = errno;
|
|
|
|
pjdlog_common(LOG_DEBUG, 1, errno,
|
|
|
|
"fcntl(F_SETFL, ~O_NONBLOCK) failed");
|
|
|
|
}
|
|
|
|
return (error);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_server(const char *addr, void **ctxp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx;
|
2010-02-18 23:16:19 +00:00
|
|
|
int ret, val;
|
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
ret = tcp_setup_new(addr, TCP_SIDE_SERVER_LISTEN, ctxp);
|
2010-02-18 23:16:19 +00:00
|
|
|
if (ret != 0)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
tctx = *ctxp;
|
|
|
|
|
|
|
|
val = 1;
|
|
|
|
/* Ignore failure. */
|
|
|
|
(void)setsockopt(tctx->tc_fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
|
|
|
sizeof(val));
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
|
2011-02-02 15:53:09 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
if (bind(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
|
|
|
|
tctx->tc_sa.ss_len) < 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
ret = errno;
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_close(tctx);
|
2010-02-18 23:16:19 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
if (listen(tctx->tc_fd, 8) < 0) {
|
|
|
|
ret = errno;
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_close(tctx);
|
2010-02-18 23:16:19 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_accept(void *ctx, void **newctxp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx = ctx;
|
|
|
|
struct tcp_ctx *newtctx;
|
2010-02-18 23:16:19 +00:00
|
|
|
socklen_t fromlen;
|
|
|
|
int ret;
|
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
|
|
|
PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_SERVER_LISTEN);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
newtctx = malloc(sizeof(*newtctx));
|
|
|
|
if (newtctx == NULL)
|
|
|
|
return (errno);
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
fromlen = tctx->tc_sa.ss_len;
|
|
|
|
newtctx->tc_fd = accept(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
|
2010-02-18 23:16:19 +00:00
|
|
|
&fromlen);
|
|
|
|
if (newtctx->tc_fd < 0) {
|
|
|
|
ret = errno;
|
|
|
|
free(newtctx);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
newtctx->tc_side = TCP_SIDE_SERVER_WORK;
|
|
|
|
newtctx->tc_magic = TCP_CTX_MAGIC;
|
2010-02-18 23:16:19 +00:00
|
|
|
*newctxp = newtctx;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_wrap(int fd, bool client, void **ctxp)
|
2011-02-02 15:53:09 +00:00
|
|
|
{
|
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
return (tcp_setup_wrap(fd,
|
|
|
|
client ? TCP_SIDE_CLIENT : TCP_SIDE_SERVER_WORK, ctxp));
|
2011-02-02 15:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_send(void *ctx, const unsigned char *data, size_t size, int fd)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx = ctx;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(fd == -1);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-02-02 15:53:09 +00:00
|
|
|
return (proto_common_send(tctx->tc_fd, data, size, -1));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx = ctx;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_fd >= 0);
|
2011-02-02 15:53:09 +00:00
|
|
|
PJDLOG_ASSERT(fdp == NULL);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-02-02 15:53:09 +00:00
|
|
|
return (proto_common_recv(tctx->tc_fd, data, size, NULL));
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_descriptor(const void *ctx)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
const struct tcp_ctx *tctx = ctx;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
return (tctx->tc_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_address_match(const void *ctx, const char *addr)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
const struct tcp_ctx *tctx = ctx;
|
2011-05-20 11:14:05 +00:00
|
|
|
struct sockaddr_storage sa1, sa2;
|
|
|
|
socklen_t salen;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
if (tcp_addr(addr, PROTO_TCP_DEFAULT_PORT, &sa1) != 0)
|
|
|
|
return (false);
|
|
|
|
|
|
|
|
salen = sizeof(sa2);
|
|
|
|
if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa2, &salen) < 0)
|
2010-02-18 23:16:19 +00:00
|
|
|
return (false);
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
if (sa1.ss_family != sa2.ss_family || sa1.ss_len != sa2.ss_len)
|
2010-02-18 23:16:19 +00:00
|
|
|
return (false);
|
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
switch (sa1.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in *sin1, *sin2;
|
|
|
|
|
|
|
|
sin1 = (struct sockaddr_in *)&sa1;
|
|
|
|
sin2 = (struct sockaddr_in *)&sa2;
|
|
|
|
|
|
|
|
return (memcmp(&sin1->sin_addr, &sin2->sin_addr,
|
|
|
|
sizeof(sin1->sin_addr)) == 0);
|
|
|
|
}
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sin1, *sin2;
|
|
|
|
|
|
|
|
sin1 = (struct sockaddr_in6 *)&sa1;
|
|
|
|
sin2 = (struct sockaddr_in6 *)&sa2;
|
|
|
|
|
|
|
|
return (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
|
|
|
|
sizeof(sin1->sin6_addr)) == 0);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return (false);
|
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_local_address(const void *ctx, char *addr, size_t size)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
const struct tcp_ctx *tctx = ctx;
|
2011-05-20 11:14:05 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t salen;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
salen = sizeof(sa);
|
|
|
|
if (getsockname(tctx->tc_fd, (struct sockaddr *)&sa, &salen) < 0) {
|
2010-08-05 18:27:41 +00:00
|
|
|
PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
|
2010-02-18 23:16:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_VERIFY(snprintf(addr, size, "tcp://%S", &sa) < (ssize_t)size);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_remote_address(const void *ctx, char *addr, size_t size)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
const struct tcp_ctx *tctx = ctx;
|
2011-05-20 11:14:05 +00:00
|
|
|
struct sockaddr_storage sa;
|
|
|
|
socklen_t salen;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-05-20 11:14:05 +00:00
|
|
|
salen = sizeof(sa);
|
|
|
|
if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa, &salen) < 0) {
|
2010-08-05 18:27:41 +00:00
|
|
|
PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
|
2010-02-18 23:16:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-20 11:14:05 +00:00
|
|
|
PJDLOG_VERIFY(snprintf(addr, size, "tcp://%S", &sa) < (ssize_t)size);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_close(void *ctx)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
2011-05-20 11:09:02 +00:00
|
|
|
struct tcp_ctx *tctx = ctx;
|
2010-02-18 23:16:19 +00:00
|
|
|
|
2011-01-31 18:32:17 +00:00
|
|
|
PJDLOG_ASSERT(tctx != NULL);
|
2011-05-20 11:09:02 +00:00
|
|
|
PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
if (tctx->tc_fd >= 0)
|
|
|
|
close(tctx->tc_fd);
|
|
|
|
tctx->tc_magic = 0;
|
|
|
|
free(tctx);
|
|
|
|
}
|
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
static struct proto tcp_proto = {
|
|
|
|
.prt_name = "tcp",
|
|
|
|
.prt_client = tcp_client,
|
|
|
|
.prt_connect = tcp_connect,
|
|
|
|
.prt_connect_wait = tcp_connect_wait,
|
|
|
|
.prt_server = tcp_server,
|
|
|
|
.prt_accept = tcp_accept,
|
|
|
|
.prt_wrap = tcp_wrap,
|
|
|
|
.prt_send = tcp_send,
|
|
|
|
.prt_recv = tcp_recv,
|
|
|
|
.prt_descriptor = tcp_descriptor,
|
|
|
|
.prt_address_match = tcp_address_match,
|
|
|
|
.prt_local_address = tcp_local_address,
|
|
|
|
.prt_remote_address = tcp_remote_address,
|
|
|
|
.prt_close = tcp_close
|
2010-02-18 23:16:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static __constructor void
|
2011-05-20 11:09:02 +00:00
|
|
|
tcp_ctor(void)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
|
2011-05-20 11:09:02 +00:00
|
|
|
proto_register(&tcp_proto, true);
|
2010-02-18 23:16:19 +00:00
|
|
|
}
|