2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/endian.h>
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
46 #include <arpa/inet.h>
59 #define GGATED_EXPORT_FILE "/etc/gg.exports"
61 struct ggd_connection {
63 unsigned c_sectorsize;
64 unsigned c_flags; /* flags (RO/RW) */
72 LIST_ENTRY(ggd_connection) c_next;
76 struct g_gate_hdr r_hdr;
78 TAILQ_ENTRY(ggd_request) r_next;
80 #define r_cmd r_hdr.gh_cmd
81 #define r_offset r_hdr.gh_offset
82 #define r_length r_hdr.gh_length
83 #define r_error r_hdr.gh_error
86 char *e_path; /* path to device/file */
87 in_addr_t e_ip; /* remote IP address */
88 in_addr_t e_mask; /* IP mask */
89 unsigned e_flags; /* flags (RO/RW) */
90 SLIST_ENTRY(ggd_export) e_next;
93 static const char *exports_file = GGATED_EXPORT_FILE;
94 static int got_sighup = 0;
95 static in_addr_t bindaddr;
97 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
98 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
99 static pthread_mutex_t inqueue_mtx, outqueue_mtx;
100 static pthread_cond_t inqueue_cond, outqueue_cond;
102 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
103 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
105 static void *recv_thread(void *arg);
106 static void *disk_thread(void *arg);
107 static void *send_thread(void *arg);
113 fprintf(stderr, "usage: %s [-nv] [-a address] [-p port] [-R rcvbuf] "
114 "[-S sndbuf] [exports file]\n", getprogname());
123 snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
132 countmask(unsigned m)
139 mask = 1 << (32 - m);
147 line_parse(char *line, unsigned lineno)
149 struct ggd_export *ex;
150 char *word, *path, *sflags;
151 unsigned flags, i, vmask;
154 ip = mask = flags = vmask = 0;
158 for (i = 0, word = strtok(line, " \t"); word != NULL;
159 i++, word = strtok(NULL, " \t")) {
161 case 0: /* IP address or host name */
162 ip = g_gate_str2ip(strsep(&word, "/"));
163 if (ip == INADDR_NONE) {
164 g_gate_xlog("Invalid IP/host name at line %u.",
172 vmask = strtoul(word, NULL, 10);
173 if (vmask == 0 && errno != 0) {
174 g_gate_xlog("Invalid IP mask value at "
177 if ((unsigned)vmask > 32) {
178 g_gate_xlog("Invalid IP mask value at line %u.",
182 mask = countmask(vmask);
185 if (strcasecmp("rd", word) == 0 ||
186 strcasecmp("ro", word) == 0) {
188 } else if (strcasecmp("wo", word) == 0) {
190 } else if (strcasecmp("rw", word) == 0) {
193 g_gate_xlog("Invalid value in flags field at "
199 if (strlen(word) >= MAXPATHLEN) {
200 g_gate_xlog("Path too long at line %u. ",
206 g_gate_xlog("Too many arguments at line %u. ", lineno);
210 g_gate_xlog("Too few arguments at line %u.", lineno);
212 ex = malloc(sizeof(*ex));
214 g_gate_xlog("Not enough memory.");
215 ex->e_path = strdup(path);
216 if (ex->e_path == NULL)
217 g_gate_xlog("Not enough memory.");
219 /* Made 'and' here. */
220 ex->e_ip = (ip & mask);
224 SLIST_INSERT_HEAD(&exports, ex, e_next);
226 g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
227 ip2str(ex->e_ip), vmask, path, sflags);
233 struct ggd_export *ex;
235 while (!SLIST_EMPTY(&exports)) {
236 ex = SLIST_FIRST(&exports);
237 SLIST_REMOVE_HEAD(&exports, e_next);
242 #define EXPORTS_LINE_SIZE 2048
246 char buf[EXPORTS_LINE_SIZE], *line;
247 unsigned lineno = 0, objs = 0, len;
252 fd = fopen(exports_file, "r");
254 g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
258 g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
261 if (fgets(buf, sizeof(buf), fd) == NULL) {
265 g_gate_xlog("Error while reading exports file: %s.",
269 /* Increase line count. */
272 /* Skip spaces and tabs. */
273 for (line = buf; *line == ' ' || *line == '\t'; ++line)
276 /* Empty line, comment or empty line at the end of file. */
277 if (*line == '\n' || *line == '#' || *line == '\0')
281 if (line[len - 1] == '\n') {
282 /* Remove new line char. */
283 line[len - 1] = '\0';
286 g_gate_xlog("Line %u too long.", lineno);
289 line_parse(line, lineno);
296 g_gate_xlog("There are no objects to export.");
298 g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
302 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
303 struct ggd_connection *conn)
305 char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
306 int error = 0, flags;
308 strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
309 strlcat(ipmask, "/", sizeof(ipmask));
310 strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
311 if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
312 if (ex->e_flags == O_WRONLY) {
313 g_gate_log(LOG_WARNING, "Read-only access requested, "
314 "but %s (%s) is exported write-only.", ex->e_path,
318 conn->c_flags |= GGATE_FLAG_RDONLY;
320 } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
321 if (ex->e_flags == O_RDONLY) {
322 g_gate_log(LOG_WARNING, "Write-only access requested, "
323 "but %s (%s) is exported read-only.", ex->e_path,
327 conn->c_flags |= GGATE_FLAG_WRONLY;
330 if (ex->e_flags == O_RDONLY) {
331 g_gate_log(LOG_WARNING, "Read-write access requested, "
332 "but %s (%s) is exported read-only.", ex->e_path,
335 } else if (ex->e_flags == O_WRONLY) {
336 g_gate_log(LOG_WARNING, "Read-write access requested, "
337 "but %s (%s) is exported write-only.", ex->e_path,
342 if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
344 else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
348 conn->c_diskfd = open(ex->e_path, flags);
349 if (conn->c_diskfd == -1) {
351 g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
358 static struct ggd_export *
359 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
360 struct ggd_connection *conn)
362 struct ggd_export *ex;
366 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
367 SLIST_FOREACH(ex, &exports, e_next) {
368 if ((ip & ex->e_mask) != ex->e_ip) {
369 g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
373 if (strcmp(cinit->gc_path, ex->e_path) != 0) {
374 g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
378 error = exports_check(ex, cinit, conn);
386 g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
393 * Remove timed out connections.
396 connection_cleanups(void)
398 struct ggd_connection *conn, *tconn;
402 LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
403 if (now - conn->c_birthtime > 10) {
404 LIST_REMOVE(conn, c_next);
405 g_gate_log(LOG_NOTICE,
406 "Connection from %s [%s] removed.",
407 ip2str(conn->c_srcip), conn->c_path);
408 close(conn->c_diskfd);
409 close(conn->c_sendfd);
410 close(conn->c_recvfd);
417 static struct ggd_connection *
418 connection_find(struct g_gate_cinit *cinit)
420 struct ggd_connection *conn;
422 LIST_FOREACH(conn, &connections, c_next) {
423 if (conn->c_token == cinit->gc_token)
429 static struct ggd_connection *
430 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
432 struct ggd_connection *conn;
436 * First, look for old connections.
437 * We probably should do it every X seconds, but what for?
438 * It is only dangerous if an attacker wants to overload connections
439 * queue, so here is a good place to do the cleanups.
441 connection_cleanups();
443 conn = malloc(sizeof(*conn));
446 conn->c_path = strdup(cinit->gc_path);
447 if (conn->c_path == NULL) {
451 conn->c_token = cinit->gc_token;
452 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
454 conn->c_sendfd = conn->c_recvfd = -1;
455 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
456 conn->c_sendfd = sfd;
458 conn->c_recvfd = sfd;
459 conn->c_mediasize = 0;
460 conn->c_sectorsize = 0;
461 time(&conn->c_birthtime);
462 conn->c_flags = cinit->gc_flags;
463 LIST_INSERT_HEAD(&connections, conn, c_next);
464 g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
470 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
471 struct sockaddr *s, int sfd)
475 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
476 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
477 if (conn->c_sendfd != -1) {
478 g_gate_log(LOG_WARNING,
479 "Send socket already exists [%s, %s].", ip2str(ip),
483 conn->c_sendfd = sfd;
485 if (conn->c_recvfd != -1) {
486 g_gate_log(LOG_WARNING,
487 "Receive socket already exists [%s, %s].",
488 ip2str(ip), conn->c_path);
491 conn->c_recvfd = sfd;
493 g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
499 * Remove one socket from the given connection or the whole
500 * connection if sfd == -1.
503 connection_remove(struct ggd_connection *conn)
506 LIST_REMOVE(conn, c_next);
507 g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
508 ip2str(conn->c_srcip), conn->c_path);
509 if (conn->c_sendfd != -1)
510 close(conn->c_sendfd);
511 if (conn->c_recvfd != -1)
512 close(conn->c_recvfd);
518 connection_ready(struct ggd_connection *conn)
521 return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
525 connection_launch(struct ggd_connection *conn)
533 else if (pid == -1) {
534 g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
537 g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
540 * Create condition variables and mutexes for in-queue and out-queue
543 error = pthread_mutex_init(&inqueue_mtx, NULL);
545 g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
548 error = pthread_cond_init(&inqueue_cond, NULL);
550 g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
553 error = pthread_mutex_init(&outqueue_mtx, NULL);
555 g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
558 error = pthread_cond_init(&outqueue_cond, NULL);
560 g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
566 * recvtd - thread for receiving I/O request
567 * diskio - thread for doing I/O request
568 * sendtd - thread for sending I/O requests back
570 error = pthread_create(&td, NULL, send_thread, conn);
572 g_gate_xlog("pthread_create(send_thread): %s.",
575 error = pthread_create(&td, NULL, recv_thread, conn);
577 g_gate_xlog("pthread_create(recv_thread): %s.",
584 sendfail(int sfd, int error, const char *fmt, ...)
586 struct g_gate_sinit sinit;
590 sinit.gs_error = error;
591 g_gate_swap2n_sinit(&sinit);
592 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
593 g_gate_swap2h_sinit(&sinit);
594 if (data != sizeof(sinit)) {
595 g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
601 g_gate_vlog(LOG_WARNING, fmt, ap);
607 malloc_waitok(size_t size)
611 while ((p = malloc(size)) == NULL) {
612 g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
619 recv_thread(void *arg)
621 struct ggd_connection *conn;
622 struct ggd_request *req;
627 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
633 req = malloc_waitok(sizeof(*req));
634 data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
637 g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
639 } else if (data == -1) {
640 g_gate_xlog("Error while receiving hdr packet: %s.",
642 } else if (data != sizeof(req->r_hdr)) {
643 g_gate_xlog("Malformed hdr packet received.");
645 g_gate_log(LOG_DEBUG, "Received hdr packet.");
646 g_gate_swap2h_hdr(&req->r_hdr);
648 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
649 (intmax_t)req->r_offset, (unsigned)req->r_length);
652 * Allocate memory for data.
654 req->r_data = malloc_waitok(req->r_length);
657 * Receive data to write for WRITE request.
659 if (req->r_cmd == GGATE_CMD_WRITE) {
660 g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
662 data = g_gate_recv(fd, req->r_data, req->r_length,
665 g_gate_xlog("Error while receiving data: %s.",
671 * Put the request onto the incoming queue.
673 error = pthread_mutex_lock(&inqueue_mtx);
675 TAILQ_INSERT_TAIL(&inqueue, req, r_next);
676 error = pthread_cond_signal(&inqueue_cond);
678 error = pthread_mutex_unlock(&inqueue_mtx);
684 disk_thread(void *arg)
686 struct ggd_connection *conn;
687 struct ggd_request *req;
692 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
696 * Get a request from the incoming queue.
698 error = pthread_mutex_lock(&inqueue_mtx);
700 while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
701 error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
704 TAILQ_REMOVE(&inqueue, req, r_next);
705 error = pthread_mutex_unlock(&inqueue_mtx);
711 assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
712 assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
713 assert((req->r_offset % conn->c_sectorsize) == 0);
714 assert((req->r_length % conn->c_sectorsize) == 0);
716 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
717 (intmax_t)req->r_offset, (unsigned)req->r_length);
723 switch (req->r_cmd) {
725 data = pread(fd, req->r_data, req->r_length,
728 case GGATE_CMD_WRITE:
729 data = pwrite(fd, req->r_data, req->r_length,
731 /* Free data memory here - better sooner. */
736 if (data != (ssize_t)req->r_length) {
737 /* Report short reads/writes as I/O errors. */
740 g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
741 req->r_error = errno;
742 if (req->r_data != NULL) {
749 * Put the request onto the outgoing queue.
751 error = pthread_mutex_lock(&outqueue_mtx);
753 TAILQ_INSERT_TAIL(&outqueue, req, r_next);
754 error = pthread_cond_signal(&outqueue_cond);
756 error = pthread_mutex_unlock(&outqueue_mtx);
765 send_thread(void *arg)
767 struct ggd_connection *conn;
768 struct ggd_request *req;
773 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
777 * Get a request from the outgoing queue.
779 error = pthread_mutex_lock(&outqueue_mtx);
781 while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
782 error = pthread_cond_wait(&outqueue_cond,
786 TAILQ_REMOVE(&outqueue, req, r_next);
787 error = pthread_mutex_unlock(&outqueue_mtx);
790 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
791 (intmax_t)req->r_offset, (unsigned)req->r_length);
796 g_gate_swap2n_hdr(&req->r_hdr);
797 if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
798 g_gate_xlog("Error while sending hdr packet: %s.",
801 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
802 g_gate_swap2h_hdr(&req->r_hdr);
803 if (req->r_data != NULL) {
804 data = g_gate_send(fd, req->r_data, req->r_length, 0);
805 if (data != (ssize_t)req->r_length) {
806 g_gate_xlog("Error while sending data: %s.",
809 g_gate_log(LOG_DEBUG,
810 "Sent %zd bytes (offset=%ju, size=%zu).", data,
811 (uintmax_t)req->r_offset, (size_t)req->r_length);
822 log_connection(struct sockaddr *from)
826 ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
827 g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
831 handshake(struct sockaddr *from, int sfd)
833 struct g_gate_version ver;
834 struct g_gate_cinit cinit;
835 struct g_gate_sinit sinit;
836 struct ggd_connection *conn;
837 struct ggd_export *ex;
840 log_connection(from);
842 * Phase 1: Version verification.
844 g_gate_log(LOG_DEBUG, "Receiving version packet.");
845 data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
846 g_gate_swap2h_version(&ver);
847 if (data != sizeof(ver)) {
848 g_gate_log(LOG_WARNING, "Malformed version packet.");
851 g_gate_log(LOG_DEBUG, "Version packet received.");
852 if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
853 g_gate_log(LOG_WARNING, "Invalid magic field.");
856 if (ver.gv_version != GGATE_VERSION) {
857 g_gate_log(LOG_WARNING, "Version %u is not supported.",
862 g_gate_swap2n_version(&ver);
863 data = g_gate_send(sfd, &ver, sizeof(ver), 0);
864 g_gate_swap2h_version(&ver);
866 sendfail(sfd, errno, "Error while sending version packet: %s.",
872 * Phase 2: Request verification.
874 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
875 data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
876 g_gate_swap2h_cinit(&cinit);
877 if (data != sizeof(cinit)) {
878 g_gate_log(LOG_WARNING, "Malformed initial packet.");
881 g_gate_log(LOG_DEBUG, "Initial packet received.");
882 conn = connection_find(&cinit);
885 * Connection should already exists.
887 g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
888 (unsigned long)conn->c_token);
889 if (connection_add(conn, &cinit, from, sfd) == -1) {
890 connection_remove(conn);
895 * New connection, allocate space.
897 conn = connection_new(&cinit, from, sfd);
899 sendfail(sfd, ENOMEM,
900 "Cannot allocate new connection.");
903 g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
904 (unsigned long)conn->c_token);
907 ex = exports_find(from, &cinit, conn);
909 connection_remove(conn);
910 sendfail(sfd, errno, NULL);
913 if (conn->c_mediasize == 0) {
914 conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
915 conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
917 sinit.gs_mediasize = conn->c_mediasize;
918 sinit.gs_sectorsize = conn->c_sectorsize;
921 g_gate_log(LOG_DEBUG, "Sending initial packet.");
923 g_gate_swap2n_sinit(&sinit);
924 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
925 g_gate_swap2h_sinit(&sinit);
927 sendfail(sfd, errno, "Error while sending initial packet: %s.",
932 if (connection_ready(conn)) {
933 connection_launch(conn);
934 connection_remove(conn);
940 huphandler(int sig __unused)
947 main(int argc, char *argv[])
949 struct sockaddr_in serv;
950 struct sockaddr from;
955 bindaddr = htonl(INADDR_ANY);
960 ch = getopt(argc, argv, "a:hnp:R:S:v");
965 bindaddr = g_gate_str2ip(optarg);
966 if (bindaddr == INADDR_NONE) {
968 "Invalid IP/host name to bind to.");
976 port = strtoul(optarg, NULL, 10);
977 if (port == 0 && errno != 0)
978 errx(EXIT_FAILURE, "Invalid port.");
982 rcvbuf = strtoul(optarg, NULL, 10);
983 if (rcvbuf == 0 && errno != 0)
984 errx(EXIT_FAILURE, "Invalid rcvbuf.");
988 sndbuf = strtoul(optarg, NULL, 10);
989 if (sndbuf == 0 && errno != 0)
990 errx(EXIT_FAILURE, "Invalid sndbuf.");
1003 if (argv[0] != NULL)
1004 exports_file = argv[0];
1007 if (!g_gate_verbose) {
1008 /* Run in daemon mode. */
1009 if (daemon(0, 0) == -1)
1010 g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1013 signal(SIGCHLD, SIG_IGN);
1015 sfd = socket(AF_INET, SOCK_STREAM, 0);
1017 g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1018 bzero(&serv, sizeof(serv));
1019 serv.sin_family = AF_INET;
1020 serv.sin_addr.s_addr = bindaddr;
1021 serv.sin_port = htons(port);
1023 g_gate_socket_settings(sfd);
1025 if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1026 g_gate_xlog("bind(): %s.", strerror(errno));
1027 if (listen(sfd, 5) == -1)
1028 g_gate_xlog("listen(): %s.", strerror(errno));
1030 g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1032 signal(SIGHUP, huphandler);
1035 fromlen = sizeof(from);
1036 tmpsfd = accept(sfd, &from, &fromlen);
1038 g_gate_xlog("accept(): %s.", strerror(errno));
1045 if (!handshake(&from, tmpsfd))