2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <event2/event.h>
50 #include <trp_route.h>
51 #include <trp_internal.h>
53 #include <trp_ptable.h>
54 #include <trp_rtable.h>
55 #include <tr_config.h>
61 /* data for event callbacks */
62 struct tr_trps_event_cookie {
68 /* callback to schedule event to process messages */
69 static void tr_trps_mq_cb(TR_MQ *mq, void *arg)
71 struct event *mq_ev=(struct event *)arg;
72 event_active(mq_ev, 0, 0);
75 static void msg_free_helper(void *p)
77 tr_msg_free_decoded((TR_MSG *)p);
80 static void tr_free_name_helper(void *arg)
82 tr_free_name((TR_NAME *)arg);
85 /* takes a TR_MSG and puts it in a TR_MQ_MSG for processing by the main thread */
86 static TRP_RC tr_trps_msg_handler(TRPS_INSTANCE *trps,
90 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
91 TR_MQ_MSG *mq_msg=NULL;
93 /* n.b., conn is available here, but do not hold onto the reference
94 * because it may be cleaned up if the originating connection goes
95 * down before the message is processed */
96 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_MSG_RECEIVED, TR_MQ_PRIO_NORMAL);
100 tr_mq_msg_set_payload(mq_msg, (void *)tr_msg, msg_free_helper);
101 trps_mq_add(trps, mq_msg);
102 talloc_free(tmp_ctx); /* cleans up the message if it did not get appended correctly */
107 static int tr_trps_gss_handler(gss_name_t client_name, gss_buffer_t gss_name,
110 struct tr_trps_event_cookie *cookie=(struct tr_trps_event_cookie *)cookie_in;
111 TRPS_INSTANCE *trps = cookie->trps;
112 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
113 TR_NAME name={gss_name->value, gss_name->length};
115 tr_debug("tr_trps_gss_handler()");
117 if ((!client_name) || (!trps) || (!cfg_mgr)) {
118 tr_debug("tr_trps_gss_handler: Bad parameters.");
122 /* look up the TRPS peer matching the GSS name */
123 if (NULL==trps_get_peer_by_gssname(trps, &name)) {
124 tr_warning("tr_trps_gss_handler: Connection attempt from unknown peer (GSS name: %.*s).", name.len, name.buf);
128 tr_debug("Client's GSS Name: %.*s", name.len, name.buf);
132 /* data passed to thread */
133 struct trps_thread_data {
134 TRP_CONNECTION *conn;
137 /* thread to handle GSS connections from peers */
138 static void *tr_trps_thread(void *arg)
140 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
141 struct trps_thread_data *thread_data=talloc_get_type_abort(arg, struct trps_thread_data);
142 TRP_CONNECTION *conn=thread_data->conn;
143 TRPS_INSTANCE *trps=thread_data->trps;
146 tr_debug("tr_trps_thread: started");
147 if (trps_authorize_connection(trps, conn)!=TRP_SUCCESS)
150 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPS_CONNECTED, TR_MQ_PRIO_HIGH);
151 tr_mq_msg_set_payload(msg, (void *)tr_dup_name(trp_connection_get_peer(conn)), tr_free_name_helper);
153 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
156 trps_mq_add(trps, msg); /* steals msg context */
159 trps_handle_connection(trps, conn);
162 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPS_DISCONNECTED, TR_MQ_PRIO_HIGH);
163 tr_mq_msg_set_payload(msg, (void *)conn, NULL); /* do not pass a free routine */
165 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
167 trps_mq_add(trps, msg);
168 tr_debug("tr_trps_thread: exit");
169 talloc_free(tmp_ctx);
173 /* called when a connection to the TRPS port is received */
174 static void tr_trps_event_cb(int listener, short event, void *arg)
176 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
177 TRPS_INSTANCE *trps = talloc_get_type_abort(arg, TRPS_INSTANCE); /* aborts on wrong type */
178 TRP_CONNECTION *conn=NULL;
179 TR_NAME *gssname=NULL;
181 struct trps_thread_data *thread_data=NULL;
183 if (0==(event & EV_READ)) {
184 tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
186 /* create a thread to handle this connection */
187 name = talloc_asprintf(tmp_ctx, "trustrouter@%s", trps->hostname);
190 gssname=tr_new_name(name); /* name cleaned up with tmp_ctx */
192 conn=trp_connection_accept(tmp_ctx, listener, gssname);
194 /* need to monitor this fd and trigger events when read becomes possible */
195 thread_data=talloc(conn, struct trps_thread_data);
196 if (thread_data==NULL) {
197 tr_err("tr_trps_event_cb: unable to allocate trps_thread_data");
200 thread_data->conn=conn;
201 thread_data->trps=trps;
202 trps_add_connection(trps, conn); /* remember the connection - this puts conn and the thread data in trps's talloc context */
203 pthread_create(trp_connection_get_thread(conn), NULL, tr_trps_thread, thread_data);
208 talloc_free(tmp_ctx);
211 static void tr_trps_cleanup_conn(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
213 /* everything belonging to the thread is in the TRP_CONNECTION
214 * associated with it */
215 tr_debug("tr_trps_cleanup_conn: freeing %p", conn);
216 pthread_join(*trp_connection_get_thread(conn), NULL);
217 trps_remove_connection(trps, conn);
218 trp_connection_free(conn);
219 tr_debug("tr_trps_cleanup_conn: deleted connection");
222 static void tr_trps_cleanup_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
224 pthread_join(*trp_connection_get_thread(trpc_get_conn(trpc)), NULL);
225 trps_remove_trpc(trps, trpc);
227 tr_debug("tr_trps_cleanup_trpc: deleted connection");
231 * Get a dynamically allocated string with a description of the route table.
232 * Caller must free the string using talloc_free().
234 * @param memctx talloc context for the string
235 * @param trps trps instance containing the route table
236 * @return pointer to the output, or NULL on error
238 static char *tr_trps_route_table_to_str(TALLOC_CTX *memctx, TRPS_INSTANCE *trps)
240 return trp_rtable_to_str(memctx, trps->rtable, " | ", NULL);
244 * Get a dynamically allocated string with a description of the community table.
245 * Caller must free the string using talloc_free().
247 * @param memctx talloc context for the string
248 * @param trps trps instance containing the community table
249 * @return pointer to the output, or NULL on error
251 static char *tr_trps_comm_table_to_str(TALLOC_CTX *memctx, TRPS_INSTANCE *trps)
253 return tr_comm_table_to_str(memctx, trps->ctable);
257 * Event handler to process TRP messages from connection threads. These
258 * are added to the message queue (mq) in tr_trps_msg_handler(), which
259 * runs in the other threads.
261 * @param socket Ignored
262 * @param event Ignored
263 * @param arg Pointer to the TRPS_INSTANCE
265 static void tr_trps_process_mq(int socket, short event, void *arg)
267 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
270 TRP_PEER *peer = NULL;
273 msg=trps_mq_pop(trps);
275 s=tr_mq_msg_get_message(msg);
276 if (0==strcmp(s, TR_MQMSG_TRPS_CONNECTED)) {
277 TR_NAME *peer_gssname=(TR_NAME *)tr_mq_msg_get_payload(msg);
278 peer=trps_get_peer_by_gssname(trps, peer_gssname); /* get the peer record */
279 tmp = tr_name_strdup(peer_gssname); /* get the name as a null-terminated string */
281 tr_err("tr_trps_process_mq: incoming connection from unknown peer (%s) reported.", tmp);
283 trp_peer_set_incoming_status(peer, PEER_CONNECTED);
284 tr_notice("tr_trps_process_mq: incoming connection from %s established.", tmp);
288 else if (0==strcmp(s, TR_MQMSG_TRPS_DISCONNECTED)) {
289 TRP_CONNECTION *conn=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TRP_CONNECTION);
290 TR_NAME *peer_gssname=trp_connection_get_peer(conn);
291 peer=trps_get_peer_by_gssname(trps, peer_gssname); /* get the peer record */
292 tmp = tr_name_strdup(peer_gssname); /* get the name as a null-terminated string */
294 tr_err("tr_trps_process_mq: incoming connection from unknown peer (%.*s) lost.", tmp);
296 trp_peer_set_incoming_status(peer, PEER_DISCONNECTED);
297 tr_trps_cleanup_conn(trps, conn);
298 tr_notice("tr_trps_process_mq: incoming connection from %s lost.", tmp);
302 else if (0==strcmp(s, TR_MQMSG_TRPC_CONNECTED)) {
303 TR_NAME *svcname=(TR_NAME *)tr_mq_msg_get_payload(msg);
304 peer=trps_get_peer_by_servicename(trps, svcname);
305 tmp = tr_name_strdup(svcname);
307 tr_err("tr_trps_process_mq: outgoing connection to unknown peer (%s) reported.", tmp);
309 trp_peer_set_outgoing_status(peer, PEER_CONNECTED);
310 tr_notice("tr_trps_process_mq: outgoing connection to %s established.", tmp);
314 else if (0==strcmp(s, TR_MQMSG_TRPC_DISCONNECTED)) {
315 TRPC_INSTANCE *trpc=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TRPC_INSTANCE);
316 TR_NAME *svcname=trpc_get_gssname(trpc);
317 peer=trps_get_peer_by_servicename(trps, svcname);
318 tmp = tr_name_strdup(svcname);
320 tr_err("tr_trps_process_mq: outgoing connection to unknown peer (%s) lost.", tmp);
322 trp_peer_set_outgoing_status(peer, PEER_DISCONNECTED);
323 tr_notice("tr_trps_process_mq: outgoing connection to %s lost.", tmp);
324 tr_trps_cleanup_trpc(trps, trpc);
329 else if (0==strcmp(s, TR_MQMSG_MSG_RECEIVED)) {
330 if (trps_handle_tr_msg(trps, tr_mq_msg_get_payload(msg))!=TRP_SUCCESS)
331 tr_notice("tr_trps_process_mq: error handling message.");
334 tr_notice("tr_trps_process_mq: unknown message '%s' received.", tr_mq_msg_get_message(msg));
337 msg=trps_mq_pop(trps);
341 static void tr_trps_update(int listener, short event, void *arg)
343 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
344 TRPS_INSTANCE *trps=cookie->trps;
345 struct event *ev=cookie->ev;
347 tr_debug("tr_trps_update: sending scheduled route/community updates.");
348 trps_update(trps, TRP_UPDATE_SCHEDULED);
349 event_add(ev, &(trps->update_interval));
350 tr_debug("tr_trps_update: update interval=%d", trps->update_interval.tv_sec);
353 static void tr_trps_sweep(int listener, short event, void *arg)
355 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
356 TRPS_INSTANCE *trps=cookie->trps;
357 struct event *ev=cookie->ev;
358 char *table_str=NULL;
360 tr_debug("tr_trps_sweep: sweeping routes.");
361 trps_sweep_routes(trps);
362 tr_debug("tr_trps_sweep: sweeping communities.");
363 trps_sweep_ctable(trps);
364 table_str=tr_trps_route_table_to_str(NULL, trps);
365 if (table_str!=NULL) {
367 talloc_free(table_str);
370 table_str=tr_trps_comm_table_to_str(NULL, trps);
371 if (table_str!=NULL) {
373 talloc_free(table_str);
375 /* schedule the event to run again */
376 event_add(ev, &(trps->sweep_interval));
379 static void tr_connection_update(int listener, short event, void *arg)
381 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
382 TRPS_INSTANCE *trps=cookie->trps;
383 struct event *ev=cookie->ev;
385 tr_debug("tr_connection_update: checking peer connections.");
386 tr_connect_to_peers(trps, ev);
387 /* schedule the event to run again */
388 event_add(ev, &(trps->connect_interval));
391 static int tr_trps_events_destructor(void *obj)
393 TR_TRPS_EVENTS *ev=talloc_get_type_abort(obj, TR_TRPS_EVENTS);
395 event_free(ev->mq_ev);
396 if (ev->connect_ev!=NULL)
397 event_free(ev->connect_ev);
398 if (ev->update_ev!=NULL)
399 event_free(ev->update_ev);
400 if (ev->sweep_ev!=NULL)
401 event_free(ev->sweep_ev);
404 static TR_TRPS_EVENTS *tr_trps_events_new(TALLOC_CTX *mem_ctx)
406 TR_TRPS_EVENTS *ev=talloc(mem_ctx, TR_TRPS_EVENTS);
408 ev->listen_ev=talloc(ev, struct tr_socket_event);
413 if (ev->listen_ev==NULL) {
417 talloc_set_destructor((void *)ev, tr_trps_events_destructor);
423 static void tr_trps_events_free(TR_TRPS_EVENTS *ev)
428 /* Configure the trps instance and set up its event handler.
429 * Fills in trps_ev, which should be allocated by caller. */
430 TRP_RC tr_trps_event_init(struct event_base *base, TR_INSTANCE *tr)
432 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
433 struct tr_socket_event *listen_ev=NULL;
434 struct tr_trps_event_cookie *trps_cookie=NULL;
435 struct tr_trps_event_cookie *connection_cookie=NULL;
436 struct tr_trps_event_cookie *update_cookie=NULL;
437 struct tr_trps_event_cookie *sweep_cookie=NULL;
438 struct timeval zero_time={0,0};
439 TRP_RC retval=TRP_ERROR;
442 if (tr->events != NULL) {
443 tr_notice("tr_trps_event_init: tr->events was not null. Freeing before reallocating..");
444 tr_trps_events_free(tr->events);
447 tr->events=tr_trps_events_new(tmp_ctx);
448 if (tr->events == NULL) {
449 tr_debug("tr_trps_event_init: unable to allocate event handles.");
454 /* get convenient handles */
455 listen_ev=tr->events->listen_ev;
457 /* Create the cookie for callbacks. It will end up part of the trps context, so it will
458 * be cleaned up when trps is freed by talloc_free. */
459 trps_cookie=talloc(tr->events, struct tr_trps_event_cookie);
460 if (trps_cookie == NULL) {
461 tr_debug("tr_trps_event_init: Unable to allocate trps_cookie.");
463 tr_trps_events_free(tr->events);
467 trps_cookie->trps=tr->trps;
468 trps_cookie->cfg_mgr=tr->cfg_mgr;
470 /* get a trps listener */
471 listen_ev->n_sock_fd=trps_get_listener(tr->trps,
474 tr->cfg_mgr->active->internal->hostname,
475 tr->cfg_mgr->active->internal->trps_port,
479 if (listen_ev->n_sock_fd==0) {
480 tr_crit("Error opening TRP server socket.");
482 tr_trps_events_free(tr->events);
487 /* Set up events for the sockets */
488 for (ii=0; ii<listen_ev->n_sock_fd; ii++) {
489 listen_ev->ev[ii]=event_new(base,
490 listen_ev->sock_fd[ii],
494 event_add(listen_ev->ev[ii], NULL);
497 /* now set up message queue processing event, only triggered by
499 tr->events->mq_ev=event_new(base,
504 tr_mq_set_notify_cb(tr->trps->mq, tr_trps_mq_cb, tr->events->mq_ev);
506 /* now set up the peer connection timer event */
507 connection_cookie=talloc(tr->events, struct tr_trps_event_cookie);
508 if (connection_cookie == NULL) {
509 tr_debug("tr_trps_event_init: Unable to allocate connection_cookie.");
511 tr_trps_events_free(tr->events);
515 connection_cookie->trps=tr->trps;
516 connection_cookie->cfg_mgr=tr->cfg_mgr;
517 tr->events->connect_ev=event_new(base, -1, EV_TIMEOUT, tr_connection_update, (void *)connection_cookie);
518 connection_cookie->ev=tr->events->connect_ev; /* in case it needs to frob the event */
519 /* The first time, do this immediately. Thereafter, it will retrigger every trps->connect_interval */
520 event_add(tr->events->connect_ev, &zero_time);
522 /* now set up the route update timer event */
523 update_cookie=talloc(tr->events, struct tr_trps_event_cookie);
524 if (update_cookie == NULL) {
525 tr_debug("tr_trps_event_init: Unable to allocate update_cookie.");
527 tr_trps_events_free(tr->events);
531 update_cookie->trps=tr->trps;
532 update_cookie->cfg_mgr=tr->cfg_mgr;
533 tr->events->update_ev=event_new(base, -1, EV_TIMEOUT, tr_trps_update, (void *)update_cookie);
534 update_cookie->ev=tr->events->update_ev; /* in case it needs to frob the event */
535 event_add(tr->events->update_ev, &(tr->trps->update_interval));
537 /* now set up the route table sweep timer event */
538 sweep_cookie=talloc(tr->events, struct tr_trps_event_cookie);
539 if (sweep_cookie == NULL) {
540 tr_debug("tr_trps_event_init: Unable to allocate sweep_cookie.");
542 tr_trps_events_free(tr->events);
546 sweep_cookie->trps=tr->trps;
547 sweep_cookie->cfg_mgr=tr->cfg_mgr;
548 tr->events->sweep_ev=event_new(base, -1, EV_TIMEOUT, tr_trps_sweep, (void *)sweep_cookie);
549 sweep_cookie->ev=tr->events->sweep_ev; /* in case it needs to frob the event */
550 event_add(tr->events->sweep_ev, &(tr->trps->sweep_interval));
552 talloc_steal(tr, tr->events);
556 talloc_free(tmp_ctx);
560 /* data passed to thread */
561 struct trpc_thread_data {
567 * Thread for handling TRPC (outgoing) connections
569 * Opens a connection to a peer. If successful, notifies the trps thread by
570 * posting a TR_MQMSG_TRPC_CONNECTED message to the trps message queue.
571 * It then waits for messages on trpc->mq. Normally these will be TR_MQMSG_TRPC_SEND
572 * messages, which this thread forwards to the peer. If its connection is lost or
573 * a TR_MQMSG_ABORT message is received on trpc->mq, the thread sends a
574 * TR_MQMSG_TRPC_DISCONNECTED message to the trps thread, then cleans up and
577 * The trps may continue queueing messages for this client even when the
578 * connection is down. To prevent the queue from growing endlessly, this thread
579 * should clear its queue after failed connection attempts.
581 static void *tr_trpc_thread(void *arg)
583 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
584 struct trpc_thread_data *thread_data=talloc_get_type_abort(arg, struct trpc_thread_data);
585 TRPC_INSTANCE *trpc=thread_data->trpc;
586 TRPS_INSTANCE *trps=thread_data->trps;
589 const char *msg_type=NULL;
590 char *encoded_msg=NULL;
591 TR_NAME *peer_gssname=NULL;
592 struct timespec wait_until = {0};
595 tr_debug("tr_trpc_thread: started");
597 /* Try to make the outgoing connection */
598 rc=trpc_connect(trpc);
599 if (rc!=TRP_SUCCESS) {
600 tr_notice("tr_trpc_thread: failed to initiate connection to %s:%d.",
601 trpc_get_server(trpc),
602 trpc_get_port(trpc));
603 trpc_mq_clear(trpc); /* clear the queue even though we did not connect */
605 /* Retrieve the GSS name used by the peer for authentication */
606 peer_gssname=trp_connection_get_peer(trpc_get_conn(trpc));
607 if (peer_gssname==NULL) {
608 tr_err("tr_trpc_thread: could not duplicate peer_gssname.");
609 talloc_free(tmp_ctx);
612 tr_debug("tr_trpc_thread: connected to peer %.*s",
613 peer_gssname->len, peer_gssname->buf);
615 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_CONNECTED, TR_MQ_PRIO_HIGH);
616 tr_mq_msg_set_payload(msg, (void *)tr_dup_name(peer_gssname), tr_free_name_helper);
618 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
619 talloc_free(tmp_ctx);
622 trps_mq_add(trps, msg); /* steals msg context */
625 /* Loop until we get an abort message or until the connection is lost. */
627 /* Wait up to 10 minutes for a message to be queued to send to the peer.
628 * Log a warning if we go longer than that, but don't give up. */
629 if (tr_mq_pop_timeout(10 * 60, &wait_until) != 0) {
630 tr_err("tr_trpc_thread: unable to set abort timeout");
631 break; /* immediately exit the loop, don't go through cleanup */
634 /* Pop a message from the queue. */
635 msg = trpc_mq_pop(trpc, &wait_until);
637 msg_type = tr_mq_msg_get_message(msg);
638 if (0 == strcmp(msg_type, TR_MQMSG_ABORT)) {
639 tr_debug("tr_trpc_thread: received abort message from main thread.");
641 } else if (0 == strcmp(msg_type, TR_MQMSG_TRPC_SEND)) {
642 encoded_msg = tr_mq_msg_get_payload(msg);
643 if (encoded_msg == NULL)
644 tr_notice("tr_trpc_thread: null outgoing TRP message.");
646 rc = trpc_send_msg(trpc, encoded_msg);
647 if (rc == TRP_SUCCESS) {
648 tr_debug("tr_trpc_thread: sent message.");
650 tr_notice("tr_trpc_thread: trpc_send_msg failed.");
651 /* Assume this means we lost the connection. */
656 tr_notice("tr_trpc_thread: unknown message '%s' received.", msg_type);
660 tr_warning("tr_trpc_thread: no outgoing messages to %.*s for 10 minutes",
661 peer_gssname->len, peer_gssname->buf);
666 /* Send a DISCONNECTED message to the main thread */
667 tr_debug("tr_trpc_thread: notifying main thread of disconnection.");
668 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_DISCONNECTED, TR_MQ_PRIO_NORMAL);
669 tr_mq_msg_set_payload(msg, (void *)trpc, NULL); /* do not pass a free routine */
671 /* can't notify main thread */
672 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
674 trps_mq_add(trps, msg);
677 talloc_free(tmp_ctx);
678 tr_debug("tr_trpc_thread: thread terminating.");
682 /* convert an IDP realm into routing table entries. Outputs number in *n_routes */
683 static TRP_ROUTE **tr_make_local_routes(TALLOC_CTX *mem_ctx,
688 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
690 TRP_ROUTE *new_entry=NULL;
691 TRP_ROUTE **entries=NULL;
692 size_t n_comms=0, ii=0;
696 if ((realm==NULL) || (realm->origin!=TR_REALM_LOCAL))
700 for (comm=realm->apcs, n_comms=0; comm!=NULL; comm=comm->next,n_comms++) {}
702 entries=talloc_array(tmp_ctx, TRP_ROUTE *, n_comms);
703 for (comm=realm->apcs,ii=0; comm!=NULL; comm=comm->next, ii++) {
704 new_entry=trp_route_new(entries);
705 if (new_entry==NULL) {
706 tr_crit("tr_make_local_routes: unable to allocate entry.");
707 talloc_free(entries);
710 trp_route_set_comm(new_entry, tr_dup_name(comm->id));
711 trp_route_set_realm(new_entry, tr_dup_name(realm->realm_id));
712 trp_route_set_peer(new_entry, tr_new_name("")); /* no peer, it's us */
713 trp_route_set_metric(new_entry, 0);
714 trp_route_set_trust_router(new_entry, tr_new_name(trust_router));
715 trp_route_set_next_hop(new_entry, tr_new_name(""));
716 trp_route_set_local(new_entry, 1);
717 entries[ii]=new_entry;
720 talloc_steal(mem_ctx, entries);
723 talloc_free(tmp_ctx);
727 void tr_peer_status_change(TRP_PEER *peer, void *cookie)
729 TRPS_INSTANCE *trps=talloc_get_type_abort(cookie, TRPS_INSTANCE);
731 if (TRP_SUCCESS!=trps_wildcard_route_req(trps, trp_peer_get_servicename(peer)))
732 tr_err("tr_send_wildcard: error sending wildcard route request.");
735 /* starts a trpc thread to connect to server:port */
736 TRP_RC tr_trpc_initiate(TRPS_INSTANCE *trps, TRP_PEER *peer, struct event *ev)
738 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
739 TRPC_INSTANCE *trpc=NULL;
740 TRP_CONNECTION *conn=NULL;
741 struct trpc_thread_data *thread_data=NULL;
744 tr_debug("tr_trpc_initiate entered");
745 trpc=trpc_new(tmp_ctx);
747 tr_crit("tr_trpc_initiate: could not allocate TRPC_INSTANCE.");
751 tr_debug("tr_trpc_initiate: allocated trpc");
753 conn=trp_connection_new(trpc);
755 tr_crit("tr_trpc_initiate: could not allocate TRP_CONNECTION.");
760 trpc_set_conn(trpc, conn);
761 trpc_set_server(trpc, talloc_strdup(trpc, trp_peer_get_server(peer)));
762 trpc_set_port(trpc, trp_peer_get_port(peer));
763 trpc_set_gssname(trpc, trp_peer_dup_servicename(peer));
764 tr_debug("tr_trpc_initiate: allocated connection");
767 thread_data=talloc(trpc, struct trpc_thread_data);
768 if (thread_data==NULL) {
769 tr_crit("tr_trpc_initiate: could not allocate struct trpc_thread_data.");
773 thread_data->trpc=trpc;
774 thread_data->trps=trps;
776 trps_add_trpc(trps, trpc); /* must add before starting thread */
777 pthread_create(trp_connection_get_thread(conn), NULL, tr_trpc_thread, thread_data);
779 tr_debug("tr_trpc_initiate: started trpc thread");
783 talloc_free(tmp_ctx);
787 /* Add local routes to the route table. */
788 TRP_RC tr_add_local_routes(TRPS_INSTANCE *trps, TR_CFG *cfg)
790 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
791 TR_IDP_REALM *cur=NULL;
792 TRP_ROUTE **local_routes=NULL;
795 char *trust_router_name=talloc_asprintf(tmp_ctx, "%s:%d", cfg->internal->hostname, cfg->internal->trps_port);
797 /* determine our trust router name */
798 if (trust_router_name==NULL)
801 for (cur=cfg->ctable->idp_realms; cur!=NULL; cur=cur->next) {
802 local_routes=tr_make_local_routes(tmp_ctx, cur, trust_router_name, &n_routes);
803 for (ii=0; ii<n_routes; ii++)
804 trps_add_route(trps, local_routes[ii]);
806 talloc_free(local_routes);
811 talloc_free(tmp_ctx);
815 /* decide how often to attempt to connect to a peer */
816 static int tr_conn_attempt_due(TRPS_INSTANCE *trps, TRP_PEER *peer, struct timespec *when)
818 return 1; /* currently make an attempt every cycle */
821 /* open missing connections to peers */
822 TRP_RC tr_connect_to_peers(TRPS_INSTANCE *trps, struct event *ev)
824 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
825 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
827 struct timespec curtime={0,0};
830 if (clock_gettime(CLOCK_REALTIME, &curtime)) {
831 tr_err("tr_connect_to_peers: failed to read time.");
836 for (peer=trp_ptable_iter_first(iter, trps->ptable);
838 peer=trp_ptable_iter_next(iter))
840 if (trps_find_trpc(trps, peer)==NULL) {
841 TR_NAME *label=trp_peer_get_label(peer);
842 tr_debug("tr_connect_to_peers: %.*s missing connection.",
843 label->len, label->buf);
844 /* has it been long enough since we last tried? */
845 if (tr_conn_attempt_due(trps, peer, &curtime)) {
846 trp_peer_set_last_conn_attempt(peer, &curtime); /* we are trying again now */
847 if (tr_trpc_initiate(trps, peer, ev)!=TRP_SUCCESS) {
848 tr_err("tr_connect_to_peers: unable to initiate TRP connection to %s:%u.",
849 trp_peer_get_server(peer),
850 trp_peer_get_port(peer));
858 trp_ptable_iter_free(iter);
859 talloc_free(tmp_ctx);
864 /* Called by the config manager after a change to the active configuration.
865 * Updates configuration of objects that do not know about the config manager. */
866 void tr_config_changed(TR_CFG *new_cfg, void *cookie)
868 TR_INSTANCE *tr=talloc_get_type_abort(cookie, TR_INSTANCE);
869 TRPS_INSTANCE *trps=tr->trps;
870 char *table_str=NULL;
872 tr->cfgwatch->poll_interval.tv_sec=new_cfg->internal->cfg_poll_interval;
873 tr->cfgwatch->poll_interval.tv_usec=0;
875 tr->cfgwatch->settling_time.tv_sec=new_cfg->internal->cfg_settling_time;
876 tr->cfgwatch->settling_time.tv_usec=0;
878 /* These need to be updated */
879 tr->tids->hostname = new_cfg->internal->hostname;
880 tr->mons->hostname = new_cfg->internal->hostname;
882 /* Update the authorized monitoring gss names */
883 if (tr->mons->authorized_gss_names) {
884 tr_debug("tr_config_changed: freeing tr->mons->authorized_gss_names");
885 tr_gss_names_free(tr->mons->authorized_gss_names);
887 if (new_cfg->internal->monitoring_credentials != NULL) {
888 tr->mons->authorized_gss_names = tr_gss_names_dup(tr->mons, new_cfg->internal->monitoring_credentials);
890 tr->mons->authorized_gss_names = tr_gss_names_new(tr->mons);
892 if (tr->mons->authorized_gss_names == NULL) {
893 tr_err("tr_config_changed: Error configuring monitoring credentials");
896 trps_set_connect_interval(trps, new_cfg->internal->trp_connect_interval);
897 trps_set_update_interval(trps, new_cfg->internal->trp_update_interval);
898 trps_set_sweep_interval(trps, new_cfg->internal->trp_sweep_interval);
899 trps_set_ctable(trps, new_cfg->ctable);
900 trps_set_ptable(trps, new_cfg->peers);
901 trps_set_peer_status_callback(trps, tr_peer_status_change, (void *)trps);
902 trps_clear_rtable(trps); /* should we do this every time??? */
903 tr_add_local_routes(trps, new_cfg); /* should we do this every time??? */
904 trps_update_active_routes(trps); /* find new routes */
905 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
906 tr_print_config(new_cfg);
907 table_str=tr_trps_route_table_to_str(NULL, trps);
908 if (table_str!=NULL) {
910 talloc_free(table_str);
912 table_str=tr_trps_comm_table_to_str(NULL, trps);
913 if (table_str!=NULL) {
915 talloc_free(table_str);