forked from nvf-crucio/PROX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_conn.c
More file actions
executable file
·251 lines (204 loc) · 5.51 KB
/
input_conn.c
File metadata and controls
executable file
·251 lines (204 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright(c) 2010-2016 Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "input_conn.h"
#include "input.h"
#include "run.h"
#include "cmd_parser.h"
static struct input tcp_server;
int tcp_server_started;
static struct input uds_server;
int uds_server_started;
/* Active clients */
struct client_conn {
struct input input;
int enabled;
int n_buf;
char buf[32768];
};
struct client_conn clients[32];
static int start_listen_tcp(void)
{
struct sockaddr_in server;
int ret, sock;
int optval = 1;
memset(&server, 0, sizeof(server));
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1)
return -1;
server.sin_family = AF_INET;
server.sin_port = ntohs(8474);
server.sin_addr.s_addr = ntohl(INADDR_ANY);
ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
if (ret)
return -1;
if (bind(sock, (struct sockaddr *) &server, sizeof(server)) == -1)
return -1;
if (listen(sock, 1) == -1)
return -1;
return sock;
}
static int start_listen_uds(void)
{
int sock;
struct sockaddr_un server = {
.sun_path = "/tmp/prox.sock",
.sun_family = AF_UNIX
};
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1)
return -1;
/* Unlink can fail, i.e. when /tmp/prox.sock does not
exists. This is not fatal. */
unlink(server.sun_path);
if (bind(sock, (struct sockaddr *) &server, sizeof(server)) == -1)
return -1;
if (listen(sock, 1) == -1)
return -1;
return sock;
}
static void write_client(struct input *input, const char *buf, size_t len)
{
int ret;
while ((ret = write(input->fd, buf, len)) != (int)len) {
buf += ret;
len -= ret;
}
}
static void handle_client(struct input* client_input)
{
char cur[1024];
size_t i;
int ret;
struct client_conn *c = NULL;
/* Get the client structure that uses this input */
for (i = 0; i < sizeof(clients)/sizeof(clients[0]); ++i) {
if (&clients[i].input == client_input) {
c = &clients[i];
break;
}
}
/* handle_client function called non-tcp client */
if (c == NULL)
return ;
ret = read(c->input.fd, cur, sizeof(cur));
if (ret == 0) {
c->enabled = 0;
unreg_input(&c->input);
return ;
}
/* Scan in data until \n (\r skipped if followed by \n) */
for (int i = 0; i < ret; ++i) {
if (cur[i] == '\r' && i + 1 < ret && cur[i + 1] == '\n')
continue;
if (cur[i] == '\n') {
c->buf[c->n_buf] = 0;
if (c->n_buf)
cmd_parser_parse(c->buf, client_input);
c->n_buf = 0;
}
else if (c->n_buf + 1 < (int)sizeof(c->buf))
c->buf[c->n_buf++] = cur[i];
else
c->n_buf = 0;
}
}
static void handle_new_client(struct input* server)
{
size_t i;
int new_client = accept(server->fd, NULL, NULL);
for (i = 0; i < sizeof(clients)/sizeof(clients[0]); ++i) {
if (clients[i].enabled == 0) {
break;
}
}
if (i == sizeof(clients)/sizeof(clients[0])) {
close(new_client);
return ;
}
clients[i].enabled = 1;
clients[i].n_buf = 0;
clients[i].input.fd = new_client;
clients[i].input.reply = server->reply;
clients[i].input.proc_input = handle_client;
reg_input(&clients[i].input);
}
int reg_input_tcp(void)
{
int fd;
if (tcp_server_started)
return -1;
if ((fd = start_listen_tcp()) < 0)
return -1;
tcp_server.fd = fd;
tcp_server.proc_input = handle_new_client;
tcp_server.reply = write_client;
if (reg_input(&tcp_server) != 0) {
close(fd);
return -1;
}
tcp_server_started = 1;
return 0;
}
int reg_input_uds(void)
{
int fd;
if (uds_server_started)
return -1;
if ((fd = start_listen_uds()) < 0)
return -1;
uds_server.fd = fd;
uds_server.proc_input = handle_new_client;
uds_server.reply = write_client;
if (reg_input(&uds_server) != 0) {
close(fd);
return -1;
}
uds_server_started = 1;
return 0;
}
void unreg_input_tcp(void)
{
if (!tcp_server_started)
return;
tcp_server_started = 0;
close(tcp_server.fd);
unreg_input(&tcp_server);
}
void unreg_input_uds(void)
{
if (!uds_server_started)
return;
uds_server_started = 0;
close(tcp_server.fd);
unreg_input(&tcp_server);
}