-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.c
executable file
·351 lines (300 loc) · 9.07 KB
/
utils.c
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (C) 2016 Intel Corporation
*
* Author: Todor Minchev <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, or (at your option) any later version, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/
#include "globals.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#if defined(_WIN32)
#include <winsock2.h>
#include <wspiapi.h>
#else
#include <netdb.h>
#include <sys/select.h>
#endif
#include "utils.h"
/* TODO - add error checks and return error codes*/
int redirect_sockets(int in_sock, int out_sock) {
int ready, received = 0;
fd_set read_fd_set ;
int size = BUFSIZ;
void *buf = calloc(1, size);
do {
FD_ZERO(&read_fd_set);
FD_SET(in_sock, &read_fd_set);
ready = select(in_sock+1, &read_fd_set, NULL, NULL, NULL);
} while (ready==-1 && errno == EINTR);
if (ready > 0 ){
if (FD_ISSET(in_sock, &read_fd_set)) {
while (size > 0) {
received = recv(in_sock, buf, size, 0);
if (received < 0 ){
ERR("Error : %s\n", strerror(errno));
} else if (received == 0) {
break ;
} else {
size -= received;
if (strstr((char*) buf, TURFF_EOM)) {
send_data(out_sock, (const void *) buf,received);
break;
} else {
send_data(out_sock, (const void *) buf,received);
}
}
}
}
} else if (ready < 0){
free(buf);
ERR("Select error\n");
}
free(buf);
return 0;
}
/* TODO - add error checks and return error codes*/
int send_data(int sock_fd, const void *buf, size_t size) {
int n, ready, sent = 0;
fd_set write_fd_set ;
struct timeval timeout;
timeout.tv_sec = (long) 0.3;
timeout.tv_usec = 0;
FD_ZERO(&write_fd_set);
FD_SET(sock_fd, &write_fd_set);
n = sock_fd +1;
ready = select(n, NULL, &write_fd_set, NULL, &timeout);
if (ready) {
while (size > 0) {
sent = send(sock_fd, buf, size, 0);
if (sent == -1){
ERR("Unable to send data. Error: [%d] %s", errno, strerror(errno));
}
else {
size -= sent;
#ifdef DBG
DEBUG("Bytes sent: %d. Bytes remaining: %zu\n", sent, size);
#endif
}
}
} else if (ready == -1){
ERR("Select timeout\n");
}
return 0;
}
/* TODO - add error checks and return error codes*/
int receive_data(int sock_fd, void *buf, size_t size, int is_ceed, int *done) {
int ready, received = 0;
fd_set read_fd_set ;
do {
FD_ZERO(&read_fd_set);
FD_SET(sock_fd, &read_fd_set);
ready = select(sock_fd+1, &read_fd_set, NULL, NULL, NULL);
} while (ready==-1 && errno == EINTR);
if (ready > 0 ){
if (FD_ISSET(sock_fd, &read_fd_set)) {
while (size > 0) {
received = recv(sock_fd, buf, size, 0);
if (received < 0 ){
ERR("Error : %s\n", strerror(errno));
} else if (received == 0) {
if (is_ceed)
*done = 1;
break ;
} else {
size -= received;
if (strstr((char*) buf, TURFF_EOM) && done != NULL ) {
memset(strstr((char*) buf, TURFF_EOM), 0, sizeof(TURFF_EOM));
printf("%s", (char*) buf);
*done = 1;
break;
} else if (is_ceed) {
printf("%s", (char*) buf);
}
buf = (char*)buf + received;
#ifdef DBG
DEBUG("\nBytes read: %d Bytes remaining: %zu\n", received, size);
#endif
}
}
}
} else if (ready < 0){
ERR("Select error : %s\n", strerror(errno));
}
return 0;
}
void send_chunk(int sock_fd, msg_chunk *chunk) {
msg_chunk *head;
head = chunk ;
while (chunk != NULL) {
send_data(sock_fd, chunk, sizeof(msg_chunk));
#ifdef DBG
DEBUG("Chunk op_code: %d , Chunk payload: %s\n", (*chunk).op_code,
(*chunk).arg);
#endif
chunk = chunk->next ;
}
/* done with this parameter - free all chunks*/
while (head != NULL) {
chunk = head;
head = chunk->next;
free(chunk);
}
}
int receive_chunk(int sock_fd, msg_chunk *chunk) {
return receive_data(sock_fd, chunk, sizeof(msg_chunk), 0, NULL);
}
void send_args(int sock_fd, char *params[]) {
int i, j, num_chunks;
msg_chunk *new_chunk, *tail_chunk, *head_chunk ;
/* send all params in chunks of 20 chars*/
for(i = 0; i < KEY_ARR_SZ; i++) {
if (params[i] != NULL) {
new_chunk = calloc(1, sizeof(msg_chunk));
head_chunk = new_chunk;
tail_chunk = new_chunk;
num_chunks = ceil((double) strlen(params[i]) /
(sizeof((*new_chunk).arg)-1));
for(j = 0; j < num_chunks; j++) {
(*tail_chunk).op_code = i;
if (j != 0) {
strncpy((*tail_chunk).arg, params[i]+(j*sizeof((*tail_chunk).arg)-j),
sizeof((*tail_chunk).arg)-1);
} else {
strncpy((*tail_chunk).arg, params[i], sizeof((*tail_chunk).arg)-1);
}
/* was this the last chunk*/
if ((j+1) != num_chunks) {
new_chunk = calloc(1, sizeof(msg_chunk));
tail_chunk->next = new_chunk;
tail_chunk = new_chunk;
}
}
/* this param is ready - send head chunk*/
send_chunk(sock_fd, head_chunk);
}
}
/* we are done with all params. Send EOM as an individual chunk*/
new_chunk = calloc(1, sizeof(msg_chunk));
(*new_chunk).op_code = -1;
strcpy((*new_chunk).arg, MSG_TERM);
send_chunk(sock_fd, new_chunk);
}
/* receive all params in chunks of 20 chars including terminator*/
void receive_args(int sock_fd, char *params[]) {
msg_chunk param_chunk;
char *tmp;
do {
memset(¶m_chunk, 0, sizeof(msg_chunk));
receive_chunk(sock_fd, ¶m_chunk);
#ifdef DBG
DEBUG("Chunk op_code: %d , Chunk payload: %s\n", param_chunk.op_code,
¶m_chunk.arg);
#endif
if (param_chunk.op_code != -1) {
if (params[param_chunk.op_code] == NULL) {
tmp = calloc(1, sizeof(param_chunk.arg));
} else {
tmp = realloc(params[param_chunk.op_code], strlen(params[param_chunk.op_code])+
sizeof(param_chunk.arg));
}
params[param_chunk.op_code] = tmp;
strcpy(params[param_chunk.op_code] + strlen(params[param_chunk.op_code]),
param_chunk.arg);
#ifdef DBG
DEBUG("Chunk op_code: %d , parameter: %s\n", param_chunk.op_code,
params[param_chunk.op_code]);
#endif
}
}while(param_chunk.op_code != -1); /* not EOM chunk*/
}
struct addrinfo* connect_to_socket(char *ip_in, char *port_in, int* sock_fd_out) {
int sock_fd, result;
struct addrinfo criteria, *srv_addr, *addr_p;
#if defined(_WIN32)
WSADATA wsa_data;
int res = WSAStartup(MAKEWORD(2, 2), &wsa_data);
if (res != 0) {
ERR("WSAStartup failed: %d\n", res);
}
#endif
/* connect to agent */
memset(&criteria, 0, sizeof(criteria));
criteria.ai_family = AF_INET;
criteria.ai_socktype = SOCK_STREAM;
criteria.ai_flags = 0;
criteria.ai_protocol = 0;
result = getaddrinfo(ip_in, port_in, &criteria,&srv_addr);
if(result != 0) {
ERR("Could not get address info list : %s\n", gai_strerror(result));
}
for(addr_p = srv_addr; addr_p != NULL; addr_p = addr_p->ai_next) {
*sock_fd_out = socket(addr_p->ai_family, addr_p->ai_socktype, addr_p->ai_protocol);
if (*sock_fd_out == -1)
continue;
if (connect(*sock_fd_out, addr_p->ai_addr, addr_p->ai_addrlen) != -1)
break; /* Connected */
close(*sock_fd_out);
}
return addr_p ;
}
struct addrinfo* bind_to_socket(char *ip_in, const char *port_in, int* sock_fd_out) {
struct addrinfo criteria, *srv_addr;
struct addrinfo *addr_p;
int result, opt = 1;
memset(&criteria, 0, sizeof(criteria));
criteria.ai_family = AF_INET;
criteria.ai_socktype = SOCK_STREAM;
criteria.ai_flags = AI_PASSIVE;
criteria.ai_protocol = 0;
criteria.ai_canonname = NULL;
criteria.ai_addr = NULL;
criteria.ai_next = NULL;
result = getaddrinfo(NULL, port_in, &criteria, &srv_addr);
if (result != 0) {
ERR("Could not get address info list\n");
}
for (addr_p = srv_addr; addr_p != NULL; addr_p = addr_p->ai_next) {
*sock_fd_out = socket(addr_p->ai_family, addr_p->ai_socktype, addr_p->ai_protocol);
if (*sock_fd_out == -1)
continue;
if (setsockopt(*sock_fd_out, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt)) < 0) {
close(*sock_fd_out);
ERR("setsockopt failed\n");
}
if (bind(*sock_fd_out, addr_p->ai_addr, addr_p->ai_addrlen) == 0)
break; /* Success */
close(*sock_fd_out);
}
freeaddrinfo(srv_addr);
return addr_p;
}
void copy_params(char *in[], char *out[]) {
int i;
for (i = 0; i< KEY_ARR_SZ; i++){
if (in[i] != NULL) {
asprintf(&out[i],"%s",in[i]);
}
}
}
void free_params(char *params[]) {
int i;
for (i = 0; i< KEY_ARR_SZ; i++){
if (params[i] != NULL) {
free(params[i]);
params[i] = NULL ;
}
}
}