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>
63 #define GGATED_EXPORT_FILE "/etc/gg.exports"
65 struct ggd_connection {
67 unsigned c_sectorsize;
68 unsigned c_flags; /* flags (RO/RW) */
76 LIST_ENTRY(ggd_connection) c_next;
80 struct g_gate_hdr r_hdr;
82 TAILQ_ENTRY(ggd_request) r_next;
84 #define r_cmd r_hdr.gh_cmd
85 #define r_offset r_hdr.gh_offset
86 #define r_length r_hdr.gh_length
87 #define r_error r_hdr.gh_error
90 char *e_path; /* path to device/file */
91 in_addr_t e_ip; /* remote IP address */
92 in_addr_t e_mask; /* IP mask */
93 unsigned e_flags; /* flags (RO/RW) */
94 SLIST_ENTRY(ggd_export) e_next;
97 static const char *exports_file = GGATED_EXPORT_FILE;
98 static int got_sighup = 0;
99 static in_addr_t bindaddr;
101 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
102 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
103 static pthread_mutex_t inqueue_mtx, outqueue_mtx;
104 static pthread_cond_t inqueue_cond, outqueue_cond;
106 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
107 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
109 static void *recv_thread(void *arg);
110 static void *disk_thread(void *arg);
111 static void *send_thread(void *arg);
117 fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] "
118 "[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname());
127 snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
136 countmask(unsigned m)
143 mask = 1 << (32 - m);
151 line_parse(char *line, unsigned lineno)
153 struct ggd_export *ex;
154 char *word, *path, *sflags;
155 unsigned flags, i, vmask;
158 ip = mask = flags = vmask = 0;
162 for (i = 0, word = strtok(line, " \t"); word != NULL;
163 i++, word = strtok(NULL, " \t")) {
165 case 0: /* IP address or host name */
166 ip = g_gate_str2ip(strsep(&word, "/"));
167 if (ip == INADDR_NONE) {
168 g_gate_xlog("Invalid IP/host name at line %u.",
176 vmask = strtoul(word, NULL, 10);
177 if (vmask == 0 && errno != 0) {
178 g_gate_xlog("Invalid IP mask value at "
181 if ((unsigned)vmask > 32) {
182 g_gate_xlog("Invalid IP mask value at line %u.",
186 mask = countmask(vmask);
189 if (strcasecmp("rd", word) == 0 ||
190 strcasecmp("ro", word) == 0) {
192 } else if (strcasecmp("wo", word) == 0) {
194 } else if (strcasecmp("rw", word) == 0) {
197 g_gate_xlog("Invalid value in flags field at "
203 if (strlen(word) >= MAXPATHLEN) {
204 g_gate_xlog("Path too long at line %u. ",
210 g_gate_xlog("Too many arguments at line %u. ", lineno);
214 g_gate_xlog("Too few arguments at line %u.", lineno);
216 ex = malloc(sizeof(*ex));
218 g_gate_xlog("Not enough memory.");
219 ex->e_path = strdup(path);
220 if (ex->e_path == NULL)
221 g_gate_xlog("Not enough memory.");
223 /* Made 'and' here. */
224 ex->e_ip = (ip & mask);
228 SLIST_INSERT_HEAD(&exports, ex, e_next);
230 g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
231 ip2str(ex->e_ip), vmask, path, sflags);
237 struct ggd_export *ex;
239 while (!SLIST_EMPTY(&exports)) {
240 ex = SLIST_FIRST(&exports);
241 SLIST_REMOVE_HEAD(&exports, e_next);
246 #define EXPORTS_LINE_SIZE 2048
250 char buf[EXPORTS_LINE_SIZE], *line;
251 unsigned lineno = 0, objs = 0, len;
256 fd = fopen(exports_file, "r");
258 g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
262 g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
265 if (fgets(buf, sizeof(buf), fd) == NULL) {
269 g_gate_xlog("Error while reading exports file: %s.",
273 /* Increase line count. */
276 /* Skip spaces and tabs. */
277 for (line = buf; *line == ' ' || *line == '\t'; ++line)
280 /* Empty line, comment or empty line at the end of file. */
281 if (*line == '\n' || *line == '#' || *line == '\0')
285 if (line[len - 1] == '\n') {
286 /* Remove new line char. */
287 line[len - 1] = '\0';
290 g_gate_xlog("Line %u too long.", lineno);
293 line_parse(line, lineno);
300 g_gate_xlog("There are no objects to export.");
302 g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
306 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
307 struct ggd_connection *conn)
309 char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
310 int error = 0, flags;
312 strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
313 strlcat(ipmask, "/", sizeof(ipmask));
314 strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
315 if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
316 if (ex->e_flags == O_WRONLY) {
317 g_gate_log(LOG_WARNING, "Read-only access requested, "
318 "but %s (%s) is exported write-only.", ex->e_path,
322 conn->c_flags |= GGATE_FLAG_RDONLY;
324 } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
325 if (ex->e_flags == O_RDONLY) {
326 g_gate_log(LOG_WARNING, "Write-only access requested, "
327 "but %s (%s) is exported read-only.", ex->e_path,
331 conn->c_flags |= GGATE_FLAG_WRONLY;
334 if (ex->e_flags == O_RDONLY) {
335 g_gate_log(LOG_WARNING, "Read-write access requested, "
336 "but %s (%s) is exported read-only.", ex->e_path,
339 } else if (ex->e_flags == O_WRONLY) {
340 g_gate_log(LOG_WARNING, "Read-write access requested, "
341 "but %s (%s) is exported write-only.", ex->e_path,
346 if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
348 else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
352 if (conn->c_diskfd != -1) {
353 if (strcmp(conn->c_path, ex->e_path) != 0) {
354 g_gate_log(LOG_ERR, "old %s and new %s: "
355 "Path mismatch during handshakes.",
356 conn->c_path, ex->e_path);
362 conn->c_diskfd = open(ex->e_path, flags);
363 if (conn->c_diskfd == -1) {
365 g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
372 static struct ggd_export *
373 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
374 struct ggd_connection *conn)
376 struct ggd_export *ex;
380 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
381 SLIST_FOREACH(ex, &exports, e_next) {
382 if ((ip & ex->e_mask) != ex->e_ip) {
383 g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
387 if (strcmp(cinit->gc_path, ex->e_path) != 0) {
388 g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
392 error = exports_check(ex, cinit, conn);
400 g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
407 * Remove timed out connections.
410 connection_cleanups(void)
412 struct ggd_connection *conn, *tconn;
416 LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
417 if (now - conn->c_birthtime > 10) {
418 LIST_REMOVE(conn, c_next);
419 g_gate_log(LOG_NOTICE,
420 "Connection from %s [%s] removed.",
421 ip2str(conn->c_srcip), conn->c_path);
422 close(conn->c_diskfd);
423 close(conn->c_sendfd);
424 close(conn->c_recvfd);
431 static struct ggd_connection *
432 connection_find(struct g_gate_cinit *cinit)
434 struct ggd_connection *conn;
436 LIST_FOREACH(conn, &connections, c_next) {
437 if (conn->c_token == cinit->gc_token)
443 static struct ggd_connection *
444 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
446 struct ggd_connection *conn;
450 * First, look for old connections.
451 * We probably should do it every X seconds, but what for?
452 * It is only dangerous if an attacker wants to overload connections
453 * queue, so here is a good place to do the cleanups.
455 connection_cleanups();
457 conn = malloc(sizeof(*conn));
460 conn->c_path = strdup(cinit->gc_path);
461 if (conn->c_path == NULL) {
465 conn->c_token = cinit->gc_token;
466 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
468 conn->c_diskfd = conn->c_sendfd = conn->c_recvfd = -1;
469 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
470 conn->c_sendfd = sfd;
472 conn->c_recvfd = sfd;
473 conn->c_mediasize = 0;
474 conn->c_sectorsize = 0;
475 time(&conn->c_birthtime);
476 conn->c_flags = cinit->gc_flags;
477 LIST_INSERT_HEAD(&connections, conn, c_next);
478 g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
484 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
485 struct sockaddr *s, int sfd)
489 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
490 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
491 if (conn->c_sendfd != -1) {
492 g_gate_log(LOG_WARNING,
493 "Send socket already exists [%s, %s].", ip2str(ip),
497 conn->c_sendfd = sfd;
499 if (conn->c_recvfd != -1) {
500 g_gate_log(LOG_WARNING,
501 "Receive socket already exists [%s, %s].",
502 ip2str(ip), conn->c_path);
505 conn->c_recvfd = sfd;
507 g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
513 * Remove one socket from the given connection or the whole
514 * connection if sfd == -1.
517 connection_remove(struct ggd_connection *conn)
520 LIST_REMOVE(conn, c_next);
521 g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
522 ip2str(conn->c_srcip), conn->c_path);
523 if (conn->c_diskfd != -1)
524 close(conn->c_diskfd);
525 if (conn->c_sendfd != -1)
526 close(conn->c_sendfd);
527 if (conn->c_recvfd != -1)
528 close(conn->c_recvfd);
534 connection_ready(struct ggd_connection *conn)
537 return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
541 connection_launch(struct ggd_connection *conn)
549 else if (pid == -1) {
550 g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
553 g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
556 * Create condition variables and mutexes for in-queue and out-queue
559 error = pthread_mutex_init(&inqueue_mtx, NULL);
561 g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
564 error = pthread_cond_init(&inqueue_cond, NULL);
566 g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
569 error = pthread_mutex_init(&outqueue_mtx, NULL);
571 g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
574 error = pthread_cond_init(&outqueue_cond, NULL);
576 g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
582 * recvtd - thread for receiving I/O request
583 * diskio - thread for doing I/O request
584 * sendtd - thread for sending I/O requests back
586 error = pthread_create(&td, NULL, send_thread, conn);
588 g_gate_xlog("pthread_create(send_thread): %s.",
591 error = pthread_create(&td, NULL, recv_thread, conn);
593 g_gate_xlog("pthread_create(recv_thread): %s.",
600 sendfail(int sfd, int error, const char *fmt, ...)
602 struct g_gate_sinit sinit;
606 memset(&sinit, 0, sizeof(sinit));
607 sinit.gs_error = error;
608 g_gate_swap2n_sinit(&sinit);
609 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
610 g_gate_swap2h_sinit(&sinit);
611 if (data != sizeof(sinit)) {
612 g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
618 g_gate_vlog(LOG_WARNING, fmt, ap);
624 malloc_waitok(size_t size)
628 while ((p = malloc(size)) == NULL) {
629 g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
636 recv_thread(void *arg)
638 struct ggd_connection *conn;
639 struct ggd_request *req;
644 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
650 req = malloc_waitok(sizeof(*req));
651 data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
654 g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
656 } else if (data == -1) {
657 g_gate_xlog("Error while receiving hdr packet: %s.",
659 } else if (data != sizeof(req->r_hdr)) {
660 g_gate_xlog("Malformed hdr packet received.");
662 g_gate_log(LOG_DEBUG, "Received hdr packet.");
663 g_gate_swap2h_hdr(&req->r_hdr);
665 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
666 (intmax_t)req->r_offset, (unsigned)req->r_length);
669 * Allocate memory for data.
671 req->r_data = malloc_waitok(req->r_length);
674 * Receive data to write for WRITE request.
676 if (req->r_cmd == GGATE_CMD_WRITE) {
677 g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
679 data = g_gate_recv(fd, req->r_data, req->r_length,
682 g_gate_xlog("Error while receiving data: %s.",
688 * Put the request onto the incoming queue.
690 error = pthread_mutex_lock(&inqueue_mtx);
692 TAILQ_INSERT_TAIL(&inqueue, req, r_next);
693 error = pthread_cond_signal(&inqueue_cond);
695 error = pthread_mutex_unlock(&inqueue_mtx);
701 disk_thread(void *arg)
703 struct ggd_connection *conn;
704 struct ggd_request *req;
709 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
713 * Get a request from the incoming queue.
715 error = pthread_mutex_lock(&inqueue_mtx);
717 while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
718 error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
721 TAILQ_REMOVE(&inqueue, req, r_next);
722 error = pthread_mutex_unlock(&inqueue_mtx);
728 assert(req->r_cmd == GGATE_CMD_READ || req->r_cmd == GGATE_CMD_WRITE);
729 assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
730 assert((req->r_offset % conn->c_sectorsize) == 0);
731 assert((req->r_length % conn->c_sectorsize) == 0);
733 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
734 (intmax_t)req->r_offset, (unsigned)req->r_length);
740 switch (req->r_cmd) {
742 data = pread(fd, req->r_data, req->r_length,
745 case GGATE_CMD_WRITE:
746 data = pwrite(fd, req->r_data, req->r_length,
748 /* Free data memory here - better sooner. */
753 if (data != (ssize_t)req->r_length) {
754 /* Report short reads/writes as I/O errors. */
757 g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
758 req->r_error = errno;
759 if (req->r_data != NULL) {
766 * Put the request onto the outgoing queue.
768 error = pthread_mutex_lock(&outqueue_mtx);
770 TAILQ_INSERT_TAIL(&outqueue, req, r_next);
771 error = pthread_cond_signal(&outqueue_cond);
773 error = pthread_mutex_unlock(&outqueue_mtx);
782 send_thread(void *arg)
784 struct ggd_connection *conn;
785 struct ggd_request *req;
790 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
794 * Get a request from the outgoing queue.
796 error = pthread_mutex_lock(&outqueue_mtx);
798 while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
799 error = pthread_cond_wait(&outqueue_cond,
803 TAILQ_REMOVE(&outqueue, req, r_next);
804 error = pthread_mutex_unlock(&outqueue_mtx);
807 g_gate_log(LOG_DEBUG, "%s: offset=%jd length=%u", __func__,
808 (intmax_t)req->r_offset, (unsigned)req->r_length);
813 g_gate_swap2n_hdr(&req->r_hdr);
814 if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
815 g_gate_xlog("Error while sending hdr packet: %s.",
818 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
819 g_gate_swap2h_hdr(&req->r_hdr);
820 if (req->r_data != NULL) {
821 data = g_gate_send(fd, req->r_data, req->r_length, 0);
822 if (data != (ssize_t)req->r_length) {
823 g_gate_xlog("Error while sending data: %s.",
826 g_gate_log(LOG_DEBUG,
827 "Sent %zd bytes (offset=%ju, size=%zu).", data,
828 (uintmax_t)req->r_offset, (size_t)req->r_length);
839 log_connection(struct sockaddr *from)
843 ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
844 g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
848 handshake(struct sockaddr *from, int sfd)
850 struct g_gate_version ver;
851 struct g_gate_cinit cinit;
852 struct g_gate_sinit sinit;
853 struct ggd_connection *conn;
854 struct ggd_export *ex;
857 log_connection(from);
859 * Phase 1: Version verification.
861 g_gate_log(LOG_DEBUG, "Receiving version packet.");
862 data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
863 g_gate_swap2h_version(&ver);
864 if (data != sizeof(ver)) {
865 g_gate_log(LOG_WARNING, "Malformed version packet.");
868 g_gate_log(LOG_DEBUG, "Version packet received.");
869 if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
870 g_gate_log(LOG_WARNING, "Invalid magic field.");
873 if (ver.gv_version != GGATE_VERSION) {
874 g_gate_log(LOG_WARNING, "Version %u is not supported.",
879 g_gate_swap2n_version(&ver);
880 data = g_gate_send(sfd, &ver, sizeof(ver), 0);
881 g_gate_swap2h_version(&ver);
883 sendfail(sfd, errno, "Error while sending version packet: %s.",
889 * Phase 2: Request verification.
891 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
892 data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
893 g_gate_swap2h_cinit(&cinit);
894 if (data != sizeof(cinit)) {
895 g_gate_log(LOG_WARNING, "Malformed initial packet.");
898 g_gate_log(LOG_DEBUG, "Initial packet received.");
899 conn = connection_find(&cinit);
902 * Connection should already exists.
904 g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
905 (unsigned long)conn->c_token);
906 if (connection_add(conn, &cinit, from, sfd) == -1) {
907 connection_remove(conn);
912 * New connection, allocate space.
914 conn = connection_new(&cinit, from, sfd);
916 sendfail(sfd, ENOMEM,
917 "Cannot allocate new connection.");
920 g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
921 (unsigned long)conn->c_token);
924 ex = exports_find(from, &cinit, conn);
926 sendfail(sfd, errno, NULL);
927 connection_remove(conn);
930 if (conn->c_mediasize == 0) {
931 conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
932 conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
934 sinit.gs_mediasize = conn->c_mediasize;
935 sinit.gs_sectorsize = conn->c_sectorsize;
938 g_gate_log(LOG_DEBUG, "Sending initial packet.");
940 g_gate_swap2n_sinit(&sinit);
941 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
942 g_gate_swap2h_sinit(&sinit);
944 sendfail(sfd, errno, "Error while sending initial packet: %s.",
949 if (connection_ready(conn)) {
950 connection_launch(conn);
951 connection_remove(conn);
957 huphandler(int sig __unused)
964 main(int argc, char *argv[])
966 const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid";
968 struct sockaddr_in serv;
969 struct sockaddr from;
975 bindaddr = htonl(INADDR_ANY);
977 while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) {
980 bindaddr = g_gate_str2ip(optarg);
981 if (bindaddr == INADDR_NONE) {
983 "Invalid IP/host name to bind to.");
987 ggated_pidfile = optarg;
994 port = strtoul(optarg, NULL, 10);
995 if (port == 0 && errno != 0)
996 errx(EXIT_FAILURE, "Invalid port.");
1000 rcvbuf = strtoul(optarg, NULL, 10);
1001 if (rcvbuf == 0 && errno != 0)
1002 errx(EXIT_FAILURE, "Invalid rcvbuf.");
1006 sndbuf = strtoul(optarg, NULL, 10);
1007 if (sndbuf == 0 && errno != 0)
1008 errx(EXIT_FAILURE, "Invalid sndbuf.");
1021 if (argv[0] != NULL)
1022 exports_file = argv[0];
1025 pfh = pidfile_open(ggated_pidfile, 0600, &otherpid);
1027 if (errno == EEXIST) {
1028 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
1029 (intmax_t)otherpid);
1031 err(EXIT_FAILURE, "Cannot open/create pidfile");
1034 if (!g_gate_verbose) {
1035 /* Run in daemon mode. */
1036 if (daemon(0, 0) == -1)
1037 g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1042 signal(SIGCHLD, SIG_IGN);
1044 sfd = socket(AF_INET, SOCK_STREAM, 0);
1046 g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1047 bzero(&serv, sizeof(serv));
1048 serv.sin_family = AF_INET;
1049 serv.sin_addr.s_addr = bindaddr;
1050 serv.sin_port = htons(port);
1052 g_gate_socket_settings(sfd);
1054 if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1055 g_gate_xlog("bind(): %s.", strerror(errno));
1056 if (listen(sfd, 5) == -1)
1057 g_gate_xlog("listen(): %s.", strerror(errno));
1059 g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1061 signal(SIGHUP, huphandler);
1064 fromlen = sizeof(from);
1065 tmpsfd = accept(sfd, &from, &fromlen);
1067 g_gate_xlog("accept(): %s.", strerror(errno));
1074 if (!handshake(&from, tmpsfd))
1078 pidfile_remove(pfh);