2 * SPDX-License-Identifier: BSD-2-Clause
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
29 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/ioctl.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
62 #define GGATED_EXPORT_FILE "/etc/gg.exports"
64 struct ggd_connection {
66 unsigned c_sectorsize;
67 unsigned c_flags; /* flags (RO/RW) */
75 LIST_ENTRY(ggd_connection) c_next;
79 struct g_gate_hdr r_hdr;
81 TAILQ_ENTRY(ggd_request) r_next;
83 #define r_cmd r_hdr.gh_cmd
84 #define r_offset r_hdr.gh_offset
85 #define r_length r_hdr.gh_length
86 #define r_error r_hdr.gh_error
89 char *e_path; /* path to device/file */
90 in_addr_t e_ip; /* remote IP address */
91 in_addr_t e_mask; /* IP mask */
92 unsigned e_flags; /* flags (RO/RW) */
93 SLIST_ENTRY(ggd_export) e_next;
96 static const char *exports_file = GGATED_EXPORT_FILE;
97 static int got_sighup = 0;
98 static in_addr_t bindaddr;
100 static TAILQ_HEAD(, ggd_request) inqueue = TAILQ_HEAD_INITIALIZER(inqueue);
101 static TAILQ_HEAD(, ggd_request) outqueue = TAILQ_HEAD_INITIALIZER(outqueue);
102 static pthread_mutex_t inqueue_mtx, outqueue_mtx;
103 static pthread_cond_t inqueue_cond, outqueue_cond;
105 static SLIST_HEAD(, ggd_export) exports = SLIST_HEAD_INITIALIZER(exports);
106 static LIST_HEAD(, ggd_connection) connections = LIST_HEAD_INITIALIZER(connections);
108 static void *recv_thread(void *arg);
109 static void *disk_thread(void *arg);
110 static void *send_thread(void *arg);
116 fprintf(stderr, "usage: %s [-nv] [-a address] [-F pidfile] [-p port] "
117 "[-R rcvbuf] [-S sndbuf] [exports file]\n", getprogname());
126 snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
135 countmask(unsigned m)
142 mask = 1 << (32 - m);
150 line_parse(char *line, unsigned lineno)
152 struct ggd_export *ex;
153 char *word, *path, *sflags;
154 unsigned flags, i, vmask;
157 ip = mask = flags = vmask = 0;
161 for (i = 0, word = strtok(line, " \t"); word != NULL;
162 i++, word = strtok(NULL, " \t")) {
164 case 0: /* IP address or host name */
165 ip = g_gate_str2ip(strsep(&word, "/"));
166 if (ip == INADDR_NONE) {
167 g_gate_xlog("Invalid IP/host name at line %u.",
175 vmask = strtoul(word, NULL, 10);
176 if (vmask == 0 && errno != 0) {
177 g_gate_xlog("Invalid IP mask value at "
180 if ((unsigned)vmask > 32) {
181 g_gate_xlog("Invalid IP mask value at line %u.",
185 mask = countmask(vmask);
188 if (strcasecmp("rd", word) == 0 ||
189 strcasecmp("ro", word) == 0) {
191 } else if (strcasecmp("wo", word) == 0) {
193 } else if (strcasecmp("rw", word) == 0) {
196 g_gate_xlog("Invalid value in flags field at "
202 if (strlen(word) >= MAXPATHLEN) {
203 g_gate_xlog("Path too long at line %u. ",
209 g_gate_xlog("Too many arguments at line %u. ", lineno);
213 g_gate_xlog("Too few arguments at line %u.", lineno);
215 ex = malloc(sizeof(*ex));
217 g_gate_xlog("Not enough memory.");
218 ex->e_path = strdup(path);
219 if (ex->e_path == NULL)
220 g_gate_xlog("Not enough memory.");
222 /* Made 'and' here. */
223 ex->e_ip = (ip & mask);
227 SLIST_INSERT_HEAD(&exports, ex, e_next);
229 g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
230 ip2str(ex->e_ip), vmask, path, sflags);
236 struct ggd_export *ex;
238 while (!SLIST_EMPTY(&exports)) {
239 ex = SLIST_FIRST(&exports);
240 SLIST_REMOVE_HEAD(&exports, e_next);
245 #define EXPORTS_LINE_SIZE 2048
249 char buf[EXPORTS_LINE_SIZE], *line;
250 unsigned lineno = 0, objs = 0, len;
255 fd = fopen(exports_file, "r");
257 g_gate_xlog("Cannot open exports file (%s): %s.", exports_file,
261 g_gate_log(LOG_INFO, "Reading exports file (%s).", exports_file);
264 if (fgets(buf, sizeof(buf), fd) == NULL) {
268 g_gate_xlog("Error while reading exports file: %s.",
272 /* Increase line count. */
275 /* Skip spaces and tabs. */
276 for (line = buf; *line == ' ' || *line == '\t'; ++line)
279 /* Empty line, comment or empty line at the end of file. */
280 if (*line == '\n' || *line == '#' || *line == '\0')
284 if (line[len - 1] == '\n') {
285 /* Remove new line char. */
286 line[len - 1] = '\0';
289 g_gate_xlog("Line %u too long.", lineno);
292 line_parse(line, lineno);
299 g_gate_xlog("There are no objects to export.");
301 g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
305 exports_check(struct ggd_export *ex, struct g_gate_cinit *cinit,
306 struct ggd_connection *conn)
308 char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
309 int error = 0, flags;
311 strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
312 strlcat(ipmask, "/", sizeof(ipmask));
313 strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
314 if ((cinit->gc_flags & GGATE_FLAG_RDONLY) != 0) {
315 if (ex->e_flags == O_WRONLY) {
316 g_gate_log(LOG_WARNING, "Read-only access requested, "
317 "but %s (%s) is exported write-only.", ex->e_path,
321 conn->c_flags |= GGATE_FLAG_RDONLY;
323 } else if ((cinit->gc_flags & GGATE_FLAG_WRONLY) != 0) {
324 if (ex->e_flags == O_RDONLY) {
325 g_gate_log(LOG_WARNING, "Write-only access requested, "
326 "but %s (%s) is exported read-only.", ex->e_path,
330 conn->c_flags |= GGATE_FLAG_WRONLY;
333 if (ex->e_flags == O_RDONLY) {
334 g_gate_log(LOG_WARNING, "Read-write access requested, "
335 "but %s (%s) is exported read-only.", ex->e_path,
338 } else if (ex->e_flags == O_WRONLY) {
339 g_gate_log(LOG_WARNING, "Read-write access requested, "
340 "but %s (%s) is exported write-only.", ex->e_path,
345 if ((conn->c_flags & GGATE_FLAG_RDONLY) != 0)
347 else if ((conn->c_flags & GGATE_FLAG_WRONLY) != 0)
351 if (conn->c_diskfd != -1) {
352 if (strcmp(conn->c_path, ex->e_path) != 0) {
353 g_gate_log(LOG_ERR, "old %s and new %s: "
354 "Path mismatch during handshakes.",
355 conn->c_path, ex->e_path);
361 conn->c_diskfd = open(ex->e_path, flags);
362 if (conn->c_diskfd == -1) {
364 g_gate_log(LOG_ERR, "Cannot open %s: %s.", ex->e_path,
371 static struct ggd_export *
372 exports_find(struct sockaddr *s, struct g_gate_cinit *cinit,
373 struct ggd_connection *conn)
375 struct ggd_export *ex;
379 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
380 SLIST_FOREACH(ex, &exports, e_next) {
381 if ((ip & ex->e_mask) != ex->e_ip) {
382 g_gate_log(LOG_DEBUG, "exports[%s]: IP mismatch.",
386 if (strcmp(cinit->gc_path, ex->e_path) != 0) {
387 g_gate_log(LOG_DEBUG, "exports[%s]: Path mismatch.",
391 error = exports_check(ex, cinit, conn);
399 g_gate_log(LOG_WARNING, "Unauthorized connection from: %s.",
406 * Remove timed out connections.
409 connection_cleanups(void)
411 struct ggd_connection *conn, *tconn;
415 LIST_FOREACH_SAFE(conn, &connections, c_next, tconn) {
416 if (now - conn->c_birthtime > 10) {
417 LIST_REMOVE(conn, c_next);
418 g_gate_log(LOG_NOTICE,
419 "Connection from %s [%s] removed.",
420 ip2str(conn->c_srcip), conn->c_path);
421 close(conn->c_diskfd);
422 close(conn->c_sendfd);
423 close(conn->c_recvfd);
430 static struct ggd_connection *
431 connection_find(struct g_gate_cinit *cinit)
433 struct ggd_connection *conn;
435 LIST_FOREACH(conn, &connections, c_next) {
436 if (conn->c_token == cinit->gc_token)
442 static struct ggd_connection *
443 connection_new(struct g_gate_cinit *cinit, struct sockaddr *s, int sfd)
445 struct ggd_connection *conn;
449 * First, look for old connections.
450 * We probably should do it every X seconds, but what for?
451 * It is only dangerous if an attacker wants to overload connections
452 * queue, so here is a good place to do the cleanups.
454 connection_cleanups();
456 conn = malloc(sizeof(*conn));
459 conn->c_path = strdup(cinit->gc_path);
460 if (conn->c_path == NULL) {
464 conn->c_token = cinit->gc_token;
465 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
467 conn->c_diskfd = conn->c_sendfd = conn->c_recvfd = -1;
468 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0)
469 conn->c_sendfd = sfd;
471 conn->c_recvfd = sfd;
472 conn->c_mediasize = 0;
473 conn->c_sectorsize = 0;
474 time(&conn->c_birthtime);
475 conn->c_flags = cinit->gc_flags;
476 LIST_INSERT_HEAD(&connections, conn, c_next);
477 g_gate_log(LOG_DEBUG, "Connection created [%s, %s].", ip2str(ip),
483 connection_add(struct ggd_connection *conn, struct g_gate_cinit *cinit,
484 struct sockaddr *s, int sfd)
488 ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
489 if ((cinit->gc_flags & GGATE_FLAG_SEND) != 0) {
490 if (conn->c_sendfd != -1) {
491 g_gate_log(LOG_WARNING,
492 "Send socket already exists [%s, %s].", ip2str(ip),
496 conn->c_sendfd = sfd;
498 if (conn->c_recvfd != -1) {
499 g_gate_log(LOG_WARNING,
500 "Receive socket already exists [%s, %s].",
501 ip2str(ip), conn->c_path);
504 conn->c_recvfd = sfd;
506 g_gate_log(LOG_DEBUG, "Connection added [%s, %s].", ip2str(ip),
512 * Remove one socket from the given connection or the whole
513 * connection if sfd == -1.
516 connection_remove(struct ggd_connection *conn)
519 LIST_REMOVE(conn, c_next);
520 g_gate_log(LOG_DEBUG, "Connection removed [%s %s].",
521 ip2str(conn->c_srcip), conn->c_path);
522 if (conn->c_diskfd != -1)
523 close(conn->c_diskfd);
524 if (conn->c_sendfd != -1)
525 close(conn->c_sendfd);
526 if (conn->c_recvfd != -1)
527 close(conn->c_recvfd);
533 connection_ready(struct ggd_connection *conn)
536 return (conn->c_sendfd != -1 && conn->c_recvfd != -1);
540 connection_launch(struct ggd_connection *conn)
548 else if (pid == -1) {
549 g_gate_log(LOG_ERR, "Cannot fork: %s.", strerror(errno));
552 g_gate_log(LOG_DEBUG, "Process created [%s].", conn->c_path);
555 * Create condition variables and mutexes for in-queue and out-queue
558 error = pthread_mutex_init(&inqueue_mtx, NULL);
560 g_gate_xlog("pthread_mutex_init(inqueue_mtx): %s.",
563 error = pthread_cond_init(&inqueue_cond, NULL);
565 g_gate_xlog("pthread_cond_init(inqueue_cond): %s.",
568 error = pthread_mutex_init(&outqueue_mtx, NULL);
570 g_gate_xlog("pthread_mutex_init(outqueue_mtx): %s.",
573 error = pthread_cond_init(&outqueue_cond, NULL);
575 g_gate_xlog("pthread_cond_init(outqueue_cond): %s.",
581 * recvtd - thread for receiving I/O request
582 * diskio - thread for doing I/O request
583 * sendtd - thread for sending I/O requests back
585 error = pthread_create(&td, NULL, send_thread, conn);
587 g_gate_xlog("pthread_create(send_thread): %s.",
590 error = pthread_create(&td, NULL, recv_thread, conn);
592 g_gate_xlog("pthread_create(recv_thread): %s.",
599 sendfail(int sfd, int error, const char *fmt, ...)
601 struct g_gate_sinit sinit;
605 memset(&sinit, 0, sizeof(sinit));
606 sinit.gs_error = error;
607 g_gate_swap2n_sinit(&sinit);
608 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
609 g_gate_swap2h_sinit(&sinit);
610 if (data != sizeof(sinit)) {
611 g_gate_log(LOG_WARNING, "Cannot send initial packet: %s.",
617 g_gate_vlog(LOG_WARNING, fmt, ap);
623 malloc_waitok(size_t size)
627 while ((p = malloc(size)) == NULL) {
628 g_gate_log(LOG_DEBUG, "Cannot allocate %zu bytes.", size);
635 recv_thread(void *arg)
637 struct ggd_connection *conn;
638 struct ggd_request *req;
643 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
649 req = malloc_waitok(sizeof(*req));
650 data = g_gate_recv(fd, &req->r_hdr, sizeof(req->r_hdr),
653 g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
655 } else if (data == -1) {
656 g_gate_xlog("Error while receiving hdr packet: %s.",
658 } else if (data != sizeof(req->r_hdr)) {
659 g_gate_xlog("Malformed hdr packet received.");
661 g_gate_log(LOG_DEBUG, "Received hdr packet.");
662 g_gate_swap2h_hdr(&req->r_hdr);
664 g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32,
665 __func__, req->r_offset, req->r_length);
668 * Allocate memory for data.
670 req->r_data = malloc_waitok(req->r_length);
673 * Receive data to write for WRITE request.
675 if (req->r_cmd == GGATE_CMD_WRITE) {
676 g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
678 data = g_gate_recv(fd, req->r_data, req->r_length,
681 g_gate_xlog("Error while receiving data: %s.",
687 * Put the request onto the incoming queue.
689 error = pthread_mutex_lock(&inqueue_mtx);
691 TAILQ_INSERT_TAIL(&inqueue, req, r_next);
692 error = pthread_cond_signal(&inqueue_cond);
694 error = pthread_mutex_unlock(&inqueue_mtx);
700 disk_thread(void *arg)
702 struct ggd_connection *conn;
703 struct ggd_request *req;
708 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
712 * Get a request from the incoming queue.
714 error = pthread_mutex_lock(&inqueue_mtx);
716 while ((req = TAILQ_FIRST(&inqueue)) == NULL) {
717 error = pthread_cond_wait(&inqueue_cond, &inqueue_mtx);
720 TAILQ_REMOVE(&inqueue, req, r_next);
721 error = pthread_mutex_unlock(&inqueue_mtx);
727 assert(req->r_offset + req->r_length <= (uintmax_t)conn->c_mediasize);
728 assert((req->r_offset % conn->c_sectorsize) == 0);
729 assert((req->r_length % conn->c_sectorsize) == 0);
731 g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32,
732 __func__, req->r_offset, req->r_length);
738 switch (req->r_cmd) {
740 data = pread(fd, req->r_data, req->r_length,
743 case GGATE_CMD_WRITE:
744 data = pwrite(fd, req->r_data, req->r_length,
746 /* Free data memory here - better sooner. */
750 case GGATE_CMD_FLUSH:
753 req->r_error = errno;
756 g_gate_log(LOG_DEBUG, "Unsupported request: %i", req->r_cmd);
757 req->r_error = EOPNOTSUPP;
758 if (req->r_data != NULL) {
764 if (data != (ssize_t)req->r_length) {
765 /* Report short reads/writes as I/O errors. */
768 g_gate_log(LOG_ERR, "Disk error: %s", strerror(errno));
769 req->r_error = errno;
770 if (req->r_data != NULL) {
777 * Put the request onto the outgoing queue.
779 error = pthread_mutex_lock(&outqueue_mtx);
781 TAILQ_INSERT_TAIL(&outqueue, req, r_next);
782 error = pthread_cond_signal(&outqueue_cond);
784 error = pthread_mutex_unlock(&outqueue_mtx);
793 send_thread(void *arg)
795 struct ggd_connection *conn;
796 struct ggd_request *req;
801 g_gate_log(LOG_NOTICE, "%s: started [%s]!", __func__, conn->c_path);
805 * Get a request from the outgoing queue.
807 error = pthread_mutex_lock(&outqueue_mtx);
809 while ((req = TAILQ_FIRST(&outqueue)) == NULL) {
810 error = pthread_cond_wait(&outqueue_cond,
814 TAILQ_REMOVE(&outqueue, req, r_next);
815 error = pthread_mutex_unlock(&outqueue_mtx);
818 g_gate_log(LOG_DEBUG, "%s: offset=%" PRIu64 " length=%" PRIu32,
819 __func__, req->r_offset, req->r_length);
824 g_gate_swap2n_hdr(&req->r_hdr);
825 if (g_gate_send(fd, &req->r_hdr, sizeof(req->r_hdr), 0) == -1) {
826 g_gate_xlog("Error while sending hdr packet: %s.",
829 g_gate_log(LOG_DEBUG, "Sent hdr packet.");
830 g_gate_swap2h_hdr(&req->r_hdr);
831 if (req->r_data != NULL) {
832 data = g_gate_send(fd, req->r_data, req->r_length, 0);
833 if (data != (ssize_t)req->r_length) {
834 g_gate_xlog("Error while sending data: %s.",
837 g_gate_log(LOG_DEBUG,
838 "Sent %zd bytes (offset=%" PRIu64 ", size=%" PRIu32
839 ").", data, req->r_offset, req->r_length);
850 log_connection(struct sockaddr *from)
854 ip = htonl(((struct sockaddr_in *)(void *)from)->sin_addr.s_addr);
855 g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
859 handshake(struct sockaddr *from, int sfd)
861 struct g_gate_version ver;
862 struct g_gate_cinit cinit;
863 struct g_gate_sinit sinit;
864 struct ggd_connection *conn;
865 struct ggd_export *ex;
868 log_connection(from);
870 * Phase 1: Version verification.
872 g_gate_log(LOG_DEBUG, "Receiving version packet.");
873 data = g_gate_recv(sfd, &ver, sizeof(ver), MSG_WAITALL);
874 g_gate_swap2h_version(&ver);
875 if (data != sizeof(ver)) {
876 g_gate_log(LOG_WARNING, "Malformed version packet.");
879 g_gate_log(LOG_DEBUG, "Version packet received.");
880 if (memcmp(ver.gv_magic, GGATE_MAGIC, strlen(GGATE_MAGIC)) != 0) {
881 g_gate_log(LOG_WARNING, "Invalid magic field.");
884 if (ver.gv_version != GGATE_VERSION) {
885 g_gate_log(LOG_WARNING, "Version %u is not supported.",
890 g_gate_swap2n_version(&ver);
891 data = g_gate_send(sfd, &ver, sizeof(ver), 0);
892 g_gate_swap2h_version(&ver);
894 sendfail(sfd, errno, "Error while sending version packet: %s.",
900 * Phase 2: Request verification.
902 g_gate_log(LOG_DEBUG, "Receiving initial packet.");
903 data = g_gate_recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
904 g_gate_swap2h_cinit(&cinit);
905 if (data != sizeof(cinit)) {
906 g_gate_log(LOG_WARNING, "Malformed initial packet.");
909 g_gate_log(LOG_DEBUG, "Initial packet received.");
910 conn = connection_find(&cinit);
913 * Connection should already exists.
915 g_gate_log(LOG_DEBUG, "Found existing connection (token=%lu).",
916 (unsigned long)conn->c_token);
917 if (connection_add(conn, &cinit, from, sfd) == -1) {
918 connection_remove(conn);
923 * New connection, allocate space.
925 conn = connection_new(&cinit, from, sfd);
927 sendfail(sfd, ENOMEM,
928 "Cannot allocate new connection.");
931 g_gate_log(LOG_DEBUG, "New connection created (token=%lu).",
932 (unsigned long)conn->c_token);
935 ex = exports_find(from, &cinit, conn);
937 sendfail(sfd, errno, NULL);
938 connection_remove(conn);
941 if (conn->c_mediasize == 0) {
942 conn->c_mediasize = g_gate_mediasize(conn->c_diskfd);
943 conn->c_sectorsize = g_gate_sectorsize(conn->c_diskfd);
945 sinit.gs_mediasize = conn->c_mediasize;
946 sinit.gs_sectorsize = conn->c_sectorsize;
949 g_gate_log(LOG_DEBUG, "Sending initial packet.");
951 g_gate_swap2n_sinit(&sinit);
952 data = g_gate_send(sfd, &sinit, sizeof(sinit), 0);
953 g_gate_swap2h_sinit(&sinit);
955 sendfail(sfd, errno, "Error while sending initial packet: %s.",
960 if (connection_ready(conn)) {
961 connection_launch(conn);
962 connection_remove(conn);
968 huphandler(int sig __unused)
975 main(int argc, char *argv[])
977 const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid";
979 struct sockaddr_in serv;
980 struct sockaddr from;
986 bindaddr = htonl(INADDR_ANY);
988 while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) {
991 bindaddr = g_gate_str2ip(optarg);
992 if (bindaddr == INADDR_NONE) {
994 "Invalid IP/host name to bind to.");
998 ggated_pidfile = optarg;
1005 port = strtoul(optarg, NULL, 10);
1006 if (port == 0 && errno != 0)
1007 errx(EXIT_FAILURE, "Invalid port.");
1011 rcvbuf = strtoul(optarg, NULL, 10);
1012 if (rcvbuf == 0 && errno != 0)
1013 errx(EXIT_FAILURE, "Invalid rcvbuf.");
1017 sndbuf = strtoul(optarg, NULL, 10);
1018 if (sndbuf == 0 && errno != 0)
1019 errx(EXIT_FAILURE, "Invalid sndbuf.");
1032 if (argv[0] != NULL)
1033 exports_file = argv[0];
1036 pfh = pidfile_open(ggated_pidfile, 0600, &otherpid);
1038 if (errno == EEXIST) {
1039 errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
1040 (intmax_t)otherpid);
1042 err(EXIT_FAILURE, "Cannot open/create pidfile");
1045 if (!g_gate_verbose) {
1046 /* Run in daemon mode. */
1047 if (daemon(0, 0) == -1)
1048 g_gate_xlog("Cannot daemonize: %s", strerror(errno));
1053 signal(SIGCHLD, SIG_IGN);
1055 sfd = socket(AF_INET, SOCK_STREAM, 0);
1057 g_gate_xlog("Cannot open stream socket: %s.", strerror(errno));
1058 bzero(&serv, sizeof(serv));
1059 serv.sin_family = AF_INET;
1060 serv.sin_addr.s_addr = bindaddr;
1061 serv.sin_port = htons(port);
1063 g_gate_socket_settings(sfd);
1065 if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) == -1)
1066 g_gate_xlog("bind(): %s.", strerror(errno));
1067 if (listen(sfd, 5) == -1)
1068 g_gate_xlog("listen(): %s.", strerror(errno));
1070 g_gate_log(LOG_INFO, "Listen on port: %d.", port);
1072 signal(SIGHUP, huphandler);
1075 fromlen = sizeof(from);
1076 tmpsfd = accept(sfd, &from, &fromlen);
1078 g_gate_xlog("accept(): %s.", strerror(errno));
1085 if (!handshake(&from, tmpsfd))
1089 pidfile_remove(pfh);