-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
161 lines (137 loc) · 4.61 KB
/
server.cpp
File metadata and controls
161 lines (137 loc) · 4.61 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
#include <stdio.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <net/if.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <sched.h>
#include <signal.h>
#include <iostream>
#include <vector>
#include "msg.hpp"
using namespace std;
#define BUFF_SIZE 1024UL
#define MAX_CLIENT 16384
#define PORT 8787
#define ERR_EXIT(a){ perror(a); exit(1); }
bool is_sigpipe = false;
void pipe_handler(int signum){
is_sigpipe = true;
}
int main(int argc, char *argv[]){
int server_sockfd, client_sockfd, write_byte;
int server_port;
struct sockaddr_in server_addr, client_addr;
int client_addr_len = sizeof(client_addr);
string buffer(2 * BUFF_SIZE, ' ');
if (argc < 2){
printf("argument error\n");
return 0;
}
server_port = atoi(argv[1]);
// Create directory
string folder = "./b08902040_server_folder";
if (mkdir(folder.c_str(), 0700) < 0)
// ERR_EXIT("mkdir failed\n");
;
chdir(folder.c_str());
// Avoid path traversal
if (unshare(CLONE_NEWUSER) < 0)
ERR_EXIT("unshare failed\n");
if (chroot("./") < 0)
ERR_EXIT("chroot failed\n");
// Get socket file descriptor
if((server_sockfd = socket(AF_INET , SOCK_STREAM , 0)) < 0){
ERR_EXIT("socket failed\n")
}
// Set server address information
bzero(&server_addr, sizeof(server_addr)); // erase the data
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(server_port);
// Bind the server file descriptor to the server address
if(bind(server_sockfd, (struct sockaddr *)&server_addr , sizeof(server_addr)) < 0){
ERR_EXIT("bind failed\n");
}
// Listen on the server file descriptor
if(listen(server_sockfd , 3) < 0){ // Is 3 enough ???
ERR_EXIT("listen failed\n");
}
// Setup fds
fd_set read_fds, read_fds_sl;
fd_set write_fds, write_fds_sl;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
int max_nfds = server_sockfd;
FD_SET(server_sockfd, &write_fds);
FD_SET(server_sockfd, &read_fds);
// Handle client msgs
vector<Socket> clients(MAX_CLIENT);
for (int i = 0; i != clients.size(); ++i)
clients[i].fd = i;
// Setup signal handler
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = pipe_handler;
sigaction(SIGPIPE, &act, nullptr);
while (true){
read_fds_sl = read_fds;
write_fds_sl = write_fds;
if (select(max_nfds + 1, &read_fds_sl, &write_fds_sl, NULL, NULL) < 0)
ERR_EXIT("select failed\n");
for (int fd = 0; fd <= max_nfds; ++fd){
is_sigpipe = false;
if (FD_ISSET(fd, &read_fds_sl)){
if (fd == server_sockfd){
int new_fd = accept(server_sockfd, (struct sockaddr *)&client_addr, (socklen_t*)&client_addr_len);
if (new_fd < 0)
ERR_EXIT("accept failed\n");
// Log
cout << "A new connection from sockfd: " << new_fd << endl;
FD_SET(new_fd, &read_fds);
clients[new_fd] = Socket(new_fd);
max_nfds = (new_fd > max_nfds) ? new_fd : max_nfds;
}
else{
int ret = clients[fd].recv_all();
if (ret < 0 || is_sigpipe){
FD_CLR(fd, &read_fds);
close(fd);
clients[fd] = Socket(fd);
}
if (ret > 0)
clients[fd].process();
if (clients[fd].do_write){
FD_CLR(fd, &read_fds);
FD_SET(fd, &write_fds);
}
}
}
else if (FD_ISSET(fd, &write_fds_sl)){
int ret = clients[fd].send_all();
if (ret < 0 || is_sigpipe){
FD_CLR(fd, &write_fds);
close(fd);
clients[fd] = Socket(fd);
}
else if (clients[fd].msg_ok()){
clients[fd].do_write = false;
clients[fd].init();
FD_CLR(fd, &write_fds);
FD_SET(fd, &read_fds);
}
}
}
}
// Note
// 1. How to detemine when to client sockfd
// 2. string(c_buf): bug?
return 0;
}