2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <config/config.h>
33 #include <sys/param.h> /* MAXHOSTNAMELEN */
34 #include <sys/socket.h>
36 #include <arpa/inet.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
52 #include <compat/strlcpy.h>
56 #include "proto_impl.h"
59 #define TCP_CTX_MAGIC 0x7c41c
62 struct sockaddr_storage tc_sa;
65 #define TCP_SIDE_CLIENT 0
66 #define TCP_SIDE_SERVER_LISTEN 1
67 #define TCP_SIDE_SERVER_WORK 2
71 static int tcp_connect_wait(void *ctx, int timeout);
72 static void tcp_close(void *ctx);
75 * Function converts the given string to unsigned number.
78 numfromstr(const char *str, intmax_t minnum, intmax_t maxnum, intmax_t *nump)
83 goto invalid; /* Empty string. */
85 for (; *str != '\0'; str++) {
86 if (*str < '0' || *str > '9')
87 goto invalid; /* Non-digit character. */
89 if (num > num * 10 + digit)
90 goto invalid; /* Overflow. */
91 num = num * 10 + digit;
93 goto invalid; /* Too big. */
96 goto invalid; /* Too small. */
105 tcp_addr(const char *addr, int defport, struct sockaddr_storage *sap)
107 char iporhost[MAXHOSTNAMELEN], portstr[6];
108 struct addrinfo hints;
109 struct addrinfo *res;
118 bzero(&hints, sizeof(hints));
119 hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
120 hints.ai_family = PF_UNSPEC;
121 hints.ai_socktype = SOCK_STREAM;
122 hints.ai_protocol = IPPROTO_TCP;
124 if (strncasecmp(addr, "tcp4://", 7) == 0) {
126 hints.ai_family = PF_INET;
127 } else if (strncasecmp(addr, "tcp6://", 7) == 0) {
129 hints.ai_family = PF_INET6;
130 } else if (strncasecmp(addr, "tcp://", 6) == 0) {
134 * Because TCP is the default assume IP or host is given without
140 * Extract optional port.
141 * There are three cases to consider.
142 * 1. hostname with port, eg. freefall.freebsd.org:8457
143 * 2. IPv4 address with port, eg. 192.168.0.101:8457
144 * 3. IPv6 address with port, eg. [fe80::1]:8457
145 * We discover IPv6 address by checking for two colons and if port is
146 * given, the address has to start with [.
149 if (strchr(addr, ':') != strrchr(addr, ':')) {
151 pp = strrchr(addr, ':');
153 pp = strrchr(addr, ':');
156 /* Port not given, use the default. */
159 if (numfromstr(pp + 1, 1, 65535, &port) < 0)
162 (void)snprintf(portstr, sizeof(portstr), "%jd", (intmax_t)port);
163 /* Extract host name or IP address. */
165 size = sizeof(iporhost);
166 if (strlcpy(iporhost, addr, size) >= size)
167 return (ENAMETOOLONG);
168 } else if (addr[0] == '[' && pp[-1] == ']') {
169 size = (size_t)(pp - addr - 2 + 1);
170 if (size > sizeof(iporhost))
171 return (ENAMETOOLONG);
172 (void)strlcpy(iporhost, addr + 1, size);
174 size = (size_t)(pp - addr + 1);
175 if (size > sizeof(iporhost))
176 return (ENAMETOOLONG);
177 (void)strlcpy(iporhost, addr, size);
180 error = getaddrinfo(iporhost, portstr, &hints, &res);
182 pjdlog_debug(1, "getaddrinfo(%s, %s) failed: %s.", iporhost,
183 portstr, gai_strerror(error));
189 memcpy(sap, res->ai_addr, res->ai_addrlen);
197 tcp_setup_new(const char *addr, int side, struct tcp_ctx **tctxp)
199 struct tcp_ctx *tctx;
202 PJDLOG_ASSERT(addr != NULL);
203 PJDLOG_ASSERT(side == TCP_SIDE_CLIENT ||
204 side == TCP_SIDE_SERVER_LISTEN);
205 PJDLOG_ASSERT(tctxp != NULL);
207 tctx = malloc(sizeof(*tctx));
211 /* Parse given address. */
212 error = tcp_addr(addr, atoi(proto_get("tcp:port")), &tctx->tc_sa);
218 PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
220 tctx->tc_fd = socket(tctx->tc_sa.ss_family, SOCK_STREAM, 0);
221 if (tctx->tc_fd == -1) {
227 PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
229 /* Socket settings. */
231 if (setsockopt(tctx->tc_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
232 sizeof(nodelay)) == -1) {
233 pjdlog_errno(LOG_WARNING, "Unable to set TCP_NOELAY");
236 tctx->tc_wait_called = (side == TCP_SIDE_CLIENT ? false : true);
237 tctx->tc_side = side;
238 tctx->tc_magic = TCP_CTX_MAGIC;
245 sockaddr_len(const struct sockaddr_storage *ss)
248 #ifdef HAVE_SOCKADDR_STORAGE_SS_LEN
251 switch (ss->ss_family) {
253 return (sizeof(struct sockaddr_in));
255 return (sizeof(struct sockaddr_in6));
257 PJDLOG_ABORT("Unexpected family %hhu.", ss->ss_family);
263 tcp_connect(const char *srcaddr, const char *dstaddr, int timeout, void **ctxp)
265 struct tcp_ctx *tctx;
266 struct sockaddr_storage sa;
267 int error, flags, ret;
269 PJDLOG_ASSERT(srcaddr == NULL || srcaddr[0] != '\0');
270 PJDLOG_ASSERT(dstaddr != NULL);
271 PJDLOG_ASSERT(timeout >= -1);
273 error = tcp_setup_new(dstaddr, TCP_SIDE_CLIENT, &tctx);
276 if (srcaddr != NULL) {
277 error = tcp_addr(srcaddr, 0, &sa);
280 if (bind(tctx->tc_fd, (struct sockaddr *)&sa,
281 sockaddr_len(&sa)) == -1) {
287 flags = fcntl(tctx->tc_fd, F_GETFL);
290 pjdlog_common(LOG_DEBUG, 1, errno, "fcntl(F_GETFL) failed");
294 * We make socket non-blocking so we can handle connection timeout
298 if (fcntl(tctx->tc_fd, F_SETFL, flags) == -1) {
300 pjdlog_common(LOG_DEBUG, 1, errno,
301 "fcntl(F_SETFL, O_NONBLOCK) failed");
305 ret = connect(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
306 sockaddr_len(&tctx->tc_sa));
307 if (ret == -1 && errno != EINPROGRESS) {
309 pjdlog_common(LOG_DEBUG, 1, errno, "connect() failed");
315 /* Connection still in progress. Wait for it. */
316 error = tcp_connect_wait(tctx, timeout);
320 /* Connection already complete. */
321 flags &= ~O_NONBLOCK;
322 if (fcntl(tctx->tc_fd, F_SETFL, flags) == -1) {
324 pjdlog_common(LOG_DEBUG, 1, errno,
325 "fcntl(F_SETFL, ~O_NONBLOCK) failed");
339 tcp_connect_wait(void *ctx, int timeout)
341 struct tcp_ctx *tctx = ctx;
345 int error, flags, ret;
347 PJDLOG_ASSERT(tctx != NULL);
348 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
349 PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_CLIENT);
350 PJDLOG_ASSERT(!tctx->tc_wait_called);
351 PJDLOG_ASSERT(tctx->tc_fd >= 0);
352 PJDLOG_ASSERT(timeout >= 0);
358 FD_SET(tctx->tc_fd, &fdset);
359 ret = select(tctx->tc_fd + 1, NULL, &fdset, NULL, &tv);
363 } else if (ret == -1) {
367 pjdlog_common(LOG_DEBUG, 1, errno, "select() failed");
370 PJDLOG_ASSERT(ret > 0);
371 PJDLOG_ASSERT(FD_ISSET(tctx->tc_fd, &fdset));
372 esize = sizeof(error);
373 if (getsockopt(tctx->tc_fd, SOL_SOCKET, SO_ERROR, &error,
376 pjdlog_common(LOG_DEBUG, 1, errno,
377 "getsockopt(SO_ERROR) failed");
381 pjdlog_common(LOG_DEBUG, 1, error,
382 "getsockopt(SO_ERROR) returned error");
386 tctx->tc_wait_called = true;
388 flags = fcntl(tctx->tc_fd, F_GETFL);
392 pjdlog_common(LOG_DEBUG, 1, errno, "fcntl(F_GETFL) failed");
395 flags &= ~O_NONBLOCK;
396 if (fcntl(tctx->tc_fd, F_SETFL, flags) == -1) {
399 pjdlog_common(LOG_DEBUG, 1, errno,
400 "fcntl(F_SETFL, ~O_NONBLOCK) failed");
406 tcp_server(const char *addr, void **ctxp)
408 struct tcp_ctx *tctx;
411 error = tcp_setup_new(addr, TCP_SIDE_SERVER_LISTEN, &tctx);
416 /* Ignore failure. */
417 (void)setsockopt(tctx->tc_fd, SOL_SOCKET, SO_REUSEADDR, &val,
420 PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
422 if (bind(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
423 sockaddr_len(&tctx->tc_sa)) == -1) {
428 if (listen(tctx->tc_fd, 8) == -1) {
440 tcp_accept(void *ctx, void **newctxp)
442 struct tcp_ctx *tctx = ctx;
443 struct tcp_ctx *newtctx;
447 PJDLOG_ASSERT(tctx != NULL);
448 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
449 PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_SERVER_LISTEN);
450 PJDLOG_ASSERT(tctx->tc_fd >= 0);
451 PJDLOG_ASSERT(tctx->tc_sa.ss_family != AF_UNSPEC);
453 newtctx = malloc(sizeof(*newtctx));
457 fromlen = sockaddr_len(&tctx->tc_sa);
458 newtctx->tc_fd = accept(tctx->tc_fd, (struct sockaddr *)&tctx->tc_sa,
460 if (newtctx->tc_fd < 0) {
466 newtctx->tc_wait_called = true;
467 newtctx->tc_side = TCP_SIDE_SERVER_WORK;
468 newtctx->tc_magic = TCP_CTX_MAGIC;
475 tcp_wrap(int fd, bool client, void **ctxp)
477 struct tcp_ctx *tctx;
479 PJDLOG_ASSERT(fd >= 0);
480 PJDLOG_ASSERT(ctxp != NULL);
482 tctx = malloc(sizeof(*tctx));
487 tctx->tc_sa.ss_family = AF_UNSPEC;
488 tctx->tc_wait_called = (client ? false : true);
489 tctx->tc_side = (client ? TCP_SIDE_CLIENT : TCP_SIDE_SERVER_WORK);
490 tctx->tc_magic = TCP_CTX_MAGIC;
497 tcp_send(void *ctx, const unsigned char *data, size_t size, int fd)
499 struct tcp_ctx *tctx = ctx;
501 PJDLOG_ASSERT(tctx != NULL);
502 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
503 PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_CLIENT ||
504 tctx->tc_side == TCP_SIDE_SERVER_WORK);
505 PJDLOG_ASSERT(tctx->tc_wait_called);
506 PJDLOG_ASSERT(tctx->tc_fd >= 0);
507 PJDLOG_ASSERT(fd == -1);
509 return (proto_common_send(tctx->tc_fd, data, size, -1));
513 tcp_recv(void *ctx, unsigned char *data, size_t size, int *fdp)
515 struct tcp_ctx *tctx = ctx;
517 PJDLOG_ASSERT(tctx != NULL);
518 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
519 PJDLOG_ASSERT(tctx->tc_side == TCP_SIDE_CLIENT ||
520 tctx->tc_side == TCP_SIDE_SERVER_WORK);
521 PJDLOG_ASSERT(tctx->tc_wait_called);
522 PJDLOG_ASSERT(tctx->tc_fd >= 0);
523 PJDLOG_ASSERT(fdp == NULL);
525 return (proto_common_recv(tctx->tc_fd, data, size, NULL));
529 tcp_descriptor(const void *ctx)
531 const struct tcp_ctx *tctx = ctx;
533 PJDLOG_ASSERT(tctx != NULL);
534 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
536 return (tctx->tc_fd);
540 tcp_address_match(const void *ctx, const char *addr)
542 const struct tcp_ctx *tctx = ctx;
543 struct sockaddr_storage sa1, sa2;
546 PJDLOG_ASSERT(tctx != NULL);
547 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
549 if (tcp_addr(addr, atoi(proto_get("tcp:port")), &sa1) != 0)
553 if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa2, &salen) < 0)
556 if (sa1.ss_family != sa2.ss_family)
559 #ifdef HAVE_SOCKADDR_STORAGE_SS_LEN
560 if (sa1.ss_len != sa2.ss_len)
564 switch (sa1.ss_family) {
567 struct sockaddr_in *sin1, *sin2;
569 sin1 = (struct sockaddr_in *)&sa1;
570 sin2 = (struct sockaddr_in *)&sa2;
572 return (memcmp(&sin1->sin_addr, &sin2->sin_addr,
573 sizeof(sin1->sin_addr)) == 0);
577 struct sockaddr_in6 *sin1, *sin2;
579 sin1 = (struct sockaddr_in6 *)&sa1;
580 sin2 = (struct sockaddr_in6 *)&sa2;
582 return (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
583 sizeof(sin1->sin6_addr)) == 0);
592 sockaddr_to_string(const void *sa, char *buf, size_t size)
594 const struct sockaddr_storage *ss;
596 ss = (const struct sockaddr_storage * const *)sa;
597 switch (ss->ss_family) {
600 char addr[INET_ADDRSTRLEN];
601 const struct sockaddr_in *sin;
604 sin = (const struct sockaddr_in *)ss;
605 port = ntohs(sin->sin_port);
606 if (inet_ntop(ss->ss_family, &sin->sin_addr, addr,
607 sizeof(addr)) == NULL) {
608 PJDLOG_ABORT("inet_ntop(AF_INET) failed: %s.",
611 snprintf(buf, size, "%s:%u", addr, port);
616 char addr[INET6_ADDRSTRLEN];
617 const struct sockaddr_in6 *sin;
620 sin = (const struct sockaddr_in6 *)ss;
621 port = ntohs(sin->sin6_port);
622 if (inet_ntop(ss->ss_family, &sin->sin6_addr, addr,
623 sizeof(addr)) == NULL) {
624 PJDLOG_ABORT("inet_ntop(AF_INET6) failed: %s.",
627 snprintf(buf, size, "[%s]:%u", addr, port);
631 snprintf(buf, size, "[unsupported family %hhu]",
636 #endif /* !__FreeBSD__ */
639 tcp_local_address(const void *ctx, char *addr, size_t size)
641 const struct tcp_ctx *tctx = ctx;
642 struct sockaddr_storage sa;
645 PJDLOG_ASSERT(tctx != NULL);
646 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
649 if (getsockname(tctx->tc_fd, (struct sockaddr *)&sa, &salen) < 0) {
650 PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
654 PJDLOG_VERIFY(snprintf(addr, size, "tcp://%S", &sa) < (ssize_t)size);
656 strlcpy(addr, "tcp://", size);
658 sockaddr_to_string(&sa, addr + 6, size - 6);
663 tcp_remote_address(const void *ctx, char *addr, size_t size)
665 const struct tcp_ctx *tctx = ctx;
666 struct sockaddr_storage sa;
669 PJDLOG_ASSERT(tctx != NULL);
670 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
673 if (getpeername(tctx->tc_fd, (struct sockaddr *)&sa, &salen) < 0) {
674 PJDLOG_VERIFY(strlcpy(addr, "N/A", size) < size);
678 PJDLOG_VERIFY(snprintf(addr, size, "tcp://%S", &sa) < (ssize_t)size);
680 strlcpy(addr, "tcp://", size);
682 sockaddr_to_string(&sa, addr + 6, size - 6);
689 struct tcp_ctx *tctx = ctx;
691 PJDLOG_ASSERT(tctx != NULL);
692 PJDLOG_ASSERT(tctx->tc_magic == TCP_CTX_MAGIC);
694 if (tctx->tc_fd >= 0)
700 static struct proto tcp_proto = {
702 .prt_connect = tcp_connect,
703 .prt_connect_wait = tcp_connect_wait,
704 .prt_server = tcp_server,
705 .prt_accept = tcp_accept,
706 .prt_wrap = tcp_wrap,
707 .prt_send = tcp_send,
708 .prt_recv = tcp_recv,
709 .prt_descriptor = tcp_descriptor,
710 .prt_address_match = tcp_address_match,
711 .prt_local_address = tcp_local_address,
712 .prt_remote_address = tcp_remote_address,
713 .prt_close = tcp_close
716 static __constructor void
720 proto_register(&tcp_proto, true);