コミットメタ情報

リビジョン4b7bf5e608d1205e93cd1927f97ed0c4d5e1680a (tree)
日時2019-08-10 13:25:58
作者Satoshi Yasuda <7m3tjz@jarl...>
コミッターSatoshi Yasuda

ログメッセージ

new file: xchange_utils.c

変更サマリ

差分

--- /dev/null
+++ b/xchange_utils.c
@@ -0,0 +1,725 @@
1+#include "xchange.h"
2+#include "config.h"
3+
4+extern void handler(int sig);
5+void send_gw (unsigned char pkt[]);
6+int getFifo (unsigned char pkt[]);
7+void putFifo (int len, unsigned char pkt[]);
8+
9+
10+#define CRCPOLY2 0xEDB88320
11+
12+unsigned int crc32(int n, unsigned char c[])
13+{
14+ int i, j;
15+ unsigned int r;
16+
17+ r = 0xFFFFFFFFUL;
18+ for (i = 0; i < n; i++) {
19+ r ^= c[i];
20+ for (j = 0; j < 8 ; j++)
21+ if (r & 1) r = (r >> 1) ^ CRCPOLY2;
22+ else r >>= 1;
23+ }
24+ return r ^ 0xFFFFFFFF;
25+}
26+
27+
28+void pselectSet(void)
29+{
30+ FD_ZERO (&fd_save);
31+ sigemptyset (&save_sig);
32+ sigaddset (&save_sig, SIGUSR1);
33+ sigaddset (&save_sig, SIGUSR2);
34+ sigaddset (&save_sig, SIGTERM);
35+ sigaddset (&save_sig, SIGINT);
36+ timeout.tv_sec = 0;
37+ timeout.tv_nsec = 10000000;
38+}
39+
40+struct ModuleTable *module_check (char call[])
41+{
42+
43+ struct ModuleTable *pnt;
44+
45+ if (call[16] == 0x20) return NULL;
46+ if (!memcmp (&call[16], "CQCQCQ", 6)) return NULL;
47+ if (!memcmp (&call[8], "CQCQCQ", 6)) return NULL;
48+
49+ pnt = module_pnt;
50+
51+ while (pnt)
52+ {
53+ //printf ("%8.8s %8.8s\n", call, pnt->ModuleName);
54+ if (!memcmp (call, pnt->ModuleName, 8)) return pnt;
55+ pnt = pnt->f_chain;
56+ }
57+ if (debug_sw)
58+ {
59+ time (&cur_time);
60+ fprintf (log_file, "%24.24s Module %8.8s not in Table\n", ctime(&cur_time), call);
61+ fflush (log_file);
62+ }
63+
64+ return NULL;
65+}
66+
67+struct ModuleTable *module_check_ex (unsigned char FrameID[])
68+{
69+ struct ModuleTable *pnt;
70+
71+ pnt = module_pnt;
72+ while (pnt)
73+ {
74+ //printf ("debug %x %x %x %x\n", FrameID[0], FrameID[1], pnt->FrameID[0], pnt->FrameID[1]);
75+ if (!memcmp (FrameID, pnt->FrameID, 2)) return pnt;
76+ pnt = pnt->f_chain;
77+ }
78+ return NULL;
79+}
80+
81+struct ModuleTable *module_send_frame_check (unsigned char FrameID[])
82+{
83+ struct ModuleTable *pnt;
84+
85+ pnt = module_pnt;
86+ while (pnt)
87+ {
88+ if (!memcmp (FrameID, pnt->SendFrameID, 2)) return pnt;
89+ pnt = pnt->f_chain;
90+ }
91+ return NULL;
92+}
93+
94+struct ModuleTable *module_recv_frame_check (unsigned char FrameID[])
95+{
96+ struct ModuleTable *pnt;
97+
98+ pnt = module_pnt;
99+ while (pnt)
100+ {
101+ if (!memcmp (FrameID, pnt->DVRecvFrameID, 2)) return pnt;
102+ pnt = pnt->f_chain;
103+ }
104+ return NULL;
105+}
106+
107+struct ModuleTable *module_skip_frame_check (unsigned char FrameID[])
108+{
109+ struct ModuleTable *pnt;
110+
111+ pnt = module_pnt;
112+ while (pnt)
113+ {
114+ if (!memcmp (FrameID, pnt->GWFrameID, 2)) return pnt;
115+ pnt = pnt->f_chain;
116+ }
117+ return NULL;
118+}
119+
120+
121+struct ModuleTable *module_zr_frame_check (unsigned char FrameID[])
122+{
123+ struct ModuleTable *pnt;
124+
125+ pnt = module_pnt;
126+ while (pnt)
127+ {
128+//printf ("debug %x %x %x %x\n", FrameID[0], FrameID[1], pnt->ZrFrameID[0], pnt->ZrFrameID[1]);
129+ if (!memcmp (FrameID, pnt->ZrFrameID, 2)) return pnt;
130+ pnt = pnt->f_chain;
131+ }
132+ return NULL;
133+}
134+void module_init(struct ModuleTable *module, char Module[])
135+{
136+ module->AccessTime = 0;
137+ memcpy (module->ModuleName, Module, 8);
138+ memset (module->mycall, 0x20, 8);
139+ memset (module->mycall_ex, 0x20, 4);
140+ memset (module->rptcall, 0x20, 8);
141+ memset (module->DVRecvFrameID, 0x00, 2);
142+ memset (module->DDRecvFrameID, 0x00, 2);
143+ memset (module->SendFrameID, 0x00, 2);
144+ memset (module->ZrFrameID, 0x00, 2);
145+ memset (module->FrameID, 0x00, 2);
146+ memset (module->GWFrameID, 0x00, 2);
147+ module->ZR_on = FALSE;
148+ module->packet_cnt = 0;
149+ module->f_chain = NULL;
150+ module->SendSeq = 0;
151+ module->rpt_save_length = 0;
152+ module->recv_time.tv_sec = 0;
153+ module->recv_time.tv_usec = 0;
154+ module->lost_cnt = 0;
155+ module->block_sw = FALSE;
156+ module->zr_dv_in_cnt = 0;
157+ module->zr_dv_out_cnt = 0;
158+ module->gw_dv_in_cnt = 0;
159+ module->gw_dv_out_cnt = 0;
160+ module->mon_dv_in_cnt = 0;
161+ module->zr_dd_in_cnt = 0;
162+ module->zr_dd_out_cnt = 0;
163+ module->gw_dd_in_cnt = 0;
164+ module->gw_dd_out_cnt = 0;
165+ module->mon_dd_in_cnt = 0;
166+ module->ReplySW = FALSE;
167+}
168+
169+void forward_init (struct forward *fwd, char FullName[], int port, char mnemonic[], int gw_on_sw, char gw_pkt_type)
170+{
171+ fwd->f_chain = NULL;
172+ fwd->port = port;
173+ memcpy (fwd->fqdn, FullName, 128);
174+ memcpy (fwd->mnemonic, mnemonic, 16);
175+ fwd->in_packets = 0;
176+ fwd->out_packets = 0;
177+ fwd->alive = FALSE;
178+}
179+
180+void addBlockTable (char FrameID[])
181+{
182+ struct BlockTable *next;
183+ time_t cur_time;
184+
185+ next = block_pnt;
186+ while (next)
187+ {
188+ if (!memcmp (next->BlockID, FrameID, 2)) return;
189+ next = next->f_chain;
190+ }
191+
192+ next = malloc (sizeof (struct BlockTable));
193+ if (next != NULL)
194+ {
195+ memcpy (next->BlockID, FrameID, 2);
196+ time (&next->recv_time);
197+ next->f_chain = NULL;
198+ if (block_pnt) block_last->f_chain = next;
199+ else block_pnt = next;
200+ block_last = next;
201+ }
202+ else
203+ {
204+ time(&cur_time);
205+ fprintf (log_file, "%24.24s Memoty not allocate block table\n", ctime(&cur_time));
206+ fflush (log_file);
207+ }
208+}
209+
210+void delBlockTable (char FrameID[])
211+{
212+ struct BlockTable *next;
213+ struct BlockTable *last;
214+ struct BlockTable *temp;
215+
216+ next = block_pnt;
217+ last = NULL;
218+ while (next)
219+ {
220+ if (!memcmp (FrameID, next->BlockID, 2))
221+ {
222+ temp = next;
223+ if (last)
224+ {
225+ last->f_chain = next->f_chain;
226+ next = last->f_chain;
227+ }
228+ else
229+ {
230+ block_pnt = next->f_chain;
231+ next = block_pnt;
232+ }
233+ free (temp);
234+ return;
235+ }
236+ last = next;
237+ next = next->f_chain;
238+ }
239+}
240+
241+int checkBlockTable (char FrameID[])
242+{
243+ struct BlockTable *next;
244+
245+ next = block_pnt;
246+ while (next)
247+ {
248+ if (!memcmp (FrameID, next->BlockID, 2))
249+ {
250+ time (&next->recv_time);
251+ return TRUE;
252+ }
253+ next = next->f_chain;
254+ }
255+ return FALSE;
256+}
257+
258+void cleanBlockTable (void)
259+{
260+ struct BlockTable *next;
261+ struct BlockTable *last;
262+ struct BlockTable *temp;
263+ time_t a_time;
264+
265+ next = block_pnt;
266+ last = NULL;
267+ time (&a_time);
268+ while (next)
269+ {
270+
271+ if ((a_time - next->recv_time) > 5)
272+ {
273+ temp = next;
274+ if (last)
275+ {
276+ last->f_chain = next->f_chain;
277+ next = last->f_chain;
278+ }
279+ else
280+ {
281+ block_pnt = next->f_chain;
282+ next = block_pnt;
283+ }
284+ free (temp);
285+
286+ }
287+ else
288+ {
289+ last = next;
290+ next = next->f_chain;
291+ }
292+ }
293+}
294+
295+int check_block (char pkt[], int length)
296+{
297+ struct ModuleTable *pnt;
298+ if (length == 58)
299+ {
300+ pnt = module_check (&pkt[20]);
301+ if (!pnt) return FALSE;
302+ if (pnt->block_sw)
303+ {
304+ addBlockTable (&pkt[14]);
305+ return TRUE;
306+ }
307+ return FALSE;
308+ }
309+ else if (length == 29)
310+ {
311+ if (checkBlockTable (&pkt[14])) return TRUE;
312+ else return FALSE;
313+ }
314+ if ((length == 32) || (pkt[16] & 0x40))
315+ {
316+ if (checkBlockTable (&pkt[14]))
317+ {
318+ delBlockTable (&pkt[14]);
319+ return TRUE;
320+ }
321+ }
322+ return FALSE;
323+}
324+
325+void block_SetReset (char pkt[])
326+{
327+
328+ struct ModuleTable *pnt;
329+
330+ pnt = module_pnt;
331+
332+ while (pnt)
333+ {
334+ if (!memcmp (&pkt[12], pnt->ModuleName, 8))
335+ {
336+ if (pkt[10] == 0x01)
337+ {
338+ if (pkt[11] & 0x01) pnt->block_sw = TRUE;
339+ else pnt->block_sw = FALSE;
340+ }
341+ return;
342+ }
343+ pnt = pnt->f_chain;
344+ }
345+ return;
346+}
347+void gateway_init (void)
348+{
349+ unsigned char init_pkt[10];
350+
351+ memset (init_pkt, 0x00, 10);
352+ memcpy (init_pkt, "INIT", 4);
353+ init_pkt[5] = 0x01;
354+ init_pkt[6] = 's';
355+
356+ sendto (gw_out_sd, init_pkt, 10, 0,
357+ gw_out_info->ai_addr, gw_out_info->ai_addrlen);
358+}
359+
360+void forward_keep_alive (void)
361+{
362+ struct forward *fwd_pnt;
363+
364+ unsigned char keep_alive[10];
365+ time_t c_time;
366+
367+ memset (keep_alive, 0x00, 10);
368+ memcpy (keep_alive, "DSTR", 4);
369+ keep_alive[6] = 's';
370+ keep_alive[7] = 0x12;
371+
372+ time (&c_time);
373+ fwd_pnt = forward_pnt;
374+
375+ while (fwd_pnt)
376+ {
377+ keep_alive[4] = (fwd_pnt->send_seq >> 8) & 0xff;
378+ keep_alive[5] = fwd_pnt->send_seq & 0xff;
379+ sendto (fwd_pnt->fwd_sd, &keep_alive, 10, 0,
380+ fwd_pnt->fwd_info->ai_addr, fwd_pnt->fwd_info->ai_addrlen);
381+ if ((c_time - fwd_pnt->alive_time) > 20) fwd_pnt->alive = FALSE;
382+ else fwd_pnt->alive = TRUE;
383+ fwd_pnt = fwd_pnt->f_chain;
384+
385+ }
386+ time (&keep_alive_time);
387+}
388+
389+int gw_down_cnt;
390+
391+void putFifo (int len, unsigned char pkt[])
392+{
393+ struct FifoPkt *ret;
394+ time_t atime;
395+
396+ if (gw_Fifo_cnt > 20)
397+ {
398+ if (gw_down_cnt < 12)
399+ {
400+ time (&atime);
401+ fprintf (log_file, "%24.24s dsgwd down\n", ctime(&atime));
402+ fflush (log_file);
403+ gw_down_cnt++;
404+ }
405+ //sleep (5);
406+ return; /* dsgwd down */
407+ }
408+ if ((len == Wp->length) && (len != 26))
409+ {
410+ if (!memcmp (&pkt[6], &Wp->pkt[6], len-6)) return;
411+ }
412+ ret = malloc (sizeof(struct FifoPkt) - 1024 + len);
413+ if (ret == NULL)
414+ {
415+ fprintf (log_file, "memory error in gw\n");
416+ fflush (log_file);
417+ return;
418+ }
419+ ret->next = NULL;
420+ ret->length = len;
421+ memcpy (ret->pkt, pkt, len);
422+ Wp->next = ret;
423+ Wp = ret;
424+ gw_Fifo_cnt++;
425+}
426+
427+int getFifo (unsigned char pkt[])
428+{
429+ struct FifoPkt *tmp;
430+ int len;
431+
432+ if (Rp->next == NULL) return 0;
433+ tmp = Rp;
434+ Rp = Rp->next;
435+ len = Rp->length;
436+ memcpy (pkt, Rp->pkt, Rp->length);
437+ free (tmp);
438+ gw_Fifo_cnt--;
439+ return len;
440+}
441+
442+enum
443+{
444+ SEND_PACKET = 0,
445+ REPLY_WAIT,
446+ RESEND_PACKET
447+} gw_send = SEND_PACKET;
448+
449+unsigned char gw_pkt[1024];
450+
451+void gw_send_check (void)
452+{
453+ int length;
454+ time_t cur_time;
455+
456+ switch (gw_send)
457+ {
458+ case SEND_PACKET:
459+ length = getFifo (gw_pkt);
460+ if (length)
461+ {
462+ send_gw (gw_pkt);
463+ gw_send = REPLY_WAIT;
464+ gw_reply_recv = FALSE;
465+ time(&gw_send_time);
466+ }
467+ break;
468+
469+ case REPLY_WAIT:
470+ if (gw_reply_recv)
471+ {
472+ gw_reply_recv = FALSE;
473+ gw_send = SEND_PACKET;
474+ gw_down_cnt = 0;
475+ }
476+ else
477+ {
478+ time (&cur_time);
479+ if ((cur_time - gw_send_time) >= 1)
480+ {
481+ gw_send = RESEND_PACKET;
482+ }
483+ }
484+ break;
485+
486+ case RESEND_PACKET:
487+ send_gw (gw_pkt);
488+ gw_send = REPLY_WAIT;
489+ gw_reply_recv = FALSE;
490+ time(&gw_send_time);
491+ break;
492+
493+ default:
494+ gw_send = SEND_PACKET;
495+ break;
496+ }
497+
498+}
499+
500+int *parser_result(const char *buf, int size){
501+ static int ret[10];
502+ int i, j = 0, start = 0;
503+
504+ for(i=0; i<size; i++){
505+ char c = buf[i];
506+ if(c >= '0' && c <= '9'){
507+ if(!start){
508+ start = 1;
509+ ret[j] = c-'0';
510+ } else {
511+ ret[j] *= 10;
512+ ret[j] += c-'0';
513+ }
514+ } else if(c == '\n'){
515+ break;
516+ } else {
517+ if(start){
518+ j++;
519+ start = 0;
520+ }
521+ }
522+ }
523+
524+ return ret;
525+}
526+
527+char CpuUsage[10];
528+int prev_idle = 0, prev_total = 0;
529+void cpu_usage (void)
530+{
531+ int size, *nums, idle, total, i;
532+ char buf[356];
533+
534+ size = read(cpu_fd, buf, sizeof(buf));
535+ if(size <= 0) return;
536+
537+ nums = parser_result(buf, size);
538+ idle=nums[3];
539+ for(i=0, total=0; i<10; i++){
540+ total += nums[i];
541+ }
542+ int diff_idle = idle-prev_idle;
543+ int diff_total = total-prev_total;
544+ float usage = (float)(((float)(1000*(diff_total-diff_idle))/(float)diff_total+5)/(float)10);
545+ sprintf(CpuUsage, "%6.2f%%", usage);
546+
547+ prev_total = total;
548+ prev_idle = idle;
549+ lseek(cpu_fd, 0, SEEK_SET);
550+}
551+
552+int getOwnIp (void)
553+{
554+ int fd;
555+ struct ifreq ifr;
556+ int ret;
557+
558+ fd = socket (AF_INET, SOCK_DGRAM, 0);
559+ ifr.ifr_addr.sa_family = AF_INET;
560+ strncpy (ifr.ifr_name, NicDevice, IFNAMSIZ);
561+ ret = ioctl (fd, SIOCGIFADDR, &ifr);
562+ close (fd);
563+ time (&cur_time);
564+ if (ret == -1)
565+ {
566+ fprintf (log_file, "%24.24s ioctl error: %s\n", ctime(&cur_time), strerror(errno));
567+ fflush (log_file);
568+ return FALSE;
569+
570+ }
571+ OwnIP.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
572+ if (debug_sw)
573+ {
574+ fprintf(log_file, "%24.24s Own IP address %s\n", ctime(&cur_time),
575+ inet_ntoa (((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
576+ fflush (log_file);
577+ }
578+ if (upnp_auto)
579+ {
580+ if ((OwnIP.s_addr & 0xff) == 0x0a)
581+ {
582+ upnp_sw = TRUE;
583+ }
584+ else if (((OwnIP.s_addr & 0xff) == 0xc0) && (((OwnIP.s_addr >> 8) & 0xff) == 0xa8))
585+ {
586+ upnp_sw = TRUE;
587+ }
588+ else if ((OwnIP.s_addr & 0xff) == 0xac)
589+ {
590+ if ((((OwnIP.s_addr >> 8) & 0xff) >= 0x10) && (((OwnIP.s_addr >> 8) & 0xff) <= 0x1f)) upnp_sw = TRUE;
591+ }
592+ else
593+ {
594+ fprintf (log_file, "%24.24s NIC:%s had Global IP\n", ctime(&cur_time), NicDevice);
595+ fflush (log_file);
596+ upnp_sw = FALSE;
597+ return FALSE;
598+ }
599+ }
600+ return TRUE;
601+}
602+
603+void module_ctrl_set (char call[])
604+{
605+ char module_ctrl[8];
606+ struct ModuleTable *module_next;
607+
608+ module_next = module_pnt;
609+
610+ memset (module_ctrl, 0x20, 8);
611+ memcpy (module_ctrl, call, 7);
612+
613+ if (!module_check (module_ctrl))
614+ {
615+ module_next = malloc (sizeof (struct ModuleTable));
616+ module_init(module_next, module_ctrl);
617+ if (module_pnt) module_last->f_chain = module_next;
618+ else module_pnt = module_next;
619+ module_last = module_next;
620+ }
621+
622+ module_ctrl[7] = 'S';
623+ if (module_check (module_ctrl)) return;
624+
625+ module_next = malloc (sizeof (struct ModuleTable));
626+ module_init(module_next, module_ctrl);
627+ if (module_pnt) module_last->f_chain = module_next;
628+ else module_pnt = module_next;
629+ module_last = module_next;
630+}
631+
632+void put_zr_Fifo (int len, unsigned char pkt[])
633+{
634+ struct FifoPkt *ret;
635+ time_t atime;
636+
637+ if ((len == zr_Wp->length) && (pkt[7] != 0x21))
638+ {
639+ if (!memcmp (&pkt[6], &zr_Wp->pkt[6], len-6)) return;
640+ }
641+ ret = malloc (sizeof(struct FifoPkt) - 1024 + len);
642+ if (ret == NULL)
643+ {
644+ fprintf (log_file, "memory error in zr\n");
645+ fflush (log_file);
646+ return;
647+ }
648+ ret->next = NULL;
649+ ret->length = len;
650+ memcpy (ret->pkt, pkt, len);
651+ zr_Wp->next = ret;
652+ zr_Wp = ret;
653+}
654+
655+int get_zr_Fifo (unsigned char pkt[])
656+{
657+ struct FifoPkt *tmp;
658+ int len;
659+
660+ if (zr_Rp->next == NULL) return 0;
661+ tmp = zr_Rp;
662+ zr_Rp = zr_Rp->next;
663+ len = zr_Rp->length;
664+ memcpy (pkt, zr_Rp->pkt, zr_Rp->length);
665+ free (tmp);
666+ return len;
667+}
668+
669+enum
670+{
671+ ZR_SEND_PACKET = 0,
672+ ZR_REPLY_WAIT,
673+ ZR_RESEND_PACKET
674+} zr_send = ZR_SEND_PACKET;
675+
676+unsigned char zr_pkt[1024];
677+
678+void zr_send_check (void)
679+{
680+ int length;
681+ time_t cur_time;
682+
683+ switch (zr_send)
684+ {
685+ case ZR_SEND_PACKET:
686+ length = get_zr_Fifo (zr_pkt);
687+ if (length)
688+ {
689+ send_zr (zr_pkt);
690+ zr_send = ZR_REPLY_WAIT;
691+ zr_reply_recv = FALSE;
692+ time(&zr_send_time);
693+ }
694+ break;
695+
696+ case ZR_REPLY_WAIT:
697+ if (zr_reply_recv)
698+ {
699+ zr_reply_recv = FALSE;
700+ zr_send = ZR_SEND_PACKET;
701+ }
702+ else
703+ {
704+ time (&cur_time);
705+ if ((cur_time - zr_send_time) >= 1)
706+ {
707+ zr_send = ZR_RESEND_PACKET;
708+ }
709+ }
710+ break;
711+
712+ case ZR_RESEND_PACKET:
713+ send_zr (zr_pkt);
714+ zr_send = ZR_REPLY_WAIT;
715+ zr_reply_recv = FALSE;
716+ time(&zr_send_time);
717+ break;
718+
719+ default:
720+ zr_send = ZR_SEND_PACKET;
721+ break;
722+ }
723+
724+}
725+
旧リポジトリブラウザで表示