-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_ack.c
296 lines (257 loc) · 7.14 KB
/
server_ack.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
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif
#define data_size 1400
char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
return out;
}
void error(const char *msg)
{
perror(msg);
exit(0);
}
struct Init_PACKET{
uint8_t type;
u_int file_size;
u_int chunk_size;
};
struct packet{
uint8_t type;
int sequence_number;
unsigned char data[data_size];
};
struct ack_packet{
uint8_t type;
int offset;
char packet_tracker[20000];
};
int main(int argc, char *argv[])
{
FILE * oFile;
FILE * f1;
int sock, length, n;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
char buf[63000];
char send_buffer[10000];
int chunks, final_chunk;
bool init_check = 0, mem_alloc_ch=0, data_check=0;
int cons_check = 0;
int type;
struct ack_packet ack_packet1;
int packet_counter = 0;
bool change_ch = 0;
int send_count = 0;
int send_max = 0;
f1 = fopen ( "log_server.txt" , "w" );
/*if (argc < 2)
{
fprintf(stderr, "ERROR, no port provided\n");
exit(0);
}*/
sock=socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) error("Opening socket");
length = sizeof(server);
bzero(&server,length);
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(atoi("12345"));
if (bind(sock,(struct sockaddr *)&server,length)<0)
error("binding");
fromlen = sizeof(struct sockaddr_in);
int file_sz;
int send_counter = 0;
printf("Waiting for init packet \n");
while(1)
{
n = recvfrom(sock,buf,sizeof(struct Init_PACKET),0,(struct sockaddr *)&from,&fromlen);
if (n < 0) error("recvfrom");
printf("No of bytes received: %d\n",n);
struct Init_PACKET* recv_init_packet = (struct Init_PACKET*)buf;
if(recv_init_packet->type == 0)
{
init_check = 1;
printf("Init packet received decoding ... \n");
file_sz = recv_init_packet->file_size;
printf("File size : %d\n",file_sz);
printf("Data size : %d\n",recv_init_packet->chunk_size);
break;
}
}
printf("allocating memory\n");
chunks = ceil(file_sz/(float)data_size);
char **buffer = (char**) malloc (chunks*sizeof(char*));
int count = 0;
for(count = 0; count < chunks - 1;count++)
{
buffer[count] = (char*) malloc (data_size*sizeof(char));
}
final_chunk = file_sz - (chunks-1)*data_size;
buffer[count] = (char*) malloc (final_chunk*sizeof(char));
ack_packet1.type = 4;
int b=0;
printf("Type packet1 %d\n",ack_packet1.type);
memcpy(send_buffer,(unsigned char*)&ack_packet1,sizeof(ack_packet1));
n = sendto(sock,send_buffer,sizeof(ack_packet1),0,(struct sockaddr *)&from,fromlen);
if (n < 0) error("sendto");
mem_alloc_ch = 1;
printf("allocating memory done \n");
printf("Now waiting for data\n");
while (1)
{
n = recvfrom(sock,buf,data_size,0,(struct sockaddr *)&from,&fromlen);
struct packet* data_packet = (struct packet*)buf;
type = data_packet->type;
if(type == 0)
{
ack_packet1.type = 4;
memcpy(send_buffer,(unsigned char*)&ack_packet1,sizeof(ack_packet1));
n = sendto(sock,send_buffer,sizeof(ack_packet1),0,(struct sockaddr *)&from,fromlen);
if (n < 0) error("sendto");
}
else if(type == 1)
{
struct packet* data_packet = (struct packet*)buf;
if(ack_packet1.packet_tracker[data_packet->sequence_number - offset] <= 0)
{
printf("Received seq no : %d\n",data_packet->sequence_number);
memcpy(buffer[data_packet->sequence_number],data_packet->data,data_size);
char *output = str2md5(buffer[data_packet->sequence_number], data_size);
char *output1 = str2md5(data_packet->data, data_size);
fprintf(f1,"type: %d, sequence number : %d hash : %s hash1: %s\n",data_packet->type,data_packet->sequence_number, output,output1);
ack_packet1.packet_tracker[data_packet->sequence_number - offset] = 1;
packet_counter++;
}
}
else if(type == 2)
{
printf("Packet counter val %d\n",packet_counter);
//fprintf(f1,"Packet counter val %d\n",packet_counter);
int b;
fprintf(f1,"\n");
if(packet_counter >= (chunks-2))
{
printf("All packets received except last \n");
ack_packet1.type = 3;
memcpy(send_buffer,(unsigned char*)&ack_packet1,sizeof(ack_packet1));
n = sendto(sock,send_buffer,sizeof(ack_packet1),0,(struct sockaddr *)&from,fromlen);
if (n < 0) error("sendto");
fprintf(f1, "p: ");
for(b = 0; b<chunks;b++)
{
fprintf(f1,"%d",ack_packet1.packet_tracker[b]);
}
fprintf(f1,"\n");
break;
}
else
{
for(send_count=offset;send_count<send_max;send_count++)
{
if(ack_packet1.packet_tracker[send_count - offset] == 0)
{
break;
}
}
printf("send_count %d\n", send_count);
if(send_count >= send_max)
{
change_ch = 1;
}
else
{
change_ch = 0;
}
if( change_ch == 1)
{
offset = offset + 1400;
if(offset > chunks)
{
offset = chunks;
}
}
ack_packet1.type = 2;
memcpy(send_buffer,(unsigned char*)&ack_packet1,sizeof(ack_packet1));
n = sendto(sock,send_buffer,sizeof(ack_packet1),0,(struct sockaddr *)&from,fromlen);
if (n < 0) error("sendto");
}
}
}
while(1)
{
printf("Waiting for last packet\n");
n = recvfrom(sock,buf,final_chunk,0,(struct sockaddr *)&from,&fromlen);
printf("No of bytes received %d\n",n );
struct packet* data_packet = (struct packet*)buf;
printf("type %d : Received seq no : %d \n",data_packet->type,data_packet->sequence_number);
printf("1\n");
if(data_packet->type == 6)
{
memcpy(buffer[data_packet->sequence_number],data_packet->data,final_chunk);
printf("2\n");
int s_count =0;
for(s_count = 0;s_count<10;s_count++)
{
printf("Sending type5\n");
ack_packet1.type = 5;
memcpy(send_buffer,(unsigned char*)&ack_packet1,sizeof(ack_packet1));
n = sendto(sock,send_buffer,sizeof(ack_packet1),0,(struct sockaddr *)&from,fromlen);
if (n < 0) error("sendto");
}
break;
}
else if(data_packet->type == 2)
{
ack_packet1.type = 3;
memcpy(send_buffer,(unsigned char*)&ack_packet1,sizeof(ack_packet1));
n = sendto(sock,send_buffer,sizeof(ack_packet1),0,(struct sockaddr *)&from,fromlen);
if (n < 0) error("sendto");
}
}
oFile = fopen ( "received_test1.bin" , "wb" );
printf("3\n");
int w_count = 0;
for(w_count = 0; w_count<chunks-1;w_count++)
{
fwrite (buffer[w_count] , sizeof(char), data_size, oFile);
}
printf("4\n");
fwrite (buffer[w_count], sizeof(char), final_chunk, oFile);
printf("5\n");
fclose (oFile);
close(sock);
return 0;
}