-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_client_timeout.c
375 lines (305 loc) · 9.15 KB
/
udp_client_timeout.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Code to start Socket Programming in ICEN/ICSI 416
* Author: Jingyuan Yi
*
* udp_client.c - A simple TCP-over-UDP client, handshakes with server and transmits files (all with attached header)
*
*
* Compile in itsunix: gcc -lsocket -lm -lnsl udp_client_timeout.c -o udp_client
*
* usage: udp_client <host> <port>
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h>
#include <stdint.h>
#include <math.h>
#define BUFSIZE 1024
#define DATASIZE 877
#define WAIT_TIME 5 // In seconds
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
exit(0);
}
static void sig_alrm(int);
static void sig_alrm(int signo)
{
return;
}
typedef struct{
int src_port; // source port
int dest_port; // destination port
int seq_no;//sequence number for sender and expected sequence number for receiver
int ack_no;
int check;
int urg_ptr;
int SYN;
int FIN;
int ACK;
}TCP_hearder;
long long unsigned int dec2bin(int dec) //convert dec to bin ,using llu to avoid overflow
{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * dec2bin(dec / 2));
}
}
void tobinstr(long long unsigned int value, int bitsCount, char* output) //bin integer to string formation
{
int i;
output[bitsCount] = '\0';
for (i = bitsCount - 1; i >= 0; --i)
{
output[i] = (value % 10) + '0';
value /= 10;
}
}
void get_tcp_header_string(TCP_hearder *header,char *head_string) //transform TCP_hearder to bin string
{
char *temp=(char *)malloc(sizeof(char)*32+1);
tobinstr(dec2bin(header->src_port),16,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->dest_port),16,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->seq_no),32,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->ack_no),32,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->check),16,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->urg_ptr),16,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->SYN),6,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->FIN),6,temp);
strcat(head_string,temp);
tobinstr(dec2bin(header->ACK),6,temp);
strcat(head_string,temp);
free(temp);
}
void print_header(TCP_hearder * header) //print the TCP header information (using the struct)
{
printf("--------------TCP HEADER--------------\n");
printf( "seq_no = %d \n", header->seq_no);
printf( "src_port = %d \n", header->src_port);
printf( "dest_port = %d \n", header->dest_port);
printf( "ack_no = %d \n", header->ack_no);
printf( "check = %d \n", header->check);
printf( "urg_ptr = %d \n", header->urg_ptr);
printf( "SYN = %d \n", header->SYN);
printf( "FIN = %d \n", header->FIN);
printf( "ACK = %d \n", header->ACK);
printf("-----------------END-----------------\n");
}
void decode_tcp_header(char *buf,TCP_hearder *header,char *data) // decode the tcp header as well as data attached.
{
int i=0;
int j=0;
int var=0;
for(j=0;i<16;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,16-j-1);
}
header->src_port = var;
var = 0;
for(j=0;i<16+16;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,16-j-1);
}
header->dest_port = var;
var = 0;
for(j=0;i<16+16+32;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,32-j-1);
}
header->seq_no = var;
var = 0;
for(j=0;i<16+16+32+32;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,32-j-1);
}
header->ack_no = var;
var = 0;
for(j=0;i<16+16+32+32+16;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,16-j-1);
}
header->check = var;
var = 0;
for(j=0;i<16+16+32+32+16+16;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,16-j-1);
}
header->urg_ptr = var;
var = 0;
for(j=0;i<16+16+32+32+16+16+6;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,6-j-1);
}
header->SYN = var;
var = 0;
for(j=0;i<16+16+32+32+16+16+6+6;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,6-j-1);
}
header->FIN = var;
var = 0;
for(j=0;i<16+16+32+32+16+16+6+6+6;i++,j++)
{
var+=(int)(buf[i]-'0')*pow(2,6-j-1);
}
header->ACK = var;
memcpy(data,buf+i,strlen(buf+i));
}
int main(int argc, char **argv) {
int sockfd, portno, n;
int serverlen;
struct sockaddr_in serveraddr;
struct hostent *server;
char *hostname;
char data[DATASIZE]= "";
char buf[BUFSIZE];
char *headstr=(char *)malloc(sizeof(char)*128+1);
char *filename;
FILE *fp;
signal(SIGALRM, sig_alrm);
/* check command line arguments */
if (argc != 4) {
fprintf(stderr,"usage: %s <hostname> <port> <filename>\n", argv[0]);
exit(0);
}
hostname = argv[1];
portno = atoi(argv[2]);
filename = argv[3];
/* check the file */
if((fp=fopen(filename,"r"))==NULL){
error("ERROR opening file");
}
/* socket: create the socket */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket\n");
/* gethostbyname: get the server's DNS entry */
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host as %s\n", hostname);
exit(0);
}
/* build the server's Internet address */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
serverlen = sizeof(serveraddr);
/* hand shake step 1 */
/* initialize the header */
TCP_hearder *myhdr = (TCP_hearder *)malloc(sizeof(myhdr));
myhdr->ack_no=2;
myhdr->check=3;
myhdr->dest_port=2333;
myhdr->seq_no=1000;
myhdr->src_port=2334;
myhdr->urg_ptr=2;
myhdr->SYN = 1;
myhdr->FIN=0;
myhdr->ACK=0;
/* print the header information (at client side)*/
print_header(myhdr);
/* transform header into binary string */
get_tcp_header_string(myhdr, headstr);
/* attach data to string */
//printf("header length = %d\n", strlen(headstr));
//printf("data length = %d\n", strlen(data));
memcpy(buf,headstr,strlen(headstr));
memcpy(data,"1st shake",strlen("1st shake"));
memcpy(buf+strlen(headstr),data,strlen(data));
/* send the message to the server */
n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&serveraddr, serverlen);
if (n < 0)
error("ERROR in sendto");
/*Set the timer for recvfrom*/
alarm(WAIT_TIME);
/* print the server's reply */
n = recvfrom(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&serveraddr, &serverlen);
if (n < 0)
{
if (errno == EINTR)
fprintf(stderr, "Socket timeout\n");
else
error("recvfrom() error\n");
}
else {
alarm(0);
TCP_hearder *ack_hdr = (TCP_hearder *)malloc(sizeof(ack_hdr));
decode_tcp_header(buf,ack_hdr,data);
printf("\n\nget 2nd handshake info from server,header below\n");
print_header(ack_hdr);
/*get 2nd handshake info from server*/
/* send 3nd handshake to server*/
myhdr->ACK = 1;
myhdr->SYN = 0;
myhdr->seq_no = myhdr->seq_no + 1 ;
myhdr->ack_no = ack_hdr->seq_no + 1;
memcpy(data,"3nd handshake",strlen("3nd handshake"));
get_tcp_header_string(myhdr, headstr);
memcpy(buf,headstr,strlen(headstr));
memcpy(buf+strlen(headstr),data,strlen(data));
printf("\n\nstart sending 3rd handshake, header below\n");
print_header(myhdr);
n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&serveraddr, serverlen);
if (n < 0)
error("ERROR in 3rd sendto");
printf("client side established 3-handshake connection\n");
/*
* start to loop to read line from disk file
* and send it to target tcp server
*/
myhdr->ACK = 0;
myhdr->SYN = 0;
while(!feof(fp)){
bzero(buf, BUFSIZE);
bzero(data, DATASIZE);
bzero(headstr,sizeof(char)*128+1);
/* get string from file by line */
fgets(data,DATASIZE,fp);
printf("data from file is : %s\n", data);
get_tcp_header_string(myhdr, headstr);
memcpy(buf,headstr,strlen(headstr));
memcpy(buf+strlen(headstr),data,strlen(data));
printf("\n\ntransmission header below\n");
print_header(myhdr);
n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&serveraddr, serverlen);
if (n < 0)
error("ERROR in sending files");
}
fclose(fp);
/* write end symbol "$" to server */
bzero(buf, BUFSIZE);
bzero(headstr,sizeof(char)*128+1);
myhdr->FIN =1; //setting finish bit
get_tcp_header_string(myhdr, headstr);
memcpy(buf,headstr,strlen(headstr));
memcpy(buf+strlen(headstr),"$",strlen("$"));
printf("\n\nserver end signal sent, header below\n");
print_header(myhdr);
n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*)&serveraddr, serverlen);
if (n < 0)
error("ERROR in sending files");
}
return 0;
}