home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / NR3.C < prev    next >
C/C++ Source or Header  |  1992-05-01  |  26KB  |  987 lines

  1. /* net/rom level 3 low level processing
  2.  * Copyright 1989 by Daniel M. Frank, W9NK.  Permission granted for
  3.  * non-commercial distribution only.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include "global.h"
  9. #include "mbuf.h"
  10. #include "iface.h"
  11. #include "pktdrvr.h"
  12. #include "netuser.h"
  13. #include "arp.h"
  14. #include "slip.h"
  15. #include "ax25.h"
  16. #include "netrom.h"
  17. #include "nr4.h"
  18. #include "lapb.h"
  19. #include "socket.h"
  20. #include "trace.h"
  21. #include "ip.h"
  22. #include "commands.h"
  23.  
  24. static int accept_bc __ARGS((char *addr,unsigned ifnum));
  25. static struct nr_bind *find_best __ARGS((struct nr_bind *list,unsigned obso));
  26. static struct nr_bind *find_binding __ARGS((struct nr_bind *list,struct nrnbr_tab *neighbor));
  27. static struct nrnbr_tab *find_nrnbr __ARGS((char *, unsigned));
  28. static struct nrnf_tab *find_nrnf __ARGS((char *, unsigned));
  29. static struct nr_bind *find_worst __ARGS((struct nr_bind *list));
  30. static int ismycall __ARGS((char *addr));
  31. #ifdef    notdef
  32. static char *nr_getroute __ARGS((char *));
  33. #endif
  34. static struct raw_nr *Raw_nr;
  35.  
  36. /* Nodes message broadcast address: "NODES" in shifted ASCII */
  37. char Nr_nodebc[AXALEN] = {
  38.     'N'<<1, 'O'<<1, 'D'<<1, 'E'<<1, 'S'<<1, ' '<<1,
  39.     ('0'<<1) | E
  40. };
  41.  
  42. struct nriface Nrifaces[NRNUMIFACE];
  43. unsigned Nr_numiface;
  44. struct nrnbr_tab *Nrnbr_tab[NRNUMCHAINS];
  45. struct nrroute_tab *Nrroute_tab[NRNUMCHAINS];
  46. struct nrnf_tab *Nrnf_tab[NRNUMCHAINS];
  47. unsigned Nr_nfmode = NRNF_NOFILTER;
  48. unsigned short Nr_ttl = 64;
  49. static unsigned Obso_init = 6;
  50. static unsigned Obso_minbc = 5;
  51. static unsigned Nr_maxroutes = 5;
  52. static unsigned Nr_autofloor = 1;
  53. int Nr_verbose = 0;
  54. struct iface *Nr_iface;
  55.  
  56. /* send a NET/ROM layer 3 datagram */
  57. void
  58. nr3output(dest, data)
  59. char *dest;
  60. struct mbuf *data;
  61. {
  62.     struct nr3hdr n3hdr;
  63.     struct mbuf *n3b;
  64.  
  65.     memcpy(n3hdr.dest,dest,AXALEN);    /* copy destination field */
  66.     n3hdr.ttl = Nr_ttl;    /* time to live from initializer parm */
  67.  
  68.     if((n3b = htonnr3(&n3hdr)) == NULLBUF){
  69.         free_p(data);
  70.         return;
  71.     }
  72.     append(&n3b, data);
  73.     /* The null interface indicates that the packet needs to have */
  74.     /* an appropriate source address inserted by nr_route */
  75.     nr_route(n3b,NULLAX25);
  76. }
  77.  
  78. /* send IP datagrams across a net/rom network connection */
  79. int
  80. nr_send(bp,iface,gateway,tos)
  81. struct mbuf *bp;
  82. struct iface *iface;
  83. int32 gateway;
  84. int tos;
  85. {
  86.     struct arp_tab *arp;
  87.  
  88.     dump(iface,IF_TRACE_OUT,bp);
  89.     iface->rawsndcnt++;
  90.     iface->lastsent = secclock();
  91.     if((arp = arp_lookup(ARP_NETROM,gateway)) == NULLARP){
  92.         free_p(bp);    /* drop the packet if no route */
  93.         return -1;
  94.     }
  95.     nr_sendraw(arp->hw_addr, NRPROTO_IP, NRPROTO_IP, bp);
  96.     return 0;
  97. }
  98.  
  99. /* Send arbitrary protocol data on top of a NET/ROM connection */
  100. void
  101. nr_sendraw(dest,family,proto,data)
  102. char *dest;
  103. unsigned family;
  104. unsigned proto;
  105. struct mbuf *data;
  106. {
  107.     struct mbuf *pbp;
  108.     struct nr4hdr n4hdr;
  109.  
  110.     /* Create a "network extension" transport header */
  111.     n4hdr.opcode = NR4OPPID;
  112.     n4hdr.u.pid.family = family;
  113.     n4hdr.u.pid.proto = proto;
  114.  
  115.     if((pbp = htonnr4(&n4hdr)) == NULLBUF){
  116.         free_p(data);
  117.         return;
  118.     }
  119.     append(&pbp,data);        /* Append the data to that */
  120.     nr3output(dest, pbp); /* and pass off to level 3 code */
  121. }
  122.  
  123. /* Arrange for receipt of raw NET/ROM datagrams */
  124. struct raw_nr *
  125. raw_nr(protocol)
  126. char protocol;
  127. {
  128.     register struct raw_nr *rp;
  129.  
  130.     rp = (struct raw_nr *)callocw(1,sizeof(struct raw_nr));
  131.     rp->protocol = protocol;
  132.     rp->next = Raw_nr;
  133.     if(rp->next != NULLRNR)
  134.         rp->next->prev = rp;
  135.     Raw_nr = rp;
  136.     return rp;
  137. }
  138. /* Free a raw NET/ROM descriptor */
  139. void
  140. del_rnr(rpp)
  141. struct raw_nr *rpp;
  142. {
  143.     register struct raw_nr *rp;
  144.  
  145.     /* Do sanity check on arg */
  146.     for(rp = Raw_nr;rp != NULLRNR;rp = rp->next)
  147.         if(rp == rpp)
  148.             break;
  149.     if(rp == NULLRNR)
  150.         return;    /* Doesn't exist */
  151.  
  152.     /* Unlink */
  153.     if(rp->prev != NULLRNR)
  154.         rp->prev->next = rp->next;
  155.     else
  156.         Raw_nr = rp->next;
  157.     if(rp->next != NULLRNR)
  158.         rp->next->prev = rp->prev;
  159.     /* Free resources */
  160.     free_q(&rp->rcvq);
  161.     free((char *)rp);
  162. }
  163.  
  164. /* Figure out if a call is assigned to one of my net/rom
  165.  * interfaces.
  166.  */
  167. static int
  168. ismycall(addr)
  169. char *addr;
  170. {
  171.     register int i;
  172.     int found = 0;
  173.     
  174.     for(i = 0; i < Nr_numiface; i++)
  175.         if(addreq(Nrifaces[i].iface->hwaddr,addr)){
  176.             found = 1;
  177.             break;
  178.         }
  179.  
  180.     return found;
  181. }
  182.  
  183.  
  184. /* Route net/rom network layer packets.
  185.  */
  186. void
  187. nr_route(bp, iaxp)
  188. struct mbuf *bp;            /* network packet */
  189. struct ax25_cb *iaxp;            /* incoming ax25 control block */
  190. {
  191.     struct nr3hdr n3hdr;
  192.     struct nr4hdr n4hdr;
  193.     struct ax25_cb *axp;
  194.     struct mbuf *hbp, *pbp;
  195.     struct raw_nr *rnr;
  196.     register struct nrnbr_tab *np;
  197.     register struct nrroute_tab *rp;
  198.     register struct nr_bind *bindp;
  199.     struct iface *iface;
  200.     unsigned ifnum;
  201.     
  202.     if(ntohnr3(&n3hdr,&bp) == -1){
  203.         free_p(bp);
  204.         return;
  205.     }
  206.     /* If this isn't an internally generated network packet,
  207.      * give the router a chance to record a route back to the
  208.      * sender, in case they aren't in the local node's routing
  209.      * table yet.
  210.      */
  211.     if(iaxp != NULLAX25 && ax_lookup(iaxp->remote) != NULLAXR){
  212.             
  213.         /* find the interface number */
  214.         for(ifnum = 0; ifnum < Nr_numiface; ifnum++)
  215.             if(iaxp->iface == Nrifaces[ifnum].iface)
  216.                 break;
  217.  
  218.         if(ifnum == Nr_numiface){    /* Not a net/rom interface! */
  219.             free_p(bp);
  220.             return;
  221.         }
  222.         /* Add (possibly) a zero-quality recorded route via */
  223.         /* the neighbor from which this packet was received */
  224.         /* Note that this doesn't work with digipeated neighbors. */
  225.         
  226.         (void) nr_routeadd("      ",n3hdr.source,ifnum,0,iaxp->remote,0,1);
  227.     }
  228.  
  229.     /* A packet from me, to me, can only be one thing:
  230.      * a horrible routing loop.  This will probably result
  231.      * from a bad manual ARP entry, but we should fix these
  232.      * obscure errors as we find them.
  233.      */
  234.     if(ismycall(n3hdr.dest)){
  235.         /* Toss if from me, or if we can't read the header */
  236.         if(iaxp == NULLAX25 || ntohnr4(&n4hdr,&bp) == -1){
  237.             free_p(bp);
  238.         } else if((n4hdr.opcode & NR4OPCODE) == NR4OPPID){
  239.             for(rnr = Raw_nr;rnr!=NULLRNR;rnr = rnr->next){
  240.                 if(rnr->protocol!=n4hdr.u.pid.family ||
  241.                  rnr->protocol != n4hdr.u.pid.proto)
  242.                     continue;
  243.                 /* Duplicate the data portion, and put the
  244.                  * level 3 header back on
  245.                  */
  246.                 dup_p(&pbp,bp,0,len_p(bp));
  247.                 if(pbp != NULLBUF &&
  248.                  (hbp = htonnr3(&n3hdr)) != NULLBUF){
  249.                     append(&hbp,pbp);
  250.                     enqueue(&rnr->rcvq,hbp);
  251.                 } else {
  252.                     free_p(pbp);
  253.                     free_p(hbp);
  254.                 }
  255.             }
  256.             /* IP does not use a NET/ROM level 3 socket */
  257.             if(n4hdr.u.pid.family == NRPROTO_IP
  258.              && n4hdr.u.pid.proto == NRPROTO_IP)
  259.                 ip_route(iaxp->iface,bp,0);
  260.             else        /* we don't do this proto */
  261.                 free_p(bp);
  262.         } else {
  263.             /* Must be net/rom transport: */
  264.             nr4input(&n4hdr,bp);
  265.         }
  266.         return;
  267.     }
  268.     if((rp = find_nrroute(n3hdr.dest)) == NULLNRRTAB){
  269.         /* no route, drop the packet */
  270.         free_p(bp);
  271.         return;
  272.     }
  273.     if((bindp = find_best(rp->routes,1)) == NULLNRBIND){
  274.         /* This shouldn't happen yet, but might if we add */
  275.         /* dead route detection */
  276.         free_p(bp);
  277.         return;
  278.     }
  279.  
  280.     np = bindp->via;
  281.     iface = Nrifaces[np->iface].iface;
  282.  
  283.     /* Now check to see if iaxp is null.  That is */
  284.     /* a signal that the packet originates here, */
  285.     /* so we need to insert the callsign of the appropriate  */
  286.     /* interface */
  287.     if(iaxp == NULLAX25)
  288.         memcpy(n3hdr.source,iface->hwaddr,AXALEN);
  289.     
  290.     /* Make sure there is a connection to the neighbor */
  291.     if((axp = find_ax25(np->call)) == NULLAX25 ||
  292.         (axp->state != LAPB_CONNECTED && axp->state != LAPB_RECOVERY)){
  293.         /* Open a new connection or reinitialize old one */
  294.         /* hwaddr has been advanced to point to neighbor + digis */
  295.         axp = open_ax25(iface,iface->hwaddr,np->call, AX_ACTIVE, Axwindow, s_arcall, s_atcall, s_ascall,-1);
  296.         if(axp == NULLAX25){
  297.             free_p(bp);
  298.             return;
  299.         }
  300.     }
  301.         
  302.     if(--n3hdr.ttl == 0){    /* the packet's time to live is over! */
  303.         free_p(bp);
  304.         return;
  305.     }
  306.     /* now format network header */
  307.     if((pbp = htonnr3(&n3hdr)) == NULLBUF){
  308.         free_p(bp);
  309.         return;
  310.     }
  311.     append(&pbp,bp);        /* append data to header */
  312.  
  313.     /* put AX.25 PID on front */
  314.     bp = pushdown(pbp,1);
  315.     bp->data[0] = PID_NETROM;
  316.  
  317.     if((pbp = segmenter(bp,axp->paclen)) == NULLBUF){
  318.         free_p(bp);
  319.         return;
  320.     }
  321.     send_ax25(axp,pbp,-1);    /* pass it off to ax25 code */
  322. }
  323.     
  324.  
  325. /* Perform a nodes broadcast on interface # ifno in the net/rom
  326.  * interface table.
  327.  */
  328. void
  329. nr_bcnodes(ifno)
  330. unsigned ifno;
  331. {
  332.     struct mbuf *hbp, *dbp, *savehdr;
  333.     struct nrroute_tab *rp;
  334.     struct nrnbr_tab *np;
  335.     struct nr_bind * bp;
  336.     struct nr3dest nrdest;
  337.     int i, didsend = 0, numdest = 0;
  338.     register char *cp;
  339.     struct iface *axif = Nrifaces[ifno].iface;
  340.     
  341.     /* prepare the header */
  342.     if((hbp = alloc_mbuf(NR3NODEHL)) == NULLBUF)
  343.         return;
  344.         
  345.     hbp->cnt = NR3NODEHL;    
  346.     
  347.     *hbp->data = NR3NODESIG;
  348.     memcpy(hbp->data+1,Nrifaces[ifno].alias,ALEN);
  349.  
  350.     /* Some people don't want to advertise any routes; they
  351.      * just want to be a terminal node.  In that case we just
  352.      * want to send our call and alias and be done with it.
  353.      */
  354.  
  355.     if(!Nr_verbose){
  356.         (*axif->output)(axif, Nr_nodebc, axif->hwaddr,
  357.                         PID_NETROM, hbp);    /* send it */
  358.         return;
  359.     }
  360.  
  361.     /* make a copy of the header in case we need to send more than */
  362.     /* one packet */
  363.     savehdr = copy_p(hbp,NR3NODEHL);
  364.  
  365.     /* now scan through the routing table, finding the best routes */
  366.     /* and their neighbors.  create destination subpackets and append */
  367.     /* them to the header */
  368.     for(i = 0; i < NRNUMCHAINS; i++){
  369.         for(rp = Nrroute_tab[i]; rp != NULLNRRTAB; rp = rp->next){
  370.             /* look for best, non-obsolescent route */
  371.             if((bp = find_best(rp->routes,0)) == NULLNRBIND)
  372.                 continue;    /* no non-obsolescent routes found */
  373.             if(bp->quality == 0)    /* this is a loopback route */
  374.                 continue;    /* we never broadcast these */
  375.             np = bp->via;
  376.             /* insert best neighbor */
  377.             memcpy(nrdest.neighbor,np->call,AXALEN);
  378.             /* insert destination from route table */
  379.             memcpy(nrdest.dest,rp->call,AXALEN);
  380.             /* insert alias from route table */
  381.             strcpy(nrdest.alias,rp->alias);
  382.             /* insert quality from binding */
  383.             nrdest.quality = bp->quality;
  384.             /* create a network format destination subpacket */
  385.             if((dbp = htonnrdest(&nrdest)) == NULLBUF){
  386.                 free_p(hbp);    /* drop the whole idea ... */
  387.                 free_p(savehdr);
  388.                 return;
  389.             }
  390.             /* we now have a partially filled packet */
  391.             didsend = 0;    
  392.             append(&hbp,dbp);/* append to header and others */
  393.             /* see if we have appended as many destinations
  394.              * as we can fit into a single broadcast.  If we
  395.              * have, go ahead and send them out.
  396.              */
  397.             if(++numdest == NRDESTPERPACK){    /* filled it up */
  398.                 /* indicate that we did broadcast */
  399.                 didsend = 1;
  400.                 /* reset the destination counter */
  401.                 numdest = 0;
  402.                 (*axif->output)(axif, Nr_nodebc, axif->hwaddr,
  403.                  PID_NETROM,hbp);    /* send it */
  404.                 /* new header */
  405.                 hbp = copy_p(savehdr,NR3NODEHL);
  406.             }
  407.         }
  408.     }
  409.  
  410.     /* Now, here is something totally weird.  If our interfaces */
  411.     /* have different callsigns than this one, advertise a very */
  412.     /* high quality route to them.  Is this a good idea?  I don't */
  413.     /* know.  However, it allows us to simulate a bunch of net/roms */
  414.     /* hooked together with a diode matrix coupler. */
  415.     for(i = 0; i < Nr_numiface; i++){
  416.         if(i == ifno)
  417.             continue;        /* don't bother with ours */
  418.         cp = Nrifaces[i].iface->hwaddr;
  419.         if(!addreq((char *)axif->hwaddr,cp)){
  420.             /* both destination and neighbor address */
  421.             memcpy(nrdest.dest,cp,AXALEN);
  422.             memcpy(nrdest.neighbor,cp,AXALEN);
  423.             /* alias of the interface */
  424.             strcpy(nrdest.alias,Nrifaces[i].alias);
  425.             /* and the very highest quality */
  426.             nrdest.quality = 255;
  427.             /* create a network format destination subpacket */
  428.             if((dbp = htonnrdest(&nrdest)) == NULLBUF){
  429.                 free_p(hbp);    /* drop the whole idea ... */
  430.                 free_p(savehdr);
  431.                 return;
  432.             }
  433.             /* we now have a partially filled packet */
  434.             didsend = 0;    
  435.             /* append to header and others */
  436.             append(&hbp,dbp);
  437.             if(++numdest == NRDESTPERPACK){    /* filled it up */
  438.                 /* indicate that we did broadcast */
  439.                 didsend = 1;
  440.                 /* reset the destination counter */
  441.                 numdest = 0;
  442.                 (*axif->output)(axif, Nr_nodebc, axif->hwaddr,
  443.                  PID_NETROM,hbp);    /* send it */
  444.                 /* new header */
  445.                 hbp = copy_p(savehdr,NR3NODEHL);
  446.             }
  447.         }
  448.     }
  449.             
  450.     /* If we have a partly filled packet left over, or we never */
  451.     /* sent one at all, we broadcast: */
  452.     if(!didsend || numdest > 0)
  453.         (*axif->output)(axif, Nr_nodebc, axif->hwaddr,PID_NETROM, hbp);
  454.  
  455.     /* free the header copies */
  456.     if(numdest == 0)
  457.         free_p(hbp);
  458.     free_p(savehdr);
  459. }
  460.  
  461. /* attach the net/rom interface.  no parms for now. */
  462. int
  463. nr_attach(argc,argv,p)
  464. int argc;
  465. char *argv[];
  466. void *p;
  467. {
  468.     if(Nr_iface != (struct iface *)0){
  469.         printf("netrom interface already attached\n");
  470.         return -1;
  471.     }
  472.     Nr_iface = (struct iface *)callocw(1,sizeof(struct iface));
  473.     Nr_iface->addr = Ip_addr;
  474.  
  475.     /* The strdup is needed to keep the detach routine happy (it'll
  476.      * free the allocated memory)
  477.      */
  478.     Nr_iface->name = strdup("netrom");
  479.     if(Nr_iface->hwaddr == NULLCHAR){
  480.         Nr_iface->hwaddr = mallocw(AXALEN);
  481.         memcpy(Nr_iface->hwaddr,Mycall,AXALEN);
  482.     }
  483.     Nr_iface->mtu = NR4MAXINFO;
  484.     setencap(Nr_iface,"NETROM");
  485.     Nr_iface->next = Ifaces;
  486.     Ifaces = Nr_iface;
  487.     memcpy(Nr4user,Mycall,AXALEN);
  488.     Nr_iface->txproc = newproc("nr tx",512,if_tx,0,Nr_iface,NULL,0);
  489.     return 0;
  490. }
  491.  
  492. /* This function checks an ax.25 address and interface number against
  493.  * the filter table and mode, and returns 1 if the address is to be
  494.  * accepted, and 0 if it is to be filtered out.
  495.  */
  496. static int
  497. accept_bc(addr,ifnum)
  498. char *addr;
  499. unsigned ifnum;
  500. {
  501.     struct nrnf_tab *fp;
  502.  
  503.     if(Nr_nfmode == NRNF_NOFILTER)        /* no filtering in effect */
  504.         return 1;
  505.  
  506.     fp = find_nrnf(addr,ifnum);        /* look it up */
  507.     
  508.     if((fp != NULLNRNFTAB && Nr_nfmode == NRNF_ACCEPT)
  509.         || (fp == NULLNRNFTAB && Nr_nfmode == NRNF_REJECT))
  510.         return 1;
  511.     else
  512.         return 0;
  513. }
  514.  
  515.  
  516. /* receive and process node broadcasts. */
  517. void
  518. nr_nodercv(iface,source,bp)
  519. struct iface *iface;
  520. char *source;
  521. struct mbuf *bp;
  522. {
  523.     register int ifnum;
  524.     char bcalias[AXALEN];
  525.     struct nr3dest ds;
  526.     
  527.     /* First, see if this is even a net/rom interface: */
  528.     for(ifnum = 0; ifnum < Nr_numiface; ifnum++)
  529.         if(iface == Nrifaces[ifnum].iface)
  530.             break;
  531.             
  532.     if(ifnum == Nr_numiface){    /* not in the interface table */
  533.         free_p(bp);
  534.         return;
  535.     }
  536.  
  537.     if(!accept_bc(source,ifnum)){    /* check against filter */
  538.         free_p(bp);
  539.         return;
  540.     }
  541.     
  542.     /* See if it has a routing broadcast signature: */
  543.     if(PULLCHAR(&bp) != NR3NODESIG){
  544.         free_p(bp);
  545.         return;
  546.     }
  547.  
  548.     /* now try to get the alias */
  549.     if(pullup(&bp,bcalias,ALEN) < ALEN){
  550.         free_p(bp);
  551.         return;
  552.     }
  553.  
  554.     bcalias[ALEN] = '\0';        /* null terminate */
  555.  
  556.     /* enter the neighbor into our routing table */
  557.     if(nr_routeadd(bcalias,source,ifnum,Nrifaces[ifnum].quality,
  558.      source, 0, 0) == -1){
  559.         free_p(bp);
  560.         return;
  561.     }
  562.     
  563.     /* we've digested the header; now digest the actual */
  564.     /* routing information */
  565.     while(ntohnrdest(&ds,&bp) != -1){
  566.         /* ignore routes to me! */
  567.         if(ismycall(ds.dest))
  568.             continue;
  569.         /* ignore routes below the minimum quality threshhold */
  570.         if(ds.quality < Nr_autofloor)
  571.             continue;
  572.         /* set loopback paths to 0 quality */
  573.         if(ismycall(ds.neighbor))
  574.             ds.quality = 0;
  575.         else
  576.             ds.quality = ((ds.quality * Nrifaces[ifnum].quality + 128)
  577.              / 256) & 0xff;
  578.         if(nr_routeadd(ds.alias,ds.dest,ifnum,ds.quality,source,0,0)
  579.             == -1)
  580.             break;
  581.     }
  582.             
  583.     free_p(bp);    /* This will free the mbuf if anything fails above */
  584. }
  585.  
  586.  
  587. /* The following are utilities for manipulating the routing table */
  588.  
  589. /* hash function for callsigns.  Look familiar? */
  590. int16
  591. nrhash(s)
  592. char *s;
  593. {
  594.     register char x;
  595.     register int i;
  596.  
  597.     x = 0;
  598.     for(i = ALEN; i !=0; i--)
  599.         x ^= *s++ & 0xfe;
  600.     x ^= *s & SSID;
  601.     return (int16)(uchar(x) % NRNUMCHAINS);
  602. }
  603.  
  604. /* Find a neighbor table entry.  Neighbors are determined by
  605.  * their callsign and the interface number.  This takes care
  606.  * of the case where the same switch or hosts uses the same
  607.  * callsign on two different channels.  This isn't done by
  608.  * net/rom, but it might be done by stations running *our*
  609.  * software.
  610.  */
  611. static struct nrnbr_tab *
  612. find_nrnbr(addr,ifnum)
  613. register char *addr;
  614. unsigned ifnum;
  615. {
  616.     int16 hashval;
  617.     register struct nrnbr_tab *np;
  618.  
  619.     /* Find appropriate hash chain */
  620.     hashval = nrhash(addr);
  621.  
  622.     /* search hash chain */
  623.     for(np = Nrnbr_tab[hashval]; np != NULLNTAB; np = np->next){
  624.         /* convert first in  list to ax25 address format */
  625.         if(addreq(np->call,addr) && np->iface == ifnum){
  626.             return np;
  627.         }
  628.     }
  629.     return NULLNTAB;
  630. }
  631.  
  632.  
  633. /* Find a route table entry */
  634. struct nrroute_tab *
  635. find_nrroute(addr)
  636. register char *addr;
  637. {
  638.     int16 hashval;
  639.     register struct nrroute_tab *rp;
  640.  
  641.     /* Find appropriate hash chain */
  642.     hashval = nrhash(addr);
  643.  
  644.     /* search hash chain */
  645.     for(rp = Nrroute_tab[hashval]; rp != NULLNRRTAB; rp = rp->next){
  646.         if(addreq(rp->call,addr)){
  647.             return rp;
  648.         }
  649.     }
  650.     return NULLNRRTAB;
  651. }
  652.  
  653. /* Try to find the AX.25 address of a node with the given alias.  Return */
  654. /* a pointer to the AX.25 address if found, otherwise NULLCHAR.  The alias */
  655. /* should be a six character, blank-padded, upper-case string. */
  656.  
  657. char *
  658. find_nralias(alias)
  659. char *alias;
  660. {
  661.     int i;
  662.     register struct nrroute_tab *rp;
  663.  
  664.     /* Since the route entries are hashed by ax.25 address, we'll */
  665.     /* have to search all the chains */
  666.     
  667.     for(i = 0; i < NRNUMCHAINS; i++)
  668.         for(rp = Nrroute_tab[i]; rp != NULLNRRTAB; rp = rp->next)
  669.             if(strncmp(alias, rp->alias, 6) == 0)
  670.                 return rp->call;
  671.  
  672.     /* If we get to here, we're out of luck */
  673.  
  674.     return NULLCHAR;
  675. }
  676.  
  677.  
  678. /* Find a binding in a list by its neighbor structure's address */
  679. static struct nr_bind *
  680. find_binding(list,neighbor)
  681. struct nr_bind *list;
  682. register struct nrnbr_tab *neighbor;
  683. {
  684.     register struct nr_bind *bp;
  685.  
  686.     for(bp = list; bp != NULLNRBIND; bp = bp->next)
  687.         if(bp->via == neighbor)
  688.             return bp;
  689.  
  690.     return NULLNRBIND;
  691. }
  692.  
  693. /* Find the worst quality non-permanent binding in a list */
  694. static
  695. struct nr_bind *
  696. find_worst(list)
  697. struct nr_bind *list;
  698. {
  699.     register struct nr_bind *bp;
  700.     struct nr_bind *worst = NULLNRBIND;
  701.     unsigned minqual = 1000;    /* infinity */
  702.  
  703.     for(bp = list; bp != NULLNRBIND; bp = bp->next)
  704.         if(!(bp->flags & NRB_PERMANENT) && bp->quality < minqual){
  705.             worst = bp;
  706.             minqual = bp->quality;
  707.         }
  708.  
  709.     return worst;
  710. }
  711.  
  712. /* Find the best binding of any sort in a list.  If obso is 1,
  713.  * include entries below the obsolescence threshhold in the
  714.  * search (used when this is called for routing broadcasts).
  715.  * If it is 0, routes below the threshhold are treated as
  716.  * though they don't exist.
  717.  */
  718. static
  719. struct nr_bind *
  720. find_best(list,obso)
  721. struct nr_bind *list;
  722. unsigned obso;
  723. {
  724.     register struct nr_bind *bp;
  725.     struct nr_bind *best = NULLNRBIND;
  726.     int maxqual = -1;    /* negative infinity */
  727.  
  728.     for(bp = list; bp != NULLNRBIND; bp = bp->next)
  729.         if((int)bp->quality > maxqual)
  730.             if(obso || bp->obsocnt >= Obso_minbc){
  731.                 best = bp;
  732.                 maxqual = bp->quality;
  733.             }
  734.  
  735.     return best;
  736. }
  737.  
  738. /* Add a route to the net/rom routing table */
  739. int
  740. nr_routeadd(alias,dest,ifnum,quality,neighbor,permanent,record)
  741. char *alias;        /* net/rom node alias, blank-padded and */
  742.             /* null-terminated */
  743. char *dest;        /* destination node callsign */
  744. unsigned ifnum;    /* net/rom interface number */
  745. unsigned quality;    /* route quality */
  746. char *neighbor;    /* neighbor node + 2 digis (max) in arp format */
  747. unsigned permanent;    /* 1 if route is permanent (hand-entered) */
  748. unsigned record;    /* 1 if route is a "record route" */
  749. {
  750.     struct nrroute_tab *rp;
  751.     struct nr_bind *bp;
  752.     struct nrnbr_tab *np;
  753.     int16 rhash, nhash;
  754.  
  755.     /* See if a routing table entry exists for this destination */
  756.     if((rp = find_nrroute(dest)) == NULLNRRTAB){
  757.         rp = (struct nrroute_tab *)callocw(1,sizeof(struct nrroute_tab));
  758.         /* create a new route table entry */
  759.         strncpy(rp->alias,alias,6);
  760.         memcpy(rp->call,dest,AXALEN);
  761.         rhash = nrhash(dest);
  762.         rp->next = Nrroute_tab[rhash];
  763.         if(rp->next != NULLNRRTAB)
  764.             rp->next->prev = rp;
  765.         Nrroute_tab[rhash] = rp;    /* link at head of hash chain */
  766.     } else if(!record){
  767.         strncpy(rp->alias,alias,6);    /* update the alias */
  768.     }
  769.  
  770.     /* See if an entry exists for this neighbor */
  771.     if((np = find_nrnbr(neighbor,ifnum)) == NULLNTAB){
  772.         np = (struct nrnbr_tab *)callocw(1,sizeof(struct nrnbr_tab));
  773.         /* create a new neighbor entry */
  774.         memcpy(np->call,neighbor,AXALEN);
  775.         np->iface = ifnum;
  776.         nhash = nrhash(neighbor);
  777.         np->next = Nrnbr_tab[nhash];
  778.         if(np->next != NULLNTAB)
  779.             np->next->prev = np;
  780.         Nrnbr_tab[nhash] = np;
  781.     } else if(permanent){        /* force this path to the neighbor */
  782.         memcpy(np->call,neighbor,AXALEN);
  783.     }
  784.         
  785.     /* See if there is a binding between the dest and neighbor */
  786.     if((bp = find_binding(rp->routes,np)) == NULLNRBIND){
  787.         bp = (struct nr_bind *)callocw(1,sizeof(struct nr_bind));
  788.         /* create a new binding and link it in */
  789.         bp->via = np;    /* goes via this neighbor */
  790.         bp->next = rp->routes;    /* link into binding chain */
  791.         if(bp->next != NULLNRBIND)
  792.             bp->next->prev = bp;
  793.         rp->routes = bp;
  794.         rp->num_routes++;    /* bump route count */
  795.         np->refcnt++;        /* bump neighbor ref count */
  796.         bp->quality = quality;
  797.         bp->obsocnt = Obso_init;    /* use initial value */
  798.         if(permanent)
  799.             bp->flags |= NRB_PERMANENT;
  800.         else if(record)    /* notice permanent overrides record! */
  801.             bp->flags |= NRB_RECORDED;
  802.     } else {
  803.         if(permanent){    /* permanent request trumps all */
  804.             bp->quality = quality;
  805.             bp->obsocnt = Obso_init;
  806.             bp->flags |= NRB_PERMANENT;
  807.             bp->flags &= ~NRB_RECORDED;    /* perm is not recorded */
  808.         } else if(!(bp->flags & NRB_PERMANENT)){    /* not permanent */
  809.             if(record){    /* came from nr_route */
  810.                 if(bp->flags & NRB_RECORDED){ /* no mod non-rec bindings */
  811.                     bp->quality = quality;
  812.                     bp->obsocnt = Obso_init; /* freshen recorded routes */
  813.                 }
  814.             } else {        /* came from a routing broadcast */
  815.                 bp->quality = quality;
  816.                 bp->obsocnt = Obso_init;
  817.                 bp->flags &= ~NRB_RECORDED; /* no longer a recorded route */
  818.             }
  819.         }
  820.     }
  821.  
  822.     /* Now, check to see if we have too many bindings, and drop */
  823.     /* the worst if we do */
  824.     if(rp->num_routes > Nr_maxroutes){
  825.         /* since find_worst never returns permanent entries, the */
  826.         /* limitation on number of routes is circumvented for    */
  827.         /* permanent routes */
  828.         if((bp = find_worst(rp->routes)) != NULLNRBIND){
  829.             nr_routedrop(dest,bp->via->call,bp->via->iface);
  830.         }
  831.     }
  832.  
  833.     return 0;
  834. }
  835.  
  836.  
  837. /* Drop a route to dest via neighbor */
  838. int
  839. nr_routedrop(dest,neighbor,ifnum)
  840. char *dest, *neighbor;
  841. unsigned ifnum;
  842. {
  843.     register struct nrroute_tab *rp;
  844.     register struct nrnbr_tab *np;
  845.     register struct nr_bind *bp;
  846.  
  847.     if((rp = find_nrroute(dest)) == NULLNRRTAB)
  848.         return -1;
  849.  
  850.     if((np = find_nrnbr(neighbor,ifnum)) == NULLNTAB)
  851.         return -1;
  852.  
  853.     if((bp = find_binding(rp->routes,np)) == NULLNRBIND)
  854.         return -1;
  855.  
  856.     /* drop the binding first */
  857.     if(bp->next != NULLNRBIND)
  858.         bp->next->prev = bp->prev;
  859.     if(bp->prev != NULLNRBIND)
  860.         bp->prev->next = bp->next;
  861.     else
  862.         rp->routes = bp->next;
  863.  
  864.     free((char *)bp);
  865.     rp->num_routes--;        /* decrement the number of bindings */
  866.     np->refcnt--;            /* and the number of neighbor references */
  867.     
  868.     /* now see if we should drop the route table entry */
  869.     if(rp->num_routes == 0){
  870.         if(rp->next != NULLNRRTAB)
  871.             rp->next->prev = rp->prev;
  872.         if(rp->prev != NULLNRRTAB)
  873.             rp->prev->next = rp->next;
  874.         else
  875.             Nrroute_tab[nrhash(dest)] = rp->next;
  876.  
  877.         free((char *)rp);
  878.     }
  879.  
  880.     /* and check to see if this neighbor can be dropped */
  881.     if(np->refcnt == 0){
  882.         if(np->next != NULLNTAB)
  883.             np->next->prev = np->prev;
  884.         if(np->prev != NULLNTAB)
  885.             np->prev->next = np->next;
  886.         else
  887.             Nrnbr_tab[nrhash(neighbor)] = np->next;
  888.  
  889.         free((char *)np);
  890.     }
  891.     
  892.     return 0;
  893. }
  894.  
  895. #ifdef    notused
  896. /* Find the best neighbor for destination dest, in arp format */
  897. static char *
  898. nr_getroute(dest)
  899. char *dest;
  900. {
  901.     register struct nrroute_tab *rp;
  902.     register struct nr_bind *bp;
  903.  
  904.     if((rp = find_nrroute(dest)) == NULLNRRTAB)
  905.         return NULLCHAR;
  906.  
  907.     if((bp = find_best(rp->routes,1)) == NULLNRBIND)    /* shouldn't happen! */
  908.         return NULLCHAR;
  909.  
  910.     return bp->via->call;
  911. }
  912. #endif    /* notused */
  913.  
  914. /* Find an entry in the filter table */
  915. static struct nrnf_tab *
  916. find_nrnf(addr,ifnum)
  917. register char *addr;
  918. unsigned ifnum;
  919. {
  920.     int16 hashval;
  921.     register struct nrnf_tab *fp;
  922.  
  923.     /* Find appropriate hash chain */
  924.     hashval = nrhash(addr);
  925.  
  926.     /* search hash chain */
  927.     for(fp = Nrnf_tab[hashval]; fp != NULLNRNFTAB; fp = fp->next){
  928.         if(addreq(fp->neighbor,addr) && fp->iface == ifnum){
  929.             return fp;
  930.         }
  931.     }
  932.  
  933.     return NULLNRNFTAB;
  934. }
  935.  
  936. /* Add an entry to the filter table.  Return 0 on success,
  937.  * -1 on failure
  938.  */
  939. int
  940. nr_nfadd(addr,ifnum)
  941. char *addr;
  942. unsigned ifnum;
  943. {
  944.     struct nrnf_tab *fp;
  945.     int16 hashval;
  946.     
  947.     if(find_nrnf(addr,ifnum) != NULLNRNFTAB)
  948.         return 0;    /* already there; it's a no-op */
  949.  
  950.     fp = (struct nrnf_tab *)callocw(1,sizeof(struct nrnf_tab));
  951.  
  952.     hashval = nrhash(addr);
  953.     memcpy(fp->neighbor,addr,AXALEN);
  954.     fp->iface = ifnum;
  955.     fp->next = Nrnf_tab[hashval];
  956.     if(fp->next != NULLNRNFTAB)
  957.         fp->next->prev = fp;
  958.     Nrnf_tab[hashval] = fp;
  959.  
  960.     return 0;
  961. }
  962.  
  963. /* Drop a neighbor from the filter table.  Returns 0 on success, -1
  964.  * on failure.
  965.  */
  966. int
  967. nr_nfdrop(addr,ifnum)
  968. char *addr;
  969. unsigned ifnum;
  970. {
  971.     struct nrnf_tab *fp;
  972.  
  973.     if((fp = find_nrnf(addr,ifnum)) == NULLNRNFTAB)
  974.         return -1;    /* not in the table */
  975.  
  976.     if(fp->next != NULLNRNFTAB)
  977.         fp->next->prev = fp->prev;
  978.     if(fp->prev != NULLNRNFTAB)
  979.         fp->prev->next = fp->next;
  980.     else
  981.         Nrnf_tab[nrhash(addr)] = fp->next;
  982.  
  983.     free((char *)fp);
  984.  
  985.     return 0;
  986. }
  987.