-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_packet.c
More file actions
executable file
·49 lines (41 loc) · 1.2 KB
/
send_packet.c
File metadata and controls
executable file
·49 lines (41 loc) · 1.2 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <time.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include "send_packet.h"
extern double drand48();
/* The default loss probability is 10% */
static float loss_probability = 0.1f;
/* Set the loss probability from your command line at the start
* of the program. */
void set_loss_probability( float x )
{
loss_probability = x;
srand48( time(NULL) );
}
/* send_packet has exactly the same parameter set as the Linux sendto
* function. However, it drops some of the packets that are intended for
* sending randomly. */
ssize_t send_packet( int sock, const char* buffer, size_t size, int flags, const struct sockaddr* addr, socklen_t addrlen )
{
float rnd = drand48();
if( (buffer[0] & (0x4|0x8)) && /* We drop only data and ACK packets */
(rnd < loss_probability) )
{
fprintf(stderr, "Randomly dropping a packet\n");
return size;
}
return sendto( sock,
buffer,
size,
flags,
addr,
addrlen );
}