-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.c
302 lines (272 loc) · 6.5 KB
/
receiver.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#pragma comment(lib, "Ws2_32.lib")
#define BUFFER_LEN 4
#define IP_SIZE 16
#define MAX_NAME 200
#define FOUR_BYTES 32
#define FAILED -1
#define P1 0b1
#define P2 0b10
#define P3 0b100
#define P4 0b1000
#define P5 0b10000
#define MASK 0x03FFFFFF
int power2(int x)
{
int res = 1;
if (x < 0) {
printf("argument is negative!\n");
return FAILED;
}
for (int i = 0; i < x; i++) {
res *= 2;
}
return res;
}
char* lowercase(char* str)
{
int str_size = strlen(str);
char* str_before = str, * str_after = NULL;
if ((str_after = (char*)calloc(1, (str_size + 1) * sizeof(char))) == NULL) {
printf("Allocation Failed\n");
exit(EXIT_FAILURE);
}
if (str != NULL) {
for (int i = 0; i < str_size; i++) {
(*(str_after + i)) = tolower((*(str_before + i)));
}
}
else {
printf("Attempt to operate on NULL pointer\n");
return NULL;
}
return str_after;
}
void resources_release(SOCKET socket, FILE* fp, bool end)
{
SOCKET* s = &socket;
if (fp != NULL) {
fclose(fp);
fp = NULL;
}
if (end) {
shutdown(socket, 2);
WSACleanup();
return;
}
if (socket != INVALID_SOCKET) {
closesocket(socket);
(*s) = INVALID_SOCKET;
}
return;
}
bool parity_bit_check(int type, unsigned int number)
{
int on_bits = 0;
unsigned int num = number;
switch (type) {
case 0: { //p0 - all bits parity - check for 2 errors
while (num > 0) {
if (num & 0b1) {
on_bits++;
}
num = num >> 1;
}
break;
}
case 1: { // p1
num = num >> 1;
while (num > 0) {
if (num & 0b1) {
on_bits++;
}
num = num >> 2;
}
break;
}
case 2: { //p2
num = num >> 2;
while (num > 0) {
for (int i = 0; i < 2; i++) {
if (num & 0b1) {
on_bits++;
}
num = num >> 1;
}
num = num >> 2;
}
break;
}
case 3: { //p3
num = num >> 4;
while (num > 0) {
for (int i = 0; i < 4; i++) {
if (num & 0b1) {
on_bits++;
}
num = num >> 1;
}
num = num >> 4;
}
break;
}
case 4: { //p4
num = num >> 8;
while (num > 0) {
for (int i = 0; i < 8; i++) {
if (num & 0b1) {
on_bits++;
}
num = num >> 1;
}
num = num >> 8;
}
break;
}
case 5: { //p5
num = num >> 16;
while (num > 0) {
if (num & 0b1) {
on_bits++;
}
num = num >> 1;
}
break;
}
}
if (on_bits % 2 == 0) {
return true;
}
else {
return false;
}
}
unsigned int hamming_decode(char* mes, int* errors)
{
int j = 0, error_pose = 0;
unsigned int res = 0, message = 0, * imessage = NULL;
int par_checks[5] = { 0 };
imessage = (unsigned int*)mes;
message = *imessage;
imessage = &message;
//error detection & correction
for (int i = 1; i < 6; i++) {
if ((parity_bit_check(i, message)) == false) {
error_pose += power2(i - 1);
}
}
if ((parity_bit_check(0, message) == false) && (error_pose == 0)) {
message = message ^ 0b1;
(*errors)++;
}
if (error_pose > 0) {
message = message ^ (power2(error_pose)); //1-bit error correct
(*errors)++;
}
//extract data
for (int i = 3; i < FOUR_BYTES; i++) {
if ((i == 4) || (i == 8) || (i == 16)) {
continue;
}
else {
if (power2(i) & message) {
res += power2(j);
}
j++;
}
}
res = res & MASK;
return res;
}
int main(int argc, char* argv[])
{
int bytes_recv = 1, data_recv = 0, file_len = 0, errors = 0, * perrors = NULL;
unsigned int w_buffer = 0, h_buffer = 0;
unsigned short port_number = 0;
char message[BUFFER_LEN] = { '\0' }, ip_number[IP_SIZE] = { '\0' }, file_name[MAX_NAME] = { '\0' };
FILE* fptr = NULL;
SOCKET sock = INVALID_SOCKET;
struct sockaddr_in sock_info;
strcpy(ip_number, argv[1]);
port_number = atoi(argv[2]);
perrors = &errors;
// Initialize Winsock
WSADATA wsaData;
int iResult, ports[2];
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with Error Code : %d\n", iResult);
return EXIT_FAILURE;
}
sock_info.sin_family = AF_INET;
sock_info.sin_addr.s_addr = inet_addr(ip_number);
sock_info.sin_port = htons(port_number);
while (1) { // receiver routine
printf("enter file name:\n");
gets(file_name);
if (0 == strcmp(lowercase(file_name), "quit")) {
printf("Quiting...\n");
resources_release(INVALID_SOCKET, NULL, true);
return EXIT_SUCCESS;
}
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
printf("socket function failed with error: %ld\n", WSAGetLastError());
resources_release(INVALID_SOCKET, NULL, true);
return EXIT_FAILURE;
}
if ((connect(sock, (struct sockaddr*)&sock_info, sizeof(sock_info))) == SOCKET_ERROR) {
printf("connect() failed with error code: %d\n", WSAGetLastError());
resources_release(INVALID_SOCKET, NULL, true);
return EXIT_FAILURE;
}
if ((fptr = fopen(file_name, "wb")) == NULL) {
printf("failed to open file!\n");
resources_release(sock, NULL, true);
return EXIT_FAILURE;
}
bytes_recv = recv(sock, message, BUFFER_LEN, 0); //first message
if ((h_buffer = hamming_decode(message, perrors)) == FAILED) { //hamming (31,26,3)
resources_release(sock, fptr, true);
return EXIT_FAILURE;
}
while ((bytes_recv = recv(sock, message, BUFFER_LEN, 0)) > 0)
{
w_buffer = h_buffer;
if ((h_buffer = hamming_decode(message, perrors)) == FAILED) { //hamming (31,26,3)
resources_release(sock, fptr, true);
return EXIT_FAILURE;
}
fwrite(&w_buffer, 1U, BUFFER_LEN * sizeof(char), fptr); //write to files
fseek(fptr, -1L, SEEK_CUR);
data_recv++;
}
if (bytes_recv < 0) { //recv() failure
printf("recv() failed with error code: %d\n", WSAGetLastError());
resources_release(sock, fptr, true);
return EXIT_FAILURE;
}
//last message
fwrite(&h_buffer, 1U, (strlen(message) - 1) * sizeof(char), fptr);
data_recv++;
data_recv *= BUFFER_LEN;
fseek(fptr, 0, SEEK_END);
file_len = ftell(fptr);
printf("received: %d\nwrote: %d\ncorrected %d errors\n", data_recv, file_len, errors);
resources_release(INVALID_SOCKET, fptr, false);
data_recv = 0, file_len = 0, errors = 0;
}
return EXIT_SUCCESS;
}