-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinalclient.c
360 lines (290 loc) · 9.66 KB
/
finalclient.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <limits.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
typedef struct packet {
char data[1024];
} Packet;
typedef struct frame {
int frame_kind; //ACK:0, SEQ:1 FIN:2
int sq_no;
int ack;
Packet packet;
} Frame;
void error(char *msg) {
perror(msg);
exit(0);
}
// CRC parameters (default values are for CRC-8):
const int order = 8;
const unsigned long polynom = 0x07;
const int direct = 1;
const unsigned long crcinit = 0x00;
const unsigned long crcxor = 0x00;
const int refin = 0;
const int refout = 0;
// 'order' [1..32] is the CRC polynom order, counted without the leading '1' bit
// 'polynom' is the CRC polynom without leading '1' bit
// 'direct' [0,1] specifies the kind of algorithm: 1=direct, no augmented zero bits
// 'crcinit' is the initial CRC value belonging to that algorithm
// 'crcxor' is the final XOR value
// 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not
// 'refout' [0,1] specifies if the CRC will be reflected before XOR
// internal global values:
unsigned long crcmask;
unsigned long crchighbit;
unsigned long crcinit_direct;
unsigned long crcinit_nondirect;
unsigned long crctab[256];
unsigned long reflect(unsigned long crc, int bitnum) {
// reflects the lower 'bitnum' bits of 'crc'
unsigned long i, j = 1, crcout = 0;
for (i = (unsigned long) 1 << (bitnum - 1); i; i >>= 1) {
if (crc & i) crcout |= j;
j <<= 1;
}
return (crcout);
}
void generate_crc_table() {
// make CRC lookup table used by table algorithms
int i, j;
unsigned long bit, crc;
for (i = 0; i < 256; i++) {
crc = (unsigned long) i;
if (refin) crc = reflect(crc, 8);
crc <<= order - 8;
for (j = 0; j < 8; j++) {
bit = crc & crchighbit;
crc <<= 1;
if (bit) crc ^= polynom;
}
if (refin) crc = reflect(crc, order);
crc &= crcmask;
crctab[i] = crc;
}
}
unsigned long crctablefast(unsigned char *p, unsigned long len) {
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
// only usable with polynom orders of 8, 16, 24 or 32.
unsigned long crc = crcinit_direct;
if (refin) crc = reflect(crc, order);
if (!refin) while (len--) crc = (crc << 8) ^ crctab[((crc >> (order - 8)) & 0xff) ^ *p++];
else while (len--) crc = (crc >> 8) ^ crctab[(crc & 0xff) ^ *p++];
if (refout ^ refin) crc = reflect(crc, order);
crc ^= crcxor;
crc &= crcmask;
return (crc);
}
char *stringToBinary(char *s) {
if (s == NULL) {
// NULL might be 0 but you cannot be sure about it
return NULL;
}
// get length of string without NUL
size_t slen = strlen(s);
// we cannot do that here, why?
// if(slen == 0){ return s;}
errno = 0;
// allocate "slen" (number of characters in string without NUL)
// times the number of bits in a "char" plus one byte for the NUL
// at the end of the return value
char *binary = malloc(slen * CHAR_BIT + 1);
if (binary == NULL) {
fprintf(stderr, "malloc has -1ed in stringToBinary(%s): %s\n", s, strerror(errno));
return NULL;
}
// finally we can put our shortcut from above here
if (slen == 0) {
*binary = '\0';
return binary;
}
char *ptr;
// keep an eye on the beginning
char *start = binary;
int i;
// loop over the input-characters
for (ptr = s; *ptr != '\0'; ptr++) {
/* perform bitwise AND for every bit of the character */
// loop over the input-character bits
for (i = CHAR_BIT - 1; i >= 0; i--, binary++) {
*binary = (*ptr & 1 << i) ? '1' : '0';
}
}
// finalize return value
*binary = '\0';
// reset pointer to beginning
binary = start;
return binary;
}
void random_flip(char *rem_message, float BER) {
unsigned int i = 0;
srand(time(0));
for (i = 0; i < strlen(rem_message); i++) {
if ((float) rand() / RAND_MAX < BER) {
if (rem_message[i] == '0') {
rem_message[i] = '1';
} else if (rem_message[i] == '1') {
rem_message[i] = '0';
} else {
fprintf(stderr, "Convert to binary was not successful\n");
}
}
}
}
int connection_setup(int port, char *addr) {
struct sockaddr_in serv_addr;
int socket_client; // client fd
// create a new socket witch AF_INET for internet domain, stream socket option, TCP(given by os) - reliable, connection oriented
if ((socket_client = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
error("Error! failed to open the socket\n");
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port); // convert a port number in host byte order to a port number in network byte order
serv_addr.sin_addr.s_addr = inet_addr(addr);
bzero(&serv_addr.sin_zero, 8); // clears the buffer
struct timeval tv;
tv.tv_sec = 5; // 5 Secs timeout
tv.tv_usec = 0; // not initialising this can cause strange errors
// set socket option for timeout
setsockopt(socket_client, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(struct timeval));
// connect the socket referred by the fd to the addr specified by socket addr
if (connect(socket_client, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr_in)) < 0) {
error("ERROR connecting");
}
return socket_client;
}
int server_communicate(int socket_client, char *formed_message) {
int str_len;
socklen_t adr_sz;
struct sockaddr_in serv_adr, from_adr;
Frame ackframe, recv_frame;
int frame_id;
int recv_result;
// send to the socket
if (send(socket_client, formed_message, strlen(formed_message), 0) < 0) {
fprintf(stderr, "Sent error...\n");
return -1;
}
printf("Sent message. Waiting for ACK/NACK...\n");
int reply_length;
char reply[64];
// receive from the socket
if ((reply_length = recv(socket_client, reply, 64, 0)) < 0) {
fprintf(stderr, "Timeout. Re-transmitting...\n");
return -1;
}
// else if (!(strcmp(reply,"ACK")==0 || strcmp(reply,"NACK")==0))
// {
// printf("Message received: %s\n",reply );
// fprintf(stderr, "Error in ACK or NACK!! Re-transmitting... \n");
// return -1;
// }
reply[reply_length] = '\0';
printf("Reply received: %s\n", reply);
if (strcmp(reply, "NACK") == 0) {
fprintf(stderr, "Previous transmission had some error. Re-transmitting...\n");
return -1;
} else if (strcmp(reply, "ACK") == 0) {
return 1;
} else {
fprintf(stderr, "Error in ACK or NACK!! Re-transmitting... \n");
return -1;
}
}
char *
itoa(int value, char *result, int base) {
// check that the base if valid
if (base < 2 || base > 36) {
*result = '\0';
return result;
}
char *ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 +
(tmp_value - value * base)];
} while (value);
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while (ptr1 < ptr) {
tmp_char = *ptr;
*ptr-- = *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage ./client ip_addr port");
return -1;
}
float BER;
printf("Enter BER (probability of bit errors): ");
scanf("%f", &BER);
int socket_client;
if ((socket_client = connection_setup(atoi(argv[2]), argv[1])) == -1) {
return -1;
}
int i;
unsigned long bit, crc;
// at first, compute constant bit masks for whole CRC and CRC high bit
crcmask = ((((unsigned long) 1 << (order - 1)) - 1) << 1) | 1;
crchighbit = (unsigned long) 1 << (order - 1);
generate_crc_table();
crcinit_direct = crcinit;
crc = crcinit;
for (i = 0; i < order; i++) {
bit = crc & 1;
if (bit) crc ^= polynom;
crc >>= 1;
if (bit) crc |= crchighbit;
}
crcinit_nondirect = crc;
while (true) {
char message[1024];
printf("Enter your message: ");
scanf("%s", message);
char rem_message[1024];
char formed_message[10000];
do {
strcpy(formed_message, stringToBinary(message));
unsigned long msg = crctablefast((unsigned char *) message, strlen(message));
char *temp;
temp = (char *) malloc(8192 * sizeof(char *));
temp = itoa(msg, temp, 2);
// printf("%s\n",temp);
i = 0;
char *e;
e = (char *) malloc(8200 * sizeof(char *));
if (strlen(temp) < 8) {
for (i = 0; i < 8 - strlen(temp); i++) {
strcat(e, "0");
}
strcat(e, temp);
strcpy(rem_message, e);
}
else {
strcpy(rem_message, temp);
}
free(temp);
free(e);
strcat(formed_message, rem_message);
//add error - flip bits randomly
random_flip(formed_message, BER);
} while (server_communicate(socket_client, formed_message) == -1);
}
close(socket_client);
return 0;
}