annotate pircharge/interf.c @ 352:bc5ddad215f2

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