-
Notifications
You must be signed in to change notification settings - Fork 14
/
dataxfer.c
2656 lines (2321 loc) · 66.9 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 <[email protected]>
*
* 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 <sys/time.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include "ser2net.h"
#include "dataxfer.h"
#include "selector.h"
#include "utils.h"
#include "telnet.h"
#include "devio.h"
#include "buffer.h"
#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 */
/* 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. */
char *enabled_str[] = { "off", "raw", "rawlp", "telnet" };
#define PORT_BUFSIZE 64
typedef struct trace_info_s
{
int hexdump; /* output each block as a hexdump */
int timestamp; /* preceed each line with a timestamp */
char *filename; /* open file. NULL if not used */
int fd; /* open file. -1 if not used */
} trace_info_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. */
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. */
/* Information about the TCP port. */
char *portname; /* The name given for the port. */
int is_stdio; /* Do stdio on the port? */
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;
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 */
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 */
/* 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. */
/* Data for the telnet processing */
telnet_data_t tn_data;
/* Is RFC 2217 mode enabled? */
int is_2217;
/* Masks for RFC 2217 */
unsigned char linestate_mask;
unsigned char modemstate_mask;
unsigned char last_modemstate;
/* Allow RFC 2217 mode */
int allow_2217;
/* kickolduser mode */
int kickolduser_mode;
/* Banner to display at startup, or NULL if none. */
char *bannerstr;
/* RFC 2217 signature. */
char *signaturestr;
/* String to send to device at startup, or NULL if none. */
char *openstr;
/* String to send to device at close, or NULL if none. */
char *closestr;
/*
* File to read/write trace, NULL if none. If the same, then
* trace information is in the same file, only one open is done.
*/
trace_info_t trace_read;
trace_info_t trace_write;
trace_info_t trace_both;
/*
* Pointers to the above, that way if two are the same file we can just
* set up one and point both to it.
*/
trace_info_t *tr;
trace_info_t *tw;
trace_info_t *tb;
struct devio io; /* For handling I/O operation to the device */
#ifdef USE_RS485_FEATURE
struct serial_rs485 *rs485conf;
#endif
} port_info_t;
port_info_t *ports = NULL; /* Linked list of ports. */
static void shutdown_port(port_info_t *port, char *reason);
static void finish_shutdown_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 }
};
/*
* Generic output function for using a controller output for
* abstract I/O.
*/
static int
cntrl_absout(struct absout *o, const char *str, ...)
{
va_list ap;
int rv;
va_start(ap, str);
rv = controller_voutputf(o->data, str, ap);
va_end(ap);
return rv;
}
/*
* Like above but does a new line at the end of the output, generally
* for error output.
*/
static int
cntrl_abserrout(struct absout *o, const char *str, ...)
{
va_list ap;
int rv;
va_start(ap, str);
rv = controller_voutputf(o->data, str, ap);
va_end(ap);
rv += controller_outputf(o->data, "\r\n");
return rv;
}
static void
init_port_data(port_info_t *port)
{
port->enabled = PORT_DISABLED;
port->tcpfd = -1;
port->tcp_to_dev_state = PORT_UNCONNECTED;
buffer_init(&port->tcp_to_dev, port->tcp_to_devbuf,
sizeof(port->tcp_to_devbuf));
port->dev_to_tcp_state = PORT_UNCONNECTED;
buffer_init(&port->dev_to_tcp, port->dev_to_tcpbuf,
sizeof(port->dev_to_tcpbuf));
port->trace_read.fd = -1;
port->trace_write.fd = -1;
port->trace_both.fd = -1;
#ifdef USE_RS485_FEATURE
port->rs485conf = NULL;
#endif
}
static void
reset_timer(port_info_t *port)
{
port->timeout_left = port->timeout;
}
static int
timestamp(trace_info_t *t, char *buf, int size)
{
time_t result;
if (!t->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_info_t *t, unsigned char *buf,
unsigned int buf_len, char *prefix)
{
int rv = 0, w, col = 0, pos, file = t->fd;
unsigned int q;
static char out[1024];
unsigned char *start;
if (buf_len == 0)
return 0;
if (!t->hexdump)
return write(file, buf, buf_len);
pos = timestamp(t, 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(t, 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_info_t *t, unsigned char *buf,
unsigned int buf_len, char *prefix)
{
int rv;
while (buf_len > 0) {
retry_write:
rv = trace_write(port, t, 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(t->fd);
t->fd = -1;
return;
}
/* Handle a partial write */
buf_len -= rv;
buf += rv;
}
}
static void
hf_out(port_info_t *port, char *buf, int len)
{
if (port->tr && port->tr->timestamp)
write_ignore_fail(port->tr->fd, buf, len);
/* don't output to write file if it's the same as read file */
if (port->tw && port->tw != port->tr && port->tw->timestamp)
write_ignore_fail(port->tw->fd, buf, len);
/* don't output to both file if it's the same as read or write file */
if (port->tb && port->tb != port->tr && port->tb != port->tw
&& port->tb->timestamp)
write_ignore_fail(port->tb->fd, buf, len);
}
static void
header_trace(port_info_t *port)
{
static char buf[1024];
static trace_info_t tr = { 1, 1, NULL, -1 };
int len = 0;
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");
hf_out(port, buf, len);
}
static void
footer_trace(port_info_t *port, char *reason)
{
static char buf[1024];
static trace_info_t tr = { 1, 1, NULL, -1 };
int len = 0;
len += timestamp(&tr, buf, sizeof(buf));
len += snprintf(buf + len, sizeof(buf), "CLOSE (%s)\n", reason);
hf_out(port, buf, len);
}
/* Data is ready to read on the serial port. */
static void
handle_dev_fd_read(struct devio *io)
{
port_info_t *port = (port_info_t *) io->user_data;
int count;
port->dev_to_tcp.pos = 0;
if (port->enabled == PORT_TELNET) {
/* Leave room for IACs. */
count = port->io.f->read(&port->io, port->dev_to_tcp.buf,
port->tcp_to_dev.maxsize/2);
} else {
count = port->io.f->read(&port->io, port->dev_to_tcp.buf,
port->tcp_to_dev.maxsize);
}
if (port->dev_monitor != NULL) {
controller_write(port->dev_monitor,
(char *) port->dev_to_tcp.buf,
count);
}
if (count < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* Nothing to read, just return. */
return;
}
/* 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, "dev read error");
return;
} else if (count == 0) {
/* The port got closed somehow, shut it down. */
shutdown_port(port, "closed port");
return;
}
if (port->tr)
/* Do read tracing, ignore errors. */
do_trace(port, port->tr, port->dev_to_tcp.buf, count, SERIAL);
if (port->tb)
/* Do both tracing, ignore errors. */
do_trace(port, port->tb, 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;
retry_write:
count = write(port->tcpfd, port->dev_to_tcp.buf, port->dev_to_tcp.cursize);
if (count == -1) {
if (errno == EINTR) {
/* EINTR means we were interrupted, just retry. */
goto retry_write;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* This was due to O_NONBLOCK, we need to shut off the reader
and start the writer monitor. */
port->io.f->read_handler_enable(&port->io, 0);
sel_set_fd_write_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_ENABLED);
port->dev_to_tcp_state = PORT_WAITING_OUTPUT_CLEAR;
} else if (errno == EPIPE) {
shutdown_port(port, "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 write error");
return;
}
} else {
port->tcp_bytes_sent += count;
port->dev_to_tcp.cursize -= count;
if (port->dev_to_tcp.cursize != 0) {
/* We didn't write all the data, shut off the reader and
start the write monitor. */
port->dev_to_tcp.pos = count;
port->io.f->read_handler_enable(&port->io, 0);
sel_set_fd_write_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_ENABLED);
port->dev_to_tcp_state = PORT_WAITING_OUTPUT_CLEAR;
}
}
reset_timer(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_io_write(&port->io, buf, &buferr);
if (reterr == -1) {
syslog(LOG_ERR, "The dev write for port %s had error: %m",
port->portname);
shutdown_port(port, "dev write error");
return;
}
if (buffer_cursize(buf) == 0) {
/* We are done writing, turn the reader back on. */
sel_set_fd_read_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_ENABLED);
port->io.f->write_handler_enable(&port->io, 0);
port->tcp_to_dev_state = PORT_WAITING_INPUT;
}
reset_timer(port);
}
static void
handle_dev_fd_write(struct devio *io)
{
port_info_t *port = (port_info_t *) io->user_data;
dev_fd_write(port, &port->tcp_to_dev);
}
/* Handle an exception from the serial port. */
static void
handle_dev_fd_except(struct devio *io)
{
port_info_t *port = (port_info_t *) io->user_data;
syslog(LOG_ERR, "Select exception on device for port %s",
port->portname);
shutdown_port(port, "fd exception");
}
/* Output the devstr buffer */
static void
handle_dev_fd_devstr_write(struct devio *io)
{
port_info_t *port = (port_info_t *) io->user_data;
dev_fd_write(port, port->devstr);
if (buffer_cursize(port->devstr) == 0) {
port->io.read_handler = handle_dev_fd_read;
port->io.write_handler = handle_dev_fd_write;
port->io.except_handler = handle_dev_fd_except;
free(port->devstr->buf);
free(port->devstr);
port->devstr = NULL;
}
}
/* Output the devstr buffer */
static void
handle_dev_fd_close_write(struct devio *io)
{
port_info_t *port = (port_info_t *) io->user_data;
int reterr, buferr;
reterr = buffer_io_write(&port->io, port->devstr, &buferr);
if (reterr == -1) {
syslog(LOG_ERR, "The dev write for port %s had error: %m",
port->portname);
goto closeit;
}
if (buffer_cursize(port->devstr) != 0)
return;
closeit:
finish_shutdown_port(port);
}
/* Data is ready to read on the TCP port. */
static void
handle_tcp_fd_read(int fd, void *data)
{
port_info_t *port = (port_info_t *) data;
int count;
port->tcp_to_dev.pos = 0;
count = read(fd, port->tcp_to_dev.buf, port->tcp_to_dev.maxsize);
if (count < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* Nothing to read, just return. */
return;
}
/* Got an error on the read, shut down the port. */
syslog(LOG_ERR, "read error for port %s: %m", port->portname);
shutdown_port(port, "tcp read error");
return;
} else if (count == 0) {
/* The other end closed the port, shut it down. */
shutdown_port(port, "tcp read close");
return;
}
port->tcp_to_dev.cursize = count;
port->tcp_bytes_received += count;
if (port->enabled == PORT_TELNET) {
port->tcp_to_dev.cursize = process_telnet_data(port->tcp_to_dev.buf,
count,
&port->tn_data);
if (port->tn_data.error) {
shutdown_port(port, "telnet output error");
return;
}
if (port->tcp_to_dev.cursize == 0) {
/* We are out of characters; they were all processed. We
don't want to continue with 0, because that will mess
up the other processing and it's not necessary. */
return;
}
}
if (port->tcp_monitor != NULL) {
controller_write(port->tcp_monitor,
(char *) port->tcp_to_dev.buf,
port->tcp_to_dev.cursize);
}
if (port->tw)
/* Do write tracing, ignore errors. */
do_trace(port, port->tw,
port->tcp_to_dev.buf, port->tcp_to_dev.cursize, NET);
if (port->tb)
/* Do both tracing, ignore errors. */
do_trace(port, port->tb,
port->tcp_to_dev.buf, port->tcp_to_dev.cursize, NET);
retry_write:
count = port->io.f->write(&port->io, port->tcp_to_dev.buf,
port->tcp_to_dev.cursize);
if (count == -1) {
if (errno == EINTR) {
/* EINTR means we were interrupted, just retry. */
goto retry_write;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
/* This was due to O_NONBLOCK, we need to shut off the reader
and start the writer monitor. */
sel_set_fd_read_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_DISABLED);
port->io.f->write_handler_enable(&port->io, 1);
port->tcp_to_dev_state = PORT_WAITING_OUTPUT_CLEAR;
} else {
/* Some other bad error. */
syslog(LOG_ERR, "The dev write for port %s had error: %m",
port->portname);
shutdown_port(port, "dev write error");
return;
}
} else {
port->dev_bytes_sent += count;
port->tcp_to_dev.cursize -= count;
if (port->tcp_to_dev.cursize != 0) {
/* We didn't write all the data, shut off the reader and
start the write monitor. */
port->tcp_to_dev.pos = count;
sel_set_fd_read_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_DISABLED);
port->io.f->write_handler_enable(&port->io, 1);
port->tcp_to_dev_state = PORT_WAITING_OUTPUT_CLEAR;
}
}
reset_timer(port);
}
/* The TCP 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
tcp_fd_write(port_info_t *port, struct sbuf *buf)
{
telnet_data_t *td = &port->tn_data;
int buferr, reterr;
if (buffer_cursize(&td->out_telnet_cmd) > 0) {
reterr = buffer_write(port->tcpfd, &td->out_telnet_cmd, &buferr);
if (reterr == -1) {
if (buferr == EPIPE) {
shutdown_port(port, "EPIPE");
} else {
/* Some other bad error. */
syslog(LOG_ERR, "The tcp write for port %s had error: %m",
port->portname);
shutdown_port(port, "tcp write error");
}
}
if (buffer_cursize(&td->out_telnet_cmd) > 0) {
/* If we have more telnet command data to send, don't
send any real data. */
return;
}
}
reterr = buffer_write(port->tcpfd, buf, &buferr);
if (reterr == -1) {
if (buferr == EPIPE) {
shutdown_port(port, "EPIPE");
} else {
/* Some other bad error. */
syslog(LOG_ERR, "The tcp write for port %s had error: %m",
port->portname);
shutdown_port(port, "tcp write error");
}
return;
}
if (buffer_cursize(buf) == 0) {
/* We are done writing, turn the reader back on. */
port->io.f->read_handler_enable(&port->io, 1);
sel_set_fd_write_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_DISABLED);
port->dev_to_tcp_state = PORT_WAITING_INPUT;
}
reset_timer(port);
}
/* The TCP 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
handle_tcp_fd_write(int fd, void *data)
{
port_info_t *port = (port_info_t *) data;
tcp_fd_write(port, &port->dev_to_tcp);
}
/* Handle an exception from the TCP port. */
static void
handle_tcp_fd_except(int fd, void *data)
{
port_info_t *port = (port_info_t *) data;
syslog(LOG_ERR, "Select exception on port %s", port->portname);
shutdown_port(port, "tcp fd exception");
}
static void
handle_tcp_fd_banner_write(int fd, void *data)
{
port_info_t *port = (port_info_t *) data;
tcp_fd_write(port, port->banner);
if (buffer_cursize(port->banner) == 0) {
sel_set_fd_handlers(ser2net_sel,
port->tcpfd,
port,
handle_tcp_fd_read,
handle_tcp_fd_write,
handle_tcp_fd_except);
free(port->banner->buf);
free(port->banner);
port->banner = NULL;
}
}
static void
telnet_cmd_handler(void *cb_data, unsigned char cmd)
{
port_info_t *port = cb_data;
if (cmd == TN_BREAK)
port->io.f->send_break(&port->io);
}
/* Called when the telnet code has output ready. */
static void
telnet_output_ready(void *cb_data)
{
port_info_t *port = cb_data;
port->io.f->read_handler_enable(&port->io, 0);
sel_set_fd_write_handler(ser2net_sel, port->tcpfd,
SEL_FD_HANDLER_ENABLED);
}
/* Checks to see if some other port has the same device in use. */
static int
is_device_already_inuse(port_info_t *check_port)
{
port_info_t *port = ports;
while (port != NULL) {
if (port != check_port) {
if ((strcmp(port->io.devname, check_port->io.devname) == 0)
&& (port->tcp_to_dev_state != PORT_UNCONNECTED))
{
return 1;
}
}
port = port->next;
}
return 0;
}
static int
from_hex_digit(char c)
{
if ((c >= '0') && (c <= '9'))
return c - '0';
if ((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
if ((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
return 0;
}
static char *smonths[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
static char *sdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static void
process_str(port_info_t *port, struct tm *time, struct timeval *tv,
const char *s,
void (*op)(void *data, char val), void *data, int isfilename)
{
char val;
char *t;
while (*s) {
if (*s == '\\') {
s++;
if (!*s)
return;
switch (*s) {
/* Standard "C" characters. */
case 'a': op(data, 7); break;
case 'b': op(data, 8); break;
case 'f': op(data, 12); break;
case 'n': op(data, 10); break;
case 'r': op(data, 13); break;
case 't': op(data, 9); break;
case 'v': op(data, 11); break;
case '\\': op(data, '\\'); break;
case '?': op(data, '?'); break;
case '\'': op(data, '\''); break;
case '"': op(data, '"'); break;
case 'd':
/* ser2net device name. */
if (isfilename) {
/* Can't have '/' in a filename. */
t = strrchr(port->io.devname, '/');
if (t)
t++;
else
t = port->io.devname;
} else
t = port->io.devname;
for (; *t; t++)
op(data, *t);
break;
case 'p':
/* ser2net TCP port. */
for (t=port->portname; *t; t++)
op(data, *t);
break;
case 's':
if (isfilename)
goto seconds;
goto serparms;
case 'B':
serparms:
/* ser2net serial parms. */
{
char str[15];
port->io.f->serparm_to_str(&port->io, str, sizeof(str));
for (t=str; *t; t++)
op(data, *t);
}
break;
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7':
/* Octal digit */
val = (*s) - '0';
s++;
if (!*s) {
op(data, val);
return;
}
if (!isdigit(*s)) {
continue;
}
val = (val * 8) + (*s) - '0';