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