util.c 4.4 KB
Newer Older
Kristin Wright's avatar
Kristin Wright committed
1
/* 
Mike Hibler's avatar
Mike Hibler committed
2
 * EMULAB-COPYRIGHT
Kristin Wright's avatar
Kristin Wright committed
3 4 5 6 7 8 9 10 11 12
 * Copyright (c) 2000 The University of Utah and the Flux Group.
 * All rights reserved.
 *
 * ---------------------------
 *
 * Filename: util.c
 *   -- Author: Kristin Wright <kwright@cs.utah.edu> 
 *
 * ---------------------------
 *
Mike Hibler's avatar
Mike Hibler committed
13
 * $Id: util.c,v 1.9 2004-06-17 18:17:01 mike Exp $
Kristin Wright's avatar
Kristin Wright committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 */

#include "discvr.h"
#include "packet.h"
#include "util.h"

void 
println_haddr(u_char *haddr, u_short hlen)
{
        int     i;
	u_char    *ptr;
	
	if ((i = hlen) > 0) {
		ptr = haddr;
		do {
29
		        printf("%s%x", (i == hlen) ? "  " : ":", *ptr++);
Kristin Wright's avatar
Kristin Wright committed
30 31
		} while (--i > 0);
	}
32
	printf("\n");
Kristin Wright's avatar
Kristin Wright committed
33 34
}

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
void
get_mac_addr(u_char *haddr, u_short hlen, char* addr)
{
    int     i;
	u_char    *ptr;
	char temp_str[10];
	int len=0;
	
	if ((i = hlen) > 0) {
		ptr = haddr;
		do {
		        //sprintf(temp_str,"%s%x", (i == hlen) ? "  " : ":", *ptr++);
		        sprintf(temp_str,"%1x%1x", (0xf0 & *ptr)>>4, (0x0f & *ptr));
				ptr++;
				strncpy(addr+len,temp_str,strlen(temp_str));
				len+=strlen(temp_str);
		} while (--i > 0);
	}
	sprintf(addr+len,"\0");
}


Kristin Wright's avatar
Kristin Wright committed
57 58 59 60 61 62 63 64 65
void 
print_haddr(u_char *haddr, u_short hlen)
{
        int     i;
	u_char    *ptr;
	
	if ((i = hlen) > 0) {
		ptr = haddr;
		do {
66
  		        printf("%s%x", (i == hlen) ? "" : ":", *ptr++);
Kristin Wright's avatar
Kristin Wright committed
67 68 69 70 71 72 73
		} while (--i > 0);
	}
}

/* 
 * Print a td reply packet. They are of the form
 * 
Kristin Wright's avatar
Kristin Wright committed
74 75 76 77
 * [Inquiry ID ]
 * [TTL, Factor]
 * [Path, Dest ] 
 * [Path, Dest ] 
Kristin Wright's avatar
Kristin Wright committed
78 79 80 81 82
 * [...]
 *
 * Inquiry IDs consist of a node ID and a 
 * timestamp. 
 * 
Kristin Wright's avatar
Kristin Wright committed
83 84 85
 * The TTL and Factor are both unsigned 16-bit
 * numbers derived from user parameters.
 * 
Kristin Wright's avatar
Kristin Wright committed
86 87 88 89 90
 * Paths and destinations consist
 * of <node ID, MAC address> pairs. Node IDs 
 * themselves are MAC addresses.
 */
void
Kristin Wright's avatar
Kristin Wright committed
91
print_tdreply(const char *mesg, size_t nbytes)
Kristin Wright's avatar
Kristin Wright committed
92
{
Kristin Wright's avatar
Kristin Wright committed
93
	struct topd_nbor *p; 
Kristin Wright's avatar
Kristin Wright committed
94 95
	
	print_tdinq(mesg);
Kristin Wright's avatar
Kristin Wright committed
96
	p = (struct topd_nbor *) (mesg + sizeof(topd_inqid_t));
Kristin Wright's avatar
Kristin Wright committed
97

Kristin Wright's avatar
Kristin Wright committed
98
	while( (char *)p < mesg + nbytes ) {
Kristin Wright's avatar
Kristin Wright committed
99

100 101
		printf("ROUTE\t\t\t\tDEST\n");
		printf("[");
Kristin Wright's avatar
Kristin Wright committed
102
		print_haddr(p->tdnbor_pnode, ETHADDRSIZ);
103
		printf("-");
Kristin Wright's avatar
Kristin Wright committed
104
		print_haddr(p->tdnbor_pif, ETHADDRSIZ);
105 106
		printf("] ");
		printf("[");	
Kristin Wright's avatar
Kristin Wright committed
107
		print_haddr(p->tdnbor_dnode, ETHADDRSIZ);
108
		printf("-");
Kristin Wright's avatar
Kristin Wright committed
109
		print_haddr(p->tdnbor_dif, ETHADDRSIZ);
110
		printf("]\n\n");
Kristin Wright's avatar
Kristin Wright committed
111 112 113

		p++;
	}
Kristin Wright's avatar
Kristin Wright committed
114 115
}

116 117 118 119 120 121 122 123 124
void
print_tdpairs(const char *mesg, size_t nbytes)
{
        struct topd_nbor *p;

        p = (struct topd_nbor *)mesg;

        while( (char *)p < mesg + nbytes ) {

125 126
                printf( "ROUTE\t\t\t\tDEST\n");
                printf( "[");
127
                print_haddr(p->tdnbor_pnode, ETHADDRSIZ);
128
                printf( "-");
129
                print_haddr(p->tdnbor_pif, ETHADDRSIZ);
130 131
                printf( "] ");
                printf("[");
132
                print_haddr(p->tdnbor_dnode, ETHADDRSIZ);
133
                printf("-");
134
                print_haddr(p->tdnbor_dif, ETHADDRSIZ);
135
                printf("]\n\n");
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

                p++;
        }
}

void
print_tdnbrlist(struct topd_nborlist * list)
{
	printf("Printing the neighbor list:\n");
	while(list!=NULL)
	{
		print_tdpairs(list->tdnbl_nbors,list->tdnbl_n * sizeof(struct topd_nbor));
		list = list->tdnbl_next;
	}
	printf("---\n");
}

void
print_tdifinbrs(struct ifi_info *ifihead)
{
	struct ifi_info      *ifi;
	ifi = ifihead;
	for(; ifi != NULL; ifi = ifi->ifi_next) {
	
	if (ifi->ifi_flags & !IFF_UP || 
		    ifi->ifi_flags & IFF_LOOPBACK || (strcmp(ifi->ifi_name,"fxp4")==0)) {
		        continue;
		}
	printf("The neighbor's list of interface \"%s\"==>\n",ifi->ifi_name);
	print_tdnbrlist(ifi->ifi_nbors);
	printf("------\n");

	}
}

Kristin Wright's avatar
Kristin Wright committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 * Print a td inquiry packet. They are of the form:
 * 
 * [Inquiry ID],
 * [Node ID or inquiring node]
 *
 * See description of Inquiry IDs and
 * Node IDs above.
 * 
 */
void
print_tdinq(const char *mesg)
{
        topd_inqid_t *tip = (topd_inqid_t *)mesg;

186
	printf("\nINQ:%u.%u TTL:%d FACTOR:%d HAS_LAN:%d NODE:",
Kristin Wright's avatar
Kristin Wright committed
187 188 189
		ntohl(tip->tdi_tv.tv_sec), 
		ntohl(tip->tdi_tv.tv_usec),
		ntohs(tip->tdi_ttl), 
190 191
		ntohs(tip->tdi_factor),
		ntohs(tip->lans_exist)); 
Kristin Wright's avatar
Kristin Wright committed
192
	print_nodeID(tip->tdi_nodeID);
193 194
	printf(" PARENT i/f:");
	print_nodeID(tip->tdi_p_nodeIF);
Kristin Wright's avatar
Kristin Wright committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
}

u_char *max_haddr(u_char *ha1, u_char *ha2)
{
        u_char *t1 = ha1;
	u_char *t2 = ha2;

	if (ha1 == 0) {
	        return ha2;
	} else if (ha2 == 0) {
	        return ha1; 
	}

        while (1) {
	        if (*t1 > *t2) {
	                return ha1;
		} else if (*t1 < *t2) {
		        return ha2;
		} 
		t1++; t2++;
	}

        /* -lkw */
	fprintf(stderr, "should never get here because mac addresses are unique.\n"); 
	exit(1);
}