-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
303 lines (267 loc) · 5.65 KB
/
server.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
#include <wait.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/random.h>
#include <arpa/inet.h>
#include <uuid/uuid.h>
#include <sys/socket.h>
#include <stdatomic.h>
#include "util.h"
#include "server.h"
#include "protocol.h"
/* ------------------ server ---------------------- */
int server_recv_file(FILE *socket_fp)
{
return recv_remote_file(socket_fp, NULL, NULL);
}
int server_compiler(FILE *socket_fp)
{
int ret = 0;
char **params;
uint32_t params_count;
params = malloc(1024);
if(params == NULL)
{
logerr("server_compiler malloc fail\n");
return -1;
}
ret = recv_params(socket_fp, params, ¶ms_count);
if(ret != 0)
goto out1;
ret = mini_exec(params_count, params, true, -1);
if(ret != 0)
{
logerr("exec fail.\n");
ret = -1;
goto out2;
}
out2:
for (int i = 0; i < params_count; ++i)
{
free(params[i]);
}
out1:
if(params)
free(params);
return ret;
}
int server_send_file(FILE *socket_fp)
{
int ret;
char *file_name;
uint32_t file_name_len;
file_name = malloc(256);
if(file_name == NULL)
{
logerr("server_send_file malloc fail\n");
return -1;
}
if(fread_all(socket_fp, (char *)&file_name_len, 4) != 0)
{
logerr("fread_all file_name_len fail\n");
ret = -1;
goto out;
}
if(file_name_len == 0)
{
logerr("server_send_file file_name_len is 0\n");
ret = -1;
goto out;
}
if(fread_all(socket_fp, file_name, file_name_len) != 0)
{
logerr("fread_all file_name fail\n");
ret = -1;
goto out;
}
loginfo("send file:%s\n", file_name);
ret = send_remote_file(socket_fp, file_name, NULL, COMPRESS_NONE);
out:
if(file_name)
free(file_name);
return ret;
}
int server_delete_file(FILE *socket_fp)
{
int ret;
char *file_name;
uint32_t file_name_len;
file_name = malloc(256);
if(file_name == NULL)
{
logerr("server_delete_file malloc fail\n");
return -1;
}
if(fread_all(socket_fp, (char *)&file_name_len, 4) != 0)
{
logerr("delete file fail, fread_all fail\n");
ret = -1;
goto out;
}
if(file_name_len == 0)
{
logerr("delete file fail, file name len is 0.\n");
ret = -1;
goto out;
}
if(fread_all(socket_fp, file_name, file_name_len) != 0)
{
logerr("delete file fail, fread_all fail\n");
ret = -1;
goto out;
}
ret = delete_file(file_name);
logfile("delete file:%s %s.\n", file_name, ret == 0 ? "success" : "fail");
out:
if(file_name)
free(file_name);
return ret;
}
atomic_ulong client_fps[256] = {0};
void *server_new_client_run(void *data)
{
char cmd;
char cmd_ret;
uint64_t client_idx = (uint64_t)data;
FILE *client_fp = (FILE *)atomic_load(&client_fps[client_idx]);
logdebug("thread:%lu start.\n", client_idx);
while(1)
{
if(wait_start(client_fp) != 0)
{
logerr("wait_start fail, thread idx:%lu\n", client_idx);
goto out;
}
if(fread_all(client_fp, &cmd, 1) != 0)
{
logerr("fread_all fail, thread idx:%lu\n", client_idx);
goto out;
}
switch(cmd)
{
case PUSH_FILE:
logprotocol("--- thread: %lu push ---\n", client_idx);
if(server_recv_file(client_fp) == 0)
cmd_ret = CMD_OK;
else
cmd_ret = CMD_ERR;
break;
case COMPILER:
logprotocol("--- thread: %lu compiler ---\n", client_idx);
if(server_compiler(client_fp) == 0)
cmd_ret = CMD_OK;
else
cmd_ret = CMD_ERR;
break;
case PULL_FILE:
logprotocol("--- thread: %lu pull ---\n", client_idx);
if(server_send_file(client_fp) == 0)
cmd_ret = CMD_OK;
else
cmd_ret = CMD_ERR;
break;
case DELETE_FILE:
logprotocol("--- thread: %lu delete ---\n", client_idx);
if(server_delete_file(client_fp) == 0)
cmd_ret = CMD_OK;
else
cmd_ret = CMD_ERR;
break;
default:
logerr("unknow cmd:%d\n", cmd);
cmd_ret = CMD_ERR;
break;
}
fwrite_all(client_fp, &cmd_ret, 1);
fflush(client_fp);
logdebug("<-- response fflush -->\n");
}
out:
fclose(client_fp);
atomic_store(&client_fps[client_idx], 0);
loginfo("thread: %lu exit.\n", client_idx);
return NULL;
}
int server(int argc, char **argv)
{
int server_fd;
int client_fd;
char *listen_addr = "0.0.0.0";
uint16_t listen_port = 3633;
int addrlen = sizeof(struct sockaddr);
struct sockaddr_in server_addr = {0};
struct sockaddr client_addr;
pthread_t thread;
for (int i = 2; i < argc; ++i)
{
if(strcmp(argv[i], "-l") == 0)
{
i++;
listen_addr = argv[i];
continue;
}
if(strcmp(argv[i], "-p") == 0)
{
i++;
listen_port = atoi(argv[i]);
continue;
}
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(listen_port);
server_addr.sin_addr.s_addr = inet_addr(listen_addr);
loginfo("addr:%s port:%d\n", listen_addr, listen_port);
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0)
{
logerr("create socket error!\n");
exit(1);
}
if (bind(server_fd, (struct sockaddr *)&server_addr, addrlen) < 0)
{
logerr("addr:%s port:%d bind error!\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port));
exit(1);
}
if (listen(server_fd, 256) < 0)
{
logerr("listen error!\n");
exit(1);
}
while(1)
{
client_fd = accept(server_fd, &client_addr, &addrlen);
if (client_fd < 0)
{
logerr("accept error!\n");
exit(1);
}
while(1)
{
for (uint64_t i = 0; i < 256; ++i)
{
if(atomic_load(&client_fps[i]) == 0)
{
atomic_store(&client_fps[i], (uint64_t)fdopen(client_fd, "rb+"));
pthread_create(&thread, NULL, server_new_client_run, (void *)i);
goto out;
}
}
usleep(50000);
}
out:
}
close(server_fd);
return 0;
}