-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_packet.c
38 lines (32 loc) · 851 Bytes
/
send_packet.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
#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 <sys/time.h>
#include <arpa/inet.h>
#include "send_packet.h"
static float loss_probability = 0.0f;
void set_loss_probability( float x )
{
loss_probability = x;
}
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[6] & 0x4) && /* Ignore termination packets. */
(rnd < loss_probability) )
{
fprintf(stderr, "Randomly dropping a packet -> ");
return size;
}
return sendto( sock,
buffer,
size,
flags,
addr,
addrlen );
}