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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
/*
* mini-slamd
* (c) 2002 Eric Biederman
*/
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
/*
* To specify the default interface for multicast packets use:
* route add -net 224.0.0.0 netmask 240.0.0.0 dev eth1
* This server is stupid and does not override the default.
*/
/* Sever states.
*
* Waiting for clients.
* Sending data to clients.
* Pinging clients for data.
*
*/
#define SLAM_PORT 10000
#define SLAM_MULTICAST_IP ((239<<24)|(255<<16)|(1<<8)|(1<<0))
#define SLAM_MULTICAST_PORT 10000
#define SLAM_MULTICAST_TTL 1
#define SLAM_MULTICAST_LOOPBACK 1
#define SLAM_MAX_CLIENTS 10
#define SLAM_PING_TIMEOUT 100 /* ms */
/*** Packets Formats ***
* Data Packet:
* transaction
* total bytes
* block size
* packet #
* data
*
* Status Request Packet
* transaction
* total bytes
* block packets
*
* Status Packet
* received packets
* requested packets
* received packets
* requested packets
* ...
* received packets
* requested packtes
* 0
*/
#define MAX_HDR (7 + 7 + 7) /* transaction, total size, block size */
#define MIN_HDR (1 + 1 + 1) /* transaction, total size, block size */
#define MAX_DATA_HDR (MAX_HDR + 7) /* header, packet # */
#define MIN_DATA_HDR (MAX_HDR + 1) /* header, packet # */
/* ETH_MAX_MTU 1500 - sizeof(iphdr) 20 - sizeof(udphdr) 8 = 1472 */
#define SLAM_MAX_NACK (1500 - (20 + 8))
/* ETH_MAX_MTU 1500 - sizeof(iphdr) 20 - sizeof(udphdr) 8 - MAX_HDR = 1451 */
#define SLAM_BLOCK_SIZE (1500 - (20 + 8 + MAX_HDR))
/* Define how many debug messages you want
* 1 - sparse but useful
* 2 - everything
*/
#ifndef DEBUG
#define DEBUG 0
#endif
static int slam_encode(
unsigned char **ptr, unsigned char *end, unsigned long value)
{
unsigned char *data = *ptr;
int bytes;
bytes = sizeof(value);
while ((bytes > 0) && ((0xff & (value >> ((bytes -1)<<3))) == 0)) {
bytes--;
}
if (bytes <= 0) {
bytes = 1;
}
if (data + bytes >= end) {
return -1;
}
if ((0xe0 & (value >> ((bytes -1)<<3))) == 0) {
/* packed together */
*data = (bytes << 5) | (value >> ((bytes -1)<<3));
} else {
bytes++;
*data = (bytes << 5);
}
bytes--;
data++;
while(bytes) {
*(data++) = 0xff & (value >> ((bytes -1)<<3));
bytes--;
}
*ptr = data;
return 0;
}
static unsigned long slam_decode(unsigned char **ptr, unsigned char *end, int *err)
{
unsigned long value;
unsigned bytes;
if (*ptr >= end) {
*err = -1;
}
bytes = ((**ptr) >> 5) & 7;
if ((bytes == 0) || (bytes > sizeof(unsigned long))) {
*err = -1;
return 0;
}
if ((*ptr) + bytes >= end) {
*err = -1;
}
value = (**ptr) & 0x1f;
bytes--;
(*ptr)++;
while(bytes) {
value <<= 8;
value |= **ptr;
(*ptr)++;
bytes--;
}
return value;
}
static struct sockaddr_in client[SLAM_MAX_CLIENTS];
static int clients;
void del_client(struct sockaddr_in *old)
{
int i;
for(i = 0; i < clients; i++) {
if ((client[i].sin_family == old->sin_family) &&
(client[i].sin_addr.s_addr == old->sin_addr.s_addr) &&
(client[i].sin_port == old->sin_port)) {
memmove(&client[i], &client[i+1],
(clients - (i+1))*sizeof(client[0]));
clients--;
}
}
}
void add_client(struct sockaddr_in *new)
{
del_client(new);
if (clients >= SLAM_MAX_CLIENTS)
return;
memcpy(&client[clients], new, sizeof(*new));
clients++;
}
void push_client(struct sockaddr_in *new)
{
del_client(new);
if (clients >= SLAM_MAX_CLIENTS) {
clients--;
}
memmove(&client[1], &client[0], clients*sizeof(*new));
memcpy(&client[0], new, sizeof(*new));
clients++;
}
void next_client(struct sockaddr_in *next)
{
/* Find the next client we want to ping next */
if (!clients) {
next->sin_family = AF_UNSPEC;
return;
}
/* Return the first client */
memcpy(next, &client[0], sizeof(*next));
}
int main(int argc, char **argv)
{
char *filename;
uint8_t nack_packet[SLAM_MAX_NACK];
int nack_len;
uint8_t request_packet[MAX_HDR];
int request_len;
uint8_t data_packet[MAX_DATA_HDR + SLAM_BLOCK_SIZE];
int data_len;
uint8_t *ptr, *end;
struct sockaddr_in master_client;
struct sockaddr_in sa_src;
struct sockaddr_in sa_mcast;
uint8_t mcast_ttl;
uint8_t mcast_loop;
int sockfd, filefd;
int result;
struct pollfd fds[1];
int state;
#define STATE_PINGING 1
#define STATE_WAITING 2
#define STATE_RECEIVING 3
#define STATE_TRANSMITTING 4
off_t size;
struct stat st;
uint64_t transaction;
unsigned long packet;
unsigned long packet_count;
unsigned slam_port, slam_multicast_port;
struct in_addr slam_multicast_ip;
slam_port = SLAM_PORT;
slam_multicast_port = SLAM_MULTICAST_PORT;
slam_multicast_ip.s_addr = htonl(SLAM_MULTICAST_IP);
if (argc != 2) {
fprintf(stderr, "Bad argument count\n");
fprintf(stderr, "Usage: mini-slamd filename\n");
exit(EXIT_FAILURE);
}
filename = argv[1];
filefd = -1;
size = 0;
transaction = 0;
/* Setup the udp socket */
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
fprintf(stderr, "Cannot create socket\n");
exit(EXIT_FAILURE);
}
memset(&sa_src, 0, sizeof(sa_src));
sa_src.sin_family = AF_INET;
sa_src.sin_port = htons(slam_port);
sa_src.sin_addr.s_addr = INADDR_ANY;
result = bind(sockfd, &sa_src, sizeof(sa_src));
if (result < 0) {
fprintf(stderr, "Cannot bind socket to port %d\n",
ntohs(sa_src.sin_port));
exit(EXIT_FAILURE);
}
/* Setup the multicast transmission address */
memset(&sa_mcast, 0, sizeof(sa_mcast));
sa_mcast.sin_family = AF_INET;
sa_mcast.sin_port = htons(slam_multicast_port);
sa_mcast.sin_addr.s_addr = slam_multicast_ip.s_addr;
if (!IN_MULTICAST(ntohl(sa_mcast.sin_addr.s_addr))) {
fprintf(stderr, "Not a multicast ip\n");
exit(EXIT_FAILURE);
}
/* Set the multicast ttl */
mcast_ttl = SLAM_MULTICAST_TTL;
setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL,
&mcast_ttl, sizeof(mcast_ttl));
/* Set the multicast loopback status */
mcast_loop = SLAM_MULTICAST_LOOPBACK;
setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &mcast_loop, sizeof(mcast_loop));
state = STATE_WAITING;
packet = 0;
packet_count = 0;
fds[0].fd = sockfd;
fds[0].events = POLLIN;
fds[0].revents = 0;
for(;;) {
switch(state) {
case STATE_PINGING:
state = STATE_WAITING;
next_client(&master_client);
if (master_client.sin_family == AF_UNSPEC) {
break;
}
#if DEBUG
printf("Pinging %s:%d\n",
inet_ntoa(master_client.sin_addr),
ntohs(master_client.sin_port));
fflush(stdout);
#endif
/* Prepare the request packet, it is all header */
ptr = request_packet;
end = &request_packet[sizeof(request_packet) -1];
slam_encode(&ptr, end, transaction);
slam_encode(&ptr, end, size);
slam_encode(&ptr, end, SLAM_BLOCK_SIZE);
request_len = ptr - request_packet;
result = sendto(sockfd, request_packet, request_len, 0,
&master_client, sizeof(master_client));
/* Forget the client I just asked, when the reply
* comes in we will remember it again.
*/
del_client(&master_client);
break;
case STATE_WAITING:
{
int timeout;
int from_len;
timeout = -1;
if (master_client.sin_family != AF_UNSPEC) {
timeout = SLAM_PING_TIMEOUT;
}
result = poll(fds, sizeof(fds)/sizeof(fds[0]), timeout);
if (result == 0) {
/* On a timeout try the next client */
state = STATE_PINGING;
break;
}
if (result > 0) {
from_len = sizeof(master_client);
result = recvfrom(sockfd,
nack_packet, sizeof(nack_packet), 0,
&master_client, &from_len);
if (result < 0)
break;
nack_len = result;
#if DEBUG
printf("Received Nack from %s:%d\n",
inet_ntoa(master_client.sin_addr),
ntohs(master_client.sin_port));
fflush(stdout);
#endif
#if DEBUG
{
ptr = nack_packet;
end = ptr + result;
packet = 0;
result = 0;
while(ptr < end) {
packet += slam_decode(&ptr, end, &result);
if (result < 0) break;
packet_count = slam_decode(&ptr, end, &result);
if (result < 0) break;
printf("%d-%d ",
packet, packet + packet_count -1);
}
printf("\n");
fflush(stdout);
}
#endif
/* Forget this client temporarily.
* If the packet appears good they will be
* readded.
*/
del_client(&master_client);
ptr = nack_packet;
end = ptr + nack_len;
result = 0;
packet = slam_decode(&ptr, end, &result);
if (result < 0)
break;
packet_count = slam_decode(&ptr, end, &result);
if (result < 0)
break;
/* We appear to have a good packet, keep
* this client.
*/
push_client(&master_client);
/* Reopen the file to transmit */
if (filefd != -1) {
close(filefd);
}
filefd = open(filename, O_RDONLY);
if (filefd < 0) {
fprintf(stderr, "Cannot open %s: %s\n",
filename, strerror(errno));
break;
}
size = lseek(filefd, 0, SEEK_END);
if (size < 0) {
fprintf(stderr, "Seek failed on %s: %s\n",
filename, strerror(errno));
break;
}
result = fstat(filefd, &st);
if (result < 0) {
fprintf(stderr, "Stat failed on %s: %s\n",
filename, strerror(errno));
break;
}
transaction = st.st_mtime;
state = STATE_TRANSMITTING;
break;
}
break;
}
case STATE_RECEIVING:
/* Now clear the queue of received packets */
{
struct sockaddr_in from;
int from_len;
uint8_t dummy_packet[SLAM_MAX_NACK];
state = STATE_TRANSMITTING;
result = poll(fds, sizeof(fds)/sizeof(fds[0]), 0);
if (result < 1)
break;
from_len = sizeof(from);
result = recvfrom(sockfd,
dummy_packet, sizeof(dummy_packet), 0,
&from, &from_len);
if (result <= 0)
break;
#if DEBUG
printf("Received Nack from %s:%d\n",
inet_ntoa(from.sin_addr),
ntohs(from.sin_port));
fflush(stdout);
#endif
/* Receive packets until I don't get any more */
state = STATE_RECEIVING;
/* Process a packet */
if (dummy_packet[0] == '\0') {
/* If the first byte is null it is a disconnect
* packet.
*/
del_client(&from);
}
else {
/* Otherwise attempt to add the client. */
add_client(&from);
}
break;
}
case STATE_TRANSMITTING:
{
off_t off;
off_t offset;
ssize_t bytes;
uint8_t *ptr2, *end2;
/* After I transmit a packet check for packets to receive. */
state = STATE_RECEIVING;
/* Find the packet to transmit */
offset = packet * SLAM_BLOCK_SIZE;
/* Seek to the desired packet */
off = lseek(filefd, offset, SEEK_SET);
if ((off < 0) || (off != offset)) {
fprintf(stderr, "Seek failed on %s:%s\n",
filename, strerror(errno));
break;
}
/* Encode the packet header */
ptr2 = data_packet;
end2 = data_packet + sizeof(data_packet);
slam_encode(&ptr2, end2, transaction);
slam_encode(&ptr2, end2, size);
slam_encode(&ptr2, end2, SLAM_BLOCK_SIZE);
slam_encode(&ptr2, end2, packet);
data_len = ptr2 - data_packet;
/* Read in the data */
bytes = read(filefd, &data_packet[data_len],
SLAM_BLOCK_SIZE);
if (bytes <= 0) {
fprintf(stderr, "Read failed on %s:%s\n",
filename, strerror(errno));
break;
}
data_len += bytes;
/* Write out the data */
result = sendto(sockfd, data_packet, data_len, 0,
&sa_mcast, sizeof(sa_mcast));
if (result != data_len) {
fprintf(stderr, "Send failed %s\n",
strerror(errno));
break;
}
#if DEBUG > 1
printf("Transmitted: %d\n", packet);
fflush(stdout);
#endif
/* Compute the next packet */
packet++;
packet_count--;
if (packet_count == 0) {
packet += slam_decode(&ptr, end, &result);
if (result >= 0)
packet_count = slam_decode(&ptr, end, &result);
if (result < 0) {
/* When a transmission is done close the file,
* so it may be updated. And then ping then start
* pinging clients to get the transmission started
* again.
*/
state = STATE_PINGING;
close(filefd);
filefd = -1;
break;
}
}
break;
}
}
}
return EXIT_SUCCESS;
}
|