2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/ioctl.h>
36 #include <sys/queue.h>
37 #include <sys/socket.h>
40 #include <arpa/inet.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
64 #define GGATED_EXPORT_FILE "/etc/gg.exports"
66 struct ggd_connection {
68 unsigned c_sectorsize;
69 unsigned c_flags; /* flags (RO/RW) */
77 LIST_ENTRY(ggd_connection) c_next;
81 struct g_gate_hdr r_hdr;
83 TAILQ_ENTRY(ggd_request) r_next;
85 #define r_cmd r_hdr.gh_cmd
86 #define r_offset r_hdr.gh_offset
87 #define r_length r_hdr.gh_length
88 #define r_error r_hdr.gh_error
91 char *e_path; /* path to device/file */
92 in_addr_t e_ip; /* remote IP address */
93 in_addr_t e_mask; /* IP mask */
94 unsigned e_flags; /* flags (RO/RW) */
95 SLIST_ENTRY(ggd_export) e_next;
98 static const char *exports_file = GGATED_EXPORT_FILE;
99 static int got_sighup = 0;
100 static in_addr_t bindaddr;
102 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
103 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
104 static pthread_mutex_t inqueue_mtx, outqueue_mtx;
105 static pthread_cond_t inqueue_cond, outqueue_cond;
107 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
108 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
110 static void *recv_thread(void *arg);
111 static void *disk_thread(void *arg);
112 static void *send_thread(void *arg);
118 fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] "
119 "[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname());
128 snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
137 countmask(unsigned m)
144 mask = 1 << (32 - m);
152 line_parse(char *line, unsigned lineno)
154 struct ggd_export *ex;
155 char *word, *path, *sflags;
156 unsigned flags, i, vmask;
159 ip = mask = flags = vmask = 0;
163 for (i = 0, word = strtok(line, " \t"); word != NULL;
164 i++, word = strtok(NULL, " \t")) {
166 case 0: /* IP address or host name */
167 ip = g_gate_str2ip(strsep(&word, "/"));
168 if (ip == INADDR_NONE) {
169 g_gate_xlog("Invalid IP/host name at line %u.",
177 vmask = strtoul(word, NULL, 10);
178 if (vmask == 0 && errno != 0) {
179 g_gate_xlog("Invalid IP mask value at "
182 if ((unsigned)vmask > 32) {
183 g_gate_xlog("Invalid IP mask value at line %u.",
187 mask = countmask(vmask);
190 if (strcasecmp("rd", word) == 0 ||
191 strcasecmp("ro", word) == 0) {
193 } else if (strcasecmp("wo", word) == 0) {
195 } else if (strcasecmp("rw", word) == 0) {
198 g_gate_xlog("Invalid value in flags field at "
204 if (strlen(word) >= MAXPATHLEN) {
205 g_gate_xlog("Path too long at line %u. ",
211 g_gate_xlog("Too many arguments at line %u. ", lineno);
215 g_gate_xlog("Too few arguments at line %u.", lineno);
217 ex = malloc(sizeof(*ex));
219 g_gate_xlog("Not enough memory.");
220 ex->e_path = strdup(path);
221 if (ex->e_path == NULL)
222 g_gate_xlog("Not enough memory.");
224 /* Made 'and' here. */
225 ex->e_ip = (ip & mask);
229 SLIST_INSERT_HEAD(&exports, ex, e_next);
231 g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
232 ip2str(ex->e_ip), vmask, path, sflags);
238 struct ggd_export *ex;
240 while (!SLIST_EMPTY(&exports)) {
241 ex = SLIST_FIRST(&exports);
242 SLIST_REMOVE_HEAD(&exports, e_next);
247 #define EXPORTS_LINE_SIZE 2048
251 char buf[EXPORTS_LINE_SIZE], *line;
252 unsigned lineno = 0, objs = 0, len;
257 fd = fopen(exports_file, "r");
259 g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
263 g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
266 if (fgets(buf, sizeof(buf), fd) == NULL) {
270 g_gate_xlog("Error while reading exports file: %s.",
274 /* Increase line count. */
277 /* Skip spaces and tabs. */
278 for (line = buf; *line == ' ' || *line == '\t'; ++line)
281 /* Empty line, comment or empty line at the end of file. */
282 if (*line == '\n' || *line == '#' || *line == '\0')
286 if (line[len - 1] == '\n') {
287 /* Remove new line char. */
288 line[len - 1] = '\0';
291 g_gate_xlog("Line %u too long.", lineno);
294 line_parse(line, lineno);
301 g_gate_xlog("There are no objects to export.");
303 g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
307 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
308 struct ggd_connection *conn)
310 char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
311 int error = 0, flags;
313 strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
314 strlcat(ipmask, "/", sizeof(ipmask));
315 strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
316 if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
317 if (ex->e_flags == O_WRONLY) {
318 g_gate_log(LOG_WARNING, "Read-only access requested, "
319 "but %s (%s) is exported write-only.", ex->e_path,
323 conn->c_flags |= GGATE_FLAG_RDONLY;
325 } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
326 if (ex->e_flags == O_RDONLY) {
327 g_gate_log(LOG_WARNING, "Write-only access requested, "
328 "but %s (%s) is exported read-only.", ex->e_path,
332 conn->c_flags |= GGATE_FLAG_WRONLY;
335 if (ex->e_flags == O_RDONLY) {
336 g_gate_log(LOG_WARNING, "Read-write access requested, "
337 "but %s (%s) is exported read-only.", ex->e_path,
340 } else if (ex->e_flags == O_WRONLY) {
341 g_gate_log(LOG_WARNING, "Read-write access requested, "
342 "but %s (%s) is exported write-only.", ex->e_path,
347 if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
349 else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
353 if (conn->c_diskfd != -1) {
354 if (strcmp(conn->c_path, ex->e_path) != 0) {
355 g_gate_log(LOG_ERR, "old %s and new %s: "
356 "Path mismatch during handshakes.",
357 conn->c_path, ex->e_path);
363 conn->c_diskfd = open(ex->e_path, flags);
364 if (conn->c_diskfd == -1) {
366 g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
373 static struct ggd_export *
374 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
375 struct ggd_connection *conn)
377 struct ggd_export *ex;
381 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
382 SLIST_FOREACH(ex, &exports, e_next) {
383 if ((ip & ex->e_mask) != ex->e_ip) {
384 g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
388 if (strcmp(cinit->gc_path, ex->e_path) != 0) {
389 g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
393 error = exports_check(ex, cinit, conn);
401 g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
408 * Remove timed out connections.
411 connection_cleanups(void)
413 struct ggd_connection *conn, *tconn;
417 LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
418 if (now - conn->c_birthtime > 10) {
419 LIST_REMOVE(conn, c_next);
420 g_gate_log(LOG_NOTICE,
421 "Connection from %s [%s] removed.",
422 ip2str(conn->c_srcip), conn->c_path);
423 close(conn->c_diskfd);
424 close(conn->c_sendfd);
425 close(conn->c_recvfd);
432 static struct ggd_connection *
433 connection_find(struct g_gate_cinit *cinit)
435 struct ggd_connection *conn;
437 LIST_FOREACH(conn, &connections, c_next) {
438 if (conn->c_token == cinit->gc_token)
444 static struct ggd_connection *
445 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
447 struct ggd_connection *conn;
451 * First, look for old connections.
452 * We probably should do it every X seconds, but what for?
453 * It is only dangerous if an attacker wants to overload connections
454 * queue, so here is a good place to do the cleanups.
456 connection_cleanups();
458 conn = malloc(sizeof(*conn));
461 conn->c_path = strdup(cinit->gc_path);
462 if (conn->c_path == NULL) {
466 conn->c_token = cinit->gc_token;
467 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
469 conn->c_diskfd = conn->c_sendfd = conn->c_recvfd = -1;
470 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
471 conn->c_sendfd = sfd;
473 conn->c_recvfd = sfd;
474 conn->c_mediasize = 0;
475 conn->c_sectorsize = 0;
476 time(&conn->c_birthtime);
477 conn->c_flags = cinit->gc_flags;
478 LIST_INSERT_HEAD(&connections, conn, c_next);
479 g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
485 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
486 struct sockaddr *s, int sfd)
490 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
491 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
492 if (conn->c_sendfd != -1) {
493 g_gate_log(LOG_WARNING,
494 "Send socket already exists [%s, %s].", ip2str(ip),
498 conn->c_sendfd = sfd;
500 if (conn->c_recvfd != -1) {
501 g_gate_log(LOG_WARNING,
502 "Receive socket already exists [%s, %s].",
503 ip2str(ip), conn->c_path);
506 conn->c_recvfd = sfd;
508 g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
514 * Remove one socket from the given connection or the whole
515 * connection if sfd == -1.
518 connection_remove(struct ggd_connection *conn)
521 LIST_REMOVE(conn, c_next);
522 g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
523 ip2str(conn->c_srcip), conn->c_path);
524 if (conn->c_diskfd != -1)
525 close(conn->c_diskfd);
526 if (conn->c_sendfd != -1)
527 close(conn->c_sendfd);
528 if (conn->c_recvfd != -1)
529 close(conn->c_recvfd);
535 connection_ready(struct ggd_connection *conn)
538 return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
542 connection_launch(struct ggd_connection *conn)
550 else if (pid == -1) {
551 g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
554 g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
557 * Create condition variables and mutexes for in-queue and out-queue
560 error = pthread_mutex_init(&inqueue_mtx, NULL);
562 g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
565 error = pthread_cond_init(&inqueue_cond, NULL);
567 g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
570 error = pthread_mutex_init(&outqueue_mtx, NULL);
572 g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
575 error = pthread_cond_init(&outqueue_cond, NULL);
577 g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
583 * recvtd - thread for receiving I/O request
584 * diskio - thread for doing I/O request
585 * sendtd - thread for sending I/O requests back
587 error = pthread_create(&td, NULL, send_thread, conn);
589 g_gate_xlog("pthread_create(send_thread): %s.",
592 error = pthread_create(&td, NULL, recv_thread, conn);
594 g_gate_xlog("pthread_create(recv_thread): %s.",
601 sendfail(int sfd, int error, const char *fmt, ...)
603 struct g_gate_sinit sinit;
607 memset(&sinit, 0, sizeof(sinit));
608 sinit.gs_error = error;
609 g_gate_swap2n_sinit(&sinit);
610 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
611 g_gate_swap2h_sinit(&sinit);
612 if (data != sizeof(sinit)) {
613 g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
619 g_gate_vlog(LOG_WARNING, fmt, ap);
625 malloc_waitok(size_t size)
629 while ((p = malloc(size)) == NULL) {
630 g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
637 recv_thread(void *arg)
639 struct ggd_connection *conn;
640 struct ggd_request *req;
645 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
651 req = malloc_waitok(sizeof(*req));
652 data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
655 g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
657 } else if (data == -1) {
658 g_gate_xlog("Error while receiving hdr packet: %s.",
660 } else if (data != sizeof(req->r_hdr)) {
661 g_gate_xlog("Malformed hdr packet received.");
663 g_gate_log(LOG_DEBUG, "Received hdr packet.");
664 g_gate_swap2h_hdr(&req->r_hdr);
666 g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32,
667 __func__, req->r_offset, req->r_length);
670 * Allocate memory for data.
672 req->r_data = malloc_waitok(req->r_length);
675 * Receive data to write for WRITE request.
677 if (req->r_cmd == GGATE_CMD_WRITE) {
678 g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
680 data = g_gate_recv(fd, req->r_data, req->r_length,
683 g_gate_xlog("Error while receiving data: %s.",
689 * Put the request onto the incoming queue.
691 error = pthread_mutex_lock(&inqueue_mtx);
693 TAILQ_INSERT_TAIL(&inqueue, req, r_next);
694 error = pthread_cond_signal(&inqueue_cond);
696 error = pthread_mutex_unlock(&inqueue_mtx);
702 disk_thread(void *arg)
704 struct ggd_connection *conn;
705 struct ggd_request *req;
710 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
714 * Get a request from the incoming queue.
716 error = pthread_mutex_lock(&inqueue_mtx);
718 while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
719 error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
722 TAILQ_REMOVE(&inqueue, req, r_next);
723 error = pthread_mutex_unlock(&inqueue_mtx);
729 assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
730 assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
731 assert((req->r_offset % conn->c_sectorsize) == 0);
732 assert((req->r_length % conn->c_sectorsize) == 0);
734 g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32,
735 __func__, req->r_offset, req->r_length);
741 switch (req->r_cmd) {
743 data = pread(fd, req->r_data, req->r_length,
746 case GGATE_CMD_WRITE:
747 data = pwrite(fd, req->r_data, req->r_length,
749 /* Free data memory here - better sooner. */
754 if (data != (ssize_t)req->r_length) {
755 /* Report short reads/writes as I/O errors. */
758 g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
759 req->r_error = errno;
760 if (req->r_data != NULL) {
767 * Put the request onto the outgoing queue.
769 error = pthread_mutex_lock(&outqueue_mtx);
771 TAILQ_INSERT_TAIL(&outqueue, req, r_next);
772 error = pthread_cond_signal(&outqueue_cond);
774 error = pthread_mutex_unlock(&outqueue_mtx);
783 send_thread(void *arg)
785 struct ggd_connection *conn;
786 struct ggd_request *req;
791 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
795 * Get a request from the outgoing queue.
797 error = pthread_mutex_lock(&outqueue_mtx);
799 while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
800 error = pthread_cond_wait(&outqueue_cond,
804 TAILQ_REMOVE(&outqueue, req, r_next);
805 error = pthread_mutex_unlock(&outqueue_mtx);
808 g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32,
809 __func__, req->r_offset, req->r_length);
814 g_gate_swap2n_hdr(&req->r_hdr);
815 if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
816 g_gate_xlog("Error while sending hdr packet: %s.",
819 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
820 g_gate_swap2h_hdr(&req->r_hdr);
821 if (req->r_data != NULL) {
822 data = g_gate_send(fd, req->r_data, req->r_length, 0);
823 if (data != (ssize_t)req->r_length) {
824 g_gate_xlog("Error while sending data: %s.",
827 g_gate_log(LOG_DEBUG,
828 "Sent %zd bytes (offset=%" PRIu64 ", size=%" PRIu32
829 ").", data, req->r_offset, req->r_length);
840 log_connection(struct sockaddr *from)
844 ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
845 g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
849 handshake(struct sockaddr *from, int sfd)
851 struct g_gate_version ver;
852 struct g_gate_cinit cinit;
853 struct g_gate_sinit sinit;
854 struct ggd_connection *conn;
855 struct ggd_export *ex;
858 log_connection(from);
860 * Phase 1: Version verification.
862 g_gate_log(LOG_DEBUG, "Receiving version packet.");
863 data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
864 g_gate_swap2h_version(&ver);
865 if (data != sizeof(ver)) {
866 g_gate_log(LOG_WARNING, "Malformed version packet.");
869 g_gate_log(LOG_DEBUG, "Version packet received.");
870 if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
871 g_gate_log(LOG_WARNING, "Invalid magic field.");
874 if (ver.gv_version != GGATE_VERSION) {
875 g_gate_log(LOG_WARNING, "Version %u is not supported.",
880 g_gate_swap2n_version(&ver);
881 data = g_gate_send(sfd, &ver, sizeof(ver), 0);
882 g_gate_swap2h_version(&ver);
884 sendfail(sfd, errno, "Error while sending version packet: %s.",
890 * Phase 2: Request verification.
892 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
893 data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
894 g_gate_swap2h_cinit(&cinit);
895 if (data != sizeof(cinit)) {
896 g_gate_log(LOG_WARNING, "Malformed initial packet.");
899 g_gate_log(LOG_DEBUG, "Initial packet received.");
900 conn = connection_find(&cinit);
903 * Connection should already exists.
905 g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
906 (unsigned long)conn->c_token);
907 if (connection_add(conn, &cinit, from, sfd) == -1) {
908 connection_remove(conn);
913 * New connection, allocate space.
915 conn = connection_new(&cinit, from, sfd);
917 sendfail(sfd, ENOMEM,
918 "Cannot allocate new connection.");
921 g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
922 (unsigned long)conn->c_token);
925 ex = exports_find(from, &cinit, conn);
927 sendfail(sfd, errno, NULL);
928 connection_remove(conn);
931 if (conn->c_mediasize == 0) {
932 conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
933 conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
935 sinit.gs_mediasize = conn->c_mediasize;
936 sinit.gs_sectorsize = conn->c_sectorsize;
939 g_gate_log(LOG_DEBUG, "Sending initial packet.");
941 g_gate_swap2n_sinit(&sinit);
942 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
943 g_gate_swap2h_sinit(&sinit);
945 sendfail(sfd, errno, "Error while sending initial packet: %s.",
950 if (connection_ready(conn)) {
951 connection_launch(conn);
952 connection_remove(conn);
958 huphandler(int sig __unused)
965 main(int argc, char *argv[])
967 const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid";
969 struct sockaddr_in serv;
970 struct sockaddr from;
976 bindaddr = htonl(INADDR_ANY);
978 while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) {
981 bindaddr = g_gate_str2ip(optarg);
982 if (bindaddr == INADDR_NONE) {
984 "Invalid IP/host name to bind to.");
988 ggated_pidfile = optarg;
995 port = strtoul(optarg, NULL, 10);
996 if (port == 0 && errno != 0)
997 errx(EXIT_FAILURE, "Invalid port.");
1001 rcvbuf = strtoul(optarg, NULL, 10);
1002 if (rcvbuf == 0 && errno != 0)
1003 errx(EXIT_FAILURE, "Invalid rcvbuf.");
1007 sndbuf = strtoul(optarg, NULL, 10);
1008 if (sndbuf == 0 && errno != 0)
1009 errx(EXIT_FAILURE, "Invalid sndbuf.");
1022 if (argv[0] != NULL)
1023 exports_file = argv[0];
1026 pfh = pidfile_open(ggated_pidfile, 0600, &otherpid);
1028 if (errno == EEXIST) {
1029 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
1030 (intmax_t)otherpid);
1032 err(EXIT_FAILURE, "Cannot open/create pidfile");
1035 if (!g_gate_verbose) {
1036 /* Run in daemon mode. */
1037 if (daemon(0, 0) == -1)
1038 g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1043 signal(SIGCHLD, SIG_IGN);
1045 sfd = socket(AF_INET, SOCK_STREAM, 0);
1047 g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1048 bzero(&serv, sizeof(serv));
1049 serv.sin_family = AF_INET;
1050 serv.sin_addr.s_addr = bindaddr;
1051 serv.sin_port = htons(port);
1053 g_gate_socket_settings(sfd);
1055 if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1056 g_gate_xlog("bind(): %s.", strerror(errno));
1057 if (listen(sfd, 5) == -1)
1058 g_gate_xlog("listen(): %s.", strerror(errno));
1060 g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1062 signal(SIGHUP, huphandler);
1065 fromlen = sizeof(from);
1066 tmpsfd = accept(sfd, &from, &fromlen);
1068 g_gate_xlog("accept(): %s.", strerror(errno));
1075 if (!handshake(&from, tmpsfd))
1079 pidfile_remove(pfh);