generate_namfile.c 9.85 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
/*
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
Leigh Stoller's avatar
Leigh Stoller committed
22 23
 */

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#include "discvr.h"
#include "packet.h"
#include "util.h"
#include "generate_namfile.h"

u_char *mac_list[MAX_NODES];
int num_nodes=0;
db_type mac_db[40];

link_type *links_list; 

void
read_mac_db()
{
	FILE *dbfptr=NULL;
	char hostname[HOSTNAME_LEN];
	char if_addr[20];
	int if_num=0,i=0;
	if((dbfptr = fopen("MAC_DB","r")) == NULL)
	{
        perror("MAC_DB file not found\n");
		exit(1);
	}
	i=0;
    while(i<40)
	{
		if(fscanf(dbfptr,"%s\t%d\t%s\n",hostname,&if_num,if_addr)==0)
			break;
		//printf("values read: \"%s\" \"%d\" \"%s\"\n",hostname,if_num,if_addr);
		mac_db[i].hostname = (char*)malloc(sizeof(char)*strlen(hostname));
		strcpy(mac_db[i].hostname,hostname);
		strcpy(mac_db[i].addrs[if_num],if_addr);
		fscanf(dbfptr,"%s\t%d\t%s\n",hostname,&if_num,if_addr);
		strcpy(mac_db[i].addrs[if_num],if_addr);
		fscanf(dbfptr,"%s\t%d\t%s\n",hostname,&if_num,if_addr);
		strcpy(mac_db[i].addrs[if_num],if_addr);
		fscanf(dbfptr,"%s\t%d\t%s\n",hostname,&if_num,if_addr);
		strcpy(mac_db[i].addrs[if_num],if_addr);
		fscanf(dbfptr,"%s\t%d\t%s\n",hostname,&if_num,if_addr);
		strcpy(mac_db[i].addrs[if_num],if_addr);
		i++;
	}
	fclose(dbfptr);
}

void
get_hostname(char *addr,char *hostname)
{
	int i=0,j=0;

	for(i=0;i<40;i++)
	{
		for(j=0;j<5;j++)
		{
			if(strcmp(addr,mac_db[i].addrs[j])==0)
			{
				strcpy(hostname,mac_db[i].hostname);
				return;
			}
		}
	}
	printf("Error:Couldnt find the mac_address in the db\n");
	strcpy(hostname,"dontknow");
}


/* Will need to change this function. Change the data structure to have the link
 * as a hash key. Two nodes may have multiple links between them.
 */
void
gen_nam_file(const char *mesg, size_t nbytes,char *file_name)
{
	struct topd_nbor *p;
	FILE *fptr=NULL;
	char addr[MAC_ADDR_STR_LEN];
	char hostname[HOSTNAME_LEN];
	int lan_count=MAX_NODES;
	int node_count=0;
	link_type * temp_ptr;
	lan_node_type *temp_ptr2;
	/* Read the MAC address and Hostname mapping from the "MAC_DB" file into 
	 * mac_db array.
	 */
	read_mac_db();

	/* Open the output nam file for writing the topology informantion
	 */
	if((fptr = fopen(file_name,"w")) == NULL)
		perror("Error:fopen");
	fprintf(fptr,"V -t * -v 1.0a5 -a 0\n");

	p = (struct topd_nbor *) (mesg + sizeof(topd_inqid_t));
	num_nodes=0;
	/* Go through the list of links received by the client as reply to its query
	 * one by one.
	 */
	while((char *)p < mesg + nbytes ) 
	{
		add_link(p);
		print_links_list();
		/* Check that this node's node_id is not there in the
		 * mac_list already. If its already there in the list then don't add to the 
		 * list again. 
		 */
		if(not_in_node_list(p->tdnbor_pnode))
		{
			mac_list[num_nodes]=(u_char*)malloc(sizeof(u_char)*ETHADDRSIZ);
			memcpy(mac_list[num_nodes],p->tdnbor_pnode, ETHADDRSIZ);
			/* Get the MAC address as a string. Convert the MAC address in the
			 * hexadecimal format to string format
			 */
			get_mac_addr(p->tdnbor_pnode,ETHADDRSIZ, addr);
			/* Use the mac_db to convert the mac address to host name
			 */
			get_hostname(addr,hostname);
			fprintf(fptr, "n -t * -s %d -v circle -c black -e red -l %s\n",num_nodes, hostname);
			num_nodes++;
		}
		//ptr_link_struct = 
		p++;
	}
	
	/* Set the version number of nam file
	 */
	

	for(temp_ptr = links_list; temp_ptr!=NULL;temp_ptr = temp_ptr->next)
	{
		if(temp_ptr->lan_if_list==NULL)
			fprintf(fptr,"l -t * -s %d -d %d -c blue\n",
					get_id(temp_ptr->pointA),get_id(temp_ptr->pointB));
	}
	lan_count = num_nodes;
	for(temp_ptr = links_list; temp_ptr!=NULL;temp_ptr = temp_ptr->next)
	{
		if(temp_ptr->lan_if_list!=NULL)
		{
			fprintf(fptr,"X -t * -n %d -o left\n",lan_count);
			fprintf(fptr,"L -t * -s %d -d %d -o up\n",
					lan_count,get_id(temp_ptr->pointA));
			for(node_count=0,temp_ptr2=temp_ptr->lan_if_list;temp_ptr2!=NULL;
				temp_ptr2=temp_ptr2->next,node_count++)
			{
				fprintf(fptr,"L -t * -s %d -d %d ",
						lan_count,get_id(temp_ptr2->node));
				if(node_count%2==0)
					fprintf(fptr,"-o down\n");
				else
					fprintf(fptr,"-o up\n");
			}
			lan_count++;
		}
	}
	fprintf(fptr,"\n");
	fclose(fptr);
}

int
get_id(u_char *mac_addr)
{
	int i=0;
	char str_mac_addr[MAC_ADDR_STR_LEN],str_mac_addr2[MAC_ADDR_STR_LEN];
	char hostname[HOSTNAME_LEN], hostname2[HOSTNAME_LEN];

	get_mac_addr(mac_addr,ETHADDRSIZ, str_mac_addr);
	get_hostname(str_mac_addr,hostname);
	for(i=0;i<num_nodes;i++)
	{
		get_mac_addr(mac_list[i],ETHADDRSIZ, str_mac_addr2);
		get_hostname(str_mac_addr2,hostname2);

		if(strcmp(hostname,hostname2)==0)
			return i;
	}
	printf("Error: Could not find \"%s\" mac address in the list\n", mac_addr);
	return MAX_NODES;
}

int 
not_in_node_list(u_char *mac_addr)
{
	int i=0;
	for(i=0;i<num_nodes;i++)
	{
		if(memcmp(mac_addr,mac_list[i],ETHADDRSIZ)==0)
            return 0;
	}
	return 1;
}

214 215 216 217 218 219 220 221 222 223 224 225
/*  Add link function adds a link to the links_list data structure which is the
 *  list of all the links in the topology. 
 *   - It ignores the link if its a duplicate to any of the existing link
 *   - If one of the interface is common to two links then the links are part
 *     of a LAN. Since the query is forwarded on the LAN from a single interface
 *     of a node hence for a LAN there is just one common interface. This
 *     interface is seperately (pointA of link structure) for making the
 *     comparisions simple.
 *     The rest of the interfaces on the LAN is stored as a link list of nodes and
 *    pointed by the "lan_if_list" in the link structure.
 */

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

void 
add_link(struct topd_nbor *p)
{
	link_type * temp_ptr;
	lan_node_type *temp_ptr_lan;
	int w,x,y,z;

	u_char zero_mac[ETHADDRSIZ];
	bzero(zero_mac,ETHADDRSIZ);

	if(memcmp(zero_mac,p->tdnbor_dnode,ETHADDRSIZ)==0)
		return;
	printf("adding...\n");
	for(temp_ptr = links_list;temp_ptr!=NULL;temp_ptr = temp_ptr->next)
	{
		w = memcmp(temp_ptr->pointA,p->tdnbor_dnode,ETHADDRSIZ);
		x = memcmp(temp_ptr->pointA,p->tdnbor_dif,ETHADDRSIZ);

		if(temp_ptr->pointB != NULL)
		{
			y = memcmp(temp_ptr->pointB,p->tdnbor_dnode,ETHADDRSIZ);
			z = memcmp(temp_ptr->pointB,p->tdnbor_dif,ETHADDRSIZ);
		}
		else
		{
			y=1;z=1;
		}
		
		if((w==0 && z==0) || (x==0 && y==0))
		{
			printf("Link already there in the list\n");
			return;
		}
		else if((w==0 && z!=0) || (w!=0 && z==0) || (x==0 && y!=0) || (x!=0 && y==0))
		{
			printf("lan link found\n");
			if(temp_ptr->lan_if_list==NULL)
			{
				/* Store the common interface in pointA, copy interface in pointB
				 * to a lan_node and set pointB to NULL
				 */
				if(y==0 || z==0)
				{
					temp_ptr->lan_if_list =
						(lan_node_type*)malloc(sizeof(lan_node_type));
					memcpy(temp_ptr->lan_if_list->node,temp_ptr->pointA,ETHADDRSIZ);
					memcpy(temp_ptr->pointA,temp_ptr->pointB,ETHADDRSIZ);
					free(temp_ptr->pointB);
					temp_ptr->pointB=NULL;
				}
				else
				{
					temp_ptr->lan_if_list =
                        (lan_node_type*)malloc(sizeof(lan_node_type));
                    memcpy(temp_ptr->lan_if_list->node,temp_ptr->pointB,ETHADDRSIZ);
					free(temp_ptr->pointB);
					temp_ptr->pointB=NULL;
				}
				
			}
			temp_ptr_lan = temp_ptr->lan_if_list;
			temp_ptr->lan_if_list = (lan_node_type*)malloc(sizeof(lan_node_type));
			if(x==0 || z==0)
				memcpy(temp_ptr->lan_if_list->node,p->tdnbor_dnode,ETHADDRSIZ);
			else if(w==0 || y==0)
				memcpy(temp_ptr->lan_if_list->node,p->tdnbor_dif,ETHADDRSIZ);
			temp_ptr->lan_if_list->next = temp_ptr_lan;
			/*
			for(temp_ptr2=temp_ptr->lan_if_list;temp_ptr2!=NULL;
				temp_ptr2 = temp_ptr2->next);
			temp_ptr2=(lan_node_type*)malloc(sizeof(lan_node_type));
			memcpy(temp_ptr2->pointA,p->tdnbor_dnode,ETHADDRSIZ);
			memcpy(temp_ptr2->pointB,p->tdnbor_dif,ETHADDRSIZ);
			temp_ptr2->lan_if_list = NULL;
			temp_ptr2->next = NULL;
			*/
		return;
		}
	}
	temp_ptr = links_list;
	links_list=(link_type*)malloc(sizeof(link_type));
	links_list->pointA = (u_char*)malloc(ETHADDRSIZ*sizeof(u_char));
	links_list->pointB = (u_char*)malloc(ETHADDRSIZ*sizeof(u_char));
	printf("copying:A->");
	print_haddr(p->tdnbor_dnode,ETHADDRSIZ);
	printf(" # B->");
	print_haddr(p->tdnbor_dif,ETHADDRSIZ);
	printf("\n");
	memcpy(links_list->pointA,p->tdnbor_dnode,ETHADDRSIZ);
	memcpy(links_list->pointB,p->tdnbor_dif,ETHADDRSIZ);
	printf("copied:A->");
	print_haddr(links_list->pointA,ETHADDRSIZ);
	printf(" # B->");
	print_haddr(links_list->pointB,ETHADDRSIZ);
	printf("\n");
	links_list->lan_if_list = NULL;
	links_list->next = temp_ptr;
}

void
print_links_list()
{
	link_type *temp_ptr;
	lan_node_type *temp_ptr2;
	printf("Links collected:\n");
	printf("---------------------------------------------------\n");

	for(temp_ptr = links_list; temp_ptr!=NULL;temp_ptr = temp_ptr->next)
	{
		if(temp_ptr->lan_if_list==NULL)
		{
			printf("[");
			print_haddr(temp_ptr->pointA,ETHADDRSIZ);
			printf("<->");
			print_haddr(temp_ptr->pointB,ETHADDRSIZ);
			printf("]");
			printf("\n");
		}
		else
		{
			printf("[");
			print_haddr(temp_ptr->pointA,ETHADDRSIZ);
			printf(" # ");
			for(temp_ptr2=temp_ptr->lan_if_list;temp_ptr2->next!=NULL;
				temp_ptr2=temp_ptr2->next)
			{
				print_haddr(temp_ptr2->node,ETHADDRSIZ);
				printf(" # ");
			}
			print_haddr(temp_ptr2->node,ETHADDRSIZ);
			printf("]");
			printf("\n");
		}
	}
	printf("---------------------------------------------------\n");
}