| 1 |
/* server.c -- Server part of spdsend |
| 2 |
Author: Milan Zamazal <[email protected]> |
| 3 |
*/ |
| 4 |
|
| 5 |
/* Copyright (C) 2004 Brailcom, o.p.s. |
| 6 |
|
| 7 |
COPYRIGHT NOTICE |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify it |
| 10 |
under the terms of the GNU General Public License as published by the Free |
| 11 |
Software Foundation; either version 2, or (at your option) any later |
| 12 |
version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "spdsend.h" |
| 28 |
|
| 29 |
#ifndef USE_THREADS |
| 30 |
#define USE_THREADS 1 |
| 31 |
#endif |
| 32 |
|
| 33 |
#include <errno.h> |
| 34 |
#include <netdb.h> |
| 35 |
#include <netinet/in.h> |
| 36 |
#include <netinet/tcp.h> |
| 37 |
#if USE_THREADS |
| 38 |
#include <pthread.h> |
| 39 |
#endif |
| 40 |
#include <pwd.h> |
| 41 |
#include <signal.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <sys/socket.h> |
| 46 |
#include <sys/stat.h> |
| 47 |
#include <sys/un.h> |
| 48 |
#include <unistd.h> |
| 49 |
|
| 50 |
#ifndef HAVE_GETLINE |
| 51 |
/* |
| 52 |
* Added by Willie Walker - getline was a GNU libc extension, later adopted |
| 53 |
* in the POSIX.1-2008 standard, but not yet found on all systems. |
| 54 |
*/ |
| 55 |
ssize_t getline(char **lineptr, size_t * n, FILE * f); |
| 56 |
#endif |
| 57 |
|
| 58 |
/* Utilities */ |
| 59 |
|
| 60 |
static void system_error(const char *message) |
| 61 |
{ |
| 62 |
perror(message); |
| 63 |
exit(1); |
| 64 |
} |
| 65 |
|
| 66 |
/* Connection management */ |
| 67 |
|
| 68 |
Stream *connections; |
| 69 |
#if USE_THREADS |
| 70 |
pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 71 |
#endif |
| 72 |
|
| 73 |
static Stream get_connection(Connection_Id id) |
| 74 |
{ |
| 75 |
if (id < CONNECTION_ID_MIN || id >= CONNECTION_ID_MAX) |
| 76 |
return NONE; |
| 77 |
return connections[id]; |
| 78 |
} |
| 79 |
|
| 80 |
static void set_connection(Connection_Id id, Stream s) |
| 81 |
{ |
| 82 |
#if USE_THREADS |
| 83 |
pthread_mutex_lock(&connections_mutex); |
| 84 |
#endif |
| 85 |
connections[id] = s; |
| 86 |
#if USE_THREADS |
| 87 |
pthread_mutex_unlock(&connections_mutex); |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
static Connection_Id new_connection(Stream s) |
| 92 |
{ |
| 93 |
#if USE_THREADS |
| 94 |
pthread_mutex_lock(&connections_mutex); |
| 95 |
#endif |
| 96 |
int id; |
| 97 |
for (id = CONNECTION_ID_MIN; |
| 98 |
id < CONNECTION_ID_MAX && connections[id] != NONE; id++) ; |
| 99 |
if (id >= CONNECTION_ID_MAX) |
| 100 |
{ |
| 101 |
#if USE_THREADS |
| 102 |
pthread_mutex_unlock(&connections_mutex); |
| 103 |
#endif |
| 104 |
return NONE; |
| 105 |
} |
| 106 |
connections[id] = s; |
| 107 |
#if USE_THREADS |
| 108 |
pthread_mutex_unlock(&connections_mutex); |
| 109 |
#endif |
| 110 |
return id; |
| 111 |
} |
| 112 |
|
| 113 |
static Connection_Id do_open_connection(const char *host, int port) |
| 114 |
{ |
| 115 |
int sock = socket(AF_INET, SOCK_STREAM, 0); |
| 116 |
|
| 117 |
if (sock < 0) |
| 118 |
return NONE; |
| 119 |
|
| 120 |
{ |
| 121 |
struct sockaddr_in name; |
| 122 |
name.sin_family = AF_INET; |
| 123 |
name.sin_port = htons(port); |
| 124 |
{ |
| 125 |
struct hostent *hostinfo; |
| 126 |
hostinfo = gethostbyname(host); |
| 127 |
if (hostinfo == NULL) { |
| 128 |
close(sock); |
| 129 |
return NONE; |
| 130 |
} |
| 131 |
name.sin_addr = *(struct in_addr *)hostinfo->h_addr; |
| 132 |
} |
| 133 |
if (connect(sock, (struct sockaddr *)&name, sizeof(name)) < 0) { |
| 134 |
close(sock); |
| 135 |
return NONE; |
| 136 |
} |
| 137 |
{ |
| 138 |
int arg = 1; |
| 139 |
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &arg, |
| 140 |
sizeof(int)); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
{ |
| 145 |
Connection_Id id = new_connection(sock); |
| 146 |
if (id == NONE) |
| 147 |
close(sock); |
| 148 |
return id; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static Success do_close_connection(Connection_Id id) |
| 153 |
{ |
| 154 |
Stream c = get_connection(id); |
| 155 |
if (c == NONE) |
| 156 |
return ERROR; |
| 157 |
close(c); |
| 158 |
set_connection(id, NONE); |
| 159 |
return OK; |
| 160 |
} |
| 161 |
|
| 162 |
static Success do_send_data(Connection_Id id, Stream from, Stream to, |
| 163 |
Success(*forwarder) (Stream, Stream, bool)) |
| 164 |
{ |
| 165 |
int sock = get_connection(id); |
| 166 |
if (sock == NONE) |
| 167 |
return ERROR; |
| 168 |
if (from == NONE) |
| 169 |
from = sock; |
| 170 |
else if (to == NONE) |
| 171 |
to = sock; |
| 172 |
{ |
| 173 |
Success result = ((*forwarder) (from, to, false)); |
| 174 |
if (result != OK) |
| 175 |
do_close_connection(id); |
| 176 |
return result; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* Processing requests */ |
| 181 |
|
| 182 |
/* Protocol: |
| 183 |
|
| 184 |
Client request: |
| 185 |
First comes the action code, of the type Action. |
| 186 |
If Action is A_OPEN, the following data follows: |
| 187 |
int port, int strlen(hostname), hostname |
| 188 |
Else: |
| 189 |
Connection_Id |
| 190 |
Then, if Action is A_DATA, the SSIP lines follow. |
| 191 |
|
| 192 |
Server answer: |
| 193 |
The result code, of the type Result. |
| 194 |
If Result is OK, Connection_Id follows. |
| 195 |
Additionally, if Action is A_DATA, SSIP reply follows. |
| 196 |
*/ |
| 197 |
|
| 198 |
static Success report(Stream s, Result code) |
| 199 |
{ |
| 200 |
return write_data(s, &code, sizeof(Result)); |
| 201 |
} |
| 202 |
|
| 203 |
static Success report_ok(Stream s, Connection_Id id) |
| 204 |
{ |
| 205 |
if (report(s, OK_CODE) == OK && |
| 206 |
write_data(s, &id, sizeof(Connection_Id)) == OK) |
| 207 |
return OK; |
| 208 |
else |
| 209 |
return ERROR; |
| 210 |
} |
| 211 |
|
| 212 |
static Success report_error(Stream s) |
| 213 |
{ |
| 214 |
return report(s, ER_CODE); |
| 215 |
} |
| 216 |
|
| 217 |
static Connection_Id read_id(Stream s) |
| 218 |
{ |
| 219 |
Connection_Id id; |
| 220 |
if (read_data(s, &id, sizeof(Connection_Id)) == ERROR) |
| 221 |
return NONE; |
| 222 |
return id; |
| 223 |
} |
| 224 |
|
| 225 |
static Success forward_ssip_answer(Stream from, Stream to, bool _closep) |
| 226 |
{ |
| 227 |
int result = OK; |
| 228 |
FILE *f = fdopen(from, "r"); |
| 229 |
size_t line_size = 256; |
| 230 |
char *line = malloc(line_size); |
| 231 |
if (line == NULL) |
| 232 |
system_error("memory allocation"); |
| 233 |
|
| 234 |
while (1) { |
| 235 |
int n = getline(&line, &line_size, f); |
| 236 |
if (n < 0 || write_data(to, line, n) == ERROR) { |
| 237 |
result = ERROR; |
| 238 |
break; |
| 239 |
} |
| 240 |
if (n > 3 && line[3] == ' ') |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
free(line); |
| 245 |
return result; |
| 246 |
} |
| 247 |
|
| 248 |
static void process_open(Stream s) |
| 249 |
{ |
| 250 |
Connection_Id id; |
| 251 |
int port; |
| 252 |
int hostlen; |
| 253 |
|
| 254 |
if (read_data(s, &port, sizeof(int)) != sizeof(int)) { |
| 255 |
report_error(s); |
| 256 |
return; |
| 257 |
} |
| 258 |
if (read_data(s, &hostlen, sizeof(int)) != sizeof(int)) { |
| 259 |
report_error(s); |
| 260 |
return; |
| 261 |
} |
| 262 |
{ |
| 263 |
char *host = malloc(hostlen + 1); |
| 264 |
if (host == NULL) |
| 265 |
system_error("memory allocation"); |
| 266 |
if (read_data(s, host, hostlen) != hostlen) { |
| 267 |
free(host); |
| 268 |
report_error(s); |
| 269 |
return; |
| 270 |
} |
| 271 |
host[hostlen] = '\0'; |
| 272 |
id = do_open_connection(host, port); |
| 273 |
free(host); |
| 274 |
} |
| 275 |
|
| 276 |
if (id == NONE) |
| 277 |
report_error(s); |
| 278 |
else |
| 279 |
report_ok(s, id); |
| 280 |
} |
| 281 |
|
| 282 |
static void process_close(Stream s) |
| 283 |
{ |
| 284 |
Connection_Id id = read_id(s); |
| 285 |
if (id != NONE && do_close_connection(id) == OK) |
| 286 |
report_ok(s, id); |
| 287 |
else |
| 288 |
report_error(s); |
| 289 |
} |
| 290 |
|
| 291 |
static void process_data(Stream s) |
| 292 |
{ |
| 293 |
Connection_Id id = read_id(s); |
| 294 |
if (id != NONE) |
| 295 |
report_ok(s, id); |
| 296 |
else |
| 297 |
report_error(s); |
| 298 |
|
| 299 |
if (do_send_data(id, s, NONE, forward_data) == OK) |
| 300 |
do_send_data(id, NONE, s, forward_ssip_answer); |
| 301 |
} |
| 302 |
|
| 303 |
static void process_request(Stream s) |
| 304 |
{ |
| 305 |
Action action; |
| 306 |
|
| 307 |
if (read_data(s, &action, sizeof(Action)) == NONE) |
| 308 |
return; |
| 309 |
|
| 310 |
if (action == A_OPEN) |
| 311 |
process_open(s); |
| 312 |
else if (action == A_CLOSE) |
| 313 |
process_close(s); |
| 314 |
else if (action == A_DATA) |
| 315 |
process_data(s); |
| 316 |
else |
| 317 |
report_error(s); |
| 318 |
|
| 319 |
close(s); |
| 320 |
} |
| 321 |
|
| 322 |
#if USE_THREADS |
| 323 |
static void *process_request_thread(void *s) |
| 324 |
{ |
| 325 |
Stream s_deref = *((Stream *) s); |
| 326 |
free(s); |
| 327 |
pthread_detach(pthread_self()); |
| 328 |
process_request(s_deref); |
| 329 |
return NULL; |
| 330 |
} |
| 331 |
#endif |
| 332 |
|
| 333 |
/* Starting the server */ |
| 334 |
|
| 335 |
static const char *login_name() |
| 336 |
{ |
| 337 |
return getpwuid(getuid())->pw_name; |
| 338 |
} |
| 339 |
|
| 340 |
static const char *server_socket_name() |
| 341 |
{ |
| 342 |
char *name; |
| 343 |
if (asprintf(&name, "/tmp/spdsend-server.%s", login_name()) < 0) |
| 344 |
system_error("memory allocation"); |
| 345 |
return name; |
| 346 |
} |
| 347 |
|
| 348 |
static void serve() |
| 349 |
{ |
| 350 |
struct sockaddr_un name; |
| 351 |
int sock; |
| 352 |
size_t size; |
| 353 |
const char *filename = server_socket_name(); |
| 354 |
|
| 355 |
sock = socket(PF_LOCAL, SOCK_STREAM, 0); |
| 356 |
if (sock < 0) |
| 357 |
system_error("socket creation"); |
| 358 |
|
| 359 |
name.sun_family = AF_LOCAL; |
| 360 |
strncpy(name.sun_path, filename, sizeof(name.sun_path)); |
| 361 |
name.sun_path[sizeof(name.sun_path) - 1] = '\0'; |
| 362 |
size = (offsetof(struct sockaddr_un, sun_path) |
| 363 |
+ strlen(name.sun_path) + 1); |
| 364 |
if (bind(sock, (struct sockaddr *)&name, size) < 0) |
| 365 |
system_error("bind"); |
| 366 |
if (listen(sock, LISTEN_QUEUE_LENGTH) < 0) |
| 367 |
system_error("listen"); |
| 368 |
|
| 369 |
while (1) { |
| 370 |
struct sockaddr_un client_address; |
| 371 |
socklen_t client_address_len = sizeof(client_address); |
| 372 |
Stream *s = malloc(sizeof(Stream)); |
| 373 |
if (s == NULL) |
| 374 |
system_error("memory allocation"); |
| 375 |
*s = accept(sock, (struct sockaddr *)&client_address, |
| 376 |
&client_address_len); |
| 377 |
if (*s < 0) { |
| 378 |
free(s); |
| 379 |
break; |
| 380 |
} |
| 381 |
{ |
| 382 |
#if USE_THREADS |
| 383 |
pthread_t tid; |
| 384 |
if (pthread_create(&tid, NULL, &process_request_thread, s)) |
| 385 |
#endif |
| 386 |
{ |
| 387 |
process_request(*s); |
| 388 |
free(s); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
close(sock); |
| 393 |
} |
| 394 |
|
| 395 |
static void daemonize() |
| 396 |
{ |
| 397 |
int ret = 0; |
| 398 |
if (fork() != 0) |
| 399 |
exit(0); |
| 400 |
setsid(); |
| 401 |
signal(SIGHUP, SIG_IGN); |
| 402 |
if (fork() != 0) |
| 403 |
exit(0); |
| 404 |
if ((ret = chdir("/")) != 0) |
| 405 |
fputs("server.c:daemonize: could not chdir", stderr); |
| 406 |
exit(1); |
| 407 |
umask(0); |
| 408 |
{ |
| 409 |
int i; |
| 410 |
for (i = 0; i < 4; i++) |
| 411 |
close(i); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
static void init_connections() |
| 416 |
{ |
| 417 |
connections = malloc(CONNECTION_ID_MAX * sizeof(Connection_Id)); |
| 418 |
if (connections == NULL) |
| 419 |
system_error("memory allocation"); |
| 420 |
{ |
| 421 |
int i; |
| 422 |
for (i = CONNECTION_ID_MIN; i < CONNECTION_ID_MAX; i++) |
| 423 |
connections[i] = NONE; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
static void start_server() |
| 428 |
{ |
| 429 |
const char *socket_name = server_socket_name(); |
| 430 |
unlink(socket_name); |
| 431 |
|
| 432 |
{ |
| 433 |
int pid = fork(); |
| 434 |
if (pid == -1) |
| 435 |
system_error("fork"); |
| 436 |
|
| 437 |
if (pid == 0) { |
| 438 |
daemonize(); |
| 439 |
init_connections(); |
| 440 |
serve(); |
| 441 |
unlink(socket_name); |
| 442 |
exit(0); |
| 443 |
} else |
| 444 |
sleep(1); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
static int connect_server() |
| 449 |
{ |
| 450 |
struct sockaddr_un name; |
| 451 |
int sock = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 452 |
int name_size; |
| 453 |
if (sock < 0) |
| 454 |
return NONE; |
| 455 |
name.sun_family = AF_LOCAL; |
| 456 |
strncpy(name.sun_path, server_socket_name(), sizeof(name.sun_path)); |
| 457 |
name.sun_path[sizeof(name.sun_path) - 1] = '\0'; |
| 458 |
name_size = (offsetof(struct sockaddr_un, sun_path) |
| 459 |
+ strlen(name.sun_path) + 1); |
| 460 |
|
| 461 |
if (connect(sock, (struct sockaddr *)&name, name_size) < 0) { |
| 462 |
close(sock); |
| 463 |
return NONE; |
| 464 |
} else |
| 465 |
return sock; |
| 466 |
} |
| 467 |
|
| 468 |
/* External functions */ |
| 469 |
|
| 470 |
Stream open_server() |
| 471 |
{ |
| 472 |
Stream s; |
| 473 |
|
| 474 |
s = connect_server(); |
| 475 |
if (s == NONE) { |
| 476 |
start_server(); |
| 477 |
s = connect_server(); |
| 478 |
} |
| 479 |
if (s == NONE) |
| 480 |
return NONE; |
| 481 |
|
| 482 |
return s; |
| 483 |
} |