-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.c
More file actions
174 lines (157 loc) · 5.06 KB
/
Copy pathcore.c
File metadata and controls
174 lines (157 loc) · 5.06 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
//
// Created by 邱俣涵 on 2021/10/4.
//
#include "core.h"
#define GREETING "220 Anonymous FTP server ready.\r\n"
#define SERVICE_UNAVAILABLE "421 Service not available, remote server has closed connection\r\n"
#define MAX_CONNECTIONS_REACHED "421 The maximum number of connections has been reached\r\n"
char rootDir[MAX_MESSAGE_SIZE] = "/tmp";
char local_ip[20];
int running = 1;//
int num_threads = 0;//Number of running threads
void* init_server(void* args) {
/** Get local ip,parse the args, create socket
* and accept connections
* If there is an error in any step, it will exit and send a signal to
* the process itself
*/
ServerParams * params=(ServerParams*)args;
int port = LISTENPORT;//default port
if(get_local_ip(local_ip) || parse_arg(params->argc, params->argv, &port, rootDir)){
return NULL;
}
int listenfd, connfd;
struct sockaddr_in addr;
pthread_t threads[MAX_CONNECTION];
User users[MAX_CONNECTION];
for (int i = 0; i < MAX_CONNECTION; i++) {
users[i].userNo = -1;
}
//create socket
if ((listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
printf("Error socket(): %s(%d)\n", strerror(errno), errno);
return NULL;
}
//set local ip and port
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(listenfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
printf("Error bind(): %s(%d)\n", strerror(errno), errno);
return NULL;
}
if (listen(listenfd, 10) == -1) {
printf("Error listen(): %s(%d)\n", strerror(errno), errno);
return NULL;
}
while (running) {
if ((connfd = accept(listenfd, NULL, NULL)) == -1) {
printf("Error accept(): %s(%d)\n", strerror(errno), errno);
continue;
}
int p;
if (num_threads < MAX_CONNECTION) {
for (p = 0; p < MAX_CONNECTION; p++) {
if (users[p].userNo == -1) {
users[p].connfd = connfd;
users[p].state = NOTLOGIN;
users[p].userNo = p;
num_threads++;
strcpy(users[p].dir, "/");
if (pthread_create(&threads[p], NULL, main_process, &users[p])) {
printf("Error pthread_create(): %s(%d)\n", strerror(errno), errno);
}
break;
}
}
} else {
send_message(connfd,MAX_CONNECTIONS_REACHED);
close(connfd);
}
}
//Send 421 to all clients
for(int i=0;i<MAX_CONNECTION;i++){
if(users[i].userNo<0)continue;
close(users[i].filefd);
fclose(users[i].fp);
send_message(users[i].connfd,SERVICE_UNAVAILABLE);
close(users[i].connfd);
}
close(listenfd);
return NULL;
}
void *main_process(void *args) {
User *user = (User *) args;
if (send_message(user->connfd, GREETING)) {
user->userNo = -1;
num_threads--;
return NULL;
}
char sentence[8192];
user->state = NOTLOGIN;
receive_message(user->connfd, sentence);
printf("%s\n", sentence);
while (handle_command(user, sentence) == 0) {
receive_message(user->connfd, sentence);
}
close(user->connfd);
num_threads--;
user->userNo = -1;
return NULL;
}
int handle_command(User *user, char *sentence) {
char command[5];
memcpy(command, sentence, 4 * sizeof(char));
command[4] = '\0';
if (strcmp(command, "LIST") == 0) {
return handle_LIST(user, sentence);
}
if (strcmp(command, "PWD\r") == 0) {
return handle_PWD(user, sentence);
}
if (strcmp(command, "PORT") == 0) {
return handle_PORT(user, sentence);
}
if (strcmp(command, "PASV") == 0) {
return handle_PASV(user, sentence);
}
if (strcmp(command, "RETR") == 0) {
return handle_RETR(user, sentence);
}
if (strcmp(command, "TYPE") == 0) {
return handle_TYPE(user, sentence);
}
if (strcmp(command, "STOR") == 0) {
return handle_STOR(user, sentence);
}
if (strcmp(command, "QUIT") == 0 || strcmp(command, "ABOR") == 0) {
return handle_QUIT(user, sentence);
}
if (strcmp(command, "MKD ") == 0) {
return handle_MKD(user, sentence);
}
if (strcmp(command, "CWD ") == 0) {
return handle_CWD(user, sentence);
}
if (strcmp(command, "RMD ") == 0) {
return handle_RMD(user, sentence);
}
if (strcmp(command, "RNTO") == 0) {
return handle_RNTO(user, sentence);
}
if (strcmp(command, "RNFR") == 0) {
return handle_RNFR(user, sentence);
}
if (strcmp(command, "USER") == 0) {
return handle_USER(user, sentence);
}
if (strcmp(command, "PASS") == 0) {
return handle_PASS(user, sentence);
}
if (strcmp(command, "SYST") == 0) {
return handle_SYST(user, sentence);
}
send_message(user->connfd, SERVICE_UNAVAILABLE);
return -1;
}