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 >
Wrap
C/C++ Source or Header
|
1993-10-25
|
13KB
|
438 lines
/*
** This file contains the general functions for dealing
** with elements of the data structures.
*/
#include <stdio.h>
#include <string.h>
#include "types.h"
#include "mrhash.h"
#include "data_struct_proto.h"
#include "build_adj_proto.h"
/**********************************************
This function inserts new interface names
into an insertion sorted linked list. The sort
will create a list sorted largest to smallest.
*************************************************/
void insert_name( Ifc_list *name_list, char *name )
{
Interface *prev, *cur;
int i, compare;
Interface *new;
new = NULL;
prev = NULL;
cur = name_list->first;
for( i = 0; i <= name_list->number; i++ )
{
/* this entry belongs at the end of the list */
if( cur == NULL )
{
new = (Interface *)calloc( 1, sizeof( Interface ));
strcpy( new->name, name );
if( prev == NULL )
name_list->first = new;
else
prev->next = new;
new->next = NULL;
name_list->number++;
return;
}
compare = strcmp( name, cur->name );
/* this entry is already in the list */
if( compare == 0 )
return;
/* this entry belongs just before cur */
if( compare > 0 )
{
new = (Interface *)calloc( 1, sizeof( Interface ));
strcpy( new->name, name );
new->next = cur;
if( prev == NULL )
name_list->first = new;
else
prev->next = new;
name_list->number++;
return;
}
prev = cur;
cur = cur->next;
}
}
/**********************************************
This function inserts new interface names
into an insertion sorted linked list. The sort
will create a list sorted largest to smallest.
*************************************************/
void insert_ifc( Ifc_list *name_list, Interface *new )
{
Interface *prev, *cur;
int i, compare;
/*printf( "in insert_ifc name %s\n", new->name);*/
prev = NULL;
cur = name_list->first;
for( i = 0; i <= name_list->number; i++ )
{
/* this entry belongs at the end of the list */
if( cur == NULL )
{
if( prev == NULL )
name_list->first = new;
else
prev->next = new;
new->next = NULL;
name_list->number++;
return;
}
compare = strcmp( new->name, cur->name );
/* this entry belongs just before cur */
if( compare >= 0 )
{
new->next = cur;
if( prev == NULL )
name_list->first = new;
else
prev->next = new;
name_list->number++;
return;
}
prev = cur;
cur = cur->next;
}
}
/**********************************************
This function deletes interface names
from a sorted linked list. The items
are sorted largest to smallest.
*************************************************/
void delete_name( Ifc_list *name_list, char *name )
{
Interface *prev, *cur;
int i, compare;
prev = NULL;
cur = name_list->first;
for( i = 0; i <= name_list->number; i++ )
{
/* this entry is not in the list */
if( cur == NULL )
return;
compare = strcmp( name, cur->name );
/* this entry is already in the list */
if( compare == 0 )
{
if( prev == NULL )
name_list->first = cur->next;
else
prev->next = cur->next;
name_list->number--;
free( cur );
return;
}
/* this entry is not in the list */
if( compare > 0 )
return;
prev = cur;
cur = cur->next;
}
}
/**********************************************
This function finds an interface name
in a sorted linked list. The items
are sorted largest to smallest.
*************************************************/
Interface *find_name( Ifc_list *name_list, char *name )
{
Interface *cur;
int i, compare;
cur = name_list->first;
for( i = 0; i <= name_list->number; i++, cur = cur->next )
{
/* this entry is not in the list */
if( cur == NULL )
return NULL;
compare = strcmp( name, cur->name );
/*printf( "comparing %s and %s got result %d\n", name, cur->name, compare);*/
/* this entry is in the list */
if( compare == 0 )
return cur;
/* this entry is not in the list */
if( compare > 0 )
return NULL;
}
return NULL;
}
/**********************************************
This function finds an interface name that is connected to a
subnet in a sorted linked list. The items are sorted largest to
smallest.
*************************************************/
Interface *find_conn_subnet( Ifc_list *name_list, char *name )
{
Interface *cur;
int i, compare;
cur = name_list->first;
for( i = 0; i <= name_list->number; i++, cur = cur->next )
{
/* this entry is not in the list */
if( cur == NULL )
return NULL;
compare = strcmp( name, cur->name );
/*printf( "comparing %s and %s got result %d\n", name, cur->name, compare);*/
/* this entry is in the list */
if( compare == 0 && (cur->type & SUBNET) )
{
/*printf( "found %s to %s metric %ld threshold %ld\n", cur->name,
cur->to_name, cur->metric, cur->threshold);*/
return cur;
}
/* this entry is not in the list */
if( compare > 0 )
return NULL;
}
return NULL;
}
/*************************************************************************
** FUNCTION: compare_names()
**
** DESCRIPTION:
** This function compares the two names passed in by first
** finding the ip address portion of the names and then comparing
** them. If they are equal, it returns TRUE. Otherwise, it returns FALSE.
**
*************************************************************************/
int compare_names( NAME name1, NAME name2 )
{
NAME ip_name1, ip_name2;
find_ip_name( name1, ip_name1 );
find_ip_name( name2, ip_name2 );
if( strcmp( ip_name1, ip_name2 ))
return FALSE;
else
return TRUE;
}
/*************************************************************************
** FUNCTION: find_ip_name()
**
** DESCRIPTION:
** This function parses the full_name passed in and looks
** for an ip_address. If full_name is already only an
** ip_name then it just copies it to ip_name and
** returns FALSE. Otherwise, it sets ip_name to be the
** numerical representation of the ip_address and returns
** TRUE.
**
*************************************************************************/
int find_ip_name( NAME full_name, NAME ip_name )
{
int ip_length;
char *ip_start;
ip_start = strchr( full_name, '(' );
if( ip_start == NULL )
{
strcpy( ip_name, full_name );
return FALSE;
}
ip_start++;
ip_length = strcspn( ip_start, ")" );
strncpy( ip_name, ip_start, ip_length );
ip_name[ip_length] = '\0';
return TRUE;
}
/*************************************************************************
*
* Convert the printable string representation of an IP address into the
* u_long (network) format. Return 0xffffffff on error and 0x00000000 on
* success. This function also determines the appropriate mask for
* the number by the number of sections of the address.
*
************************************************************************/
ADDRESS inet_parse(char *name, ADDRESS *address, ADDRESS *mask)
{
int i, byte_num = 0;
u_int a[4];
NAME temp;
if( mask != NULL )
*mask = 0xffffffff;
*address = 0x00000000;
/*
** Try to read the address as if it had all four bytes set. If there
** were less than four bytes, the return value will indicate the
** number of bytes actually found in the address. The rest of the
** address will be padded with zeros and the mask will be set accordingly.
*/
find_ip_name( name, temp);
byte_num = sscanf( temp, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3] );
for(i=0 ; i< byte_num; i++)
((u_char *)address)[i] = a[i];
for( i = byte_num; i< 4; i++ )
{
((u_char *)address)[i] = 0x00;
if( mask != NULL )
((u_char *)mask)[i] = 0x00;
}
return (*address);
}
/************************************************************************
FUNCTION: find_actual_name()
DESCRIPTION:
This function finds the name that corresponds to ones in the
network description file and or subnet file. It returns a
pointer to the hash table entry for the passed in name. It
starts by assuming the given name is correct, then it tries
seeing if this is an ip name. If the name is not found either
of these ways, then an appropriate subnet is searched for.
**************************************************************************/
HENTRY_t *find_actual_name( Network *net, NAME name, Ifc_list *subnets )
{
HENTRY_t *ht_entry_p, *ht_entry_p1;
NAME temp_name;
Interface *temp_ifc;
if( !strcmp( name, "\0" ))
return NULL;
/*
** Check if a valid ip_name was specified and
** determine the corresponding full name.
*/
ht_entry_p1 = lookup_htable( &net->ip_map, name );
if( ht_entry_p1 == NULL )
ht_entry_p = lookup_htable( &net->name_map, name );
else
ht_entry_p = lookup_htable( &net->name_map, ht_entry_p1->name );
if( ht_entry_p != NULL )
return ht_entry_p;
/*
** The specified source is not a known router so, see if
** it is on a known subnet.
*/
find_ip_name( name, temp_name );
temp_ifc = find_subnet( temp_name, subnets );
if( temp_ifc == NULL )
{
printf( "\nINTERFACE %s UNKNOWN\n", name );
return NULL;
}
else
{/*
** Consider the requested node to be a subnet
*/
ht_entry_p = lookup_htable( &net->name_map, temp_ifc->name );
}
return ht_entry_p;
}
/*********************************************************************
*
* Convert an IP number in u_long (network) format into a printable
* string using the mask provided.
*
********************************************************************/
char *inet_fmt(ADDRESS addr, ADDRESS mask, char *name)
{
register u_char *a_arry, *m_arry;
a_arry = (u_char *)&addr;
m_arry = (u_char *)&mask;
if (m_arry[3] != 0) sprintf(name, "%u.%u.%u.%u", a_arry[0], a_arry[1], a_arry[2], a_arry[3]);
else if (m_arry[2] != 0) sprintf(name, "%u.%u.%u", a_arry[0], a_arry[1], a_arry[2]);
else if (m_arry[1] != 0) sprintf(name, "%u.%u", a_arry[0], a_arry[1]);
else sprintf(name, "%u", a_arry[0]);
return (name);
}
/******************************************************************
FUNCTION: find_ifc( net, frm_name, to_name )
DESCRIPTION:
Find the interface in the network that goes from frm_name
to to_name and return a pointer to it. The search starts with the
interface passed in unless NULL is passed in.
*******************************************************************/
Interface *find_ifc( Network *net, Interface *ifc_p, NAME frm_name, NAME to_name)
{
long index;
HENTRY_t *ht_entry_p;
ht_entry_p = lookup_htable( &net->name_map, frm_name );
if( ht_entry_p == NULL )
return (Interface *)NULL;
index = ht_entry_p->index;
if( ifc_p == NULL )
ifc_p = net->adj_list[index].interfaces.first;
for(; ifc_p != NULL; ifc_p = ifc_p->next )
{
if( !strcmp( ifc_p->name, frm_name ) && !strcmp( ifc_p->to_name, to_name ))
{/*
** This is the right interface
*/
return ifc_p;
}
}
/*printf( "Unable to find interface %s %s\n", frm_name, to_name );*/
return (Interface *)NULL;
}