-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncServer.c
More file actions
256 lines (191 loc) · 6.47 KB
/
funcServer.c
File metadata and controls
256 lines (191 loc) · 6.47 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
/*
* funcServer.c
*
* Created on: Dec 5, 2012
* Author: rosyid
*/
#include "funcServer.h"
static void init_fs() {
memset(&(fs.hints), 0, sizeof(fs.hints));
fs.hints.ai_family = AF_INET;
fs.hints.ai_socktype = SOCK_STREAM;
fs.hints.ai_flags = AI_PASSIVE;
}
static void ftps_listen() {
char* addr_serv;
int x = 1;
getaddrinfo(NULL, FTPPORT, &(fs.hints), &(fs.srv_info));
fs.socket_fd = socket(fs.srv_info->ai_family, fs.srv_info->ai_socktype, fs.srv_info->ai_protocol);
setsockopt(fs.socket_fd, SOL_SOCKET, SO_REUSEADDR, &x, fs.srv_info->ai_addrlen);
bind(fs.socket_fd, fs.srv_info->ai_addr, fs.srv_info->ai_addrlen);
listen(fs.socket_fd, BACKLOG);
addr_serv = inet_ntoa((*(struct sockaddr_in*)fs.srv_info->ai_addr).sin_addr);
printf("Server STARTED!\nHOST:%s\n", addr_serv);
}
static void sigchild_handler(int x) {
while(waitpid(-1, NULL, WNOHANG) > 0);
}
static void ftps_accept() {
char* client_addr_str;
unsigned int client_addrlen;
fs.sa.sa_handler = sigchild_handler;
sigemptyset(&(fs.sa.sa_mask));
fs.sa.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &fs.sa, NULL);
connection_id = 0;
while(1) {
client_addrlen = sizeof(fs.client_addr);
fs.accsocket_fd = accept(fs.socket_fd, (struct sockaddr*) &(fs.client_addr), &client_addrlen);
client_addr_str = inet_ntoa(((struct sockaddr_in*) &fs.client_addr)->sin_addr);
connection_id++;
printf("Catch connection from %s\n", client_addr_str);
printf("ID Connection : %d\n", connection_id);
//buat proses baru untuk menghandle client baru
if(!fork()) {
close(fs.socket_fd);
ftps_handle_conn(client_addr_str);
close(fs.accsocket_fd);
printf("Connection to %s (%d) closed\n", client_addr_str, connection_id);
exit(0);
}
close(fs.accsocket_fd);
}
}
static int ftps_retrieve(char *path) {
char err[MEDIUMBUFFSIZE];
char msg[SMALLBUFFSIZE];
printf("Retrieve file : %s (%d)\n", path, connection_id);
if(ftp_file_exist(path, err) == -1) {
printf("-- FAILED [%s]\n", err);
sprintf(msg, "%s", SR501);
send(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
return ST_ERROR;
}
strcpy(msg, SR150);
send(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
int last_byte = recv(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
msg[last_byte] = '\0';
if(!strcmp(msg, "READY")) {
printf("-- Client ready to receive file\n");
strcpy(msg, SR250);
send(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
printf("-- File transfer STARTED\n");
ftp_send_file_partitioned(path, fs.accsocket_fd);
printf("-- File transfer FINISHED\n");
strcpy(msg, SR250);
send(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
}
return ST_RETR;
}
static int ftps_store(char *filename) {
char message[STDBUFFSIZE];
printf("Store file : %s (%d)\n", filename, connection_id);
strcpy(message, SR150);
send(fs.accsocket_fd, message, SMALLBUFFSIZE, 0);
int last_byte = recv(fs.accsocket_fd, message, SMALLBUFFSIZE, 0);
message[last_byte] = '\0';
if(!strcmp(message, "READY")) {
printf("-- Client ready to send file\n");
strcpy(message, SR250);
send(fs.accsocket_fd, message, SMALLBUFFSIZE, 0);
printf("-- File transfer STARTED\n");
ftp_retrieve_file_partitioned(filename, fs.accsocket_fd);
printf("-- File transfer END\n");
strcpy(message, SR250);
send(fs.accsocket_fd, message, SMALLBUFFSIZE, 0);
}
return ST_STOR;
}
static int ftps_quit(const char *client_addr) {
char msg[STDBUFFSIZE];
sprintf(msg, SR200, " [Connection to %s CLOSED!]\n", client_addr);
send(fs.accsocket_fd, msg, sizeof(msg), 0);
return ST_QUIT;
}
static int ftps_list(char *path) {
struct dirent **list;
char msg[BIGBUFFSIZE];
int i, n;
if(path == NULL) {
path = getcwd(NULL, STDBUFFSIZE);
}
printf("List on %s directory (%d)\n", path, connection_id);
n = scandir(path, &list, NULL, alphasort);
if(n < 0) {
printf("-- FAILED\n");
strcpy(msg, SR501);
send(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
return ST_ERROR;
}
printf("-- SUCCESS\n");
strcpy(msg, SR150);
send(fs.accsocket_fd, msg, SMALLBUFFSIZE, 0);
memset(&msg, 0, BIGBUFFSIZE);
sprintf(msg, "CWD: %s\n", getcwd(NULL, STDBUFFSIZE));
sprintf(msg, "%sPATH: %s\n", msg, path);
for(i = 0; i < n; i++) {
if(list[i]->d_type == DT_DIR) {
sprintf(msg, "%s%s (dir)\n", msg, list[i]->d_name);
} else {
sprintf(msg, "%s%s\n", msg, list[i]->d_name);
}
free(list[i]);
}
free(list);
//printf("--%s\n", msg);
send(fs.accsocket_fd, msg, BIGBUFFSIZE, 0);
return ST_LIST;
}
static int ftps_cwd(char *path) {
char msg[STDBUFFSIZE];
printf("CWD: %s from (%d)\n", path, connection_id);
if(chdir(path) == -1) {
strcpy(msg, SR501);
printf("-- FAILED\n");
} else {
strcpy(msg, SR200);
printf("-- SUCCESS\n");
}
send(fs.accsocket_fd, msg, STDBUFFSIZE, 0);
return ST_CWD;
}
static void ftps_parse_msg(char *client_msg, const char *client_addr, int *loop_status) {
char ** arr_msg;
ftp_tokenizer(client_msg, &arr_msg, ' ', 20);
if(!strcmp(arr_msg[0], CMD_RETR)) {
ftps_retrieve(arr_msg[1]);
} else if(!strcmp(arr_msg[0], CMD_STOR)) {
ftps_store(arr_msg[1]);
} else if(!strcmp(arr_msg[0], CMD_QUIT)) {
ftps_quit(client_addr);
} else if(!strcmp(arr_msg[0], CMD_LIST)) {
ftps_list(arr_msg[1]);
} else if(!strcmp(arr_msg[0], CMD_CWD)) {
ftps_cwd(arr_msg[1]);
}
free(arr_msg);
return;
}
static void ftps_handle_conn(const char *client_addr) {
char msg[STDBUFFSIZE];
char client_msg[STDBUFFSIZE];
int loop = 1;
int last_byte;
sprintf(msg, SR200, "| [Server connected to %s]\n", client_addr);
send(fs.accsocket_fd, msg, sizeof(msg), 0);
while(loop) {
last_byte = recv(fs.accsocket_fd, client_msg, STDBUFFSIZE, 0);
client_msg[last_byte] = '\0';
ftps_parse_msg(client_msg, client_addr, &loop);
printf("\n");
}
}
int ftps_server_main(int argc, char **argv) {
char* cwd;
cwd = ftp_getcwd(argc, argv);
printf("FTelePortation Server\n");
printf("CWD: %s\n", cwd);
init_fs();
ftps_listen();
ftps_accept();
}