1 /*
2  * sc_remoted
3  *
4  * $Id: sc_remoted.c,v 1.89 2020/08/18 11:00:10 mjl Exp $
5  *
6  *        Matthew Luckie
7  *        mjl@luckie.org.nz
8  *
9  * Copyright (C) 2014-2020 Matthew Luckie
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  *****************
25  *
26  * This code defines a protocol that exists between a central server
27  * running sc_remoted, and a remote system running scamper.  As the
28  * protocol allows multiple local processes to drive a single remote
29  * scamper process, the protocol is based around "channels" to
30  * separate multiple streams of scamper control connection over a
31  * single TCP socket.
32  *
33  * The protocol is roughly designed as follows:
34  *
35  * Header:
36  * ------
37  * uint32_t  sequence
38  * uint32_t  channel
39  * uint16_t  msglen
40  *
41  * The control header is included in every message sent between the
42  * scamper instance and the remote controller.
43  * The sequence number uniquely identifies the message among a stream
44  * of messages.
45  * The channel number identifies the stream; channel #0 is reserved for
46  * control messages.
47  * The msglen value defines the size of the message following the header.
48  *
49  * Control Messages:
50  * ----------------
51  * uint8_t   type
52  *
53  * A control message begins with a mandatory type number.  The following
54  * control message types are defined, with arrows defining who may send
55  * which message type.
56  *
57  * 0 - Master        (remoted <-- scamper) -- CONTROL_MASTER_NEW
58  * 1 - Master ID     (remoted --> scamper) -- CONTROL_MASTER_ID
59  * 2 - New Channel   (remoted --> scamper) -- CONTROL_CHANNEL_NEW
60  * 3 - Channel FIN   (remoted <-> scamper) -- CONTROL_CHANNEL_FIN
61  * 4 - Keepalive     (remoted <-> scamper) -- CONTROL_KEEPALIVE
62  * 5 - Ack           (remoted <-> scamper) -- CONTROL_ACK
63  * 6 - Master Resume (remoted <-- scamper) -- CONTROL_MASTER_RES
64  * 7 - Master Reject (remoted --> scamper) -- CONTROL_MASTER_REJ
65  * 8 - Master OK     (remoted --> scamper) -- CONTROL_MASTER_OK
66  *
67  * Control Message - Master New (CONTROL_MASTER_NEW)
68  * ----------------------------
69  *
70  * Whenever a scamper instance establishes a TCP connection with a remote
71  * controller, it sends a message that identifies itself.  The message
72  * is formatted as follows:
73  *
74  * uint8_t   magic_len
75  * uint8_t  *magic
76  * uint8_t   monitorname_len
77  * char     *monitorname
78  *
79  * The magic value is generated randomly by the scamper instance when
80  * the process starts, and is never modified.  The same magic value is
81  * always supplied in a control socket connection and allows the remote
82  * controller to identify that the scamper instance supports graceful
83  * restart.
84  * The monitorname is sent if the remote scamper instance uses the -M
85  * option.
86  * Both magic_len and monitorname_len include the terminating null byte.
87  *
88  * Control Message - Master ID (CONTROL_MASTER_ID)
89  * ---------------------------
90  *
91  * After the "Master New" message has been received by the remote
92  * controller, the remote controller sends an ID value to the scamper
93  * instance that it can use as a list identifier in warts.  The message
94  * is formatted as follows:
95  *
96  * uint8_t   id_len;
97  * char     *id
98  *
99  * Control Message - New Channel (CONTROL_CHANNEL_NEW)
100  * -----------------------------
101  *
102  * Whenever a remote controller has a new connection on a unix domain
103  * socket, it sends a control message to scamper with a new channel
104  * number to use for the connection.  The message is formatted as
105  * follows:
106  *
107  * uint32_t  channel
108  *
109  * Control Message - Channel FIN (CONTROL_CHANNEL_FIN)
110  * -----------------------------
111  *
112  * Whenever a client connection has no more to send, it sends a FIN
113  * type to close the channel. the FIN message must be sent by both the
114  * remote controller and the scamper instance for a channel to be
115  * closed.  The message is formatted as follows:
116  *
117  * uint32_t  channel
118  *
119  * Control Message - Keepalive (CONTROL_KEEPALIVE)
120  * ---------------------------
121  *
122  * Both scamper and remoted periodically send keepalive messages to
123  * each other when their keepalive timers expire.  Keepalive messages
124  * have no payload.
125  *
126  * Control Message - Acknowledgement (CONTROL_ACK)
127  * ---------------------------------
128  *
129  * Both scamper and remoted acknowledge messages received by either
130  * end of the connnection, provided the sender expected the message
131  * to be acknowledged.
132  *
133  * uint32_t  sequence
134  *
135  * Control Message - Resume (CONTROL_MASTER_RES)
136  * ------------------------
137  *
138  * Whenever a connection between scamper and remoted is interrupted,
139  * scamper can resume by sending a resumption message with the same
140  * magic value scamper supplied initially.  the resume message also
141  * includes the next sequence number the scamper expects from remoted,
142  * the left edge of the
143  *
144  * uint8_t   magic_len
145  * uint8_t  *magic
146  * uint32_t  rcv_nxt
147  * uint32_t  snd_una
148  * uint32_t  snd_nxt
149  *
150  * Control Message - Reject (CONTROL_MASTER_REJ)
151  * ------------------------
152  *
153  * If a scamper connection is rejected because the magic value
154  * supplied by scamper is invalid, remoted sends scamper a reject
155  * message.
156  *
157  * Control Message - OK (CONTROL_MASTER_OK)
158  * --------------------
159  *
160  * If a scamper resumption request is acceptable, then the remote
161  * controller replies back with the next expected sequence number from
162  * scamper.  this number will be in the range of snd_una and snd_nxt
163  * sent by the scamper instance in the resume message.
164  *
165  * uint32_t  rcv_nxt
166  *
167  */
168 
169 #ifdef HAVE_CONFIG_H
170 #include "config.h"
171 #endif
172 #include "internal.h"
173 
174 #include "scamper_linepoll.h"
175 #include "scamper_writebuf.h"
176 #include "mjl_splaytree.h"
177 #include "mjl_list.h"
178 #include "utils.h"
179 
180 #define SC_MESSAGE_HDRLEN 10
181 
182 /*
183  * sc_unit
184  *
185  * this generic structure says what kind of node is pointed to, and is
186  * used to help garbage collect with kqueue / epoll.
187  */
188 typedef struct sc_unit
189 {
190   void               *data;
191   dlist_t            *list; /* list == gclist if on that list */
192   dlist_node_t       *node;
193   uint8_t             type;
194   uint8_t             gc;
195 } sc_unit_t;
196 
197 #define UNIT_TYPE_MASTER  0
198 #define UNIT_TYPE_CHANNEL 1
199 
200 /*
201  * sc_fd
202  *
203  * this structure associates a file descriptor with a data pointer, as
204  * well as information about what type the fd is and any current
205  * state.
206  */
207 typedef struct sc_fd
208 {
209   int                 fd;
210   sc_unit_t          *unit;
211   uint8_t             type;
212   uint8_t             flags;
213 } sc_fd_t;
214 
215 #define FD_TYPE_SERVER       0
216 #define FD_TYPE_MASTER_INET  1
217 #define FD_TYPE_MASTER_UNIX  2
218 #define FD_TYPE_CHANNEL_UNIX 3
219 
220 #define FD_FLAG_READ        1
221 #define FD_FLAG_WRITE       2
222 
223 /*
224  * sc_master_t
225  *
226  * this structure holds a mapping between a remote scamper process
227  * that is willing to be driven and a local unix domain socket where
228  * local processes can connect.  it also includes a list of all
229  * clients connected using the socket.
230  */
231 typedef struct sc_master
232 {
233   sc_unit_t          *unit;
234   char               *monitorname;
235   char               *name;
236   uint8_t            *magic;
237   uint8_t             magic_len;
238   int                 mode;
239 
240   sc_fd_t            *unix_fd;
241   sc_fd_t             inet_fd;
242   scamper_writebuf_t *inet_wb;
243 
244 #ifdef HAVE_OPENSSL
245   int                 inet_mode;
246   SSL                *inet_ssl;
247   BIO                *inet_rbio;
248   BIO                *inet_wbio;
249 #endif
250 
251   struct timeval      tx_ka;
252   struct timeval      rx_abort;
253   struct timeval      zombie;
254 
255   slist_t            *messages;
256   uint32_t            snd_nxt;
257   uint32_t            rcv_nxt;
258 
259   dlist_t            *channels;
260   uint32_t            next_channel;
261   dlist_node_t       *node;
262   splaytree_node_t   *tree_node;
263   uint8_t             buf[65536 + SC_MESSAGE_HDRLEN];
264   size_t              buf_offset;
265 } sc_master_t;
266 
267 /*
268  * sc_channel_t
269  *
270  * this structure holds a mapping between a local process that wants
271  * to drive a remote scamper, and a channel corresponding to that
272  * instance.
273  */
274 typedef struct sc_channel
275 {
276   uint32_t            id;
277   sc_unit_t          *unit;
278   sc_fd_t            *unix_fd;
279   scamper_linepoll_t *unix_lp;
280   scamper_writebuf_t *unix_wb;
281   sc_master_t        *master;
282   dlist_node_t       *node;
283   uint8_t             flags;
284 } sc_channel_t;
285 
286 /*
287  * sc_message_t
288  *
289  * this structure contains messages that we send over the Internet
290  * socket until we receive an acknowledgement from scamper that it got
291  * the message.
292  */
293 typedef struct sc_message
294 {
295   uint32_t            sequence;
296   uint32_t            channel;
297   uint16_t            msglen;
298   void               *data;
299 } sc_message_t;
300 
301 #define OPT_HELP    0x0001
302 #define OPT_UNIX    0x0002
303 #define OPT_PORT    0x0004
304 #define OPT_DAEMON  0x0008
305 #define OPT_IPV4    0x0010
306 #define OPT_IPV6    0x0020
307 #define OPT_OPTION  0x0040
308 #define OPT_TLSCERT 0x0080
309 #define OPT_TLSPRIV 0x0100
310 #define OPT_ZOMBIE  0x0200
311 #define OPT_PIDFILE 0x0400
312 #define OPT_ALL     0xffff
313 
314 #define FLAG_DEBUG      0x0001
315 #define FLAG_SELECT     0x0002
316 #define FLAG_ALLOW_G    0x0004
317 #define FLAG_ALLOW_O    0x0008
318 
319 #define CHANNEL_FLAG_EOF_TX 0x01
320 #define CHANNEL_FLAG_EOF_RX 0x02
321 
322 #define MASTER_MODE_CONNECT 0
323 #define MASTER_MODE_GO      1
324 #define MASTER_MODE_FLUSH   2
325 
326 #define CONTROL_MASTER_NEW   0 /* scamper --> remoted */
327 #define CONTROL_MASTER_ID    1 /* scamper <-- remoted */
328 #define CONTROL_CHANNEL_NEW  2 /* scamper <-- remoted */
329 #define CONTROL_CHANNEL_FIN  3 /* scamper <-> remoted */
330 #define CONTROL_KEEPALIVE    4 /* scamper <-> remoted */
331 #define CONTROL_ACK          5 /* scamper <-> remoted */
332 #define CONTROL_MASTER_RES   6 /* scamper --> remoted */
333 #define CONTROL_MASTER_REJ   7 /* scamper <-- remoted */
334 #define CONTROL_MASTER_OK    8 /* scamper <-- remoted */
335 
336 static uint16_t     options        = 0;
337 static char        *unix_name      = NULL;
338 static char        *ss_addr        = NULL;
339 static int          ss_port        = 0;
340 static splaytree_t *mstree         = NULL;
341 static dlist_t     *mslist         = NULL;
342 static dlist_t     *gclist         = NULL;
343 static int          stop           = 0;
344 static int          reload         = 0;
345 static uint16_t     flags          = 0;
346 static int          serversockets[2];
347 static int          zombie         = 60 * 15;
348 static char        *pidfile        = NULL;
349 static struct timeval now;
350 
351 #if defined(HAVE_EPOLL)
352 static int          epfd           = -1;
353 #elif defined(HAVE_KQUEUE)
354 static int          kqfd           = -1;
355 #endif
356 
357 #ifdef HAVE_OPENSSL
358 static SSL_CTX     *tls_ctx = NULL;
359 static char        *tls_certfile   = NULL;
360 static char        *tls_privfile   = NULL;
361 #define SSL_MODE_ACCEPT      0x00
362 #define SSL_MODE_ESTABLISHED 0x01
363 #endif
364 
365 /*
366  * sc_unit_gc_t:
367  *
368  * method to cleanup tasks when its time to garbage collect
369  */
370 typedef void (*sc_unit_gc_t)(void *);
371 static void sc_channel_free(sc_channel_t *);
372 static void sc_master_free(sc_master_t *);
373 static const sc_unit_gc_t unit_gc[] = {
374   (sc_unit_gc_t)sc_master_free,      /* UNIT_TYPE_MASTER */
375   (sc_unit_gc_t)sc_channel_free,     /* UNIT_TYPE_CHANNEL */
376 };
377 
378 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
379 typedef void (*sc_fd_cb_t)(void *);
380 static void sc_channel_unix_read_do(sc_channel_t *);
381 static void sc_channel_unix_write_do(sc_channel_t *);
382 static void sc_master_inet_read_do(sc_master_t *);
383 static void sc_master_inet_write_do(sc_master_t *);
384 static void sc_master_unix_accept_do(sc_master_t *);
385 
386 static const sc_fd_cb_t read_cb[] = {
387   NULL,                                 /* FD_TYPE_SERVER */
388   (sc_fd_cb_t)sc_master_inet_read_do,   /* FD_TYPE_MASTER_INET */
389   (sc_fd_cb_t)sc_master_unix_accept_do, /* FD_TYPE_MASTER_UNIX */
390   (sc_fd_cb_t)sc_channel_unix_read_do,  /* FD_TYPE_CHANNEL_UNIX */
391 };
392 static const sc_fd_cb_t write_cb[] = {
393   NULL,                                 /* FD_TYPE_SERVER */
394   (sc_fd_cb_t)sc_master_inet_write_do,  /* FD_TYPE_MASTER_INET */
395   NULL,                                 /* FD_TYPE_MASTER_UNIX */
396   (sc_fd_cb_t)sc_channel_unix_write_do, /* FD_TYPE_CHANNEL_UNIX */
397 };
398 #endif
399 
usage(uint32_t opt_mask)400 static void usage(uint32_t opt_mask)
401 {
402   fprintf(stderr,
403 	  "usage: sc_remoted [-?46D] [-O option] -P [ip:]port -U unix\n"
404 #ifdef HAVE_OPENSSL
405 	  "                  [-c certfile] [-p privfile]\n"
406 #endif
407 	  "                  [-e pidfile] [-Z zombie-time]\n"
408 	  );
409 
410   if(opt_mask == 0)
411     {
412       fprintf(stderr, "\n     sc_remoted -?\n\n");
413       return;
414     }
415 
416   if(opt_mask & OPT_IPV4)
417     fprintf(stderr, "     -4 only listen for connections over IPv4\n");
418 
419   if(opt_mask & OPT_IPV6)
420     fprintf(stderr, "     -6 only listen for connections over IPv6\n");
421 
422   if(opt_mask & OPT_DAEMON)
423     fprintf(stderr, "     -D operate as a daemon\n");
424 
425   if(opt_mask & OPT_PIDFILE)
426     fprintf(stderr, "     -e write process ID to specified file\n");
427 
428   if(opt_mask & OPT_OPTION)
429     {
430       fprintf(stderr, "     -O options\n");
431       fprintf(stderr, "        allowgroup: allow group access to sockets\n");
432       fprintf(stderr, "        allowother: allow other access to sockets\n");
433       fprintf(stderr, "        debug: print debugging messages\n");
434       fprintf(stderr, "        select: use select\n");
435     }
436 
437   if(opt_mask & OPT_PORT)
438     fprintf(stderr, "     -P [ip:]port to accept remote scamper connections\n");
439 
440   if(opt_mask & OPT_UNIX)
441     fprintf(stderr, "     -U directory for unix domain sockets\n");
442 
443 #ifdef HAVE_OPENSSL
444   if(opt_mask & OPT_TLSCERT)
445     fprintf(stderr, "     -c server certificate in PEM format\n");
446   if(opt_mask & OPT_TLSPRIV)
447     fprintf(stderr, "     -p private key in PEM format\n");
448 #endif
449 
450   if(opt_mask & OPT_ZOMBIE)
451     fprintf(stderr, "     -Z time to retain state for disconnected scamper\n");
452 
453   return;
454 }
455 
check_options(int argc,char * argv[])456 static int check_options(int argc, char *argv[])
457 {
458   struct sockaddr_storage sas;
459   char *opts = "?46DO:c:e:p:P:U:Z:", *opt_addrport = NULL, *opt_zombie = NULL;
460   char *opt_pidfile = NULL;
461   long lo;
462   int ch;
463 
464   while((ch = getopt(argc, argv, opts)) != -1)
465     {
466       switch(ch)
467 	{
468 	case '4':
469 	  options |= OPT_IPV4;
470 	  break;
471 
472 	case '6':
473 	  options |= OPT_IPV6;
474 	  break;
475 
476 	case 'D':
477 	  options |= OPT_DAEMON;
478 	  break;
479 
480 	case 'e':
481 	  options |= OPT_PIDFILE;
482 	  opt_pidfile = optarg;
483 	  break;
484 
485 	case 'O':
486 	  if(strcasecmp(optarg, "select") == 0)
487 	    flags |= FLAG_SELECT;
488 	  else if(strcasecmp(optarg, "allowgroup") == 0)
489 	    flags |= FLAG_ALLOW_G;
490 	  else if(strcasecmp(optarg, "allowother") == 0)
491 	    flags |= FLAG_ALLOW_O;
492 	  else if(strcasecmp(optarg, "debug") == 0)
493 	    flags |= FLAG_DEBUG;
494 	  else
495 	    {
496 	      usage(OPT_ALL);
497 	      return -1;
498 	    }
499 	  break;
500 
501 	case 'P':
502 	  opt_addrport = optarg;
503 	  break;
504 
505 #ifdef HAVE_OPENSSL
506 	case 'c':
507 	  tls_certfile = optarg;
508 	  options |= OPT_TLSCERT;
509 	  break;
510 
511 	case 'p':
512 	  tls_privfile = optarg;
513 	  options |= OPT_TLSPRIV;
514 	  break;
515 #endif
516 
517 	case 'U':
518 	  unix_name = optarg;
519 	  break;
520 
521 	case 'Z':
522 	  opt_zombie = optarg;
523 	  break;
524 
525 	case '?':
526 	default:
527 	  usage(OPT_ALL);
528 	  return -1;
529 	}
530     }
531 
532   if(unix_name == NULL || opt_addrport == NULL)
533     {
534       usage(OPT_PORT|OPT_UNIX);
535       return -1;
536     }
537 
538 #ifdef HAVE_OPENSSL
539   if((options & (OPT_TLSCERT|OPT_TLSPRIV)) != 0 &&
540      (options & (OPT_TLSCERT|OPT_TLSPRIV)) != (OPT_TLSCERT|OPT_TLSPRIV))
541     {
542       usage(OPT_TLSCERT|OPT_TLSPRIV);
543       return -1;
544     }
545 #endif
546 
547   if(string_addrport(opt_addrport, &ss_addr, &ss_port) != 0)
548     {
549       usage(OPT_PORT);
550       return -1;
551     }
552 
553   /*
554    * if there was an address specified, and either -4 or -6 was passed in,
555    * ensure the address at least matches the specified address family
556    */
557   if(ss_addr != NULL && (options & (OPT_IPV4|OPT_IPV6)) != 0 &&
558      (sockaddr_compose_str((struct sockaddr *)&sas, ss_addr, ss_port) != 0 ||
559       ((options & OPT_IPV4) != 0 && sas.ss_family == AF_INET6) ||
560       ((options & OPT_IPV6) != 0 && sas.ss_family == AF_INET)))
561     {
562       usage(OPT_PORT | (options & (OPT_IPV4|OPT_IPV6)));
563       return -1;
564     }
565 
566   if(opt_pidfile != NULL && (pidfile = strdup(opt_pidfile)) == NULL)
567     {
568       usage(OPT_PIDFILE);
569       return -1;
570     }
571 
572   if(opt_zombie != NULL)
573     {
574       if(string_tolong(opt_zombie, &lo) != 0 || lo < 0 || lo > (60 * 60))
575 	{
576 	  usage(OPT_ZOMBIE);
577 	  return -1;
578 	}
579       zombie = lo;
580     }
581 
582   return 0;
583 }
584 
remote_debug(const char * func,const char * format,...)585 static void remote_debug(const char *func, const char *format, ...)
586 {
587   char message[512], ts[16];
588   struct tm *tm;
589   va_list ap;
590   time_t t;
591   int ms;
592 
593   if(options & OPT_DAEMON)
594     return;
595 
596   if((flags & FLAG_DEBUG) == 0)
597     return;
598 
599   va_start(ap, format);
600   vsnprintf(message, sizeof(message), format, ap);
601   va_end(ap);
602 
603   t = now.tv_sec;
604   if((tm = localtime(&t)) == NULL)
605     return;
606   ms = now.tv_usec / 1000;
607   snprintf(ts, sizeof(ts), "[%02d:%02d:%02d:%03d]",
608 	   tm->tm_hour, tm->tm_min, tm->tm_sec, ms);
609 
610   fprintf(stderr, "%s %s: %s\n", ts, func, message);
611   fflush(stderr);
612   return;
613 }
614 
sc_fd_peername(const sc_fd_t * fd,char * buf,size_t len)615 static int sc_fd_peername(const sc_fd_t *fd, char *buf, size_t len)
616 {
617   struct sockaddr_storage sas;
618   socklen_t sl;
619 
620   sl = sizeof(sas);
621   if(getpeername(fd->fd, (struct sockaddr *)&sas, &sl) != 0)
622     {
623       remote_debug(__func__, "could not getpeername: %s", strerror(errno));
624       return -1;
625     }
626   if(sockaddr_tostr((struct sockaddr *)&sas, buf, len) == NULL)
627     {
628       remote_debug(__func__, "could not convert to string");
629       return -1;
630     }
631   return 0;
632 }
633 
sc_fd_read_add(sc_fd_t * fd)634 static int sc_fd_read_add(sc_fd_t *fd)
635 {
636 #if defined(HAVE_EPOLL)
637   struct epoll_event ev;
638 #elif defined(HAVE_KQUEUE)
639   struct kevent kev;
640 #endif
641 
642   if((fd->flags & FD_FLAG_READ) != 0)
643     return 0;
644   fd->flags |= FD_FLAG_READ;
645   if((flags & FLAG_SELECT) != 0)
646     return 0;
647 
648 #if defined(HAVE_EPOLL)
649   ev.data.ptr = fd;
650   if((fd->flags & FD_FLAG_WRITE) == 0)
651     {
652       ev.events = EPOLLIN;
653       if(epoll_ctl(epfd, EPOLL_CTL_ADD, fd->fd, &ev) != 0)
654 	return -1;
655     }
656   else
657     {
658       ev.events = EPOLLIN | EPOLLOUT;
659       if(epoll_ctl(epfd, EPOLL_CTL_MOD, fd->fd, &ev) != 0)
660 	return -1;
661     }
662 #elif defined(HAVE_KQUEUE)
663   EV_SET(&kev, fd->fd, EVFILT_READ, EV_ADD, 0, 0, fd);
664   if(kevent(kqfd, &kev, 1, NULL, 0, NULL) != 0)
665     return -1;
666 #endif
667   return 0;
668 }
669 
sc_fd_read_del(sc_fd_t * fd)670 static int sc_fd_read_del(sc_fd_t *fd)
671 {
672 #if defined(HAVE_EPOLL)
673   struct epoll_event ev;
674 #elif defined(HAVE_KQUEUE)
675   struct kevent kev;
676 #endif
677 
678   if((fd->flags & FD_FLAG_READ) == 0)
679     return 0;
680   fd->flags &= ~(FD_FLAG_READ);
681   if((flags & FLAG_SELECT) != 0)
682     return 0;
683 
684 #if defined(HAVE_EPOLL)
685   ev.data.ptr = fd;
686   if((fd->flags & FD_FLAG_WRITE) == 0)
687     {
688       ev.events = 0;
689       if(epoll_ctl(epfd, EPOLL_CTL_DEL, fd->fd, &ev) != 0)
690 	return -1;
691     }
692   else
693     {
694       ev.events = EPOLLOUT;
695       if(epoll_ctl(epfd, EPOLL_CTL_MOD, fd->fd, &ev) != 0)
696 	return -1;
697     }
698 #elif defined(HAVE_KQUEUE)
699   EV_SET(&kev, fd->fd, EVFILT_READ, EV_DELETE, 0, 0, fd);
700   if(kevent(kqfd, &kev, 1, NULL, 0, NULL) != 0)
701     return -1;
702 #endif
703   return 0;
704 }
705 
sc_fd_write_add(sc_fd_t * fd)706 static int sc_fd_write_add(sc_fd_t *fd)
707 {
708 #if defined(HAVE_EPOLL)
709   struct epoll_event ev;
710 #elif defined(HAVE_KQUEUE)
711   struct kevent kev;
712 #endif
713 
714   if((fd->flags & FD_FLAG_WRITE) != 0)
715     return 0;
716   fd->flags |= FD_FLAG_WRITE;
717   if((flags & FLAG_SELECT) != 0)
718     return 0;
719 
720 #if defined(HAVE_EPOLL)
721   ev.data.ptr = fd;
722   if((fd->flags & FD_FLAG_READ) == 0)
723     {
724       ev.events = EPOLLOUT;
725       if(epoll_ctl(epfd, EPOLL_CTL_ADD, fd->fd, &ev) != 0)
726 	return -1;
727     }
728   else
729     {
730       ev.events = EPOLLIN | EPOLLOUT;
731       if(epoll_ctl(epfd, EPOLL_CTL_MOD, fd->fd, &ev) != 0)
732 	return -1;
733     }
734 #elif defined(HAVE_KQUEUE)
735   EV_SET(&kev, fd->fd, EVFILT_WRITE, EV_ADD, 0, 0, fd);
736   if(kevent(kqfd, &kev, 1, NULL, 0, NULL) != 0)
737     return -1;
738 #endif
739   return 0;
740 }
741 
sc_fd_write_del(sc_fd_t * fd)742 static int sc_fd_write_del(sc_fd_t *fd)
743 {
744 #if defined(HAVE_EPOLL)
745   struct epoll_event ev;
746 #elif defined(HAVE_KQUEUE)
747   struct kevent kev;
748 #endif
749 
750   if((fd->flags & FD_FLAG_WRITE) == 0)
751     return 0;
752   fd->flags &= ~(FD_FLAG_WRITE);
753   if((flags & FLAG_SELECT) != 0)
754     return 0;
755 
756 #if defined(HAVE_EPOLL)
757   ev.data.ptr = fd;
758   if((fd->flags & FD_FLAG_READ) == 0)
759     {
760       ev.events = 0;
761       if(epoll_ctl(epfd, EPOLL_CTL_DEL, fd->fd, &ev) != 0)
762 	return -1;
763     }
764   else
765     {
766       ev.events = EPOLLIN;
767       if(epoll_ctl(epfd, EPOLL_CTL_MOD, fd->fd, &ev) != 0)
768 	return -1;
769     }
770 #elif defined(HAVE_KQUEUE)
771   EV_SET(&kev, fd->fd, EVFILT_WRITE, EV_DELETE, 0, 0, fd);
772   if(kevent(kqfd, &kev, 1, NULL, 0, NULL) != 0)
773     return -1;
774 #endif
775   return 0;
776 }
777 
778 #ifdef HAVE_OPENSSL
ssl_want_read(sc_master_t * ms)779 static int ssl_want_read(sc_master_t *ms)
780 {
781   uint8_t buf[1024];
782   int pending, rc, size, off = 0;
783 
784   if((pending = BIO_pending(ms->inet_wbio)) < 0)
785     {
786       remote_debug(__func__, "BIO_pending returned %d", pending);
787       return -1;
788     }
789 
790   while(off < pending)
791     {
792       if((size_t)(pending - off) > sizeof(buf))
793 	size = sizeof(buf);
794       else
795 	size = pending - off;
796 
797       if((rc = BIO_read(ms->inet_wbio, buf, size)) <= 0)
798 	{
799 	  if(BIO_should_retry(ms->inet_wbio) == 0)
800 	    remote_debug(__func__, "BIO_read should not retry");
801 	  else
802 	    remote_debug(__func__, "BIO_read returned %d", rc);
803 	  return -1;
804 	}
805       off += rc;
806 
807       scamper_writebuf_send(ms->inet_wb, buf, rc);
808       sc_fd_write_add(&ms->inet_fd);
809     }
810 
811   return pending;
812 }
813 #endif
814 
sc_fd_free(sc_fd_t * sfd)815 static void sc_fd_free(sc_fd_t *sfd)
816 {
817   if(sfd == NULL)
818     return;
819   if(sfd->fd != -1)
820     close(sfd->fd);
821   free(sfd);
822   return;
823 }
824 
sc_fd_alloc(int fd,uint8_t type,sc_unit_t * unit)825 static sc_fd_t *sc_fd_alloc(int fd, uint8_t type, sc_unit_t *unit)
826 {
827   sc_fd_t *sfd;
828   if((sfd = malloc_zero(sizeof(sc_fd_t))) == NULL)
829     return NULL;
830   sfd->fd = fd;
831   sfd->type = type;
832   sfd->unit = unit;
833   return sfd;
834 }
835 
sc_unit_onremove(sc_unit_t * scu)836 static void sc_unit_onremove(sc_unit_t *scu)
837 {
838   scu->node = NULL;
839   scu->list = NULL;
840   return;
841 }
842 
sc_unit_gc(sc_unit_t * scu)843 static void sc_unit_gc(sc_unit_t *scu)
844 {
845   if(scu->gc != 0)
846     return;
847   scu->gc = 1;
848   dlist_node_tail_push(gclist, scu->node);
849   scu->list = gclist;
850   return;
851 }
852 
sc_unit_free(sc_unit_t * scu)853 static void sc_unit_free(sc_unit_t *scu)
854 {
855   if(scu == NULL)
856     return;
857   if(scu->node != NULL)
858     dlist_node_pop(scu->list, scu->node);
859   free(scu);
860   return;
861 }
862 
sc_unit_alloc(uint8_t type,void * data)863 static sc_unit_t *sc_unit_alloc(uint8_t type, void *data)
864 {
865   sc_unit_t *scu;
866   if((scu = malloc_zero(sizeof(sc_unit_t))) == NULL ||
867      (scu->node = dlist_node_alloc(scu)) == NULL)
868     {
869       if(scu != NULL) sc_unit_free(scu);
870       return NULL;
871     }
872   scu->type = type;
873   scu->data = data;
874   return scu;
875 }
876 
sc_message_free(sc_message_t * msg)877 static void sc_message_free(sc_message_t *msg)
878 {
879   if(msg->data != NULL) free(msg->data);
880   free(msg);
881   return;
882 }
883 
sc_master_cmp(const sc_master_t * a,const sc_master_t * b)884 static int sc_master_cmp(const sc_master_t *a, const sc_master_t *b)
885 {
886   if(a->magic_len < b->magic_len) return -1;
887   if(a->magic_len > b->magic_len) return  1;
888   return memcmp(a->magic, b->magic, a->magic_len);
889 }
890 
sc_master_onremove(sc_master_t * ms)891 static void sc_master_onremove(sc_master_t *ms)
892 {
893   ms->node = NULL;
894   return;
895 }
896 
sc_master_channel_find(sc_master_t * ms,uint32_t id)897 static sc_channel_t *sc_master_channel_find(sc_master_t *ms, uint32_t id)
898 {
899   dlist_node_t *dn;
900   sc_channel_t *cn;
901   for(dn=dlist_head_node(ms->channels); dn != NULL; dn=dlist_node_next(dn))
902     {
903       cn = dlist_node_item(dn);
904       if(cn->id == id)
905 	return cn;
906     }
907   return NULL;
908 }
909 
sc_master_channels_onremove(sc_channel_t * cn)910 static void sc_master_channels_onremove(sc_channel_t *cn)
911 {
912   cn->node = NULL;
913   return;
914 }
915 
sc_master_inet_write(sc_master_t * ms,void * ptr,uint16_t len,uint32_t sequence,uint32_t channel)916 static int sc_master_inet_write(sc_master_t *ms, void *ptr, uint16_t len,
917 				uint32_t sequence, uint32_t channel)
918 {
919   uint8_t hdr[SC_MESSAGE_HDRLEN];
920 
921   /* form the header */
922   bytes_htonl(hdr+0, sequence);
923   bytes_htonl(hdr+4, channel);
924   bytes_htons(hdr+8, len);
925 
926 #ifdef HAVE_OPENSSL
927   if(ms->inet_ssl != NULL)
928     {
929       SSL_write(ms->inet_ssl, hdr, SC_MESSAGE_HDRLEN);
930       SSL_write(ms->inet_ssl, ptr, len);
931       if(ssl_want_read(ms) < 0)
932 	{
933 	  remote_debug(__func__, "ssl_want_read failed");
934 	  return -1;
935 	}
936       return 0;
937     }
938 #endif
939 
940   if(scamper_writebuf_send(ms->inet_wb, hdr, SC_MESSAGE_HDRLEN) != 0 ||
941      scamper_writebuf_send(ms->inet_wb, ptr, len) != 0)
942     {
943       remote_debug(__func__, "could not write message");
944       return -1;
945     }
946 
947   sc_fd_write_add(&ms->inet_fd);
948   return 0;
949 }
950 
951 /*
952  * sc_master_inet_send
953  *
954  * transparently handle sending when an SSL socket could be used.
955  */
sc_master_inet_send(sc_master_t * ms,void * ptr,uint16_t len,uint32_t channel,int ack)956 static int sc_master_inet_send(sc_master_t *ms, void *ptr, uint16_t len,
957 			       uint32_t channel, int ack)
958 {
959   sc_message_t *msg = NULL;
960   uint32_t sequence;
961 
962   if(len == 0)
963     {
964       remote_debug(__func__, "invalid length %d", len);
965       return -1;
966     }
967 
968   if(ack == 0 && ms->mode == MASTER_MODE_CONNECT)
969     return 0;
970 
971   /* get a copy of the sequence number before it might change */
972   sequence = ms->snd_nxt;
973 
974   /* if we require the segment to be acknowledged, keep a copy of it */
975   if(ack != 0)
976     {
977       if((msg = malloc(sizeof(sc_message_t))) == NULL)
978 	{
979 	  remote_debug(__func__, "could not malloc message");
980 	  goto err;
981 	}
982       msg->data = NULL;
983       if((msg->data = memdup(ptr, len)) == NULL)
984 	{
985 	  remote_debug(__func__, "could not dup data");
986 	  goto err;
987 	}
988       msg->sequence = sequence;
989       msg->channel = channel;
990       msg->msglen = len;
991       if(slist_tail_push(ms->messages, msg) == NULL)
992 	{
993 	  remote_debug(__func__, "could not push message");
994 	  goto err;
995 	}
996       msg = NULL;
997       ms->snd_nxt++;
998     }
999 
1000   if(ms->mode == MASTER_MODE_CONNECT)
1001     {
1002       remote_debug(__func__, "not sending in connect mode");
1003       return 0;
1004     }
1005 
1006   if(sc_master_inet_write(ms, ptr, len, sequence, channel) != 0)
1007     goto err;
1008   timeval_add_s(&ms->tx_ka, &now, 30);
1009 
1010   return 0;
1011 
1012  err:
1013   if(msg != NULL) sc_message_free(msg);
1014   return -1;
1015 }
1016 
sc_master_inet_free(sc_master_t * ms)1017 static void sc_master_inet_free(sc_master_t *ms)
1018 {
1019   remote_debug(__func__, "%s", ms->name);
1020 
1021   if(ms->inet_fd.fd != -1)
1022     {
1023       sc_fd_read_del(&ms->inet_fd);
1024       sc_fd_write_del(&ms->inet_fd);
1025       close(ms->inet_fd.fd);
1026       ms->inet_fd.fd = -1;
1027     }
1028   if(ms->inet_wb != NULL)
1029     {
1030       scamper_writebuf_free(ms->inet_wb);
1031       ms->inet_wb = NULL;
1032     }
1033 
1034 #ifdef HAVE_OPENSSL
1035   if(ms->inet_ssl != NULL)
1036     {
1037       SSL_free(ms->inet_ssl);
1038     }
1039   else
1040     {
1041       if(ms->inet_wbio != NULL)
1042 	BIO_free(ms->inet_wbio);
1043       if(ms->inet_rbio != NULL)
1044 	BIO_free(ms->inet_rbio);
1045     }
1046   ms->inet_ssl = NULL;
1047   ms->inet_wbio = NULL;
1048   ms->inet_rbio = NULL;
1049 #endif
1050 
1051   return;
1052 }
1053 
1054 /*
1055  * sc_master_unix_create
1056  *
1057  * create a unix domain socket for the scamper instance, that local
1058  * users can connect to in order to interact with the remote scamper
1059  * instance.  The name of the socket is derived from getpeername on the
1060  * Internet socket, and the monitorname if the remote scamper supplied
1061  * that variable.
1062  */
sc_master_unix_create(sc_master_t * ms)1063 static int sc_master_unix_create(sc_master_t *ms)
1064 {
1065   struct sockaddr_un sn;
1066   mode_t mode;
1067   char sab[128], filename[65535], tmp[512];
1068   int fd;
1069 
1070   /*
1071    * these are set so that we know whether or not to take
1072    * responsibility for cleaning them up upon a failure condition.
1073    */
1074   fd = -1;
1075   filename[0] = '\0';
1076 
1077   /* figure out the name for the unix domain socket */
1078   if(sc_fd_peername(&ms->inet_fd, sab, sizeof(sab)) != 0)
1079     goto err;
1080   if(ms->monitorname != NULL)
1081     {
1082       snprintf(tmp, sizeof(tmp), "%s-%s", ms->monitorname, sab);
1083       ms->name = strdup(tmp);
1084     }
1085   else
1086     {
1087       ms->name = strdup(sab);
1088     }
1089   if(ms->name == NULL)
1090     {
1091       remote_debug(__func__, "could not strdup ms->name: %s", strerror(errno));
1092       goto err;
1093     }
1094 
1095   /* create a unix domain socket for the remote scamper process */
1096   if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1097     {
1098       remote_debug(__func__, "could not create unix socket: %s",
1099 		   strerror(errno));
1100       goto err;
1101     }
1102   snprintf(filename, sizeof(filename), "%s/%s", unix_name, ms->name);
1103   if(sockaddr_compose_un((struct sockaddr *)&sn, filename) != 0)
1104     {
1105       filename[0] = '\0'; /* could not actually bind so no unlink */
1106       remote_debug(__func__, "could not compose socket: %s", strerror(errno));
1107       goto err;
1108     }
1109   if(bind(fd, (struct sockaddr *)&sn, sizeof(sn)) != 0)
1110     {
1111       filename[0] = '\0'; /* could not actually bind so no unlink */
1112       remote_debug(__func__, "could not bind unix socket: %s",strerror(errno));
1113       goto err;
1114     }
1115 
1116   /* set the requested permissions on the control sockets */
1117   mode = S_IRWXU;
1118   if(flags & FLAG_ALLOW_G) mode |= S_IRWXG;
1119   if(flags & FLAG_ALLOW_O) mode |= S_IRWXO;
1120   if(chmod(filename, mode) != 0)
1121     {
1122       remote_debug(__func__, "could not chmod: %s", strerror(errno));
1123       goto err;
1124     }
1125 
1126   if(listen(fd, -1) != 0)
1127     {
1128       remote_debug(__func__, "could not listen: %s", strerror(errno));
1129       goto err;
1130     }
1131 
1132   /*
1133    * at this point, allocate the unix_fd structure and take
1134    * responsibility for the socket and filesystem point
1135    */
1136   if((ms->unix_fd = sc_fd_alloc(fd, FD_TYPE_MASTER_UNIX, ms->unit)) == NULL)
1137     {
1138       remote_debug(__func__, "could not alloc unix fd: %s", strerror(errno));
1139       goto err;
1140     }
1141   filename[0] = '\0'; fd = -1;
1142 
1143   if(sc_fd_read_add(ms->unix_fd) != 0)
1144     {
1145       remote_debug(__func__, "could not monitor unix fd: %s", strerror(errno));
1146       goto err;
1147     }
1148 
1149   return 0;
1150 
1151  err:
1152   if(fd != -1) close(fd);
1153   if(filename[0] != '\0') unlink(filename);
1154   return -1;
1155 }
1156 
sc_master_unix_free(sc_master_t * ms)1157 static void sc_master_unix_free(sc_master_t *ms)
1158 {
1159   char filename[65535];
1160 
1161   if(ms->unix_fd != NULL)
1162     {
1163       sc_fd_free(ms->unix_fd);
1164       ms->unix_fd = NULL;
1165       snprintf(filename, sizeof(filename), "%s/%s", unix_name, ms->name);
1166       unlink(filename);
1167     }
1168 
1169   return;
1170 }
1171 
1172 /*
1173  * sc_master_zombie
1174  *
1175  * there was an error reading or writing to the scamper-facing file
1176  * descriptor.  turn the master into a zombie for now, to allow scamper
1177  * to call back and resume.
1178  */
sc_master_zombie(sc_master_t * ms)1179 static void sc_master_zombie(sc_master_t *ms)
1180 {
1181   if(ms->mode == MASTER_MODE_FLUSH)
1182     {
1183       sc_unit_gc(ms->unit);
1184       return;
1185     }
1186   ms->mode = MASTER_MODE_CONNECT;
1187   sc_master_unix_free(ms);
1188   sc_master_inet_free(ms);
1189   timeval_add_s(&ms->zombie, &now, zombie);
1190   remote_debug(__func__, "%s zombie until %d", ms->name, ms->zombie.tv_sec);
1191   return;
1192 }
1193 
sc_master_inet_write_do(sc_master_t * ms)1194 static void sc_master_inet_write_do(sc_master_t *ms)
1195 {
1196   if(scamper_writebuf_write(ms->inet_fd.fd, ms->inet_wb) != 0)
1197     goto zombie;
1198 
1199   if(scamper_writebuf_len(ms->inet_wb) == 0)
1200     {
1201       if(ms->mode == MASTER_MODE_FLUSH)
1202 	{
1203 	  sc_unit_gc(ms->unit);
1204 	  return;
1205 	}
1206       if(sc_fd_write_del(&ms->inet_fd) != 0)
1207 	goto zombie;
1208     }
1209 
1210   return;
1211 
1212  zombie:
1213   if(zombie == 0 || ms->name == NULL)
1214     {
1215       sc_unit_gc(ms->unit);
1216       return;
1217     }
1218   sc_master_zombie(ms);
1219   return;
1220 }
1221 
1222 /*
1223  * sc_master_tx_keepalive
1224  *
1225  * send a keepalive.  do not expect an acknowledgement.
1226  */
sc_master_tx_keepalive(sc_master_t * ms)1227 static int sc_master_tx_keepalive(sc_master_t *ms)
1228 {
1229   uint8_t buf[1];
1230   buf[0] = CONTROL_KEEPALIVE;
1231   return sc_master_inet_send(ms, buf, 1, 0, 0);
1232 }
1233 
sc_master_tx_ack(sc_master_t * ms,uint32_t sequence)1234 static int sc_master_tx_ack(sc_master_t *ms, uint32_t sequence)
1235 {
1236   uint8_t buf[1+4];
1237   buf[0] = CONTROL_ACK;
1238   bytes_htonl(buf+1, sequence);
1239   return sc_master_inet_send(ms, buf, 5, 0, 0);
1240 }
1241 
sc_master_tx_rej(sc_master_t * ms)1242 static int sc_master_tx_rej(sc_master_t *ms)
1243 {
1244   uint8_t buf[1];
1245   ms->mode = MASTER_MODE_FLUSH;
1246   buf[0] = CONTROL_MASTER_REJ;
1247   if(sc_master_inet_send(ms, buf, 1, 0, 0) != 0)
1248     return -1;
1249   return 0;
1250 }
1251 
1252 /*
1253  * sc_master_control_master
1254  *
1255  * a remote scamper connection has said hello.
1256  * create a unix file descriptor to listen locally for drivers that want to
1257  * use it.
1258  *
1259  */
sc_master_control_master(sc_master_t * ms,uint8_t * buf,size_t len)1260 static int sc_master_control_master(sc_master_t *ms, uint8_t *buf, size_t len)
1261 {
1262   char     sab[128];
1263   uint8_t  resp[1+1+128];
1264   uint8_t *magic = NULL;
1265   char    *monitorname = NULL;
1266   uint8_t  magic_len, monitorname_len, u8;
1267   size_t   off = 0;
1268 
1269   /* ensure that there is a magic value present */
1270   if(len == 0 || (magic_len = buf[off++]) == 0)
1271     {
1272       remote_debug(__func__, "magic value not found");
1273       goto err;
1274     }
1275   magic = buf + off;
1276 
1277   /* ensure the magic length value makes sense */
1278   if(len - off < magic_len)
1279     {
1280       remote_debug(__func__, "len %u - off %u < magic_len %u",
1281 		   len, off, magic_len);
1282       goto err;
1283     }
1284   off += magic_len;
1285 
1286   /* check if there is a monitorname supplied */
1287   if(off < len && (monitorname_len = buf[off++]) > 0)
1288     {
1289       if(off + monitorname_len > len)
1290 	{
1291 	  remote_debug(__func__,
1292 		       "malformed monitorname length variable: %u + %u > %u",
1293 		       off, monitorname_len, len);
1294 	  goto err;
1295 	}
1296       monitorname = (char *)(buf+off);
1297       for(u8=0; u8<monitorname_len-1; u8++)
1298 	{
1299 	  if(isalnum(monitorname[u8]) == 0 &&
1300 	     monitorname[u8] != '.' && monitorname[u8] != '-')
1301 	    goto err;
1302 	}
1303       if(monitorname[monitorname_len-1] != '\0')
1304 	goto err;
1305       if((ms->monitorname = memdup(monitorname, monitorname_len)) == NULL)
1306 	goto err;
1307       off += monitorname_len;
1308       assert(off <= len);
1309     }
1310 
1311   /* copy the magic value out.  check that the magic value is unique */
1312   if((ms->magic = memdup(magic, magic_len)) == NULL)
1313     {
1314       remote_debug(__func__, "could not memdup magic: %s", strerror(errno));
1315       goto err;
1316     }
1317   ms->magic_len = magic_len;
1318   if((ms->tree_node = splaytree_insert(mstree, ms)) == NULL)
1319     {
1320       remote_debug(__func__, "could not insert magic node into tree");
1321       goto err;
1322     }
1323 
1324   /* create the unix domain socket for the scamper instance */
1325   if(sc_master_unix_create(ms) != 0)
1326     goto err;
1327 
1328   /* send the list name to the client. do not expect an ack */
1329   if(sc_fd_peername(&ms->inet_fd, sab, sizeof(sab)) != 0)
1330     goto err;
1331   remote_debug(__func__, "%s", sab);
1332   ms->mode = MASTER_MODE_GO;
1333   off = strlen(sab);
1334   resp[0] = CONTROL_MASTER_ID;
1335   resp[1] = off + 1;
1336   memcpy(resp+2, sab, off + 1);
1337   if(sc_master_inet_send(ms, resp, 1 + 1 + off + 1, 0, 0) != 0)
1338     {
1339       remote_debug(__func__, "could not write ID: %s\n", strerror(errno));
1340       goto err;
1341     }
1342 
1343   return 0;
1344 
1345  err:
1346   return -1;
1347 }
1348 
1349 /*
1350  * sc_master_control_channel_fin
1351  *
1352  *
1353  */
sc_master_control_channel_fin(sc_master_t * ms,uint8_t * buf,size_t len)1354 static int sc_master_control_channel_fin(sc_master_t *ms,
1355 					 uint8_t *buf, size_t len)
1356 {
1357   sc_channel_t *cn;
1358   uint32_t id;
1359 
1360   if(len != 4)
1361     {
1362       remote_debug(__func__, "malformed channel fin: %u\n",(uint32_t)len);
1363       return -1;
1364     }
1365 
1366   id = bytes_ntohl(buf);
1367   if((cn = sc_master_channel_find(ms, id)) == NULL)
1368     {
1369       remote_debug(__func__, "could not find channel %u\n", id);
1370       return -1;
1371     }
1372   cn->flags |= CHANNEL_FLAG_EOF_RX;
1373 
1374   if(cn->unix_wb == NULL || scamper_writebuf_gtzero(cn->unix_wb) == 0)
1375     sc_unit_gc(cn->unit);
1376   else
1377     sc_fd_read_del(cn->unix_fd);
1378 
1379   return 0;
1380 }
1381 
sc_master_control_keepalive(sc_master_t * ms,uint8_t * buf,size_t len)1382 static int sc_master_control_keepalive(sc_master_t *ms,uint8_t *buf,size_t len)
1383 {
1384   if(len != 0)
1385     {
1386       remote_debug(__func__, "malformed keepalive: %u", (uint32_t)len);
1387       return -1;
1388     }
1389   return 0;
1390 }
1391 
sc_master_control_ack(sc_master_t * ms,uint8_t * buf,size_t len)1392 static int sc_master_control_ack(sc_master_t *ms, uint8_t *buf, size_t len)
1393 {
1394   uint32_t sequence;
1395   sc_message_t *msg;
1396 
1397   if(len != 4)
1398     {
1399       remote_debug(__func__, "malformed acknowledgement: %u", (uint32_t)len);
1400       return -1;
1401     }
1402 
1403   sequence = bytes_ntohl(buf);
1404   if((msg = slist_head_item(ms->messages)) == NULL)
1405     {
1406       remote_debug(__func__, "nothing to ack: %u", sequence);
1407       return -1;
1408     }
1409   if(msg->sequence != sequence)
1410     {
1411       remote_debug(__func__, "unexpected sequence: %u", sequence);
1412       return -1;
1413     }
1414 
1415   slist_head_pop(ms->messages);
1416   sc_message_free(msg);
1417   return 0;
1418 }
1419 
sc_master_control_resume(sc_master_t * ms,uint8_t * buf,size_t len)1420 static int sc_master_control_resume(sc_master_t *ms, uint8_t *buf, size_t len)
1421 {
1422   sc_master_t fm, *ms2;
1423   sc_message_t *msg;
1424   slist_node_t *sn;
1425   uint8_t *magic = NULL, magic_len;
1426   size_t   off = 0;
1427   uint32_t rcv_nxt, snd_una, snd_nxt;
1428   uint8_t  ok[5];
1429 
1430   /* ensure that there is a magic value present */
1431   if(len == 0 || (magic_len = buf[off++]) == 0)
1432     {
1433       remote_debug(__func__, "magic value not found");
1434       goto err;
1435     }
1436   magic = buf + off;
1437 
1438   /* ensure the magic length value makes sense */
1439   if(off + magic_len > len)
1440     {
1441       remote_debug(__func__, "len %u - off %u < magic_len %u",
1442 		   len, off, magic_len);
1443       goto err;
1444     }
1445   off += magic_len;
1446 
1447   /* ensure there is enough left for the three expected sequence numbers */
1448   if(len - off < 12)
1449     {
1450       remote_debug(__func__, "len %u - off %u < 12 for sequence", len, off);
1451       goto err;
1452     }
1453   rcv_nxt = bytes_ntohl(buf+off); off += 4;
1454   snd_una = bytes_ntohl(buf+off); off += 4;
1455   snd_nxt = bytes_ntohl(buf+off); off += 4;
1456   assert(off <= len);
1457 
1458   /* see if we can find the control socket based on the magic value */
1459   fm.magic = magic;
1460   fm.magic_len = magic_len;
1461   if((ms2 = splaytree_find(mstree, &fm)) == NULL)
1462     {
1463       remote_debug(__func__, "could not find master given magic");
1464       if(sc_master_tx_rej(ms) != 0)
1465 	goto err;
1466       goto done;
1467     }
1468 
1469   /*
1470    * check that the next segment of data that scamper expects from
1471    * remoted is reasonable
1472    */
1473   if(ms2->snd_nxt != rcv_nxt)
1474     {
1475       for(sn=slist_head_node(ms2->messages); sn != NULL; sn=slist_node_next(sn))
1476 	{
1477 	  msg = slist_node_item(sn);
1478 	  if(msg->sequence == rcv_nxt)
1479 	    break;
1480 	}
1481       if(sn == NULL)
1482 	{
1483 	  remote_debug(__func__, "rcv_nxt value %u expected %u",
1484 		       rcv_nxt, ms2->snd_nxt);
1485 	  if(sc_master_tx_rej(ms) != 0)
1486 	    goto err;
1487 	  goto done;
1488 	}
1489     }
1490 
1491   /*
1492    * check that the next segment of data that remoted expects from
1493    * scamper is reasonable
1494    */
1495   if(SEQ_GT(snd_una, ms2->rcv_nxt) || SEQ_GT(ms2->rcv_nxt, snd_nxt))
1496     {
1497       remote_debug(__func__,
1498 		   "scamper's send window %u:%u not expected %u",
1499 		   snd_una, snd_nxt, ms2->rcv_nxt);
1500       if(sc_master_tx_rej(ms) != 0)
1501 	goto err;
1502       goto done;
1503     }
1504 
1505   /*
1506    * go through frames that have not been acknowledged, and remove the frames
1507    * that the remote controller already has
1508    */
1509   while((msg = slist_head_item(ms2->messages)) != NULL)
1510     {
1511       if(SEQ_LT(msg->sequence, rcv_nxt) == 0)
1512 	break;
1513       msg = slist_head_pop(ms2->messages);
1514       sc_message_free(msg);
1515     }
1516 
1517   /* adjust state */
1518   ms2->tx_ka = ms->tx_ka;
1519   ms2->rx_abort = ms->rx_abort;
1520   ms2->zombie = ms->zombie;
1521   ms2->buf_offset = 0;
1522 
1523   /* switch over the file descriptors */
1524   sc_master_inet_free(ms2);
1525   sc_master_unix_free(ms2);
1526   ms2->inet_fd = ms->inet_fd;
1527   ms2->inet_fd.unit = ms2->unit;
1528   ms->inet_fd.fd = -1;
1529   ms2->inet_wb = ms->inet_wb; ms->inet_wb = NULL;
1530 #ifdef HAVE_OPENSSL
1531   ms2->inet_mode = ms->inet_mode;
1532   ms2->inet_ssl  = ms->inet_ssl;  ms->inet_ssl = NULL;
1533   ms2->inet_rbio = ms->inet_rbio; ms->inet_rbio = NULL;
1534   ms2->inet_wbio = ms->inet_wbio; ms->inet_wbio = NULL;
1535 #endif
1536 
1537   if(ms2->inet_fd.flags & FD_FLAG_READ)
1538     {
1539       sc_fd_read_del(&ms2->inet_fd);
1540       sc_fd_read_add(&ms2->inet_fd);
1541     }
1542   if(ms2->inet_fd.flags & FD_FLAG_WRITE)
1543     {
1544       sc_fd_write_del(&ms2->inet_fd);
1545       sc_fd_write_add(&ms2->inet_fd);
1546     }
1547 
1548   /* create a new unix domain socket */
1549   if(sc_master_unix_create(ms2) != 0)
1550     goto err;
1551 
1552   /*
1553    * don't need the incoming sc_master_t, as we've switched the file
1554    * descriptors over.
1555    */
1556   sc_unit_gc(ms->unit);
1557 
1558   /* send an OK message to the scamper instance */
1559   ms2->mode = MASTER_MODE_GO;
1560   ok[0] = CONTROL_MASTER_OK;
1561   bytes_htonl(ok+1, ms2->rcv_nxt);
1562   if(sc_master_inet_write(ms2, ok, 5, 0, 0) != 0)
1563     goto err;
1564   remote_debug(__func__, "ok");
1565 
1566   for(sn=slist_head_node(ms2->messages); sn != NULL; sn=slist_node_next(sn))
1567     {
1568       msg = slist_node_item(sn);
1569       if(sc_master_inet_write(ms2, msg->data, msg->msglen, msg->sequence,
1570 			      msg->channel) != 0)
1571 	goto err;
1572     }
1573 
1574  done:
1575   return 0;
1576 
1577  err:
1578   return -1;
1579 }
1580 
sc_master_control(sc_master_t * ms)1581 static int sc_master_control(sc_master_t *ms)
1582 {
1583   uint32_t seq;
1584   uint16_t msglen;
1585   uint8_t type, *buf;
1586 
1587   seq = bytes_ntohl(ms->buf);
1588   msglen = bytes_ntohs(ms->buf+8);
1589   buf = ms->buf + SC_MESSAGE_HDRLEN;
1590 
1591   if(msglen < 1)
1592     {
1593       remote_debug(__func__, "malformed control msg: %u", msglen);
1594       return -1;
1595     }
1596 
1597   type = buf[0];
1598   buf++; msglen--;
1599 
1600   if(ms->mode == MASTER_MODE_CONNECT)
1601     {
1602       /* we expect sequence zero.  no acks for any of these messages. */
1603       if(seq != 0)
1604 	{
1605 	  remote_debug(__func__, "expected sequence zero in mode connect");
1606 	  return -1;
1607 	}
1608       switch(type)
1609 	{
1610 	case CONTROL_MASTER_NEW:
1611 	  return sc_master_control_master(ms, buf, msglen);
1612 	case CONTROL_MASTER_RES:
1613 	  return sc_master_control_resume(ms, buf, msglen);
1614 	}
1615     }
1616   else if(ms->mode == MASTER_MODE_GO)
1617     {
1618       /* check the sequence number is what we expect */
1619       if(seq != ms->rcv_nxt)
1620 	{
1621 	  remote_debug(__func__, "got seq %u expected %u", seq, ms->rcv_nxt);
1622 	  return -1;
1623 	}
1624 
1625       if(type == CONTROL_CHANNEL_FIN)
1626 	{
1627 	  if(sc_master_tx_ack(ms, seq) != 0)
1628 	    return -1;
1629 	  ms->rcv_nxt++;
1630 	}
1631 
1632       switch(type)
1633 	{
1634 	case CONTROL_CHANNEL_FIN:
1635 	  return sc_master_control_channel_fin(ms, buf, msglen);
1636 	case CONTROL_KEEPALIVE:
1637 	  return sc_master_control_keepalive(ms, buf, msglen);
1638 	case CONTROL_ACK:
1639 	  return sc_master_control_ack(ms, buf, msglen);
1640 	}
1641     }
1642 
1643   remote_debug(__func__, "unhandled type %d", type);
1644   return -1;
1645 }
1646 
1647 /*
1648  * sc_master_inet_read_cb
1649  *
1650  * process data from the master inet-facing socket.  the data has been
1651  * through the SSL decoding routines, if necessary.
1652  *
1653  * todo: make this zero copy when the entire message is intact in the buf.
1654  */
sc_master_inet_read_cb(sc_master_t * ms,uint8_t * buf,size_t len)1655 static void sc_master_inet_read_cb(sc_master_t *ms, uint8_t *buf, size_t len)
1656 {
1657   sc_channel_t *channel;
1658   uint32_t seq, id;
1659   uint16_t msglen, x, y;
1660   size_t off = 0;
1661   uint8_t *ptr;
1662 
1663   while(off < len)
1664     {
1665       /* to start with, ensure that we have a complete header */
1666       while(ms->buf_offset < SC_MESSAGE_HDRLEN && off < len)
1667 	ms->buf[ms->buf_offset++] = buf[off++];
1668       if(off == len)
1669 	return;
1670 
1671       /* figure out how large the message is supposed to be */
1672       seq = bytes_ntohl(ms->buf);
1673       id = bytes_ntohl(ms->buf+4);
1674       msglen = bytes_ntohs(ms->buf+8);
1675 
1676       /* ensure the sequence number is what we expect */
1677       if(seq != ms->rcv_nxt)
1678 	{
1679 	  remote_debug(__func__, "got seq %u expected %u", seq, ms->rcv_nxt);
1680 	  goto err;
1681 	}
1682 
1683       /* check the channel id is valid */
1684       channel = NULL;
1685       if(id != 0 && (channel = sc_master_channel_find(ms, id)) == NULL)
1686 	{
1687 	  remote_debug(__func__, "could not find channel %u", id);
1688 	  goto err;
1689 	}
1690 
1691       /* figure out how to build the message */
1692       x = msglen - (ms->buf_offset - SC_MESSAGE_HDRLEN);
1693       y = len - off;
1694 
1695       if(y < x)
1696 	{
1697 	  /* if we cannot complete the message, buffer what we have */
1698 	  memcpy(ms->buf + ms->buf_offset, buf+off, y);
1699 	  ms->buf_offset += y;
1700 	  return;
1701 	}
1702 
1703       /* we now have a complete message */
1704       memcpy(ms->buf + ms->buf_offset, buf+off, x);
1705       off += x;
1706 
1707       /* reset the buf_offset for the next message */
1708       ms->buf_offset = 0;
1709 
1710       /* get a pointer to the data */
1711       ptr = ms->buf + SC_MESSAGE_HDRLEN;
1712 
1713       /* if the message is a control message */
1714       if(id == 0)
1715 	{
1716 	  if(sc_master_control(ms) != 0)
1717 	    goto err;
1718 	  continue;
1719 	}
1720 
1721       if(sc_master_tx_ack(ms, seq) != 0)
1722 	goto err;
1723       ms->rcv_nxt++;
1724 
1725       /* the unix domain socket may have gone away but we need to flush */
1726       if(channel->unix_wb != NULL)
1727 	{
1728 	  if(scamper_writebuf_send(channel->unix_wb, ptr, msglen) != 0)
1729 	    sc_unit_gc(channel->unit);
1730 	  sc_fd_write_add(channel->unix_fd);
1731 	}
1732     }
1733 
1734   return;
1735 
1736  err:
1737   sc_unit_gc(ms->unit);
1738   return;
1739 }
1740 
1741 /*
1742  * sc_master_inet_read_do
1743  *
1744  */
sc_master_inet_read_do(sc_master_t * ms)1745 static void sc_master_inet_read_do(sc_master_t *ms)
1746 {
1747   ssize_t rrc;
1748   uint8_t buf[4096];
1749 
1750 #ifdef HAVE_OPENSSL
1751   int rc;
1752 #endif
1753 
1754   if((rrc = read(ms->inet_fd.fd, buf, sizeof(buf))) < 0)
1755     {
1756       if(errno == EAGAIN || errno == EINTR)
1757 	return;
1758       remote_debug(__func__, "read failed: %s", strerror(errno));
1759       goto zombie;
1760     }
1761 
1762   if(rrc == 0)
1763     {
1764       remote_debug(__func__, "%s disconnected", ms->name);
1765       goto zombie;
1766     }
1767 
1768   timeval_add_s(&ms->rx_abort, &now, 60);
1769 
1770 #ifdef HAVE_OPENSSL
1771   if(ms->inet_ssl != NULL)
1772     {
1773       BIO_write(ms->inet_rbio, buf, rrc);
1774       ERR_clear_error();
1775 
1776       if(ms->inet_mode == SSL_MODE_ACCEPT)
1777 	{
1778 	  if((rc = SSL_accept(ms->inet_ssl)) > 0)
1779 	    {
1780 	      ms->inet_mode = SSL_MODE_ESTABLISHED;
1781 	      if(ssl_want_read(ms) < 0)
1782 		{
1783 		  remote_debug(__func__, "ssl_want_read failed");
1784 		  goto err;
1785 		}
1786 	      return;
1787 	    }
1788 	}
1789       else
1790 	{
1791 	  while((rc = SSL_read(ms->inet_ssl, buf, sizeof(buf))) > 0)
1792 	    {
1793 	      sc_master_inet_read_cb(ms, buf, (size_t)rc);
1794 	      /*
1795 	       * the callback function might end up disconnecting the
1796 	       * SSL connection
1797 	       */
1798 	      if(ms->inet_ssl == NULL)
1799 		return;
1800 	    }
1801 	}
1802 
1803       if((rc = SSL_get_error(ms->inet_ssl, rc)) == SSL_ERROR_WANT_READ)
1804 	{
1805 	  if(ssl_want_read(ms) < 0)
1806 	    {
1807 	      remote_debug(__func__, "ssl_want_read failed");
1808 	      goto err;
1809 	    }
1810 	}
1811       else if(rc != SSL_ERROR_WANT_WRITE)
1812 	{
1813 	  remote_debug(__func__, "mode %s rc %d",
1814 		       ms->inet_mode == SSL_MODE_ACCEPT ? "accept" : "estab",
1815 		       rc);
1816 	  goto err;
1817 	}
1818 
1819       return;
1820     }
1821 #endif
1822 
1823   sc_master_inet_read_cb(ms, buf, (size_t)rrc);
1824   return;
1825 
1826   /* if we are keeping state for disconnected scamper instances */
1827  zombie:
1828   if(zombie == 0 || ms->name == NULL)
1829     sc_unit_gc(ms->unit);
1830   else
1831     sc_master_zombie(ms);
1832   return;
1833 
1834  err:
1835   sc_unit_gc(ms->unit);
1836   return;
1837 }
1838 
1839 /*
1840  * sc_master_unix_accept_do
1841  *
1842  * a local process has connected to the unix domain socket that
1843  * corresponds to a remote scamper process.  accept the socket and
1844  * cause the remote scamper process to create a new channel.
1845  */
sc_master_unix_accept_do(sc_master_t * ms)1846 static void sc_master_unix_accept_do(sc_master_t *ms)
1847 {
1848   struct sockaddr_storage ss;
1849   socklen_t socklen = sizeof(ss);
1850   sc_channel_t *cn = NULL;
1851   uint8_t msg[1+4];
1852   int s = -1;
1853 
1854   if((s = accept(ms->unix_fd->fd, (struct sockaddr *)&ss, &socklen)) == -1)
1855     {
1856       remote_debug(__func__, "accept failed: %s", strerror(errno));
1857       goto err;
1858     }
1859 
1860   if((cn = malloc_zero(sizeof(sc_channel_t))) == NULL)
1861     goto err;
1862   cn->id = ms->next_channel++;
1863   if(ms->next_channel == 0)
1864     ms->next_channel++;
1865 
1866   /* allocate a unit to describe this structure */
1867   if((cn->unit = sc_unit_alloc(UNIT_TYPE_CHANNEL, cn)) == NULL)
1868     {
1869       remote_debug(__func__, "could not alloc unit: %s", strerror(errno));
1870       goto err;
1871     }
1872 
1873   if((cn->unix_fd = sc_fd_alloc(s, FD_TYPE_CHANNEL_UNIX, cn->unit)) == NULL)
1874     {
1875       remote_debug(__func__, "could not alloc unix_fd: %s", strerror(errno));
1876       goto err;
1877     }
1878   s = -1;
1879   sc_fd_read_add(cn->unix_fd);
1880 
1881   if((cn->unix_wb = scamper_writebuf_alloc()) == NULL)
1882     goto err;
1883   if((cn->node = dlist_tail_push(ms->channels, cn)) == NULL)
1884     goto err;
1885   cn->master = ms;
1886 
1887   /* send a new channel message to scamper. expect an acknowledgement */
1888   msg[0] = CONTROL_CHANNEL_NEW;
1889   bytes_htonl(msg+1, cn->id);
1890   if(sc_master_inet_send(ms, msg, 1 + 4, 0, 1) != 0)
1891     goto err;
1892 
1893   return;
1894 
1895  err:
1896   if(s != -1) close(s);
1897   if(cn != NULL) sc_channel_free(cn);
1898   return;
1899 }
1900 
1901 /*
1902  * sc_master_free
1903  *
1904  * clean up the sc_master_t.
1905  */
sc_master_free(sc_master_t * ms)1906 static void sc_master_free(sc_master_t *ms)
1907 {
1908   if(ms == NULL)
1909     return;
1910 
1911   sc_master_unix_free(ms);
1912 
1913   if(ms->channels != NULL)
1914     dlist_free_cb(ms->channels, (dlist_free_t)sc_channel_free);
1915   if(ms->messages != NULL)
1916     slist_free_cb(ms->messages, (slist_free_t)sc_message_free);
1917 
1918   if(ms->unit != NULL) sc_unit_free(ms->unit);
1919 
1920   sc_master_inet_free(ms);
1921 
1922   if(ms->tree_node != NULL) splaytree_remove_node(mstree, ms->tree_node);
1923   if(ms->name != NULL) free(ms->name);
1924   if(ms->monitorname != NULL) free(ms->monitorname);
1925   if(ms->magic != NULL) free(ms->magic);
1926   if(ms->node != NULL) dlist_node_pop(mslist, ms->node);
1927   free(ms);
1928   return;
1929 }
1930 
sc_master_alloc(int fd)1931 static sc_master_t *sc_master_alloc(int fd)
1932 {
1933   sc_master_t *ms = NULL;
1934 
1935 #ifdef HAVE_OPENSSL
1936   int rc;
1937 #endif
1938 
1939   if((ms = malloc_zero(sizeof(sc_master_t))) == NULL)
1940     return NULL;
1941   ms->inet_fd.fd = fd; fd = -1;
1942   ms->inet_fd.type = FD_TYPE_MASTER_INET;
1943 
1944   if((ms->channels = dlist_alloc()) == NULL)
1945     {
1946       remote_debug(__func__, "could not alloc channels: %s", strerror(errno));
1947       goto err;
1948     }
1949   dlist_onremove(ms->channels, (dlist_onremove_t)sc_master_channels_onremove);
1950   ms->next_channel = 1;
1951 
1952   /* allocate a unit to describe this */
1953   if((ms->unit = sc_unit_alloc(UNIT_TYPE_MASTER, ms)) == NULL)
1954     {
1955       remote_debug(__func__, "could not alloc unit: %s", strerror(errno));
1956       goto err;
1957     }
1958   ms->inet_fd.unit = ms->unit;
1959 
1960   if((ms->inet_wb = scamper_writebuf_alloc()) == NULL)
1961     {
1962       remote_debug(__func__, "could not alloc wb: %s", strerror(errno));
1963       goto err;
1964     }
1965 
1966   if((ms->messages = slist_alloc()) == NULL)
1967     {
1968       remote_debug(__func__, "could not alloc messages: %s", strerror(errno));
1969       goto err;
1970     }
1971 
1972 #ifdef HAVE_OPENSSL
1973   if(tls_certfile != NULL)
1974     {
1975       if((ms->inet_wbio = BIO_new(BIO_s_mem())) == NULL ||
1976 	 (ms->inet_rbio = BIO_new(BIO_s_mem())) == NULL ||
1977 	 (ms->inet_ssl = SSL_new(tls_ctx)) == NULL)
1978 	{
1979 	  remote_debug(__func__, "could not alloc SSL");
1980 	  goto err;
1981 	}
1982       SSL_set_bio(ms->inet_ssl, ms->inet_rbio, ms->inet_wbio);
1983       SSL_set_accept_state(ms->inet_ssl);
1984       rc = SSL_accept(ms->inet_ssl);
1985       assert(rc == -1);
1986       if((rc = SSL_get_error(ms->inet_ssl, rc)) != SSL_ERROR_WANT_READ)
1987 	{
1988 	  remote_debug(__func__, "unexpected %d from SSL_accept", rc);
1989 	  goto err;
1990 	}
1991       if(ssl_want_read(ms) < 0)
1992 	goto err;
1993     }
1994 #endif
1995 
1996   return ms;
1997 
1998  err:
1999   if(ms != NULL) sc_master_free(ms);
2000   if(fd != -1) close(fd);
2001   return NULL;
2002 }
2003 
2004 /*
2005  * sc_channel_unix_write_do
2006  *
2007  * we can write to the unix fd without blocking, so do so.
2008  */
sc_channel_unix_write_do(sc_channel_t * cn)2009 static void sc_channel_unix_write_do(sc_channel_t *cn)
2010 {
2011   int gtzero;
2012 
2013   /* if we did a read which returned -1, then the unix_fd will be null */
2014   if(cn->unix_fd == NULL)
2015     return;
2016 
2017   if(scamper_writebuf_write(cn->unix_fd->fd, cn->unix_wb) != 0)
2018     {
2019       remote_debug(__func__, "write to %s channel %u failed",
2020 		   cn->master->name, cn->id);
2021       goto err;
2022     }
2023 
2024   /*
2025    * if we still have data to write, then wait until we get signal to
2026    * write again
2027    */
2028   if((gtzero = scamper_writebuf_gtzero(cn->unix_wb)) != 0)
2029     return;
2030 
2031   /* nothing more to write, so remove fd */
2032   if(sc_fd_write_del(cn->unix_fd) != 0)
2033     {
2034       remote_debug(__func__, "could not delete unix write for %s channel %u",
2035 		   cn->master->name, cn->id);
2036       goto err;
2037     }
2038 
2039   /* got an EOF, so we're done now */
2040   if((cn->flags & CHANNEL_FLAG_EOF_RX) != 0)
2041     {
2042       remote_debug(__func__, "received EOF for %s channel %u",
2043 		   cn->master->name, cn->id);
2044       sc_unit_gc(cn->unit);
2045       return;
2046     }
2047 
2048   return;
2049 
2050  err:
2051   /* got an error trying to write, so we're done */
2052   sc_fd_free(cn->unix_fd); cn->unix_fd = NULL;
2053   scamper_writebuf_free(cn->unix_wb); cn->unix_wb = NULL;
2054 
2055   /* we've received an EOF, we're done */
2056   if((cn->flags & CHANNEL_FLAG_EOF_RX) != 0)
2057     {
2058       sc_unit_gc(cn->unit);
2059       return;
2060     }
2061   return;
2062 }
2063 
2064 /*
2065  * sc_channel_unix_read_do
2066  *
2067  * a local client process has written to a unix domain socket, which
2068  * we will process line by line.
2069  */
sc_channel_unix_read_do(sc_channel_t * cn)2070 static void sc_channel_unix_read_do(sc_channel_t *cn)
2071 {
2072   ssize_t rc;
2073   uint8_t buf[4096];
2074 
2075   if((rc = read(cn->unix_fd->fd, buf, sizeof(buf))) <= 0)
2076     {
2077       if(rc == -1 && (errno == EAGAIN || errno == EINTR))
2078 	return;
2079 
2080       /* send an EOF if we haven't tx'd or rx'd an EOF. expect an ack */
2081       if((cn->flags & (CHANNEL_FLAG_EOF_RX|CHANNEL_FLAG_EOF_TX)) == 0)
2082 	{
2083 	  buf[0] = CONTROL_CHANNEL_FIN;
2084 	  bytes_htonl(buf+1, cn->id);
2085 	  sc_master_inet_send(cn->master, buf, 5, 0, 1);
2086 	  cn->flags |= CHANNEL_FLAG_EOF_TX;
2087 	}
2088 
2089       /* if we've received an EOF, we're done */
2090       if((cn->flags & CHANNEL_FLAG_EOF_RX) != 0)
2091 	{
2092 	  sc_unit_gc(cn->unit);
2093 	  return;
2094 	}
2095 
2096       /*
2097        * if we've received an error, close down the file descriptor
2098        * and write buf.  we keep the channel around so that when we
2099        * receive an EOF, we can match it and clean it up.
2100        */
2101       if(rc == -1)
2102 	{
2103 	  sc_fd_free(cn->unix_fd); cn->unix_fd = NULL;
2104 	  scamper_writebuf_free(cn->unix_wb); cn->unix_wb = NULL;
2105 	}
2106       else
2107 	{
2108 	  sc_fd_read_del(cn->unix_fd);
2109 	}
2110       return;
2111     }
2112 
2113   /* send the message to scamper, expecting an acknowledgement */
2114   sc_master_inet_send(cn->master, buf, rc, cn->id, 1);
2115 
2116   return;
2117 }
2118 
sc_channel_free(sc_channel_t * cn)2119 static void sc_channel_free(sc_channel_t *cn)
2120 {
2121   if(cn == NULL)
2122     return;
2123   if(cn->master != NULL && cn->node != NULL)
2124     dlist_node_pop(cn->master->channels, cn->node);
2125   if(cn->unix_fd != NULL) sc_fd_free(cn->unix_fd);
2126   if(cn->unix_lp != NULL) scamper_linepoll_free(cn->unix_lp, 0);
2127   if(cn->unix_wb != NULL) scamper_writebuf_free(cn->unix_wb);
2128   if(cn->unit != NULL) sc_unit_free(cn->unit);
2129   free(cn);
2130   return;
2131 }
2132 
2133 /*
2134  * serversocket_accept
2135  *
2136  * a new connection has arrived.  accept the new connection while we wait
2137  * to understand the intention behind the socket.
2138  */
serversocket_accept(int ss)2139 static int serversocket_accept(int ss)
2140 {
2141   struct sockaddr_storage sas;
2142   sc_master_t *ms = NULL;
2143   socklen_t slen;
2144   int inet_fd = -1;
2145   char buf[256];
2146 
2147   slen = sizeof(ss);
2148   if((inet_fd = accept(ss, (struct sockaddr *)&sas, &slen)) == -1)
2149     {
2150       remote_debug(__func__, "could not accept: %s", strerror(errno));
2151       goto err;
2152     }
2153   if(fcntl_set(inet_fd, O_NONBLOCK) == -1)
2154     {
2155       remote_debug(__func__, "could not set O_NONBLOCK: %s", strerror(errno));
2156       goto err;
2157     }
2158 
2159   ms = sc_master_alloc(inet_fd);
2160   inet_fd = -1;
2161   if(ms == NULL)
2162     goto err;
2163 
2164   if(sc_fd_peername(&ms->inet_fd, buf, sizeof(buf)) == 0)
2165     remote_debug(__func__, "%s", buf);
2166 
2167   if(sc_fd_read_add(&ms->inet_fd) != 0)
2168     {
2169       remote_debug(__func__, "could not monitor inet fd: %s", strerror(errno));
2170       goto err;
2171     }
2172 
2173   timeval_add_s(&ms->rx_abort, &now, 30);
2174   timeval_cpy(&ms->tx_ka, &ms->rx_abort);
2175 
2176   if((ms->node = dlist_tail_push(mslist, ms)) == NULL)
2177     {
2178       remote_debug(__func__, "could not push to mslist: %s", strerror(errno));
2179       goto err;
2180     }
2181 
2182   return 0;
2183 
2184  err:
2185   if(inet_fd != -1) close(inet_fd);
2186   if(ms != NULL) sc_master_free(ms);
2187   return -1;
2188 }
2189 
serversocket_init_sa(const struct sockaddr * sa)2190 static int serversocket_init_sa(const struct sockaddr *sa)
2191 {
2192   char buf[256];
2193   int opt, fd = -1;
2194 
2195   if((fd = socket(sa->sa_family, SOCK_STREAM, IPPROTO_TCP)) < 0)
2196     {
2197       remote_debug(__func__, "could not open %s socket: %s",
2198 		   sa->sa_family == AF_INET ? "ipv4" : "ipv6", strerror(errno));
2199       goto err;
2200     }
2201 
2202   opt = 1;
2203   if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) != 0)
2204     {
2205       remote_debug(__func__, "could not set SO_REUSEADDR on %s socket: %s",
2206 		   sa->sa_family == AF_INET ? "ipv4" : "ipv6", strerror(errno));
2207       goto err;
2208     }
2209 
2210 #ifdef IPV6_V6ONLY
2211   if(sa->sa_family == PF_INET6)
2212     {
2213       opt = 1;
2214       if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
2215 		    (char *)&opt, sizeof(opt)) != 0)
2216 	{
2217 	  remote_debug(__func__, "could not set IPV6_V6ONLY: %s",
2218 		       strerror(errno));
2219 	  goto err;
2220 	}
2221     }
2222 #endif
2223 
2224   if(bind(fd, sa, sockaddr_len(sa)) != 0)
2225     {
2226       remote_debug(__func__, "could not bind %s socket to %s: %s",
2227 		   sa->sa_family == AF_INET ? "ipv4" : "ipv6",
2228 		   sockaddr_tostr(sa, buf, sizeof(buf)), strerror(errno));
2229       goto err;
2230     }
2231 
2232   if(listen(fd, -1) != 0)
2233     {
2234       remote_debug(__func__, "could not listen %s socket: %s",
2235 		   sa->sa_family == AF_INET ? "ipv4" : "ipv6", strerror(errno));
2236       goto err;
2237     }
2238 
2239   return fd;
2240 
2241  err:
2242   if(fd != -1) close(fd);
2243   return -1;
2244 }
2245 
2246 /*
2247  * serversocket_init
2248  *
2249  * create two sockets so that we can use both IPv4 and IPv6 for incoming
2250  * connections from remote scamper processes.
2251  */
serversocket_init(void)2252 static int serversocket_init(void)
2253 {
2254   struct sockaddr_storage sas;
2255   int i, pf, fd, x;
2256 
2257   if(ss_addr != NULL)
2258     {
2259       if(sockaddr_compose_str((struct sockaddr *)&sas, ss_addr, ss_port) != 0)
2260 	{
2261 	  remote_debug(__func__, "could not compose sockaddr");
2262 	  return -1;
2263 	}
2264       if((fd = serversocket_init_sa((struct sockaddr *)&sas)) == -1)
2265 	return -1;
2266       serversockets[0] = fd;
2267       return 0;
2268     }
2269 
2270   x = 0;
2271   for(i=0; i<2; i++)
2272     {
2273       pf = i == 0 ? PF_INET : PF_INET6;
2274       if((pf == PF_INET  && (options & OPT_IPV6) != 0) ||
2275 	 (pf == PF_INET6 && (options & OPT_IPV4) != 0))
2276 	continue;
2277 
2278       sockaddr_compose((struct sockaddr *)&sas, pf, NULL, ss_port);
2279       if((fd = serversocket_init_sa((struct sockaddr *)&sas)) == -1)
2280 	return -1;
2281       serversockets[x++] = fd;
2282     }
2283 
2284   return 0;
2285 }
2286 
2287 /*
2288  * unixdomain_direxists
2289  *
2290  * make sure the directory specified actually exists
2291  */
unixdomain_direxists(void)2292 static int unixdomain_direxists(void)
2293 {
2294   struct stat sb;
2295   if(stat(unix_name, &sb) != 0)
2296     {
2297       usage(OPT_UNIX);
2298       remote_debug(__func__,"could not stat %s: %s",unix_name,strerror(errno));
2299       return -1;
2300     }
2301   if((sb.st_mode & S_IFDIR) != 0)
2302     return 0;
2303   usage(OPT_UNIX);
2304   remote_debug(__func__, "%s is not a directory", unix_name);
2305   return -1;
2306 }
2307 
cleanup(void)2308 static void cleanup(void)
2309 {
2310   int i;
2311 
2312   for(i=0; i<2; i++)
2313     close(serversockets[i]);
2314 
2315   if(ss_addr != NULL)
2316     free(ss_addr);
2317   if(mslist != NULL)
2318     dlist_free_cb(mslist, (dlist_free_t)sc_master_free);
2319   if(mstree != NULL)
2320     splaytree_free(mstree, NULL);
2321 
2322 #ifdef HAVE_OPENSSL
2323   if(tls_ctx != NULL) SSL_CTX_free(tls_ctx);
2324 #endif
2325 
2326   if(gclist != NULL) dlist_free(gclist);
2327 
2328   if(pidfile != NULL)
2329     {
2330       unlink(pidfile);
2331       free(pidfile);
2332     }
2333 
2334 #ifdef HAVE_EPOLL
2335   if(epfd != -1) close(epfd);
2336 #endif
2337 
2338 #ifdef HAVE_KQUEUE
2339   if(kqfd != -1) close(kqfd);
2340 #endif
2341 
2342   return;
2343 }
2344 
2345 #ifdef HAVE_OPENSSL
remoted_tlsctx(void)2346 static int remoted_tlsctx(void)
2347 {
2348   if((tls_ctx = SSL_CTX_new(SSLv23_method())) == NULL)
2349     return -1;
2350   if(SSL_CTX_use_certificate_chain_file(tls_ctx,tls_certfile)!=1)
2351     {
2352       remote_debug(__func__, "could not SSL_CTX_use_certificate_file");
2353       ERR_print_errors_fp(stderr);
2354       return -1;
2355     }
2356   if(SSL_CTX_use_PrivateKey_file(tls_ctx,tls_privfile,SSL_FILETYPE_PEM)!=1)
2357     {
2358       remote_debug(__func__, "could not SSL_CTX_use_PrivateKey_file");
2359       ERR_print_errors_fp(stderr);
2360       return -1;
2361     }
2362   SSL_CTX_set_verify(tls_ctx, SSL_VERIFY_NONE, NULL);
2363   SSL_CTX_set_options(tls_ctx,
2364 		      SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
2365 		      SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
2366   return 0;
2367 }
2368 
remoted_tlsctx_reload(void)2369 static int remoted_tlsctx_reload(void)
2370 {
2371   if(tls_ctx == NULL)
2372     return 0;
2373   SSL_CTX_free(tls_ctx); tls_ctx = NULL;
2374   return remoted_tlsctx();
2375 }
2376 #endif
2377 
remoted_pidfile(void)2378 static int remoted_pidfile(void)
2379 {
2380   char buf[32];
2381   mode_t mode;
2382   size_t len;
2383   int fd, fd_flags = O_WRONLY | O_TRUNC | O_CREAT;
2384 
2385 #ifndef _WIN32
2386   pid_t pid = getpid();
2387   mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2388 #else
2389   DWORD pid = GetCurrentProcessId();
2390   mode = _S_IREAD | _S_IWRITE;
2391 #endif
2392 
2393   if((fd = open(pidfile, fd_flags, mode)) == -1)
2394     {
2395       remote_debug(__func__, "could not open %s: %s", pidfile, strerror(errno));
2396       return -1;
2397     }
2398 
2399   snprintf(buf, sizeof(buf), "%ld\n", (long)pid);
2400   len = strlen(buf);
2401   if(write_wrap(fd, buf, NULL, len) != 0)
2402     {
2403       remote_debug(__func__, "could not write pid: %s", strerror(errno));
2404       goto err;
2405     }
2406   close(fd);
2407 
2408   return 0;
2409 
2410  err:
2411   if(fd != -1) close(fd);
2412   return -1;
2413 }
2414 
remoted_sighup(int sig)2415 static void remoted_sighup(int sig)
2416 {
2417   if(sig == SIGHUP)
2418     reload = 1;
2419   return;
2420 }
2421 
remoted_sigint(int sig)2422 static void remoted_sigint(int sig)
2423 {
2424   if(sig == SIGINT)
2425     stop = 1;
2426   return;
2427 }
2428 
2429 #if defined(HAVE_EPOLL) || defined(HAVE_KQUEUE)
2430 #if defined(HAVE_EPOLL)
epoll_loop(void)2431 static int epoll_loop(void)
2432 #else
2433 static int kqueue_loop(void)
2434 #endif
2435 {
2436 #if defined(HAVE_EPOLL)
2437   struct epoll_event events[1024];
2438   int events_c = sizeof(events) / sizeof(struct epoll_event);
2439   int timeout;
2440 #else
2441   struct kevent events[1024];
2442   int events_c = sizeof(events) / sizeof(struct kevent);
2443   struct timespec ts, *timeout;
2444 #endif
2445   struct timeval tv, to, *tvp;
2446   sc_master_t *ms;
2447   dlist_node_t *dn;
2448   sc_fd_t *scfd, scfd_ss[2];
2449   sc_unit_t *scu;
2450   int i, rc;
2451 
2452 #if defined(HAVE_EPOLL)
2453   if((epfd = epoll_create(1000)) == -1)
2454     {
2455       remote_debug(__func__, "epoll_create failed: %s", strerror(errno));
2456       return -1;
2457     }
2458 #else
2459   if((kqfd = kqueue()) == -1)
2460     {
2461       remote_debug(__func__, "kqueue failed: %s", strerror(errno));
2462       return -1;
2463     }
2464 #endif
2465 
2466   /* add the server sockets to the poll set */
2467   memset(&scfd_ss, 0, sizeof(scfd_ss));
2468   for(i=0; i<2; i++)
2469     {
2470       if(serversockets[i] == -1)
2471 	continue;
2472       scfd_ss[i].type = FD_TYPE_SERVER;
2473       scfd_ss[i].fd = serversockets[i];
2474       if(sc_fd_read_add(&scfd_ss[i]) != 0)
2475 	return -1;
2476     }
2477 
2478   /* main event loop */
2479   while(stop == 0)
2480     {
2481       if(reload != 0)
2482 	{
2483 	  reload = 0;
2484 #ifdef HAVE_OPENSSL
2485 	  if(remoted_tlsctx_reload() != 0)
2486 	    return -1;
2487 #endif
2488 	}
2489 
2490 #if defined(HAVE_EPOLL)
2491       timeout = -1;
2492 #else
2493       timeout = NULL;
2494 #endif
2495       rc = 0;
2496       if((dn = dlist_head_node(mslist)) != NULL)
2497 	{
2498 	  gettimeofday_wrap(&now);
2499 
2500 	  /* to start with, handle keepalives */
2501 	  while(dn != NULL)
2502 	    {
2503 	      ms = dlist_node_item(dn);
2504 	      dn = dlist_node_next(dn);
2505 
2506 	      /* check if it is time to take care of a zombie */
2507 	      if(ms->inet_fd.fd == -1)
2508 		{
2509 		  if(timeval_cmp(&now, &ms->zombie) < 0)
2510 		    {
2511 		      timeval_diff_tv(&tv, &now, &ms->zombie);
2512 		      goto set_timeout;
2513 		    }
2514 		  remote_debug(__func__, "removing %s zombie", ms->name);
2515 		  sc_master_free(ms);
2516 		  continue;
2517 		}
2518 
2519 	      /* if the connection has gone silent, abort */
2520 	      if(timeval_cmp(&now, &ms->rx_abort) >= 0)
2521 		{
2522 		  if(zombie > 0 && ms->name != NULL)
2523 		    {
2524 		      sc_master_zombie(ms);
2525 		      timeval_diff_tv(&tv, &now, &ms->zombie);
2526 		      goto set_timeout;
2527 		    }
2528 		  sc_master_free(ms);
2529 		  continue;
2530 		}
2531 
2532 	      /*
2533 	       * ensure we send something every 30 seconds.
2534 	       * unix_fd being not null signifies the remote controller
2535 	       * has received an opening "master" frame.
2536 	       */
2537 	      if(ms->unix_fd != NULL && timeval_cmp(&now, &ms->tx_ka) >= 0)
2538 		{
2539 		  timeval_add_s(&ms->tx_ka, &now, 30);
2540 		  if(sc_master_tx_keepalive(ms) != 0)
2541 		    {
2542 		      sc_master_free(ms);
2543 		      continue;
2544 		    }
2545 		}
2546 
2547 	      /* now figure out timeout to set */
2548 	      if(timeval_cmp(&ms->rx_abort, &ms->tx_ka) <= 0)
2549 		tvp = &ms->rx_abort;
2550 	      else
2551 		tvp = &ms->tx_ka;
2552 	      if(timeval_cmp(&now, tvp) <= 0)
2553 		timeval_diff_tv(&tv, &now, tvp);
2554 	      else
2555 		memset(&tv, 0, sizeof(tv));
2556 
2557 	    set_timeout:
2558 	      if(rc == 0)
2559 		{
2560 		  timeval_cpy(&to, &tv);
2561 		  rc++;
2562 		}
2563 	      else
2564 		{
2565 		  if(timeval_cmp(&tv, &to) < 0)
2566 		    timeval_cpy(&to, &tv);
2567 		}
2568 	    }
2569 	}
2570 
2571 #if defined(HAVE_EPOLL)
2572       if(rc != 0)
2573 	{
2574 	  timeout = (to.tv_sec * 1000) + (to.tv_usec / 1000);
2575 	  if(timeout == 0 && to.tv_usec != 0)
2576 	    timeout++;
2577 	}
2578       if((rc = epoll_wait(epfd, events, events_c, timeout)) == -1)
2579 	{
2580 	  if(errno == EINTR)
2581 	    continue;
2582 	  remote_debug(__func__, "epoll_wait failed: %s", strerror(errno));
2583 	  return -1;
2584 	}
2585 #else
2586       if(rc != 0)
2587 	{
2588 	  ts.tv_sec = to.tv_sec;
2589 	  ts.tv_nsec = to.tv_usec * 1000;
2590 	  timeout = &ts;
2591 	}
2592       if((rc = kevent(kqfd, NULL, 0, events, events_c, timeout)) == -1)
2593 	{
2594 	  if(errno == EINTR)
2595 	    continue;
2596 	  remote_debug(__func__, "kevent failed: %s", strerror(errno));
2597 	  return -1;
2598 	}
2599 #endif
2600 
2601       gettimeofday_wrap(&now);
2602 
2603       for(i=0; i<rc; i++)
2604 	{
2605 #if defined(HAVE_EPOLL)
2606 	  scfd = events[i].data.ptr;
2607 #else
2608 	  scfd = events[i].udata;
2609 #endif
2610 
2611 	  if((scu = scfd->unit) == NULL)
2612 	    {
2613 	      serversocket_accept(scfd->fd);
2614 	      continue;
2615 	    }
2616 
2617 #if defined(HAVE_EPOLL)
2618 	  if(events[i].events & EPOLLIN && scu->gc == 0)
2619 	    read_cb[scfd->type](scu->data);
2620 	  if(events[i].events & EPOLLOUT && scu->gc == 0)
2621 	    write_cb[scfd->type](scu->data);
2622 #else
2623 	  if(scu->gc != 0)
2624 	    {
2625 	      assert(scu->list == gclist);
2626 	      continue;
2627 	    }
2628 	  if(events[i].filter == EVFILT_READ)
2629 	    read_cb[scfd->type](scu->data);
2630 	  else if(events[i].filter == EVFILT_WRITE)
2631 	    write_cb[scfd->type](scu->data);
2632 #endif
2633 	}
2634 
2635       while((scu = dlist_head_pop(gclist)) != NULL)
2636 	unit_gc[scu->type](scu->data);
2637     }
2638 
2639   return 0;
2640 }
2641 #endif
2642 
select_loop(void)2643 static int select_loop(void)
2644 {
2645   struct timeval tv, to, *timeout, *tvp;
2646   fd_set rfds;
2647   fd_set wfds, *wfdsp;
2648   int i, count, nfds;
2649   dlist_node_t *dn, *dn2;
2650   sc_master_t *ms;
2651   sc_channel_t *cn;
2652   sc_unit_t *scu;
2653 
2654   while(stop == 0)
2655     {
2656       if(reload != 0)
2657 	{
2658 	  reload = 0;
2659 #ifdef HAVE_OPENSSL
2660 	  if(remoted_tlsctx_reload() != 0)
2661 	    return -1;
2662 #endif
2663 	}
2664 
2665       FD_ZERO(&rfds); FD_ZERO(&wfds);
2666       wfdsp = NULL; nfds = -1; timeout = NULL;
2667 
2668       for(i=0; i<2; i++)
2669 	{
2670 	  if(serversockets[i] == -1)
2671 	    continue;
2672 	  FD_SET(serversockets[i], &rfds);
2673 	  if(serversockets[i] > nfds)
2674 	    nfds = serversockets[i];
2675 	}
2676 
2677       if((dn = dlist_head_node(mslist)) != NULL)
2678 	{
2679 	  gettimeofday_wrap(&now);
2680 
2681 	  /* to start with, handle keepalives */
2682 	  while(dn != NULL)
2683 	    {
2684 	      ms = dlist_node_item(dn);
2685 	      dn = dlist_node_next(dn);
2686 
2687 	      /* check if it is time to take care of a zombie */
2688 	      if(ms->inet_fd.fd == -1)
2689 		{
2690 		  if(timeval_cmp(&now, &ms->zombie) < 0)
2691 		    {
2692 		      timeval_diff_tv(&tv, &now, &ms->zombie);
2693 		      goto set_timeout;
2694 		    }
2695 		  remote_debug(__func__, "removing %s zombie", ms->name);
2696 		  sc_master_free(ms);
2697 		  continue;
2698 		}
2699 
2700 	      /* if the connection has gone silent, abort */
2701 	      if(timeval_cmp(&now, &ms->rx_abort) >= 0)
2702 		{
2703 		  if(zombie > 0 && ms->name != NULL)
2704 		    {
2705 		      sc_master_zombie(ms);
2706 		      timeval_diff_tv(&tv, &now, &ms->zombie);
2707 		      goto set_timeout;
2708 		    }
2709 		  sc_master_free(ms);
2710 		  continue;
2711 		}
2712 
2713 	      /*
2714 	       * ensure we send something every 30 seconds
2715 	       * unix_fd being not null signifies the remote controller
2716 	       * has received an opening "master" frame.
2717 	       */
2718 	      if(ms->unix_fd != NULL && timeval_cmp(&now, &ms->tx_ka) >= 0)
2719 		{
2720 		  timeval_add_s(&ms->tx_ka, &now, 30);
2721 		  if(sc_master_tx_keepalive(ms) != 0)
2722 		    {
2723 		      sc_master_free(ms);
2724 		      continue;
2725 		    }
2726 		}
2727 
2728 	      /* now figure out timeout to set */
2729 	      if(timeval_cmp(&ms->rx_abort, &ms->tx_ka) <= 0)
2730 		tvp = &ms->rx_abort;
2731 	      else
2732 		tvp = &ms->tx_ka;
2733 	      if(timeval_cmp(&now, tvp) <= 0)
2734 		timeval_diff_tv(&tv, &now, tvp);
2735 	      else
2736 		memset(&tv, 0, sizeof(tv));
2737 
2738 	      if(ms->inet_fd.fd != -1)
2739 		{
2740 		  /* put the master inet socket into the select set */
2741 		  FD_SET(ms->inet_fd.fd, &rfds);
2742 		  if(ms->inet_fd.fd > nfds)
2743 		    nfds = ms->inet_fd.fd;
2744 		  if(scamper_writebuf_len(ms->inet_wb) > 0)
2745 		    {
2746 		      FD_SET(ms->inet_fd.fd, &wfds);
2747 		      wfdsp = &wfds;
2748 		    }
2749 		}
2750 
2751 	      /* listen on the master unix domain socket for new connections */
2752 	      if(ms->unix_fd != NULL)
2753 		{
2754 		  FD_SET(ms->unix_fd->fd, &rfds);
2755 		  if(ms->unix_fd->fd > nfds) nfds = ms->unix_fd->fd;
2756 		}
2757 
2758 	      /* set the unix domain sockets for connected systems */
2759 	      dn2 = dlist_head_node(ms->channels);
2760 	      while(dn2 != NULL)
2761 		{
2762 		  cn = dlist_node_item(dn2);
2763 		  dn2 = dlist_node_next(dn2);
2764 		  if(cn->unix_fd == NULL)
2765 		    continue;
2766 		  if((cn->unix_fd->flags & (FD_FLAG_READ|FD_FLAG_WRITE)) == 0)
2767 		    continue;
2768 		  if(cn->unix_fd->fd > nfds)
2769 		    nfds = cn->unix_fd->fd;
2770 		  if(cn->unix_fd->flags & FD_FLAG_READ)
2771 		    FD_SET(cn->unix_fd->fd, &rfds);
2772 		  if(cn->unix_fd->flags & FD_FLAG_WRITE)
2773 		    {
2774 		      FD_SET(cn->unix_fd->fd, &wfds);
2775 		      wfdsp = &wfds;
2776 		    }
2777 		}
2778 
2779 	    set_timeout:
2780 	      if(timeout == NULL)
2781 		{
2782 		  timeval_cpy(&to, &tv);
2783 		  timeout = &to;
2784 		}
2785 	      else
2786 		{
2787 		  if(timeval_cmp(&tv, &to) < 0)
2788 		    timeval_cpy(&to, &tv);
2789 		}
2790 	    }
2791 	}
2792 
2793       if((count = select(nfds+1, &rfds, wfdsp, NULL, timeout)) < 0)
2794 	{
2795 	  if(errno == EINTR)
2796 	    continue;
2797 	  remote_debug(__func__, "select failed: %s", strerror(errno));
2798 	  return -1;
2799 	}
2800 
2801       gettimeofday_wrap(&now);
2802 
2803       if(count > 0)
2804 	{
2805 	  for(i=0; i<2; i++)
2806 	    {
2807 	      if(serversockets[i] != -1 &&
2808 		 FD_ISSET(serversockets[i], &rfds) &&
2809 		 serversocket_accept(serversockets[i]) != 0)
2810 		return -1;
2811 	    }
2812 
2813 	  for(dn=dlist_head_node(mslist); dn != NULL; dn=dlist_node_next(dn))
2814 	    {
2815 	      ms = dlist_node_item(dn);
2816 	      if(ms->inet_fd.fd != -1 && FD_ISSET(ms->inet_fd.fd, &rfds))
2817 		sc_master_inet_read_do(ms);
2818 	      if(ms->unit->gc == 0 && ms->unix_fd != NULL &&
2819 		 FD_ISSET(ms->unix_fd->fd, &rfds))
2820 		sc_master_unix_accept_do(ms);
2821 	      if(ms->unit->gc == 0 && wfdsp != NULL &&
2822 		 ms->inet_fd.fd != -1 && FD_ISSET(ms->inet_fd.fd, wfdsp))
2823 		sc_master_inet_write_do(ms);
2824 
2825 	      for(dn2 = dlist_head_node(ms->channels);
2826 		  dn2 != NULL && ms->unit->gc == 0;
2827 		  dn2 = dlist_node_next(dn2))
2828 		{
2829 		  cn = dlist_node_item(dn2);
2830 		  if(cn->unix_fd != NULL && FD_ISSET(cn->unix_fd->fd, &rfds))
2831 		    sc_channel_unix_read_do(cn);
2832 		  if(wfdsp != NULL && cn->unix_fd != NULL &&
2833 		     FD_ISSET(cn->unix_fd->fd, wfdsp))
2834 		    sc_channel_unix_write_do(cn);
2835 		}
2836 	    }
2837 	}
2838 
2839       while((scu = dlist_head_pop(gclist)) != NULL)
2840 	unit_gc[scu->type](scu->data);
2841     }
2842 
2843   return 0;
2844 }
2845 
main(int argc,char * argv[])2846 int main(int argc, char *argv[])
2847 {
2848   int i;
2849 
2850 #ifndef _WIN32
2851   struct sigaction si_sa;
2852 #endif
2853 
2854 #ifdef DMALLOC
2855   free(malloc(1));
2856 #endif
2857 
2858   gettimeofday_wrap(&now);
2859 
2860   for(i=0; i<2; i++)
2861     serversockets[i] = -1;
2862 
2863   atexit(cleanup);
2864 
2865   if(check_options(argc, argv) != 0)
2866     return -1;
2867 
2868   if(pidfile != NULL && remoted_pidfile() != 0)
2869     return -1;
2870 
2871 #ifdef HAVE_OPENSSL
2872   if(tls_certfile != NULL)
2873     {
2874       SSL_library_init();
2875       SSL_load_error_strings();
2876       if(remoted_tlsctx() != 0)
2877 	return -1;
2878     }
2879 #endif
2880 
2881 #ifdef HAVE_DAEMON
2882   if((options & OPT_DAEMON) != 0 && daemon(1, 0) != 0)
2883     return -1;
2884 #endif
2885 
2886 #ifndef _WIN32
2887   if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
2888     {
2889       remote_debug(__func__, "could not ignore SIGPIPE");
2890       return -1;
2891     }
2892 
2893   sigemptyset(&si_sa.sa_mask);
2894   si_sa.sa_flags   = 0;
2895   si_sa.sa_handler = remoted_sighup;
2896   if(sigaction(SIGHUP, &si_sa, 0) == -1)
2897     {
2898       remote_debug(__func__, "could not set sigaction for SIGHUP");
2899       return -1;
2900     }
2901 
2902   sigemptyset(&si_sa.sa_mask);
2903   si_sa.sa_flags   = 0;
2904   si_sa.sa_handler = remoted_sigint;
2905   if(sigaction(SIGINT, &si_sa, 0) == -1)
2906     {
2907       remote_debug(__func__, "could not set sigaction for SIGINT");
2908       return -1;
2909     }
2910 #endif
2911 
2912   if(unixdomain_direxists() != 0 || serversocket_init() != 0)
2913     return -1;
2914 
2915   if((mslist = dlist_alloc()) == NULL ||
2916      (mstree = splaytree_alloc((splaytree_cmp_t)sc_master_cmp)) == NULL ||
2917      (gclist = dlist_alloc()) == NULL)
2918     return -1;
2919   dlist_onremove(mslist, (dlist_onremove_t)sc_master_onremove);
2920   dlist_onremove(gclist, (dlist_onremove_t)sc_unit_onremove);
2921 
2922 #if defined(HAVE_EPOLL)
2923   if((flags & FLAG_SELECT) == 0)
2924     return epoll_loop();
2925 #elif defined(HAVE_KQUEUE)
2926   if((flags & FLAG_SELECT) == 0)
2927     return kqueue_loop();
2928 #endif
2929 
2930   return select_loop();
2931 }
2932