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.
47 #include <trust_router/tr_name.h>
48 #include <trp_internal.h>
50 #include <trp_ptable.h>
51 #include <trp_rtable.h>
55 static int trps_destructor(void *object)
57 TRPS_INSTANCE *trps=talloc_get_type_abort(object, TRPS_INSTANCE);
58 if (trps->rtable!=NULL)
59 trp_rtable_free(trps->rtable);
63 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
65 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
72 trps->update_interval=(struct timeval){0,0};
73 trps->sweep_interval=(struct timeval){0,0};
76 trps->mq=tr_mq_new(trps);
78 /* failed to allocate mq */
84 if (trps_init_rtable(trps) != TRP_SUCCESS) {
85 /* failed to allocate rtable */
90 talloc_set_destructor((void *)trps, trps_destructor);
95 /* create a new route table, first discarding an old one if necessary */
96 TRP_RC trps_init_rtable(TRPS_INSTANCE *trps)
98 if (trps->rtable != NULL) {
99 trp_rtable_free(trps->rtable);
103 trps->rtable=trp_rtable_new();
104 if (trps->rtable==NULL) {
110 void trps_clear_rtable(TRPS_INSTANCE *trps)
112 trp_rtable_clear(trps->rtable);
115 void trps_free (TRPS_INSTANCE *trps)
121 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
123 return tr_mq_pop(trps->mq, 0);
126 void trps_mq_add(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
128 tr_mq_add(trps->mq, msg);
131 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
133 return trps->connect_interval.tv_sec;
136 void trps_set_connect_interval(TRPS_INSTANCE *trps, unsigned int interval)
138 trps->connect_interval.tv_sec=interval;
139 trps->connect_interval.tv_usec=0;
142 unsigned int trps_get_update_interval(TRPS_INSTANCE *trps)
144 return trps->update_interval.tv_sec;
147 void trps_set_update_interval(TRPS_INSTANCE *trps, unsigned int interval)
149 trps->update_interval.tv_sec=interval;
150 trps->update_interval.tv_usec=0;
153 unsigned int trps_get_sweep_interval(TRPS_INSTANCE *trps)
155 return trps->sweep_interval.tv_sec;
158 void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
160 trps->sweep_interval.tv_sec=interval;
161 trps->sweep_interval.tv_usec=0;
164 void trps_set_ctable(TRPS_INSTANCE *trps, TR_COMM_TABLE *comm)
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 /* Listens on all interfaces. Returns number of sockets opened. Their
280 * descriptors are stored in *fd_out, which should point to space for
281 * up to max_fd of them. */
282 static size_t trps_listen(TRPS_INSTANCE *trps, int port, int *fd_out, size_t max_fd)
287 struct addrinfo *ai=NULL;
288 struct addrinfo *ai_head=NULL;
289 struct addrinfo hints={.ai_flags=AI_PASSIVE,
290 .ai_family=AF_UNSPEC,
291 .ai_socktype=SOCK_STREAM,
292 .ai_protocol=IPPROTO_TCP};
296 port_str=talloc_asprintf(NULL, "%d", port);
297 if (port_str==NULL) {
298 tr_debug("trps_listen: unable to allocate port.");
301 getaddrinfo(NULL, port_str, &hints, &ai_head);
302 talloc_free(port_str);
304 for (ai=ai_head,n_opened=0; (ai!=NULL)&&(n_opened<max_fd); ai=ai->ai_next) {
305 if (0 > (conn = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))) {
306 tr_debug("trps_listen: unable to open socket.");
311 if (0!=setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)))
312 tr_debug("trps_listen: unable to set SO_REUSEADDR."); /* not fatal? */
314 if (ai->ai_family==AF_INET6) {
315 /* don't allow IPv4-mapped IPv6 addresses (per RFC4942, not sure
316 * if still relevant) */
317 if (0!=setsockopt(conn, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))) {
318 tr_debug("trps_listen: unable to set IPV6_V6ONLY. Skipping interface.");
324 rc=bind(conn, ai->ai_addr, ai->ai_addrlen);
326 tr_debug("trps_listen: unable to bind to socket.");
331 if (0>listen(conn, 512)) {
332 tr_debug("trps_listen: unable to listen on bound socket.");
337 /* ok, this one worked. Save it */
338 fd_out[n_opened++]=conn;
340 freeaddrinfo(ai_head);
343 tr_debug("trps_listen: no addresses available for listening.");
347 tr_debug("trps_listen: TRP Server listening on port %d on %d socket%s",
350 (n_opened==1)?"":"s");
355 /* get the currently selected route if available */
356 TRP_ROUTE *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
358 return trp_rtable_get_entry(trps->rtable, comm, realm, peer);
361 TRP_ROUTE *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
363 tr_debug("trps_get_selected_route: entered. trps=%p, comm=%p, realm=%p", trps, comm, realm);
364 return trp_rtable_get_selected_entry(trps->rtable, comm, realm);
367 /* copy the result if you want to keep it */
368 TR_NAME *trps_get_next_hop(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
370 TRP_ROUTE *route=trps_get_selected_route(trps, comm, realm);
374 return trp_route_get_next_hop(route);
378 /* mark a route as retracted */
379 static void trps_retract_route(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
381 trp_route_set_metric(entry, TRP_METRIC_INFINITY);
382 trp_route_set_triggered(entry, 1);
385 /* is this route retracted? */
386 static int trps_route_retracted(TRPS_INSTANCE *trps, TRP_ROUTE *entry)
388 return (trp_metric_is_infinite(trp_route_get_metric(entry)));
391 static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MSG **msg)
396 TRP_PEER *peer=NULL; /* entry in the peer table */
397 TR_NAME *conn_peer=NULL; /* name from the TRP_CONN, which comes from the gss context */
399 tr_debug("trps_read_message: started");
400 if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
401 *(trp_connection_get_gssctx(conn)),
404 tr_debug("trps_read_message: error");
410 tr_debug("trps_read_message: message received, %u bytes.", (unsigned) buflen);
411 tr_debug("trps_read_message: %.*s", buflen, buf);
413 *msg=tr_msg_decode(buf, buflen);
418 conn_peer=trp_connection_get_peer(conn);
419 if (conn_peer==NULL) {
420 tr_err("trps_read_message: connection has no peer name");
424 peer=trps_get_peer_by_gssname(trps, conn_peer);
426 tr_err("trps_read_message: could not find peer with gssname=%s", trp_connection_get_gssname(conn));
430 /* verify we received a message we support, otherwise drop it now */
431 switch (tr_msg_get_msg_type(*msg)) {
433 trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(conn_peer));
434 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 */
435 /* update provenance if necessary */
436 trp_upd_add_to_provenance(tr_msg_get_trp_upd(*msg), trp_peer_get_label(peer));
440 trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(conn_peer));
444 tr_debug("trps_read_message: received unsupported message from %.*s", conn_peer->len, conn_peer->buf);
445 tr_msg_free_decoded(*msg);
447 return TRP_UNSUPPORTED;
453 int trps_get_listener(TRPS_INSTANCE *trps,
454 TRPS_MSG_FUNC msg_handler,
455 TRP_AUTH_FUNC auth_handler,
456 const char *hostname,
465 n_fd=trps_listen(trps, port, fd_out, max_fd);
467 tr_debug("trps_get_listener: Error opening port %d.");
469 /* opening port succeeded */
470 tr_debug("trps_get_listener: Opened port %d.", port);
472 /* make the sockets non-blocking */
473 for (ii=0; ii<n_fd; ii++) {
474 if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
475 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
476 for (ii=0; ii<n_fd; ii++) {
487 /* store the caller's request handler & cookie */
488 trps->msg_handler = msg_handler;
489 trps->auth_handler = auth_handler;
490 trps->hostname = talloc_strdup(trps, hostname);
492 trps->cookie = cookie;
498 TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
500 /* try to establish a GSS context */
501 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
502 tr_notice("trps_authorize_connection: failed to authorize connection");
503 trp_connection_close(conn);
506 tr_notice("trps_authorize_connection: authorized connection");
510 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
515 /* loop as long as the connection exists */
516 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
517 rc=trps_read_message(trps, conn, &msg);
520 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
524 trp_connection_close(conn);
528 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
532 tr_debug("trps_handle_connection: connection closed.");
535 /* TODO: check realm/comm, now part of the update instead of inforec */
536 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
539 tr_notice("trps_validate_update: null TRP update.");
543 if (trp_upd_get_realm(upd)==NULL) {
544 tr_notice("trps_validate_update: received TRP update without realm.");
548 if (trp_upd_get_comm(upd)==NULL) {
549 tr_notice("trps_validate_update: received TRP update without community.");
553 if (trp_upd_get_inforec(upd)==NULL) {
554 tr_notice("trps_validate_update: received TRP update with no info records.");
558 if (trp_upd_get_peer(upd)==NULL) {
559 tr_notice("trps_validate_update: received TRP update without origin peer information.");
567 /* ensure that the update could be accepted if feasible */
568 static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
570 switch(trp_inforec_get_type(rec)) {
571 case TRP_INFOREC_TYPE_ROUTE:
572 if ((trp_inforec_get_trust_router(rec)==NULL)
573 || (trp_inforec_get_next_hop(rec)==NULL)) {
574 tr_debug("trps_validate_inforec: missing record info.");
578 /* check for valid metric */
579 if (trp_metric_is_invalid(trp_inforec_get_metric(rec))) {
580 tr_debug("trps_validate_inforec: invalid metric (%u).", trp_inforec_get_metric(rec));
584 /* check for valid interval */
585 if (trp_inforec_get_interval(rec)==TRP_INTERVAL_INVALID) {
586 tr_debug("trps_validate_inforec: invalid interval.");
591 case TRP_INFOREC_TYPE_COMMUNITY:
592 /* TODO: validate community updates */
596 tr_notice("trps_validate_inforec: unsupported record type.");
597 return TRP_UNSUPPORTED;
603 /* link cost to a peer */
604 static unsigned int trps_cost(TRPS_INSTANCE *trps, TR_NAME *peer)
609 static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
611 TRP_ROUTE *entry=trp_rtable_get_entry(trps->rtable, comm, realm, peer);
613 return TRP_METRIC_INFINITY;
614 return trp_route_get_metric(entry) + trps_cost(trps, peer);
617 static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
619 unsigned int rec_metric=trp_inforec_get_metric(rec);
620 unsigned int new_metric=0;
621 unsigned int current_metric=0;
622 TR_NAME *next_hop=NULL;
624 /* we check these in the validation stage, but just in case... */
625 if (trp_metric_is_invalid(rec_metric))
628 /* retractions (aka infinite metrics) are always feasible */
629 if (trp_metric_is_infinite(rec_metric))
632 /* updates from our current next hop are always feasible*/
633 next_hop=trps_get_next_hop(trps, comm, realm);
635 && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
640 /* compare the existing metric we advertise to what we would advertise
641 * if we accept this update */
642 current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
643 new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
644 if (new_metric <= current_metric)
650 /* uses memory pointed to by *ts, also returns that value. On error, its contents are {0,0} */
651 static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int interval, struct timespec *ts)
653 const unsigned int small_factor=3; /* how many intervals we wait before expiring */
654 if (0!=clock_gettime(TRP_CLOCK, ts)) {
655 tr_err("trps_compute_expiry: could not read realtime clock.");
659 tr_debug("trps_compute_expiry: tv_sec=%u, interval=%u, small_factor*interval=%u", ts->tv_sec, interval, small_factor*interval);
660 ts->tv_sec += small_factor*interval;
664 static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
666 TRP_ROUTE *entry=NULL;
668 entry=trp_rtable_get_entry(trps->rtable,
669 trp_upd_get_comm(upd),
670 trp_upd_get_realm(upd),
671 trp_inforec_get_next_hop(rec));
673 entry=trp_route_new(NULL);
675 tr_err("trps_accept_update: unable to allocate new entry.");
679 trp_route_set_comm(entry, trp_upd_dup_comm(upd));
680 trp_route_set_realm(entry, trp_upd_dup_realm(upd));
681 trp_route_set_peer(entry, trp_upd_dup_peer(upd));
682 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
683 trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
684 /* TODO: pass next hop port (now defaults to TID_PORT) --jlr */
685 if ((trp_route_get_comm(entry)==NULL)
686 ||(trp_route_get_realm(entry)==NULL)
687 ||(trp_route_get_peer(entry)==NULL)
688 ||(trp_route_get_trust_router(entry)==NULL)
689 ||(trp_route_get_next_hop(entry)==NULL)) {
690 /* at least one field could not be allocated */
691 tr_err("trps_accept_update: unable to allocate all fields for entry.");
692 trp_route_free(entry);
695 trp_rtable_add(trps->rtable, entry);
698 /* We now have an entry in the table, whether it's new or not. Update metric and expiry, unless
699 * the metric is infinity. An infinite metric can only occur here if we just retracted an existing
700 * route (we never accept retractions as new routes), so there is no risk of leaving the expiry
701 * time unset on a new route entry. */
702 tr_debug("trps_accept_update: accepting route update.");
703 trp_route_set_metric(entry, trp_inforec_get_metric(rec));
704 trp_route_set_interval(entry, trp_inforec_get_interval(rec));
706 /* check whether the trust router has changed */
707 if (0!=tr_name_cmp(trp_route_get_trust_router(entry),
708 trp_inforec_get_trust_router(rec))) {
709 /* The name changed. Set this route as triggered. */
710 tr_debug("trps_accept_update: trust router for route changed.");
711 trp_route_set_triggered(entry, 1);
712 trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec)); /* frees old name */
714 if (!trps_route_retracted(trps, entry)) {
715 tr_debug("trps_accept_update: route not retracted, setting expiry timer.");
716 trp_route_set_expiry(entry, trps_compute_expiry(trps,
717 trp_route_get_interval(entry),
718 trp_route_get_expiry(entry)));
724 static TRP_RC trps_handle_inforec_route(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
726 TRP_ROUTE *route=NULL;
729 /* determine feasibility */
730 feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
731 tr_debug("trps_handle_update: record feasibility=%d", feas);
733 /* do we have an existing route? */
734 route=trps_get_route(trps,
735 trp_upd_get_comm(upd),
736 trp_upd_get_realm(upd),
737 trp_upd_get_peer(upd));
739 /* there was a route table entry already */
740 tr_debug("trps_handle_updates: route entry already exists.");
742 /* Update is feasible. Accept it. */
743 trps_accept_update(trps, upd, rec);
745 /* Update is infeasible. Ignore it unless the trust router has changed. */
746 if (0!=tr_name_cmp(trp_route_get_trust_router(route),
747 trp_inforec_get_trust_router(rec))) {
748 /* the trust router associated with the route has changed, treat update as a retraction */
749 trps_retract_route(trps, route);
753 /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
754 tr_debug("trps_handle_update: no route entry exists yet.");
755 if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
756 trps_accept_update(trps, upd, rec);
762 static int trps_name_in_provenance(TR_NAME *name, json_t *prov)
765 TR_NAME *this_name=NULL;
769 return 0; /* no provenance list, so it has no names in it */
771 /* now check to see if name is in the provenance */
772 for (ii=0; ii<json_array_size(prov); ii++) {
773 s=json_string_value(json_array_get(prov, ii));
775 tr_debug("trps_name_in_provenance: empty entry in provenance list.");
779 this_name=tr_new_name(s);
780 if (this_name==NULL) {
781 tr_debug("trps_name_in_provenance: unable to allocate name.");
784 if (0==tr_name_cmp(name, this_name)) {
785 tr_free_name(this_name);
788 tr_free_name(this_name);
793 static TR_COMM *trps_create_new_comm(TALLOC_CTX *mem_ctx, TR_NAME *comm_id, TRP_INFOREC *rec)
795 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
796 TR_COMM *comm=tr_comm_new(tmp_ctx);
799 tr_debug("trps_create_new_comm: unable to allocate new community.");
802 /* fill in the community with info */
803 tr_comm_set_id(comm, tr_dup_name(comm_id));
804 if (tr_comm_get_id(comm)==NULL) {
805 tr_debug("trps_create_new_comm: unable to allocate community name.");
809 tr_comm_set_type(comm, trp_inforec_get_comm_type(rec));
810 if (trp_inforec_get_apcs(rec)!=NULL) {
811 tr_comm_set_apcs(comm, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
812 if (tr_comm_get_apcs(comm)==NULL) {
813 tr_debug("trps_create_new_comm: unable to allocate APC list.");
818 if (trp_inforec_get_owner_realm(rec)!=NULL) {
819 tr_comm_set_owner_realm(comm, tr_dup_name(trp_inforec_get_owner_realm(rec)));
820 if (tr_comm_get_owner_realm(comm)==NULL) {
821 tr_debug("trps_create_new_comm: unable to allocate owner realm name.");
826 if (trp_inforec_get_owner_contact(rec)!=NULL) {
827 tr_comm_set_owner_contact(comm, tr_dup_name(trp_inforec_get_owner_contact(rec)));
828 if (tr_comm_get_owner_contact(comm)==NULL) {
829 tr_debug("trps_create_new_comm: unable to allocate owner contact.");
834 comm->expiration_interval=trp_inforec_get_exp_interval(rec);
835 talloc_steal(mem_ctx, comm);
838 talloc_free(tmp_ctx);
842 static TR_RP_REALM *trps_create_new_rp_realm(TALLOC_CTX *mem_ctx, TR_NAME *realm_id, TRP_INFOREC *rec)
844 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
845 TR_RP_REALM *rp=tr_rp_realm_new(tmp_ctx);
848 tr_debug("trps_create_new_rp_realm: unable to allocate new realm.");
851 /* fill in the realm */
852 tr_rp_realm_set_id(rp, tr_dup_name(realm_id));
853 if (tr_rp_realm_get_id(rp)==NULL) {
854 tr_debug("trps_create_new_rp_realm: unable to allocate realm name.");
858 talloc_steal(mem_ctx, rp);
861 talloc_free(tmp_ctx);
865 static TR_IDP_REALM *trps_create_new_idp_realm(TALLOC_CTX *mem_ctx, TR_NAME *realm_id, TRP_INFOREC *rec)
867 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
868 TR_IDP_REALM *idp=tr_idp_realm_new(tmp_ctx);
871 tr_debug("trps_create_new_idp_realm: unable to allocate new realm.");
874 /* fill in the realm */
875 tr_idp_realm_set_id(idp, tr_dup_name(realm_id));
876 if (tr_idp_realm_get_id(idp)==NULL) {
877 tr_debug("trps_create_new_idp_realm: unable to allocate realm name.");
881 if (trp_inforec_get_apcs(rec)!=NULL) {
882 tr_idp_realm_set_apcs(idp, tr_apc_dup(tmp_ctx, trp_inforec_get_apcs(rec)));
883 if (tr_idp_realm_get_apcs(idp)==NULL) {
884 tr_debug("trps_create_new_idp_realm: unable to allocate APC list.");
889 idp->origin=TR_REALM_DISCOVERED;
891 talloc_steal(mem_ctx, idp);
894 talloc_free(tmp_ctx);
898 static TRP_RC trps_handle_inforec_comm(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
900 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
901 TR_NAME *comm_id=trp_upd_get_comm(upd);
902 TR_NAME *realm_id=trp_upd_get_realm(upd);
903 TR_NAME *origin_id=NULL;
904 TR_NAME *our_peer_label=NULL;
906 TR_RP_REALM *rp_realm=NULL;
907 TR_IDP_REALM *idp_realm=NULL;
908 struct timespec expiry={0,0};
911 if ((comm_id==NULL) || (realm_id==NULL))
914 origin_id=trp_inforec_dup_origin(rec);
918 /* see whether we want to add this */
919 our_peer_label=trps_dup_label(trps);
920 if (our_peer_label==NULL) {
921 tr_debug("trps_handle_inforec_comm: unable to allocate peer label.");
925 if (trps_name_in_provenance(our_peer_label, trp_inforec_get_provenance(rec)))
926 tr_debug("trps_handle_inforec_comm: rejecting community inforec to avoid loop.");
928 /* no loop occurring, accept the update */
929 comm=tr_comm_table_find_comm(trps->ctable, comm_id);
931 tr_debug("trps_handle_inforec_comm: unknown community %.*s in inforec, creating it.",
932 comm_id->len, comm_id->buf);
933 comm=trps_create_new_comm(tmp_ctx, comm_id, rec);
935 tr_debug("trps_handle_inforec_comm: unable to create new community.");
938 tr_comm_table_add_comm(trps->ctable, comm);
940 /* TODO: see if other comm data match the new inforec and update or complain */
942 trps_compute_expiry(trps, trp_inforec_get_interval(rec), &expiry);
943 if ((expiry.tv_sec==0)&&(expiry.tv_nsec==0))
946 switch (trp_inforec_get_role(rec)) {
948 rp_realm=tr_rp_realm_lookup(trps->ctable->rp_realms, realm_id);
949 if (rp_realm==NULL) {
950 tr_debug("trps_handle_inforec_comm: unknown RP realm %.*s in inforec, creating it.",
951 realm_id->len, realm_id->buf);
952 rp_realm=trps_create_new_rp_realm(tmp_ctx, realm_id, rec);
953 if (rp_realm==NULL) {
954 tr_debug("trps_handle_inforec_comm: unable to create new RP realm.");
955 /* we may leave an unused community in the table, but it will only last until
956 * the next table sweep if it does not get any realms before that happens */
959 tr_comm_table_add_rp_realm(trps->ctable, rp_realm);
961 /* TODO: if realm existed, see if data match the new inforec and update or complain */
962 tr_comm_add_rp_realm(trps->ctable, comm, rp_realm, trp_inforec_get_interval(rec), trp_inforec_get_provenance(rec), &expiry);
963 tr_debug("trps_handle_inforec_comm: added RP realm %.*s to comm %.*s (origin %.*s).",
964 realm_id->len, realm_id->buf,
965 comm_id->len, comm_id->buf,
966 origin_id->len, origin_id->buf);
969 idp_realm=tr_idp_realm_lookup(trps->ctable->idp_realms, realm_id);
970 if (idp_realm==NULL) {
971 tr_debug("trps_handle_inforec_comm: unknown IDP realm %.*s in inforec, creating it.",
972 realm_id->len, realm_id->buf);
973 idp_realm=trps_create_new_idp_realm(tmp_ctx, realm_id, rec);
974 if (idp_realm==NULL) {
975 tr_debug("trps_handle_inforec_comm: unable to create new IDP realm.");
976 /* we may leave an unused community in the table, but it will only last until
977 * the next table sweep if it does not get any realms before that happens */
980 tr_comm_table_add_idp_realm(trps->ctable, idp_realm);
982 /* TODO: if realm existed, see if data match the new inforec and update or complain */
983 tr_comm_add_idp_realm(trps->ctable, comm, idp_realm, trp_inforec_get_interval(rec), trp_inforec_get_provenance(rec), &expiry);
984 tr_debug("trps_handle_inforec_comm: added IDP realm %.*s to comm %.*s (origin %.*s).",
985 realm_id->len, realm_id->buf,
986 comm_id->len, comm_id->buf,
987 origin_id->len, origin_id->buf);
990 tr_debug("trps_handle_inforec_comm: unable to add realm.");
998 if (our_peer_label!=NULL)
999 tr_free_name(our_peer_label);
1000 if (origin_id!=NULL)
1001 tr_free_name(origin_id);
1002 talloc_free(tmp_ctx);
1006 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
1008 TRP_INFOREC *rec=NULL;
1010 if (trps_validate_update(trps, upd) != TRP_SUCCESS) {
1011 tr_notice("trps_handle_update: received invalid TRP update.");
1015 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1016 /* validate/sanity check the record update */
1017 if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
1018 tr_notice("trps_handle_update: invalid inforec in TRP update, discarding entire update.");
1023 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1024 switch (trp_inforec_get_type(rec)) {
1025 case TRP_INFOREC_TYPE_ROUTE:
1026 tr_debug("trps_handle_update: handling route inforec.");
1027 if (TRP_SUCCESS!=trps_handle_inforec_route(trps, upd, rec))
1028 tr_notice("trps_handle_update: error handling route inforec.");
1030 case TRP_INFOREC_TYPE_COMMUNITY:
1031 tr_debug("trps_handle_update: handling community inforec.");
1032 if (TRP_SUCCESS!=trps_handle_inforec_comm(trps, upd, rec))
1033 tr_notice("trps_handle_update: error handling community inforec.");
1035 tr_comm_table_print(stdout, trps->ctable); /* for debugging TODO remove */
1039 tr_notice("trps_handle_update: unsupported inforec in TRP update.");
1046 static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1049 tr_notice("trps_validate_request: null TRP request.");
1053 if (trp_req_get_comm(req)==NULL) {
1054 tr_notice("trps_validate_request: received TRP request with null community.");
1058 if (trp_req_get_realm(req)==NULL) {
1059 tr_notice("trps_validate_request: received TRP request with null realm.");
1063 if (trp_req_get_peer(req)==NULL) {
1064 tr_notice("trps_validate_request: received TRP request without origin peer information.");
1071 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
1072 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
1075 TR_NAME *exclude_peer)
1077 TRP_ROUTE **entry=NULL;
1078 TRP_ROUTE *best=NULL;
1081 unsigned int kk_min=0;
1082 unsigned int min_metric=TRP_METRIC_INFINITY;
1084 entry=trp_rtable_get_realm_entries(trps->rtable, comm, realm, &n_entry);
1085 for (kk=0; kk<n_entry; kk++) {
1086 if (trp_route_get_metric(entry[kk]) < min_metric) {
1087 if ((exclude_peer==NULL) || (0!=tr_name_cmp(trp_route_get_peer(entry[kk]),
1090 min_metric=trp_route_get_metric(entry[kk]);
1094 if (trp_metric_is_finite(min_metric))
1101 /* TODO: think this through more carefully. At least ought to add hysteresis
1102 * to avoid flapping between routers or routes. */
1103 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
1105 size_t n_comm=0, ii=0;
1106 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1107 size_t n_realm=0, jj=0;
1108 TR_NAME **realm=NULL;
1109 TRP_ROUTE *best_route=NULL, *cur_route=NULL;
1110 unsigned int best_metric=0, cur_metric=0;
1112 for (ii=0; ii<n_comm; ii++) {
1113 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1114 for (jj=0; jj<n_realm; jj++) {
1115 best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
1116 if (best_route==NULL)
1117 best_metric=TRP_METRIC_INFINITY;
1119 best_metric=trp_route_get_metric(best_route);
1121 cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
1122 if (cur_route!=NULL) {
1123 cur_metric=trp_route_get_metric(cur_route);
1124 if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
1125 /* The new route has a lower metric than the previous, and is finite. Accept. */
1126 trp_route_set_selected(cur_route, 0);
1127 trp_route_set_selected(best_route, 1);
1128 } else if (!trp_metric_is_finite(cur_metric)) /* rejects infinite or invalid metrics */
1129 trp_route_set_selected(cur_route, 0);
1130 } else if (trp_metric_is_finite(best_metric)) {
1131 trp_route_set_selected(best_route, 1);
1136 realm=NULL; n_realm=0;
1140 comm=NULL; n_comm=0;
1145 /* true if curtime >= expiry */
1146 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
1148 return (tr_cmp_timespec(curtime, expiry) >= 0);
1151 /* Sweep for expired routes. For each expired route, if its metric is infinite, the route is flushed.
1152 * If its metric is finite, the metric is set to infinite and the route's expiration time is updated. */
1153 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
1155 struct timespec sweep_time={0,0};
1156 TRP_ROUTE **entry=NULL;
1160 /* use a single time for the entire sweep */
1161 if (0!=clock_gettime(TRP_CLOCK, &sweep_time)) {
1162 tr_err("trps_sweep_routes: could not read realtime clock.");
1163 sweep_time.tv_sec=0;
1164 sweep_time.tv_nsec=0;
1168 entry=trp_rtable_get_entries(trps->rtable, &n_entry); /* must talloc_free *entry */
1170 /* loop over the entries */
1171 for (ii=0; ii<n_entry; ii++) {
1172 if (!trp_route_is_local(entry[ii]) && trps_expired(trp_route_get_expiry(entry[ii]), &sweep_time)) {
1173 tr_debug("trps_sweep_routes: route expired.");
1174 if (!trp_metric_is_finite(trp_route_get_metric(entry[ii]))) {
1176 tr_debug("trps_sweep_routes: metric was infinity, flushing route.");
1177 trp_rtable_remove(trps->rtable, entry[ii]); /* entry[ii] is no longer valid */
1180 /* set metric to infinity and reset timer */
1181 tr_debug("trps_sweep_routes: setting metric to infinity and resetting expiry.");
1182 trp_route_set_metric(entry[ii], TRP_METRIC_INFINITY);
1183 trp_route_set_expiry(entry[ii], trps_compute_expiry(trps,
1184 trp_route_get_interval(entry[ii]),
1185 trp_route_get_expiry(entry[ii])));
1195 static char *timespec_to_str(struct timespec *ts)
1200 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
1203 s=malloc(40); /* long enough to contain strftime result */
1207 if (strftime(s, 40, "%F %T", &tm)==0) {
1215 /* Sweep for expired communities/realms/memberships. */
1216 TRP_RC trps_sweep_ctable(TRPS_INSTANCE *trps)
1218 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1219 struct timespec sweep_time={0,0};
1220 TR_COMM_MEMB *memb=NULL;
1221 TR_COMM_ITER *iter=NULL;
1222 TRP_RC rc=TRP_ERROR;
1224 /* use a single time for the entire sweep */
1225 if (0!=clock_gettime(TRP_CLOCK, &sweep_time)) {
1226 tr_err("trps_sweep_ctable: could not read realtime clock.");
1227 sweep_time.tv_sec=0;
1228 sweep_time.tv_nsec=0;
1232 /* iterate all memberships */
1233 iter=tr_comm_iter_new(tmp_ctx);
1235 tr_err("trps_sweep_ctable: unable to allocate iterator.");
1239 for (memb=tr_comm_memb_iter_all_first(iter, trps->ctable);
1241 memb=tr_comm_memb_iter_all_next(iter)) {
1242 if (tr_comm_memb_get_origin(memb)==NULL)
1243 continue; /* do not expire local entries */
1245 if (tr_comm_memb_is_expired(memb, &sweep_time)) {
1246 if (tr_comm_memb_get_times_expired(memb)>0) {
1247 /* Already expired once; flush. */
1248 tr_debug("trps_sweep_ctable: flushing expired community membership (%.*s in %.*s, origin %.*s, expired %s).",
1249 tr_comm_memb_get_realm_id(memb)->len, tr_comm_memb_get_realm_id(memb)->buf,
1250 tr_comm_get_id(tr_comm_memb_get_comm(memb))->len, tr_comm_get_id(tr_comm_memb_get_comm(memb))->buf,
1251 tr_comm_memb_get_origin(memb)->len, tr_comm_memb_get_origin(memb)->buf,
1252 timespec_to_str(tr_comm_memb_get_expiry(memb)));
1253 tr_comm_table_remove_memb(trps->ctable, memb);
1254 tr_comm_memb_free(memb);
1256 /* This is the first expiration. Note this and reset the expiry time. */
1257 tr_comm_memb_expire(memb);
1258 trps_compute_expiry(trps, tr_comm_memb_get_interval(memb), tr_comm_memb_get_expiry(memb));
1259 tr_debug("trps_sweep_ctable: community membership expired at %s, resetting expiry to %s (%.*s in %.*s, origin %.*s).",
1260 timespec_to_str(&sweep_time),
1261 timespec_to_str(tr_comm_memb_get_expiry(memb)),
1262 tr_comm_memb_get_realm_id(memb)->len, tr_comm_memb_get_realm_id(memb)->buf,
1263 tr_comm_get_id(tr_comm_memb_get_comm(memb))->len, tr_comm_get_id(tr_comm_memb_get_comm(memb))->buf,
1264 tr_comm_memb_get_origin(memb)->len, tr_comm_memb_get_origin(memb)->buf);
1269 /* get rid of any unreferenced realms, etc */
1270 tr_comm_table_sweep(trps->ctable);
1273 talloc_free(tmp_ctx);
1278 static unsigned int trps_metric_add(unsigned int m1, unsigned int m2)
1280 if (trp_metric_is_invalid(m1) || trp_metric_is_invalid(m2))
1281 return TRP_METRIC_INVALID;
1283 if (trp_metric_is_infinite(m1) || trp_metric_is_infinite(m2))
1284 return TRP_METRIC_INFINITY;
1286 if (trp_metric_is_finite(m1+m2))
1289 return TRP_METRIC_INFINITY;
1292 /* convert an rentry into a new trp update info record */
1293 static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1295 TRP_INFOREC *rec=trp_inforec_new(mem_ctx, TRP_INFOREC_TYPE_ROUTE);
1296 unsigned int linkcost=0;
1299 if (trp_route_is_local(route))
1302 linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
1303 trp_route_get_peer(route)));
1306 /* Note that we leave the next hop empty since the recipient fills that in.
1307 * This is where we add the link cost (currently always 1) to the next peer. */
1308 if ((trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
1309 ||(trp_inforec_set_metric(rec,
1310 trps_metric_add(trp_route_get_metric(route),
1311 linkcost)) != TRP_SUCCESS)
1312 ||(trp_inforec_set_interval(rec, trps_get_update_interval(trps)) != TRP_SUCCESS)) {
1313 tr_err("trps_route_to_inforec: error creating route update.");
1321 static TRP_UPD *trps_route_to_upd(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TRP_ROUTE *route)
1323 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1324 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1325 TRP_INFOREC *rec=NULL;
1328 tr_err("trps_route_to_upd: could not create update message.");
1331 trp_upd_set_realm(upd, trp_route_dup_realm(route));
1332 if (trp_upd_get_realm(upd)==NULL) {
1333 tr_err("trps_route_to_upd: could not copy realm.");
1334 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1337 trp_upd_set_comm(upd, trp_route_dup_comm(route));
1338 if (trp_upd_get_comm(upd)==NULL) {
1339 tr_err("trps_route_to_upd: could not copy comm.");
1340 upd=NULL; /* it's still in tmp_ctx, so it will be freed */
1343 rec=trps_route_to_inforec(tmp_ctx, trps, route);
1345 tr_err("trps_route_to_upd: could not create route info record for realm %.*s in comm %.*s.",
1346 trp_route_get_realm(route)->len, trp_route_get_realm(route)->buf,
1347 trp_route_get_comm(route)->len, trp_route_get_comm(route)->buf);
1348 upd=NULL; /* it's till in tmp_ctx, so it will be freed */
1351 trp_upd_add_inforec(upd, rec);
1354 talloc_steal(mem_ctx, upd);
1357 talloc_free(tmp_ctx);
1361 /* select the correct route to comm/realm to be announced to peer */
1362 static TRP_ROUTE *trps_select_realm_update(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer_gssname)
1366 /* Take the currently selected route unless it is through the peer we're sending the update to.
1367 * I.e., enforce the split horizon rule. */
1368 route=trp_rtable_get_selected_entry(trps->rtable, comm, realm);
1370 /* No selected route, this should only happen if the only route has been retracted,
1371 * in which case we do not want to advertise it. */
1374 tr_debug("trps_select_realm_update: %s vs %s", peer_gssname->buf,
1375 trp_route_get_peer(route)->buf);
1376 if (0==tr_name_cmp(peer_gssname, trp_route_get_peer(route))) {
1377 tr_debug("trps_select_realm_update: matched, finding alternate route");
1378 /* the selected entry goes through the peer we're reporting to, choose an alternate */
1379 route=trps_find_best_route(trps, comm, realm, peer_gssname);
1380 if ((route==NULL) || (!trp_metric_is_finite(trp_route_get_metric(route))))
1381 return NULL; /* don't advertise a nonexistent or retracted route */
1386 /* Add TRP_UPD msgs to the updates GPtrArray. Caller needs to arrange for these to be freed. */
1387 static TRP_RC trps_select_route_updates_for_peer(TALLOC_CTX *mem_ctx,
1389 TRPS_INSTANCE *trps,
1390 TR_NAME *peer_gssname,
1394 TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
1395 TR_NAME **realm=NULL;
1398 TRP_ROUTE *best=NULL;
1404 for (ii=0; ii<n_comm; ii++) {
1405 realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
1406 for (jj=0; jj<n_realm; jj++) {
1407 best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_gssname);
1408 /* If we found a route, add it to the list. If triggered!=0, then only
1409 * add triggered routes. */
1410 if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best))) {
1411 upd=trps_route_to_upd(mem_ctx, trps, best);
1413 tr_err("trps_select_route_updates_for_peer: unable to create update message.");
1416 g_ptr_array_add(updates, upd);
1432 static TRP_INFOREC *trps_memb_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_COMM_MEMB *memb)
1434 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1435 TRP_INFOREC *rec=NULL;
1441 comm=tr_comm_memb_get_comm(memb);
1442 rec=trp_inforec_new(tmp_ctx, TRP_INFOREC_TYPE_COMMUNITY);
1446 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_get_type(comm))) {
1451 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_comm_memb_get_role(memb))) {
1456 if ((NULL!=tr_comm_get_apcs(comm)) &&
1457 ( (TRP_SUCCESS!=trp_inforec_set_apcs(rec,
1458 tr_apc_dup(rec, tr_comm_get_apcs(comm)))) ||
1459 (NULL==trp_inforec_get_apcs(rec)))) {
1464 if ((NULL!=tr_comm_get_owner_realm(comm)) &&
1465 ( (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_dup_name(tr_comm_get_owner_realm(comm)))) ||
1466 (NULL==trp_inforec_get_owner_realm(rec)))) {
1471 if ((NULL!=tr_comm_get_owner_contact(comm)) &&
1472 ( (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_dup_name(tr_comm_get_owner_contact(comm)))) ||
1473 (NULL==trp_inforec_get_owner_contact(rec)))) {
1478 if ((NULL!=tr_comm_memb_get_provenance(memb)) &&
1479 (TRP_SUCCESS!=trp_inforec_set_provenance(rec, tr_comm_memb_get_provenance(memb)))) {
1484 if (TRP_SUCCESS!=trp_inforec_set_interval(rec, trps_get_update_interval(trps))) {
1490 talloc_steal(mem_ctx, rec);
1493 talloc_free(tmp_ctx);
1497 /* construct an update with all the inforecs for comm/realm/role to be sent to peer */
1498 static TRP_UPD *trps_comm_update(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *trps, TR_NAME *peer_gssname, TR_COMM *comm, TR_REALM *realm)
1500 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1501 TRP_UPD *upd=trp_upd_new(tmp_ctx);
1502 TRP_INFOREC *rec=NULL;
1503 TR_COMM_ITER *iter=NULL;
1504 TR_COMM_MEMB *memb=NULL;
1509 trp_upd_set_comm(upd, tr_comm_dup_id(comm));
1510 trp_upd_set_realm(upd, tr_realm_dup_id(realm));
1511 /* leave peer empty */
1513 iter=tr_comm_iter_new(tmp_ctx);
1515 tr_err("trps_comm_update: unable to allocate iterator.");
1520 /* now add inforecs */
1521 switch (realm->role) {
1523 memb=tr_comm_table_find_idp_memb(trps->ctable,
1524 tr_realm_get_id(realm),
1525 tr_comm_get_id(comm));
1528 memb=tr_comm_table_find_rp_memb(trps->ctable,
1529 tr_realm_get_id(realm),
1530 tr_comm_get_id(comm));
1536 for (memb=tr_comm_memb_iter_first(iter, memb);
1538 memb=tr_comm_memb_iter_next(iter)) {
1539 rec=trps_memb_to_inforec(tmp_ctx, trps, memb);
1541 tr_err("trps_comm_update: unable to allocate inforec.");
1545 trp_upd_add_inforec(upd, rec);
1549 if (trp_upd_get_inforec(upd)==NULL)
1550 upd=NULL; /* no inforecs, no reason to send the update */
1552 talloc_steal(mem_ctx, upd); /* success! */
1555 talloc_free(tmp_ctx);
1559 /* Find all community updates to send to a peer and add these as TR_UPD records
1560 * to the updates GPtrArray. */
1561 static TRP_RC trps_select_comm_updates_for_peer(TALLOC_CTX *mem_ctx,
1563 TRPS_INSTANCE *trps,
1564 TR_NAME *peer_gssname,
1567 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1568 TR_COMM_ITER *comm_iter=NULL;
1570 TR_COMM_ITER *realm_iter=NULL;
1571 TR_REALM *realm=NULL;
1573 TRP_RC rc=TRP_ERROR;
1575 /* currently do not send any communities on triggered updates */
1581 comm_iter=tr_comm_iter_new(tmp_ctx);
1582 realm_iter=tr_comm_iter_new(tmp_ctx);
1583 if ((comm_iter==NULL) || (realm_iter==NULL)) {
1584 tr_err("trps_select_comm_updates_for_peer: unable to allocate iterator.");
1589 /* do every community */
1590 for (comm=tr_comm_table_iter_first(comm_iter, trps->ctable);
1592 comm=tr_comm_table_iter_next(comm_iter)) {
1593 /* do every realm in this community */
1594 tr_debug("trps_select_comm_updates_for_peer: looking through community %.*s",
1595 tr_comm_get_id(comm)->len,
1596 tr_comm_get_id(comm)->buf);
1597 for (realm=tr_realm_iter_first(realm_iter, trps->ctable, tr_comm_get_id(comm));
1599 realm=tr_realm_iter_next(realm_iter)) {
1600 /* get the update for this comm/realm */
1601 tr_debug("trps_select_comm_updates_for_peer: adding realm %.*s",
1602 tr_realm_get_id(realm)->len,
1603 tr_realm_get_id(realm)->buf);
1604 upd=trps_comm_update(mem_ctx, trps, peer_gssname, comm, realm);
1606 g_ptr_array_add(updates, upd);
1611 talloc_free(tmp_ctx);
1616 /* helper for trps_update_one_peer. Frees the TRP_UPD pointed to by a GPtrArray element */
1617 static void trps_trp_upd_destroy(gpointer data)
1619 trp_upd_free((TRP_UPD *)data);
1622 /* all routes/communities to a single peer, unless comm/realm are specified (both or neither must be NULL) */
1623 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
1625 TRP_UPDATE_TYPE update_type,
1629 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1630 TR_MSG msg; /* not a pointer! */
1632 TRP_ROUTE *route=NULL;
1635 TRP_RC rc=TRP_ERROR;
1636 TR_NAME *peer_label=trp_peer_get_label(peer);
1637 GPtrArray *updates=g_ptr_array_new_with_free_func(trps_trp_upd_destroy);
1639 if (updates==NULL) {
1640 tr_err("trps_update_one_peer: unable to allocate updates array.");
1645 switch (update_type) {
1646 case TRP_UPDATE_TRIGGERED:
1647 tr_debug("trps_update_one_peer: preparing triggered update for %.*s",
1648 peer_label->len, peer_label->buf);
1650 case TRP_UPDATE_SCHEDULED:
1651 tr_debug("trps_update_one_peer: preparing scheduled update for %.*s",
1652 peer_label->len, peer_label->buf);
1654 case TRP_UPDATE_REQUESTED:
1655 tr_debug("trps_update_one_peer: preparing requested update for %.*s",
1656 peer_label->len, peer_label->buf);
1659 tr_err("trps_update_one_peer: invalid update type requested.");
1664 /* First, gather route updates. */
1665 tr_debug("trps_update_one_peer: selecting route updates for %.*s.", peer_label->len, peer_label->buf);
1666 if ((comm==NULL) && (realm==NULL)) {
1668 rc=trps_select_route_updates_for_peer(tmp_ctx,
1672 update_type==TRP_UPDATE_TRIGGERED);
1673 } else if ((comm!=NULL) && (realm!=NULL)) {
1674 /* a single community/realm was requested */
1675 route=trps_select_realm_update(trps, comm, realm, peer_label);
1677 /* we have no actual update to send back, MUST send a retraction */
1678 tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
1679 route=trp_route_new(tmp_ctx);
1680 trp_route_set_comm(route, tr_dup_name(comm));
1681 trp_route_set_realm(route, tr_dup_name(realm));
1682 trp_route_set_peer(route, tr_new_name(""));
1683 trp_route_set_metric(route, TRP_METRIC_INFINITY);
1684 trp_route_set_trust_router(route, tr_new_name(""));
1685 trp_route_set_next_hop(route, tr_new_name(""));
1687 upd=trps_route_to_upd(tmp_ctx, trps, route);
1689 tr_err("trps_update_one_peer: unable to allocate route update.");
1693 g_ptr_array_add(updates, upd);
1695 tr_err("trps_update_one_peer: error: only comm or realm was specified. Need both or neither.");
1700 /* Second, gather community updates */
1701 tr_debug("trps_update_one_peer: selecting community updates for %.*s.", peer_label->len, peer_label->buf);
1702 rc=trps_select_comm_updates_for_peer(tmp_ctx, updates, trps, peer_label, update_type==TRP_UPDATE_TRIGGERED);
1704 /* see if we have anything to send */
1705 if (updates->len<=0)
1706 tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
1708 tr_debug("trps_update_one_peer: sending %d update messages.", updates->len);
1709 for (ii=0; ii<updates->len; ii++) {
1710 upd=(TRP_UPD *)g_ptr_array_index(updates, ii);
1711 /* now encode the update message */
1712 tr_msg_set_trp_upd(&msg, upd);
1713 encoded=tr_msg_encode(&msg);
1714 if (encoded==NULL) {
1715 tr_err("trps_update_one_peer: error encoding update.");
1720 tr_debug("trps_update_one_peer: adding message to queue.");
1721 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
1722 tr_err("trps_update_one_peer: error queueing update.");
1724 tr_debug("trps_update_one_peer: update queued successfully.");
1726 tr_msg_free_encoded(encoded);
1735 g_ptr_array_free(updates, TRUE); /* frees any TRP_UPD records */
1736 talloc_free(tmp_ctx);
1740 /* all routes/communities to all peers */
1741 TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
1743 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1744 TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
1745 TRP_PEER *peer=NULL;
1746 TRP_RC rc=TRP_SUCCESS;
1748 if (trps->ptable==NULL)
1749 return TRP_SUCCESS; /* no peers, nothing to do */
1752 tr_err("trps_update: failed to allocate peer table iterator.");
1753 talloc_free(tmp_ctx);
1757 for (peer=trp_ptable_iter_first(iter, trps->ptable);
1758 (peer!=NULL) && (rc==TRP_SUCCESS);
1759 peer=trp_ptable_iter_next(iter))
1761 if (!trps_peer_connected(trps, peer)) {
1762 TR_NAME *peer_label=trp_peer_get_label(peer);
1763 tr_debug("trps_update: no TRP connection to %.*s, skipping.",
1764 peer_label->len, peer_label->buf);
1767 rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
1770 tr_debug("trps_update: rc=%u after attempting update.", rc);
1771 trp_ptable_iter_free(iter);
1772 trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
1773 talloc_free(tmp_ctx);
1777 TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
1779 trp_rtable_add(trps->rtable, route); /* should return status */
1783 /* steals the peer object */
1784 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
1786 if (trps->ptable==NULL) {
1787 trps->ptable=trp_ptable_new(trps);
1788 if (trps->ptable==NULL)
1791 return trp_ptable_add(trps->ptable, peer);
1794 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
1796 if (trps->ptable==NULL)
1799 return trp_ptable_find_gss_name(trps->ptable, gssname);
1802 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
1804 if (trps->ptable==NULL)
1807 return trp_ptable_find_servicename(trps->ptable, servicename);
1810 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
1812 TRPC_INSTANCE *trpc=trps_find_trpc(trps, peer);
1816 if (trpc_get_status(trpc)==TRP_CONNECTION_UP)
1823 static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
1826 TR_NAME *realm=NULL;
1828 tr_debug("trps_handle_request: handling TRP request.");
1830 if (trps_validate_request(trps, req) != TRP_SUCCESS) {
1831 tr_notice("trps_handle_request: received invalid TRP request.");
1835 if (!trp_req_is_wildcard(req)) {
1836 comm=trp_req_get_comm(req);
1837 realm=trp_req_get_realm(req);
1838 tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
1839 comm->len, comm->buf, realm->len, realm->buf);
1841 tr_debug("trps_handle_request: all routes requested.");
1842 /* leave comm/realm NULL */
1844 return trps_update_one_peer(trps,
1845 trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
1846 TRP_UPDATE_REQUESTED,
1852 TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
1854 TRP_RC rc=TRP_ERROR;
1856 switch (tr_msg_get_msg_type(tr_msg)) {
1858 rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
1859 if (rc==TRP_SUCCESS) {
1860 rc=trps_update_active_routes(trps);
1861 trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
1866 rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
1870 /* unknown error or one we don't care about (e.g., TID messages) */
1875 /* send wildcard route request to a peer */
1876 TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
1878 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1879 TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
1880 TR_MSG msg; /* not a pointer */
1881 TRP_REQ *req=trp_req_new(tmp_ctx);
1883 TRP_RC rc=TRP_ERROR;
1886 tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
1890 if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
1891 tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
1896 tr_msg_set_trp_req(&msg, req);
1897 encoded=tr_msg_encode(&msg);
1898 if (encoded==NULL) {
1899 tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
1904 tr_debug("trps_wildcard_route_req: adding message to queue.");
1905 if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
1906 tr_err("trps_wildcard_route_req: error queueing request.");
1909 tr_debug("trps_wildcard_route_req: request queued successfully.");
1915 tr_msg_free_encoded(encoded);
1919 talloc_free(tmp_ctx);