-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
213 lines (157 loc) · 4.76 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
#include "chatter.h"
int server_port;
int num_chat_rooms;
FILE *output;
char *config_file;
pool_t *threadpool;
void spawn_login_thread();
void *echo_thread_func(void *arg);
void pipe_handler(int signo){
error("SIGPIPE");
return;
}
void *check_user_connections_func(void *arg){
user_info_t *user;
int err;
socklen_t err_size = sizeof(err);
// getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &error_code, &error_code_size);
while(true){
lock_user_info(1);
user = user_infos;
while (user != NULL){
getsockopt(user->connfd, SOL_SOCKET, SO_ERROR, &err, &err_size);
debug("check_user_connections thread. %s socket state: %d %s", user->username, err, strerror(err));
user = user->next;
}
unlock_user_info(1);
sleep(2);
}
}
void
accept_connections()
{
int listenfd, *connfd;
int err, flag;
struct sockaddr_in listen_sa, *conn_sa;
socklen_t listen_addrlen, conn_addrlen;
char client_ip[INET_ADDRSTRLEN];
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if ((err = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int))) < 0){
error("setsockopt failed: %s",strerror(err));
}
listen_addrlen = sizeof(listen_sa);
bzero(&listen_sa,sizeof(listen_sa));
listen_sa.sin_family = AF_INET;
listen_sa.sin_port = htons(server_port);
listen_sa.sin_addr.s_addr = htonl(INADDR_ANY);
bind(listenfd,(SA *)&listen_sa,sizeof(listen_sa));
listen(listenfd,50);
// block on accept() - that's okay
while(true){
memset(client_ip,0,INET_ADDRSTRLEN);
conn_sa = calloc(1,sizeof(*conn_sa));
conn_addrlen = sizeof(conn_sa);
//despite the fact that the connection socket descriptor is just
//an int,and doesn't absolutely need to be dynamically allocated,
//I am doing so for the sake of consistency. The alternative is
//to cast to it to a void pointer which seems hacky.
connfd = malloc(sizeof(int));
*connfd = accept(listenfd,(SA *)conn_sa,&conn_addrlen);
inet4_ntop(client_ip,conn_sa->sin_addr.s_addr);
flag = 1;
err = setsockopt(*connfd,IPPROTO_TCP,TCP_NODELAY,&flag,sizeof(int));
if (err < 0){
errorw("setsockopt returned an error: %d %s",err, strerror(err));
}
infow("Accepted a client connection: %s", client_ip);
pool_queue(threadpool,login_thread_func,connfd);
}
}
/*
Initialize server's data structures, and start the echo thread.
*/
// uint16_t - minimum
// uint16_t - maximum
// uint16_t - Size of
// uint16_t - Time in
// terminating.
// thread pool size
// thread pool size
// the queue used for queuing up work.
// milliseconds for a worker thread to linger before
int th_min, th_max, th_size, th_linger;
void read_configfile(){
FILE *fp;
fp = fopen(config_file,"r");
if (fp == NULL){
error("bad config file path: %s", config_file);
exit(1);
}
fscanf(fp,"%d\n%d\n%d\n%d\n",&th_min,&th_max,&th_size,&th_linger);
printf(GRAY "Read values from config file:\nmin:%d max:%d size:%d linger:%d" KNRM "\n",th_min,th_max,th_size,th_linger);
}
void
server_init()
{
output = stdout;
read_configfile();
printf(KWHT "Chatter Server (created by Vasia Patov for 522 w/Jwong) :)" KNRM "\n\n\n");
init_user_mutexes();
init_room_mutexes();
pthread_attr_init(&echo_thread_attr);
assert(signal(SIGPIPE, pipe_handler) != SIG_ERR);
threadpool = pool_create(th_min,th_max,th_linger,NULL);
// pool_queue(threadpool,check_user_connections_func,NULL);
infow("Starting server. Currently listening on port: %d", server_port);
}
/*
Argument parsing was based on this SO post:
https://stackoverflow.com/a/24479532/3664123
*/
void parse_args(int argc, char** argv){
int opt;
char *parse_motd;
while ((opt = getopt(argc, argv, "ehn")) != -1) {
switch (opt) {
case 'h': fprintf(stderr,"%s",server_help_message); exit(EXIT_FAILURE);
case 'e': echo_mode = true; break;
case 'n': num_chat_rooms = 5; break;
default:
fprintf(stderr,"%s",server_help_message);
exit(EXIT_FAILURE);
}
}
if (num_chat_rooms && optind + 4 != argc){
fprintf(stderr,"%s",server_help_message);
exit(EXIT_FAILURE);
}
else if (!num_chat_rooms && optind + 3 != argc){
fprintf(stderr,"%s",server_help_message);
exit(EXIT_FAILURE);
}
if (num_chat_rooms){
num_chat_rooms = atoi(argv[optind++]);
}
server_port = atoi(argv[optind++]);
parse_motd = argv[optind++];
int j = 0;
for (int i = 0; i < strlen(parse_motd); i++){
if (parse_motd[i] == '\\' && parse_motd[i+1] == 'n'){
motd[j++] = '\n';
i++;
}
else {
motd[j++] = parse_motd[i];
}
}
if (server_port == 0){
fprintf(stderr,"%s",server_help_message);
exit(EXIT_FAILURE);
}
config_file = argv[optind++];
}
int main(int argc, char **argv){
parse_args(argc,argv);
server_init();
accept_connections();
}