-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
executable file
·234 lines (193 loc) · 6.39 KB
/
server.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdio.h>
#if WIN32
#include <ws2tcpip.h>
#include <winsock.h>
#pragma comment(lib,"ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#endif
#include <stdlib.h>
#include "dieWithError.h"
#define IFRAME (128)
#define HEADER_LEN (1)
#define PACKET_COUNTER_LEN (1)
#define CRC_LEN (1)
#define FOOTER_LEN ((PACKET_COUNTER_LEN) + (CRC_LEN))
#define OVERHEAD_LEN ((HEADER_LEN) + (FOOTER_LEN))
#define MESSAGE_LEN ((IFRAME) - (OVERHEAD_LEN))
#define RESPONSE_LEN (1)
#define MAX_PACKET_COUNT (64)
#define ERR_REQ (65)
typedef struct {
unsigned int packet_counter : 12;
unsigned int crc : 4;
} packet_footer;
typedef struct {
unsigned int c0 : 1;
unsigned int c1 : 1;
unsigned int c2 : 1;
unsigned int c3 : 1; // MSB
} crc_st;
char verifyCRC(char *frame, const unsigned int totalBytes);
int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in receiverAddr;
struct sockaddr_in senderAddr;
char frame[IFRAME];
unsigned char responseFrame[RESPONSE_LEN];
const char header[] = {0x7E};
unsigned short serverPort;
unsigned int senderAddrLen;
struct timeval timeout;
int recvMsgSize;
char crcRemainder;
unsigned int currentPacketCount;
unsigned int prevPacketCount;
int enable = 1;
int first = 1;
int transmissionSuccessful = 1;
int i;
FILE *fp;
timeout.tv_usec = 0;
timeout.tv_sec = 5;
if (argc != 2)
{
fprintf(stderr,"Usage: %s <UDP SERVER PORT>\n", argv[0]);
exit(1);
}
serverPort = atoi(argv[1]);
#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
fprintf(stderr, "WSAStartup() failed");
exit(1);
}
// Windows equivalent fopen here
#else
fp = fopen("received_file", "wb");
#endif
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Set recvfrom timeout */
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
DieWithError("setsockopt() timeout failed\n");
/* allow socket reuse */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
DieWithError("setsockopt() reuseaddr failed\n");
/* construct local address structure */
memset(&receiverAddr, 0, sizeof(receiverAddr));
receiverAddr.sin_family = AF_INET; /* address family : IPv4 */
receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
receiverAddr.sin_port = htons(serverPort);
senderAddrLen = sizeof(senderAddr);
if (bind(sock, (struct sockaddr *) &receiverAddr, sizeof(receiverAddr)) < 0) {
printf("bind error: %s\n", strerror(errno));
DieWithError("bind() failed");
}
printf("Server listening on port %u\n", serverPort);
for (;;)
{
memset(frame, 0, sizeof(frame));
memset(responseFrame, 0, sizeof(responseFrame));
transmissionSuccessful = 1;
/* wait to receive a packet */
recvMsgSize = recvfrom(sock, frame, IFRAME, 0, (struct sockaddr *) &senderAddr, &senderAddrLen);
if (recvMsgSize < OVERHEAD_LEN) {
if (errno == EWOULDBLOCK) {
printf("File transmission stopped. Quitting...\n");
break;
}
printf("recvfrom() returned value < OVERHEAD_LEN\n"); // (%u)", (unsigned int) OVERHEAD_LEN);
transmissionSuccessful = 0;
}
crcRemainder = verifyCRC(frame, recvMsgSize);
if (crcRemainder) {
printf("Incorrect packet recieved, (CRC crcRemainder = %02X)\n", crcRemainder);
transmissionSuccessful = 0;
} else if (!first) { /* check packet sequence */
prevPacketCount = currentPacketCount;
currentPacketCount = (unsigned int) frame[recvMsgSize - FOOTER_LEN];
if (currentPacketCount == 0) {
if (prevPacketCount != (MAX_PACKET_COUNT-1))
transmissionSuccessful = 0;
} else if ((currentPacketCount - prevPacketCount) != 1) {
transmissionSuccessful = 0;
}
if (!transmissionSuccessful) {
printf("Current packet count: %u, previous packet count: %u\n"
"Current packet count is not 1 greater than previous mod 63\n."
"Requesting retransmission...\n",
currentPacketCount, prevPacketCount);
}
} else {
first = 0;
currentPacketCount = (unsigned int) frame[recvMsgSize - FOOTER_LEN];
}
if ((memcmp(frame, header, HEADER_LEN) != 0) && transmissionSuccessful) {
printf("Header not found\n");
transmissionSuccessful = 0;
}
if (transmissionSuccessful) {
printf("Received correct packet\n");
fwrite(&frame[HEADER_LEN], (recvMsgSize - OVERHEAD_LEN), 1, fp);
responseFrame[0] = (currentPacketCount) % (MAX_PACKET_COUNT);
} else {
printf("Requesting retransmission\n");
responseFrame[0] = (unsigned char) ERR_REQ;
}
if (!(sendto(sock, responseFrame, RESPONSE_LEN, 0, (struct sockaddr *)
&senderAddr,
sizeof(senderAddr)) != recvMsgSize))
DieWithError("sendto() sent a different number of bytes than expected");
}
#ifdef WIN32
closesocket(sock);
WSACleanup();
#else
close(sock);
#endif
return 0;
}
char verifyCRC(char *frame, const unsigned int totalBytes)
{
char ans = 0;
crc_st crc;
unsigned int byte = 0;
unsigned int bit;
unsigned int i;
int leadingZeros = 1;
memset(&crc, 0, sizeof(crc_st));
while(byte < totalBytes)
{
i = 0;
while(i < 8) {
bit = (frame[byte] >> (7-i)) & 0x1;
if (byte || bit || !leadingZeros) { /* Ignore leading zeros */
if ((byte != (IFRAME-1)) || (i < 4)) { /* Ignore last 4 padding bits */
bit = crc.c3 ^ bit;
crc.c3 = bit ^ crc.c2;
crc.c2 = bit ^ crc.c1;
crc.c1 = crc.c0;
crc.c0 = bit;
leadingZeros = 0;
}
}
i++;
}
byte++;
}
ans |= (((char) crc.c3) << 3);
ans |= (((char) crc.c2) << 2);
ans |= (((char) crc.c1) << 1);
ans |= (char) crc.c0;
return ans;
}