home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / AX25AAR.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  5KB  |  190 lines

  1. #include "global.h"
  2. #ifdef AX25
  3. #ifdef AUTOROUTE
  4. #include "mbuf.h"
  5. #include "timer.h"
  6. #include "iface.h"
  7. #include "ax25.h"
  8. #include "icmp.h"
  9. #include "ip.h"
  10. #include "arp.h"
  11. #include "icmp.h"
  12. #include "rip.h"
  13. #include "socket.h"
  14. #include "cmdparse.h"
  15. #include <ctype.h>
  16.   
  17. int ax25_check_corruption __ARGS((struct ax25 *header));
  18. int ax25_legal            __ARGS((char *call_ssid));
  19. int Ax25_autoroute = 0; /*auto-routing on by default*/
  20.   
  21. #ifdef RSPF
  22. extern int RspfActive;
  23. #endif
  24.   
  25. int
  26. doax25autoroute(argc,argv,p)
  27. int argc ;
  28. char *argv[] ;
  29. void *p;
  30. {
  31. #ifdef RSPF
  32.     if(RspfActive) {
  33.         tputs("RSPF active. ");
  34.         argc = 0;
  35.     }
  36. #endif
  37.     return setbool(&Ax25_autoroute,"AX.25 IP autorouting",argc,argv);
  38. }
  39.   
  40. /* Create an ARP_RESPONSE packet for any IP packet we hear via an ax25 interface
  41.  * This will automatically update any preexisting entry.
  42.  * If no route is currently defined, then add that as well...
  43.  *
  44.  */
  45. void
  46. ax25eavesdrop(iface,source,dest,bp)
  47. struct iface *iface;
  48. char *source;
  49. char *dest;
  50. struct mbuf *bp;
  51. {
  52.     struct ip ipstuff;
  53.     register struct route *rt;
  54.     struct arp_tab *ap;
  55.     int16 ip_len;
  56.     int16 length;
  57.     char (*mpp)[AXALEN];
  58.   
  59.     if(len_p(bp) < IPLEN){
  60.         /* The packet is shorter than a legal IP header */
  61.         return;
  62.     }
  63.     /* Sneak a peek at the IP header's IHL field to find its length */
  64.     ip_len = (bp->data[0] & 0xf) << 2;
  65.     if(ip_len < IPLEN){
  66.         /* The IP header length field is too small */
  67.         return;
  68.     }
  69.     if(cksum(NULLHEADER,bp,ip_len) != 0){
  70.         /* Bad IP header checksum; discard */
  71.         return;
  72.     }
  73.     if(ntohip(&ipstuff,&bp) == -1){
  74.         return;
  75.     }
  76.   
  77.     Arp_stat.recv++;
  78.   
  79.     for(mpp = Ax25multi;(*mpp)[0] != '\0';mpp++){
  80.         if(addreq(source,*mpp)){
  81.             /* This guy is trying to say he's got the broadcast address! */
  82.             Arp_stat.badaddr++;
  83.             return;
  84.         }
  85.     }
  86.   
  87.   
  88. /* If this entry already exists, but points to a different iface, don't add. */
  89.     if((rt = rt_lookup(ipstuff.source)) != NULLROUTE)
  90.         if(rt != &R_default)
  91.             if(rt->iface != iface)
  92.                 return;
  93.   
  94. /* If no route currently exists, add one with a high metric, ttl = ARPLIFE, mark it as private */
  95. /* If we know a better route, or we have a manual route entered, go home. */
  96.     if( rt == NULLROUTE ||
  97.         rt == &R_default ||
  98.         ((rt->timer.duration != 0) && (rt->metric >= (RIP_INFINITY - 1))))
  99.         rt_add(ipstuff.source, (unsigned int)32, (int32)0, iface, RIP_INFINITY - 1, ARPLIFE, 1);
  100.   
  101. /* If route is via a gateway, do not add an ARP entry. */
  102.     if( rt->gateway == (int32)0){
  103.         /* If this guy is already in the table, update its entry
  104.          * unless it's a manual entry (noted by the lack of a timer)
  105.          */
  106.         ap = NULLARP;   /* ap plays the role of merge_flag in the spec */
  107.         if(((ap = arp_lookup(ARP_AX25,ipstuff.source,iface)) != NULLARP
  108.         && ap->timer.duration != 0) || ap == NULLARP){
  109.             ap = arp_add(ipstuff.source,ARP_AX25,source,0,iface);
  110.         }
  111.     }
  112.     return;
  113. }
  114.   
  115. /* Inspect the AX.25 header to determine if it is legal.  If it has anything
  116.     wrong with it, dump this packet...
  117. */
  118. #define ishash(x)   ((char)(x) == '#' ? 1 : 0)
  119. #define isaspace(x) ((char)(x) == ' ' ? 1 : 0)
  120.   
  121. int ax25_check_corruption(header)
  122. struct ax25 *header;
  123. {
  124.     int loop;
  125.   
  126.     /* The header structure contains two fixed-length and one variable-length
  127.         char arrays, containing the ax25 callsign/ssid pairs for the source,
  128.         destination, and (optional) digipeaters.
  129.   
  130.         The integer "ndigis" says how many digis are in the path, upto the
  131.         constant MAXDIGIS.
  132.   
  133.         Because the arrays are not null-terminated, we can just walk through
  134.         the char arrays looking for any illegal characters to determine if
  135.         the header is corrupt.  We'll rely on the higher-level protocols to
  136.         determine if the data in the packet is corrupt...
  137.   
  138.         We return 0 if the packet is possibly good, or 1 if it is definitely
  139.         corrupt.
  140.     */
  141.   
  142.     if (header->ndigis > MAXDIGIS || header->ndigis < 0){
  143.         return 1;
  144.     }
  145.   
  146.     if (ax25_legal(header->source)){
  147.         return 1;
  148.     }
  149.   
  150.     if (ax25_legal(header->dest)){
  151.         return 1;
  152.     }
  153.   
  154.     for (loop = 0; loop < header->ndigis; loop++)
  155.         if (ax25_legal(header->digis[loop])){
  156.             return 1;
  157.         }
  158.   
  159.     return 0;
  160.   
  161.   
  162. }
  163.   
  164. /* Check the callsign portions. */
  165. int ax25_legal(call_ssid)
  166. char *call_ssid;
  167. {
  168.     int loop;
  169.     unsigned char chr;
  170.   
  171.     for (loop = 0; loop < AXALEN-1; loop++){
  172.   
  173.         /* Put the ascii value of the char into chr */
  174.         chr = (*(call_ssid + loop));
  175.         chr = chr >> 1;
  176.   
  177.         /* Now see if it valid. */
  178.         if ((isupper(chr) | isdigit(chr) | ishash(chr) | isaspace(chr)) == 0)
  179.          /* A bad chr */
  180.             return 1;
  181.     }
  182.   
  183.     /* The header passed inspection, so return 0... */
  184.     return 0;
  185.   
  186. }
  187.   
  188. #endif /* AUTOROUTE */
  189. #endif /* AX25 */
  190.