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.
45 #include <trust_router/tr_name.h>
46 #include <trp_internal.h>
48 #include <trp_ptable.h>
49 #include <trp_rtable.h>
53 static int trps_destructor(void *object)
55 TRPS_INSTANCE *trps=talloc_get_type_abort(object, TRPS_INSTANCE);
56 if (trps->rtable!=NULL)
57 trp_rtable_free(trps->rtable);
61 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
63 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
70 trps->update_interval=(struct timeval){0,0};
71 trps->sweep_interval=(struct timeval){0,0};
74 trps->mq=tr_mq_new(trps);
76 /* failed to allocate mq */
82 if (trps_init_rtable(trps) != TRP_SUCCESS) {
83 /* failed to allocate rtable */
88 talloc_set_destructor((void *)trps, trps_destructor);
93 /* create a new route table, first discarding an old one if necessary */
94 TRP_RC trps_init_rtable(TRPS_INSTANCE *trps)
96 if (trps->rtable != NULL) {
97 trp_rtable_free(trps->rtable);
101 trps->rtable=trp_rtable_new();
102 if (trps->rtable==NULL) {
108 void trps_clear_rtable(TRPS_INSTANCE *trps)
110 trp_rtable_clear(trps->rtable);
113 void trps_free (TRPS_INSTANCE *trps)
119 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
121 return tr_mq_pop(trps->mq);
124 void trps_mq_add(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
126 tr_mq_add(trps->mq, msg);
129 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
131 return trps->connect_interval.tv_sec;
134 void trps_set_connect_interval(TRPS_INSTANCE *trps, unsigned int interval)
136 trps->connect_interval.tv_sec=interval;
137 trps->connect_interval.tv_usec=0;
140 unsigned int trps_get_update_interval(TRPS_INSTANCE *trps)
142 return trps->update_interval.tv_sec;
145 void trps_set_update_interval(TRPS_INSTANCE *trps, unsigned int interval)
147 trps->update_interval.tv_sec=interval;
148 trps->update_interval.tv_usec=0;
151 unsigned int trps_get_sweep_interval(TRPS_INSTANCE *trps)
153 return trps->sweep_interval.tv_sec;
156 void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
158 trps->sweep_interval.tv_sec=interval;
159 trps->sweep_interval.tv_usec=0;
162 void trps_set_ctable(TRPS_INSTANCE *trps, TR_COMM_TABLE *comm)
164 if (trps->ctable!=NULL)
165 tr_comm_table_free(trps->ctable);
169 void trps_set_ptable(TRPS_INSTANCE *trps, TRP_PTABLE *ptable)
171 if (trps->ptable!=NULL)
172 trp_ptable_free(trps->ptable);
176 void trps_set_peer_status_callback(TRPS_INSTANCE *trps, void (*cb)(TRP_PEER *, void *), void *cookie)
178 TRP_PTABLE_ITER *iter=NULL;
180 if (trps->ptable==NULL)
183 iter=trp_ptable_iter_new(NULL);
184 for (peer=trp_ptable_iter_first(iter, trps->ptable); peer!=NULL; peer=trp_ptable_iter_next(iter))
185 trp_peer_set_conn_status_cb(peer, cb, cookie);
186 trp_ptable_iter_free(iter);
189 /* Get the label peers will know us by - needs to match trp_peer_get_label() output.
190 * There is no get, only dup, because we don't store the label except when requested. */
191 TR_NAME *trps_dup_label(TRPS_INSTANCE *trps)
193 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
195 char *s=talloc_asprintf(tmp_ctx, "%s:%u", trps->hostname, trps->port);
198 label=tr_new_name(s);
201 talloc_free(tmp_ctx);
205 TRPC_INSTANCE *trps_find_trpc(TRPS_INSTANCE *trps, TRP_PEER *peer)
207 TRPC_INSTANCE *cur=NULL;
209 TR_NAME *peer_servicename=trp_peer_get_servicename(peer);
211 for (cur=trps->trpc; cur!=NULL; cur=trpc_get_next(cur)) {
212 name=trpc_get_gssname(cur);
213 if ((name!=NULL) && (0==tr_name_cmp(peer_servicename, name))) {
220 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
222 if (trps->conn==NULL)
225 trp_connection_append(trps->conn, new);
227 talloc_steal(trps, new);
230 /* ok to call more than once; guarantees connection no longer in the list.
231 * Caller is responsible for freeing the removed element afterwards. */
232 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove)
234 trps->conn=trp_connection_remove(trps->conn, remove);
237 void trps_add_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *trpc)
239 if (trps->trpc==NULL)
242 trpc_append(trps->trpc, trpc);
244 talloc_steal(trps, trpc);
247 /* ok to call more than once; guarantees trpc no longer in the list.
248 * Caller is responsible for freeing the removed element afterwards. */
249 void trps_remove_trpc(TRPS_INSTANCE *trps, TRPC_INSTANCE *remove)
251 trps->trpc=trpc_remove(trps->trpc, remove);
254 TRP_RC trps_send_msg(TRPS_INSTANCE *trps, TRP_PEER *peer, const char *msg)
256 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
257 TR_MQ_MSG *mq_msg=NULL;
260 TRPC_INSTANCE *trpc=NULL;
262 /* get the connection for this peer */
263 trpc=trps_find_trpc(trps, peer);
264 /* instead, let's let that happen and then clear the queue when an attempt to
267 tr_warning("trps_send_msg: skipping message queued for missing TRP client entry.");
269 mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND, TR_MQ_PRIO_NORMAL);
270 msg_dup=talloc_strdup(mq_msg, msg); /* get local copy in mq_msg context */
271 tr_mq_msg_set_payload(mq_msg, msg_dup, NULL); /* no need for a free() func */
272 trpc_mq_add(trpc, mq_msg);
275 talloc_free(tmp_ctx);
279 static int trps_listen (TRPS_INSTANCE *trps, int port)
286 struct sockaddr_storage storage;
287 struct sockaddr_in in4;
290 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
292 saddr->sin_port = htons (port);
293 saddr->sin_family = AF_INET;
294 saddr->sin_addr.s_addr = INADDR_ANY;
296 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
299 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
301 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
304 if (0 > (rc = listen(conn, 512)))
307 tr_debug("trps_listen: TRP Server listening on port %d", port);
311 /* get the currently selected route if available */
312 TRP_ROUTE *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
314 return trp_rtable_get_entry(trps->rtable, comm, realm, peer);
317 TRP_ROUTE *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
319 tr_debug("trps_get_selected_route: entered. trps=%p, comm=%p, realm=%p", trps, comm, realm);
320 return trp_rtable_get_selected_entry(trps->rtable, comm, realm);
323 /* copy the result if you want to keep it */
324 TR_NAME *trps_get_next_hop(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
326 TRP_ROUTE *route=trps_get_selected_route(trps, comm, realm);
330 return trp_route_get_next_hop(route);
334 /* mark a route as retracted */
335 static void trps_retract_route(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
337 trp_route_set_metric(entry, TRP_METRIC_INFINITY);
338 trp_route_set_triggered(entry, 1);
341 /* is this route retracted? */
342 static int trps_route_retracted(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
344 return (trp_metric_is_infinite(trp_route_get_metric(entry)));
347 static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MSG **msg)
352 TRP_PEER *peer=NULL; /* entry in the peer table */
353 TR_NAME *conn_peer=NULL; /* name from the TRP_CONN, which comes from the gss context */
355 tr_debug("trps_read_message: started");
356 if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
357 *(trp_connection_get_gssctx(conn)),
360 tr_debug("trps_read_message: error");
366 tr_debug("trps_read_message: message received, %u bytes.", (unsigned) buflen);
367 tr_debug("trps_read_message: %.*s", buflen, buf);
369 *msg=tr_msg_decode(buf, buflen);
374 conn_peer=trp_connection_get_peer(conn);
375 if (conn_peer==NULL) {
376 tr_err("trps_read_message: connection has no peer name");
380 peer=trps_get_peer_by_gssname(trps, conn_peer);
382 tr_err("trps_read_message: could not find peer with gssname=%s", trp_connection_get_gssname(conn));
386 /* verify we received a message we support, otherwise drop it now */
387 switch (tr_msg_get_msg_type(*msg)) {
389 trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(conn_peer));
390 trp_upd_set_next_hop(tr_msg_get_trp_upd(*msg), trp_peer_get_server(peer), 0); /* TODO: 0 should be the configured TID port */
394 trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(conn_peer));
398 tr_debug("trps_read_message: received unsupported message from %.*s", conn_peer->len, conn_peer->buf);
399 tr_msg_free_decoded(*msg);
401 return TRP_UNSUPPORTED;
407 int trps_get_listener(TRPS_INSTANCE *trps,
408 TRPS_MSG_FUNC msg_handler,
409 TRP_AUTH_FUNC auth_handler,
410 const char *hostname,
416 if (0 > (listen = trps_listen(trps, port))) {
418 if (0 == strerror_r(errno, errbuf, 256)) {
419 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
421 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
426 /* opening port succeeded */
427 tr_debug("trps_get_listener: Opened port %d.", port);
429 /* make this socket non-blocking */
430 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
431 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
438 /* store the caller's request handler & cookie */
439 trps->msg_handler = msg_handler;
440 trps->auth_handler = auth_handler;
441 trps->hostname = talloc_strdup(trps, hostname);
443 trps->cookie = cookie;
449 TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
451 /* try to establish a GSS context */
452 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
453 tr_notice("trps_authorize_connection: failed to authorize connection");
454 trp_connection_close(conn);
457 tr_notice("trps_authorize_connection: authorized connection");
461 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
466 /* loop as long as the connection exists */
467 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
468 rc=trps_read_message(trps, conn, &msg);
471 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
475 trp_connection_close(conn);
479 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
483 tr_debug("trps_handle_connection: connection closed.");
486 /* TODO: check realm/comm, now part of the update instead of inforec */
487 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
490 tr_notice("trps_validate_update: null TRP update.");
494 if (trp_upd_get_realm(upd)==NULL) {
495 tr_notice("trps_validate_update: received TRP update without realm.");
499 if (trp_upd_get_comm(upd)==NULL) {
500 tr_notice("trps_validate_update: received TRP update without community.");
504 if (trp_upd_get_inforec(upd)==NULL) {
505 tr_notice("trps_validate_update: received TRP update with no info records.");
509 if (trp_upd_get_peer(upd)==NULL) {
510 tr_notice("trps_validate_update: received TRP update without origin peer information.");
518 /* ensure that the update could be accepted if feasible */
519 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
521 switch(trp_inforec_get_type(rec)) {
522 case TRP_INFOREC_TYPE_ROUTE:
523 if ((trp_inforec_get_trust_router(rec)==NULL)
524 || (trp_inforec_get_next_hop(rec)==NULL)) {
525 tr_debug("trps_validate_inforec: missing record info.");
529 /* check for valid metric */
530 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
531 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
535 /* check for valid interval */
536 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
537 tr_debug("trps_validate_inforec: invalid interval.");
542 case TRP_INFOREC_TYPE_COMMUNITY:
543 /* TODO: validate community updates */
547 tr_notice("trps_validate_inforec: unsupported record type.");
548 return TRP_UNSUPPORTED;
554 /* link cost to a peer */
555 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
560 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
562 TRP_ROUTE *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
564 return TRP_METRIC_INFINITY;
565 return trp_route_get_metric(entry) + trps_cost(trps, peer);
568 static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
570 unsigned int rec_metric=trp_inforec_get_metric(rec);
571 unsigned int new_metric=0;
572 unsigned int current_metric=0;
573 TR_NAME *next_hop=NULL;
575 /* we check these in the validation stage, but just in case... */
576 if (trp_metric_is_invalid(rec_metric))
579 /* retractions (aka infinite metrics) are always feasible */
580 if (trp_metric_is_infinite(rec_metric))
583 /* updates from our current next hop are always feasible*/
584 next_hop=trps_get_next_hop(trps, comm, realm);
586 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
591 /* compare the existing metric we advertise to what we would advertise
592 * if we accept this update */
593 current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
594 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
595 if (new_metric <= current_metric)
601 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
602 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
604 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
605 if (0!=clock_gettime(CLOCK_REALTIME, ts)) {
606 tr_err("trps_compute_expiry: could not read realtime clock.");
610 ts->tv_sec += small_factor*interval;
614 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
616 TRP_ROUTE *entry=NULL;
618 entry=trp_rtable_get_entry(trps->rtable,
619 trp_upd_get_comm(upd),
620 trp_upd_get_realm(upd),
621 trp_inforec_get_next_hop(rec));
623 entry=trp_route_new(NULL);
625 tr_err("trps_accept_update: unable to allocate new entry.");
629 trp_route_set_comm(entry, trp_upd_dup_comm(upd));
630 trp_route_set_realm(entry, trp_upd_dup_realm(upd));
631 trp_route_set_peer(entry, trp_upd_dup_peer(upd));
632 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
633 trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
634 /* TODO: pass next hop port (now defaults to TID_PORT) --jlr */
635 if ((trp_route_get_comm(entry)==NULL)
636 ||(trp_route_get_realm(entry)==NULL)
637 ||(trp_route_get_peer(entry)==NULL)
638 ||(trp_route_get_trust_router(entry)==NULL)
639 ||(trp_route_get_next_hop(entry)==NULL)) {
640 /* at least one field could not be allocated */
641 tr_err("trps_accept_update: unable to allocate all fields for entry.");
642 trp_route_free(entry);
645 trp_rtable_add(trps->rtable, entry);
648 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
649 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
650 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
651 * time unset on a new route entry. */
652 tr_debug("trps_accept_update: accepting route update.");
653 trp_route_set_metric(entry, trp_inforec_get_metric(rec));
654 trp_route_set_interval(entry, trp_inforec_get_interval(rec));
656 /* check whether the trust router has changed */
657 if (0!=tr_name_cmp(trp_route_get_trust_router(entry),
658 trp_inforec_get_trust_router(rec))) {
659 /* The name changed. Set this route as triggered. */
660 tr_debug("trps_accept_update: trust router for route changed.");
661 trp_route_set_triggered(entry, 1);
662 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec)); /* frees old name */
664 if (!trps_route_retracted(trps, entry)) {
665 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
666 trp_route_set_expiry(entry, trps_compute_expiry(trps,
667 trp_route_get_interval(entry),
668 trp_route_get_expiry(entry)));
674 static TRP_RC trps_handle_inforec_route(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
676 TRP_ROUTE *route=NULL;
679 /* determine feasibility */
680 feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
681 tr_debug("trps_handle_update: record feasibility=%d", feas);
683 /* do we have an existing route? */
684 route=trps_get_route(trps,
685 trp_upd_get_comm(upd),
686 trp_upd_get_realm(upd),
687 trp_upd_get_peer(upd));
689 /* there was a route table entry already */
690 tr_debug("trps_handle_updates: route entry already exists.");
692 /* Update is feasible. Accept it. */
693 trps_accept_update(trps, upd, rec);
695 /* Update is infeasible. Ignore it unless the trust router has changed. */
696 if (0!=tr_name_cmp(trp_route_get_trust_router(route),
697 trp_inforec_get_trust_router(rec))) {
698 /* the trust router associated with the route has changed, treat update as a retraction */
699 trps_retract_route(trps, route);
703 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
704 tr_debug("trps_handle_update: no route entry exists yet.");
705 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
706 trps_accept_update(trps, upd, rec);
712 static int trps_name_in_provenance(TR_NAME *name, json_t *prov)
715 TR_NAME *this_name=NULL;
719 return 0; /* no provenance list, so it has no names in it */
721 /* now check to see if name is in the provenance */
722 for (ii=0; ii<json_array_size(prov); ii++) {
723 s=json_string_value(json_array_get(prov, ii));
725 tr_debug("trps_name_in_provenance: empty entry in provenance list.");
729 this_name=tr_new_name(s);
730 if (this_name==NULL) {
731 tr_debug("trps_name_in_provenance: unable to allocate name.");
734 if (0==tr_name_cmp(name, this_name)) {
735 tr_free_name(this_name);
738 tr_free_name(this_name);
743 static TR_COMM *trps_create_new_comm(TALLOC_CTX *mem_ctx, TR_NAME *comm_id, TRP_INFOREC *rec)
745 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
746 TR_COMM *comm=tr_comm_new(tmp_ctx);
749 tr_debug("trps_create_new_comm: unable to allocate new community.");
752 /* fill in the community with info */
753 tr_comm_set_id(comm, tr_dup_name(comm_id));
754 if (tr_comm_get_id(comm)==NULL) {
755 tr_debug("trps_create_new_comm: unable to allocate community name.");
759 tr_comm_set_type(comm, trp_inforec_get_comm_type(rec));
760 if (trp_inforec_get_apcs(rec)!=NULL) {
761 tr_comm_set_apcs(comm, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
762 if (tr_comm_get_apcs(comm)==NULL) {
763 tr_debug("trps_create_new_comm: unable to allocate APC list.");
768 if (trp_inforec_get_owner_realm(rec)!=NULL) {
769 tr_comm_set_owner_realm(comm, tr_dup_name(trp_inforec_get_owner_realm(rec)));
770 if (tr_comm_get_owner_realm(comm)==NULL) {
771 tr_debug("trps_create_new_comm: unable to allocate owner realm name.");
776 if (trp_inforec_get_owner_contact(rec)!=NULL) {
777 tr_comm_set_owner_contact(comm, tr_dup_name(trp_inforec_get_owner_contact(rec)));
778 if (tr_comm_get_owner_contact(comm)==NULL) {
779 tr_debug("trps_create_new_comm: unable to allocate owner contact.");
784 comm->expiration_interval=trp_inforec_get_exp_interval(rec);
785 talloc_steal(mem_ctx, comm);
788 talloc_free(tmp_ctx);
792 static TR_RP_REALM *trps_create_new_rp_realm(TALLOC_CTX *mem_ctx, TR_NAME *realm_id, TRP_INFOREC *rec)
794 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
795 TR_RP_REALM *rp=tr_rp_realm_new(tmp_ctx);
798 tr_debug("trps_create_new_rp_realm: unable to allocate new realm.");
801 /* fill in the realm */
802 tr_rp_realm_set_id(rp, tr_dup_name(realm_id));
803 if (tr_rp_realm_get_id(rp)==NULL) {
804 tr_debug("trps_create_new_rp_realm: unable to allocate realm name.");
808 talloc_steal(mem_ctx, rp);
811 talloc_free(tmp_ctx);
815 static TR_IDP_REALM *trps_create_new_idp_realm(TALLOC_CTX *mem_ctx, TR_NAME *realm_id, TRP_INFOREC *rec)
817 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
818 TR_IDP_REALM *idp=tr_idp_realm_new(tmp_ctx);
821 tr_debug("trps_create_new_idp_realm: unable to allocate new realm.");
824 /* fill in the realm */
825 tr_idp_realm_set_id(idp, tr_dup_name(realm_id));
826 if (tr_idp_realm_get_id(idp)==NULL) {
827 tr_debug("trps_create_new_idp_realm: unable to allocate realm name.");
831 if (trp_inforec_get_apcs(rec)!=NULL) {
832 tr_idp_realm_set_apcs(idp, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
833 if (tr_idp_realm_get_apcs(idp)==NULL) {
834 tr_debug("trps_create_new_idp_realm: unable to allocate APC list.");
839 idp->origin=TR_REALM_DISCOVERED;
841 talloc_steal(mem_ctx, idp);
844 talloc_free(tmp_ctx);
848 static TRP_RC trps_handle_inforec_comm(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
850 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
851 TR_NAME *comm_id=trp_upd_get_comm(upd);
852 TR_NAME *realm_id=trp_upd_get_realm(upd);
853 TR_NAME *origin_id=NULL;
854 TR_NAME *our_peer_label=NULL;
856 TR_RP_REALM *rp_realm=NULL;
857 TR_IDP_REALM *idp_realm=NULL;
858 struct timespec expiry={0,0};
861 if ((comm_id==NULL) || (realm_id==NULL))
864 origin_id=trp_inforec_dup_origin(rec);
868 /* see whether we want to add this */
869 our_peer_label=trps_dup_label(trps);
870 if (our_peer_label==NULL) {
871 tr_debug("trps_handle_inforec_comm: unable to allocate peer label.");
875 if (trps_name_in_provenance(our_peer_label, trp_inforec_get_provenance(rec)))
876 tr_debug("trps_handle_inforec_comm: rejecting community inforec to avoid loop.");
878 /* no loop occurring, accept the update */
879 comm=tr_comm_table_find_comm(trps->ctable, comm_id);
881 tr_debug("trps_handle_inforec_comm: unknown community %.*s in inforec, creating it.",
882 comm_id->len, comm_id->buf);
883 comm=trps_create_new_comm(tmp_ctx, comm_id, rec);
885 tr_debug("trps_handle_inforec_comm: unable to create new community.");
888 tr_comm_table_add_comm(trps->ctable, comm);
890 /* TODO: see if other comm data match the new inforec and update or complain */
892 trps_compute_expiry(trps, trp_inforec_get_interval(rec), &expiry);
893 if ((expiry.tv_sec==0)&&(expiry.tv_nsec==0))
896 switch (trp_inforec_get_role(rec)) {
898 rp_realm=tr_rp_realm_lookup(trps->ctable->rp_realms, realm_id);
899 if (rp_realm==NULL) {
900 tr_debug("trps_handle_inforec_comm: unknown RP realm %.*s in inforec, creating it.",
901 realm_id->len, realm_id->buf);
902 rp_realm=trps_create_new_rp_realm(tmp_ctx, realm_id, rec);
903 if (rp_realm==NULL) {
904 tr_debug("trps_handle_inforec_comm: unable to create new RP realm.");
905 /* we may leave an unused community in the table, but it will only last until
906 * the next table sweep if it does not get any realms before that happens */
909 tr_rp_realm_add(trps->ctable->rp_realms, rp_realm);
911 /* TODO: if realm existed, see if data match the new inforec and update or complain */
912 tr_comm_add_rp_realm(trps->ctable, comm, rp_realm, trp_inforec_get_provenance(rec), &expiry);
913 tr_debug("trps_handle_inforec_comm: added RP realm %.*s to comm %.*s (origin %.*s).",
914 realm_id->len, realm_id->buf,
915 comm_id->len, comm_id->buf,
916 origin_id->len, origin_id->buf);
919 idp_realm=tr_idp_realm_lookup(trps->ctable->idp_realms, realm_id);
920 if (idp_realm==NULL) {
921 tr_debug("trps_handle_inforec_comm: unknown IDP realm %.*s in inforec, creating it.",
922 realm_id->len, realm_id->buf);
923 idp_realm=trps_create_new_idp_realm(tmp_ctx, realm_id, rec);
924 if (idp_realm==NULL) {
925 tr_debug("trps_handle_inforec_comm: unable to create new IDP realm.");
926 /* we may leave an unused community in the table, but it will only last until
927 * the next table sweep if it does not get any realms before that happens */
930 tr_idp_realm_add(trps->ctable->idp_realms, idp_realm);
932 /* TODO: if realm existed, see if data match the new inforec and update or complain */
933 tr_comm_add_idp_realm(trps->ctable, comm, idp_realm, trp_inforec_get_provenance(rec), &expiry);
934 tr_debug("trps_handle_inforec_comm: added IDP realm %.*s to comm %.*s (origin %.*s).",
935 realm_id->len, realm_id->buf,
936 comm_id->len, comm_id->buf,
937 origin_id->len, origin_id->buf);
940 tr_debug("trps_handle_inforec_comm: unable to add realm.");
948 if (our_peer_label!=NULL)
949 tr_free_name(our_peer_label);
951 tr_free_name(origin_id);
952 talloc_free(tmp_ctx);
956 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
958 TRP_INFOREC *rec=NULL;
960 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
961 tr_notice("trps_handle_update: received invalid TRP update.");
965 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
966 /* validate/sanity check the record update */
967 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
968 tr_notice("trps_handle_update: invalid inforec in TRP update, discarding entire update.");
973 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
974 switch (trp_inforec_get_type(rec)) {
975 case TRP_INFOREC_TYPE_ROUTE:
976 if (TRP_SUCCESS!=trps_handle_inforec_route(trps, upd, rec))
977 tr_notice("trps_handle_update: error handling route inforec.");
979 case TRP_INFOREC_TYPE_COMMUNITY:
980 if (TRP_SUCCESS!=trps_handle_inforec_comm(trps, upd, rec))
981 tr_notice("trps_handle_update: error handling community inforec.");
984 tr_notice("trps_handle_update: unsupported inforec in TRP update.");
991 static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
994 tr_notice("trps_validate_request: null TRP request.");
998 if (trp_req_get_comm(req)==NULL) {
999 tr_notice("trps_validate_request: received TRP request with null community.");
1003 if (trp_req_get_realm(req)==NULL) {
1004 tr_notice("trps_validate_request: received TRP request with null realm.");
1008 if (trp_req_get_peer(req)==NULL) {
1009 tr_notice("trps_validate_request: received TRP request without origin peer information.");
1016 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
1017 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
1020 TR_NAME *exclude_peer)
1022 TRP_ROUTE **entry=NULL;
1023 TRP_ROUTE *best=NULL;
1026 unsigned int kk_min=0;
1027 unsigned int min_metric=TRP_METRIC_INFINITY;
1029 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
1030 for (kk=0; kk<n_entry; kk++) {
1031 if (trp_route_get_metric(entry[kk]) < min_metric) {
1032 if ((exclude_peer==NULL) || (0!=tr_name_cmp(trp_route_get_peer(entry[kk]),
1035 min_metric=trp_route_get_metric(entry[kk]);
1039 if (trp_metric_is_finite(min_metric))
1046 /* TODO: think this through more carefully. At least ought to add hysteresis
1047 * to avoid flapping between routers or routes. */
1048 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
1050 size_t n_comm=0, ii=0;
1051 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1052 size_t n_realm=0, jj=0;
1053 TR_NAME **realm=NULL;
1054 TRP_ROUTE *best_route=NULL, *cur_route=NULL;
1055 unsigned int best_metric=0, cur_metric=0;
1057 for (ii=0; ii<n_comm; ii++) {
1058 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1059 for (jj=0; jj<n_realm; jj++) {
1060 best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
1061 if (best_route==NULL)
1062 best_metric=TRP_METRIC_INFINITY;
1064 best_metric=trp_route_get_metric(best_route);
1066 cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
1067 if (cur_route!=NULL) {
1068 cur_metric=trp_route_get_metric(cur_route);
1069 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
1070 /* The new route has a lower metric than the previous, and is finite. Accept. */
1071 trp_route_set_selected(cur_route, 0);
1072 trp_route_set_selected(best_route, 1);
1073 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
1074 trp_route_set_selected(cur_route, 0);
1075 } else if (trp_metric_is_finite(best_metric)) {
1076 trp_route_set_selected(best_route, 1);
1081 realm=NULL; n_realm=0;
1085 comm=NULL; n_comm=0;
1090 /* true if curtime >= expiry */
1091 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
1093 return ((curtime->tv_sec > expiry->tv_sec)
1094 || ((curtime->tv_sec == expiry->tv_sec)
1095 &&(curtime->tv_nsec >= expiry->tv_nsec)));
1098 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
1099 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
1100 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
1102 struct timespec sweep_time={0,0};
1103 TRP_ROUTE **entry=NULL;
1107 /* use a single time for the entire sweep */
1108 if (0!=clock_gettime(CLOCK_REALTIME, &sweep_time)) {
1109 tr_err("trps_sweep_routes: could not read realtime clock.");
1110 sweep_time.tv_sec=0;
1111 sweep_time.tv_nsec=0;
1115 entry=trp_rtable_get_entries(trps->rtable, &n_entry); /* must talloc_free *entry */
1117 /* loop over the entries */
1118 for (ii=0; ii<n_entry; ii++) {
1119 if (!trp_route_is_local(entry[ii]) && trps_expired(trp_route_get_expiry(entry[ii]), &sweep_time)) {
1120 tr_debug("trps_sweep_routes: route expired.");
1121 if (!trp_metric_is_finite(trp_route_get_metric(entry[ii]))) {
1123 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
1124 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
1127 /* set metric to infinity and reset timer */
1128 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
1129 trp_route_set_metric(entry[ii], TRP_METRIC_INFINITY);
1130 trp_route_set_expiry(entry[ii], trps_compute_expiry(trps,
1131 trp_route_get_interval(entry[ii]),
1132 trp_route_get_expiry(entry[ii])));
1142 static unsigned int trps_metric_add(unsigned int m1, unsigned int m2)
1144 if (trp_metric_is_invalid(m1) || trp_metric_is_invalid(m2))
1145 return TRP_METRIC_INVALID;
1147 if (trp_metric_is_infinite(m1) || trp_metric_is_infinite(m2))
1148 return TRP_METRIC_INFINITY;
1150 if (trp_metric_is_finite(m1+m2))
1153 return TRP_METRIC_INFINITY;
1156 /* convert an rentry into a new trp update info record */
1157 static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1159 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
1160 unsigned int linkcost=0;
1163 if (trp_route_is_local(route))
1166 linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
1167 trp_route_get_peer(route)));
1170 /* Note that we leave the next hop empty since the recipient fills that in.
1171 * This is where we add the link cost (currently always 1) to the next peer. */
1172 if ((trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
1173 ||(trp_inforec_set_metric(rec,
1174 trps_metric_add(trp_route_get_metric(route),
1175 linkcost)) != TRP_SUCCESS)
1176 ||(trp_inforec_set_interval(rec, trps_get_update_interval(trps)) != TRP_SUCCESS)) {
1177 tr_err("trps_route_to_inforec: error creating route update.");
1185 static TRP_UPD *trps_route_to_upd(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1187 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1188 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1189 TRP_INFOREC *rec=NULL;
1192 tr_err("trps_route_to_upd: could not create update message.");
1195 trp_upd_set_realm(upd, trp_route_dup_realm(route));
1196 if (trp_upd_get_realm(upd)==NULL) {
1197 tr_err("trps_route_to_upd: could not copy realm.");
1198 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1201 trp_upd_set_comm(upd, trp_route_dup_comm(route));
1202 if (trp_upd_get_comm(upd)==NULL) {
1203 tr_err("trps_route_to_upd: could not copy comm.");
1204 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1207 rec=trps_route_to_inforec(tmp_ctx, trps, route);
1209 tr_err("trps_route_to_upd: could not create route info record for realm %.*s in comm %.*s.",
1210 trp_route_get_realm(route)->len, trp_route_get_realm(route)->buf,
1211 trp_route_get_comm(route)->len, trp_route_get_comm(route)->buf);
1212 upd=NULL; /* it's till in tmp_ctx, so it will be freed */
1215 trp_upd_add_inforec(upd, rec);
1218 talloc_steal(mem_ctx, upd);
1221 talloc_free(tmp_ctx);
1225 /* select the correct route to comm/realm to be announced to peer */
1226 static TRP_ROUTE *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_gssname)
1230 /* Take the currently selected route unless it is through the peer we're sending the update to.
1231 * I.e., enforce the split horizon rule. */
1232 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
1234 /* No selected route, this should only happen if the only route has been retracted,
1235 * in which case we do not want to advertise it. */
1238 tr_debug("trps_select_realm_update: %s vs %s", peer_gssname->buf,
1239 trp_route_get_peer(route)->buf);
1240 if (0==tr_name_cmp(peer_gssname, trp_route_get_peer(route))) {
1241 tr_debug("trps_select_realm_update: matched, finding alternate route");
1242 /* the selected entry goes through the peer we're reporting to, choose an alternate */
1243 route=trps_find_best_route(trps, comm, realm, peer_gssname);
1244 if ((route==NULL) || (!trp_metric_is_finite(trp_route_get_metric(route))))
1245 return NULL; /* don't advertise a nonexistent or retracted route */
1250 /* Add TRP_UPD msgs to the updates GPtrArray. Caller needs to arrange for these to be freed. */
1251 static TRP_RC trps_select_route_updates_for_peer(TALLOC_CTX *mem_ctx,
1253 TRPS_INSTANCE *trps,
1254 TR_NAME *peer_gssname,
1258 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1259 TR_NAME **realm=NULL;
1262 TRP_ROUTE *best=NULL;
1268 for (ii=0; ii<n_comm; ii++) {
1269 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1270 for (jj=0; jj<n_realm; jj++) {
1271 best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_gssname);
1272 /* If we found a route, add it to the list. If triggered!=0, then only
1273 * add triggered routes. */
1274 if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best))) {
1275 upd=trps_route_to_upd(mem_ctx, trps, best);
1277 tr_err("trps_select_route_updates_for_peer: unable to create update message.");
1280 g_ptr_array_add(updates, upd);
1296 static TRP_INFOREC *trps_memb_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_COMM_MEMB *memb)
1298 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1299 TRP_INFOREC *rec=NULL;
1305 comm=tr_comm_memb_get_comm(memb);
1306 rec=trp_inforec_new(tmp_ctx, TRP_INFOREC_TYPE_COMMUNITY);
1310 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_get_type(comm))) {
1315 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_comm_memb_get_role(memb))) {
1320 if ((NULL!=tr_comm_get_apcs(comm)) &&
1321 ( (TRP_SUCCESS!=trp_inforec_set_apcs(rec,
1322 tr_apc_dup(rec, tr_comm_get_apcs(comm)))) ||
1323 (NULL==trp_inforec_get_apcs(rec)))) {
1328 if ((NULL!=tr_comm_get_owner_realm(comm)) &&
1329 ( (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_dup_name(tr_comm_get_owner_realm(comm)))) ||
1330 (NULL==trp_inforec_get_owner_realm(rec)))) {
1335 if ((NULL!=tr_comm_get_owner_contact(comm)) &&
1336 ( (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_dup_name(tr_comm_get_owner_contact(comm)))) ||
1337 (NULL==trp_inforec_get_owner_contact(rec)))) {
1342 if ((NULL!=tr_comm_memb_get_provenance(memb)) &&
1343 (TRP_SUCCESS!=trp_inforec_set_provenance(rec, tr_comm_memb_get_provenance(memb)))) {
1348 if (TRP_SUCCESS!=trp_inforec_set_interval(rec, tr_comm_memb_get_interval(memb))) {
1354 talloc_steal(mem_ctx, rec);
1357 talloc_free(tmp_ctx);
1361 /* construct an update with all the inforecs for comm/realm/role to be sent to peer */
1362 static TRP_UPD *trps_comm_update(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_NAME *peer_gssname, TR_COMM *comm, TR_REALM *realm)
1364 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1365 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1366 TRP_INFOREC *rec=NULL;
1367 TR_COMM_ITER *iter=NULL;
1368 TR_COMM_MEMB *memb=NULL;
1373 trp_upd_set_comm(upd, tr_comm_dup_id(comm));
1374 trp_upd_set_realm(upd, tr_realm_dup_id(realm));
1375 /* leave peer empty */
1377 iter=tr_comm_iter_new(tmp_ctx);
1379 tr_err("trps_comm_update: unable to allocate iterator.");
1384 /* now add inforecs */
1385 switch (realm->role) {
1387 memb=tr_comm_table_find_idp_memb(trps->ctable,
1388 tr_realm_get_id(realm),
1389 tr_comm_get_id(comm));
1392 memb=tr_comm_table_find_rp_memb(trps->ctable,
1393 tr_realm_get_id(realm),
1394 tr_comm_get_id(comm));
1400 for (memb=tr_comm_memb_iter_first(iter, memb);
1402 memb=tr_comm_memb_iter_next(iter)) {
1403 rec=trps_memb_to_inforec(tmp_ctx, trps, memb);
1405 tr_err("trps_comm_update: unable to allocate inforec.");
1409 trp_upd_add_inforec(upd, rec);
1413 if (trp_upd_get_inforec(upd)==NULL)
1414 upd=NULL; /* no inforecs, no reason to send the update */
1416 talloc_steal(mem_ctx, upd); /* success! */
1419 talloc_free(tmp_ctx);
1423 /* Find all community updates to send to a peer and add these as TR_UPD records
1424 * to the updates GPtrArray. */
1425 static TRP_RC trps_select_comm_updates_for_peer(TALLOC_CTX *mem_ctx, GPtrArray *updates, TRPS_INSTANCE *trps, TR_NAME *peer_gssname)
1427 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1428 TR_COMM_ITER *comm_iter=NULL;
1430 TR_COMM_ITER *realm_iter=NULL;
1431 TR_REALM *realm=NULL;
1433 TRP_RC rc=TRP_ERROR;
1435 comm_iter=tr_comm_iter_new(tmp_ctx);
1436 realm_iter=tr_comm_iter_new(tmp_ctx);
1437 if ((comm_iter==NULL) || (realm_iter==NULL)) {
1438 tr_err("trps_select_comm_updates_for_peer: unable to allocate iterator.");
1443 /* do every community */
1444 for (comm=tr_comm_table_iter_first(comm_iter, trps->ctable);
1446 comm=tr_comm_table_iter_next(comm_iter)) {
1447 /* do every realm in this community */
1448 tr_debug("trps_select_comm_updates_for_peer: looking through community %.*s",
1449 tr_comm_get_id(comm)->len,
1450 tr_comm_get_id(comm)->buf);
1451 for (realm=tr_realm_iter_first(realm_iter, trps->ctable, tr_comm_get_id(comm));
1453 realm=tr_realm_iter_next(realm_iter)) {
1454 /* get the update for this comm/realm */
1455 tr_debug("trps_select_comm_updates_for_peer: adding realm %.*s",
1456 tr_realm_get_id(realm)->len,
1457 tr_realm_get_id(realm)->buf);
1458 upd=trps_comm_update(mem_ctx, trps, peer_gssname, comm, realm);
1460 g_ptr_array_add(updates, upd);
1465 talloc_free(tmp_ctx);
1470 /* helper for trps_update_one_peer. Frees the TRP_UPD pointed to by a GPtrArray element */
1471 static void trps_trp_upd_destroy(gpointer data)
1473 trp_upd_free((TRP_UPD *)data);
1476 /* all routes/communities to a single peer, unless comm/realm are specified (both or neither must be NULL) */
1477 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
1479 TRP_UPDATE_TYPE update_type,
1483 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1484 TR_MSG msg; /* not a pointer! */
1486 TRP_ROUTE *route=NULL;
1489 TRP_RC rc=TRP_ERROR;
1490 TR_NAME *peer_label=trp_peer_get_label(peer);
1491 GPtrArray *updates=g_ptr_array_new_with_free_func(trps_trp_upd_destroy);
1493 if (updates==NULL) {
1494 tr_err("trps_update_one_peer: unable to allocate updates array.");
1499 switch (update_type) {
1500 case TRP_UPDATE_TRIGGERED:
1501 tr_debug("trps_update_one_peer: preparing triggered update for %.*s",
1502 peer_label->len, peer_label->buf);
1504 case TRP_UPDATE_SCHEDULED:
1505 tr_debug("trps_update_one_peer: preparing scheduled update for %.*s",
1506 peer_label->len, peer_label->buf);
1508 case TRP_UPDATE_REQUESTED:
1509 tr_debug("trps_update_one_peer: preparing requested update for %.*s",
1510 peer_label->len, peer_label->buf);
1513 tr_err("trps_update_one_peer: invalid update type requested.");
1518 /* First, gather route updates. */
1519 tr_debug("trps_update_one_peer: selecting route updates for %.*s.", peer_label->len, peer_label->buf);
1520 if ((comm==NULL) && (realm==NULL)) {
1522 rc=trps_select_route_updates_for_peer(tmp_ctx,
1526 update_type==TRP_UPDATE_TRIGGERED);
1527 } else if ((comm!=NULL) && (realm!=NULL)) {
1528 /* a single community/realm was requested */
1529 route=trps_select_realm_update(trps, comm, realm, peer_label);
1531 /* we have no actual update to send back, MUST send a retraction */
1532 tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
1533 route=trp_route_new(tmp_ctx);
1534 trp_route_set_comm(route, tr_dup_name(comm));
1535 trp_route_set_realm(route, tr_dup_name(realm));
1536 trp_route_set_peer(route, tr_new_name(""));
1537 trp_route_set_metric(route, TRP_METRIC_INFINITY);
1538 trp_route_set_trust_router(route, tr_new_name(""));
1539 trp_route_set_next_hop(route, tr_new_name(""));
1541 upd=trps_route_to_upd(tmp_ctx, trps, route);
1543 tr_err("trps_update_one_peer: unable to allocate route update.");
1547 g_ptr_array_add(updates, upd);
1549 tr_err("trps_update_one_peer: error: only comm or realm was specified. Need both or neither.");
1554 /* Second, gather community updates */
1555 tr_debug("trps_update_one_peer: selecting community updates for %.*s.", peer_label->len, peer_label->buf);
1556 rc=trps_select_comm_updates_for_peer(tmp_ctx, updates, trps, peer_label);
1558 /* see if we have anything to send */
1559 if (updates->len<=0)
1560 tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
1562 tr_debug("trps_update_one_peer: sending %d update messages.", updates->len);
1563 for (ii=0; ii<updates->len; ii++) {
1564 upd=(TRP_UPD *)g_ptr_array_index(updates, ii);
1565 /* now encode the update message */
1566 tr_msg_set_trp_upd(&msg, upd);
1567 encoded=tr_msg_encode(&msg);
1568 if (encoded==NULL) {
1569 tr_err("trps_update_one_peer: error encoding update.");
1574 tr_debug("trps_update_one_peer: adding message to queue.");
1575 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
1576 tr_err("trps_update_one_peer: error queueing update.");
1578 tr_debug("trps_update_one_peer: update queued successfully.");
1580 tr_msg_free_encoded(encoded);
1589 g_ptr_array_free(updates, TRUE); /* frees any TRP_UPD records */
1590 talloc_free(tmp_ctx);
1594 /* all routes/communities to all peers */
1595 TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
1597 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1598 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
1599 TRP_PEER *peer=NULL;
1600 TRP_RC rc=TRP_SUCCESS;
1602 if (trps->ptable==NULL)
1603 return TRP_SUCCESS; /* no peers, nothing to do */
1606 tr_err("trps_update: failed to allocate peer table iterator.");
1607 talloc_free(tmp_ctx);
1611 for (peer=trp_ptable_iter_first(iter, trps->ptable);
1612 peer!=NULL && rc==TRP_SUCCESS;
1613 peer=trp_ptable_iter_next(iter))
1615 if (!trps_peer_connected(trps, peer)) {
1616 TR_NAME *peer_label=trp_peer_get_label(peer);
1617 tr_debug("trps_update: no TRP connection to %.*s, skipping.",
1618 peer_label->len, peer_label->buf);
1621 rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
1624 tr_debug("trps_update: rc=%u after attempting update.", rc);
1625 trp_ptable_iter_free(iter);
1626 trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
1627 talloc_free(tmp_ctx);
1631 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
1633 trp_rtable_add(trps->rtable, route); /* should return status */
1637 /* steals the peer object */
1638 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
1640 if (trps->ptable==NULL) {
1641 trps->ptable=trp_ptable_new(trps);
1642 if (trps->ptable==NULL)
1645 return trp_ptable_add(trps->ptable, peer);
1648 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
1650 if (trps->ptable==NULL)
1653 return trp_ptable_find_gss_name(trps->ptable, gssname);
1656 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
1658 if (trps->ptable==NULL)
1661 return trp_ptable_find_servicename(trps->ptable, servicename);
1664 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
1666 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
1670 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)
1677 static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1680 TR_NAME *realm=NULL;
1682 tr_debug("trps_handle_request: handling TRP request.");
1684 if (trps_validate_request(trps, req) != TRP_SUCCESS) {
1685 tr_notice("trps_handle_request: received invalid TRP request.");
1689 if (!trp_req_is_wildcard(req)) {
1690 comm=trp_req_get_comm(req);
1691 realm=trp_req_get_realm(req);
1692 tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
1693 comm->len, comm->buf, realm->len, realm->buf);
1695 tr_debug("trps_handle_request: all routes requested.");
1696 /* leave comm/realm NULL */
1698 return trps_update_one_peer(trps,
1699 trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
1700 TRP_UPDATE_REQUESTED,
1706 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
1708 TRP_RC rc=TRP_ERROR;
1710 switch (tr_msg_get_msg_type(tr_msg)) {
1712 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
1713 if (rc==TRP_SUCCESS) {
1714 rc=trps_update_active_routes(trps);
1715 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
1720 rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
1724 /* unknown error or one we don't care about (e.g., TID messages) */
1729 /* send wildcard route request to a peer */
1730 TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
1732 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1733 TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
1734 TR_MSG msg; /* not a pointer */
1735 TRP_REQ *req=trp_req_new(tmp_ctx);
1737 TRP_RC rc=TRP_ERROR;
1740 tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
1744 if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
1745 tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
1750 tr_msg_set_trp_req(&msg, req);
1751 encoded=tr_msg_encode(&msg);
1752 if (encoded==NULL) {
1753 tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
1758 tr_debug("trps_wildcard_route_req: adding message to queue.");
1759 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
1760 tr_err("trps_wildcard_route_req: error queueing request.");
1763 tr_debug("trps_wildcard_route_req: request queued successfully.");
1769 tr_msg_free_encoded(encoded);
1773 talloc_free(tmp_ctx);