home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / mrdebug.tar.Z / mrdebug.tar / data_struct.c < prev    next >
C/C++ Source or Header  |  1993-10-25  |  13KB  |  438 lines

  1. /*
  2. **          This file contains the general functions for dealing
  3. **          with elements of the data structures.
  4. */
  5. #include    <stdio.h>
  6. #include    <string.h>
  7.  
  8. #include       "types.h"
  9. #include       "mrhash.h"
  10. #include       "data_struct_proto.h"
  11. #include       "build_adj_proto.h"
  12.  
  13. /**********************************************
  14.  
  15.     This function inserts new interface names
  16.     into an insertion sorted linked list.  The sort
  17.     will create a list sorted largest to smallest.
  18.  
  19. *************************************************/
  20.  
  21. void  insert_name( Ifc_list *name_list, char *name )
  22. {
  23.     Interface *prev, *cur;
  24.     int        i, compare;
  25.     Interface *new;
  26.     
  27.     new  = NULL;
  28.     prev = NULL;
  29.     cur  = name_list->first;
  30.     for( i = 0; i <= name_list->number; i++ )
  31.         {
  32.         /*  this entry belongs at the end of the list */
  33.         if( cur == NULL )
  34.             {
  35.             new = (Interface *)calloc( 1, sizeof( Interface ));
  36.             strcpy( new->name, name );
  37.             if( prev == NULL )
  38.                 name_list->first = new;
  39.             else
  40.                 prev->next = new;
  41.             new->next = NULL;
  42.                        name_list->number++;
  43.             return;
  44.             }
  45.  
  46.         compare = strcmp( name, cur->name );
  47.  
  48.         /*  this entry is already in the list */
  49.         if( compare == 0 )
  50.             return;
  51.         
  52.         
  53.         /*  this entry belongs just before cur */
  54.         if( compare > 0 )
  55.             {
  56.             new = (Interface *)calloc( 1, sizeof( Interface ));
  57.                         strcpy( new->name, name );
  58.             new->next = cur;
  59.             if( prev == NULL )
  60.                                 name_list->first = new;
  61.                         else
  62.                                 prev->next = new;
  63.             name_list->number++;
  64.                         return;
  65.                         }
  66.         prev = cur;
  67.         cur = cur->next;
  68.         }
  69. }
  70.  
  71. /**********************************************
  72.  
  73.     This function inserts new interface names
  74.     into an insertion sorted linked list.  The sort
  75.     will create a list sorted largest to smallest.
  76.  
  77. *************************************************/
  78.  
  79. void       insert_ifc( Ifc_list *name_list, Interface *new )
  80. {
  81.     Interface *prev, *cur;
  82.     int        i, compare;
  83.     
  84.     /*printf( "in insert_ifc name %s\n", new->name);*/
  85.     prev = NULL;
  86.     cur  = name_list->first;
  87.     for( i = 0; i <= name_list->number; i++ )
  88.         {
  89.         /*  this entry belongs at the end of the list */
  90.         if( cur == NULL )
  91.             {
  92.             if( prev == NULL )
  93.                 name_list->first = new;
  94.             else
  95.                 prev->next = new;
  96.             new->next = NULL;
  97.                        name_list->number++;
  98.             return;
  99.             }
  100.  
  101.         compare = strcmp( new->name, cur->name );
  102.  
  103.         /*  this entry belongs just before cur */
  104.         if( compare >= 0 )
  105.             {
  106.             new->next = cur;
  107.             if( prev == NULL )
  108.                                 name_list->first = new;
  109.                         else
  110.                                 prev->next = new;
  111.             name_list->number++;
  112.                         return;
  113.                         }
  114.         prev = cur;
  115.         cur = cur->next;
  116.         }
  117. }
  118.  
  119. /**********************************************
  120.  
  121.         This function deletes interface names
  122.         from a sorted linked list.  The items
  123.         are sorted largest to smallest.
  124.  
  125. *************************************************/
  126.  
  127. void  delete_name( Ifc_list *name_list, char *name )
  128. {
  129.         Interface *prev, *cur;
  130.         int        i, compare;
  131.  
  132.         prev = NULL;
  133.         cur  = name_list->first;
  134.         for( i = 0; i <= name_list->number; i++ )
  135.                 {
  136.                 /*  this entry is not in the list */
  137.                 if( cur == NULL )
  138.                         return;
  139.  
  140.                 compare = strcmp( name, cur->name );
  141.  
  142.                 /*  this entry is already in the list */
  143.                 if( compare == 0 )
  144.             {
  145.             if( prev == NULL )
  146.                 name_list->first = cur->next;
  147.             else
  148.                 prev->next = cur->next;
  149.             name_list->number--;
  150.             free( cur );
  151.                         return;
  152.             }
  153.  
  154.  
  155.                 /*  this entry is not in the list */
  156.                 if( compare > 0 )
  157.                         return;
  158.                 prev = cur;
  159.                 cur = cur->next;
  160.                 }
  161. }
  162.  
  163. /**********************************************
  164.  
  165.         This function finds an interface name
  166.         in a sorted linked list.  The items
  167.         are sorted largest to smallest.
  168.  
  169. *************************************************/
  170.  
  171. Interface  *find_name( Ifc_list *name_list, char *name )
  172. {
  173.         Interface *cur;
  174.         int        i, compare;
  175.  
  176.         cur  = name_list->first;
  177.         for( i = 0; i <= name_list->number; i++, cur = cur->next )
  178.                 {
  179.                 /*  this entry is not in the list */
  180.                 if( cur == NULL )
  181.                         return NULL;
  182.  
  183.                 compare = strcmp( name, cur->name );
  184.  
  185.         /*printf( "comparing %s and %s got result %d\n", name, cur->name, compare);*/
  186.  
  187.                 /*  this entry is in the list */
  188.                 if( compare == 0 )
  189.                         return cur;
  190.  
  191.  
  192.                 /*  this entry is not in the list */
  193.                 if( compare > 0 )
  194.                         return NULL;
  195.                 }
  196.     return NULL;
  197. }
  198.  
  199. /**********************************************
  200.  
  201.         This function finds an interface name that is connected to a
  202.         subnet in a sorted linked list.  The items are sorted largest to
  203.         smallest.
  204.  
  205. *************************************************/
  206.  
  207. Interface  *find_conn_subnet( Ifc_list *name_list, char *name )
  208. {
  209.         Interface *cur;
  210.         int        i, compare;
  211.  
  212.         cur  = name_list->first;
  213.         for( i = 0; i <= name_list->number; i++, cur = cur->next )
  214.                 {
  215.                 /*  this entry is not in the list */
  216.                 if( cur == NULL )
  217.                         return NULL;
  218.  
  219.                 compare = strcmp( name, cur->name );
  220.  
  221.         /*printf( "comparing %s and %s got result %d\n", name, cur->name, compare);*/
  222.  
  223.                 /*  this entry is in the list */
  224.                 if( compare == 0 && (cur->type & SUBNET) )
  225.            {
  226.            /*printf( "found %s to %s metric %ld threshold %ld\n", cur->name,
  227.                   cur->to_name, cur->metric, cur->threshold);*/
  228.               return cur;
  229.            }
  230.  
  231.                 /*  this entry is not in the list */
  232.                 if( compare > 0 )
  233.                         return NULL;
  234.                 }
  235.     return NULL;
  236. }
  237.  
  238. /*************************************************************************
  239. **      FUNCTION: compare_names()
  240. **
  241. **      DESCRIPTION:
  242. **           This function compares the two names passed in by first
  243. **           finding the ip address portion of the names and then comparing
  244. **           them.  If they are equal, it returns TRUE.  Otherwise, it returns FALSE.
  245. **
  246. *************************************************************************/
  247. int     compare_names( NAME name1, NAME name2 )
  248. {
  249.    NAME  ip_name1, ip_name2;
  250.  
  251.    find_ip_name( name1, ip_name1 );
  252.    find_ip_name( name2, ip_name2 );
  253.    if( strcmp( ip_name1, ip_name2 ))
  254.       return FALSE;
  255.    else
  256.       return TRUE;
  257. }
  258.    
  259.  
  260. /*************************************************************************
  261. **      FUNCTION: find_ip_name()
  262. **
  263. **      DESCRIPTION:
  264. **           This function parses the full_name passed in and looks
  265. **           for an ip_address.  If full_name is already only an
  266. **           ip_name then it just copies it to ip_name and
  267. **           returns FALSE.  Otherwise, it sets ip_name to be the
  268. **           numerical representation of the ip_address and returns
  269. **           TRUE.
  270. **
  271. *************************************************************************/
  272. int     find_ip_name( NAME full_name, NAME ip_name )
  273. {
  274.    int ip_length;
  275.    char *ip_start;
  276.  
  277.    ip_start = strchr( full_name, '(' );
  278.    if( ip_start == NULL )
  279.       {
  280.       strcpy( ip_name, full_name );
  281.       return FALSE;
  282.       }
  283.    ip_start++;
  284.    ip_length = strcspn( ip_start, ")" );
  285.    strncpy( ip_name, ip_start, ip_length );
  286.    ip_name[ip_length] = '\0';
  287.    return TRUE;
  288. }
  289.    
  290.  
  291. /*************************************************************************
  292.  *
  293.  * Convert the printable string representation of an IP address into the
  294.  * u_long (network) format.  Return 0xffffffff on error and 0x00000000 on
  295.  * success. This function also determines the appropriate mask for
  296.  * the number by the number of sections of the address.
  297.  *
  298.  ************************************************************************/
  299. ADDRESS inet_parse(char *name, ADDRESS *address, ADDRESS *mask)
  300. {
  301.     int   i, byte_num = 0;
  302.     u_int a[4];
  303.     NAME  temp;
  304.  
  305.     if( mask != NULL )
  306.        *mask    = 0xffffffff;
  307.     *address = 0x00000000;
  308.     /*
  309.     **    Try to read the address as if it had all four bytes set.  If there
  310.     **    were less than four bytes, the return value will indicate the
  311.     **    number of bytes actually found in the address.  The rest of the
  312.     **    address will be padded with zeros and the mask will be set accordingly.
  313.     */
  314.     find_ip_name( name, temp);
  315.     byte_num = sscanf( temp, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3] );
  316.     for(i=0 ; i< byte_num; i++)
  317.        ((u_char *)address)[i] = a[i];
  318.     for( i = byte_num; i< 4; i++ ) 
  319.        {
  320.        ((u_char *)address)[i] = 0x00;
  321.        if( mask != NULL )
  322.       ((u_char *)mask)[i] = 0x00;
  323.        }
  324.     
  325.     return (*address);
  326. }
  327.  
  328. /************************************************************************
  329.  
  330.   FUNCTION: find_actual_name()
  331.  
  332.   DESCRIPTION:
  333.         This function finds the name that corresponds to ones in the
  334.     network description file and or subnet file.  It returns a
  335.     pointer to the hash table entry for the passed in name.  It
  336.     starts by assuming the given name is correct, then it tries
  337.     seeing if this is an ip name.  If the name is not found either
  338.     of these ways, then an appropriate subnet is searched for.
  339.  
  340. **************************************************************************/
  341. HENTRY_t   *find_actual_name( Network *net, NAME name, Ifc_list *subnets )
  342. {
  343.    HENTRY_t   *ht_entry_p, *ht_entry_p1;
  344.    NAME        temp_name;
  345.    Interface  *temp_ifc;
  346.    
  347.    if( !strcmp( name, "\0" ))
  348.       return NULL;
  349.    /*
  350.    **     Check if a valid ip_name was specified and
  351.    **     determine the corresponding full name.
  352.    */
  353.    ht_entry_p1 = lookup_htable( &net->ip_map, name );
  354.    if( ht_entry_p1 == NULL )
  355.       ht_entry_p = lookup_htable( &net->name_map, name );
  356.    else
  357.       ht_entry_p = lookup_htable( &net->name_map, ht_entry_p1->name );
  358.    if( ht_entry_p != NULL )
  359.       return ht_entry_p;
  360.    /*
  361.    **     The specified source is not a known router so, see if
  362.    **     it is on a known subnet.
  363.    */
  364.    find_ip_name( name, temp_name );
  365.    temp_ifc = find_subnet( temp_name, subnets );
  366.    if( temp_ifc == NULL )
  367.       {
  368.       printf( "\nINTERFACE %s UNKNOWN\n", name );
  369.       return NULL;
  370.       }
  371.    else
  372.       {/*
  373.        **     Consider the requested node to be a subnet
  374.        */
  375.       ht_entry_p = lookup_htable( &net->name_map, temp_ifc->name );
  376.       }
  377.    return ht_entry_p;
  378. }
  379.  
  380. /*********************************************************************
  381.  *
  382.  * Convert an IP number in u_long (network) format into a printable
  383.  * string using the mask provided.
  384.  *
  385.  ********************************************************************/
  386. char *inet_fmt(ADDRESS addr, ADDRESS mask, char *name)
  387. {
  388.     register u_char *a_arry, *m_arry;
  389.  
  390.     a_arry = (u_char *)&addr;
  391.     m_arry = (u_char *)&mask;
  392.  
  393.     if      (m_arry[3] != 0) sprintf(name, "%u.%u.%u.%u", a_arry[0], a_arry[1], a_arry[2], a_arry[3]);
  394.     else if (m_arry[2] != 0) sprintf(name, "%u.%u.%u",    a_arry[0], a_arry[1], a_arry[2]);
  395.     else if (m_arry[1] != 0) sprintf(name, "%u.%u",       a_arry[0], a_arry[1]);
  396.     else                sprintf(name, "%u",          a_arry[0]);
  397.  
  398.     return (name);
  399. }
  400.  
  401. /******************************************************************
  402.  
  403.   FUNCTION: find_ifc( net, frm_name, to_name )
  404.  
  405.  
  406.   DESCRIPTION:
  407.       Find the interface in the network that goes from frm_name
  408.       to to_name and return a pointer to it.  The search starts with the
  409.       interface passed in unless NULL is passed in.
  410.  
  411. *******************************************************************/
  412.  
  413. Interface   *find_ifc( Network *net, Interface *ifc_p, NAME frm_name, NAME to_name)
  414. {
  415.    long       index;
  416.    HENTRY_t  *ht_entry_p;
  417.  
  418.  
  419.    ht_entry_p = lookup_htable( &net->name_map, frm_name );
  420.    if( ht_entry_p == NULL )
  421.       return (Interface *)NULL;
  422.    index = ht_entry_p->index;
  423.    if( ifc_p == NULL )
  424.       ifc_p = net->adj_list[index].interfaces.first;
  425.    for(; ifc_p != NULL; ifc_p = ifc_p->next )
  426.       {
  427.       if( !strcmp( ifc_p->name, frm_name ) && !strcmp( ifc_p->to_name, to_name ))
  428.      {/*
  429.       **      This is the right interface
  430.       */
  431.            return ifc_p;
  432.          }
  433.       }
  434.    /*printf( "Unable to find interface %s %s\n", frm_name, to_name  );*/
  435.    return (Interface *)NULL;
  436. }           
  437.  
  438.