-
Notifications
You must be signed in to change notification settings - Fork 2
/
synexec_slave_beacon.c
197 lines (178 loc) · 6.06 KB
/
synexec_slave_beacon.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
/*
* ------------------------------------
* synexec - Synchronised Executioner
* ------------------------------------
* synexec_slave_beacon.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 <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <pthread.h>
#include "synexec_common.h"
#include "synexec_netops.h"
#include "synexec_slave_beacon.h"
// Global variables
struct sockaddr_in master_addr;
pthread_mutex_t master_mutex;
pthread_cond_t master_cond;
extern struct in_addr net_ifip;
extern struct in_addr net_ifbc;
extern uint16_t net_port;
extern uint32_t session;
extern int verbose;
extern char quit;
/*
* void *
* beacon();
* ---------
* This thread creates an UDP socket and binds it to the predefined broadcast
* address. It listens on port 'net_port' for MT_SYNEXEC_MSG_PROBE and sets
* the global struct 'master_addr' with the address of the sender (correcting
* master_addr.sin_port to match net_port, facilitating further usage of the
* structure.
*
* Mandatory params:
* Optional params :
*
* NOTES:
* This function always return NULL, setting the global quit flag on error
* Return values:
* 0 This function always return NULL
*/
void *
beacon(){
// Local variables
int beacon_fd = -1; // Beacon UDP socket
struct sockaddr_in beacon_addr; // Beacon sockaddr
socklen_t beacon_slen = 0; // Beacon socket len
struct sockaddr_in sender_addr; // Sender sockaddr
synexec_msg_t net_msg; // Synexec msg
ssize_t net_len = 0; // Bytes read
fd_set fds; // Select fds
struct timeval fds_timeout; // Select timeout
int i; // Temporary integer
// Initialise structs
memset(&master_addr, 0, sizeof(master_addr));
memset(&beacon_addr, 0, sizeof(beacon_addr));
memset(&sender_addr, 0, sizeof(sender_addr));
memset(&net_msg, 0, sizeof(net_msg));
// Create UDP socket
if ((beacon_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket");
fprintf(stderr, "%s: Error creating beacon UDP socket.\n", __FUNCTION__);
goto err;
}
if ((i = fcntl(beacon_fd, F_GETFD)) < 0 ||
fcntl(beacon_fd, F_SETFD, i | FD_CLOEXEC)) {
fprintf(stderr, "%s: Could not set socket to be closed on exec\n", __FUNCTION__);
}
// Bind beacon UDP socket to net_ifbc
beacon_addr.sin_family = AF_INET;
memcpy(&beacon_addr.sin_addr, &net_ifbc, sizeof(beacon_addr.sin_addr));
beacon_addr.sin_port = htons(net_port);
if (bind(beacon_fd, (struct sockaddr *)&beacon_addr, sizeof(struct sockaddr)) < 0){
perror("bind");
fprintf(stderr, "%s: Error binding beacon UDP socket.\n", __FUNCTION__);
goto err;
}
// Listen to packets
while(!quit){
memset(&net_msg, 0, sizeof(net_msg));
beacon_slen = sizeof(struct sockaddr);
FD_ZERO(&fds);
FD_SET(beacon_fd, &fds);
fds_timeout.tv_sec = SYNEXEC_SLAVE_BEACON_LOOPTIMEO_SEC;
fds_timeout.tv_usec = 0;
i = select(beacon_fd+1, &fds, NULL, NULL, &fds_timeout);
if (i == 0){
continue;
}else
if (i == -1){
goto err;
}
// Receive and parse the packet
net_len = recvfrom(beacon_fd, &net_msg, sizeof(net_msg), 0, (struct sockaddr *)&sender_addr, &beacon_slen);
net_msg_ntoh(&net_msg);
if (verbose > 0){
printf("%s: UDP Received %ld bytes.\n", __FUNCTION__, (long)net_len);
fflush(stdout);
}
if (net_len < 0){
perror("recvfrom");
fprintf(stderr, "%s: Error reading from beacon UDP socket.\n", __FUNCTION__);
goto err;
}else
if (net_len != sizeof(net_msg)){
// Disregard packets that are not compliant
continue;
}else
if ((net_msg.version != MT_SYNEXEC_VERSION) ||
(net_msg.command != MT_SYNEXEC_MSG_PROBE) ||
(net_msg.session != session)){
// Disregard commands other than probe
continue;
}
if (verbose > 2){
printf("%s: Received probe from '%s'.\n", __FUNCTION__, inet_ntoa(sender_addr.sin_addr));
printf("%s: net_msg.version = %u\n", __FUNCTION__, net_msg.version);
printf("%s: net_msg.session = %u\n", __FUNCTION__, net_msg.session);
printf("%s: net_msg.command = %hhu\n", __FUNCTION__, net_msg.command);
printf("%s: net_msg.datalen = %hu\n", __FUNCTION__, net_msg.datalen);
if (verbose > 3){
printf("%s: Packet had %ld bytes: ", __FUNCTION__, (long)net_len);
for (i=0; i<net_len; i++){
printf("%02hhX ", *(((char *)&net_msg)+i));
}
printf("\n");
}
fflush(stdout);
}
// Set global master IP address and port, signalling the worker thread
pthread_mutex_lock(&master_mutex);
if (master_addr.sin_port == 0){
memcpy(&master_addr, &sender_addr, sizeof(master_addr));
master_addr.sin_port = htons(net_port);
pthread_cond_signal(&master_cond);
}
pthread_mutex_unlock(&master_mutex);
}
out:
if (beacon_fd >= 0){
close(beacon_fd);
}
// Return
return NULL;
err:
// Set global error condition
quit = 2;
// Unlock the worker thread
pthread_mutex_lock(&master_mutex);
memset(&master_addr, 0xFF, sizeof(master_addr));
pthread_cond_signal(&master_cond);
pthread_mutex_unlock(&master_mutex);
goto out;
}