-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelayServer.c
More file actions
172 lines (149 loc) · 4.64 KB
/
relayServer.c
File metadata and controls
172 lines (149 loc) · 4.64 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
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
/*
* Copyright (C) 2019 Weston Berg
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* RelayServer - A simple relay server with packet loss simulation
*/
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <time.h>
#include <unistd.h>
#define UPPER_THRESHOLD 100
#define LOWER_THRESHOLD 0
volatile sig_atomic_t sigint_rec = 0; /* Set when SIGINT signal recieved */
/*
* Handler for SIGINT signal
*/
void sigint_handler(int i)
{
sigint_rec = 1;
}
/*
* Prints out command line format
*/
static void usage(void)
{
printf("Usage: ./relayServer <srcIP> <srcPort> "
"<destIP> <destPort> <lossRate>\n");
}
/*
* Determine if the packet is lost
*/
static int packet_lost(int lossRate)
{
int randNum = (rand() % (UPPER_THRESHOLD - LOWER_THRESHOLD + 1)) + LOWER_THRESHOLD;
if(randNum < lossRate) {
return 1;
}
return 0;
}
/*
* Main loop
*/
int main(int argc, char *argv[])
{
char srcIP[16], destIP[16]; /* Src & Dest IP addresses */
unsigned short srcPort, destPort; /* Src & Dest port numbers */
int lossRate; /* Packet loss rate for transmission */
int sockfd; /* Socket file descriptor */
int opt = 1; /* setsockopt option value */
/* Structs containing address info */
struct sockaddr_in servAddr, srcAddr, destAddr;
socklen_t srcLen, destLen; /* Length of address structs */
ssize_t inMsgLen, outMsgLen; /* Result of recvfrom/sendto */
size_t bufSz = 2048; /* Size of packets */
char buffer[bufSz]; /* Buffer to write into/read out of */
struct sigaction sa; /* Configures sigaction call */
// Setup handler for sigation to catch SIGINT
sa.sa_handler = sigint_handler;
sigaction(SIGINT, &sa, NULL);
// Seed random number generator
srand(time(NULL));
// Retrieve command line args
if(argc == 6) {
strcpy(srcIP, argv[1]);
srcPort = atoi(argv[2]);
strcpy(destIP, argv[3]);
destPort = atoi(argv[4]);
lossRate = atoi(argv[5]);
} else {
usage();
exit(EXIT_FAILURE);
}
// Open socket
printf("Creating and configuring socket...\n");
if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket failure");
exit(EXIT_FAILURE);
}
// Configure socket for reuse
if((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) == -1) {
perror("setsockopt failure");
exit(EXIT_FAILURE);
}
// Init and populate address structs
printf("Initializing local and remote machine addresses...\n");
memset(&servAddr, 0, sizeof(servAddr));
memset(&srcAddr, 0, sizeof(srcAddr));
memset(&destAddr, 0, sizeof(destAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(srcPort);
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
srcAddr.sin_family = AF_INET;
srcAddr.sin_port = htons(srcPort);
srcAddr.sin_addr.s_addr = inet_addr(srcIP);
srcLen = sizeof(srcAddr);
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(destPort);
destAddr.sin_addr.s_addr = inet_addr(destIP);
destLen = sizeof(destAddr);
// Bind the socket
printf("Binding socket...\n");
if((bind(sockfd, (struct sockaddr *) &servAddr, sizeof(servAddr))) == -1) {
perror("bind failure");
exit(EXIT_FAILURE);
}
// Continuously recieve and forward data over socket
printf("Listening over socket...\n");
errno = 0;
while(!sigint_rec) {
// Recieve data from source
inMsgLen = recvfrom(sockfd, (char *) buffer, bufSz, MSG_WAITALL,
(struct sockaddr *) &srcAddr, &srcLen);
if(inMsgLen == -1) {
if(errno == EINTR) {
continue;
}
perror("recvfrom failure");
}
// Control loss rate
if(!packet_lost(lossRate)) {
// Forward recieved data to destination
outMsgLen = sendto(sockfd, (const char *) buffer, inMsgLen, MSG_CONFIRM,
(const struct sockaddr *) &destAddr, destLen);
if(outMsgLen == -1) {
if(errno == EINTR) {
continue;
}
perror("sendto failure");
}
}
}
close(sockfd);
printf("\nSocket closed\n");
return 0;
}