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_internal.h>
51 #include <trp_ptable.h>
52 #include <trp_rtable.h>
53 #include <tr_config.h>
59 /* data for event callbacks */
60 struct tr_trps_event_cookie {
66 /* callback to schedule event to process messages */
67 static void tr_trps_mq_cb(TR_MQ *mq, void *arg)
69 struct event *mq_ev=(struct event *)arg;
70 event_active(mq_ev, 0, 0);
73 static void msg_free_helper(void *p)
75 tr_msg_free_decoded((TR_MSG *)p);
78 static void tr_free_name_helper(void *arg)
80 tr_free_name((TR_NAME *)arg);
83 /* takes a TR_MSG and puts it in a TR_MQ_MSG for processing by the main thread */
84 static TRP_RC tr_trps_msg_handler(TRPS_INSTANCE *trps,
88 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
89 TR_MQ_MSG *mq_msg=NULL;
91 /* n.b., conn is available here, but do not hold onto the reference
92 * because it may be cleaned up if the originating connection goes
93 * down before the message is processed */
94 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_MSG_RECEIVED, TR_MQ_PRIO_NORMAL);
98 tr_mq_msg_set_payload(mq_msg, (void *)tr_msg, msg_free_helper);
99 trps_mq_add(trps, mq_msg);
100 talloc_free(tmp_ctx); /* cleans up the message if it did not get appended correctly */
105 static int tr_trps_gss_handler(gss_name_t client_name, gss_buffer_t gss_name,
108 struct tr_trps_event_cookie *cookie=(struct tr_trps_event_cookie *)cookie_in;
109 TRPS_INSTANCE *trps = cookie->trps;
110 TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
111 TR_NAME name={gss_name->value, gss_name->length};
113 tr_debug("tr_trps_gss_handler()");
115 if ((!client_name) || (!gss_name) || (!trps) || (!cfg_mgr)) {
116 tr_debug("tr_trps_gss_handler: Bad parameters.");
120 /* look up the TRPS peer matching the GSS name */
121 if (NULL==trps_get_peer_by_gssname(trps, &name)) {
122 tr_warning("tr_trps_gss_handler: Connection attempt from unknown peer (GSS name: %.*s).", name.len, name.buf);
126 tr_debug("Client's GSS Name: %.*s", name.len, name.buf);
130 /* data passed to thread */
131 struct trps_thread_data {
132 TRP_CONNECTION *conn;
135 /* thread to handle GSS connections from peers */
136 static void *tr_trps_thread(void *arg)
138 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
139 struct trps_thread_data *thread_data=talloc_get_type_abort(arg, struct trps_thread_data);
140 TRP_CONNECTION *conn=thread_data->conn;
141 TRPS_INSTANCE *trps=thread_data->trps;
144 tr_debug("tr_trps_thread: started");
145 if (trps_authorize_connection(trps, conn)!=TRP_SUCCESS)
148 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPS_CONNECTED, TR_MQ_PRIO_HIGH);
149 tr_mq_msg_set_payload(msg, (void *)tr_dup_name(trp_connection_get_peer(conn)), tr_free_name_helper);
151 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
154 trps_mq_add(trps, msg); /* steals msg context */
157 trps_handle_connection(trps, conn);
160 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPS_DISCONNECTED, TR_MQ_PRIO_HIGH);
161 tr_mq_msg_set_payload(msg, (void *)conn, NULL); /* do not pass a free routine */
163 tr_err("tr_trps_thread: error allocating TR_MQ_MSG");
165 trps_mq_add(trps, msg);
166 tr_debug("tr_trps_thread: exit");
167 talloc_free(tmp_ctx);
171 /* called when a connection to the TRPS port is received */
172 static void tr_trps_event_cb(int listener, short event, void *arg)
174 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
175 TRPS_INSTANCE *trps = talloc_get_type_abort(arg, TRPS_INSTANCE); /* aborts on wrong type */
176 TRP_CONNECTION *conn=NULL;
177 TR_NAME *gssname=NULL;
179 struct trps_thread_data *thread_data=NULL;
181 if (0==(event & EV_READ)) {
182 tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
184 /* create a thread to handle this connection */
185 if (asprintf(&name, "trustrouter@%s", trps->hostname)==-1) {
188 gssname=tr_new_name(name);
189 free(name); name=NULL;
190 conn=trp_connection_accept(tmp_ctx, listener, gssname);
192 /* need to monitor this fd and trigger events when read becomes possible */
193 thread_data=talloc(conn, struct trps_thread_data);
194 if (thread_data==NULL) {
195 tr_err("tr_trps_event_cb: unable to allocate trps_thread_data");
196 talloc_free(tmp_ctx);
199 thread_data->conn=conn;
200 thread_data->trps=trps;
201 trps_add_connection(trps, conn); /* remember the connection */
202 pthread_create(trp_connection_get_thread(conn), NULL, tr_trps_thread, thread_data);
207 talloc_free(tmp_ctx);
210 static void tr_trps_cleanup_conn(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
212 /* everything belonging to the thread is in the TRP_CONNECTION
213 * associated with it */
214 tr_debug("tr_trps_cleanup_conn: freeing %p", conn);
215 pthread_join(*trp_connection_get_thread(conn), NULL);
216 trps_remove_connection(trps, conn);
217 trp_connection_free(conn);
218 tr_debug("tr_trps_cleanup_conn: deleted connection");
221 static void tr_trps_cleanup_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
223 pthread_join(*trp_connection_get_thread(trpc_get_conn(trpc)), NULL);
224 trps_remove_trpc(trps, trpc);
226 tr_debug("tr_trps_cleanup_trpc: deleted connection");
229 static void tr_trps_print_route_table(TRPS_INSTANCE *trps, FILE *f)
231 char *table=trp_rtable_to_str(NULL, trps->rtable, " | ", NULL);
233 fprintf(f, "Unable to print route table.\n");
235 fprintf(f, "%s\n", table);
241 * Event handler to process TRP messages from connection threads. These
242 * are added to the message queue (mq) in tr_trps_msg_handler(), which
243 * runs in the other threads.
245 * @param socket Ignored
246 * @param event Ignored
247 * @param arg Pointer to the TRPS_INSTANCE
249 static void tr_trps_process_mq(int socket, short event, void *arg)
251 TRPS_INSTANCE *trps=talloc_get_type_abort(arg, TRPS_INSTANCE);
255 msg=trps_mq_pop(trps);
257 s=tr_mq_msg_get_message(msg);
258 if (0==strcmp(s, TR_MQMSG_TRPS_CONNECTED)) {
259 TR_NAME *gssname=(TR_NAME *)tr_mq_msg_get_payload(msg);
260 TRP_PEER *peer=trps_get_peer_by_gssname(trps, gssname);
262 tr_err("tr_trps_process_mq: incoming connection from unknown peer (%s) reported.", gssname->buf);
264 trp_peer_set_incoming_status(peer, PEER_CONNECTED);
265 tr_err("tr_trps_process_mq: incoming connection from %s established.", gssname->buf);
268 else if (0==strcmp(s, TR_MQMSG_TRPS_DISCONNECTED)) {
269 TRP_CONNECTION *conn=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TRP_CONNECTION);
270 TR_NAME *gssname=trp_connection_get_gssname(conn);
271 TRP_PEER *peer=trps_get_peer_by_gssname(trps, gssname);
273 tr_err("tr_trps_process_mq: incoming connection from unknown peer (%s) lost.",
274 trp_connection_get_gssname(conn)->buf);
276 trp_peer_set_incoming_status(peer, PEER_DISCONNECTED);
277 tr_trps_cleanup_conn(trps, conn);
278 tr_err("tr_trps_process_mq: incoming connection from %s lost.", gssname->buf);
281 else if (0==strcmp(s, TR_MQMSG_TRPC_CONNECTED)) {
282 TR_NAME *svcname=(TR_NAME *)tr_mq_msg_get_payload(msg);
283 TRP_PEER *peer=trps_get_peer_by_servicename(trps, svcname);
285 tr_err("tr_trps_process_mq: outgoing connection to unknown peer (%s) reported.", svcname->buf);
287 trp_peer_set_outgoing_status(peer, PEER_CONNECTED);
288 tr_err("tr_trps_process_mq: outgoing connection to %s established.", svcname->buf);
291 else if (0==strcmp(s, TR_MQMSG_TRPC_DISCONNECTED)) {
292 /* trpc connection died */
293 TRPC_INSTANCE *trpc=talloc_get_type_abort(tr_mq_msg_get_payload(msg), TRPC_INSTANCE);
294 TR_NAME *gssname=trpc_get_gssname(trpc);
295 TRP_PEER *peer=trps_get_peer_by_servicename(trps, gssname);
297 tr_err("tr_trps_process_mq: outgoing connection to unknown peer (%s) lost.", gssname->buf);
299 trp_peer_set_outgoing_status(peer, PEER_DISCONNECTED);
300 tr_err("tr_trps_process_mq: outgoing connection to %s lost.", gssname->buf);
301 tr_trps_cleanup_trpc(trps, trpc);
305 else if (0==strcmp(s, TR_MQMSG_MSG_RECEIVED)) {
306 if (trps_handle_tr_msg(trps, tr_mq_msg_get_payload(msg))!=TRP_SUCCESS)
307 tr_notice("tr_trps_process_mq: error handling message.");
309 tr_trps_print_route_table(trps, stderr);
313 tr_notice("tr_trps_process_mq: unknown message '%s' received.", tr_mq_msg_get_message(msg));
316 msg=trps_mq_pop(trps);
320 static void tr_trps_update(int listener, short event, void *arg)
322 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
323 TRPS_INSTANCE *trps=cookie->trps;
324 struct event *ev=cookie->ev;
326 tr_debug("tr_trps_update: sending scheduled route/community updates.");
327 trps_update(trps, TRP_UPDATE_SCHEDULED);
328 event_add(ev, &(trps->update_interval));
329 tr_debug("tr_trps_update: update interval=%d", trps->update_interval.tv_sec);
332 static void tr_trps_sweep(int listener, short event, void *arg)
334 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
335 TRPS_INSTANCE *trps=cookie->trps;
336 struct event *ev=cookie->ev;
338 tr_debug("tr_trps_sweep: sweeping routes.");
339 trps_sweep_routes(trps);
340 tr_debug("tr_trps_sweep: sweeping communities.");
341 trps_sweep_ctable(trps);
342 tr_trps_print_route_table(trps, stderr);
343 /* schedule the event to run again */
344 event_add(ev, &(trps->sweep_interval));
347 static void tr_connection_update(int listener, short event, void *arg)
349 struct tr_trps_event_cookie *cookie=talloc_get_type_abort(arg, struct tr_trps_event_cookie);
350 TRPS_INSTANCE *trps=cookie->trps;
351 struct event *ev=cookie->ev;
353 tr_debug("tr_connection_update: checking peer connections.");
354 tr_connect_to_peers(trps, ev);
355 /* schedule the event to run again */
356 event_add(ev, &(trps->connect_interval));
359 static int tr_trps_events_destructor(void *obj)
361 TR_TRPS_EVENTS *ev=talloc_get_type_abort(obj, TR_TRPS_EVENTS);
363 event_free(ev->mq_ev);
364 if (ev->connect_ev!=NULL)
365 event_free(ev->connect_ev);
366 if (ev->update_ev!=NULL)
367 event_free(ev->update_ev);
368 if (ev->sweep_ev!=NULL)
369 event_free(ev->sweep_ev);
372 static TR_TRPS_EVENTS *tr_trps_events_new(TALLOC_CTX *mem_ctx)
374 TR_TRPS_EVENTS *ev=talloc(mem_ctx, TR_TRPS_EVENTS);
376 ev->listen_ev=talloc(ev, struct tr_socket_event);
381 if (ev->listen_ev==NULL) {
385 talloc_set_destructor((void *)ev, tr_trps_events_destructor);
391 static void tr_trps_events_free(TR_TRPS_EVENTS *ev)
396 /* Configure the trps instance and set up its event handler.
397 * Fills in trps_ev, which should be allocated by caller. */
398 TRP_RC tr_trps_event_init(struct event_base *base, TR_INSTANCE *tr)
400 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
401 struct tr_socket_event *listen_ev=NULL;
402 struct tr_trps_event_cookie *trps_cookie=NULL;
403 struct tr_trps_event_cookie *connection_cookie=NULL;
404 struct tr_trps_event_cookie *update_cookie=NULL;
405 struct tr_trps_event_cookie *sweep_cookie=NULL;
406 struct timeval zero_time={0,0};
407 TRP_RC retval=TRP_ERROR;
410 if (tr->events != NULL) {
411 tr_notice("tr_trps_event_init: tr->events was not null. Freeing before reallocating..");
412 tr_trps_events_free(tr->events);
415 tr->events=tr_trps_events_new(tmp_ctx);
416 if (tr->events == NULL) {
417 tr_debug("tr_trps_event_init: unable to allocate event handles.");
422 /* get convenient handles */
423 listen_ev=tr->events->listen_ev;
425 /* Create the cookie for callbacks. It will end up part of the trps context, so it will
426 * be cleaned up when trps is freed by talloc_free. */
427 trps_cookie=talloc(tr->events, struct tr_trps_event_cookie);
428 if (trps_cookie == NULL) {
429 tr_debug("tr_trps_event_init: Unable to allocate trps_cookie.");
431 tr_trps_events_free(tr->events);
435 trps_cookie->trps=tr->trps;
436 trps_cookie->cfg_mgr=tr->cfg_mgr;
438 /* get a trps listener */
439 listen_ev->n_sock_fd=trps_get_listener(tr->trps,
442 tr->cfg_mgr->active->internal->hostname,
443 tr->cfg_mgr->active->internal->trps_port,
447 if (listen_ev->n_sock_fd==0) {
448 tr_crit("Error opening TRP server socket.");
450 tr_trps_events_free(tr->events);
455 /* Set up events for the sockets */
456 for (ii=0; ii<listen_ev->n_sock_fd; ii++) {
457 listen_ev->ev[ii]=event_new(base,
458 listen_ev->sock_fd[ii],
462 event_add(listen_ev->ev[ii], NULL);
465 /* now set up message queue processing event, only triggered by
467 tr->events->mq_ev=event_new(base,
472 tr_mq_set_notify_cb(tr->trps->mq, tr_trps_mq_cb, tr->events->mq_ev);
474 /* now set up the peer connection timer event */
475 connection_cookie=talloc(tr->events, struct tr_trps_event_cookie);
476 if (connection_cookie == NULL) {
477 tr_debug("tr_trps_event_init: Unable to allocate connection_cookie.");
479 tr_trps_events_free(tr->events);
483 connection_cookie->trps=tr->trps;
484 connection_cookie->cfg_mgr=tr->cfg_mgr;
485 tr->events->connect_ev=event_new(base, -1, EV_TIMEOUT, tr_connection_update, (void *)connection_cookie);
486 connection_cookie->ev=tr->events->connect_ev; /* in case it needs to frob the event */
487 /* The first time, do this immediately. Thereafter, it will retrigger every trps->connect_interval */
488 event_add(tr->events->connect_ev, &zero_time);
490 /* now set up the route update timer event */
491 update_cookie=talloc(tr->events, struct tr_trps_event_cookie);
492 if (update_cookie == NULL) {
493 tr_debug("tr_trps_event_init: Unable to allocate update_cookie.");
495 tr_trps_events_free(tr->events);
499 update_cookie->trps=tr->trps;
500 update_cookie->cfg_mgr=tr->cfg_mgr;
501 tr->events->update_ev=event_new(base, -1, EV_TIMEOUT, tr_trps_update, (void *)update_cookie);
502 update_cookie->ev=tr->events->update_ev; /* in case it needs to frob the event */
503 event_add(tr->events->update_ev, &(tr->trps->update_interval));
505 /* now set up the route table sweep timer event */
506 sweep_cookie=talloc(tr->events, struct tr_trps_event_cookie);
507 if (sweep_cookie == NULL) {
508 tr_debug("tr_trps_event_init: Unable to allocate sweep_cookie.");
510 tr_trps_events_free(tr->events);
514 sweep_cookie->trps=tr->trps;
515 sweep_cookie->cfg_mgr=tr->cfg_mgr;
516 tr->events->sweep_ev=event_new(base, -1, EV_TIMEOUT, tr_trps_sweep, (void *)sweep_cookie);
517 sweep_cookie->ev=tr->events->sweep_ev; /* in case it needs to frob the event */
518 event_add(tr->events->sweep_ev, &(tr->trps->sweep_interval));
520 talloc_steal(tr, tr->events);
524 talloc_free(tmp_ctx);
529 struct trpc_notify_cb_data {
532 pthread_mutex_t mutex;
535 static void tr_trpc_mq_cb(TR_MQ *mq, void *arg)
537 struct trpc_notify_cb_data *cb_data=(struct trpc_notify_cb_data *) arg;
538 pthread_mutex_lock(&(cb_data->mutex));
539 if (!cb_data->msg_ready) {
540 cb_data->msg_ready=1;
541 pthread_cond_signal(&(cb_data->cond));
543 pthread_mutex_unlock(&(cb_data->mutex));
546 /* data passed to thread */
547 struct trpc_thread_data {
551 static void *tr_trpc_thread(void *arg)
553 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
554 struct trpc_thread_data *thread_data=talloc_get_type_abort(arg, struct trpc_thread_data);
555 TRPC_INSTANCE *trpc=thread_data->trpc;
556 TRPS_INSTANCE *trps=thread_data->trps;
559 const char *msg_type=NULL;
560 char *encoded_msg=NULL;
561 TR_NAME *peer_gssname=NULL;
565 struct trpc_notify_cb_data cb_data={0,
566 PTHREAD_COND_INITIALIZER,
567 PTHREAD_MUTEX_INITIALIZER};
569 tr_debug("tr_trpc_thread: started");
571 /* set up the mq for receiving */
572 pthread_mutex_lock(&(cb_data.mutex)); /* hold this lock until we enter the main loop */
574 tr_mq_lock(trpc->mq);
575 tr_mq_set_notify_cb(trpc->mq, tr_trpc_mq_cb, (void *) &cb_data);
576 tr_mq_unlock(trpc->mq);
578 rc=trpc_connect(trpc);
579 if (rc!=TRP_SUCCESS) {
580 tr_notice("tr_trpc_thread: failed to initiate connection to %s:%d.",
581 trpc_get_server(trpc),
582 trpc_get_port(trpc));
584 peer_gssname=trp_connection_get_peer(trpc_get_conn(trpc));
585 if (peer_gssname==NULL) {
586 tr_err("tr_trpc_thread: could not duplicate peer_gssname.");
587 talloc_free(tmp_ctx);
590 tr_debug("tr_trpc_thread: connected to peer %s", peer_gssname->buf);
592 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_CONNECTED, TR_MQ_PRIO_HIGH);
593 tr_mq_msg_set_payload(msg, (void *)tr_dup_name(peer_gssname), tr_free_name_helper);
595 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
596 talloc_free(tmp_ctx);
599 trps_mq_add(trps, msg); /* steals msg context */
604 pthread_cond_wait(&(cb_data.cond), &(cb_data.mutex));
605 /* verify the condition */
606 if (cb_data.msg_ready) {
607 for (msg=trpc_mq_pop(trpc),n_sent=0; msg!=NULL; msg=trpc_mq_pop(trpc),n_sent++) {
608 msg_type=tr_mq_msg_get_message(msg);
610 if (0==strcmp(msg_type, TR_MQMSG_ABORT)) {
614 else if (0==strcmp(msg_type, TR_MQMSG_TRPC_SEND)) {
615 encoded_msg=tr_mq_msg_get_payload(msg);
616 if (encoded_msg==NULL)
617 tr_notice("tr_trpc_thread: null outgoing TRP message.");
619 rc = trpc_send_msg(trpc, encoded_msg);
620 if (rc!=TRP_SUCCESS) {
621 tr_notice("tr_trpc_thread: trpc_send_msg failed.");
628 tr_notice("tr_trpc_thread: unknown message '%s' received.", msg_type);
633 tr_err("tr_trpc_thread: notified of msg, but queue empty");
635 tr_debug("tr_trpc_thread: sent %d messages.", n_sent);
640 tr_debug("tr_trpc_thread: exiting.");
641 msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_DISCONNECTED, TR_MQ_PRIO_HIGH);
642 tr_mq_msg_set_payload(msg, (void *)trpc, NULL); /* do not pass a free routine */
644 tr_err("tr_trpc_thread: error allocating TR_MQ_MSG");
646 trps_mq_add(trps, msg);
648 trpc_mq_clear(trpc); /* clear any queued messages */
650 talloc_free(tmp_ctx);
654 /* convert an IDP realm into routing table entries. Outputs number in *n_routes */
655 static TRP_ROUTE **tr_make_local_routes(TALLOC_CTX *mem_ctx,
660 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
662 TRP_ROUTE *new_entry=NULL;
663 TRP_ROUTE **entries=NULL;
664 size_t n_comms=0, ii=0;
668 if ((realm==NULL) || (realm->origin!=TR_REALM_LOCAL))
672 for (comm=realm->apcs, n_comms=0; comm!=NULL; comm=comm->next,n_comms++) {}
674 entries=talloc_array(tmp_ctx, TRP_ROUTE *, n_comms);
675 for (comm=realm->apcs,ii=0; comm!=NULL; comm=comm->next, ii++) {
676 new_entry=trp_route_new(entries);
677 if (new_entry==NULL) {
678 tr_crit("tr_make_local_routes: unable to allocate entry.");
679 talloc_free(entries);
682 trp_route_set_comm(new_entry, tr_dup_name(comm->id));
683 trp_route_set_realm(new_entry, tr_dup_name(realm->realm_id));
684 trp_route_set_peer(new_entry, tr_new_name("")); /* no peer, it's us */
685 trp_route_set_metric(new_entry, 0);
686 trp_route_set_trust_router(new_entry, tr_new_name(trust_router));
687 trp_route_set_next_hop(new_entry, tr_new_name(""));
688 trp_route_set_local(new_entry, 1);
689 entries[ii]=new_entry;
692 talloc_steal(mem_ctx, entries);
695 talloc_free(tmp_ctx);
699 void tr_peer_status_change(TRP_PEER *peer, void *cookie)
701 TRPS_INSTANCE *trps=talloc_get_type_abort(cookie, TRPS_INSTANCE);
703 if (TRP_SUCCESS!=trps_wildcard_route_req(trps, trp_peer_get_servicename(peer)))
704 tr_err("tr_send_wildcard: error sending wildcard route request.");
707 /* starts a trpc thread to connect to server:port */
708 TRP_RC tr_trpc_initiate(TRPS_INSTANCE *trps, TRP_PEER *peer, struct event *ev)
710 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
711 TRPC_INSTANCE *trpc=NULL;
712 TRP_CONNECTION *conn=NULL;
713 struct trpc_thread_data *thread_data=NULL;
716 tr_debug("tr_trpc_initiate entered");
717 trpc=trpc_new(tmp_ctx);
719 tr_crit("tr_trpc_initiate: could not allocate TRPC_INSTANCE.");
723 tr_debug("tr_trpc_initiate: allocated trpc");
725 conn=trp_connection_new(trpc);
727 tr_crit("tr_trpc_initiate: could not allocate TRP_CONNECTION.");
732 trpc_set_conn(trpc, conn);
733 trpc_set_server(trpc, talloc_strdup(trpc, trp_peer_get_server(peer)));
734 trpc_set_port(trpc, trp_peer_get_port(peer));
735 trpc_set_gssname(trpc, trp_peer_dup_servicename(peer));
736 tr_debug("tr_trpc_initiate: allocated connection");
739 thread_data=talloc(trpc, struct trpc_thread_data);
740 if (thread_data==NULL) {
741 tr_crit("tr_trpc_initiate: could not allocate struct trpc_thread_data.");
745 thread_data->trpc=trpc;
746 thread_data->trps=trps;
748 trps_add_trpc(trps, trpc); /* must add before starting thread */
749 pthread_create(trp_connection_get_thread(conn), NULL, tr_trpc_thread, thread_data);
751 tr_debug("tr_trpc_initiate: started trpc thread");
755 talloc_free(tmp_ctx);
759 /* Add local routes to the route table. */
760 TRP_RC tr_add_local_routes(TRPS_INSTANCE *trps, TR_CFG *cfg)
762 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
763 TR_IDP_REALM *cur=NULL;
764 TRP_ROUTE **local_routes=NULL;
767 char *trust_router_name=talloc_asprintf(tmp_ctx, "%s:%d", cfg->internal->hostname, cfg->internal->trps_port);
769 /* determine our trust router name */
770 if (trust_router_name==NULL)
773 for (cur=cfg->ctable->idp_realms; cur!=NULL; cur=cur->next) {
774 local_routes=tr_make_local_routes(tmp_ctx, cur, trust_router_name, &n_routes);
775 for (ii=0; ii<n_routes; ii++)
776 trps_add_route(trps, local_routes[ii]);
778 talloc_free(local_routes);
783 talloc_free(tmp_ctx);
787 /* decide how often to attempt to connect to a peer */
788 static int tr_conn_attempt_due(TRPS_INSTANCE *trps, TRP_PEER *peer, struct timespec *when)
790 return 1; /* currently make an attempt every cycle */
793 /* open missing connections to peers */
794 TRP_RC tr_connect_to_peers(TRPS_INSTANCE *trps, struct event *ev)
796 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
797 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
799 struct timespec curtime={0,0};
802 if (clock_gettime(CLOCK_REALTIME, &curtime)) {
803 tr_err("tr_connect_to_peers: failed to read time.");
808 for (peer=trp_ptable_iter_first(iter, trps->ptable);
810 peer=trp_ptable_iter_next(iter))
812 if (trps_find_trpc(trps, peer)==NULL) {
813 TR_NAME *label=trp_peer_get_label(peer);
814 tr_debug("tr_connect_to_peers: %.*s missing connection.",
815 label->len, label->buf);
816 /* has it been long enough since we last tried? */
817 if (tr_conn_attempt_due(trps, peer, &curtime)) {
818 trp_peer_set_last_conn_attempt(peer, &curtime); /* we are trying again now */
819 if (tr_trpc_initiate(trps, peer, ev)!=TRP_SUCCESS) {
820 tr_err("tr_connect_to_peers: unable to initiate TRP connection to %s:%u.",
821 trp_peer_get_server(peer),
822 trp_peer_get_port(peer));
830 trp_ptable_iter_free(iter);
831 talloc_free(tmp_ctx);
836 /* Called by the config manager after a change to the active configuration.
837 * Updates configuration of objects that do not know about the config manager. */
838 void tr_config_changed(TR_CFG *new_cfg, void *cookie)
840 TR_INSTANCE *tr=talloc_get_type_abort(cookie, TR_INSTANCE);
841 TRPS_INSTANCE *trps=tr->trps;
843 tr->cfgwatch->poll_interval.tv_sec=new_cfg->internal->cfg_poll_interval;
844 tr->cfgwatch->poll_interval.tv_usec=0;
846 tr->cfgwatch->settling_time.tv_sec=new_cfg->internal->cfg_settling_time;
847 tr->cfgwatch->settling_time.tv_usec=0;
849 trps_set_connect_interval(trps, new_cfg->internal->trp_connect_interval);
850 trps_set_update_interval(trps, new_cfg->internal->trp_update_interval);
851 trps_set_sweep_interval(trps, new_cfg->internal->trp_sweep_interval);
852 trps_set_ctable(trps, new_cfg->ctable);
853 trps_set_ptable(trps, new_cfg->peers);
854 trps_set_peer_status_callback(trps, tr_peer_status_change, (void *)trps);
855 trps_clear_rtable(trps); /* should we do this every time??? */
856 tr_add_local_routes(trps, new_cfg); /* should we do this every time??? */
857 trps_update_active_routes(trps); /* find new routes */
858 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
859 tr_print_config(new_cfg);
860 tr_trps_print_route_table(trps, stderr);