-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendto_dbg.c
More file actions
47 lines (42 loc) · 1.2 KB
/
sendto_dbg.c
File metadata and controls
47 lines (42 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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
void *memcpy(void *dest, const void *src, size_t n);
static int first_time = 1;
static int cutoff = 25; /* default is 25% loss */
void sendto_dbg_init(int percent)
{
/* percent is in integer form (i.e. 1 = 1%, 5 = 5%) */
cutoff = percent;
if( cutoff < 0 ) cutoff = 0;
else if( cutoff > 100 ) cutoff = 100;
printf("\n++++++++++ cutoff value is %d +++++++++\n", cutoff);
}
int sendto_dbg(int s, const char *buf, int len, int flags,
const struct sockaddr *to, int tolen )
{
int ret;
int decision;
struct sockaddr_in drop_addr;
if( first_time )
{
struct timeval t;
gettimeofday( &t, NULL );
srand( t.tv_sec );
printf("\n+++++++++\n seed is %d\n++++++++\n", (int)t.tv_sec);
first_time = 0;
}
decision = rand() % 100 + 1;
if( (cutoff > 0) && (decision <= cutoff ) ) {
/*printf("dropping pkt\n");*/
memcpy(&drop_addr, to, tolen);
drop_addr.sin_port = htons(ntohs(drop_addr.sin_port) + 1);
ret = sendto( s, buf, len, flags, (struct sockaddr *) &drop_addr, tolen );
return(ret);
}
ret = sendto( s, buf, len, flags, to, tolen );
return( ret );
}