Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2013, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | #include "fdcore-internal.h" |
| 37 | |
| 38 | /* Server (listening) part of the framework */ |
| 39 | |
| 40 | static struct fd_list FD_SERVERS = FD_LIST_INITIALIZER(FD_SERVERS); /* The list of all server objects */ |
| 41 | /* We don't need to protect this list, it is only accessed from the main framework thread. */ |
| 42 | |
| 43 | enum s_state { |
| 44 | NOT_CREATED=0, |
| 45 | RUNNING, |
| 46 | TERMINATED, |
| 47 | ERROR /* an error occurred, this is not a valid status */ |
| 48 | }; |
| 49 | |
| 50 | /* Servers information */ |
| 51 | struct server { |
| 52 | struct fd_list chain; /* link in the FD_SERVERS list */ |
| 53 | |
| 54 | struct cnxctx * conn; /* server connection context (listening socket) */ |
| 55 | int proto; /* IPPROTO_TCP or IPPROTO_SCTP */ |
| 56 | int secur; /* TLS is started immediatly after connection ? 0: no; 1: RFU; 2: yes (TLS/TCP or TLS/SCTP) */ |
| 57 | |
| 58 | pthread_t thr; /* The thread waiting for new connections (will store the data in the clients fifo) */ |
| 59 | enum s_state state; /* state of the thread */ |
| 60 | |
| 61 | struct fifo *pending; /* FIFO of struct cnxctx */ |
| 62 | struct pool_workers { |
| 63 | struct server * s; /* pointer to the parent server structure */ |
| 64 | int id; /* The worker id for logs */ |
| 65 | pthread_t worker; /* The thread */ |
| 66 | } *workers; /* array of cnf_thr_srv items */ |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | /* Micro functions to read/change the status thread-safely */ |
| 71 | static pthread_mutex_t s_lock = PTHREAD_MUTEX_INITIALIZER; |
| 72 | static enum s_state get_status(struct server * s) |
| 73 | { |
| 74 | enum s_state r; |
| 75 | CHECK_POSIX_DO( pthread_mutex_lock(&s_lock), return ERROR ); |
| 76 | r = s->state; |
| 77 | CHECK_POSIX_DO( pthread_mutex_unlock(&s_lock), return ERROR ); |
| 78 | return r; |
| 79 | } |
| 80 | static void set_status(struct server * s, enum s_state st) |
| 81 | { |
| 82 | CHECK_POSIX_DO( pthread_mutex_lock(&s_lock), return ); |
| 83 | s->state = st; |
| 84 | CHECK_POSIX_DO( pthread_mutex_unlock(&s_lock), return ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /* dump one item of the server->pending fifo */ |
| 89 | static DECLARE_FD_DUMP_PROTOTYPE(dump_cnx, void * item) { |
| 90 | struct cnxctx * c = item; |
| 91 | CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, " '%s'", fd_cnx_getid(c)), return NULL); |
| 92 | return *buf; |
| 93 | } |
| 94 | |
| 95 | /* Dump all servers information */ |
| 96 | DECLARE_FD_DUMP_PROTOTYPE(fd_servers_dump, int details) |
| 97 | { |
| 98 | struct fd_list * li; |
| 99 | |
| 100 | FD_DUMP_HANDLE_OFFSET(); |
| 101 | |
| 102 | for (li = FD_SERVERS.next; li != &FD_SERVERS; li = li->next) { |
| 103 | struct server * s = (struct server *)li; |
| 104 | enum s_state st = get_status(s); |
| 105 | |
| 106 | if (details) { |
| 107 | CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "{server}(@%p)'%s': %s, %s(%d), %s", s, fd_cnx_getid(s->conn), |
| 108 | IPPROTO_NAME( s->proto ), |
| 109 | s->secur ? "Secur" : "NotSecur", s->secur, |
| 110 | (st == NOT_CREATED) ? "Thread not created" : |
| 111 | ((st == RUNNING) ? "Thread running" : |
| 112 | ((st == TERMINATED) ? "Thread terminated" : |
| 113 | "Thread status unknown"))), return NULL); |
| 114 | /* Dump the client list of this server */ |
| 115 | CHECK_MALLOC_DO( fd_fifo_dump(FD_DUMP_STD_PARAMS, "pending connections", s->pending, dump_cnx), return NULL ); |
| 116 | |
| 117 | if (li->next != &FD_SERVERS) { |
| 118 | CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "\n"), return NULL); |
| 119 | } |
| 120 | } else { |
| 121 | CHECK_MALLOC_DO( fd_dump_extend( FD_DUMP_STD_PARAMS, "'%s'(%s,%s) ", fd_cnx_getid(s->conn), |
| 122 | IPPROTO_NAME( s->proto ), s->secur ? "Secur" : "NotSecur"), return NULL); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return *buf; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* The thread in the pool for handling new clients connecting to a server */ |
| 131 | static void * client_worker(void * arg) |
| 132 | { |
| 133 | struct pool_workers * pw = arg; |
| 134 | struct server * s = pw->s; |
| 135 | struct cnxctx * c = NULL; |
| 136 | int fatal = 0; |
| 137 | struct timespec ts; |
| 138 | struct fd_cnx_rcvdata rcv_data; |
| 139 | struct fd_msg_pmdl * pmdl = NULL; |
| 140 | struct msg * msg = NULL; |
| 141 | struct msg_hdr *hdr = NULL; |
| 142 | struct fd_pei pei; |
| 143 | |
| 144 | TRACE_ENTRY("%p", arg); |
| 145 | |
| 146 | /* Set the thread name */ |
| 147 | { |
| 148 | char buf[48]; |
| 149 | snprintf(buf, sizeof(buf), "Worker#%d[%s%s]", pw->id, IPPROTO_NAME(s->proto), s->secur?", Sec" : ""); |
| 150 | fd_log_threadname ( buf ); |
| 151 | } |
| 152 | |
| 153 | /* Loop until canceled / error */ |
| 154 | next_client: |
| 155 | LOG_A("Ready to process next incoming connection"); |
| 156 | |
| 157 | memset(&rcv_data, 0, sizeof(rcv_data)); |
| 158 | |
| 159 | /* Get the next connection */ |
| 160 | CHECK_FCT_DO( fd_fifo_get( s->pending, &c ), { fatal = 1; goto cleanup; } ); |
| 161 | |
| 162 | /* Handshake if we are a secure server port, or start clear otherwise */ |
| 163 | if (s->secur) { |
| 164 | LOG_D("Starting handshake with %s", fd_cnx_getid(c)); |
| 165 | |
| 166 | int ret = fd_cnx_handshake(c, GNUTLS_SERVER, (s->secur == 1) ? ALGO_HANDSHAKE_DEFAULT : ALGO_HANDSHAKE_3436, NULL, NULL); |
| 167 | if (ret != 0) { |
| 168 | char buf[1024]; |
| 169 | snprintf(buf, sizeof(buf), "TLS handshake failed for connection '%s', connection closed.", fd_cnx_getid(c)); |
| 170 | |
| 171 | fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, NULL, buf, NULL); |
| 172 | |
| 173 | goto cleanup; |
| 174 | } |
| 175 | } else { |
| 176 | CHECK_FCT_DO( fd_cnx_start_clear(c, 0), goto cleanup ); |
| 177 | } |
| 178 | |
| 179 | /* Set the timeout to receive the first message */ |
| 180 | CHECK_SYS_DO( clock_gettime(CLOCK_REALTIME, &ts), { fatal = 1; goto cleanup; } ); |
| 181 | ts.tv_sec += INCNX_TIMEOUT; |
| 182 | |
| 183 | /* Receive the first Diameter message on the connection -- cleanup in case of timeout */ |
| 184 | CHECK_FCT_DO( fd_cnx_receive(c, &ts, &rcv_data.buffer, &rcv_data.length), |
| 185 | { |
| 186 | char buf[1024]; |
| 187 | |
| 188 | switch (__ret__) { |
| 189 | case ETIMEDOUT: |
| 190 | snprintf(buf, sizeof(buf), "Client '%s' did not send CER within %ds, connection aborted.", fd_cnx_getid(c), INCNX_TIMEOUT); |
| 191 | fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, NULL, buf, NULL); |
| 192 | break; |
| 193 | |
| 194 | case ENOTCONN: |
| 195 | snprintf(buf, sizeof(buf), "Connection from '%s' in error before CER was received.", fd_cnx_getid(c)); |
| 196 | fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, NULL, buf, NULL); |
| 197 | break; |
| 198 | |
| 199 | default: |
| 200 | snprintf(buf, sizeof(buf), "Connection from '%s': unspecified error, connection aborted.", fd_cnx_getid(c)); |
| 201 | fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, NULL, buf, NULL); |
| 202 | } |
| 203 | goto cleanup; |
| 204 | } ); |
| 205 | |
| 206 | TRACE_DEBUG(FULL, "Received %zdb from new client '%s'", rcv_data.length, fd_cnx_getid(c)); |
| 207 | |
| 208 | pmdl = fd_msg_pmdl_get_inbuf(rcv_data.buffer, rcv_data.length); |
| 209 | |
| 210 | /* Try parsing this message */ |
| 211 | CHECK_FCT_DO( fd_msg_parse_buffer( &rcv_data.buffer, rcv_data.length, &msg ), |
| 212 | { /* Parsing failed */ |
| 213 | fd_hook_call(HOOK_MESSAGE_PARSING_ERROR, NULL, NULL, &rcv_data, pmdl ); |
| 214 | goto cleanup; |
| 215 | } ); |
| 216 | |
| 217 | /* Log incoming message */ |
| 218 | fd_hook_associate(msg, pmdl); |
| 219 | fd_hook_call(HOOK_MESSAGE_RECEIVED, msg, NULL, fd_cnx_getid(c), fd_msg_pmdl_get(msg)); |
| 220 | |
| 221 | /* We expect a CER, it must parse with our dictionary and rules */ |
| 222 | CHECK_FCT_DO( fd_msg_parse_rules( msg, fd_g_config->cnf_dict, &pei ), |
| 223 | { /* Parsing failed -- trace details */ |
| 224 | char buf[1024]; |
| 225 | |
| 226 | fd_hook_call(HOOK_MESSAGE_PARSING_ERROR, msg, NULL, pei.pei_message ?: pei.pei_errcode, fd_msg_pmdl_get(msg)); |
| 227 | |
| 228 | snprintf(buf, sizeof(buf), "Error parsing CER from '%s', connection aborted.", fd_cnx_getid(c)); |
| 229 | fd_hook_call(HOOK_PEER_CONNECT_FAILED, NULL, NULL, buf, NULL); |
| 230 | |
| 231 | goto cleanup; |
| 232 | } ); |
| 233 | |
| 234 | /* Now check we received a CER */ |
| 235 | CHECK_FCT_DO( fd_msg_hdr ( msg, &hdr ), { fatal = 1; goto cleanup; } ); |
| 236 | CHECK_PARAMS_DO( (hdr->msg_appl == 0) && (hdr->msg_flags & CMD_FLAG_REQUEST) && (hdr->msg_code == CC_CAPABILITIES_EXCHANGE), |
| 237 | { /* Parsing failed -- trace details */ |
| 238 | char buf[1024]; |
| 239 | snprintf(buf, sizeof(buf), "Expected CER from '%s', received a different message, connection aborted.", fd_cnx_getid(c)); |
| 240 | fd_hook_call(HOOK_PEER_CONNECT_FAILED, msg, NULL, buf, NULL); |
| 241 | goto cleanup; |
| 242 | } ); |
| 243 | |
| 244 | /* Finally, pass the information to the peers module which will handle it in a separate thread */ |
| 245 | pthread_cleanup_push((void *)fd_cnx_destroy, c); |
| 246 | pthread_cleanup_push((void *)fd_msg_free, msg); |
| 247 | CHECK_FCT_DO( fd_peer_handle_newCER( &msg, &c ), ); |
| 248 | pthread_cleanup_pop(0); |
| 249 | pthread_cleanup_pop(0); |
| 250 | |
| 251 | cleanup: |
| 252 | /* Cleanup the parsed message if any */ |
| 253 | if (msg) { |
| 254 | CHECK_FCT_DO( fd_msg_free(msg), /* continue */); |
| 255 | msg = NULL; |
| 256 | } |
| 257 | |
| 258 | /* Close the connection if needed */ |
| 259 | if (c != NULL) { |
| 260 | fd_cnx_destroy(c); |
| 261 | c = NULL; |
| 262 | } |
| 263 | |
| 264 | /* Cleanup the received buffer if any */ |
| 265 | free(rcv_data.buffer); |
| 266 | |
| 267 | |
| 268 | if (!fatal) |
| 269 | goto next_client; |
| 270 | |
| 271 | LOG_E("Worker thread exiting."); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | /* The thread managing a server */ |
| 276 | static void * serv_th(void * arg) |
| 277 | { |
| 278 | struct server *s = (struct server *)arg; |
| 279 | |
| 280 | CHECK_PARAMS_DO(s, goto error); |
| 281 | fd_log_threadname ( fd_cnx_getid(s->conn) ); |
| 282 | |
| 283 | set_status(s, RUNNING); |
| 284 | |
| 285 | /* Accept incoming connections */ |
| 286 | CHECK_FCT_DO( fd_cnx_serv_listen(s->conn), goto error ); |
| 287 | |
| 288 | do { |
| 289 | struct cnxctx * conn = NULL; |
| 290 | |
| 291 | /* Wait for a new client or cancel */ |
| 292 | CHECK_MALLOC_DO( conn = fd_cnx_serv_accept(s->conn), break ); |
| 293 | |
| 294 | /* Store this connection in the fifo for processing by the worker pool. Will block when the fifo is full */ |
| 295 | pthread_cleanup_push((void *)fd_cnx_destroy, conn); |
| 296 | CHECK_FCT_DO( fd_fifo_post( s->pending, &conn ), break ); |
| 297 | pthread_cleanup_pop(0); |
| 298 | |
| 299 | } while (1); |
| 300 | error: |
| 301 | if (s) |
| 302 | set_status(s, TERMINATED); |
| 303 | |
| 304 | /* Send error signal to the core */ |
| 305 | LOG_F( "An error occurred in server module! Thread is terminating..."); |
| 306 | CHECK_FCT_DO(fd_core_shutdown(), ); |
| 307 | |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /* Create a new server structure */ |
| 313 | static struct server * new_serv( int proto, int secur ) |
| 314 | { |
| 315 | struct server * new; |
| 316 | int i; |
| 317 | |
| 318 | /* New server structure */ |
| 319 | CHECK_MALLOC_DO( new = malloc(sizeof(struct server)), return NULL ); |
| 320 | |
| 321 | memset(new, 0, sizeof(struct server)); |
| 322 | fd_list_init(&new->chain, new); |
| 323 | new->proto = proto; |
| 324 | new->secur = secur; |
| 325 | |
| 326 | CHECK_FCT_DO( fd_fifo_new(&new->pending, 5), return NULL); |
| 327 | CHECK_MALLOC_DO( new->workers = calloc( fd_g_config->cnf_thr_srv, sizeof(struct pool_workers) ), return NULL ); |
| 328 | |
| 329 | for (i = 0; i < fd_g_config->cnf_thr_srv; i++) { |
| 330 | /* Create the pool */ |
| 331 | new->workers[i].s = new; |
| 332 | new->workers[i].id = i; |
| 333 | CHECK_POSIX_DO( pthread_create( &new->workers[i].worker, NULL, client_worker, &new->workers[i]), return NULL ); |
| 334 | } |
| 335 | |
| 336 | return new; |
| 337 | } |
| 338 | |
| 339 | /* Start all the servers */ |
| 340 | int fd_servers_start() |
| 341 | { |
| 342 | struct server * s; |
| 343 | |
| 344 | int empty_conf_ep = FD_IS_LIST_EMPTY(&fd_g_config->cnf_endpoints); |
| 345 | |
| 346 | /* SCTP */ |
| 347 | if (!fd_g_config->cnf_flags.no_sctp) { |
| 348 | #ifdef DISABLE_SCTP |
| 349 | ASSERT(0); |
| 350 | #else /* DISABLE_SCTP */ |
| 351 | |
| 352 | /* Create the server on unsecure port */ |
| 353 | if (fd_g_config->cnf_port) { |
| 354 | CHECK_MALLOC( s = new_serv(IPPROTO_SCTP, 0) ); |
| 355 | CHECK_MALLOC( s->conn = fd_cnx_serv_sctp(fd_g_config->cnf_port, empty_conf_ep ? NULL : &fd_g_config->cnf_endpoints) ); |
| 356 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 357 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 358 | } |
| 359 | |
| 360 | /* Create the server on secure port */ |
| 361 | if (fd_g_config->cnf_port_tls) { |
| 362 | CHECK_MALLOC( s = new_serv(IPPROTO_SCTP, 2 /* Change when DTLS is introduced */) ); |
| 363 | CHECK_MALLOC( s->conn = fd_cnx_serv_sctp(fd_g_config->cnf_port_tls, empty_conf_ep ? NULL : &fd_g_config->cnf_endpoints) ); |
| 364 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 365 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 366 | } |
| 367 | |
| 368 | /* Create the other server on 3436 secure port */ |
| 369 | /*if (fd_g_config->cnf_port_3436) { |
| 370 | CHECK_MALLOC( s = new_serv(IPPROTO_SCTP, 2) ); |
| 371 | CHECK_MALLOC( s->conn = fd_cnx_serv_sctp(fd_g_config->cnf_port_3436, empty_conf_ep ? NULL : &fd_g_config->cnf_endpoints) ); |
| 372 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 373 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 374 | }*/ |
| 375 | |
| 376 | #endif /* DISABLE_SCTP */ |
| 377 | } |
| 378 | |
| 379 | /* TCP */ |
| 380 | if (!fd_g_config->cnf_flags.no_tcp) { |
| 381 | |
| 382 | if (empty_conf_ep) { |
| 383 | /* Bind TCP servers on [0.0.0.0] */ |
| 384 | if (!fd_g_config->cnf_flags.no_ip4) { |
| 385 | |
| 386 | if (fd_g_config->cnf_port) { |
| 387 | CHECK_MALLOC( s = new_serv(IPPROTO_TCP, 0) ); |
| 388 | CHECK_MALLOC( s->conn = fd_cnx_serv_tcp(fd_g_config->cnf_port, AF_INET, NULL) ); |
| 389 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 390 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 391 | } |
| 392 | |
| 393 | if (fd_g_config->cnf_port_tls) { |
| 394 | CHECK_MALLOC( s = new_serv(IPPROTO_TCP, 1) ); |
| 395 | CHECK_MALLOC( s->conn = fd_cnx_serv_tcp(fd_g_config->cnf_port_tls, AF_INET, NULL) ); |
| 396 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 397 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* Bind TCP servers on [::] */ |
| 402 | if (!fd_g_config->cnf_flags.no_ip6) { |
| 403 | |
| 404 | if (fd_g_config->cnf_port) { |
| 405 | CHECK_MALLOC( s = new_serv(IPPROTO_TCP, 0) ); |
| 406 | CHECK_MALLOC( s->conn = fd_cnx_serv_tcp(fd_g_config->cnf_port, AF_INET6, NULL) ); |
| 407 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 408 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 409 | } |
| 410 | |
| 411 | if (fd_g_config->cnf_port_tls) { |
| 412 | CHECK_MALLOC( s = new_serv(IPPROTO_TCP, 1) ); |
| 413 | CHECK_MALLOC( s->conn = fd_cnx_serv_tcp(fd_g_config->cnf_port_tls, AF_INET6, NULL) ); |
| 414 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 415 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 416 | } |
| 417 | } |
| 418 | } else { |
| 419 | /* Create all endpoints -- check flags */ |
| 420 | struct fd_list * li; |
| 421 | for (li = fd_g_config->cnf_endpoints.next; li != &fd_g_config->cnf_endpoints; li = li->next) { |
| 422 | struct fd_endpoint * ep = (struct fd_endpoint *)li; |
| 423 | sSA * sa = (sSA *) &ep->ss; |
| 424 | if (! (ep->flags & EP_FL_CONF)) |
| 425 | continue; |
| 426 | if (fd_g_config->cnf_flags.no_ip4 && (sa->sa_family == AF_INET)) |
| 427 | continue; |
| 428 | if (fd_g_config->cnf_flags.no_ip6 && (sa->sa_family == AF_INET6)) |
| 429 | continue; |
| 430 | |
| 431 | if (fd_g_config->cnf_port) { |
| 432 | CHECK_MALLOC( s = new_serv(IPPROTO_TCP, 0) ); |
| 433 | CHECK_MALLOC( s->conn = fd_cnx_serv_tcp(fd_g_config->cnf_port, sa->sa_family, ep) ); |
| 434 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 435 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 436 | } |
| 437 | |
| 438 | if (fd_g_config->cnf_port_tls) { |
| 439 | CHECK_MALLOC( s = new_serv(IPPROTO_TCP, 1) ); |
| 440 | CHECK_MALLOC( s->conn = fd_cnx_serv_tcp(fd_g_config->cnf_port_tls, sa->sa_family, ep) ); |
| 441 | fd_list_insert_before( &FD_SERVERS, &s->chain ); |
| 442 | CHECK_POSIX( pthread_create( &s->thr, NULL, serv_th, s ) ); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /* Now, if we had an empty list of local adresses (no address configured), try to read the real addresses from the kernel */ |
| 449 | if (empty_conf_ep) { |
| 450 | CHECK_FCT(fd_cnx_get_local_eps(&fd_g_config->cnf_endpoints)); |
| 451 | if (FD_IS_LIST_EMPTY(&fd_g_config->cnf_endpoints)) { |
| 452 | TRACE_DEBUG(INFO, "Unable to find the address(es) of the local system. " |
| 453 | "Please use \"ListenOn\" parameter in the configuration. " |
| 454 | "This information is required to generate the CER/CEA messages."); |
| 455 | return EINVAL; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | { |
| 460 | char * buf = NULL; |
| 461 | size_t len = 0, offset = 0; |
| 462 | CHECK_MALLOC_DO( fd_dump_extend( &buf, &len, &offset , "Local server address(es): "), ); |
| 463 | CHECK_MALLOC_DO( fd_ep_dump( &buf, &len, &offset, 0, 0, &fd_g_config->cnf_endpoints ), ); |
| 464 | LOG_N("%s", buf ?: "Error dumping addresses"); |
| 465 | free(buf); |
| 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* Terminate all the servers */ |
| 471 | int fd_servers_stop() |
| 472 | { |
| 473 | TRACE_ENTRY(""); |
| 474 | |
| 475 | TRACE_DEBUG(INFO, "Shutting down server sockets..."); |
| 476 | |
| 477 | /* Loop on all servers */ |
| 478 | while (!FD_IS_LIST_EMPTY(&FD_SERVERS)) { |
| 479 | struct server * s = (struct server *)(FD_SERVERS.next); |
| 480 | int i; |
| 481 | struct cnxctx * c; |
| 482 | |
| 483 | /* cancel thread */ |
| 484 | CHECK_FCT_DO( fd_thr_term(&s->thr), /* continue */); |
| 485 | |
| 486 | /* destroy server connection context */ |
| 487 | fd_cnx_destroy(s->conn); |
| 488 | |
| 489 | /* cancel and destroy all worker threads */ |
| 490 | for (i = 0; i < fd_g_config->cnf_thr_srv; i++) { |
| 491 | /* Destroy worker thread */ |
| 492 | CHECK_FCT_DO( fd_thr_term(&s->workers[i].worker), /* continue */); |
| 493 | } |
| 494 | free(s->workers); |
| 495 | |
| 496 | /* Close any pending connection */ |
| 497 | while ( fd_fifo_tryget( s->pending, &c ) == 0 ) { |
| 498 | fd_cnx_destroy(c); |
| 499 | } |
| 500 | CHECK_FCT_DO( fd_fifo_del(&s->pending), ); |
| 501 | |
| 502 | /* Now destroy the server object */ |
| 503 | fd_list_unlink(&s->chain); |
| 504 | free(s); |
| 505 | } |
| 506 | |
| 507 | /* We're done! */ |
| 508 | return 0; |
| 509 | } |