-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncClient.c
More file actions
310 lines (251 loc) · 8.16 KB
/
funcClient.c
File metadata and controls
310 lines (251 loc) · 8.16 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
/*
* funcClient.c
*
* Created on: Dec 5, 2012
* Author: rosyid
*/
#include "funcClient.h"
static void init_fc() {
fc.connect_status = DISCONNECTED;
memset(&(fc.hints), 0, sizeof(fc.hints));
fc.hints.ai_family = AF_INET;
fc.hints.ai_socktype = SOCK_STREAM;
}
static int ftpc_connect(char *srv_addr) {
char res[STDBUFFSIZE];
//minta info tentang dengan siapa kita akan connect
getaddrinfo(srv_addr, FTPPORT, &(fc.hints), &(fc.srv_info));
//buat socket untuk koneksi
if((fc.socket_fd = socket(
fc.srv_info->ai_family, fc.srv_info->ai_socktype, fc.srv_info->ai_protocol))
== -1) {
printf("| ERROR! Couldn't create socket\n");
close(fc.socket_fd);
return ST_ERROR;
}
//mencoba connect
if(connect(fc.socket_fd, fc.srv_info->ai_addr, fc.srv_info->ai_addrlen) == -1) {
printf("| ERROR! Couldn't connect to server\n");
return ST_ERROR;
}
//mengambil IP address dari tujuan
fc.srv_ipaddr = inet_ntoa((*(struct sockaddr_in*) fc.srv_info->ai_addr).sin_addr);
printf("| Connecting to %s ...\n", fc.srv_ipaddr);
freeaddrinfo(fc.srv_info);
recv(fc.socket_fd, res, STDBUFFSIZE, 0);
printf("|| Message from server : %s\n", res);
res[3] = 0;
if(!strcmp(res, SR200)) {
printf("| CONNECTED\n");
fc.connect_status = CONNECTED;
}
return ST_CONN;
}
static int ftpc_sendsrvmsg(const char *msg,
int msg_size, char *res, int res_size) {
int lb;
if(send(fc.socket_fd, msg, msg_size, 0) == -1) {
printf("| ERROR! Sending server message error!\n");
return -1;
}
if((lb = recv(fc.socket_fd, res, res_size, 0)) == -1) {
printf("| ERROR! receiving server message error!\n");
return -1;
}
res[lb] = '\0';
return 0;
}
static int ftpc_retrieve(char *path) {
if(fc.connect_status == DISCONNECTED) {
printf("| ERROR! No active CONNECTION\n");
return ST_ERROR;
}
char filename[STDBUFFSIZE];
char result[STDBUFFSIZE];
char message[STDBUFFSIZE];
sprintf(message, "%s %s", CMD_RETR, path);
ftpc_sendsrvmsg(message, STDBUFFSIZE, result, STDBUFFSIZE);
result[3] = '\0';
printf("|| Message from server : %s\n", result);
if(!strcmp(result, SR150)) {
ftp_get_filename_from_path(path, filename);
send(fc.socket_fd, "READY", SMALLBUFFSIZE, 0);
recv(fc.socket_fd, message, SMALLBUFFSIZE, 0);
printf("|| Message from server : %s\n", result);
printf("| Begin retrieving file ..\n");
ftp_retrieve_file_partitioned(filename, fc.socket_fd);
printf("| End retrieving file ..\n");
recv(fc.socket_fd, message, SMALLBUFFSIZE, 0);
printf("|| Message from server : %s\n", result);
}
return ST_RETR;
}
static int ftpc_store(char *path) {
char result[STDBUFFSIZE];
char message[STDBUFFSIZE];
char err_msg[STDBUFFSIZE];
char tmp[STDBUFFSIZE];
if(fc.connect_status == DISCONNECTED) {
printf("| ERROR! No active CONNECTION\n");
return ST_ERROR;
}
if(ftp_file_exist(path, err_msg) == -1) {
printf("| ERROR! %s\n", err_msg);
return ST_ERROR;
}
strcpy(tmp, path);
ftp_get_filename_from_path(path, tmp);
memset(&message, 0, sizeof(message));
sprintf(message, "%s %s", CMD_STOR, tmp);
ftpc_sendsrvmsg(message, STDBUFFSIZE, result, STDBUFFSIZE);
printf("|| Message from server : %s\n", result);
if(!strcmp(result, SR150)) {
send(fc.socket_fd, "READY", SMALLBUFFSIZE, 0);
recv(fc.socket_fd, message, SMALLBUFFSIZE, 0);
printf("|| Message from server : %s\n", result);
printf("| Begin sending file ..\n");
ftp_send_file_partitioned(path, fc.socket_fd);
printf("| End sending file ..\n");
recv(fc.socket_fd, message, SMALLBUFFSIZE, 0);
printf("|| Message from server : %s\n", result);
}
return ST_STOR;
}
static int ftpc_quit() {
if(fc.connect_status == DISCONNECTED) {
printf("| ERROR! No active CONNECTION\n");
return ST_ERROR;
}
char msg[] = CMD_QUIT;
char res[MEDIUMBUFFSIZE];
ftpc_sendsrvmsg(msg, sizeof(msg), res, sizeof(msg));
printf("|| Message from server : %s\n", res);
res[3] = '\0';
if(!strcmp(res, SR200)) {
close(fc.socket_fd);
fc.connect_status = DISCONNECTED;
printf("| DISCONNECTED!\n");
}
return ST_QUIT;
}
static int ftpc_list(char *path) {
char msg[STDBUFFSIZE];
char res[BIGBUFFSIZE];
int last_byte;
if(fc.connect_status == DISCONNECTED) {
printf("| ERROR! No active CONNECTION\n");
return ST_ERROR;
}
memset(&msg, 0, STDBUFFSIZE);
if(path != NULL) {
sprintf(msg, "%s %s", CMD_LIST, path);
} else {
sprintf(msg, CMD_LIST);
}
ftpc_sendsrvmsg(msg, SMALLBUFFSIZE, res, SMALLBUFFSIZE);
printf("|| Message from server : %s\n", res);
res[3] = '\0';
if(!strcmp(res, SR150)) {
memset(&res, 0, BIGBUFFSIZE);
last_byte = recv(fc.socket_fd, res, BIGBUFFSIZE, 0);
res[last_byte] = '\0';
printf("%s\n", res);
} else {
printf("| ERROR while listing content on server\n");
}
return ST_LIST;
}
static int ftpc_cd(char* path) {
if(chdir(path) == -1) {
printf("| ERROR while changing directory to %s\n", path);
return ST_ERROR;
}
printf("| Directory changed to %s\n", path);
printf("| Current Working Directory : %s\n", getcwd(NULL, STDBUFFSIZE));
return ST_CD;
}
static int ftpc_cwd(char* path) {
if(fc.connect_status == DISCONNECTED) {
printf("| ERROR! No active CONNECTION\n");
return ST_ERROR;
}
char msg[STDBUFFSIZE];
char res[STDBUFFSIZE];
memset(&msg, 0, sizeof(msg));
sprintf(msg, "%s %s", CMD_CWD, path);
ftpc_sendsrvmsg(msg, sizeof(msg), res, sizeof(res));
printf("|| Message from server : %s\n", res);
res[3] = '\0';
if (!strcmp(res, SR200)) {
printf("| Server's working directory sucessfully changed to %s\n", path);
} else {
printf("| ERROR! Couldn't change server working directory to %s\n", path);
printf("| Invalid path or permission denied.\n");
}
return ST_CWD;
}
static void ftpc_parse_command(char *command, int *loop_status) {
char** arr_cmd;
int arr_cmd_len;
arr_cmd_len = ftp_tokenizer(command, &arr_cmd, ' ', 10);
if(!strcmp(arr_cmd[0], CMD_CONN)) {
if(arr_cmd_len == 2) {
ftpc_connect(arr_cmd[1]);
} else {
printf("| Argument not valid\n| CONN <ip-address>\n");
}
} else if(!strcmp(arr_cmd[0], CMD_QUIT)) {
ftpc_quit();
} else if(!strcmp(arr_cmd[0], CMD_RETR)) {
if(arr_cmd_len == 2) {
ftpc_retrieve(arr_cmd[1]);
} else {
printf("| Argument not valid\n| RETR <file-path>\n");
}
} else if(!strcmp(arr_cmd[0], CMD_STOR)) {
if(arr_cmd_len == 2) {
ftpc_store(arr_cmd[1]);
} else {
printf("| Argument not valid\n| STOR <file-path>\n");
}
} else if(!strcmp(arr_cmd[0], CMD_LIST)) {
if(arr_cmd_len == 2) {
ftpc_list(arr_cmd[1]);
} else {
ftpc_list(NULL);
}
} else if(!strcmp(arr_cmd[0], CMD_CWD)) {
if(arr_cmd_len == 2) {
ftpc_cwd(arr_cmd[1]);
} else {
printf("| Argument not valid\n| CWD <path>\n");
}
} else if(!strcmp(arr_cmd[0], CMD_CD)) {
if(arr_cmd_len == 2) {
ftpc_cd(arr_cmd[1]);
} else {
printf("| Argument not valid\n| CD <path>\n");
}
} else if(!strcmp(arr_cmd[0], CMD_SHUTDOWN)) {
if(fc.connect_status == CONNECTED) {
ftpc_quit();
}
*loop_status = 0;
} else {
printf("| Command undefined..\n");
}
free(arr_cmd);
}
int ftpc_client_main(int argc, char** argv) {
char cmd[INPUTBUFFSIZE];
strcpy(fc.cwd, ftp_getcwd(argc, argv));
printf("FTelePortation Client\n");
printf("CWD: %s\n", fc.cwd);
init_fc();
int st = 1;
while(st) {
printf("\nftp>> ");
ftp_gets(cmd);
ftpc_parse_command(cmd, &st);
}
}