4 #include <event2/event.h>
16 #include <trp_internal.h>
17 #include <trp_ptable.h>
18 #include <trp_rtable.h>
19 #include <tr_config.h>
25 /* data for event callbacks */
26 struct tr_trps_event_cookie {
32 /* callback to schedule event to process messages */
33 static void tr_trps_mq_cb(TR_MQ *mq, void *arg)
35 struct event *mq_ev=(struct event *)arg;
36 event_active(mq_ev, 0, 0);
39 static void msg_free_helper(void *p)
41 tr_msg_free_decoded((TR_MSG *)p);
44 static void tr_free_name_helper(void *arg)
46 tr_free_name((TR_NAME *)arg);
49 /* takes a TR_MSG and puts it in a TR_MQ_MSG for processing by the main thread */
50 static TRP_RC tr_trps_msg_handler(TRPS_INSTANCE *trps,
54 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
55 TR_MQ_MSG *mq_msg=NULL;
57 /* n.b., conn is available here, but do not hold onto the reference
58 * because it may be cleaned up if the originating connection goes
59 * down before the message is processed */
60 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_MSG_RECEIVED, TR_MQ_PRIO_NORMAL);
64 tr_mq_msg_set_payload(mq_msg, (void *)tr_msg, msg_free_helper);
65 trps_mq_add(trps, mq_msg);
66 talloc_free(tmp_ctx); /* cleans up the message if it did not get appended correctly */
71 static int tr_trps_gss_handler(gss_name_t client_name, gss_buffer_t gss_name,
74 struct tr_trps_event_cookie *cookie=(struct tr_trps_event_cookie *)cookie_in;
75 TRPS_INSTANCE *trps = cookie->trps;
76 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
77 TR_NAME name={gss_name->value, gss_name->length};
79 tr_debug("tr_trps_gss_handler()");
81 if ((!client_name) || (!gss_name) || (!trps) || (!cfg_mgr)) {
82 tr_debug("tr_trps_gss_handler: Bad parameters.");
86 /* look up the TRPS peer matching the GSS name */
87 if (NULL==trps_get_peer_by_gssname(trps, &name)) {
88 tr_warning("tr_trps_gss_handler: Connection attempt from unknown peer (GSS name: %.*s).", name.len, name.buf);
92 tr_debug("Client's GSS Name: %.*s", name.len, name.buf);
96 /* data passed to thread */
97 struct trps_thread_data {
101 /* thread to handle GSS connections from peers */
102 static void *tr_trps_thread(void *arg)
104 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
105 struct trps_thread_data *thread_data=talloc_get_type_abort(arg, struct trps_thread_data);
106 TRP_CONNECTION *conn=thread_data->conn;
107 TRPS_INSTANCE *trps=thread_data->trps;
110 tr_debug("tr_trps_thread: started");
111 if (trps_authorize_connection(trps, conn)!=TRP_SUCCESS)
114 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPS_CONNECTED, TR_MQ_PRIO_HIGH);
115 tr_mq_msg_set_payload(msg, (void *)tr_dup_name(trp_connection_get_peer(conn)), tr_free_name_helper);
117 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
120 trps_mq_add(trps, msg); /* steals msg context */
123 trps_handle_connection(trps, conn);
126 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPS_DISCONNECTED, TR_MQ_PRIO_HIGH);
127 tr_mq_msg_set_payload(msg, (void *)conn, NULL); /* do not pass a free routine */
129 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
131 trps_mq_add(trps, msg);
132 tr_debug("tr_trps_thread: exit");
133 talloc_free(tmp_ctx);
137 /* called when a connection to the TRPS port is received */
138 static void tr_trps_event_cb(int listener, short event, void *arg)
140 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
141 TRPS_INSTANCE *trps = talloc_get_type_abort(arg, TRPS_INSTANCE); /* aborts on wrong type */
142 TRP_CONNECTION *conn=NULL;
143 TR_NAME *gssname=NULL;
145 struct trps_thread_data *thread_data=NULL;
147 if (0==(event & EV_READ)) {
148 tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
150 /* create a thread to handle this connection */
151 if (asprintf(&name, "trustrouter@%s", trps->hostname)==-1) {
154 gssname=tr_new_name(name);
155 free(name); name=NULL;
156 conn=trp_connection_accept(tmp_ctx, listener, gssname);
158 /* need to monitor this fd and trigger events when read becomes possible */
159 thread_data=talloc(conn, struct trps_thread_data);
160 if (thread_data==NULL) {
161 tr_err("tr_trps_event_cb: unable to allocate trps_thread_data");
162 talloc_free(tmp_ctx);
165 thread_data->conn=conn;
166 thread_data->trps=trps;
167 trps_add_connection(trps, conn); /* remember the connection */
168 pthread_create(trp_connection_get_thread(conn), NULL, tr_trps_thread, thread_data);
173 talloc_free(tmp_ctx);
176 static void tr_trps_cleanup_conn(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
178 /* everything belonging to the thread is in the TRP_CONNECTION
179 * associated with it */
180 tr_debug("tr_trps_cleanup_conn: freeing %p", conn);
181 pthread_join(*trp_connection_get_thread(conn), NULL);
182 trps_remove_connection(trps, conn);
183 talloc_report_full(conn, stderr);
184 trp_connection_free(conn);
185 tr_debug("tr_trps_cleanup_conn: deleted connection");
188 static void tr_trps_cleanup_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
190 pthread_join(*trp_connection_get_thread(trpc_get_conn(trpc)), NULL);
191 trps_remove_trpc(trps, trpc);
193 tr_debug("tr_trps_cleanup_trpc: deleted connection");
196 static void tr_trps_print_route_table(TRPS_INSTANCE *trps, FILE *f)
198 char *table=trp_rtable_to_str(NULL, trps->rtable, " | ", NULL);
200 fprintf(f, "Unable to print route table.\n");
202 fprintf(f, "%s\n", table);
207 static void tr_trps_process_mq(int socket, short event, void *arg)
209 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
213 talloc_report_full(trps->mq, stderr);
214 msg=trps_mq_pop(trps);
216 s=tr_mq_msg_get_message(msg);
217 if (0==strcmp(s, TR_MQMSG_TRPS_CONNECTED)) {
218 TR_NAME *gssname=(TR_NAME *)tr_mq_msg_get_payload(msg);
219 TRP_PEER *peer=trps_get_peer_by_gssname(trps, gssname);
221 tr_err("tr_trps_process_mq: incoming connection from unknown peer (%s) reported.", gssname->buf);
223 trp_peer_set_incoming_status(peer, PEER_CONNECTED);
224 tr_err("tr_trps_process_mq: incoming connection from %s established.", gssname->buf);
227 else if (0==strcmp(s, TR_MQMSG_TRPS_DISCONNECTED)) {
228 TRP_CONNECTION *conn=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TRP_CONNECTION);
229 TR_NAME *gssname=trp_connection_get_gssname(conn);
230 TRP_PEER *peer=trps_get_peer_by_gssname(trps, gssname);
232 tr_err("tr_trps_process_mq: incoming connection from unknown peer (%s) lost.",
233 trp_connection_get_gssname(conn)->buf);
235 trp_peer_set_incoming_status(peer, PEER_DISCONNECTED);
236 tr_trps_cleanup_conn(trps, conn);
237 tr_err("tr_trps_process_mq: incoming connection from %s lost.", gssname->buf);
240 else if (0==strcmp(s, TR_MQMSG_TRPC_CONNECTED)) {
241 TR_NAME *svcname=(TR_NAME *)tr_mq_msg_get_payload(msg);
242 TRP_PEER *peer=trps_get_peer_by_servicename(trps, svcname);
244 tr_err("tr_trps_process_mq: outgoing connection to unknown peer (%s) reported.", svcname->buf);
246 trp_peer_set_outgoing_status(peer, PEER_CONNECTED);
247 tr_err("tr_trps_process_mq: outgoing connection to %s established.", svcname->buf);
250 else if (0==strcmp(s, TR_MQMSG_TRPC_DISCONNECTED)) {
251 /* trpc connection died */
252 TRPC_INSTANCE *trpc=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TRPC_INSTANCE);
253 TR_NAME *gssname=trpc_get_gssname(trpc);
254 TRP_PEER *peer=trps_get_peer_by_servicename(trps, gssname);
256 tr_err("tr_trps_process_mq: outgoing connection to unknown peer (%s) lost.", gssname->buf);
258 trp_peer_set_outgoing_status(peer, PEER_DISCONNECTED);
259 tr_err("tr_trps_process_mq: outgoing connection to %s lost.", gssname->buf);
260 tr_trps_cleanup_trpc(trps, trpc);
264 else if (0==strcmp(s, TR_MQMSG_MSG_RECEIVED)) {
265 if (trps_handle_tr_msg(trps, tr_mq_msg_get_payload(msg))!=TRP_SUCCESS)
266 tr_notice("tr_trps_process_mq: error handling message.");
268 tr_trps_print_route_table(trps, stderr);
272 tr_notice("tr_trps_process_mq: unknown message '%s' received.", tr_mq_msg_get_message(msg));
275 msg=trps_mq_pop(trps);
279 static void tr_trps_update(int listener, short event, void *arg)
281 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
282 TRPS_INSTANCE *trps=cookie->trps;
283 struct event *ev=cookie->ev;
285 tr_debug("tr_trps_update: sending scheduled route updates.");
286 trps_update(trps, TRP_UPDATE_SCHEDULED);
287 event_add(ev, &(trps->update_interval));
290 static void tr_trps_sweep(int listener, short event, void *arg)
292 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
293 TRPS_INSTANCE *trps=cookie->trps;
294 struct event *ev=cookie->ev;
296 tr_debug("tr_trps_sweep: sweeping routes.");
297 trps_sweep_routes(trps);
298 tr_trps_print_route_table(trps, stderr);
299 /* schedule the event to run again */
300 event_add(ev, &(trps->sweep_interval));
303 static void tr_connection_update(int listener, short event, void *arg)
305 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
306 TRPS_INSTANCE *trps=cookie->trps;
307 struct event *ev=cookie->ev;
309 tr_debug("tr_connection_update: checking peer connections.");
310 tr_connect_to_peers(trps, ev);
311 /* schedule the event to run again */
312 event_add(ev, &(trps->connect_interval));
315 static int tr_trps_events_destructor(void *obj)
317 TR_TRPS_EVENTS *ev=talloc_get_type_abort(obj, TR_TRPS_EVENTS);
319 event_free(ev->mq_ev);
320 if (ev->connect_ev!=NULL)
321 event_free(ev->connect_ev);
322 if (ev->update_ev!=NULL)
323 event_free(ev->update_ev);
324 if (ev->sweep_ev!=NULL)
325 event_free(ev->sweep_ev);
328 static TR_TRPS_EVENTS *tr_trps_events_new(TALLOC_CTX *mem_ctx)
330 TR_TRPS_EVENTS *ev=talloc(mem_ctx, TR_TRPS_EVENTS);
332 ev->listen_ev=talloc(ev, struct tr_socket_event);
337 if (ev->listen_ev==NULL) {
341 talloc_set_destructor((void *)ev, tr_trps_events_destructor);
347 static void tr_trps_events_free(TR_TRPS_EVENTS *ev)
352 /* Configure the trps instance and set up its event handler.
353 * Fills in trps_ev, which should be allocated by caller. */
354 TRP_RC tr_trps_event_init(struct event_base *base, TR_INSTANCE *tr)
356 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
357 struct tr_socket_event *listen_ev=NULL;
358 struct tr_trps_event_cookie *trps_cookie=NULL;
359 struct tr_trps_event_cookie *connection_cookie=NULL;
360 struct tr_trps_event_cookie *update_cookie=NULL;
361 struct tr_trps_event_cookie *sweep_cookie=NULL;
362 struct timeval zero_time={0,0};
363 TRP_RC retval=TRP_ERROR;
365 if (tr->events != NULL) {
366 tr_notice("tr_trps_event_init: tr->events was not null. Freeing before reallocating..");
367 tr_trps_events_free(tr->events);
370 tr->events=tr_trps_events_new(tmp_ctx);
371 if (tr->events == NULL) {
372 tr_debug("tr_trps_event_init: unable to allocate event handles.");
377 /* get convenient handles */
378 listen_ev=tr->events->listen_ev;
380 /* Create the cookie for callbacks. It will end up part of the trps context, so it will
381 * be cleaned up when trps is freed by talloc_free. */
382 trps_cookie=talloc(tr->events, struct tr_trps_event_cookie);
383 if (trps_cookie == NULL) {
384 tr_debug("tr_trps_event_init: Unable to allocate trps_cookie.");
386 tr_trps_events_free(tr->events);
390 trps_cookie->trps=tr->trps;
391 trps_cookie->cfg_mgr=tr->cfg_mgr;
393 /* get a trps listener */
394 listen_ev->sock_fd=trps_get_listener(tr->trps,
397 tr->cfg_mgr->active->internal->hostname,
398 tr->cfg_mgr->active->internal->trps_port,
399 (void *)trps_cookie);
400 if (listen_ev->sock_fd < 0) {
401 tr_crit("Error opening TRP server socket.");
403 tr_trps_events_free(tr->events);
407 trps_cookie->ev=listen_ev->ev; /* in case it needs to frob the event */
410 listen_ev->ev=event_new(base,
415 event_add(listen_ev->ev, NULL);
417 /* now set up message queue processing event, only triggered by
419 tr->events->mq_ev=event_new(base,
424 tr_mq_set_notify_cb(tr->trps->mq, tr_trps_mq_cb, tr->events->mq_ev);
426 /* now set up the peer connection timer event */
427 connection_cookie=talloc(tr->events, struct tr_trps_event_cookie);
428 if (connection_cookie == NULL) {
429 tr_debug("tr_trps_event_init: Unable to allocate connection_cookie.");
431 tr_trps_events_free(tr->events);
435 connection_cookie->trps=tr->trps;
436 connection_cookie->cfg_mgr=tr->cfg_mgr;
437 tr->events->connect_ev=event_new(base, -1, EV_TIMEOUT, tr_connection_update, (void *)connection_cookie);
438 connection_cookie->ev=tr->events->connect_ev; /* in case it needs to frob the event */
439 /* The first time, do this immediately. Thereafter, it will retrigger every trps->connect_interval */
440 event_add(tr->events->connect_ev, &zero_time);
442 /* now set up the route update timer event */
443 update_cookie=talloc(tr->events, struct tr_trps_event_cookie);
444 if (update_cookie == NULL) {
445 tr_debug("tr_trps_event_init: Unable to allocate update_cookie.");
447 tr_trps_events_free(tr->events);
451 update_cookie->trps=tr->trps;
452 update_cookie->cfg_mgr=tr->cfg_mgr;
453 tr->events->update_ev=event_new(base, -1, EV_TIMEOUT, tr_trps_update, (void *)update_cookie);
454 update_cookie->ev=tr->events->update_ev; /* in case it needs to frob the event */
455 event_add(tr->events->update_ev, &(tr->trps->update_interval));
457 /* now set up the route table sweep timer event */
458 sweep_cookie=talloc(tr->events, struct tr_trps_event_cookie);
459 if (sweep_cookie == NULL) {
460 tr_debug("tr_trps_event_init: Unable to allocate sweep_cookie.");
462 tr_trps_events_free(tr->events);
466 sweep_cookie->trps=tr->trps;
467 sweep_cookie->cfg_mgr=tr->cfg_mgr;
468 tr->events->sweep_ev=event_new(base, -1, EV_TIMEOUT, tr_trps_sweep, (void *)sweep_cookie);
469 sweep_cookie->ev=tr->events->sweep_ev; /* in case it needs to frob the event */
470 event_add(tr->events->sweep_ev, &(tr->trps->sweep_interval));
472 talloc_steal(tr, tr->events);
476 talloc_free(tmp_ctx);
481 struct trpc_notify_cb_data {
484 pthread_mutex_t mutex;
487 static void tr_trpc_mq_cb(TR_MQ *mq, void *arg)
489 struct trpc_notify_cb_data *cb_data=(struct trpc_notify_cb_data *) arg;
490 pthread_mutex_lock(&(cb_data->mutex));
491 if (!cb_data->msg_ready) {
492 cb_data->msg_ready=1;
493 pthread_cond_signal(&(cb_data->cond));
495 pthread_mutex_unlock(&(cb_data->mutex));
498 /* data passed to thread */
499 struct trpc_thread_data {
503 static void *tr_trpc_thread(void *arg)
505 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
506 struct trpc_thread_data *thread_data=talloc_get_type_abort(arg, struct trpc_thread_data);
507 TRPC_INSTANCE *trpc=thread_data->trpc;
508 TRPS_INSTANCE *trps=thread_data->trps;
511 const char *msg_type=NULL;
512 char *encoded_msg=NULL;
513 TR_NAME *peer_gssname=NULL;
515 struct trpc_notify_cb_data cb_data={0,
516 PTHREAD_COND_INITIALIZER,
517 PTHREAD_MUTEX_INITIALIZER};
519 tr_debug("tr_trpc_thread: started");
521 /* set up the mq for receiving */
522 pthread_mutex_lock(&(cb_data.mutex)); /* hold this lock until we enter the main loop */
524 tr_mq_lock(trpc->mq);
525 tr_mq_set_notify_cb(trpc->mq, tr_trpc_mq_cb, (void *) &cb_data);
526 tr_mq_unlock(trpc->mq);
528 rc=trpc_connect(trpc);
529 if (rc!=TRP_SUCCESS) {
530 tr_notice("tr_trpc_thread: failed to initiate connection to %s:%d.",
531 trpc_get_server(trpc),
532 trpc_get_port(trpc));
534 peer_gssname=trp_connection_get_peer(trpc_get_conn(trpc));
535 if (peer_gssname==NULL) {
536 tr_err("tr_trpc_thread: could not duplicate peer_gssname.");
537 talloc_free(tmp_ctx);
540 tr_debug("tr_trpc_thread: connected to peer %s", peer_gssname->buf);
542 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_CONNECTED, TR_MQ_PRIO_HIGH);
543 tr_mq_msg_set_payload(msg, (void *)tr_dup_name(peer_gssname), tr_free_name_helper);
545 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
546 talloc_free(tmp_ctx);
549 trps_mq_add(trps, msg); /* steals msg context */
554 pthread_cond_wait(&(cb_data.cond), &(cb_data.mutex));
555 /* verify the condition */
556 if (cb_data.msg_ready) {
557 msg=trpc_mq_pop(trpc);
559 /* no message in the queue */
560 tr_err("tr_trpc_thread: notified of msg, but queue empty");
564 msg_type=tr_mq_msg_get_message(msg);
566 if (0==strcmp(msg_type, TR_MQMSG_ABORT)) {
568 break; /* exit loop */
570 else if (0==strcmp(msg_type, TR_MQMSG_TRPC_SEND)) {
571 encoded_msg=tr_mq_msg_get_payload(msg);
572 if (encoded_msg==NULL)
573 tr_notice("tr_trpc_thread: null outgoing TRP message.");
575 rc = trpc_send_msg(trpc, encoded_msg);
576 if (rc!=TRP_SUCCESS) {
577 tr_notice("tr_trpc_thread: trpc_send_msg failed.");
584 tr_notice("tr_trpc_thread: unknown message '%s' received.", msg_type);
591 tr_debug("tr_trpc_thread: exiting.");
592 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_DISCONNECTED, TR_MQ_PRIO_HIGH);
593 tr_mq_msg_set_payload(msg, (void *)trpc, NULL); /* do not pass a free routine */
595 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
597 trps_mq_add(trps, msg);
599 trpc_mq_clear(trpc); /* clear any queued messages */
601 talloc_free(tmp_ctx);
605 /* convert an IDP realm into routing table entries. Outputs number in *n_routes */
606 static TRP_ROUTE **tr_make_local_routes(TALLOC_CTX *mem_ctx,
611 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
613 TRP_ROUTE *new_entry=NULL;
614 TRP_ROUTE **entries=NULL;
615 size_t n_comms=0, ii=0;
619 if ((realm==NULL) || (realm->origin!=TR_REALM_LOCAL))
623 for (comm=realm->apcs, n_comms=0; comm!=NULL; comm=comm->next,n_comms++) {}
625 entries=talloc_array(tmp_ctx, TRP_ROUTE *, n_comms);
626 for (comm=realm->apcs,ii=0; comm!=NULL; comm=comm->next, ii++) {
627 new_entry=trp_route_new(entries);
628 if (new_entry==NULL) {
629 tr_crit("tr_make_local_routes: unable to allocate entry.");
630 talloc_free(entries);
633 trp_route_set_comm(new_entry, tr_dup_name(comm->id));
634 trp_route_set_realm(new_entry, tr_dup_name(realm->realm_id));
635 trp_route_set_peer(new_entry, tr_new_name("")); /* no peer, it's us */
636 trp_route_set_metric(new_entry, 0);
637 trp_route_set_trust_router(new_entry, tr_new_name(trust_router));
638 trp_route_set_next_hop(new_entry, tr_new_name(""));
639 trp_route_set_local(new_entry, 1);
640 entries[ii]=new_entry;
643 talloc_steal(mem_ctx, entries);
646 talloc_free(tmp_ctx);
650 void tr_peer_status_change(TRP_PEER *peer, void *cookie)
652 TRPS_INSTANCE *trps=talloc_get_type_abort(cookie, TRPS_INSTANCE);
654 if (TRP_SUCCESS!=trps_wildcard_route_req(trps, trp_peer_get_servicename(peer)))
655 tr_err("tr_send_wildcard: error sending wildcard route request.");
658 /* starts a trpc thread to connect to server:port */
659 TRP_RC tr_trpc_initiate(TRPS_INSTANCE *trps, TRP_PEER *peer, struct event *ev)
661 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
662 TRPC_INSTANCE *trpc=NULL;
663 TRP_CONNECTION *conn=NULL;
664 struct trpc_thread_data *thread_data=NULL;
667 tr_debug("tr_trpc_initiate entered");
668 trpc=trpc_new(tmp_ctx);
670 tr_crit("tr_trpc_initiate: could not allocate TRPC_INSTANCE.");
674 tr_debug("tr_trpc_initiate: allocated trpc");
676 conn=trp_connection_new(trpc);
678 tr_crit("tr_trpc_initiate: could not allocate TRP_CONNECTION.");
683 trpc_set_conn(trpc, conn);
684 trpc_set_server(trpc, talloc_strdup(trpc, trp_peer_get_server(peer)));
685 trpc_set_port(trpc, trp_peer_get_port(peer));
686 trpc_set_gssname(trpc, trp_peer_dup_servicename(peer));
687 tr_debug("tr_trpc_initiate: allocated connection");
690 thread_data=talloc(trpc, struct trpc_thread_data);
691 if (thread_data==NULL) {
692 tr_crit("tr_trpc_initiate: could not allocate struct trpc_thread_data.");
696 thread_data->trpc=trpc;
697 thread_data->trps=trps;
699 trps_add_trpc(trps, trpc); /* must add before starting thread */
700 pthread_create(trp_connection_get_thread(conn), NULL, tr_trpc_thread, thread_data);
702 tr_debug("tr_trpc_initiate: started trpc thread");
706 talloc_free(tmp_ctx);
710 /* Add local routes to the route table. */
711 TRP_RC tr_add_local_routes(TRPS_INSTANCE *trps, TR_CFG *cfg)
713 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
714 TR_IDP_REALM *cur=NULL;
715 TRP_ROUTE **local_routes=NULL;
718 char *trust_router_name=talloc_asprintf(tmp_ctx, "%s:%d", cfg->internal->hostname, cfg->internal->trps_port);
720 /* determine our trust router name */
721 if (trust_router_name==NULL)
724 for (cur=cfg->idp_realms; cur!=NULL; cur=cur->next) {
725 local_routes=tr_make_local_routes(tmp_ctx, cur, trust_router_name, &n_routes);
726 for (ii=0; ii<n_routes; ii++)
727 trps_add_route(trps, local_routes[ii]);
729 talloc_free(local_routes);
734 talloc_free(tmp_ctx);
738 /* decide how often to attempt to connect to a peer */
739 static int tr_conn_attempt_due(TRPS_INSTANCE *trps, TRP_PEER *peer, struct timespec *when)
741 return 1; /* currently make an attempt every cycle */
744 /* open missing connections to peers */
745 TRP_RC tr_connect_to_peers(TRPS_INSTANCE *trps, struct event *ev)
747 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
748 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
750 struct timespec curtime={0,0};
753 if (clock_gettime(CLOCK_REALTIME, &curtime)) {
754 tr_err("tr_connect_to_peers: failed to read time.");
759 for (peer=trp_ptable_iter_first(iter, trps->ptable);
761 peer=trp_ptable_iter_next(iter))
763 if (trps_find_trpc(trps, peer)==NULL) {
764 TR_NAME *label=trp_peer_get_label(peer);
765 tr_debug("tr_connect_to_peers: %.*s missing connection.",
766 label->len, label->buf);
767 /* has it been long enough since we last tried? */
768 if (tr_conn_attempt_due(trps, peer, &curtime)) {
769 trp_peer_set_last_conn_attempt(peer, &curtime); /* we are trying again now */
770 if (tr_trpc_initiate(trps, peer, ev)!=TRP_SUCCESS) {
771 tr_err("tr_connect_to_peers: unable to initiate TRP connection to %s:%u.",
772 trp_peer_get_server(peer),
773 trp_peer_get_port(peer));
781 trp_ptable_iter_free(iter);
782 talloc_free(tmp_ctx);
787 /* Called by the config manager after a change to the active configuration.
788 * Updates configuration of objects that do not know about the config manager. */
789 void tr_config_changed(TR_CFG *new_cfg, void *cookie)
791 TR_INSTANCE *tr=talloc_get_type_abort(cookie, TR_INSTANCE);
792 TRPS_INSTANCE *trps=tr->trps;
794 tr->cfgwatch->poll_interval.tv_sec=new_cfg->internal->cfg_poll_interval;
795 tr->cfgwatch->poll_interval.tv_usec=0;
797 tr->cfgwatch->settling_time.tv_sec=new_cfg->internal->cfg_settling_time;
798 tr->cfgwatch->settling_time.tv_usec=0;
800 trps_set_connect_interval(trps, new_cfg->internal->trp_connect_interval);
801 trps_set_update_interval(trps, new_cfg->internal->trp_update_interval);
802 trps_set_sweep_interval(trps, new_cfg->internal->trp_sweep_interval);
803 trps_set_ptable(trps, new_cfg->peers);
804 trps_set_peer_status_callback(trps, tr_peer_status_change, (void *)trps);
805 trps_clear_rtable(trps); /* should we do this every time??? */
806 tr_add_local_routes(trps, new_cfg); /* should we do this every time??? */
807 trps_update_active_routes(trps); /* find new routes */
808 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
809 tr_print_config(new_cfg);
810 tr_trps_print_route_table(trps, stderr);