This repository was archived by the owner on Jan 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathclient.c
More file actions
345 lines (296 loc) · 8.85 KB
/
Copy pathclient.c
File metadata and controls
345 lines (296 loc) · 8.85 KB
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
/*
* project: tftp client program
* author: Oscar Sanchez (oms1005@gmail.com)
* description: a small tftp (trivial ftp) client program
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <getopt.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/uio.h>
#include <strings.h>
#include <sys/stat.h>
#include "header.h"
#include "checksum.h"
#define RETRY 10
int recvfromTimeOut(int socket, long sec, long usec)
{
// Setup timeval variable
struct timeval timeout;
timeout.tv_sec = sec;
timeout.tv_usec = usec;
// Setup fd_set structure
fd_set fds;
FD_ZERO(&fds);
FD_SET(socket, &fds);
// >0: data ready to be read
return select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
}
/* Returns the recv message */
int tftp(int sockfd, const void * packet, int expected_seqnum, int timeout, int flag ) {
int numbytes = 0, ret = 0, retry = RETRY;
int is_retransmit = 0;
char ackbuf[HEADER_SIZE];
header_t head;
int res = 0; /* Timer/Select Variable */
do {
/* Try to send the packet */
if ((numbytes = send(sockfd, packet, PACKET_SIZE, 0)) == -1) {
perror("Error in tftp");
exit(1);
} else if (numbytes >0) {
if (flag == ACK){
printf("Packet ");
if(is_retransmit)
printf("'%d' Re-transmitted.\n", expected_seqnum);
else
printf("'%d' sent\n", expected_seqnum);
} else {
printf("FIN ");
if(is_retransmit)
printf("'%d' Re-transmitted.\n", expected_seqnum);
else
printf("'%d' sent\n", expected_seqnum);
}
}
//sleep(1);
/* Start the timeout */
res = recvfromTimeOut(sockfd, 0, timeout);
switch (res)
{
case 0:
// Timed out, do whatever you want to handle this situation
printf("Packet '%d' ******Timed Out*****, %d retries left\n", expected_seqnum, retry-1);
is_retransmit = 1;
ret = -2;
break;
case -1:
// Error occured, maybe we should display an error message?
printf("ERROR with timer recvfromTimeOut()\n");
exit(-1);
break;
default:
// Ok the data is ready, call recvfrom() to get it then
//printf("We are trying to received something\n");
//ret = recv(sockfd,ackbuf,HEADER_SIZE,0);
ret = recv(sockfd,ackbuf,HEADER_SIZE,0);
if( ret == -1) {
perror("Error in tftp");
exit(1);
}
if (ret > 0) {
//printf("We received something\n");
read_header(&head, (packet_t *)ackbuf);
//read_packet((u_char*) recved, (packet_t *)buf, (u_short) (numbytes - HEADER_SIZE));
if ( (int) head.flag == ACK && (int) head.seq == expected_seqnum ) {
printf("ACK '%d' received\n", expected_seqnum);
return ret;
} else if ( (int) head.flag == FIN && (int) head.seq == expected_seqnum ){
ret -10;
return ret;
}
}
return ret;
}
} while(--retry);
if (!retry && ret == -2) {
printf("Packet '%d' failed with '%d' retries. \n", expected_seqnum, RETRY);
ret = -1;
}
return ret;
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(int argc, char *argv[])
{
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
// used for inputs
char ch, *filename = (char*) "sendfile.txt", *address = (char *) "127.0.0.1";
int port = 69;
/* Read command line options */
while ((ch = getopt(argc, argv, "h:p:f:")) != -1) {
switch (ch) {
case 'h':
address = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 'f':
filename = optarg;
break;
case '?':
if (optopt == 'f') {
fprintf(stderr, "Option -%c requires an argument\n", optopt);
} else {
fprintf(stderr, "Unknown option\n");
}
exit(1);
}
}
char PORT[10];
sprintf(PORT,"%d",port);
printf("Starting client connecting to host: %s on port: %d with filename: %s\n", address, port, filename );
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(address, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
s, sizeof s);
printf("client: connecting to %s\n", s);
//freeaddrinfo(servinfo); // all done with this structure
/* Set Socket to non blocking */
fcntl(sockfd, F_SETFL, O_NONBLOCK);
/* Initialize data*/
char databuf[PAYLOAD_SIZE];
char packetbuf[PACKET_SIZE];
int seqnum = 0;
packet_t packet;
u_short checksum;
int retry = RETRY;
int numbytes = 0, ret = 0;
FILE * filefd;
struct stat finfo;
int filesize=0;
int send_results = 0;
/* Initialize the arrays to zeros */
bzero(databuf, PAYLOAD_SIZE);
bzero(packetbuf, PACKET_SIZE);
/* open the file for reading */
if ((filefd = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "Could not open file for reading!\n");
close(sockfd);
return 2;
} else {
// if (-1 == stat(argv[3], &finfo))
// fprintf(stderr,"error stating file!\n");
printf("File '%s' of size '%d' bytes opened and ready to send filename\n", filename, (int)finfo.st_size );
}
/* Copy the filename to the buffer */
strcpy(databuf, filename);
databuf[PAYLOAD_SIZE] = '\0';
/* create a packet */
fill_header(seqnum, 0, PAYLOAD_SIZE, ACK, &packet);
fill_packet((u_char*) &databuf[0], &packet, PAYLOAD_SIZE);
/* calculate checksum */
checksum = add_checksum(PACKET_SIZE, 0, (u_short*) &packet);
packet.header.checksum = htons(checksum);
//assert(add_checksum(PACKET_SIZE, 0, (u_short *)&packet) == 0);
/* Send the filename... */
send_results = tftp(sockfd, &packet, seqnum, 900000, ACK );
if (send_results == -1) {
fprintf(stderr, "Time out..\n");
close(sockfd);
return 2;
} else if (send_results == -10) {
printf("Received the final FIN.. Closing up shop.");
fclose(filefd);
close(sockfd);
return 0;
}
seqnum++;
/* File Sending Section*/
/* Re-Initialize the arrays to zeros */
bzero(databuf, PAYLOAD_SIZE);
bzero(packetbuf, PACKET_SIZE);
int readCounter = 0;
int writeCounter = 0;
char* readbufptr = NULL;
/* read the file, and send it to the client in chunks of size PAYLOAD_SIZ */
while((readCounter = fread(databuf,1, PAYLOAD_SIZE, filefd)) > 0)
{
writeCounter = 0;
readbufptr = databuf;
databuf[readCounter] = '\0';
fill_header(seqnum, 0, readCounter, ACK, &packet);
fill_packet((u_char*) &databuf[0], &packet, readCounter);
/* calculate checksum */
checksum = add_checksum(PACKET_SIZE, 0, (u_short*) &packet);
packet.header.checksum = htons(checksum);
//printf("Bytes sent: '%d' while readCounter '%d'\n", sizeof(packet), readCounter);
/* Send the filecontents... */
/* Send the filename... */
send_results = tftp(sockfd, &packet, seqnum, 900000, ACK );
if (send_results == -1) {
fprintf(stderr, "Time out..\n");
close(sockfd);
return 2;
} else if (send_results == -10) {
printf("Received the final FIN.. Closing up shop.");
fclose(filefd);
close(sockfd);
return 0;
}
seqnum++;
/* Re-Initialize the arrays to zeros */
bzero(databuf, PAYLOAD_SIZE);
bzero(packetbuf, PACKET_SIZE);
}
/* Re-Initialize the arrays to zeros */
bzero(databuf, PAYLOAD_SIZE);
bzero(packetbuf, PACKET_SIZE);
/* Send the FIN */
fill_header(seqnum, 0, readCounter, FIN, &packet);
/* calculate checksum */
checksum = add_checksum(PACKET_SIZE, 0, (u_short*) &packet);
packet.header.checksum = htons(checksum);
/* Send the filename... */
send_results = tftp(sockfd, &packet, seqnum, 900000, FIN );
if (send_results == -1) {
fprintf(stderr, "Time out..\n");
close(sockfd);
return 2;
} else if (send_results == -10) {
printf("Received the final FIN.. Closing up shop.");
fclose(filefd);
close(sockfd);
return 0;
}
seqnum++;
printf("Goodbye\n");
freeaddrinfo(servinfo); // all done with this structure
close(sockfd);
return 0;
}