-
Notifications
You must be signed in to change notification settings - Fork 2
/
synexec_comm.c
456 lines (415 loc) · 12.1 KB
/
synexec_comm.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* ------------------------------------
* synexec - Synchronised Executioner
* ------------------------------------
* synexec_comm.c
* ----------------
* Copyright 2014 (c) Citrix
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version only.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Read the README file for the changelog and information on how to
* compile and use this program.
*/
// Header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "synexec_comm.h"
#include "synexec_common.h"
#include "synexec_netops.h"
// Global variables
struct in_addr net_ifip; // Interface main IP address
struct in_addr net_ifbc; // Interface broadcast IP address
char * net_ifname; // Interface name
uint16_t net_port = 0; // Network port
extern uint32_t session;
extern int verbose;
/*
* int
* comm_init(uint16_t _net_port, char *_net_ifname, char force_bcast);
* -------------------------------------------------------------------
* This function initialises the global structures 'net_ifip', 'net_ifbc' and
* 'net_port'. To set the local IP address and the broadcast IP address, it
* gets the data related to interface '_net_ifname'. If '_net_ifname' is NULL,
* it uses the data related to the interface which the default route is
* assigned. If 'force_bcast' is set, the broadcast address is always forced
* to 255.255.255.255.
*
* Mandatory params: _net_port
* Optional params : _net_ifname, force_bcast
*
* Return values:
* -1 Error
* 0 Success
*/
int
comm_init(uint16_t _net_port, char *_net_ifname, char force_bcast){
// Local variables
int err = 0; // Return code
// Initialise memory structs
memset(&net_ifip, 0, sizeof(net_ifip));
memset(&net_ifbc, 0, sizeof(net_ifbc));
// Set interface name
if (_net_ifname == NULL){
if ((get_ifdef(&_net_ifname, NULL) != 0) || (!_net_ifname)){
fprintf(stderr, "Unable to find default route.\n");
goto err;
}
}
if ((net_ifname = strdup(_net_ifname)) == NULL){
perror("strdup");
fprintf(stderr, "Error duplicating interface name.\n");
goto err;
}
// Get other network configuration
if (get_ifipaddr(net_ifname, &net_ifip) != 0){
goto err;
}
if (force_bcast){
err = get_ifbroad("any", &net_ifbc);
}else{
err = get_ifbroad(net_ifname, &net_ifbc);
}
if (err){
goto err;
}
net_port = _net_port;
// Debug
if (verbose > 0){
printf("%s: net_ifname = '%s',", __FUNCTION__, net_ifname);
printf( " net_ifip = '%s',", inet_ntoa(net_ifip));
printf( " net_ifbc = '%s',", inet_ntoa(net_ifbc));
printf( " net_port = '%hu'\n", net_port);
fflush(stdout);
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* static int
* _comm_send(int sock, struct timeval *timeout, void *data, uint16_t datalen);
* ----------------------------------------------------------------------------
* This function sends 'datalen' bytes from the buffer contained in 'data' to
* the TCP socket 'sock'. If 'timeout' is specified, it will be used.
* Otherwise the system default is used.
*
* Mandatory params: sock, data, datalen
* Optional params : timeout
*
* Return values:
* -1 Error
* 0 Timeout
* n Bytes sent
*/
static int
_comm_send(int sock, struct timeval *timeout, void *data, uint16_t datalen){
// Local variables
fd_set fds; // Select fd_set
struct timeval fds_timeout; // Select timeout
int err = 0; // Return code
// Wait for socket to become ready for writing
FD_ZERO(&fds);
FD_SET(sock, &fds);
if (timeout){
err = select(sock+1, NULL, &fds, NULL, timeout);
}else{
fds_timeout.tv_sec = SYNEXEC_COMM_TIMEOUT_SEC;
fds_timeout.tv_usec = SYNEXEC_COMM_TIMEOUT_USEC;
err = select(sock+1, NULL, &fds, NULL, &fds_timeout);
}
if (err == 0){
if (verbose > 0){
fprintf(stdout, "%s: Select timed out to write on the socket.\n", __FUNCTION__);
fflush(stdout);
}
goto out;
}else
if (err != 1){
if (verbose > 0){
perror("select");
fprintf(stderr, "%s: Error selecting socket to write.\n", __FUNCTION__);
fflush(stderr);
}
goto err;
}
// Send the message
err = send(sock, data, datalen, 0);
if (err != datalen){
// Unable to send (whole?) message
if (verbose > 0){
perror("send");
fprintf(stderr, "%s: Send returned %d. Expected %hu.\n", __FUNCTION__, err, datalen);
fflush(stderr);
}
goto err;
}
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* comm_send(int sock, char command, struct timeval *timeout,
* void **data, uint16_t *datalen);
* ----------------------------------------------------------
* This function sends a TCP message to 'sock'. It creates the header net_msg
* based on 'command', 'data' and 'datalen'. If 'data'/'datalen' are NULL,
* only the header is sent (e.g. as in a PROBE).
* Upon return, 'timeout' (when specified) will contain the value set by
* select() (which can be undefined in case of error).
*
* Mandatory params: sock, command
* Optional params : timeout, data, datalen
*
* NOTES:
* If 'timeout' is not specified, the netops default will be used.
* If 'data' or 'datalen' are not specified, only the header is sent.
*
* Return values:
* -1 Error
* 0 Select timed out
* 1 Data sent
*/
int
comm_send(int sock, char command, struct timeval *timeout, void *data, uint16_t datalen){
// Local variables
synexec_msg_t net_msg; // synexec msg
ssize_t xfer_bytes; // Transfered bytes
int err = 0; // Return code
// Compose message
memset(&net_msg, 0, sizeof(net_msg));
net_msg.version = MT_SYNEXEC_VERSION;
net_msg.session = session;
net_msg.command = command;
net_msg.datalen = datalen;
net_msg_hton(&net_msg);
// Send the message
err = _comm_send(sock, timeout, &net_msg, sizeof(net_msg));
if (err <= 0){
goto err;
}
xfer_bytes = err;
if (!datalen){
goto out;
}
err = _comm_send(sock, timeout, data, datalen);
if (err <= 0){
goto err;
}
xfer_bytes += err;
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* static int
* _comm_recv(int sock, struct timeval *timeout, void *data, uint16_t datalen);
* ----------------------------------------------------------------------------
* This function reads data from the TCP socket 'sock'. It will read at most
* 'datalen' bytes from the socket, storing them in 'data'. If 'timeout' is
* specified, it will be used. Otherwise the system default is used.
*
* Mandatory params: sock, data, datalen
* Optional params : timeout
*
* Return values:
* -1 Error
* 0 Timeout
* n Bytes read
*/
static int
_comm_recv(int sock, struct timeval *timeout, void *data, uint16_t datalen){
// Local variables
fd_set fds; // Select fd_set
struct timeval fds_timeout; // Select timeout
ssize_t xfer_bytes = 0; // Transfered bytes
int i; // Temporary integer
int err = 0; // Return code
// Validate mandatory arguments
if ((sock < 0) || (!data) || (!datalen)){
goto err;
}
// Wait for the socket to become ready for reading
retry:
while(datalen){
FD_ZERO(&fds);
FD_SET(sock, &fds);
if (timeout){
err = select(sock+1, &fds, NULL, NULL, timeout);
}else{
fds_timeout.tv_sec = SYNEXEC_COMM_TIMEOUT_SEC;
fds_timeout.tv_usec = SYNEXEC_COMM_TIMEOUT_USEC;
err = select(sock+1, &fds, NULL, NULL, &fds_timeout);
}
if (err == 0){
// Select timed out
if (verbose > 1){
fprintf(stdout, "%s: Select timed out.\n", __FUNCTION__);
fflush(stdout);
}
break;
}else
if (err != 1){
// Select failed
if (errno == EINTR)
goto retry;
if (verbose > 0){
perror("select");
fprintf(stderr, "%s: Select error while reading from socket.\n", __FUNCTION__);
fflush(stderr);
}
goto err;
}
// Receive data
memset(data, 0, datalen);
i = read(sock, data+xfer_bytes, datalen);
if (verbose > 2){
fprintf(stdout, "%s: read(%d, %p, %hu) = %d\n", __FUNCTION__, sock, data, datalen, i);
fflush(stdout);
}
if (i == 0){
fprintf(stderr, "%s: Expected to read data but got none\n", __FUNCTION__);
fflush(stderr);
goto err;
} else if (i < 0){
perror("read");
fprintf(stderr, "%s: Read error while reading from socket.\n", __FUNCTION__);
fflush(stderr);
goto err;
}
xfer_bytes += i;
datalen -= i;
}
// Return how many bytes were read
err = xfer_bytes;
out:
// Return
return(err);
err:
err = -1;
goto out;
}
/*
* int
* comm_recv(int sock, synexec_msg_t *net_msg, struct timeval *timeout,
* void **data, uint16_t *datalen);
* --------------------------------------------------------------------
* This function reads a TCP message from 'sock'. It stores the header in
* 'net_msg' and any further data in the area pointed to by 'data'
* (setting 'datalen' accordingly).
* Upon return, 'timeout' (when specified) will contain the value set by
* select() (which can be undefined in case of error).
*
* Mandatory params: sock, net_msg
* Optional params : timeout, data, datalen
*
* NOTES:
* If 'timeout' is not specified, the netops default will be used.
* If 'data' is NULL and we receive net_msg->datalen != 0, this function will
* still read (and then discard) the read data. '*data', on the other hand,
* should be initially NULL and will be allocated if needed.
* '*data' is allocated with malloc() and should be free()d after use.
* If 'datalen' is not specified, it is simply not set.
*
* Return values:
* -1 Error
* 0 Select timed out
* 1 Data read
*/
int
comm_recv(int sock, synexec_msg_t *net_msg, struct timeval *timeout, void **data, uint16_t *datalen){
// Local variables
uint16_t xfer_bytes = 0; // Bytes actually transferred
char *_data = NULL; // Temporary data buffer
int err = 0; // Return code
// Validate mandatory arguments
if ((sock < 0) || (!net_msg)){
goto err;
}
// Receive header
err = _comm_recv(sock, timeout, (void *)net_msg, sizeof(*net_msg));
if (verbose > 2){
fprintf(stderr, "%s: _comm_recv(%d, %p, %p, %lu) = %d\n", __FUNCTION__, sock, timeout, net_msg, (unsigned long)sizeof(*net_msg), err);
fflush(stderr);
}
if (err < 0){
goto err;
}else
if (err == 0){
goto out;
}
// Validate header
net_msg_ntoh(net_msg);
if ((net_msg->version != MT_SYNEXEC_VERSION) ||
(net_msg->session != session)){
// Invalid reply
if (verbose > 0){
fprintf(stderr, "%s: Read header with invalid session id or version.\n", __FUNCTION__);
fflush(stderr);
}
goto err;
}
// Return if no further data to read
if (net_msg->datalen == 0){
goto out;
}
// Read net_msg->datalen more bytes
xfer_bytes = net_msg->datalen;
if (datalen){
*datalen = xfer_bytes;
}
if (data == NULL){
data = (void **)&_data;
}
if ((*data = calloc(1, xfer_bytes)) == NULL){
perror("calloc");
fprintf(stderr, "%s: Error allocating %d bytes for reading buffer.\n", __FUNCTION__, xfer_bytes);
fflush(stderr);
goto err;
}
err = _comm_recv(sock, timeout, *data, xfer_bytes);
if (err <= 0){
// We cannot afford to tolerate timeouts at this stage, so error out on 0 as well
goto err;
}
// At this point we must have read a header, so count it up
err += sizeof(*net_msg);
out:
// Return
return(err);
err:
err = -1;
if (data && *data){
free(*data);
*data = NULL;
}
goto out;
}