/
ftpclient.c
355 lines (252 loc) · 7.54 KB
/
ftpclient.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
/*
* a2client.c
*
* PURPOSE: An ftp client.
* INSTRUCTIONS:
*
* There will be 3 arguments in command line
* a2client <ip_address> <port_number>
*
* choose ip_address to be 127.0.0.1
* choose port number to be 10000
*
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <fcntl.h>
#define MAX_LENGTH 50
#define SEPCHARS " \t\r\n"
enum BOOLEAN{
FALSE = 0,
TRUE = 1
};
typedef enum BOOLEAN boolean;
/* PURPOSE: This is to get the filename that is specified in a command.
*/
void get_filename(char command[], char filename[]){
char *word;
//parse
word = strtok(command, SEPCHARS);
if(word == NULL){
perror("\nError in PUT command");
exit(1);
}
//get the filename
word = strtok(NULL, SEPCHARS);
if(word == NULL){
strncpy(filename, "", MAX_LENGTH);
}
else{
strncpy(filename, word, MAX_LENGTH);
}
}
/* PURPOSE: This is for checking if a file really is in the client directory.
* This is done by actually trying to open the file. If we can't open
* the file then we know it doesn't exist.
*/
boolean check_directory(char command[]){
int file;
char filename[MAX_LENGTH];
boolean has_write = TRUE;
get_filename(command, filename);
//read the file
file = open(filename, O_RDONLY);
if(file == -1){
perror("\nError in open file");
close(file);
has_write = FALSE;
}
return has_write;
}
/* PURPOSE: Process the LIST command entered by client. Server will send us a list
* of it's current directory.
*/
void process_list(int sock_data){
//read the data from the server
char list[MAX_LENGTH];
int n;
while((n = read(sock_data, list, MAX_LENGTH-1))){
list[n] = '\0';
fprintf(stderr, "%s", list);
}
}
/* PURPOSE: Process the GET command entered by client. This will make a new
* file and put it in our directory.
*/
void process_get(char command[], int sock_data){
int file;
int bytes_received = 0;
char temp[MAX_LENGTH];
char filename[MAX_LENGTH];
int n;
get_filename(command, filename);
//create new file
file = open(filename,O_WRONLY|O_CREAT,S_IRWXU);
if(file == -1){
perror("\nError in open file");
exit(1);
}
//read data in from server and write these into new file
n = read(sock_data, temp, MAX_LENGTH);
while(n > 0){
bytes_received += n;
write(file, temp, n);
n = read(sock_data, temp, MAX_LENGTH);
}
if(n == 0){
close(file);
close(sock_data);
fprintf(stderr, "\nBytes received: %d\n", bytes_received);
}
else if(n < 0){
fprintf(stderr, "\nError in read()");
exit(1);
}
}
/* PURPOSE: Process the PUT command entered by client
*
*/
void process_put(char command[], int sock_data){
int file;
char filename[MAX_LENGTH];
char temp[MAX_LENGTH];
get_filename(command, filename);
//read the file
file = open(filename, O_RDONLY);
if(file == -1){
perror("\nError in open file");
exit(1);
}
//read the file line by line, send each line to the server
int byte = read(file, temp, MAX_LENGTH);
while(byte > 0){
//fprintf(stderr, "%s", temp); //if we want to see what we send
write(sock_data, temp, byte);
byte = read(file, temp, MAX_LENGTH);
}
//the client will close the connection once it has transferred all the data
//(sent all the lines)
if(byte == 0){
close(file);
close(sock_data);
fprintf(stderr, "\nDone transferring file. Closed data connection.\n\n");
}
else if(byte < 0){
perror("\nError in read file");
exit(1);
}
}
/* PURPOSE: Creates a data connection whenever a LIST, GET or PUT command
* is entered by client.
*/
void create_data_connect(char command[], char ip[], char portnum[]){
int sock_data;
struct sockaddr_in inf;
//create a tcp socket
if ((sock_data = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("\nError in socket()");
exit(1);
}
//construct the struct
memset(&inf, 0, sizeof(inf)); /* Clear struct */
inf.sin_family = AF_INET; /* Internet/IP */
inf.sin_addr.s_addr = inet_addr(ip); /* IP address */
//here we use the port number from the control connection + 1
inf.sin_port = htons(atoi(portnum) + 1); /* server port */
//connect to the server
if (connect(sock_data, (struct sockaddr *) &inf, sizeof(inf)) < 0) {
perror("\nError in connect()");
exit(1);
}
//if we issued a PUT command
if(strncmp("PUT", command, 3) == 0){
process_put(command, sock_data);
}
else if(strncmp("GET", command, 3) == 0){
process_get(command, sock_data);
}
//we issued a LIST command
else if(strncmp("LIST", command, 4) == 0){
process_list(sock_data);
}
else{
fprintf(stderr, "\nNot a valid command.\n\n");
}
}
int main(int argc, char *argv[]) {
int sock_control;
boolean has_quit = 0;
struct sockaddr_in address;
if (argc != 3) {
fprintf(stderr, "USAGE: a2client <ip_address> <port_number>\n");
exit(1);
}
//create a tcp socket
if ((sock_control = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("\nError in socket()");
exit(1);
}
//construct the struct
memset(&address, 0, sizeof(address)); /* Clear struct */
address.sin_family = AF_INET; /* Internet/IP */
address.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */
address.sin_port = htons(atoi(argv[2])); /* server port */
//connect to the server
if (connect(sock_control, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("\nError in connect()");
exit(1);
}
//keep getting input from client until they enter QUIT command
//these input from client will be sent as commands to server
while(has_quit == FALSE){
char command[MAX_LENGTH];
char command_temp[MAX_LENGTH];
fgets(command, MAX_LENGTH, stdin); //get input from user
strcat(command, "\r\n");
strcpy(command_temp, command);
boolean has_write = TRUE;
//if doing a PUT command, check if the file in your directory does in fact exist
//if the file does not exists on our (client) directory, do not bother sending
//this PUT command to the server
//(since on a PUT, the server actually makes an empty file in its directory assuming that
//it is going to get data to put in it)
if(strncmp("PUT", command, 3) == 0){
has_write = check_directory(command_temp);
}
if(has_write == TRUE){
//send command to server
if(write(sock_control, command, strlen(command)) < 0){
perror("\nError in write()");
exit(1);
}
char response[MAX_LENGTH];
int n;
//read response from server
if((n = read(sock_control, response, MAX_LENGTH-1))){
response[n] = '\0';
fprintf(stderr, "%s", response);
}
//if user enters GET, LIST or PUT
//will not make a data connection if a response is anything other
//than 103 Command okay
if(strncmp("103", response, 3) == 0){
//if the command is ok, the client will connect to the server to form
//a tcp data connection
create_data_connect(command, argv[1], argv[2]);
}
if(strncmp("QUIT", command, 4) == 0){
has_quit = TRUE;
}
}
}
//when client enters QUIT we quit here
printf("\n\nProcessing complete\n\n");
exit(0);
}