forked from cminyard/ser2net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataxfer.c
4758 lines (4101 loc) · 114 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 <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include <limits.h>
#include <sys/time.h>
#include <gensio/gensio.h>
#include <gensio/sergensio.h>
#include <gensio/argvutils.h>
#include "ser2net.h"
#include "dataxfer.h"
#include "readconfig.h"
#include "led.h"
#ifdef gensio_version_major
/* When the version info was added, the type was changed. */
typedef struct gensio_addr gaddrinfo;
#define gensio_free_addrinfo(o, a) gensio_addr_free(a)
#include <sys/socket.h>
#include <netdb.h>
#else
typedef struct addrinfo gaddrinfo;
#endif
#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 net_to_dev_state and dev_to_net_state. */
#define PORT_CLOSED 0 /* The accepter is disabled. */
#define PORT_UNCONNECTED 1 /* The TCP port is not connected
to anything right now. */
#define PORT_WAITING_INPUT 2 /* Waiting for input from the
input side. */
#define PORT_WAITING_OUTPUT_CLEAR 3 /* Waiting for output to clear
so I can send data. */
#define PORT_CLOSING 4 /* Waiting for output close
string to be sent. */
char *state_str[] = { "closed", "unconnected", "waiting input",
"waiting output", "closing" };
char *enabled_str[] = { "off", "on" };
typedef struct trace_info_s
{
bool hexdump; /* output each block as a hexdump */
bool 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 port_info_t;
typedef struct net_info net_info_t;
struct gbuf {
unsigned char *buf;
gensiods maxsize;
gensiods cursize;
gensiods pos;
};
static gensiods
gbuf_room_left(struct gbuf *buf) {
return buf->maxsize - buf->cursize;
}
static void
gbuf_append(struct gbuf *buf, unsigned char *data, gensiods len)
{
memcpy(buf->buf + buf->pos, data, len);
buf->cursize += len;
buf->pos += len;
}
static gensiods
gbuf_cursize(struct gbuf *buf)
{
return buf->cursize;
}
static void
gbuf_reset(struct gbuf *buf)
{
buf->cursize = 0;
buf->pos = 0;
}
static int
gbuf_init(struct gbuf *buf, gensiods size)
{
buf->buf = malloc(size);
if (!buf->buf)
return ENOMEM;
buf->maxsize = size;
buf->cursize = 0;
buf->pos = 0;
return 0;
}
struct net_info {
port_info_t *port; /* My port. */
bool closing; /* Is the connection in the process
of closing? */
struct gensio *net; /* When connected, the network
connection, NULL otherwise. */
bool remote_fixed; /* Tells if the remote address was
set in the configuration, and
cannot be changed. */
bool connect_back; /* True if we connect to the remote
address when data comes in. */
const char *remote_str;
gensiods bytes_received; /* Number of bytes read from the
network port. */
gensiods bytes_sent; /* Number of bytes written to the
network port. */
struct gbuf *banner; /* Outgoing banner */
gensiods write_pos; /* Our current position in the
output buffer where we need
to start writing next. */
int timeout_left; /* The amount of time left (in
seconds) before the timeout
goes off. */
/*
* Close the session when all the output has been written to the
* network port.
*/
bool close_on_output_done;
unsigned char linestate_mask;
unsigned char modemstate_mask;
bool modemstate_sent; /* Has a modemstate been sent? */
bool linestate_sent; /* Has a linestate been sent? */
/*
* If a user gets kicked, store the information for the new user
* here since we have already accepted the connection or received
* the packet, we have to store it someplace.
*/
struct gensio *new_net;
};
struct port_info
{
struct gensio_lock *lock;
/* If false, port is not accepting, if true it is. */
bool enabled;
const char *shutdown_reason;
/* The port has been deleted, but still has connections in use. */
bool deleted;
int timeout; /* The number of seconds to
wait without any I/O before
we shut the port down. */
struct gensio_timer *timer; /* Used to timeout when the no
I/O has been seen for a
certain period of time. */
struct gensio_timer *send_timer; /* Used to delay a bit when
waiting for characters to
batch up as many characters
as possible. */
bool send_timer_running;
unsigned int nocon_read_enable_time_left;
/* Used if a connect back is requested an no connections could
be made, to try again. */
/*
* Used to count timeouts during a shutdown, to make sure close
* happens in a reasonable amount of time. If this is zero, this
* means that shutdown_port_io() has already been called.
*/
unsigned int shutdown_timeout_count;
struct gensio_runner *runshutdown; /* Used to run things at the
base context. This way we
don't have to worry that we
are running inside a
handler context that needs
to be waited for exit. */
unsigned int chardelay; /* The amount of time to wait after
receiving a character before
sending it, unless we receive
another character. Based on
bit rate. */
unsigned int bps; /* Bits per second rate. */
unsigned int bpc; /* Bits per character. */
unsigned int stopbits;
unsigned int paritybits;
bool enable_chardelay;
unsigned int chardelay_scale; /* The number of character
periods to wait for the
next character, in tenths of
a character period. */
unsigned int chardelay_min; /* The minimum chardelay, in
microseconds. */
unsigned int chardelay_max; /* Maximum amount of time to
wait before sending the data. */
gensio_time send_time; /* When using chardelay, the
time when we will send the
data, no matter what, set
by chardelay_max. */
/* Information about the network port. */
char *name; /* The name given for the port. */
char *accstr; /* The accepter string. */
struct gensio_accepter *accepter; /* Used to receive new connections. */
bool accepter_stopped;
struct port_remaddr *remaddrs; /* Remote addresses allowed. */
struct port_remaddr *connbacks; /* Connect back addresses */
unsigned int num_waiting_connect_backs;
unsigned int max_connections; /* Maximum number of connections
we can accept at a time for this
port. */
net_info_t *netcons;
gensiods dev_bytes_received; /* Number of bytes read from the device. */
gensiods dev_bytes_sent; /* Number of bytes written to the device. */
/*
* Informationd use when transferring information from the network
* port to the terminal device.
*/
int net_to_dev_state; /* State of transferring
data from the network port
to the device. */
struct gbuf net_to_dev; /* Buffer for network
to dev transfers. */
struct controller_info *net_monitor; /* If non-null, send any input
received from the network port
to this controller port. */
struct gbuf *devstr; /* Outgoing string */
/*
* Information used when transferring information from the
* terminal device to the network port.
*/
int dev_to_net_state; /* State of transferring
data from the device to
the network port. */
struct gbuf dev_to_net;
/*
* We have called shutdown_port but the accepter has not yet been
* read disabled.
*/
bool shutdown_started;
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. */
/*
* The port was reconfigured but had pending users. This holds the
* new config until the pending users have finished.
*/
struct port_info *new_config;
char *rs485; /* If not NULL, rs485 was specified. */
/* For RFC 2217 */
unsigned char last_modemstate;
unsigned char last_linestate;
/* Allow RFC 2217 mode */
bool allow_2217;
/* Send a break if we get a sync command? */
bool telnet_brk_on_sync;
/* kickolduser mode */
bool 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;
/*
* Close on string to shutdown connection when received from
* serial side, or NULL if none.
*/
char *closeon;
gensiods closeon_pos;
gensiods closeon_len;
/*
* 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;
char *devname;
struct gensio *io; /* For handling I/O operation to the device */
bool io_open;
void (*dev_write_handler)(port_info_t *);
/*
* devname as specified on the line, not the substituted version. Only
* non-null if devname was substituted.
*/
char *orig_devname;
/*
* LED to flash for serial traffic
*/
struct led_s *led_tx;
struct led_s *led_rx;
/*
* Directory that has authentication info.
*/
char *authdir;
/*
* Delimiter for sending.
*/
char *sendon;
gensiods sendon_pos;
gensiods sendon_len;
};
static void setup_port(port_info_t *port, net_info_t *netcon);
static int handle_net_event(struct gensio *net, void *user_data,
int event, int err,
unsigned char *buf, gensiods *buflen,
const char *const *auxdata);
/*
* This infrastructure allows a list of addresses to be kept. This is
* for checking remote addresses
*/
struct port_remaddr
{
char *str;
gaddrinfo *ai;
bool is_port_set;
struct port_remaddr *next;
};
/* Add a remaddr to the given list, return 0 on success or errno on fail. */
static int
remaddr_append(struct port_remaddr **list, struct port_remaddr **cblist,
const char *str, bool is_connect_back)
{
struct port_remaddr *r = NULL, *r2, *rcb = NULL;
gaddrinfo *ai = NULL;
bool is_port_set = false;
int err = 0;
if (!is_connect_back) {
if (*str == '!') {
str++;
is_connect_back = true;
}
#ifdef gensio_version_major
err = gensio_scan_network_port(so, str, false, &ai, NULL,
&is_port_set, NULL, NULL);
#else
int socktype, protocol;
err = gensio_scan_network_port(so, str, false, &ai,
&socktype, &protocol,
&is_port_set, NULL, NULL);
#endif
if (err)
return err;
/* FIXME - We currently ignore the protocol. */
r = malloc(sizeof(*r));
if (!r) {
err = GE_NOMEM;
goto out;
}
memset(r, 0, sizeof(*r));
r->str = strdup(str);
if (!r->str) {
free(r);
err = GE_NOMEM;
goto out;
}
r->ai = ai;
ai = NULL;
r->is_port_set = is_port_set;
r->next = NULL;
r2 = *list;
if (!r2) {
*list = r;
} else {
while (r2->next)
r2 = r2->next;
r2->next = r;
}
}
if (is_connect_back) {
rcb = malloc(sizeof(*rcb));
if (!rcb) {
err = GE_NOMEM;
goto out;
}
memset(rcb, 0, sizeof(*rcb));
rcb->str = strdup(str);
if (!rcb->str) {
free(rcb);
err = GE_NOMEM;
goto out;
}
rcb->next = NULL;
r2 = *cblist;
if (!r2) {
*cblist = rcb;
} else {
while (r2->next)
r2 = r2->next;
r2->next = rcb;
}
}
out:
if (err) {
if (r) {
if (r->str)
free(r->str);
if (r->ai)
gensio_free_addrinfo(so, r->ai);
free(r);
}
if (rcb) {
if (rcb->str)
free(rcb->str);
free(rcb);
}
if (ai)
gensio_free_addrinfo(so, ai);
}
return err;
}
static bool
ai_check(gaddrinfo *ai, const struct sockaddr *addr, socklen_t len,
bool is_port_set)
{
#ifdef gensio_version_major
return gensio_addr_addr_present(ai, addr, len, is_port_set);
#else
while (ai) {
if (gensio_sockaddr_equal(addr, len, ai->ai_addr, ai->ai_addrlen,
is_port_set))
return true;
ai = ai->ai_next;
}
return false;
#endif
}
/* Check that the given address matches something in the list. */
static bool
remaddr_check(const struct port_remaddr *list,
const struct sockaddr *addr, socklen_t len)
{
const struct port_remaddr *r = list;
if (!r)
return true;
for (; r; r = r->next) {
if (ai_check(r->ai, addr, len, r->is_port_set))
return true;
}
return false;
}
#define for_each_connection(port, netcon) \
for (netcon = port->netcons; \
netcon < &(port->netcons[port->max_connections]); \
netcon++)
static struct gensio_lock *ports_lock;
static port_info_t *ports = NULL; /* Linked list of ports. */
static port_info_t *new_ports = NULL; /* New ports during config/reconfig. */
static port_info_t *new_ports_end = NULL;
static void shutdown_one_netcon(net_info_t *netcon, const char *reason);
static int shutdown_port(port_info_t *port, const char *errreason);
/*
* 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 int
num_connected_net(port_info_t *port)
{
net_info_t *netcon;
int count = 0;
for_each_connection(port, netcon) {
if (netcon->net)
count++;
}
return count;
}
static net_info_t *
first_live_net_con(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->net)
return netcon;
}
return NULL;
}
static int
init_port_data(port_info_t *port)
{
port->enabled = false;
port->net_to_dev_state = PORT_CLOSED;
port->dev_to_net_state = PORT_CLOSED;
port->trace_read.fd = -1;
port->trace_write.fd = -1;
port->trace_both.fd = -1;
port->telnet_brk_on_sync = find_default_bool("telnet-brk-on-sync");
port->kickolduser_mode = find_default_bool("kickolduser");
port->enable_chardelay = find_default_int("chardelay");
port->chardelay_scale = find_default_int("chardelay-scale");
port->chardelay_min = find_default_int("chardelay-min");
port->chardelay_max = find_default_int("chardelay-max");
port->dev_to_net.maxsize = find_default_int("dev-to-net-bufsize");
port->net_to_dev.maxsize = find_default_int("net-to-dev-bufsize");
port->max_connections = find_default_int("max-connections");
if (find_default_str("authdir", &port->authdir))
return ENOMEM;
if (find_default_str("signature", &port->signaturestr))
return ENOMEM;
if (find_default_str("banner", &port->bannerstr))
return ENOMEM;
if (find_default_str("openstr", &port->openstr))
return ENOMEM;
if (find_default_str("closestr", &port->closestr))
return ENOMEM;
if (find_default_str("closeon", &port->closeon))
return ENOMEM;
if (find_default_str("sendon", &port->sendon))
return ENOMEM;
port->led_tx = NULL;
port->led_rx = NULL;
return 0;
}
static void
reset_timer(net_info_t *netcon)
{
netcon->timeout_left = netcon->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, const 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, const unsigned char *buf,
gensiods buf_len, const char *prefix)
{
int rv = 0, w, col = 0, pos, file = t->fd;
gensiods q;
static char out[1024];
const 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) {
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;
}
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, const unsigned char *buf,
gensiods buf_len, const 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->name, err);
else
syslog(LOG_ERR, "Unable to write to trace file on port %s: %s",
port->name, 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, net_info_t *netcon)
{
char buf[1024];
trace_info_t tr = { 1, 1, NULL, -1 };
gensiods len = 0;
len += timestamp(&tr, buf, sizeof(buf));
if (sizeof(buf) > len)
len += snprintf(buf + len, sizeof(buf) - len, "OPEN (");
gensio_raddr_to_str(netcon->net, &len, buf, sizeof(buf));
if (sizeof(buf) > len)
len += snprintf(buf + len, sizeof(buf) - len, ")\n");
hf_out(port, buf, len);
}
static void
footer_trace(port_info_t *port, char *type, const char *reason)
{
char buf[1024];
trace_info_t tr = { 1, 1, NULL, -1 };
int len = 0;
len += timestamp(&tr, buf, sizeof(buf));
if (sizeof(buf) > len)
len += snprintf(buf + len, sizeof(buf) - len,
"CLOSE %s (%s)\n", type, reason);
hf_out(port, buf, len);
}
static bool
any_net_data_to_write(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (!netcon->net)
continue;
if (netcon->write_pos < port->dev_to_net.cursize)
return true;
}
return false;
}
static void
start_net_send(port_info_t *port)
{
net_info_t *netcon;
if (port->dev_to_net_state == PORT_WAITING_OUTPUT_CLEAR)
return;
gensio_set_read_callback_enable(port->io, false);
for_each_connection(port, netcon) {
if (!netcon->net)
continue;
netcon->write_pos = 0;
gensio_set_write_callback_enable(netcon->net, true);
}
port->dev_to_net_state = PORT_WAITING_OUTPUT_CLEAR;
}
void
send_timeout(struct gensio_timer *timer, void *data)
{
port_info_t *port = (port_info_t *) data;
so->lock(port->lock);
if (port->dev_to_net_state == PORT_CLOSING) {
so->unlock(port->lock);
return;
}
port->send_timer_running = false;
if (port->dev_to_net.cursize)
start_net_send(port);
so->unlock(port->lock);
}
static void
disable_all_net_read(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->net)
gensio_set_read_callback_enable(netcon->net, false);
}
}
static void
enable_all_net_read(port_info_t *port)
{
net_info_t *netcon;
for_each_connection(port, netcon) {
if (netcon->net)
gensio_set_read_callback_enable(netcon->net, true);
}
}
static void
connect_back_done(struct gensio *net, int err, void *cb_data)
{
net_info_t *netcon = cb_data;
port_info_t *port = netcon->port;
so->lock(port->lock);
if (err) {
netcon->net = NULL;
gensio_free(net);
} else {
setup_port(port, netcon);
}
assert(port->num_waiting_connect_backs > 0);
port->num_waiting_connect_backs--;
if (port->num_waiting_connect_backs == 0) {
if (num_connected_net(port) == 0)
/* No connections could be made. */
port->nocon_read_enable_time_left = 10;
else
gensio_set_read_callback_enable(port->io, true);
}
so->unlock(port->lock);
}
static int
port_check_connect_backs(port_info_t *port)
{
net_info_t *netcon;
bool tried = false;
if (!port->connbacks)
return 0;
for_each_connection(port, netcon) {
if (netcon->connect_back && !netcon->net) {
int err;
tried = true;
err = gensio_acc_str_to_gensio(port->accepter, netcon->remote_str,
handle_net_event, netcon,
&netcon->net);
if (err) {
syslog(LOG_ERR, "Unable to allocate connect back port %s,"
" addr %s: %s\n", port->name, netcon->remote_str,
gensio_err_to_str(err));
continue;
}
err = gensio_open(netcon->net, connect_back_done, netcon);
if (err) {
gensio_free(netcon->net);
netcon->net = NULL;
syslog(LOG_ERR, "Unable to open connect back port %s,"
" addr %s: %s\n", port->name, netcon->remote_str,
gensio_err_to_str(err));
continue;
}
port->num_waiting_connect_backs++;
}
}
if (tried && !port->num_waiting_connect_backs && !num_connected_net(port)) {
/*
* This is kind of a bad situation. We got some data, attempted
* connects, but failed. Shut down the read enable for a while.
*/
port->nocon_read_enable_time_left = 10;
gensio_set_read_callback_enable(port->io, false);
} else if (port->num_waiting_connect_backs) {
gensio_set_read_callback_enable(port->io, false);
}
return port->num_waiting_connect_backs;
}
/* Data is ready to read on the serial port. */
static int
handle_dev_read(port_info_t *port, int err, unsigned char *buf,
gensiods buflen)
{
gensiods count = 0;
bool send_now = false;
int nr_handlers = 0;
so->lock(port->lock);
if (port->dev_to_net_state != PORT_WAITING_INPUT) {
gensio_set_read_callback_enable(port->io, false);
goto out_unlock;
}
if (err) {
if (port->dev_to_net.cursize) {
/* Let the output drain before shutdown. */
count = 0;
send_now = true;
goto do_send;
}
/* Got an error on the read, shut down the port. */
syslog(LOG_ERR, "dev read error for device on port %s: %s",
port->name, gensio_err_to_str(err));
if (port->connbacks)
port->enabled = false;
shutdown_port(port, "dev read error");
}
nr_handlers = port_check_connect_backs(port);
if (nr_handlers > 0)
goto out_unlock;
if (gbuf_room_left(&port->dev_to_net) < buflen)
buflen = gbuf_room_left(&port->dev_to_net);