FreeCalypso > hg > freecalypso-sw
annotate rvinterf/lowlevel/localsock.c @ 187:f5f8776f7c66
rvinterf: added -s option for changing the local socket pathname
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Sun, 24 Nov 2013 23:25:59 +0000 |
parents | ec040f521cc2 |
children | 549e6cd1e77d |
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 int listener; |
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 extern struct client *client_head; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
19 extern int max_fd; |
187
f5f8776f7c66
rvinterf: added -s option for changing the local socket pathname
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
185
diff
changeset
|
20 extern char *socket_pathname; |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
21 |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
22 create_listener_socket() |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 { |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 /* local socket binding voodoo copied from osmocon */ |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 struct sockaddr_un local; |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 unsigned int namelen; |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 int rc; |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
29 listener = socket(AF_UNIX, SOCK_STREAM, 0); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
30 if (listener < 0) { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
31 perror("socket(AF_UNIX, SOCK_STREAM, 0)"); |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 exit(1); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 } |
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 local.sun_family = AF_UNIX; |
187
f5f8776f7c66
rvinterf: added -s option for changing the local socket pathname
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
185
diff
changeset
|
36 strncpy(local.sun_path, socket_pathname, sizeof(local.sun_path)); |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 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
|
38 unlink(local.sun_path); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 /* 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
|
41 * calculating the proper length of the sockaddr */ |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 local.sun_len = strlen(local.sun_path); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 #endif |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 namelen = SUN_LEN(&local); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 #else |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 namelen = strlen(local.sun_path) + |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
49 offsetof(struct sockaddr_un, sun_path) + 1; |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 #endif |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
52 rc = bind(listener, (struct sockaddr *) &local, namelen); |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 if (rc != 0) { |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
54 perror("bind on local socket"); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
55 exit(1); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
56 } |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
57 rc = listen(listener, 3); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
58 if (rc != 0) { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
59 perror("listen"); |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 exit(1); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 } |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
63 if (listener > max_fd) |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
64 max_fd = listener; |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 return(0); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 } |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
68 static void |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
69 prep_for_length_rx(cli) |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
70 struct client *cli; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
71 { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
72 cli->rx_state = 0; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
73 cli->rx_ptr = cli->rx_buf; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
74 cli->rx_left = 2; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
75 } |
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 static void |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
78 prep_for_message_rx(cli) |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
79 struct client *cli; |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 { |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
81 cli->rx_state = 1; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
82 cli->rx_ptr = cli->rx_buf; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
83 cli->rx_left = cli->rx_msglen; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
84 } |
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 handle_listener_select() |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
87 { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
88 struct sockaddr_un un_addr; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
89 socklen_t len; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
90 int rc; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
91 struct client *newcli; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
92 |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
93 len = sizeof(un_addr); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
94 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
|
95 if (rc < 0) { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
96 perror("rvinterf: accept"); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
97 exit(1); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
98 } |
185
ec040f521cc2
rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
178
diff
changeset
|
99 if (rc > max_fd) |
ec040f521cc2
rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
178
diff
changeset
|
100 max_fd = rc; |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
101 newcli = malloc(sizeof(struct client)); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
102 if (!newcli) { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
103 perror("rvinterf: malloc for new client"); |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 exit(1); |
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 } |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
106 bzero(newcli, sizeof(struct client)); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
107 newcli->fd = rc; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
108 newcli->next = client_head; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
109 client_head = newcli; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
110 prep_for_length_rx(newcli); |
185
ec040f521cc2
rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
178
diff
changeset
|
111 output_line("*** Client program connected"); |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
112 return(0); |
171
4d8e4c58df71
rvtdump: implemented Tx extension hack, compiles
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
113 } |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
114 |
178
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
115 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
|
116 struct client *cli; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
117 char *msg; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
118 { |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
119 int len, len1; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
120 u_char hdr[3]; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
121 |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
122 len = strlen(msg); |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
123 len1 = len + 1; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
124 hdr[0] = len1 >> 8; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
125 hdr[1] = len1 & 0xFF; |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
126 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
|
127 write(cli->fd, hdr, 3); |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
128 write(cli->fd, msg, len); |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
129 } |
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
130 |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
131 void |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
132 handle_client_select(cli) |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
133 struct client *cli; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
134 { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
135 int cc; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
136 |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
137 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
|
138 if (cc <= 0) { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
139 /* normal client exit condition */ |
185
ec040f521cc2
rvinterf works after fixing max_fd
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
178
diff
changeset
|
140 output_line("*** Client program disconnected"); |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
141 close_socket: cli->rx_state = 2; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
142 return; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
143 } |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
144 cli->rx_ptr += cc; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
145 cli->rx_left -= cc; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
146 if (cli->rx_left) |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
147 return; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
148 /* got the thing, process it */ |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
149 if (cli->rx_state) { |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
150 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
|
151 process_msg_from_client(cli); |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
152 } else { |
178
7ab6b29e76bb
rvinterf: forwarding of Rx packets to clients implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
177
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 "-Invalid length, closing socket"); |
177
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
157 goto close_socket; |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
158 } |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
159 prep_for_message_rx(cli); |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
160 } |
fef035264dd4
rvinterf: beginning of local socket handling
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
173
diff
changeset
|
161 } |