forked from pokhrel/ProtocolDesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.c
142 lines (125 loc) · 3.11 KB
/
help.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
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
#include<stdlib.h>
#include<time.h>
#include"help.h"
/* Returns socket */
int custom_socket_remote(const char ip[],const char port[],const char localip[],struct sockaddr * addr){
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
if( getaddrinfo(ip,port, &hints, &result) != 0){
perror("getaddrinfo, ");
return -1;
}
for(rp = result;rp != NULL;rp = rp->ai_next){
if( (sfd = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol)) == -1){
perror("socket, ");
continue;
}
else{
memcpy(addr,rp->ai_addr,rp->ai_addrlen);
break;
}
}
/* None worked */
if(rp == NULL)
sfd = -1;
freeaddrinfo(result);
if(sfd != -1){
/* This part works for ipv4 only */
struct sockaddr_in laddr;
memset(&laddr,0,sizeof(struct sockaddr_in));
laddr.sin_family= AF_INET;
laddr.sin_port=0;
inet_pton(AF_INET,localip,(void*)&(laddr.sin_addr.s_addr));
if(bind(sfd,(struct sockaddr*)&laddr,sizeof(struct sockaddr_in))== -1){
perror("bind, ");
sfd = -1;
}
}
return sfd;
}
/* Returns socket. */
int custom_socket(int family,const char port[],const char localip[]){
struct addrinfo hints,*result,*rp;
int sfd;
memset(&hints,0,sizeof(struct addrinfo));
hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
if( getaddrinfo(localip,port,&hints,&result) != 0){
perror("getaddrinfo, ");
return -1;
}
for(rp = result;rp != NULL;rp = rp->ai_next){
if( (sfd = socket(rp->ai_family,rp->ai_socktype,rp->ai_protocol)) == -1){
perror("socket, ");
continue;
}
if(bind(sfd,rp->ai_addr,rp->ai_addrlen) == -1){
perror("bind, ");
close(sfd);
}
else
break;
}
/* None worked. */
if(rp == NULL)
sfd = -1;
freeaddrinfo(result);
return sfd;
}
// setting p == q means independent loss, as far as I understand
// otherwise loss is dependent of previous state
int getNextState(int previous, double p, double q)
{
if (p == 0.0 && q == 0.0)
return RECEIVED;
if (!(previous == LOST || previous == RECEIVED)) // invalid previous state
return -1;
if (p < 0 || p > 1 || q < 0 || q > 1) // invalid loss ratio
return -1;
srand(time(NULL));
if (previous == RECEIVED)
{
if (rand() < p * ((double)RAND_MAX + 1.0))
return LOST;
else
return RECEIVED;
}
else
{
if (rand() < q * ((double)RAND_MAX + 1.0))
return LOST;
else
return RECEIVED;
}
}
int setngetR(int fd){
int a = 200000;
socklen_t s;
s = sizeof(int);
if(setsockopt(fd,SOL_SOCKET,SO_RCVBUF,&a,s) == -1)
return -1;
if(getsockopt(fd,SOL_SOCKET,SO_RCVBUF,&a,&s) == -1)
return -1;
return a;
}
int setngetS(int fd){
int a = 200000;
socklen_t s;
s = sizeof(int);
if(setsockopt(fd,SOL_SOCKET,SO_SNDBUF,&a,s) == -1)
return -1;
if(getsockopt(fd,SOL_SOCKET,SO_SNDBUF,&a,&s) == -1)
return -1;
return a;
}