-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRewindClient.c
More file actions
371 lines (294 loc) · 9.66 KB
/
RewindClient.c
File metadata and controls
371 lines (294 loc) · 9.66 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
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
#include "RewindClient.h"
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#ifdef USE_OPENSSL
#include <openssl/sha.h>
#endif
#ifndef HEADER_SHA_H
#include "sha256.h"
#define SHA256_DIGEST_LENGTH SHA256_BLOCK_SIZE
#define SHA256(data, length, hash) \
{ \
SHA256_CTX context; \
sha256_init(&context); \
sha256_update(&context, data, length); \
sha256_final(&context, hash); \
}
#endif
#ifdef __linux__
#include <endian.h>
#include <byteswap.h>
#endif
#ifdef __MACH__
#include <mach/mach.h>
#include <machine/endian.h>
#define htobe16(value) OSSwapHostToBigInt16(value)
#define be16toh(value) OSSwapBigToHostInt16(value)
#define htobe32(value) OSSwapHostToBigInt32(value)
#define be32toh(value) OSSwapBigToHostInt32(value)
#define htole16(value) OSSwapHostToLittleInt16(value)
#define le16toh(value) OSSwapLittleToHostInt16(value)
#define htole32(value) OSSwapHostToLittleInt32(value)
#define le32toh(value) OSSwapLittleToHostInt32(value)
#define __bswap_16(value) OSSwapConstInt16(value)
#define __bswap_32(value) OSSwapConstInt32(value)
#endif
#define BUFFER_SIZE 256
#define ATTEMPT_COUNT 3
#define RECEIVE_TIMEOUT 2
#define CONNECT_TIMEOUT 5
static int CompareAddresses(struct sockaddr* value1, struct sockaddr_in6* value2)
{
struct sockaddr_in* value;
if ((value1->sa_family == AF_INET) &&
(value2->sin6_family == AF_INET6) &&
(IN6_IS_ADDR_V4MAPPED(&value2->sin6_addr)))
{
value = (struct sockaddr_in*)value1;
return
(value->sin_addr.s_addr - value2->sin6_addr.__in6_u.__u6_addr32[3]) |
(value->sin_port - value2->sin6_port);
}
if ((value1->sa_family == AF_INET6) &&
(value2->sin6_family == AF_INET6))
{
// Compare full socket address
return memcmp(value1, value2, sizeof(struct sockaddr_in6));
}
return -1;
}
struct RewindContext* CreateRewindContext(uint32_t number, const char* verion)
{
struct utsname name;
struct timeval interval;
struct sockaddr_in6 address;
struct RewindContext* context = (struct RewindContext*)calloc(1, sizeof(struct RewindContext));
if (context != NULL)
{
// Create socket
address.sin6_family = AF_INET6;
address.sin6_addr = in6addr_any;
address.sin6_port = 0;
address.sin6_scope_id = 0;
interval.tv_sec = RECEIVE_TIMEOUT;
interval.tv_usec = 0;
context->handle = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if ((context->handle < 0) ||
(bind(context->handle, (struct sockaddr*)&address, sizeof(struct sockaddr_in6)) < 0) ||
(setsockopt(context->handle, SOL_SOCKET, SO_RCVTIMEO, &interval, sizeof(struct timeval)) < 0))
{
close(context->handle);
free(context);
return NULL;
}
// Create supplementary data
uname(&name);
context->data = (struct RewindVersionData*)malloc(BUFFER_SIZE);
context->length = sizeof(struct RewindVersionData);
context->length += sprintf(
context->data->description,
"%s %s %s",
verion,
name.sysname,
name.machine);
context->data->number = htole32(number);
context->data->service = REWIND_SERVICE_SIMPLE_APPLICATION;
}
return context;
}
void ReleaseRewindContext(struct RewindContext* context)
{
if (context != NULL)
{
freeaddrinfo(context->address);
close(context->handle);
free(context->data);
free(context);
}
}
void TransmitRewindData(struct RewindContext* context, uint16_t type, uint16_t flag, void* data, size_t length)
{
struct msghdr message;
struct iovec vectors[2];
size_t index;
uint32_t number;
struct RewindData header;
memset(&header, 0, sizeof(struct RewindData));
memcpy(&header, REWIND_PROTOCOL_SIGN, REWIND_SIGN_LENGTH);
index = flag & REWIND_FLAG_REAL_TIME_1;
number = context->counters[index];
header.type = htole16(type);
header.flags = htole16(flag);
header.number = htole32(number);
header.length = htole16(length);
vectors[0].iov_base = &header;
vectors[0].iov_len = sizeof(struct RewindData);
vectors[1].iov_base = data;
vectors[1].iov_len = length;
message.msg_name = context->address->ai_addr;
message.msg_namelen = context->address->ai_addrlen;
message.msg_iov = vectors;
message.msg_iovlen = 2;
message.msg_control = NULL;
message.msg_controllen = 0;
message.msg_flags = 0;
sendmsg(context->handle, &message, 0);
context->counters[index] ++;
}
ssize_t ReceiveRewindData(struct RewindContext* context, struct RewindData* buffer, ssize_t length)
{
struct sockaddr_in6 address;
socklen_t size = sizeof(struct sockaddr_in6);
length = recvfrom(context->handle, buffer, length, 0, (struct sockaddr*)&address, &size);
if (length < 0)
return CLIENT_ERROR_SOCKET_IO;
if (CompareAddresses(context->address->ai_addr, &address) != 0)
return CLIENT_ERROR_WRONG_ADDRESS;
if ((length < sizeof(struct RewindData)) ||
(memcmp(buffer, REWIND_PROTOCOL_SIGN, REWIND_SIGN_LENGTH) != 0))
return CLIENT_ERROR_WRONG_DATA;
return length;
}
int ConnectRewindClient(struct RewindContext* context, const char* location, const char* port, const char* password, uint32_t options)
{
struct addrinfo hints;
struct RewindData* buffer = (struct RewindData*)alloca(BUFFER_SIZE);
ssize_t length;
size_t attempt = 0;
struct timeval now;
struct timeval threshold;
uint8_t* digest = (uint8_t*)alloca(SHA256_DIGEST_LENGTH);
struct RewindConfigurationData data;
// Resolve server IP address
if (context->address != NULL)
{
freeaddrinfo(context->address);
context->address = NULL;
}
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
#ifdef __linux__
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
#endif
#ifdef __MACH__
hints.ai_flags = AI_V4MAPPED;
hints.ai_family = AF_INET6;
#endif
if (getaddrinfo(location, port, &hints, &context->address) != 0)
return CLIENT_ERROR_DNS_RESOLVE;
// Do login procedure
gettimeofday(&now, NULL);
threshold.tv_sec = now.tv_sec + CONNECT_TIMEOUT;
threshold.tv_usec = now.tv_usec;
while (timercmp(&now, &threshold, <))
{
TransmitRewindData(context, REWIND_TYPE_KEEP_ALIVE, REWIND_FLAG_NONE, context->data, context->length);
length = ReceiveRewindData(context, buffer, BUFFER_SIZE);
gettimeofday(&now, NULL);
if ((length == CLIENT_ERROR_WRONG_ADDRESS) ||
(length == CLIENT_ERROR_SOCKET_IO) &&
((errno == EWOULDBLOCK) ||
(errno == EAGAIN)))
continue;
if (length < 0)
return length;
switch (le16toh(buffer->type))
{
case REWIND_TYPE_CHALLENGE:
if (attempt < ATTEMPT_COUNT)
{
length -= sizeof(struct RewindData);
length += sprintf(buffer->data + length, "%s", password);
SHA256(buffer->data, length, digest);
TransmitRewindData(context, REWIND_TYPE_AUTHENTICATION, REWIND_FLAG_NONE, digest, SHA256_DIGEST_LENGTH);
attempt ++;
continue;
}
return CLIENT_ERROR_WRONG_PASSWORD;
case REWIND_TYPE_KEEP_ALIVE:
if (options != 0)
{
data.options = htole32(options);
TransmitRewindData(context, REWIND_TYPE_CONFIGURATION, REWIND_FLAG_NONE, &data, sizeof(struct RewindConfigurationData));
continue;
}
case REWIND_TYPE_CONFIGURATION:
return CLIENT_ERROR_SUCCESS;
}
}
return CLIENT_ERROR_RESPONSE_TIMEOUT;
}
int WaitForRewindSessionEnd(struct RewindContext* context, struct RewindSessionPollData* request, time_t interval1, time_t interval2)
{
struct RewindData* buffer = (struct RewindData*)alloca(BUFFER_SIZE);
struct RewindSessionPollData* response = (struct RewindSessionPollData*)buffer->data;
ssize_t length;
uint32_t state = 0b00;
struct timeval now;
struct timeval threshold1;
struct timeval threshold2;
if (interval1 < RECEIVE_TIMEOUT)
interval1 = RECEIVE_TIMEOUT;
gettimeofday(&now, NULL);
threshold1.tv_sec = now.tv_sec + interval1 + interval2;
threshold1.tv_usec = now.tv_usec;
threshold2.tv_sec = 0;
threshold2.tv_usec = 0;
while (timercmp(&now, &threshold1, <))
{
TransmitRewindData(context, REWIND_TYPE_KEEP_ALIVE, REWIND_FLAG_NONE, context->data, context->length);
TransmitRewindData(context, REWIND_TYPE_SESSION_POLL, REWIND_FLAG_NONE, request, sizeof(struct RewindSessionPollData));
length = ReceiveRewindData(context, buffer, BUFFER_SIZE);
gettimeofday(&now, NULL);
if ((length == CLIENT_ERROR_WRONG_ADDRESS) ||
(length == CLIENT_ERROR_SOCKET_IO) &&
((errno == EWOULDBLOCK) ||
(errno == EAGAIN)))
continue;
if (length < 0)
return length;
switch (le16toh(buffer->type))
{
case REWIND_TYPE_KEEP_ALIVE:
state |= 0b01;
break;
case REWIND_TYPE_SESSION_POLL:
if ((response->state == 0) &&
(threshold2.tv_sec == 0))
{
threshold2.tv_sec = now.tv_sec + interval2;
threshold2.tv_usec = now.tv_usec;
}
if ((response->state != 0) &&
(threshold2.tv_sec != 0))
{
threshold2.tv_sec = 0;
threshold2.tv_usec = 0;
}
if ((threshold2.tv_sec != 0) &&
(timercmp(&now, &threshold2, >)))
{
// No active sessions during <interval2>
return CLIENT_ERROR_SUCCESS;
}
state |= 0b10;
break;
}
if (state == 0b11)
{
// Got REWIND_TYPE_KEEP_ALIVE and REWIND_TYPE_SESSION_POLL
// Wait for 2 seconds before the next attempt
sleep(RECEIVE_TIMEOUT);
state = 0b00;
}
}
return CLIENT_ERROR_RESPONSE_TIMEOUT;
}