forked from longshine/ser2nets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataxfer.c
3571 lines (3143 loc) · 88.5 KB
/
dataxfer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2001 Corey Minyard <minyard@acm.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This code handles the actual transfer of data between the serial
ports and the TCP ports. */
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include "dataxfer.h"
#include "selector.h"
#include "devcfg.h"
#include "utils.h"
#include "telnet.h"
#include "http.h"
extern selector_t *ser2net_sel;
#define SERIAL "term"
#define NET "tcp "
/** BASED ON sshd.c FROM openssh.com */
#ifdef HAVE_TCPD_H
#include <tcpd.h>
static char *progname = "ser2net";
#endif /* HAVE_TCPD_H */
#ifdef USE_UUCP_LOCKING
static char *uucp_lck_dir = "/var/lock";
#endif /* USE_UUCP_LOCKING */
/* States for the tcp_to_dev_state and dev_to_tcp_state. */
#define PORT_UNCONNECTED 0 /* The TCP port is not connected
to anything right now. */
#define PORT_WAITING_INPUT 1 /* Waiting for input from the
input side. */
#define PORT_WAITING_OUTPUT_CLEAR 2 /* Waiting for output to clear
so I can send data. */
#define PORT_CLOSING 3 /* Waiting for output close
string to be sent. */
char *state_str[] = { "unconnected", "waiting input", "waiting output",
"closing" };
#define PORT_DISABLED 0 /* The port is not open. */
#define PORT_RAW 1 /* Port will not do telnet negotiation. */
#define PORT_RAWLP 2 /* Port will not do telnet negotiation and
termios setting, open for output only. */
#define PORT_TELNET 3 /* Port will do telnet negotiation. */
#define PORT_REMOTE_RAW 4 /* Remote port in RAW. */
#define PORT_HTTP 5 /* Port will process HTTP request. */
char *enabled_str[] = { "off", "raw", "rawlp", "telnet", "rraw", "http" };
#define PORT_BUFSIZE 64
#define PORT_IS_FREE(port) (NULL == port->tcp_list)
#define DEBUG 1
#if DEBUG
#define D(...) printf(__VA_ARGS__);
#define tcp_print_list(...) tcp_print_list_impl(__VA_ARGS__);
#else
#define D(...)
#define tcp_print_list
#endif
struct port_info;
typedef struct tcp_info_s
{
struct port_info *port;
int timeout; /* The number of seconds to
wait without any I/O before
we shut the port down. */
int timeout_left; /* The amount of time left (in
seconds) before the timeout
goes off. */
sel_timer_t *timer; /* Used to timeout when the no
I/O has been seen for a
certain period of time. */
int tcpfd; /* When connected, the file
descriptor for the TCP
port used for I/O. */
struct sockaddr_storage remote; /* The socket address of who
is connected to this port. */
unsigned int tcp_bytes_received; /* Number of bytes read from the
TCP port. */
unsigned int tcp_bytes_sent; /* Number of bytes written to the
TCP port. */
struct sbuf *banner; /* Outgoing banner */
/* Information use when transferring information from the terminal
device to the TCP port. */
int dev_to_tcp_state; /* State of transferring
data from the device to
the TCP port. */
struct sbuf dev_to_tcp; /* Buffer struct for
device to TCP
transfers. */
/* Data for the telnet processing */
telnet_data_t tn_data;
/* Data for the http processing */
http_data_t ht_data;
struct tcp_info_s *prev;
struct tcp_info_s *next;
} tcp_info_t;
typedef struct trace_s
{
int hexdump; /* output each block as a hexdump */
int timestamp; /* preceed each line with a timestamp */
int file; /* open file. -1 if not used */
} trace_t;
typedef struct port_info
{
int enabled; /* If PORT_DISABLED, the port
is disabled and the TCP
accept port is not
operational. If PORT_RAW,
the port is enabled and
will not do any telnet
negotiations. If
PORT_RAWLP, the port is enabled
only for output without any
termios setting - it allows
to redirect /dev/lpX devices If
PORT_TELNET, the port is
enabled and it will do
telnet negotiations. */
int timeout; /* The number of seconds to
wait without any I/O before
we shut the port down. */
/* Information about the TCP port. */
char *portname; /* The name given for the port. */
int is_stdio; /* Do stdio on the port? */
int is_client; /* Is client socket? */
struct addrinfo *ai; /* The address list for the portname. */
int *acceptfds; /* The file descriptor used to
accept connections on the
TCP port. */
unsigned int nr_acceptfds;
struct sockaddr_storage remote; /* The socket address of who
is connected to this port. */
unsigned int tcp_bytes_received; /* Number of bytes read from the
TCP port. */
unsigned int tcp_bytes_sent; /* Number of bytes written to the
TCP port. */
/* Information about the terminal device. */
char *devname; /* The full path to the device */
int devfd; /* The file descriptor for the
device, only valid if the
TCP port is open. */
unsigned int dev_bytes_received; /* Number of bytes read from the
device. */
unsigned int dev_bytes_sent; /* Number of bytes written to the
device. */
/* Information use when transferring information from the TCP port
to the terminal device. */
int tcp_to_dev_state; /* State of transferring
data from the TCP port
to the device. */
struct sbuf tcp_to_dev; /* Buffer struct for
TCP to device
transfers. */
unsigned char tcp_to_devbuf[PORT_BUFSIZE]; /* Buffer used for
TCP to device
transfers. */
struct controller_info *tcp_monitor; /* If non-null, send any input
received from the TCP port
to this controller port. */
struct sbuf *devstr; /* Outgoing string */
char *closestr; /* Since the close string is
processed at shutdown, the
conf may have been redone and
the data freed. So keep a
copy here. */
/* Information use when transferring information from the terminal
device to the TCP port. */
int dev_to_tcp_state; /* State of transferring
data from the device to
the TCP port. */
struct sbuf dev_to_tcp; /* Buffer struct for
device to TCP
transfers. */
unsigned char dev_to_tcpbuf[PORT_BUFSIZE]; /* Buffer used for
device to TCP
transfers. */
struct controller_info *dev_monitor; /* If non-null, send any input
received from the device
to this controller port. */
struct port_info *next; /* Used to keep a linked list
of these. */
int config_num; /* Keep track of what configuration this was last
updated under. Setting to -1 means to delete
the port when the current session is done. */
struct port_info *new_config; /* If the port is reconfigged while
open, this will hold the new
configuration that should be
loaded when the current session
is done. */
/* Is RFC 2217 mode enabled? */
int is_2217;
/* Holds whether break is on or not. */
int break_set;
/* Masks for RFC 2217 */
unsigned char linestate_mask;
unsigned char modemstate_mask;
unsigned char last_modemstate;
/* Read and write trace information */
trace_t wt;
trace_t rt;
trace_t bt;
dev_info_t dinfo; /* device configuration information */
tcp_info_t *tcp_list;
/* Timer for restarting port when startup_port failed */
int restart_timeout;
int restart_timeout_left;
sel_timer_t *restart_timer;
} port_info_t;
port_info_t *ports = NULL; /* Linked list of ports. */
static void shutdown_port(port_info_t *port, tcp_info_t *tcp, char *reason);
static void finish_shutdown_port(port_info_t *port);
static char* startup_port(port_info_t *port);
/* The init sequence we use. */
static unsigned char telnet_init_seq[] = {
TN_IAC, TN_WILL, TN_OPT_SUPPRESS_GO_AHEAD,
TN_IAC, TN_WILL, TN_OPT_ECHO,
TN_IAC, TN_DONT, TN_OPT_ECHO,
TN_IAC, TN_DO, TN_OPT_BINARY_TRANSMISSION,
};
/* Our telnet command table. */
static void com_port_handler(void *cb_data, unsigned char *option, int len);
static int com_port_will(void *cb_data);
static struct telnet_cmd telnet_cmds[] =
{
/* I will, I do, sent will, sent do */
{ TN_OPT_SUPPRESS_GO_AHEAD, 0, 1, 1, 0, },
{ TN_OPT_ECHO, 0, 1, 1, 1, },
{ TN_OPT_BINARY_TRANSMISSION, 1, 1, 0, 1, },
{ TN_OPT_COM_PORT, 1, 0, 0, 0, 0, 0,
com_port_handler, com_port_will },
{ 255 }
};
#ifdef USE_UUCP_LOCKING
static int
uucp_fname_lock_size(char *devname)
{
char *ptr;
(ptr = strrchr(devname, '/'));
if (ptr == NULL) {
ptr = devname;
} else {
ptr = ptr + 1;
}
return 7 + strlen(uucp_lck_dir) + strlen(ptr);
}
static void
uucp_fname_lock(char *buf, char *devname)
{
char *ptr;
(ptr = strrchr(devname, '/'));
if (ptr == NULL) {
ptr = devname;
} else {
ptr = ptr + 1;
}
sprintf(buf, "%s/LCK..%s", uucp_lck_dir, ptr);
}
static void
uucp_rm_lock(char *devname)
{
char *lck_file;
if (!uucp_locking_enabled) return;
lck_file = malloc(uucp_fname_lock_size(devname));
if (lck_file == NULL) {
return;
}
uucp_fname_lock(lck_file, devname);
unlink(lck_file);
free(lck_file);
}
/* return 0=OK, -1=error, 1=locked by other proces */
static int
uucp_mk_lock(char *devname)
{
struct stat stt;
int pid = -1;
if (!uucp_locking_enabled)
return 0;
if (stat(uucp_lck_dir, &stt) == 0) { /* is lock file directory present? */
char *lck_file;
union {
uint32_t ival;
char str[64];
} buf;
int fd;
lck_file = malloc(uucp_fname_lock_size(devname));
if (lck_file == NULL)
return -1;
uucp_fname_lock(lck_file, devname);
pid = 0;
if ((fd = open(lck_file, O_RDONLY)) >= 0) {
int n;
n = read(fd, &buf, sizeof(buf));
close(fd);
if( n == 4 ) /* Kermit-style lockfile. */
pid = buf.ival;
else if (n > 0) { /* Ascii lockfile. */
buf.str[n] = 0;
sscanf(buf.str, "%d", &pid);
}
if (pid > 0 && kill((pid_t)pid, 0) < 0 && errno == ESRCH) {
/* death lockfile - remove it */
unlink(lck_file);
sleep(1);
pid = 0;
} else
pid = 1;
}
if (pid == 0) {
int mask;
size_t rv;
mask = umask(022);
fd = open(lck_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
umask(mask);
if (fd >= 0) {
snprintf(buf.str, sizeof(buf), "%10ld\n",
(long)getpid());
rv = write_full(fd, buf.str, strlen(buf.str));
close(fd);
if (rv < 0) {
pid = -1;
unlink(lck_file);
}
} else {
pid = -1;
}
}
free(lck_file);
}
return pid;
}
#endif /* USE_UUCP_LOCKING */
static void
init_port_data(port_info_t *port)
{
port->enabled = PORT_DISABLED;
port->portname = NULL;
port->acceptfds = NULL;
port->timeout = 0;
port->next = NULL;
port->new_config = NULL;
port->tcp_monitor = NULL;
port->is_stdio = 0;
port->is_client = 0;
port->ai = NULL;
port->devname = NULL;
port->devfd = -1;
memset(&(port->remote), 0, sizeof(port->remote));
memset(&(port->dinfo.termctl), 0, sizeof(port->dinfo.termctl));
port->tcp_to_dev_state = PORT_UNCONNECTED;
buffer_init(&port->tcp_to_dev, port->tcp_to_devbuf,
sizeof(port->tcp_to_devbuf));
port->tcp_bytes_received = 0;
port->tcp_bytes_sent = 0;
port->dev_to_tcp_state = PORT_UNCONNECTED;
buffer_init(&port->dev_to_tcp, port->dev_to_tcpbuf,
sizeof(port->dev_to_tcpbuf));
port->dev_bytes_received = 0;
port->dev_bytes_sent = 0;
port->devstr = NULL;
port->closestr = NULL;
port->is_2217 = 0;
port->dev_monitor = NULL;
port->break_set = 0;
port->dinfo.disablebreak = 0;
port->wt.file = -1;
port->wt.timestamp = 0;
port->wt.hexdump = 0;
port->rt.file = -1;
port->rt.timestamp = 0;
port->rt.hexdump = 0;
port->bt.file = -1;
port->bt.timestamp = 0;
port->bt.hexdump = 0;
port->restart_timeout = 0;
}
static void
reset_timer(port_info_t *port)
{
tcp_info_t *tcp = port->tcp_list;
while (tcp) {
tcp->timeout = port->timeout;
tcp->timeout_left = tcp->timeout;
tcp = tcp->next;
}
}
static void
reset_restart_timer(port_info_t *port)
{
port->restart_timeout_left = port->restart_timeout;
}
static void
tick_timer(sel_timer_t *timer)
{
struct timeval then;
gettimeofday(&then, NULL);
then.tv_sec += 1;
sel_start_timer(timer, &then);
}
static int
timestamp(trace_t *tr, char *buf, int size)
{
time_t result;
if(!tr->timestamp)
return 0;
result = time(NULL);
return strftime(buf, size, "%Y/%m/%d %H:%M:%S ", localtime(&result));
}
static int
trace_write_end(char *out, int size, unsigned char *start, int col)
{
int pos=0, w;
strncat(out, " |", size-pos);
pos += 2;
for(w = 0; w < col; w++) {
pos += snprintf(out + pos, size - pos, "%c",
isprint(start[w]) ? start[w] : '.');
}
strncat(out+pos, "|\n", size-pos);
pos += 2;
return pos;
}
int
trace_write(port_info_t *port, trace_t *tr, unsigned char *buf,
unsigned int buf_len, char *prefix)
{
int rv=0, w, col=0, pos, file = tr->file;
unsigned int q;
static char out[1024];
unsigned char *start;
if( buf_len == 0 )
return 0;
if(!tr->hexdump)
return write(file, buf, buf_len);
pos = timestamp(tr, out, sizeof(out));
pos += snprintf(out + pos, sizeof(out) - pos, "%s ", prefix);
start = buf;
for (q = 0; q < buf_len; q++) {
pos += snprintf(out + pos, sizeof(out) - pos, "%02x ", buf[q]);
col++;
if (col >= 8) {
pos += trace_write_end(out + pos, sizeof(out) - pos, start, col);
rv = write(file, out, strlen(out));
if (rv < 0)
return rv;
pos = timestamp(tr, out, sizeof(out));
pos += snprintf(out + pos, sizeof(out) - pos, "%s ", prefix);
col = 0;
start = buf + q + 1;
}
}
if ( col > 0 ) {
for (w = 8; w > col; w--) {
strncat(out + pos, " ", sizeof(out) - pos);
pos += 3;
}
pos += trace_write_end(out + pos, sizeof(out) - pos, start, col);
rv = write(file, out, strlen(out));
if(rv < 0)
return rv;
}
return buf_len;
}
static void
do_trace(port_info_t *port, trace_t *tr, unsigned char *buf,
unsigned int buf_len, char *prefix)
{
int rv;
while (buf_len > 0) {
retry_write:
rv = trace_write(port, tr, buf, buf_len, prefix);
if (rv == -1) {
char errbuf[128];
int err = errno;
if (err == EINTR)
goto retry_write;
/* Fatal error writing to the file, log it and close the file. */
if (strerror_r(err, errbuf, sizeof(errbuf)) == -1)
syslog(LOG_ERR, "Unable write to trace file on port %s: %d",
port->portname, err);
else
syslog(LOG_ERR, "Unable to write to trace file on port %s: %s",
port->portname, errbuf);
close(tr->file);
tr->file = -1;
return;
}
/* Handle a partial write */
buf_len -= rv;
buf += rv;
}
}
static void
header_trace(port_info_t *port)
{
static char buf[1024];
static trace_t tr = { 1, 1, -1 };
int len = 0, doneR=-1, doneW=-1;
char portstr[NI_MAXSERV];
len += timestamp(&tr, buf, sizeof(buf));
len += snprintf(buf + len, sizeof(buf) - len, "OPEN (");
getnameinfo((struct sockaddr *) &(port->remote), sizeof(port->remote),
buf + len, sizeof(buf) - len,
portstr, sizeof(portstr), NI_NUMERICHOST);
len += strlen(buf + len);
if ((sizeof(buf) - len) > 2) {
buf[len] = ':';
len++;
}
strncpy(buf + len, portstr, sizeof(buf) - len);
len += strlen(buf + len);
len += snprintf(buf + len, sizeof(buf) - len, ")\n");
if (port->rt.file != -1 && port->rt.timestamp) {
write_ignore_fail(port->rt.file, buf, len);
doneR = port->rt.file;
}
/* don't output to wt.file if it's the same as rt.file */
if (port->wt.file != doneR && port->wt.timestamp) {
write_ignore_fail(port->wt.file, buf, len);
doneW = port->wt.file;
}
/* don't output to bt.file if it's the same as rt.file or wt.file */
if (port->bt.file != doneR && port->bt.file != doneW
&& port->bt.timestamp)
write_ignore_fail(port->bt.file, buf, len);
}
static void
footer_trace(port_info_t *port, char *reason)
{
static char buf[1024];
static trace_t tr = { 1, 1, -1 };
int len=0, doneR=-1, doneW=-1;
len += timestamp(&tr, buf, sizeof(buf));
len += snprintf(buf + len, sizeof(buf), "CLOSE (%s)\n", reason);
if (port->rt.file != -1 && port->rt.timestamp) {
write_ignore_fail(port->rt.file, buf, len);
doneR = port->rt.file;
}
/* don't output to wt.file if it's the same as rt.file */
if (port->wt.file != doneR && port->wt.timestamp) {
write_ignore_fail(port->wt.file, buf, len);
doneW = port->wt.file;
}
/* don't output to bt.file if it's the same as rt.file or wt.file */
if (port->bt.file != doneR && port->bt.file != doneW
&& port->bt.timestamp)
write_ignore_fail(port->bt.file, buf, len);
}
static void
tcp_reset_timer(tcp_info_t *tcp)
{
tcp->timeout_left = tcp->timeout;
}
void got_timeout(selector_t *sel, sel_timer_t *timer, void *data);
static tcp_info_t *
tcp_new(int tcpfd)
{
tcp_info_t *tcp = (tcp_info_t *) malloc(sizeof(tcp_info_t));
memset(tcp, 0, sizeof(tcp_info_t));
tcp->tcpfd = tcpfd;
sel_alloc_timer(ser2net_sel, got_timeout, tcp, &tcp->timer);
return tcp;
}
#if DEBUG
static void
tcp_print_list_impl(tcp_info_t *tcp_list)
{
tcp_info_t *tcp = tcp_list;
D("[D] tcps:")
while (tcp) {
D(" %d (%d, %d)", tcp->tcpfd, tcp->prev ? tcp->prev->tcpfd : 0, tcp->next ? tcp->next->tcpfd : 0)
tcp = tcp->next;
}
D("\n")
}
#endif
static int
tcp_compare(void *t1, void *t2)
{
tcp_info_t *tcp1 = (tcp_info_t *) t1;
tcp_info_t *tcp2 = (tcp_info_t *) t2;
return (tcp1->tcpfd == tcp2->tcpfd) ? 0 : (tcp1->tcpfd < tcp2->tcpfd ? -1 : 1);
}
static int
tcp_insert(port_info_t *port, tcp_info_t *tcp)
{
if (NULL == port->tcp_list) {
port->tcp_list = tcp;
} else {
tcp_info_t *p;
p = port->tcp_list;
while (p->next) {
p = p->next;
}
tcp->next = p->next;
if (tcp->next)
tcp->next->prev = tcp;
p->next = tcp;
tcp->prev = p;
}
tcp->port = port;
tcp->timeout = port->timeout;
tcp_print_list(port->tcp_list)
return 0;
}
static void
tcp_free(tcp_info_t *tcp)
{
sel_free_timer(tcp->timer);
free(tcp);
}
static tcp_info_t *
tcp_find_in_port(port_info_t *port, int fd)
{
tcp_info_t *tcp = port->tcp_list;
while (tcp) {
if (tcp->tcpfd == fd)
return tcp;
tcp = tcp->next;
}
return NULL;
}
static void
tcp_close(tcp_info_t *tcp)
{
sel_stop_timer(tcp->timer);
if (tcp->tcpfd != -1) {
sel_clear_fd_handlers(ser2net_sel, tcp->tcpfd);
close(tcp->tcpfd);
}
if (tcp->banner) {
free(tcp->banner->buf);
free(tcp->banner);
tcp->banner = NULL;
}
tcp->tcp_bytes_received = 0;
tcp->tcp_bytes_sent = 0;
tcp->dev_to_tcp_state = PORT_UNCONNECTED;
buffer_reset(&tcp->dev_to_tcp);
}
static void
tcp_remove(port_info_t *port, tcp_info_t *tcp)
{
if (tcp->prev) {
tcp->prev->next = tcp->next;
} else {
port->tcp_list = tcp->next;
}
if (tcp->next) {
tcp->next->prev = tcp->prev;
}
tcp->prev = tcp->next = NULL;
tcp_free(tcp);
tcp_print_list(port->tcp_list)
}
static void
tcp_shutdown(tcp_info_t *tcp, char *reason)
{
D("tcp %d shutdown: %s\n", tcp->tcpfd, reason)
tcp_close(tcp);
tcp_remove(tcp->port, tcp);
}
static void
tcp_set_fd_write_handler(tcp_info_t *tcp, int state)
{
port_info_t *port = tcp->port;
sel_set_fd_write_handler(ser2net_sel, tcp->tcpfd, state);
if (SEL_FD_HANDLER_ENABLED == state) {
sel_set_fd_read_handler(ser2net_sel, port->devfd, SEL_FD_HANDLER_DISABLED);
port->dev_to_tcp_state = PORT_WAITING_OUTPUT_CLEAR;
} else if (SEL_FD_HANDLER_DISABLED == state) {
tcp_info_t *t = port->tcp_list;
while (t) {
if (t->dev_to_tcp.cursize != 0) {
D("tcp %d still has %d\n", t->tcpfd, t->dev_to_tcp.cursize)
return;
}
t = t->next;
}
sel_set_fd_read_handler(ser2net_sel, port->devfd, SEL_FD_HANDLER_ENABLED);
}
}
static void
tcp_set_fd_read_handler(port_info_t *port, int state)
{
tcp_info_t *tcp = port->tcp_list;
while (tcp) {
sel_set_fd_read_handler(ser2net_sel, tcp->tcpfd, state);
tcp_reset_timer(tcp);
tcp = tcp->next;
}
if (SEL_FD_HANDLER_ENABLED == state)
sel_set_fd_write_handler(ser2net_sel, port->devfd, SEL_FD_HANDLER_DISABLED);
else if (SEL_FD_HANDLER_DISABLED == state)
sel_set_fd_write_handler(ser2net_sel, port->devfd, SEL_FD_HANDLER_ENABLED);
}
static void
tcp_write_to(tcp_info_t *tcp)
{
port_info_t *port = tcp->port;
int count;
retry_write:
count = write(tcp->tcpfd, tcp->dev_to_tcp.buf, tcp->dev_to_tcp.cursize);
if (count == -1) {
if (errno == EINTR) {
/* EINTR means we were interrupted, just retry. */
goto retry_write;
}
if (errno == EAGAIN) {
/* This was due to O_NONBLOCK, we need to shut off the reader
and start the writer monitor. */
tcp_set_fd_write_handler(tcp, SEL_FD_HANDLER_ENABLED);
} else if (errno == EPIPE) {
shutdown_port(port, tcp, "EPIPE");
return;
} else {
/* Some other bad error. */
syslog(LOG_ERR, "The tcp write for port %s had error: %m",
port->portname);
shutdown_port(port, tcp, "tcp write error");
return;
}
} else {
tcp->tcp_bytes_sent += count;
port->tcp_bytes_sent += count;
tcp->dev_to_tcp.cursize -= count;
if (tcp->dev_to_tcp.cursize != 0) {
/* We didn't write all the data, shut off the reader and
start the write monitor. */
tcp->dev_to_tcp.pos = count;
tcp_set_fd_write_handler(tcp, SEL_FD_HANDLER_ENABLED);
port->dev_to_tcp_state = PORT_WAITING_OUTPUT_CLEAR;
}
}
tcp_reset_timer(tcp);
}
static void
tcp_write(port_info_t *port)
{
tcp_info_t *tcp = port->tcp_list;
while (tcp) {
tcp->dev_to_tcp.pos = 0;
if (PORT_HTTP == port->enabled) {
http_process_response(&tcp->ht_data, port->dev_to_tcp.buf, port->dev_to_tcp.cursize);
} else {
buffer_output(&tcp->dev_to_tcp, port->dev_to_tcp.buf, port->dev_to_tcp.cursize);
}
tcp_write_to(tcp);
tcp = tcp->next;
}
}
/* Data is ready to read on the serial port. */
static void
handle_dev_fd_read(int fd, void *data)
{
port_info_t *port = (port_info_t *) data;
int count;
port->dev_to_tcp.pos = 0;
if (port->enabled == PORT_TELNET) {
/* Leave room for IACs. */
count = read(fd, port->dev_to_tcp.buf, port->tcp_to_dev.maxsize/2);
} else if (port->enabled == PORT_HTTP) {
/* Leave room for websocket. */
count = read(fd, port->dev_to_tcp.buf, port->tcp_to_dev.maxsize/2);
} else {
count = read(fd, port->dev_to_tcp.buf, port->tcp_to_dev.maxsize);
}
if (count < 0) {
/* Got an error on the read, shut down the port. */
syslog(LOG_ERR, "dev read error for port %s: %m", port->portname);
shutdown_port(port, NULL, "dev read error");
return;
} else if (count == 0) {
/* The port got closed somehow, shut it down. */
shutdown_port(port, NULL, "closed port");
return;
}
if (port->dev_monitor != NULL) {
controller_write(port->dev_monitor,
(char *) port->dev_to_tcp.buf,
count);
}
if (port->rt.file != -1)
/* Do read tracing, ignore errors. */
do_trace(port, &port->rt, port->dev_to_tcp.buf, count, SERIAL);
if (port->bt.file != -1)
/* Do both tracing, ignore errors. */
do_trace(port, &port->bt, port->dev_to_tcp.buf, count, SERIAL);
port->dev_bytes_received += count;
if (port->enabled == PORT_TELNET) {
int i, j;
/* Double the IACs on a telnet stream. This will fit because
we only use half the buffer for telnet connections. */
for (i=0; i<count; i++) {
if (port->dev_to_tcp.buf[i] == 255) {
for (j=count; j>i; j--)
port->dev_to_tcp.buf[j+1] = port->dev_to_tcp.buf[j];
count++;
i++;
port->dev_to_tcp.buf[i] = 255;
}
}
}
port->dev_to_tcp.cursize = count;
tcp_write(port);
}
/* The serial port has room to write some data. This is only activated
if a write fails to complete, it is deactivated as soon as writing
is available again. */
static void
dev_fd_write(port_info_t *port, struct sbuf *buf)
{
int reterr, buferr;
reterr = buffer_write(port->devfd, buf, &buferr);
if (reterr == -1) {
syslog(LOG_ERR, "The dev write for port %s had error: %m",
port->portname);
shutdown_port(port, NULL, "dev write error");
return;
}
if (buffer_cursize(buf) == 0) {
/* We are done writing, turn the reader back on. */
tcp_set_fd_read_handler(port, SEL_FD_HANDLER_ENABLED);
port->tcp_to_dev_state = PORT_WAITING_INPUT;
}
reset_timer(port);
}
static void
handle_dev_fd_write(int fd, void *data)
{
port_info_t *port = (port_info_t *) data;
dev_fd_write(port, &port->tcp_to_dev);
}
/* Handle an exception from the serial port. */
static void
handle_dev_fd_except(int fd, void *data)