Home | History | Annotate | Line # | Download | only in mrouted
rsrr.c revision 1.3
      1  1.3  mycroft /*	$NetBSD: rsrr.c,v 1.3 1995/12/10 10:07:14 mycroft Exp $	*/
      2  1.2  thorpej 
      3  1.1  mycroft /*
      4  1.1  mycroft  * Copyright (c) 1993 by the University of Southern California
      5  1.1  mycroft  * All rights reserved.
      6  1.1  mycroft  *
      7  1.1  mycroft  * Permission to use, copy, modify, and distribute this software and its
      8  1.1  mycroft  * documentation in source and binary forms for non-commercial purposes
      9  1.1  mycroft  * and without fee is hereby granted, provided that the above copyright
     10  1.1  mycroft  * notice appear in all copies and that both the copyright notice and
     11  1.1  mycroft  * this permission notice appear in supporting documentation. and that
     12  1.1  mycroft  * any documentation, advertising materials, and other materials related
     13  1.1  mycroft  * to such distribution and use acknowledge that the software was
     14  1.1  mycroft  * developed by the University of Southern California, Information
     15  1.1  mycroft  * Sciences Institute.  The name of the University may not be used to
     16  1.1  mycroft  * endorse or promote products derived from this software without
     17  1.1  mycroft  * specific prior written permission.
     18  1.1  mycroft  *
     19  1.1  mycroft  * THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
     20  1.1  mycroft  * the suitability of this software for any purpose.  THIS SOFTWARE IS
     21  1.1  mycroft  * PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     22  1.1  mycroft  * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     23  1.1  mycroft  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     24  1.1  mycroft  *
     25  1.1  mycroft  * Other copyrights might apply to parts of this software and are so
     26  1.1  mycroft  * noted when applicable.
     27  1.1  mycroft  */
     28  1.1  mycroft 
     29  1.1  mycroft /* RSRR code written by Daniel Zappala, USC Information Sciences Institute,
     30  1.1  mycroft  * April 1995.
     31  1.1  mycroft  */
     32  1.1  mycroft 
     33  1.3  mycroft /* May 1995 -- Added support for Route Change Notification */
     34  1.3  mycroft 
     35  1.1  mycroft #ifdef RSRR
     36  1.1  mycroft 
     37  1.1  mycroft #include "defs.h"
     38  1.3  mycroft #include <sys/param.h>
     39  1.3  mycroft #if (defined(BSD) && (BSD >= 199103))
     40  1.3  mycroft #include <stddef.h>
     41  1.3  mycroft #endif
     42  1.1  mycroft 
     43  1.1  mycroft /* Taken from prune.c */
     44  1.1  mycroft /*
     45  1.1  mycroft  * checks for scoped multicast addresses
     46  1.1  mycroft  */
     47  1.1  mycroft #define GET_SCOPE(gt) { \
     48  1.1  mycroft 	register int _i; \
     49  1.1  mycroft 	if (((gt)->gt_mcastgrp & 0xff000000) == 0xef000000) \
     50  1.1  mycroft 	    for (_i = 0; _i < numvifs; _i++) \
     51  1.1  mycroft 		if (scoped_addr(_i, (gt)->gt_mcastgrp)) \
     52  1.1  mycroft 		    VIFM_SET(_i, (gt)->gt_scope); \
     53  1.1  mycroft 	}
     54  1.1  mycroft 
     55  1.1  mycroft /*
     56  1.1  mycroft  * Exported variables.
     57  1.1  mycroft  */
     58  1.1  mycroft int rsrr_socket;			/* interface to reservation protocol */
     59  1.1  mycroft 
     60  1.1  mycroft /*
     61  1.1  mycroft  * Global RSRR variables.
     62  1.1  mycroft  */
     63  1.1  mycroft char rsrr_recv_buf[RSRR_MAX_LEN];	/* RSRR receive buffer */
     64  1.1  mycroft char rsrr_send_buf[RSRR_MAX_LEN];	/* RSRR send buffer */
     65  1.1  mycroft 
     66  1.3  mycroft struct sockaddr_un client_addr;
     67  1.3  mycroft int client_length = sizeof(client_addr);
     68  1.3  mycroft 
     69  1.3  mycroft 
     70  1.1  mycroft /*
     71  1.1  mycroft  * Procedure definitions needed internally.
     72  1.1  mycroft  */
     73  1.3  mycroft static void	rsrr_accept __P((int recvlen));
     74  1.3  mycroft static void	rsrr_accept_iq __P((void));
     75  1.3  mycroft static int	rsrr_accept_rq __P((struct rsrr_rq *route_query, int flags,
     76  1.3  mycroft 					struct gtable *gt_notify));
     77  1.3  mycroft static int	rsrr_send __P((int sendlen));
     78  1.3  mycroft static void	rsrr_cache __P((struct gtable *gt,
     79  1.3  mycroft 					struct rsrr_rq *route_query));
     80  1.1  mycroft 
     81  1.1  mycroft /* Initialize RSRR socket */
     82  1.1  mycroft void
     83  1.1  mycroft rsrr_init()
     84  1.1  mycroft {
     85  1.1  mycroft     int servlen;
     86  1.1  mycroft     struct sockaddr_un serv_addr;
     87  1.1  mycroft 
     88  1.1  mycroft     if ((rsrr_socket = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)
     89  1.1  mycroft 	log(LOG_ERR, errno, "Can't create RSRR socket");
     90  1.1  mycroft 
     91  1.1  mycroft     unlink(RSRR_SERV_PATH);
     92  1.1  mycroft     bzero((char *) &serv_addr, sizeof(serv_addr));
     93  1.1  mycroft     serv_addr.sun_family = AF_UNIX;
     94  1.1  mycroft     strcpy(serv_addr.sun_path, RSRR_SERV_PATH);
     95  1.3  mycroft #if (defined(BSD) && (BSD >= 199103))
     96  1.3  mycroft     servlen = offsetof(struct sockaddr_un, sun_path) +
     97  1.3  mycroft 		strlen(serv_addr.sun_path);
     98  1.3  mycroft     serv_addr.sun_len = servlen;
     99  1.3  mycroft #else
    100  1.1  mycroft     servlen = sizeof(serv_addr.sun_family) + strlen(serv_addr.sun_path);
    101  1.3  mycroft #endif
    102  1.1  mycroft 
    103  1.1  mycroft     if (bind(rsrr_socket, (struct sockaddr *) &serv_addr, servlen) < 0)
    104  1.1  mycroft 	log(LOG_ERR, errno, "Can't bind RSRR socket");
    105  1.1  mycroft 
    106  1.1  mycroft     if (register_input_handler(rsrr_socket,rsrr_read) < 0)
    107  1.1  mycroft 	log(LOG_WARNING, 0, "Couldn't register RSRR as an input handler");
    108  1.1  mycroft }
    109  1.1  mycroft 
    110  1.1  mycroft /* Read a message from the RSRR socket */
    111  1.1  mycroft void
    112  1.3  mycroft rsrr_read(f, rfd)
    113  1.3  mycroft 	int f;
    114  1.3  mycroft 	fd_set *rfd;
    115  1.1  mycroft {
    116  1.1  mycroft     register int rsrr_recvlen;
    117  1.1  mycroft     register int omask;
    118  1.1  mycroft 
    119  1.1  mycroft     bzero((char *) &client_addr, sizeof(client_addr));
    120  1.1  mycroft     rsrr_recvlen = recvfrom(rsrr_socket, rsrr_recv_buf, sizeof(rsrr_recv_buf),
    121  1.3  mycroft 			    0, (struct sockaddr *)&client_addr, &client_length);
    122  1.1  mycroft     if (rsrr_recvlen < 0) {
    123  1.1  mycroft 	if (errno != EINTR)
    124  1.1  mycroft 	    log(LOG_ERR, errno, "RSRR recvfrom");
    125  1.1  mycroft 	return;
    126  1.1  mycroft     }
    127  1.1  mycroft     /* Use of omask taken from main() */
    128  1.1  mycroft     omask = sigblock(sigmask(SIGALRM));
    129  1.3  mycroft     rsrr_accept(rsrr_recvlen);
    130  1.1  mycroft     (void)sigsetmask(omask);
    131  1.1  mycroft }
    132  1.1  mycroft 
    133  1.1  mycroft /* Accept a message from the reservation protocol and take
    134  1.1  mycroft  * appropriate action.
    135  1.1  mycroft  */
    136  1.3  mycroft static void
    137  1.3  mycroft rsrr_accept(recvlen)
    138  1.1  mycroft     int recvlen;
    139  1.1  mycroft {
    140  1.1  mycroft     struct rsrr_header *rsrr;
    141  1.1  mycroft     struct rsrr_rq *route_query;
    142  1.1  mycroft 
    143  1.1  mycroft     if (recvlen < RSRR_HEADER_LEN) {
    144  1.1  mycroft 	log(LOG_WARNING, 0,
    145  1.1  mycroft 	    "Received RSRR packet of %d bytes, which is less than min size",
    146  1.1  mycroft 	    recvlen);
    147  1.1  mycroft 	return;
    148  1.1  mycroft     }
    149  1.1  mycroft 
    150  1.1  mycroft     rsrr = (struct rsrr_header *) rsrr_recv_buf;
    151  1.1  mycroft 
    152  1.1  mycroft     if (rsrr->version > RSRR_MAX_VERSION) {
    153  1.1  mycroft 	log(LOG_WARNING, 0,
    154  1.1  mycroft 	    "Received RSRR packet version %d, which I don't understand",
    155  1.1  mycroft 	    rsrr->version);
    156  1.1  mycroft 	return;
    157  1.1  mycroft     }
    158  1.1  mycroft 
    159  1.1  mycroft     switch (rsrr->version) {
    160  1.1  mycroft       case 1:
    161  1.1  mycroft 	switch (rsrr->type) {
    162  1.1  mycroft 	  case RSRR_INITIAL_QUERY:
    163  1.1  mycroft 	    /* Send Initial Reply to client */
    164  1.1  mycroft 	    log(LOG_INFO, 0, "Received Initial Query\n");
    165  1.3  mycroft 	    rsrr_accept_iq();
    166  1.1  mycroft 	    break;
    167  1.1  mycroft 	  case RSRR_ROUTE_QUERY:
    168  1.1  mycroft 	    /* Check size */
    169  1.1  mycroft 	    if (recvlen < RSRR_RQ_LEN) {
    170  1.1  mycroft 		log(LOG_WARNING, 0,
    171  1.1  mycroft 		    "Received Route Query of %d bytes, which is too small",
    172  1.1  mycroft 		    recvlen);
    173  1.1  mycroft 		break;
    174  1.1  mycroft 	    }
    175  1.1  mycroft 	    /* Get the query */
    176  1.1  mycroft 	    route_query = (struct rsrr_rq *) (rsrr_recv_buf + RSRR_HEADER_LEN);
    177  1.1  mycroft 	    log(LOG_INFO, 0,
    178  1.1  mycroft 		"Received Route Query for src %s grp %s notification %d",
    179  1.1  mycroft 		inet_fmt(route_query->source_addr.s_addr, s1),
    180  1.1  mycroft 		inet_fmt(route_query->dest_addr.s_addr,s2),
    181  1.1  mycroft 		BIT_TST(rsrr->flags,RSRR_NOTIFICATION_BIT));
    182  1.1  mycroft 	    /* Send Route Reply to client */
    183  1.3  mycroft 	    rsrr_accept_rq(route_query,rsrr->flags,NULL);
    184  1.1  mycroft 	    break;
    185  1.1  mycroft 	  default:
    186  1.1  mycroft 	    log(LOG_WARNING, 0,
    187  1.1  mycroft 		"Received RSRR packet type %d, which I don't handle",
    188  1.1  mycroft 		rsrr->type);
    189  1.1  mycroft 	    break;
    190  1.1  mycroft 	}
    191  1.1  mycroft 	break;
    192  1.1  mycroft 
    193  1.1  mycroft       default:
    194  1.1  mycroft 	log(LOG_WARNING, 0,
    195  1.1  mycroft 	    "Received RSRR packet version %d, which I don't understand",
    196  1.1  mycroft 	    rsrr->version);
    197  1.1  mycroft 	break;
    198  1.1  mycroft     }
    199  1.1  mycroft }
    200  1.1  mycroft 
    201  1.1  mycroft /* Send an Initial Reply to the reservation protocol. */
    202  1.3  mycroft static void
    203  1.3  mycroft rsrr_accept_iq()
    204  1.1  mycroft {
    205  1.1  mycroft     struct rsrr_header *rsrr;
    206  1.1  mycroft     struct rsrr_vif *vif_list;
    207  1.1  mycroft     struct uvif *v;
    208  1.1  mycroft     int vifi, sendlen;
    209  1.1  mycroft 
    210  1.1  mycroft     /* Check for space.  There should be room for plenty of vifs,
    211  1.1  mycroft      * but we should check anyway.
    212  1.1  mycroft      */
    213  1.1  mycroft     if (numvifs > RSRR_MAX_VIFS) {
    214  1.1  mycroft 	log(LOG_WARNING, 0,
    215  1.1  mycroft 	    "Can't send RSRR Route Reply because %d is too many vifs %d",
    216  1.1  mycroft 	    numvifs);
    217  1.1  mycroft 	return;
    218  1.1  mycroft     }
    219  1.1  mycroft 
    220  1.1  mycroft     /* Set up message */
    221  1.1  mycroft     rsrr = (struct rsrr_header *) rsrr_send_buf;
    222  1.1  mycroft     rsrr->version = 1;
    223  1.1  mycroft     rsrr->type = RSRR_INITIAL_REPLY;
    224  1.1  mycroft     rsrr->flags = 0;
    225  1.1  mycroft     rsrr->num = numvifs;
    226  1.1  mycroft 
    227  1.1  mycroft     vif_list = (struct rsrr_vif *) (rsrr_send_buf + RSRR_HEADER_LEN);
    228  1.1  mycroft 
    229  1.1  mycroft     /* Include the vif list. */
    230  1.1  mycroft     for (vifi=0, v = uvifs; vifi < numvifs; vifi++, v++) {
    231  1.1  mycroft 	vif_list[vifi].id = vifi;
    232  1.1  mycroft 	vif_list[vifi].status = 0;
    233  1.1  mycroft 	if (v->uv_flags & VIFF_DISABLED)
    234  1.1  mycroft 	    BIT_SET(vif_list[vifi].status,RSRR_DISABLED_BIT);
    235  1.1  mycroft 	vif_list[vifi].threshold = v->uv_threshold;
    236  1.1  mycroft 	vif_list[vifi].local_addr.s_addr = v->uv_lcl_addr;
    237  1.1  mycroft     }
    238  1.1  mycroft 
    239  1.1  mycroft     /* Get the size. */
    240  1.1  mycroft     sendlen = RSRR_HEADER_LEN + numvifs*RSRR_VIF_LEN;
    241  1.1  mycroft 
    242  1.1  mycroft     /* Send it. */
    243  1.1  mycroft     log(LOG_INFO, 0, "Send RSRR Initial Reply");
    244  1.3  mycroft     rsrr_send(sendlen);
    245  1.1  mycroft }
    246  1.1  mycroft 
    247  1.3  mycroft /* Send a Route Reply to the reservation protocol.  The Route Query
    248  1.3  mycroft  * contains the query to which we are responding.  The flags contain
    249  1.3  mycroft  * the incoming flags from the query or, for route change
    250  1.3  mycroft  * notification, the flags that should be set for the reply.  The
    251  1.3  mycroft  * kernel table entry contains the routing info to use for a route
    252  1.3  mycroft  * change notification.
    253  1.3  mycroft  */
    254  1.3  mycroft static int
    255  1.3  mycroft rsrr_accept_rq(route_query,flags,gt_notify)
    256  1.3  mycroft     struct rsrr_rq *route_query;
    257  1.3  mycroft     int flags;
    258  1.3  mycroft     struct gtable *gt_notify;
    259  1.1  mycroft {
    260  1.1  mycroft     struct rsrr_header *rsrr;
    261  1.1  mycroft     struct rsrr_rr *route_reply;
    262  1.1  mycroft     struct gtable *gt,local_g;
    263  1.1  mycroft     struct rtentry *r;
    264  1.1  mycroft     int sendlen,i;
    265  1.1  mycroft     u_long mcastgrp;
    266  1.1  mycroft 
    267  1.1  mycroft     /* Set up message */
    268  1.1  mycroft     rsrr = (struct rsrr_header *) rsrr_send_buf;
    269  1.1  mycroft     rsrr->version = 1;
    270  1.1  mycroft     rsrr->type = RSRR_ROUTE_REPLY;
    271  1.3  mycroft     rsrr->flags = 0;
    272  1.1  mycroft     rsrr->num = 0;
    273  1.1  mycroft 
    274  1.1  mycroft     route_reply = (struct rsrr_rr *) (rsrr_send_buf + RSRR_HEADER_LEN);
    275  1.1  mycroft     route_reply->dest_addr.s_addr = route_query->dest_addr.s_addr;
    276  1.1  mycroft     route_reply->source_addr.s_addr = route_query->source_addr.s_addr;
    277  1.1  mycroft     route_reply->query_id = route_query->query_id;
    278  1.1  mycroft 
    279  1.1  mycroft     /* Blank routing entry for error. */
    280  1.1  mycroft     route_reply->in_vif = 0;
    281  1.1  mycroft     route_reply->reserved = 0;
    282  1.1  mycroft     route_reply->out_vif_bm = 0;
    283  1.1  mycroft 
    284  1.3  mycroft     /* Get the size. */
    285  1.3  mycroft     sendlen = RSRR_RR_LEN;
    286  1.3  mycroft 
    287  1.3  mycroft     /* If kernel table entry is defined, then we are sending a Route Reply
    288  1.3  mycroft      * due to a Route Change Notification event.  Use the kernel table entry
    289  1.3  mycroft      * to supply the routing info.
    290  1.3  mycroft      */
    291  1.3  mycroft     if (gt_notify) {
    292  1.3  mycroft 	/* Set flags */
    293  1.3  mycroft 	rsrr->flags = flags;
    294  1.3  mycroft 	/* Include the routing entry. */
    295  1.3  mycroft 	route_reply->in_vif = gt_notify->gt_route->rt_parent;
    296  1.3  mycroft 	route_reply->out_vif_bm = gt_notify->gt_grpmems;
    297  1.3  mycroft 
    298  1.3  mycroft     } else if (find_src_grp(route_query->source_addr.s_addr, 0,
    299  1.3  mycroft 			    route_query->dest_addr.s_addr)) {
    300  1.3  mycroft 
    301  1.3  mycroft 	/* Found kernel entry. Code taken from add_table_entry() */
    302  1.1  mycroft 	gt = gtp ? gtp->gt_gnext : kernel_table;
    303  1.1  mycroft 
    304  1.1  mycroft 	/* Include the routing entry. */
    305  1.1  mycroft 	route_reply->in_vif = gt->gt_route->rt_parent;
    306  1.1  mycroft 	route_reply->out_vif_bm = gt->gt_grpmems;
    307  1.3  mycroft 
    308  1.3  mycroft 	/* Cache reply if using route change notification. */
    309  1.3  mycroft 	if BIT_TST(flags,RSRR_NOTIFICATION_BIT) {
    310  1.3  mycroft 	    rsrr_cache(gt,route_query);
    311  1.3  mycroft 	    BIT_SET(rsrr->flags,RSRR_NOTIFICATION_BIT);
    312  1.3  mycroft 	}
    313  1.1  mycroft 
    314  1.1  mycroft     } else {
    315  1.1  mycroft 	/* No kernel entry; use routing table. */
    316  1.1  mycroft 	r = determine_route(route_query->source_addr.s_addr);
    317  1.1  mycroft 
    318  1.1  mycroft 	if (r != NULL) {
    319  1.1  mycroft 	    /* We need to mimic what will happen if a data packet
    320  1.1  mycroft 	     * is forwarded by multicast routing -- the kernel will
    321  1.1  mycroft 	     * make an upcall and mrouted will install a route in the kernel.
    322  1.1  mycroft 	     * Our outgoing vif bitmap should reflect what that table
    323  1.1  mycroft 	     * will look like.  Grab code from add_table_entry().
    324  1.1  mycroft 	     * This is gross, but it's probably better to be accurate.
    325  1.1  mycroft 	     */
    326  1.1  mycroft 
    327  1.1  mycroft 	    gt = &local_g;
    328  1.1  mycroft 	    mcastgrp = route_query->dest_addr.s_addr;
    329  1.1  mycroft 
    330  1.1  mycroft 	    gt->gt_mcastgrp    	= mcastgrp;
    331  1.1  mycroft 	    gt->gt_grpmems	= 0;
    332  1.1  mycroft 	    gt->gt_scope	= 0;
    333  1.1  mycroft 	    gt->gt_route        = r;
    334  1.1  mycroft 
    335  1.1  mycroft 	    /* obtain the multicast group membership list */
    336  1.1  mycroft 	    for (i = 0; i < numvifs; i++) {
    337  1.1  mycroft 		if (VIFM_ISSET(i, r->rt_children) &&
    338  1.1  mycroft 		    !(VIFM_ISSET(i, r->rt_leaves)))
    339  1.1  mycroft 		    VIFM_SET(i, gt->gt_grpmems);
    340  1.1  mycroft 
    341  1.1  mycroft 		if (VIFM_ISSET(i, r->rt_leaves) && grplst_mem(i, mcastgrp))
    342  1.1  mycroft 		    VIFM_SET(i, gt->gt_grpmems);
    343  1.1  mycroft 	    }
    344  1.1  mycroft 
    345  1.1  mycroft 	    GET_SCOPE(gt);
    346  1.1  mycroft 	    gt->gt_grpmems &= ~gt->gt_scope;
    347  1.1  mycroft 
    348  1.1  mycroft 	    /* Include the routing entry. */
    349  1.1  mycroft 	    route_reply->in_vif = gt->gt_route->rt_parent;
    350  1.1  mycroft 	    route_reply->out_vif_bm = gt->gt_grpmems;
    351  1.1  mycroft 
    352  1.1  mycroft 	} else {
    353  1.1  mycroft 	    /* Set error bit. */
    354  1.1  mycroft 	    BIT_SET(rsrr->flags,RSRR_ERROR_BIT);
    355  1.1  mycroft 	}
    356  1.1  mycroft     }
    357  1.1  mycroft 
    358  1.3  mycroft     if (gt_notify)
    359  1.3  mycroft 	log(LOG_INFO, 0, "Route Change: Send RSRR Route Reply");
    360  1.3  mycroft 
    361  1.3  mycroft     else
    362  1.3  mycroft 	log(LOG_INFO, 0, "Send RSRR Route Reply");
    363  1.3  mycroft 
    364  1.3  mycroft     log(LOG_INFO, 0, "for src %s dst %s in vif %d out vif %d\n",
    365  1.1  mycroft 	inet_fmt(route_reply->source_addr.s_addr,s1),
    366  1.3  mycroft 	inet_fmt(route_reply->dest_addr.s_addr,s2),
    367  1.1  mycroft 	route_reply->in_vif,route_reply->out_vif_bm);
    368  1.1  mycroft 
    369  1.1  mycroft     /* Send it. */
    370  1.3  mycroft     return rsrr_send(sendlen);
    371  1.1  mycroft }
    372  1.1  mycroft 
    373  1.1  mycroft /* Send an RSRR message. */
    374  1.3  mycroft static int
    375  1.3  mycroft rsrr_send(sendlen)
    376  1.1  mycroft     int sendlen;
    377  1.1  mycroft {
    378  1.1  mycroft     int error;
    379  1.1  mycroft 
    380  1.1  mycroft     /* Send it. */
    381  1.1  mycroft     error = sendto(rsrr_socket, rsrr_send_buf, sendlen, 0,
    382  1.3  mycroft 		   (struct sockaddr *)&client_addr, client_length);
    383  1.1  mycroft 
    384  1.1  mycroft     /* Check for errors. */
    385  1.1  mycroft     if (error < 0) {
    386  1.1  mycroft 	log(LOG_WARNING, errno, "Failed send on RSRR socket");
    387  1.3  mycroft     } else if (error != sendlen) {
    388  1.1  mycroft 	log(LOG_WARNING, 0,
    389  1.1  mycroft 	    "Sent only %d out of %d bytes on RSRR socket\n", error, sendlen);
    390  1.1  mycroft     }
    391  1.3  mycroft     return error;
    392  1.3  mycroft }
    393  1.3  mycroft 
    394  1.3  mycroft /* Cache a message being sent to a client.  Currently only used for
    395  1.3  mycroft  * caching Route Reply messages for route change notification.
    396  1.3  mycroft  */
    397  1.3  mycroft static void
    398  1.3  mycroft rsrr_cache(gt,route_query)
    399  1.3  mycroft     struct gtable *gt;
    400  1.3  mycroft     struct rsrr_rq *route_query;
    401  1.3  mycroft {
    402  1.3  mycroft     struct rsrr_cache *rc, **rcnp;
    403  1.3  mycroft     struct rsrr_header *rsrr;
    404  1.3  mycroft 
    405  1.3  mycroft     rsrr = (struct rsrr_header *) rsrr_send_buf;
    406  1.3  mycroft 
    407  1.3  mycroft     rcnp = &gt->gt_rsrr_cache;
    408  1.3  mycroft     while ((rc = *rcnp) != NULL) {
    409  1.3  mycroft 	if ((rc->route_query.source_addr.s_addr ==
    410  1.3  mycroft 	     route_query->source_addr.s_addr) &&
    411  1.3  mycroft 	    (rc->route_query.dest_addr.s_addr ==
    412  1.3  mycroft 	     route_query->dest_addr.s_addr) &&
    413  1.3  mycroft 	    (!strcmp(rc->client_addr.sun_path,client_addr.sun_path))) {
    414  1.3  mycroft 	    /* Cache entry already exists.
    415  1.3  mycroft 	     * Check if route notification bit has been cleared.
    416  1.3  mycroft 	     */
    417  1.3  mycroft 	    if (!BIT_TST(rsrr->flags,RSRR_NOTIFICATION_BIT)) {
    418  1.3  mycroft 		/* Delete cache entry. */
    419  1.3  mycroft 		*rcnp = rc->next;
    420  1.3  mycroft 		free(rc);
    421  1.3  mycroft 	    } else {
    422  1.3  mycroft 		/* Update */
    423  1.3  mycroft 		rc->route_query.query_id = route_query->query_id;
    424  1.3  mycroft 		log(LOG_DEBUG, 0,
    425  1.3  mycroft 			"Update cached query id %ld from client %s\n",
    426  1.3  mycroft 			rc->route_query.query_id, rc->client_addr.sun_path);
    427  1.3  mycroft 	    }
    428  1.3  mycroft 	    return;
    429  1.3  mycroft 	}
    430  1.3  mycroft 	rcnp = &rc->next;
    431  1.3  mycroft     }
    432  1.3  mycroft 
    433  1.3  mycroft     /* Cache entry doesn't already exist.  Create one and insert at
    434  1.3  mycroft      * front of list.
    435  1.3  mycroft      */
    436  1.3  mycroft     rc = (struct rsrr_cache *) malloc(sizeof(struct rsrr_cache));
    437  1.3  mycroft     if (rc == NULL)
    438  1.3  mycroft 	log(LOG_ERR, 0, "ran out of memory");
    439  1.3  mycroft     rc->route_query.source_addr.s_addr = route_query->source_addr.s_addr;
    440  1.3  mycroft     rc->route_query.dest_addr.s_addr = route_query->dest_addr.s_addr;
    441  1.3  mycroft     rc->route_query.query_id = route_query->query_id;
    442  1.3  mycroft     strcpy(rc->client_addr.sun_path, client_addr.sun_path);
    443  1.3  mycroft     rc->client_length = client_length;
    444  1.3  mycroft     rc->next = gt->gt_rsrr_cache;
    445  1.3  mycroft     gt->gt_rsrr_cache = rc;
    446  1.3  mycroft     log(LOG_DEBUG, 0, "Cached query id %ld from client %s\n",
    447  1.3  mycroft 	   rc->route_query.query_id,rc->client_addr.sun_path);
    448  1.3  mycroft }
    449  1.3  mycroft 
    450  1.3  mycroft /* Send all the messages in the cache.  Currently this is used to send
    451  1.3  mycroft  * all the cached Route Reply messages for route change notification.
    452  1.3  mycroft  */
    453  1.3  mycroft void
    454  1.3  mycroft rsrr_cache_send(gt,notify)
    455  1.3  mycroft     struct gtable *gt;
    456  1.3  mycroft     int notify;
    457  1.3  mycroft {
    458  1.3  mycroft     struct rsrr_cache *rc, **rcnp;
    459  1.3  mycroft     int flags = 0;
    460  1.3  mycroft 
    461  1.3  mycroft     if (notify)
    462  1.3  mycroft 	BIT_SET(flags,RSRR_NOTIFICATION_BIT);
    463  1.3  mycroft 
    464  1.3  mycroft     rcnp = &gt->gt_rsrr_cache;
    465  1.3  mycroft     while ((rc = *rcnp) != NULL) {
    466  1.3  mycroft 	if (rsrr_accept_rq(&rc->route_query,flags,gt) < 0) {
    467  1.3  mycroft 	    log(LOG_DEBUG, 0, "Deleting cached query id %ld from client %s\n",
    468  1.3  mycroft 		   rc->route_query.query_id,rc->client_addr.sun_path);
    469  1.3  mycroft 	    /* Delete cache entry. */
    470  1.3  mycroft 	    *rcnp = rc->next;
    471  1.3  mycroft 	    free(rc);
    472  1.3  mycroft 	} else {
    473  1.3  mycroft 	    rcnp = &rc->next;
    474  1.3  mycroft 	}
    475  1.3  mycroft     }
    476  1.3  mycroft }
    477  1.3  mycroft 
    478  1.3  mycroft /* Clean the cache by deleting all entries. */
    479  1.3  mycroft void
    480  1.3  mycroft rsrr_cache_clean(gt)
    481  1.3  mycroft     struct gtable *gt;
    482  1.3  mycroft {
    483  1.3  mycroft     struct rsrr_cache *rc,*rc_next;
    484  1.3  mycroft 
    485  1.3  mycroft     printf("cleaning cache for group %s\n",inet_fmt(gt->gt_mcastgrp, s1));
    486  1.3  mycroft     rc = gt->gt_rsrr_cache;
    487  1.3  mycroft     while (rc) {
    488  1.3  mycroft 	rc_next = rc->next;
    489  1.3  mycroft 	free(rc);
    490  1.3  mycroft 	rc = rc_next;
    491  1.3  mycroft     }
    492  1.3  mycroft     gt->gt_rsrr_cache = NULL;
    493  1.1  mycroft }
    494  1.1  mycroft 
    495  1.1  mycroft void
    496  1.1  mycroft rsrr_clean()
    497  1.1  mycroft {
    498  1.1  mycroft     unlink(RSRR_SERV_PATH);
    499  1.1  mycroft }
    500  1.1  mycroft 
    501  1.3  mycroft #endif /* RSRR */
    502