comparison pircharge/interf.c @ 228:fec90990f613

pirchgdbg started
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 19 Dec 2017 02:58:38 +0000
parents
children
comparison
equal deleted inserted replaced
227:bb86424f78e6 228:fec90990f613
1 /*
2 * This module implements the link to rvinterf.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <rvinterf/localsock.h>
10 #include <rvinterf/exitcodes.h>
11
12 extern int sock;
13
14 u_char rvi_msg[LOCALSOCK_MAX_MSG];
15 int rvi_msg_len;
16
17 static int rx_state, rx_left;
18 static u_char *rx_ptr;
19
20 void
21 localsock_prep_for_length_rx()
22 {
23 rx_state = 0;
24 rx_ptr = rvi_msg;
25 rx_left = 2;
26 }
27
28 static void
29 prep_for_message_rx()
30 {
31 rx_state = 1;
32 rx_ptr = rvi_msg;
33 rx_left = rvi_msg_len;
34 }
35
36 void
37 process_msg_from_rvinterf()
38 {
39 switch (rvi_msg[0]) {
40 case RVI2CLI_PKT_FROM_TARGET:
41 process_pkt_from_target();
42 return;
43 case RVI2CLI_LOCAL_CMD_RESP:
44 if (rvi_msg_len < 2)
45 goto bad;
46 if (rvi_msg[1] == '+')
47 return;
48 fprintf(stderr, "Error from rvinterf: %.*s\n", rvi_msg_len - 1,
49 rvi_msg + 1);
50 exit(ERROR_RVINTERF);
51 default:
52 bad:
53 fprintf(stderr,
54 "Error: unexpected message type %02X from rvinterf\n",
55 rvi_msg[0]);
56 exit(ERROR_RVINTERF);
57 }
58 }
59
60 void
61 handle_rvinterf_input()
62 {
63 int cc;
64
65 cc = read(sock, rx_ptr, rx_left);
66 if (cc <= 0) {
67 perror("read from rvinterf socket");
68 exit(ERROR_RVINTERF);
69 }
70 rx_ptr += cc;
71 rx_left -= cc;
72 if (rx_left)
73 return;
74 /* got the thing, process it */
75 if (rx_state) {
76 process_msg_from_rvinterf();
77 localsock_prep_for_length_rx();
78 } else {
79 rvi_msg_len = rvi_msg[0] << 8 | rvi_msg[1];
80 if (rvi_msg_len < 1 || rvi_msg_len > LOCALSOCK_MAX_MSG) {
81 fprintf(stderr,
82 "Invalid length from rvinterf: %02X%02X\n",
83 rvi_msg[0], rvi_msg[1]);
84 exit(ERROR_RVINTERF);
85 }
86 prep_for_message_rx();
87 }
88 }
89
90 void
91 send_pkt_to_target(pkt, pktlen)
92 u_char *pkt;
93 {
94 u_char hdrbuf[3];
95 int len1;
96
97 len1 = pktlen + 1;
98 hdrbuf[0] = len1 >> 8;
99 hdrbuf[1] = len1 & 0xFF;
100 hdrbuf[2] = CLI2RVI_PKT_TO_TARGET;
101 write(sock, hdrbuf, 3);
102 write(sock, pkt, pktlen);
103 }