-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
231 lines (188 loc) · 4.69 KB
/
Copy pathserver.c
File metadata and controls
231 lines (188 loc) · 4.69 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
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
#include <stdio.h>
#include <getopt.h>
#include "internal.h"
#include "plugin_server.h"
char port[STRING_LENGTH] = { 0 };
static void help(char *name)
{
fprintf(stderr, "Usage: %s [-h, --help] [-p, --port port]\n", name);
exit(1);
}
static void options(int argc, char ** argv)
{
int opt;
int option_index;
struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{0, 0, 0, 0}
};
while (1) {
opt = getopt_long(argc, argv, "hp:",
long_options, &option_index);
if (opt == -1) { break; }
switch (opt) {
case 0:
break;
case 'p':
strncpy(port, optarg, STRING_LENGTH);
break;
case '?':
default:
help(argv[0]);
break;
}
}
}
int
main(int argc, char ** argv)
{
connection_info client;
int sock, pid, gai_error_code;
struct addrinfo *res, *resorig, hint;
char numhost[NI_MAXHOST] = { 0 };
socklen_t sz;
int optval = 1;
/* INITIALIZERS */
con_info_init(&client);
strcpy(port, DEFAULT_PORT); // loading of recommended default port
/* PROGRAM */
options(argc, argv);
memset(&hint, 0, sizeof (hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
hint.ai_flags = AI_PASSIVE;
if ((gai_error_code = getaddrinfo(NULL, port, &hint, &resorig)) != 0) {
printf("server: %s\n", gai_strerror(gai_error_code));
exit(1);
}
for (res = resorig; res != NULL; res = res->ai_next) {
if ((sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) == -1) {
freeaddrinfo(resorig);
err(1, "socket");
}
if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof (optval)) == -1)) {
freeaddrinfo(resorig);
close(sock);
err(1, "setsockopt");
}
if (bind(sock, res->ai_addr, res->ai_addrlen) == 0) {
break;
} else {
freeaddrinfo(resorig);
close(sock);
err(1, "bind");
}
if (res->ai_next == NULL) {
printf("Creation of socket and ");
printf("binding failed. Exiting...\n");
freeaddrinfo(resorig);
exit(1);
}
}
freeaddrinfo(resorig);
listen(sock, SOMAXCONN);
while (1) {
printf("Waiting for connections...\n");
sz = sizeof (client.remote_addr);
client.fdsock = accept(sock,
(struct sockaddr *)&client.remote_addr,
&sz);
getnameinfo((struct sockaddr *)&client.remote_addr, sz, numhost,
sizeof (numhost), NULL, 0, NI_NUMERICHOST);
printf("Connection from %s\n", numhost);
strncpy(client.name, numhost, STRING_LENGTH);
char lib_name[STRING_LENGTH] = { 0 };
void *lib_handle;
void (*symbol_run)(connection_info*);
/*
* Forking is just fine for server,
* because server do not have to share infos
* with other processes
*/
switch (pid = fork()) {
case -1: // error in forking
err(1, NULL);
case 0: // children process
if (handshake_server(&client) == 0) {
printf("%s: Handshake accomplished.\n",
numhost);
} else {
fprintf(stderr,
"%s: ", numhost);
fprintf(stderr,
"Client did not ");
fprintf(stderr,
"accomplish handshake!\n");
goto cleanup;
}
/* ********************************* */
/* do all the work which client asks */
/* ********************************* */
char net_ok[NET_STRING_LENGTH] =
"net_load_lib:succesfuly_loaded";
char net_err[NET_STRING_LENGTH] =
"net_load_lib:error";
while (1) {
int ret_lib_load;
char string_exit[NET_STRING_LENGTH]
= { 0 };
printf("%s: Waiting for job...\n",
numhost);
net_read(&client, &string_exit,
NET_STRING_LENGTH);
if (strcmp(string_exit,
"net_load_lib:exit")
== 0) {
break; }
// load library
if ((ret_lib_load = net_read(&client,
&lib_name,
STRING_LENGTH)) == -1) {
net_write(&client, net_err,
NET_STRING_LENGTH);
continue;
} else if (ret_lib_load == 0) {
fprintf(stderr,
"%s: ", numhost);
fprintf(stderr,
"Connection closed ");
fprintf(stderr,
"by remote machine.\n");
goto cleanup;
}
lib_handle = load_library(lib_name);
symbol_run = load_symbol(lib_handle,
"run_server");
net_write(&client, net_ok,
NET_STRING_LENGTH);
// run symbol
if (symbol_run != NULL) {
symbol_run(&client);
} else {
printf("%s: ", numhost);
printf("Repeating loading");
printf(" library...\n");
}
close_library(lib_handle);
}
close(client.fdsock);
printf("%s: Connection exited succesfuly.\n",
numhost);
exit(0);
default: // parrent process
break;
}
}
printf("Waiting for all children processes.\n");
while (wait(NULL) > 0); // wait for all children
close(sock);
return (0);
cleanup:
close(sock);
close(client.fdsock);
exit(1);
}