-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
263 lines (184 loc) · 6.63 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "server.h"
#include "queue.h"
#include "key_list.h"
#define SERVER_DEFAULTS struct server_defaults
#define SERVER_DEFAULTS_PTR struct server_defaults *
SERVER_DEFAULTS {
int port, max_threads, wait_timeout, no_act_timeout;
} defaults = { PORT, MAX_THREADS, NO_CONN_TIMEOUT, NO_ACT_TIMEOUT };
SERVER_DEFAULTS_PTR defaults_ptr = &defaults;
#define THREAD_MANAGEMENT struct thread_management
#define THREAD_MANAGEMENT_PTR struct thread_management *
THREAD_MANAGEMENT {
int key;
pthread_t thread_id;
};
QUEUE_PTR conn_queue = NULL;
KEY_LIST_PTR thread_list = NULL;
pthread_mutex_t lock;
bool done = false;
void *client_handler(void *parameters) {
THREAD_MANAGEMENT_PTR t_data;
KEY_LIST_NODE_PTR t_ptr;
time_t last_traffic = time(NULL);
HANDLER_PARAMS_PTR p = (HANDLER_PARAMS_PTR)parameters;
struct sockaddr_in *addr_in = (struct sockaddr_in *)&p->client_address;
char *ip = inet_ntoa(addr_in->sin_addr);
t_ptr = (KEY_LIST_NODE_PTR)find_key_list_node(thread_list, p->thread_key);
if (t_ptr != NULL) {
t_data = (THREAD_MANAGEMENT_PTR)t_ptr->data;
printf("\tNew Thread (key %d) - thread_id %ld (Total threads %d)\n", t_data->key, pthread_self(), get_key_list_size(thread_list));
printf("\t\tNew connection: client ip %s, port %d\n", ip, addr_in->sin_port);
}
/* Do thread-specific work here */
while (true) {
sleep(SLEEP);
if ((time(NULL) - last_traffic) >= defaults_ptr->no_act_timeout) {
printf("\t\tNo-activity timeout - closing socket, ending thread\n");
break;
}
}
/* End thread-specific work here */
close(p->connfd);
printf("Exiting Thread (key %d) - thread_id %ld\n", t_data->key, t_data->thread_id);
free(parameters);
remove_key_list_node(thread_list, t_data->key);
pthread_detach(pthread_self());
pthread_exit(NULL);
}
int main(int argc, int *argv) {
signal(SIGINT, signal_handler);
struct sockaddr_in server_address, client_address;
int sockfd, connfd, client_size = 0;
int i = 0;
printf("Server starts!\n\n");
if (!initialise()) {
exit(1);
}
/* 1. Create socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("Socket creation failed!\n");
exit(2);
}
bzero(&server_address, sizeof(server_address));
/* 2. Bind socket to server address */
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(defaults_ptr->port);
if (bind(sockfd, (SOCKADDR_PTR)&server_address, sizeof(server_address)) != 0) {
printf("Socket bind failed!\n");
exit(3);
}
fcntl(sockfd, F_SETFL, O_NONBLOCK);
/* 3. Socket to passive mode, listen for connections */
if (listen(sockfd, 5) != 0) {
printf("Socket listen failed!\n");
exit(4);
}
printf("Listening on port %d\n", defaults_ptr->port);
/* 4. Accept connections from client */
while (!done) {
connfd = accept(sockfd, (SOCKADDR_PTR)&client_address, &client_size);
/* 5. Add the connection to the queue */
if ((connfd > 0) && (conn_queue != NULL)) {
HANDLER_PARAMS_PTR gci = get_client_info(connfd, client_address);
if (enqueue(conn_queue, gci)) {
printf("Stored client connection (%d)\n", queue_size(conn_queue));
} else {
printf("Couldn\'t store connection... Closing\n");
close(connfd);
}
}
/* 5a. Get next connection and start a thread */
if (!is_key_list_full(thread_list) && queue_size(conn_queue) > 0) {
HANDLER_PARAMS_PTR client_conn = (HANDLER_PARAMS_PTR)dequeue(conn_queue);
if (client_conn->connfd < 0) {
continue;
}
THREAD_MANAGEMENT_PTR t = (THREAD_MANAGEMENT_PTR)malloc(sizeof(THREAD_MANAGEMENT));
if (t == NULL) {
printf("Memory allocation for thread management node failed!\n");
} else {
int key = add_key_list_node(thread_list, t);
t->key = client_conn->thread_key = key;
printf("Starting thread (key %d)\n", key);
pthread_create(&t->thread_id, NULL, client_handler, client_conn);
}
}
/* 5b. Check for timed-out sockets and close them */
if ((conn_queue != NULL) && (queue_size(conn_queue) > 0)) {
process_queue(conn_queue, time_out_socket_connection);
}
}
/* 6. Close any open client sockets, wait for all threads to finish, */
printf("\nCleaning up sockets\n");
process_queue(conn_queue, cleanup_socket_connections);
destroy_queue(conn_queue);
printf("Cleaning up threads\n");
process_key_list(thread_list, cleanup_threads);
destroy_key_list(thread_list);
/* 7. Clean up server socket */
close(sockfd);
printf("\nServer stops.\n");
return 0;
}
bool cleanup_socket_connections(void *p) {
HANDLER_PARAMS_PTR client_conn = (HANDLER_PARAMS_PTR)p;
if (client_conn->connfd > 0) {
close(client_conn->connfd);
}
return false;
}
bool time_out_socket_connection(void *p) {
HANDLER_PARAMS_PTR client_conn = (HANDLER_PARAMS_PTR)p;
if (client_conn->connfd < 0) {
return false;
}
time_t now = time(NULL);
int v = now - client_conn->start;
if (v >= defaults_ptr->wait_timeout) {
printf("Timing out socket (%d) - duration %d seconds\n", client_conn->connfd, v);
close(client_conn->connfd);
client_conn->connfd = -1;
return true;
}
return false;
}
bool cleanup_threads(void *p) {
THREAD_MANAGEMENT_PTR t_ptr = (THREAD_MANAGEMENT_PTR)p;
pthread_join(t_ptr->thread_id, NULL);
return false;
}
HANDLER_PARAMS_PTR get_client_info(int connfd, struct sockaddr_in client_address) {
HANDLER_PARAMS_PTR p = (HANDLER_PARAMS_PTR)malloc(sizeof(HANDLER_PARAMS));
if (p != NULL) {
p->connfd = connfd;
p->client_address = client_address;
p->start = time(NULL);
}
return p;
}
bool initialise() {
int i;
if (pthread_mutex_init(&lock, NULL) != 0) {
printf("Mutex initialisation failed!");
return false;
}
thread_list = create_new_key_list(defaults_ptr->max_threads, &lock);
conn_queue = create_new_queue();
return true;
}
void signal_handler(int signal) {
done = true;
}