util.c 5.11 KB
Newer Older
Kristin Wright's avatar
Kristin Wright committed
1 2
/* 
 * Copyright (c) 2000 The 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/>.
 * 
 * }}}
Kristin Wright's avatar
Kristin Wright committed
22 23 24 25 26 27 28 29
 *
 * ---------------------------
 *
 * Filename: util.c
 *   -- Author: Kristin Wright <kwright@cs.utah.edu> 
 *
 * ---------------------------
 *
Mike Hibler's avatar
Mike Hibler committed
30
 * $Id: util.c,v 1.9 2004-06-17 18:17:01 mike Exp $
Kristin Wright's avatar
Kristin Wright committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

#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 {
46
		        printf("%s%x", (i == hlen) ? "  " : ":", *ptr++);
Kristin Wright's avatar
Kristin Wright committed
47 48
		} while (--i > 0);
	}
49
	printf("\n");
Kristin Wright's avatar
Kristin Wright committed
50 51
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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
74 75 76 77 78 79 80 81 82
void 
print_haddr(u_char *haddr, u_short hlen)
{
        int     i;
	u_char    *ptr;
	
	if ((i = hlen) > 0) {
		ptr = haddr;
		do {
83
  		        printf("%s%x", (i == hlen) ? "" : ":", *ptr++);
Kristin Wright's avatar
Kristin Wright committed
84 85 86 87 88 89 90
		} while (--i > 0);
	}
}

/* 
 * Print a td reply packet. They are of the form
 * 
Kristin Wright's avatar
Kristin Wright committed
91 92 93 94
 * [Inquiry ID ]
 * [TTL, Factor]
 * [Path, Dest ] 
 * [Path, Dest ] 
Kristin Wright's avatar
Kristin Wright committed
95 96 97 98 99
 * [...]
 *
 * Inquiry IDs consist of a node ID and a 
 * timestamp. 
 * 
Kristin Wright's avatar
Kristin Wright committed
100 101 102
 * The TTL and Factor are both unsigned 16-bit
 * numbers derived from user parameters.
 * 
Kristin Wright's avatar
Kristin Wright committed
103 104 105 106 107
 * Paths and destinations consist
 * of <node ID, MAC address> pairs. Node IDs 
 * themselves are MAC addresses.
 */
void
Kristin Wright's avatar
Kristin Wright committed
108
print_tdreply(const char *mesg, size_t nbytes)
Kristin Wright's avatar
Kristin Wright committed
109
{
Kristin Wright's avatar
Kristin Wright committed
110
	struct topd_nbor *p; 
Kristin Wright's avatar
Kristin Wright committed
111 112
	
	print_tdinq(mesg);
Kristin Wright's avatar
Kristin Wright committed
113
	p = (struct topd_nbor *) (mesg + sizeof(topd_inqid_t));
Kristin Wright's avatar
Kristin Wright committed
114

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

117 118
		printf("ROUTE\t\t\t\tDEST\n");
		printf("[");
Kristin Wright's avatar
Kristin Wright committed
119
		print_haddr(p->tdnbor_pnode, ETHADDRSIZ);
120
		printf("-");
Kristin Wright's avatar
Kristin Wright committed
121
		print_haddr(p->tdnbor_pif, ETHADDRSIZ);
122 123
		printf("] ");
		printf("[");	
Kristin Wright's avatar
Kristin Wright committed
124
		print_haddr(p->tdnbor_dnode, ETHADDRSIZ);
125
		printf("-");
Kristin Wright's avatar
Kristin Wright committed
126
		print_haddr(p->tdnbor_dif, ETHADDRSIZ);
127
		printf("]\n\n");
Kristin Wright's avatar
Kristin Wright committed
128 129 130

		p++;
	}
Kristin Wright's avatar
Kristin Wright committed
131 132
}

133 134 135 136 137 138 139 140 141
void
print_tdpairs(const char *mesg, size_t nbytes)
{
        struct topd_nbor *p;

        p = (struct topd_nbor *)mesg;

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

142 143
                printf( "ROUTE\t\t\t\tDEST\n");
                printf( "[");
144
                print_haddr(p->tdnbor_pnode, ETHADDRSIZ);
145
                printf( "-");
146
                print_haddr(p->tdnbor_pif, ETHADDRSIZ);
147 148
                printf( "] ");
                printf("[");
149
                print_haddr(p->tdnbor_dnode, ETHADDRSIZ);
150
                printf("-");
151
                print_haddr(p->tdnbor_dif, ETHADDRSIZ);
152
                printf("]\n\n");
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

                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
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/*
 * 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;

203
	printf("\nINQ:%u.%u TTL:%d FACTOR:%d HAS_LAN:%d NODE:",
Kristin Wright's avatar
Kristin Wright committed
204 205 206
		ntohl(tip->tdi_tv.tv_sec), 
		ntohl(tip->tdi_tv.tv_usec),
		ntohs(tip->tdi_ttl), 
207 208
		ntohs(tip->tdi_factor),
		ntohs(tip->lans_exist)); 
Kristin Wright's avatar
Kristin Wright committed
209
	print_nodeID(tip->tdi_nodeID);
210 211
	printf(" PARENT i/f:");
	print_nodeID(tip->tdi_p_nodeIF);
Kristin Wright's avatar
Kristin Wright committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

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);
}