9 #include <trust_router/tr_name.h>
10 #include <trp_internal.h>
11 #include <trp_ptable.h>
12 #include <trp_rtable.h>
16 static int trps_destructor(void *object)
18 TRPS_INSTANCE *trps=talloc_get_type_abort(object, TRPS_INSTANCE);
19 if (trps->rtable!=NULL)
20 trp_rtable_free(trps->rtable);
24 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
26 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
33 trps->update_interval=(struct timeval){0,0};
34 trps->sweep_interval=(struct timeval){0,0};
36 trps->mq=tr_mq_new(trps);
38 /* failed to allocate mq */
43 trps->ptable=trp_ptable_new(trps);
44 if (trps->ptable==NULL) {
45 /* failed to allocate ptable */
51 if (trps_init_rtable(trps) != TRP_SUCCESS) {
52 /* failed to allocate rtable */
57 talloc_set_destructor((void *)trps, trps_destructor);
62 /* create a new route table, first discarding an old one if necessary */
63 TRP_RC trps_init_rtable(TRPS_INSTANCE *trps)
65 if (trps->rtable != NULL) {
66 trp_rtable_free(trps->rtable);
70 trps->rtable=trp_rtable_new();
71 if (trps->rtable==NULL) {
77 void trps_clear_rtable(TRPS_INSTANCE *trps)
79 trp_rtable_clear(trps->rtable);
82 void trps_free (TRPS_INSTANCE *trps)
88 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
90 return tr_mq_pop(trps->mq);
93 void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
95 tr_mq_append(trps->mq, msg);
98 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
100 return trps->connect_interval.tv_sec;
103 void trps_set_connect_interval(TRPS_INSTANCE *trps, unsigned int interval)
105 trps->connect_interval.tv_sec=interval;
106 trps->connect_interval.tv_usec=0;
109 unsigned int trps_get_update_interval(TRPS_INSTANCE *trps)
111 return trps->update_interval.tv_sec;
114 void trps_set_update_interval(TRPS_INSTANCE *trps, unsigned int interval)
116 trps->update_interval.tv_sec=interval;
117 trps->update_interval.tv_usec=0;
120 unsigned int trps_get_sweep_interval(TRPS_INSTANCE *trps)
122 return trps->sweep_interval.tv_sec;
125 void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
127 trps->sweep_interval.tv_sec=interval;
128 trps->sweep_interval.tv_usec=0;
131 TRPC_INSTANCE *trps_find_trpc(TRPS_INSTANCE *trps, TRP_PEER *peer)
133 TRPC_INSTANCE *cur=NULL;
135 TR_NAME *peer_gssname=trp_peer_get_gssname(peer);
137 for (cur=trps->trpc; cur!=NULL; cur=trpc_get_next(cur)) {
138 name=trpc_get_gssname(cur);
139 if ((name!=NULL) && (0==tr_name_cmp(peer_gssname, name))) {
146 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
148 if (trps->conn==NULL)
151 trp_connection_append(trps->conn, new);
153 talloc_steal(trps, new);
156 /* ok to call more than once; guarantees connection no longer in the list.
157 * Caller is responsible for freeing the removed element afterwards. */
158 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove)
160 trps->conn=trp_connection_remove(trps->conn, remove);
163 void trps_add_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
165 if (trps->trpc==NULL)
168 trpc_append(trps->trpc, trpc);
170 talloc_steal(trps, trpc);
173 /* ok to call more than once; guarantees trpc no longer in the list.
174 * Caller is responsible for freeing the removed element afterwards. */
175 void trps_remove_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *remove)
177 trps->trpc=trpc_remove(trps->trpc, remove);
180 TRP_RC trps_send_msg(TRPS_INSTANCE *trps, TRP_PEER *peer, const char *msg)
182 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
183 TR_MQ_MSG *mq_msg=NULL;
186 TRPC_INSTANCE *trpc=NULL;
188 /* get the connection for this peer */
189 trpc=trps_find_trpc(trps, peer);
190 if ((trpc==NULL) || (trpc_get_status(trps->trpc)!=TRP_CONNECTION_UP)) {
191 /* We could just let these sit on the queue in the hopes that a connection
192 * is eventually established. However, we'd then have to ensure the queue
193 * didn't keep growing, etc. */
194 tr_warning("trps_send_msg: skipping message queued while TRPC connection not up.");
196 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND);
197 msg_dup=talloc_strdup(mq_msg, msg); /* get local copy in mq_msg context */
198 tr_mq_msg_set_payload(mq_msg, msg_dup, NULL); /* no need for a free() func */
199 trpc_mq_append(trpc, mq_msg);
202 talloc_free(tmp_ctx);
206 static int trps_listen (TRPS_INSTANCE *trps, int port)
213 struct sockaddr_storage storage;
214 struct sockaddr_in in4;
217 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
219 saddr->sin_port = htons (port);
220 saddr->sin_family = AF_INET;
221 saddr->sin_addr.s_addr = INADDR_ANY;
223 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
226 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
228 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
231 if (0 > (rc = listen(conn, 512)))
234 tr_debug("trps_listen: TRP Server listening on port %d", port);
238 #if 0 /* remove this if I forget to do so */
239 /* returns EACCES if authorization is denied */
240 int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data)
242 TRPS_INSTANCE *trps = (TRPS_INSTANCE *)data;
245 if (0!=trps->auth_handler(clientName, displayName, trps->cookie)) {
246 tr_debug("trps_auth_cb: client '%.*s' denied authorization.", displayName->length, displayName->value);
247 result=EACCES; /* denied */
254 /* get the currently selected route if available */
255 TRP_RENTRY *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
257 return trp_rtable_get_entry(trps->rtable, comm, realm, peer);
260 TRP_RENTRY *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
262 return trp_rtable_get_selected_entry(trps->rtable, comm, realm);
265 /* copy the result if you want to keep it */
266 TR_NAME *trps_get_next_hop(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
268 TRP_RENTRY *route=trps_get_selected_route(trps, comm, realm);
272 return trp_rentry_get_next_hop(route);
276 /* mark a route as retracted */
277 static void trps_retract_route(TRPS_INSTANCE *trps, TRP_RENTRY *entry)
279 trp_rentry_set_metric(entry, TRP_METRIC_INFINITY);
282 /* is this route retracted? */
283 static int trps_route_retracted(TRPS_INSTANCE *trps, TRP_RENTRY *entry)
285 return (trp_metric_is_infinite(trp_rentry_get_metric(entry)));
288 static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MSG **msg)
295 tr_debug("trps_read_message: started");
296 if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
297 *(trp_connection_get_gssctx(conn)),
300 tr_debug("trps_read_message: error");
306 tr_debug("trps_read_message(): Request Received, %u bytes.", (unsigned) buflen);
307 tr_debug("trps_read_message(): %.*s", buflen, buf);
309 *msg=tr_msg_decode(buf, buflen);
314 peer=trp_connection_get_peer(conn);
315 /* verify we received a message we support, otherwise drop it now */
316 switch (tr_msg_get_msg_type(*msg)) {
318 trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(peer));
322 trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(peer));
326 tr_debug("trps_read_message: received unsupported message from %.*s", peer->len, peer->buf);
327 tr_msg_free_decoded(*msg);
329 return TRP_UNSUPPORTED;
335 int trps_get_listener(TRPS_INSTANCE *trps,
336 TRPS_MSG_FUNC msg_handler,
337 TRP_AUTH_FUNC auth_handler,
338 const char *hostname,
344 if (0 > (listen = trps_listen(trps, port))) {
346 if (0 == strerror_r(errno, errbuf, 256)) {
347 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
349 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
354 /* opening port succeeded */
355 tr_debug("trps_get_listener: Opened port %d.", port);
357 /* make this socket non-blocking */
358 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
359 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
366 /* store the caller's request handler & cookie */
367 trps->msg_handler = msg_handler;
368 trps->auth_handler = auth_handler;
369 trps->hostname = talloc_strdup(trps, hostname);
371 trps->cookie = cookie;
377 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
379 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
383 /* try to establish a GSS context */
384 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
385 tr_notice("tr_trps_conn_thread: failed to authorize connection");
388 tr_notice("trps_handle_connection: authorized connection");
390 /* loop as long as the connection exists */
391 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
392 rc=trps_read_message(trps, conn, &msg);
395 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
399 trp_connection_close(conn);
403 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
407 tr_debug("trps_handle_connection: connection closed.");
408 talloc_free(tmp_ctx);
411 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
413 if (trp_upd_get_inforec(upd)==NULL) {
414 tr_notice("trps_validate_update: received TRP update with no info records.");
418 if (trp_upd_get_peer(upd)==NULL) {
419 tr_notice("trps_validate_update: received TRP update without origin peer information.");
426 /* ensure that the update could be accepted if feasible */
427 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
429 switch(trp_inforec_get_type(rec)) {
430 case TRP_INFOREC_TYPE_ROUTE:
431 if ((trp_inforec_get_comm(rec)==NULL)
432 || (trp_inforec_get_realm(rec)==NULL)
433 || (trp_inforec_get_trust_router(rec)==NULL)
434 || (trp_inforec_get_next_hop(rec)==NULL)) {
435 tr_debug("trps_validate_inforec: missing record info.");
439 /* check for valid metric */
440 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
441 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
445 /* check for valid interval */
446 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
447 tr_debug("trps_validate_inforec: invalid interval.");
453 tr_notice("trps_validate_inforec: unsupported record type.");
454 return TRP_UNSUPPORTED;
460 /* link cost to a peer */
461 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
466 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
468 TRP_RENTRY *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
470 return TRP_METRIC_INFINITY;
471 return trp_rentry_get_metric(entry) + trps_cost(trps, peer);
474 static int trps_check_feasibility(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
476 unsigned int rec_metric=trp_inforec_get_metric(rec);
477 unsigned int new_metric=0;
478 unsigned int current_metric=0;
479 TR_NAME *next_hop=NULL;
481 /* we check these in the validation stage, but just in case... */
482 if (trp_metric_is_invalid(rec_metric))
485 /* retractions (aka infinite metrics) are always feasible */
486 if (trp_metric_is_infinite(rec_metric))
489 /* updates from our current next hop are always feasible*/
490 next_hop=trps_get_next_hop(trps,
491 trp_inforec_get_comm(rec),
492 trp_inforec_get_realm(rec));;
494 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
499 /* compare the existing metric we advertise to what we would advertise
500 * if we accept this update */
501 current_metric=trps_advertised_metric(trps,
502 trp_inforec_get_comm(rec),
503 trp_inforec_get_realm(rec),
504 trp_inforec_get_next_hop(rec));
505 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
506 if (new_metric <= current_metric)
512 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
513 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
515 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
516 if (0!=clock_gettime(CLOCK_REALTIME, ts)) {
517 tr_err("trps_compute_expiry: could not read realtime clock.");
521 ts->tv_sec += small_factor*interval;
525 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
527 TRP_RENTRY *entry=NULL;
529 entry=trp_rtable_get_entry(trps->rtable,
530 trp_inforec_get_comm(rec),
531 trp_inforec_get_realm(rec),
532 trp_inforec_get_next_hop(rec));
534 entry=trp_rentry_new(NULL);
536 tr_err("trps_accept_update: unable to allocate new entry.");
540 trp_rentry_set_apc(entry, tr_dup_name(trp_inforec_get_comm(rec)));
541 trp_rentry_set_realm(entry, tr_dup_name(trp_inforec_get_realm(rec)));
542 trp_rentry_set_peer(entry, tr_dup_name(trp_inforec_get_next_hop(rec)));
543 trp_rentry_set_trust_router(entry, tr_dup_name(trp_inforec_get_trust_router(rec)));
544 trp_rentry_set_next_hop(entry, tr_dup_name(trp_inforec_get_next_hop(rec)));
545 if ((trp_rentry_get_apc(entry)==NULL)
546 ||(trp_rentry_get_realm(entry)==NULL)
547 ||(trp_rentry_get_peer(entry)==NULL)
548 ||(trp_rentry_get_trust_router(entry)==NULL)
549 ||(trp_rentry_get_next_hop(entry)==NULL)) {
550 /* at least one field could not be allocated */
551 tr_err("trps_accept_update: unable to allocate all fields for entry.");
552 trp_rentry_free(entry);
555 trp_rtable_add(trps->rtable, entry);
558 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
559 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
560 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
561 * time unset on a new route entry. */
562 tr_debug("trps_accept_update: accepting route update.");
563 trp_rentry_set_metric(entry, trp_inforec_get_metric(rec));
564 trp_rentry_set_interval(entry, trp_inforec_get_interval(rec));
565 if (!trps_route_retracted(trps, entry)) {
566 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
567 trp_rentry_set_expiry(entry, trps_compute_expiry(trps,
568 trp_rentry_get_interval(entry),
569 trp_rentry_get_expiry(entry)));
574 /* TODO: handle community updates */
575 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
578 TRP_INFOREC *rec=NULL;
579 TRP_RENTRY *route=NULL;
581 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
582 tr_notice("trps_handle_update: received invalid TRP update.");
586 rec=trp_upd_get_inforec(upd);
587 for (;rec!=NULL; rec=trp_inforec_get_next(rec)) {
588 /* validate/sanity check the record update */
589 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
590 tr_notice("trps_handle_update: invalid record in TRP update.");
594 /* determine feasibility */
595 feas=trps_check_feasibility(trps, rec);
596 tr_debug("trps_handle_update: record feasibility=%d", feas);
598 /* do we have an existing route? */
599 route=trps_get_route(trps, trp_inforec_get_comm(rec), trp_inforec_get_realm(rec), trp_inforec_get_next_hop(rec));
601 /* there was a route table entry already */
602 tr_debug("trps_handle_updates: route entry already exists.");
604 /* Update is feasible. Accept it. */
605 trps_accept_update(trps, rec);
607 /* Update is infeasible. Ignore it unless the trust router has changed. */
608 if (0!=tr_name_cmp(trp_rentry_get_trust_router(route),
609 trp_inforec_get_trust_router(rec))) {
610 /* the trust router associated with the route has changed, treat update as a retraction */
611 trps_retract_route(trps, route);
615 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
616 tr_debug("trps_handle_update: no route entry exists yet.");
617 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
618 trps_accept_update(trps, rec);
624 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
625 static TRP_RENTRY *trps_find_best_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *exclude_peer)
627 TRP_RENTRY **entry=NULL;
628 TRP_RENTRY *best=NULL;
631 unsigned int kk_min=0;
632 unsigned int min_metric=TRP_METRIC_INFINITY;
634 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
635 for (kk=0; kk<n_entry; kk++) {
636 if (trp_rentry_get_metric(entry[kk]) < min_metric) {
637 if ((exclude_peer==NULL) || (0!=tr_name_cmp(trp_rentry_get_peer(entry[kk]),
640 min_metric=trp_rentry_get_metric(entry[kk]);
644 if (trp_metric_is_finite(min_metric));
651 /* TODO: think this through more carefully. At least ought to add hysteresis
652 * to avoid flapping between routers or routes. */
653 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
655 size_t n_apc=0, ii=0;
656 TR_NAME **apc=trp_rtable_get_apcs(trps->rtable, &n_apc);
657 size_t n_realm=0, jj=0;
658 TR_NAME **realm=NULL;
659 TRP_RENTRY *best_route=NULL, *cur_route=NULL;
660 unsigned int best_metric=0, cur_metric=0;
662 for (ii=0; ii<n_apc; ii++) {
663 realm=trp_rtable_get_apc_realms(trps->rtable, apc[ii], &n_realm);
664 for (jj=0; jj<n_realm; jj++) {
665 best_route=trps_find_best_route(trps, apc[ii], realm[jj], NULL);
666 if (best_route==NULL)
667 best_metric=TRP_METRIC_INFINITY;
669 best_metric=trp_rentry_get_metric(best_route);
671 cur_route=trps_get_selected_route(trps, apc[ii], realm[jj]);
672 if (cur_route!=NULL) {
673 cur_metric=trp_rentry_get_metric(cur_route);
674 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
675 trp_rentry_set_selected(cur_route, 0);
676 trp_rentry_set_selected(best_route, 1);
677 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
678 trp_rentry_set_selected(cur_route, 0);
679 } else if (trp_metric_is_finite(best_metric))
680 trp_rentry_set_selected(best_route, 1);
684 realm=NULL; n_realm=0;
693 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
697 switch (tr_msg_get_msg_type(tr_msg)) {
699 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
700 if (rc==TRP_SUCCESS) {
701 rc=trps_update_active_routes(trps);
706 return TRP_UNSUPPORTED;
709 /* unknown error or one we don't care about (e.g., TID messages) */
714 /* true if curtime >= expiry */
715 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
717 return ((curtime->tv_sec > expiry->tv_sec)
718 || ((curtime->tv_sec == expiry->tv_sec)
719 &&(curtime->tv_nsec > expiry->tv_nsec)));
722 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
723 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
724 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
726 struct timespec sweep_time={0,0};
727 TRP_RENTRY **entry=NULL;
731 /* use a single time for the entire sweep */
732 if (0!=clock_gettime(CLOCK_REALTIME, &sweep_time)) {
733 tr_err("trps_sweep_routes: could not read realtime clock.");
735 sweep_time.tv_nsec=0;
739 entry=trp_rtable_get_entries(trps->rtable, &n_entry); /* must talloc_free *entry */
741 /* loop over the entries */
742 for (ii=0; ii<n_entry; ii++) {
743 if (trps_expired(trp_rentry_get_expiry(entry[ii]), &sweep_time)) {
744 tr_debug("trps_sweep_routes: route expired.");
745 if (!trp_metric_is_finite(trp_rentry_get_metric(entry[ii]))) {
747 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
748 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
751 /* set metric to infinity and reset timer */
752 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
753 trp_rentry_set_metric(entry[ii], TRP_METRIC_INFINITY);
754 trp_rentry_set_expiry(entry[ii], trps_compute_expiry(trps,
755 trp_rentry_get_interval(entry[ii]),
756 trp_rentry_get_expiry(entry[ii])));
765 /* select the correct route to comm/realm to be announced to peer */
766 static TRP_RENTRY *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_gssname)
770 /* Take the currently selected route unless it is through the peer we're sending the update to.
771 * I.e., enforce the split horizon rule. */
772 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
774 /* No selected route, this should only happen if the only route has been retracted,
775 * in which case we do not want to advertise it. */
778 tr_debug("trps_select_realm_update: %s vs %s", peer_gssname->buf,
779 trp_rentry_get_peer(route)->buf);
780 if (0==tr_name_cmp(peer_gssname, trp_rentry_get_peer(route))) {
781 tr_debug("trps_select_realm_update: matched, finding alternate route");
782 /* the selected entry goes through the peer we're reporting to, choose an alternate */
783 route=trps_find_best_route(trps, comm, realm, peer_gssname);
784 if ((route==NULL) || (!trp_metric_is_finite(trp_rentry_get_metric(route))))
785 return NULL; /* don't advertise a nonexistent or retracted route */
790 /* returns an array of pointers to updates (*not* an array of updates). Returns number of entries
791 * via n_update parameter. (The allocated space will generally be larger than required, see note in
793 static TRP_RENTRY **trps_select_updates_for_peer(TALLOC_CTX *memctx, TRPS_INSTANCE *trps, TR_NAME *peer_gssname, size_t *n_update)
796 TR_NAME **apc=trp_rtable_get_apcs(trps->rtable, &n_apc);
797 TR_NAME **realm=NULL;
800 TRP_RENTRY *best=NULL;
801 TRP_RENTRY **result=NULL;
804 /* Need to allocate space for the results. For simplicity, we just allocate a block
805 * with space for every route table entry to be returned. This is guaranteed to be large
806 * enough. If the routing table gets very large, this may be wasteful, but that seems
807 * unlikely to be significant in the near future. */
808 result=talloc_array(memctx, TRP_RENTRY *, trp_rtable_size(trps->rtable));
815 for (ii=0; ii<n_apc; ii++) {
816 realm=trp_rtable_get_apc_realms(trps->rtable, apc[ii], &n_realm);
817 for (jj=0; jj<n_realm; jj++) {
818 best=trps_select_realm_update(trps, apc[ii], realm[jj], peer_gssname);
820 result[n_used++]=best;
834 /* convert an rentry into a new trp update info record */
835 static TRP_INFOREC *trps_rentry_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_RENTRY *entry)
837 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
838 unsigned int linkcost=0;
841 linkcost=trp_peer_get_linkcost(trps_get_peer(trps,
842 trp_rentry_get_next_hop(entry)));
844 /* Note that we leave the next hop empty since the recipient fills that in.
845 * This is where we add the link cost (currently always 1) to the next peer. */
846 if ((trp_inforec_set_comm(rec, trp_rentry_dup_apc(entry)) != TRP_SUCCESS)
847 ||(trp_inforec_set_realm(rec, trp_rentry_dup_realm(entry)) != TRP_SUCCESS)
848 ||(trp_inforec_set_trust_router(rec, trp_rentry_dup_trust_router(entry)) != TRP_SUCCESS)
849 ||(trp_inforec_set_metric(rec, trp_rentry_get_metric(entry)+linkcost) != TRP_SUCCESS)
850 ||(trp_inforec_set_interval(rec, trps_get_update_interval(trps)) != TRP_SUCCESS)) {
851 tr_err("trps_rentry_to_inforec: error creating route update.");
859 TRP_RC trps_scheduled_update(TRPS_INSTANCE *trps)
861 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
862 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
864 TR_MSG msg; /* not a pointer! */
866 TRP_RENTRY **update_list=NULL;
867 TRP_INFOREC *rec=NULL;
868 size_t n_updates=0, ii=0;
871 TR_NAME *peer_gssname=NULL;
874 tr_err("trps_scheduled_update: failed to allocate peer table iterator.");
875 talloc_free(tmp_ctx);
879 for (peer=trp_ptable_iter_first(iter, trps->ptable);
881 peer=trp_ptable_iter_next(iter))
883 peer_gssname=trp_peer_get_gssname(peer);
884 if (!trps_peer_connected(trps, peer)) {
885 tr_debug("trps_scheduled_update: no TRP connection to %.*s, skipping.",
886 peer_gssname->len, peer_gssname->buf);
889 tr_debug("trps_scheduled_update: preparing scheduled route update for %.*s",
890 peer_gssname->len, peer_gssname->buf);
891 /* do not fill in peer, recipient does that */
892 update_list=trps_select_updates_for_peer(tmp_ctx, trps, peer_gssname, &n_updates);
893 if ((n_updates>0) && (update_list!=NULL)) {
894 tr_debug("trps_scheduled_update: sending %u update records.", (unsigned int)n_updates);
895 upd=trp_upd_new(tmp_ctx);
897 for (ii=0; ii<n_updates; ii++) {
898 rec=trps_rentry_to_inforec(tmp_ctx, trps, update_list[ii]);
900 tr_err("trps_scheduled_update: could not create all update records.");
904 trp_upd_add_inforec(upd, rec);
906 talloc_free(update_list);
909 /* now encode the update message */
910 tr_msg_set_trp_upd(&msg, upd);
911 encoded=tr_msg_encode(&msg);
913 tr_err("trps_scheduled_update: error encoding update.");
918 tr_debug("trps_scheduled_update: adding message to queue.");
919 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
920 tr_err("trps_scheduled_update: error queueing update.");
922 tr_debug("trps_scheduled_update: update queued successfully.");
925 tr_msg_free_encoded(encoded);
932 trp_ptable_iter_free(iter);
933 talloc_free(tmp_ctx);
937 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_RENTRY *route)
939 trp_rtable_add(trps->rtable, route); /* should return status */
943 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
945 return trp_ptable_add(trps->ptable, peer);
948 TRP_PEER *trps_get_peer(TRPS_INSTANCE *trps, TR_NAME *gssname)
950 return trp_ptable_find(trps->ptable, gssname);
953 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
955 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
959 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)