-
Notifications
You must be signed in to change notification settings - Fork 2
/
redund.c
2650 lines (2441 loc) · 85.6 KB
/
redund.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
/*
* Copyright (C) 2001-2012 Moxa Inc.
* All rights reserved.
*
* Moxa NPort/Async Server UNIX Real TTY daemon program.
*
* Usage: npreal2d_redund [-t reset-time]
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/sysinfo.h>
#include <pthread.h>
#ifdef STREAM
#include <sys/ptms.h>
#endif
#ifdef SSL_ON
#include <openssl/ssl.h>
#endif
#include <arpa/inet.h>
#include "redund.h"
#include "npreal2d.h"
#define MOXA_DEBUG 1
#define CON_TIME 1000000 /* connection time : micro-second */
#define RE_TIME 5000000 /* reconnection time : micro-second */
//#define Gsession 0x01
/* the define of KeepAlive */
int8_t Gsession = 0x00;
int Gsession_cnt = 0;
int Gkeep_alive = 1; /* open keepalive */
int Gkeep_idle = 1; /* idle time (sec)*/
int Gkeep_interval = 3; /* interval time (sec) */
int Gkeep_count = 2; /* try how many time */
int Gport_cnt = 0;
int Gport_num = 0;
pthread_mutex_t Gmutex = PTHREAD_MUTEX_INITIALIZER;
int connect_nonb(int fd, struct sockaddr_in *sockaddr, socklen_t socklen, int usec);
int redund_send_data(int fd, int fd_bk, const char *sbuf, ssize_t len, struct expect_struct *expect, TTYINFO *infop);
int redund_send_cmd(int fd, int fd_bk, const char *sbuf, ssize_t len, struct expect_struct *expect);
int redund_add_hdr(const char *sbuf, char *dbuf, ssize_t len, struct expect_struct *expect);
int redund_add_hdr_data(int fd, const char *sbuf, char *dbuf, ssize_t len, struct expect_struct *expect, TTYINFO *infop);
int redund_recv_data(int fd, int fd_bk, char *sbuf, ssize_t len, struct expect_struct *expect, TTYINFO *infop);
int redund_recv_cmd(int fd, int fd_bk, char *sbuf, ssize_t len, struct expect_struct *expect, TTYINFO *infop);
int redund_data_init(int fd, struct expect_struct *expect);
int redund_cmd_init(int fd, struct expect_struct *expect);
int redund_reconnect(void *infop);
/* wrap for redund */
int do_redund_send_data(TTYINFO *infop, SERVINFO *servp, struct sysinfo *sys_info, fd_set *wfd);
int do_redund_recv_data(TTYINFO *infop, SERVINFO *servp, struct sysinfo *sys_info, fd_set *rfd);
int do_redund_send_cmd(TTYINFO *infop, int n);
int do_redund_recv_cmd(TTYINFO *infop, char *cmd_buf, fd_set *rfd);
int do_redund_reconnect(TTYINFO *infop);
/* poll_nport_send & poll_npoll_recv */
void redund_poll_nport_recv(int af_type);
void redund_poll_nport_send(SERVINFO *servp);
#define REDUND_INIT 0
#define REDUND_MPT_OPEN 1
#define REDUND_CONN_FAIL 2
#define REDUND_TCP_OPEN 3
#define REDUND_TCP_CONN 4
#ifdef SSL_ON
#define REDUND_SSL_CONN 5
#define REDUND_TCP_CLOSE 6
#define REDUND_TCP_WAIT 7
#define REDUND_MPT_REOPEN 8
#define REDUND_TTY_WAIT 9
#define REDUND_RW_DATA 10
#define REDUND_REMOTE_LISTEN 11
#else
#define REDUND_TCP_CLOSE 5
#define REDUND_TCP_WAIT 6
#define REDUND_MPT_REOPEN 7
#define REDUND_TTY_WAIT 8
#define REDUND_RW_DATA 9
#define REDUND_REMOTE_LISTEN 10
#endif
#define CONNECT_OK 0
#define CONNECT_FAIL 1
#define CLOSE_OK 2
#define TCP_LISTEN 1
#define TCP_CONNECTED 4
#define ERROR_MPT_OPEN 0x0001
#define ERROR_TCP_OPEN 0x0002
#define ERROR_TCP_CONN 0x0004
#define ERROR_FORK 0x0008
#define REDUND_SIZE 1024 /* temporary data buffer size */
#define MAX_TTYS 256 /* max. handling TTYs number */
#define MAX_PORTS 16 /* max. port of server is 16 */
#define IS_IPV4 0
#define IS_IPV6 1
extern int ttys, servers;
extern TTYINFO ttys_info[MAX_TTYS];
extern SERVINFO serv_info[MAX_TTYS];
extern char EventLog[160]; /* Event log file name */
extern int pipefd[2];
extern int maxfd;
extern int timeout_time;
extern int polling_time; /* default disable polling function */
extern int polling_fd;
extern int polling_nport_fd[2];
extern int Restart_daemon;
static int No_tty_defined;
static int enable_ipv6 = 1; /* 2 enable ipv6, 1 disenable ipv6 */
static int cnt = 0;
#define EN_IPV6 2
#define DIS_IPV6 1
redund_handle_ttys() /* child process ok */
{
int i, n, m, maxfd, t0, sndx, len, len1, j, ret;
int k;
TTYINFO * infop, *infop_tmp;
SERVINFO * servp;
fd_set rfd, wfd, efd;
struct timeval tm;
char cmd_buf[CMD_REDUND_SIZE], buf[100];
ConnMsg msg;
int tcp_wait_count;
struct sysinfo sys_info;
int test;
signal(SIGPIPE, SIG_IGN); /* add for "broken pipe" error */
while (1)
{
if (Restart_daemon == 1)
{
for ( i=0, infop=&ttys_info[0]; i<ttys; i+=1, infop+=1 )
{
if (!infop->redundant_mode)
continue;
if (infop->tty_used_timestamp)
{
ioctl(infop->mpt_fd,
_IOC(_IOC_READ|_IOC_WRITE,'m',CMD_DISCONNECTED,0),
0);
}
infop->reconn_flag = 1;
}
return;
}
tm.tv_sec = 3;
tm.tv_usec = 0;
FD_ZERO(&rfd);
FD_ZERO(&wfd);
FD_ZERO(&efd);
maxfd = -1;
sndx = -1;
tcp_wait_count = 0;
for ( i=0, infop=&ttys_info[0]; i<ttys; i+=1, infop+=1 )
{
if (!infop->redundant_mode)
continue;
if ( infop->state == REDUND_INIT ||
infop->state == REDUND_MPT_OPEN ||
infop->state == REDUND_MPT_REOPEN )
{
_OpenTty(infop);
}
if ( infop->state == REDUND_CONN_FAIL )
{
sysinfo(&sys_info);
if ( (sys_info.uptime - infop->time_out) >= 1 ) {
infop->state = REDUND_TCP_OPEN;
}
}
if ( infop->state == REDUND_TCP_OPEN )
redund_open(infop);
if ( infop->state == REDUND_TCP_CONN )
redund_connect(infop);
if ( infop->state == REDUND_TCP_CLOSE )
redund_close(infop);
if ( infop->state == REDUND_TCP_WAIT )
{
redund_connect_check(infop);
if ( infop->state == REDUND_TCP_WAIT )
tcp_wait_count++;
}
if ( infop->state < REDUND_TTY_WAIT )
{
tm.tv_sec = 1;
}
else if ( infop->state == REDUND_REMOTE_LISTEN)
{
redund_close(infop);
continue;
}
if (infop->mpt_fd >= 0)
FD_SET(infop->mpt_fd, &efd);
if ( infop->mpt_fd > maxfd )
maxfd = infop->mpt_fd;
servp = &serv_info[infop->serv_index];
if ( (infop->state >= REDUND_RW_DATA)&&polling_time )
{
if (!infop->first_servertime)
{
sysinfo(&sys_info);
infop->first_servertime = sys_info.uptime - 1;
servp->last_servertime = (time_t)((int32_t)(sys_info.uptime - 1));
}
if ( sndx < 0 )
{
sysinfo(&sys_info);
if ( ((time_t)((int32_t)sys_info.uptime) - servp->next_sendtime) > 0 )
{
sndx = infop->serv_index;
FD_SET(polling_fd, &wfd);
}
if (((time_t)((int32_t)sys_info.uptime)-servp->last_servertime)>timeout_time)
{
infop->first_servertime = 0;
infop->state = REDUND_REMOTE_LISTEN;
infop->time_out = sys_info.uptime;
servp->start_item = 0;
}
}
FD_SET(polling_fd, &rfd);
if ( polling_fd > maxfd )
maxfd = polling_fd;
for(n=0; n<enable_ipv6; n++)
{
FD_SET(polling_nport_fd[n], &rfd);
if ( polling_nport_fd[n] > maxfd )
maxfd = polling_nport_fd[n];
}
}
if (infop->state >= REDUND_RW_DATA)
{
if (infop->mpt_fd > maxfd)
maxfd = infop->mpt_fd;
if (infop->redund.connect[0]) {
if (infop->redund.sock_data[0] > maxfd)
maxfd = infop->redund.sock_data[0];
if (infop->redund.sock_cmd[0] > maxfd)
maxfd = infop->redund.sock_cmd[0];
}
if (infop->redund.connect[1]) {
if (infop->redund.sock_data[1] > maxfd)
maxfd = infop->redund.sock_data[1];
if (infop->redund.sock_cmd[1] > maxfd)
maxfd = infop->redund.sock_cmd[1];
}
if ( infop->mpt_datakeep )
{
if (infop->redund.connect[0]) {
FD_SET(infop->redund.sock_data[0], &wfd);
}
if (infop->redund.connect[1]) {
FD_SET(infop->redund.sock_data[1], &wfd);
}
}
else
{
FD_SET(infop->mpt_fd, &rfd);
}
if ( infop->sock_datakeep )
{
FD_SET(infop->mpt_fd, &wfd);
}
else
{
if (infop->redund.connect[0]) {
FD_SET(infop->redund.sock_data[0], &rfd);
}
if (infop->redund.connect[1]) {
FD_SET(infop->redund.sock_data[1], &rfd);
}
}
if (infop->redund.connect[0]) {
FD_SET(infop->redund.sock_cmd[0], &rfd);
}
if (infop->redund.connect[1]) {
FD_SET(infop->redund.sock_cmd[1], &rfd);
}
}
}
if (tcp_wait_count)
{
tm.tv_sec = 0;
tm.tv_usec = 20000;
}
cnt++;
cnt++;
if ((j= select(maxfd+1, &rfd, &wfd, &efd, &tm)) <= 0 )
{
continue;
}
cnt++;
for ( i=0, infop=&ttys_info[0]; i<ttys; i+=1, infop+=1 )
{
if (!infop->redundant_mode)
continue;
if ( infop->mpt_fd < 0)
continue;
if ( (infop->mpt_fd)&&FD_ISSET(infop->mpt_fd, &efd) )
{ /* cmd ready */
if ((n=ioctl(infop->mpt_fd,
_IOC(_IOC_READ|_IOC_WRITE,'m',CMD_RETRIEVE,CMD_REDUND_SIZE),
infop->mpt_cmdbuf)) > 0)
{
if (infop->mpt_cmdbuf[0] == NPREAL_ASPP_COMMAND_SET)
{
do_redund_send_cmd(infop, n);
}
else if (infop->mpt_cmdbuf[0] == NPREAL_LOCAL_COMMAND_SET)
{
switch (infop->mpt_cmdbuf[1])
{
case LOCAL_CMD_TTY_USED:
if (infop->state != REDUND_TTY_WAIT)
{
#ifdef SSL_ON
if (infop->ssl_enable)
{
SSL_shutdown(infop->pssl);
SSL_free(infop->pssl);
infop->pssl = NULL;
}
#endif
shutdown(infop->redund.sock_data[0], 2);
shutdown(infop->redund.sock_cmd[0], 2);
close(infop->redund.sock_data[0]);
close(infop->redund.sock_cmd[0]);
infop->redund.sock_data[0] = -1;
infop->redund.sock_cmd[0] = -1;
shutdown(infop->redund.sock_data[1], 2);
shutdown(infop->redund.sock_cmd[1], 2);
close(infop->redund.sock_data[1]);
close(infop->redund.sock_cmd[1]);
infop->redund.sock_data[1] = -1;
infop->redund.sock_cmd[1] = -1;
infop->local_tcp_port = 0;
infop->local_cmd_port = 0;
sprintf(cmd_buf, "Repeat connection!, %d, %s\n", infop->tcp_port, infop->ip_addr_s);
log_event(cmd_buf);
sleep(1);
}
infop->state = REDUND_TCP_OPEN;
infop->redund.close[0] = 0;
infop->redund.close[1] = 0;
infop->redund.thread[0] = 0;
infop->redund.thread[1] = 0;
infop->redund.data.repush_seq[0] = -1;
infop->redund.data.repush_seq[1] = -1;
infop->redund.cmd.repush_seq[0] = -1;
infop->redund.cmd.repush_seq[1] = -1;
infop->redund.data.last_seq = -1;
infop->redund.cmd.last_seq = -1;
sysinfo(&sys_info);
infop->tty_used_timestamp = sys_info.uptime;
continue;
case LOCAL_CMD_TTY_UNUSED: /* when utility calling close(fd) */
#ifdef SSL_ON
if (infop->ssl_enable)
{
SSL_shutdown(infop->pssl);
SSL_free(infop->pssl);
infop->pssl = NULL;
}
#endif
infop->redund.close[0] = 1;
infop->redund.close[1] = 1;
shutdown(infop->redund.sock_data[1], 2);
shutdown(infop->redund.sock_cmd[1], 2);
close(infop->redund.sock_data[1]);
close(infop->redund.sock_cmd[1]);
infop->redund.sock_data[1] = -1;
infop->redund.sock_cmd[1] = -1;
shutdown(infop->redund.sock_data[0], 2);
shutdown(infop->redund.sock_cmd[0], 2);
close(infop->redund.sock_data[0]);
close(infop->redund.sock_cmd[0]);
infop->redund.sock_data[0] = -1;
infop->redund.sock_cmd[0] = -1;
infop->local_tcp_port = 0;
infop->local_cmd_port = 0;
cmd_buf[0] = NPREAL_LOCAL_COMMAND_SET;
cmd_buf[1] = LOCAL_CMD_TTY_UNUSED;
ioctl(infop->mpt_fd,
_IOC(_IOC_READ|_IOC_WRITE,'m',CMD_RESPONSE,2),
cmd_buf);
infop->sock_datakeep = 0;
infop->sock_dataofs = 0;
infop->mpt_datakeep = 0;
infop->mpt_dataofs = 0;
if ((infop->state < REDUND_RW_DATA) && !(infop->error_flags & ERROR_TCP_CONN))
{
sprintf(cmd_buf, "Socket connect fail (%s,TCP port %d) !",
infop->ip_addr_s,
infop->tcp_port);
log_event(cmd_buf);
}
infop->state = REDUND_TTY_WAIT;
infop->tty_used_timestamp = 0;
infop->first_servertime = 0;
memset(&infop->redund.data, 0, sizeof(struct expect_struct));
memset(&infop->redund.cmd, 0, sizeof(struct expect_struct));
infop->redund.data_open[0] = 0;
infop->redund.data_open[1] = 0;
infop->redund.cmd_open[0] = 0;
infop->redund.cmd_open[1] = 0;
infop->redund.connect[0] = 0;
infop->redund.connect[1] = 0;
infop->redund.data.last_seq = -1;
infop->redund.cmd.last_seq = -1;
infop->redund.wlen = 0;
infop->redund.rlen = 0;
infop->redund.host_ack = 0;
infop->redund.reconnect[0] = 0;
infop->redund.reconnect[1] = 0;
Gsession_cnt = 0;
continue;
}
}
}
}
if ( infop->state < REDUND_RW_DATA )
continue;
ret = do_redund_recv_cmd(infop, cmd_buf, &rfd);
if ( FD_ISSET(infop->mpt_fd, &rfd) )
{
m = infop->mpt_datakeep + infop->mpt_dataofs;
n = read(infop->mpt_fd,
infop->mpt_bufptr + m,
REDUND_SIZE - m);
if ( n > 0 )
infop->mpt_datakeep += n;
}
ret = do_redund_recv_data(infop, servp ,&sys_info ,&rfd);
#if 0
if (ret < 0 && ret == -2) {
for (k = 0, infop_tmp = &ttys_info[0]; k < ttys; k += 1, infop_tmp += 1) {
if (infop_tmp->redund.connect[0] > 0 || infop_tmp->redund.connect[1] > 0) {
close(infop_tmp->redund.sock_data[1]);
close(infop_tmp->redund.sock_cmd[1]);
infop_tmp->redund.connect[1] = 0;
}
}
} else if (ret < 0 && ret == -1) {
for (k = 0, infop_tmp = &ttys_info[0]; k < ttys; k += 1, infop_tmp += 1) {
if (infop_tmp->redund.connect[0] > 0 || infop_tmp->redund.connect[1] > 0) {
close(infop_tmp->redund.sock_data[0]);
close(infop_tmp->redund.sock_cmd[0]);
infop_tmp->redund.connect[0] = 0;
}
}
}
#endif
if ( FD_ISSET(infop->mpt_fd, &wfd) )
{
n = write(infop->mpt_fd,
infop->sock_bufptr+infop->sock_dataofs,
infop->sock_datakeep);
if ( n > 0 )
{
infop->sock_datakeep -= n;
if ( infop->sock_datakeep )
infop->sock_dataofs += n;
else
infop->sock_dataofs = 0;
}
}
ret = do_redund_send_data(infop, servp ,&sys_info ,&wfd);
#if 0
for (k = 0, infop_tmp = &ttys_info[0]; k < ttys; k += 1, infop_tmp += 1) {
if (infop_tmp->redund.connect[0] > 0 || infop_tmp->redund.connect[1] > 0) {
if (infop_tmp->redund.connect[0] == 0 || infop_tmp->redund.connect[1] == 0)
do_redund_reconnect(infop_tmp);
}
}
#else
do_redund_reconnect(infop);//
#endif
}
if ( polling_time == 0 )
continue;
#if 1
if ((sndx >= 0) && FD_ISSET(polling_fd, &wfd)) {
redund_poll_nport_send(&serv_info[sndx]);
}
if (FD_ISSET(polling_fd, &rfd)) {
}
for (n = 0; n < enable_ipv6; n++) {
if (FD_ISSET(polling_nport_fd[n], &rfd)) {
redund_poll_nport_recv(n);
}
}
#endif
}
}
_OpenTty(infop)
TTYINFO * infop;
{
int flag;
char buf[80];
flag = 0;
infop->redund.data.repush_seq[0] = -1;
infop->redund.data.repush_seq[1] = -1;
infop->redund.cmd.repush_seq[0] = -1;
infop->redund.cmd.repush_seq[1] = -1;
infop->redund.data.last_seq = -1;
infop->redund.cmd.last_seq = -1;
infop->redund.close[0] = 0;
infop->redund.close[1] = 0;
infop->redund.thread[0] = 0;
infop->redund.thread[1] = 0;
if ( infop->mpt_fd >= 0 )
{
/* close(infop->mpt_fd); 05-02-2001 Jming */
flag = 1;
}
else
{
flag = 0;
infop->mpt_fd = open(infop->mpt_name, O_RDWR);
}
if ( infop->mpt_fd >= 0 )
{
struct net_node_setting nd_settings;
int tty_status = 0;
infop->reconn_flag = 1;
nd_settings.server_type = infop->server_type;
nd_settings.disable_fifo = infop->disable_fifo;
ioctl(infop->mpt_fd,
_IOC(_IOC_READ|_IOC_WRITE,'m',CMD_NET_SETTING,
sizeof(struct net_node_setting)),
&nd_settings);
ioctl(infop->mpt_fd,
_IOC(_IOC_READ|_IOC_WRITE,'m',CMD_GET_TTY_STATUS,
sizeof(int)),&tty_status);
if (infop->tty_used_timestamp == 0)
{
if (!tty_status)
{
infop->state = REDUND_TTY_WAIT;
}
else
{
ioctl(infop->mpt_fd,
_IOC(_IOC_READ|_IOC_WRITE,'m',CMD_DISCONNECTED,0),
0);
infop->state = REDUND_TCP_OPEN;
}
}
else {
infop->state = REDUND_TCP_OPEN;
}
}
else if ( flag == 0 )
{
if ( !(infop->error_flags & ERROR_MPT_OPEN) )
{
sprintf(buf, "Master tty open fail (%s) !",
infop->mpt_name);
log_event(buf);
infop->error_flags |= ERROR_MPT_OPEN;
}
}
}
#define SOCK_BUF 1048
redund_open(infop)
TTYINFO * infop;
{
char buf[80];
int on = 1;
int af;
int ret;
int inter = 1;
af = infop->af;
/* open first data socket */
infop->redund.sock_data[0] = socket(af, SOCK_STREAM, 0);
if (infop->redund.sock_data[0] >= 0) {
infop->redund.data_open[0] = 1;
}
/* open second data socket */
infop->redund.sock_data[1] = socket(af, SOCK_STREAM, 0);
if (infop->redund.sock_data[1] >= 0) {
infop->redund.data_open[1] = 1;
} else {
//printf("[AP] Socket data two fail\n");
}
/* open first cmd socket */
infop->redund.sock_cmd[0] = socket(af, SOCK_STREAM, 0);
if (infop->redund.sock_cmd[0] >= 0) {
if (setsockopt(infop->redund.sock_cmd[0], SOL_SOCKET,
SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) {
log_event("Set TCP keep alive fail !");
}
ret = setsockopt(infop->redund.sock_cmd[0], SOL_TCP, TCP_KEEPIDLE,
&Gkeep_idle,
sizeof(Gkeep_idle));
if (ret < 0)
printf("setsockopt SO_KEEPIDLE error! %d\n", ret);
ret = setsockopt(infop->redund.sock_cmd[0], SOL_TCP, TCP_KEEPINTVL,
&(Gkeep_interval),
sizeof(Gkeep_interval));
if (ret < 0)
printf("setsockopt SO_KEEPINTVL error! %d\n", ret);
ret = setsockopt(infop->redund.sock_cmd[0], SOL_TCP, TCP_KEEPCNT,
&(Gkeep_count),
sizeof(Gkeep_count));
if (ret < 0)
printf("setsockopt SO_KEEPCNT error! %d\n", ret);
#if 0
if (strlen(infop->scope_id) > 0) {
if (setsockopt(infop->redund.sock_cmd[0], SOL_SOCKET,
SO_BINDTODEVICE, infop->scope_id, strlen(infop->scope_id)) < 0) {
log_event("Set TCP bind to device fail !");
}
}
#endif
infop->redund.cmd_open[0] = 1;
}
/* open second cmd socket */
infop->redund.sock_cmd[1] = socket(af, SOCK_STREAM, 0);
if (infop->redund.sock_cmd[1] >= 0) {
if (setsockopt(infop->redund.sock_cmd[1], SOL_SOCKET,
SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) {
log_event("Set TCP keep alive fail !");
}
ret = setsockopt(infop->redund.sock_cmd[1], SOL_TCP, TCP_KEEPIDLE,
&Gkeep_idle,
sizeof(Gkeep_idle));
if (ret < 0)
printf("setsockopt SO_KEEPIDLE error! %d\n", ret);
ret = setsockopt(infop->redund.sock_cmd[1], SOL_TCP, TCP_KEEPINTVL,
&Gkeep_interval,
sizeof(Gkeep_interval));
if (ret < 0)
printf("setsockopt SO_KEEPINTVL error! %d\n", ret);
ret = setsockopt(infop->redund.sock_cmd[1], SOL_TCP, TCP_KEEPCNT,
&Gkeep_count,
sizeof(Gkeep_count));
if (ret < 0)
printf("setsockopt SO_KEEPCNT error! %d\n", ret);
#if 0
if (strlen(infop->scope_id) > 0) {
if (setsockopt(infop->redund.sock_cmd[1], SOL_SOCKET,
SO_BINDTODEVICE, infop->scope_id, strlen(infop->scope_id)) < 0) {
log_event("Set TCP bind to device fail !");
}
}
#endif
infop->redund.cmd_open[1] = 1;
} else {
//printf("[AP] Socket cmd two fail\n");
}
/* check the first connection ok or no */
if ((infop->redund.sock_data[0] < 0) || (infop->redund.sock_cmd[0] < 0))
{
close(infop->redund.sock_data[0]);
close(infop->redund.sock_cmd[0]);
if ( !(infop->error_flags & ERROR_TCP_OPEN) )
{
if (infop->redund.sock_data[0] < 0)
{
sprintf(buf, "Socket one open fail (%s, TCP port %d) !",
infop->ip_addr_s,
infop->tcp_port);
log_event(buf);
}
if (infop->redund.sock_cmd[0] < 0)
{
sprintf(buf, "Socket one open fail (%s, TCP port %d) !",
infop->ip_addr_s,
infop->cmd_port);
log_event(buf);
}
infop->error_flags |= ERROR_TCP_OPEN;
}
/* first pair fail */
infop->redund.sock_data[0] = -1;
infop->redund.sock_cmd[0] = -1;
infop->redund.data_open[0] = 0;
}
/* check the second connection ok or no */
if ((infop->redund.sock_data[1] < 0) || (infop->redund.sock_cmd[1] < 0))
{
close(infop->redund.sock_data[1]);
close(infop->redund.sock_cmd[1]);
if ( !(infop->error_flags & ERROR_TCP_OPEN) )
{
if (infop->redund.sock_data[1] < 0)
{
sprintf(buf, "Socket two open fail (%s, TCP port %d) !",
infop->ip_addr_s,
infop->tcp_port);
log_event(buf);
}
if (infop->redund.sock_cmd[1] < 0)
{
sprintf(buf, "Socket two open fail (%s, TCP port %d) !",
infop->ip_addr_s,
infop->cmd_port);
log_event(buf);
}
infop->error_flags |= ERROR_TCP_OPEN;
}
/* second pair fail */
infop->redund.sock_data[1] = -1;
infop->redund.sock_cmd[1] = -1;
infop->redund.data_open[1] = 0;
}
if ((infop->redund.cmd_open[0] && infop->redund.data_open[0])
|| (infop->redund.cmd_open[1] && infop->redund.data_open[1]))
infop->state = REDUND_TCP_CONN;
else
infop->state = REDUND_TCP_OPEN;
}
redund_connect(infop)
TTYINFO * infop;
{
int childpid, n;
ConnMsg msg;
union sock_addr sock, sock_bk;
int ret;
infop->redund.data.ack = 0;
infop->redund.data.seq = 0;
infop->redund.data.nport_ack = 0;
infop->redund.cmd.ack = 0;
infop->redund.cmd.seq = 0;
infop->redund.cmd.nport_ack = 0;
if (infop->af == AF_INET6 && enable_ipv6 == DIS_IPV6) {
//printf("[AP]edund_connect return\n");
return;
}
infop->state = REDUND_TCP_WAIT;
infop->tcp_wait_id++;
if ((childpid = fork()) == 0) {
#if 1
/* child process */
msg.tcp_wait_id = infop->tcp_wait_id;
close(infop->pipe_port[0]);
msg.status = CONNECT_FAIL;
if (infop->af == AF_INET) {
sock.sin.sin_family = AF_INET;
sock.sin.sin_addr.s_addr = *(u_long*)infop->ip6_addr;
sock.sin.sin_port = htons(infop->cmd_port);
} else {
memset(&sock.sin6, 0, sizeof(sock));
sock.sin6.sin6_family = AF_INET6;
sock.sin6.sin6_port = htons(infop->cmd_port);
memcpy(sock.sin6.sin6_addr.s6_addr, infop->ip6_addr, 16);
}
if (connect_nonb(infop->redund.sock_cmd[0], (struct sockaddr_in*)&sock, sizeof(sock), CON_TIME) >= 0) {
if (infop->af == AF_INET) {
sock.sin.sin_family = AF_INET;
sock.sin.sin_addr.s_addr = *(u_long*)infop->ip6_addr;
sock.sin.sin_port = htons(infop->tcp_port);
} else {
sock.sin6.sin6_family = AF_INET6;
sock.sin6.sin6_port = htons(infop->tcp_port);
memcpy(sock.sin6.sin6_addr.s6_addr, infop->ip6_addr, 16);
}
if (connect_nonb(infop->redund.sock_data[0], (struct sockaddr_in*)&sock, sizeof(sock), CON_TIME) >= 0) {
if (infop->af == AF_INET6) {
int rand[16];
if (write(infop->redund.sock_cmd[0], rand, 16) >= 0) {
if (read(infop->redund.sock_cmd[0], rand, 16) != 16) {
msg.infop = infop;
write(infop->pipe_port[1], (char *)&msg, sizeof(ConnMsg));
close(infop->pipe_port[1]);
exit(0);
}
}
}
if (redund_data_init(infop->redund.sock_data[0], &infop->redund.data) > 0) {
if (redund_cmd_init(infop->redund.sock_cmd[0], &infop->redund.cmd) > 0) {
infop->redund.connect[0] = 1;
infop->redund.session = Gsession;
} else {
infop->redund.connect[0] = 0;
}
} else {
infop->redund.connect[0] = 0;
}
}
}
#endif
msg.tcp_wait_id = infop->tcp_wait_id;
close(infop->pipe_port[0]);
msg.status = CONNECT_FAIL;
if (infop->af == AF_INET) {
sock_bk.sin.sin_family = AF_INET;
sock_bk.sin.sin_addr.s_addr = *(u_long*)infop->redund.ip6_addr;
sock_bk.sin.sin_port = htons(infop->cmd_port);
} else {
memset(&sock_bk.sin6, 0, sizeof(sock_bk));
sock_bk.sin6.sin6_family = AF_INET6;
sock_bk.sin6.sin6_port = htons(infop->cmd_port);
memcpy(sock_bk.sin6.sin6_addr.s6_addr, infop->redund.ip6_addr, 16);
}
if ((ret = connect_nonb(infop->redund.sock_cmd[1], (struct sockaddr_in*)&sock_bk, sizeof(sock_bk), CON_TIME)) >= 0) {
if (infop->af == AF_INET) {
sock_bk.sin.sin_family = AF_INET;
sock_bk.sin.sin_addr.s_addr = *(u_long*)infop->redund.ip6_addr;
sock_bk.sin.sin_port = htons(infop->tcp_port);
} else {
sock_bk.sin6.sin6_family = AF_INET6;
sock_bk.sin6.sin6_port = htons(infop->tcp_port);
memcpy(sock_bk.sin6.sin6_addr.s6_addr, infop->redund.ip6_addr, 16);
}
if (connect_nonb(infop->redund.sock_data[1], (struct sockaddr_in*)&sock_bk, sizeof(sock_bk), CON_TIME) >= 0) {
if (infop->af == AF_INET6) {
int rand[16];
if (write(infop->redund.sock_cmd[1], rand, 16) >= 0) {
if (read(infop->redund.sock_cmd[1], rand, 16) != 16) {
msg.infop = infop;
write(infop->pipe_port[1], (char *)&msg, sizeof(ConnMsg));
close(infop->pipe_port[1]);
exit(0);
}
}
}
if (redund_data_init(infop->redund.sock_data[1], &infop->redund.data) > 0) {
if (redund_cmd_init(infop->redund.sock_cmd[1], &infop->redund.cmd) > 0) {
infop->redund.connect[1] = 1;
infop->redund.session = Gsession;
} else {
infop->redund.connect[1] = 0;
}
} else {
infop->redund.connect[1] = 0;
}
}
} else {
}
msg.connect[0] = infop->redund.connect[0];
msg.connect[1] = infop->redund.connect[1];
msg.session = Gsession;
if (infop->redund.connect[0] || infop->redund.connect[1])
msg.status = CONNECT_OK;
msg.infop = infop;
write(infop->pipe_port[1], (char *)&msg, sizeof(ConnMsg));
close(infop->pipe_port[1]);
exit(0);
} else if (childpid < 0) {
infop->state = REDUND_TCP_CONN;
if (!(infop->error_flags & ERROR_FORK)) {
log_event("Can't fork child process !");
infop->error_flags |= ERROR_FORK;
}
}
}
redund_close(infop)
TTYINFO * infop;
{
struct sockaddr_in sin;
int childpid;
ConnMsg msg;
infop->state = REDUND_TCP_WAIT;
infop->tcp_wait_id++;
if ( (childpid = fork()) == 0 )
{ /* child process */
msg.tcp_wait_id = infop->tcp_wait_id;
//close(pipefd[0]);
close(infop->pipe_port[0]);
#ifdef SSL_ON
if (infop->ssl_enable)
{
SSL_shutdown(infop->pssl);
SSL_free(infop->pssl);
infop->pssl = NULL;
}
#endif
close(infop->redund.sock_data[1]);
close(infop->redund.sock_cmd[1]);
close(infop->redund.sock_data[0]);
close(infop->redund.sock_cmd[0]);
sleep(1);
msg.status = CLOSE_OK;
msg.infop = infop;
//write(pipefd[1], (char *)&msg, sizeof(ConnMsg));
//close(pipefd[1]);
write(infop->pipe_port[1], (char *)&msg, sizeof(ConnMsg));
close(infop->pipe_port[1]);
exit(0);
}
else if ( childpid < 0 )
{
infop->state = REDUND_TCP_CLOSE;
if ( !(infop->error_flags & ERROR_FORK) )
{
log_event("Can't fork child process !");
infop->error_flags |= ERROR_FORK;
}
}
if ( infop->state != REDUND_TCP_CLOSE )
{
#ifdef SSL_ON
if (infop->ssl_enable)
{
SSL_shutdown(infop->pssl);
SSL_free(infop->pssl);
infop->pssl = NULL;
}
#endif
close(infop->redund.sock_data[0]);
close(infop->redund.sock_cmd[0]);
close(infop->redund.sock_data[1]);
close(infop->redund.sock_cmd[1]);
infop->local_tcp_port = 0;
infop->local_cmd_port = 0;
}
}
redund_connect_check(TTYINFO *infopp)
{
ConnMsg msg;
TTYINFO * infop;
char buf[80];
int ret;
struct sysinfo sys_info;
struct sockaddr_in local_sin;
struct sockaddr_in6 local_sin6;
socklen_t socklen = sizeof(local_sin);
struct sockaddr * ptr;
if ((ret=read(infopp->pipe_port[0], (char *)&msg, sizeof(ConnMsg))) == sizeof(ConnMsg))
{
infop = msg.infop;
Gsession = msg.session;
Gsession_cnt = 1;
infop->redund.session = msg.session;
infop->redund.connect[0] = msg.connect[0];
infop->redund.connect[1] = msg.connect[1];
if (!infop->redund.connect[0]) {
close(infop->redund.sock_data[0]);
close(infop->redund.sock_cmd[0]);