annotate rvinterf/lowlevel/localsock.c @ 185:ec040f521cc2

rvinterf works after fixing max_fd
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 24 Nov 2013 20:27:21 +0000
parents 7ab6b29e76bb
children f5f8776f7c66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
2 * This rvinterf module handles the local UNIX domain socket interface
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/socket.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/un.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <stdio.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <string.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <strings.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <stdlib.h>
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <unistd.h>
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
13 #include "../localsock.h"
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
14 #include "client.h"
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
16 static char sockpath[] = "/tmp/rvinterf_socket";
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
18 int listener;
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
20 extern struct client *client_head;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
21 extern int max_fd;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
22
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
23 create_listener_socket()
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 {
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 /* local socket binding voodoo copied from osmocon */
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 struct sockaddr_un local;
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 unsigned int namelen;
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 int rc;
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
30 listener = socket(AF_UNIX, SOCK_STREAM, 0);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
31 if (listener < 0) {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
32 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 exit(1);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 }
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 local.sun_family = AF_UNIX;
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 strncpy(local.sun_path, sockpath, sizeof(local.sun_path));
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 unlink(local.sun_path);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 /* we use the same magic that X11 uses in Xtranssock.c for
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 * calculating the proper length of the sockaddr */
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 local.sun_len = strlen(local.sun_path);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 #endif
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 namelen = SUN_LEN(&local);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 #else
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 namelen = strlen(local.sun_path) +
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
50 offsetof(struct sockaddr_un, sun_path) + 1;
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 #endif
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
53 rc = bind(listener, (struct sockaddr *) &local, namelen);
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 if (rc != 0) {
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
55 perror("bind on local socket");
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
56 exit(1);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
57 }
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
58 rc = listen(listener, 3);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
59 if (rc != 0) {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
60 perror("listen");
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 exit(1);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 }
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
64 if (listener > max_fd)
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
65 max_fd = listener;
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 return(0);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 }
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
69 static void
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
70 prep_for_length_rx(cli)
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
71 struct client *cli;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
72 {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
73 cli->rx_state = 0;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
74 cli->rx_ptr = cli->rx_buf;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
75 cli->rx_left = 2;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
76 }
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
77
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
78 static void
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
79 prep_for_message_rx(cli)
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
80 struct client *cli;
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 {
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
82 cli->rx_state = 1;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
83 cli->rx_ptr = cli->rx_buf;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
84 cli->rx_left = cli->rx_msglen;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
85 }
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
86
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
87 handle_listener_select()
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
88 {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
89 struct sockaddr_un un_addr;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
90 socklen_t len;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
91 int rc;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
92 struct client *newcli;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
93
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
94 len = sizeof(un_addr);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
95 rc = accept(listener, (struct sockaddr *) &un_addr, &len);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
96 if (rc < 0) {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
97 perror("rvinterf: accept");
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
98 exit(1);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
99 }
185
ec040f521cc2 rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 178
diff changeset
100 if (rc > max_fd)
ec040f521cc2 rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 178
diff changeset
101 max_fd = rc;
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
102 newcli = malloc(sizeof(struct client));
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
103 if (!newcli) {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
104 perror("rvinterf: malloc for new client");
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 exit(1);
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 }
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
107 bzero(newcli, sizeof(struct client));
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
108 newcli->fd = rc;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
109 newcli->next = client_head;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
110 client_head = newcli;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
111 prep_for_length_rx(newcli);
185
ec040f521cc2 rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 178
diff changeset
112 output_line("*** Client program connected");
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
113 return(0);
171
4d8e4c58df71 rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 }
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
115
178
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
116 send_local_msg_to_client(cli, msg)
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
117 struct client *cli;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
118 char *msg;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
119 {
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
120 int len, len1;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
121 u_char hdr[3];
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
122
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
123 len = strlen(msg);
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
124 len1 = len + 1;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
125 hdr[0] = len1 >> 8;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
126 hdr[1] = len1 & 0xFF;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
127 hdr[2] = RVI2CLI_LOCAL_CMD_RESP;
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
128 write(cli->fd, hdr, 3);
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
129 write(cli->fd, msg, len);
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
130 }
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
131
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
132 void
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
133 handle_client_select(cli)
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
134 struct client *cli;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
135 {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
136 int cc;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
137
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
138 cc = read(cli->fd, cli->rx_ptr, cli->rx_left);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
139 if (cc <= 0) {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
140 /* normal client exit condition */
185
ec040f521cc2 rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 178
diff changeset
141 output_line("*** Client program disconnected");
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
142 close_socket: cli->rx_state = 2;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
143 return;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
144 }
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
145 cli->rx_ptr += cc;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
146 cli->rx_left -= cc;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
147 if (cli->rx_left)
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
148 return;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
149 /* got the thing, process it */
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
150 if (cli->rx_state) {
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
151 prep_for_length_rx(cli);
178
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
152 process_msg_from_client(cli);
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
153 } else {
178
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
154 cli->rx_msglen = cli->rx_buf[0] << 8 | cli->rx_buf[1];
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
155 if (cli->rx_msglen < 1 || cli->rx_msglen > LOCALSOCK_MAX_MSG) {
178
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
156 send_local_msg_to_client(cli,
7ab6b29e76bb rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 177
diff changeset
157 "-Invalid length, closing socket");
177
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
158 goto close_socket;
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
159 }
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
160 prep_for_message_rx(cli);
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
161 }
fef035264dd4 rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 173
diff changeset
162 }