util.c 3.15 KB
Newer Older
Kristin Wright's avatar
Kristin Wright committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
 * Copyright (c) 2000 The University of Utah and the Flux Group.
 * All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software is hereby
 * granted provided that (1) source code retains these copyright, permission,
 * and disclaimer notices, and (2) redistributions including binaries
 * reproduce the notices in supporting documentation.
 *
 * THE UNIVERSITY OF UTAH ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  THE UNIVERSITY OF UTAH DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * ---------------------------
 *
 * Filename: util.c
 *   -- Author: Kristin Wright <kwright@cs.utah.edu> 
 *
 * ---------------------------
 *
Kristin Wright's avatar
Kristin Wright committed
21
 * $Id: util.c,v 1.2 2000-07-13 18:52:52 kwright Exp $
Kristin Wright's avatar
Kristin Wright 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
 */

#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 {
		        fprintf(stderr, "%s%x", (i == hlen) ? "  " : ":", *ptr++);
		} while (--i > 0);
	}
	fprintf(stderr, "\n");
}

void 
print_haddr(u_char *haddr, u_short hlen)
{
        int     i;
	u_char    *ptr;
	
	if ((i = hlen) > 0) {
		ptr = haddr;
		do {
Kristin Wright's avatar
Kristin Wright committed
52
  		        fprintf(stderr, "%s%x", (i == hlen) ? "" : ":", *ptr++);
Kristin Wright's avatar
Kristin Wright committed
53 54 55 56 57 58 59
		} while (--i > 0);
	}
}

/* 
 * Print a td reply packet. They are of the form
 * 
Kristin Wright's avatar
Kristin Wright committed
60 61 62 63
 * [Inquiry ID ]
 * [TTL, Factor]
 * [Path, Dest ] 
 * [Path, Dest ] 
Kristin Wright's avatar
Kristin Wright committed
64 65 66 67 68
 * [...]
 *
 * Inquiry IDs consist of a node ID and a 
 * timestamp. 
 * 
Kristin Wright's avatar
Kristin Wright committed
69 70 71
 * The TTL and Factor are both unsigned 16-bit
 * numbers derived from user parameters.
 * 
Kristin Wright's avatar
Kristin Wright committed
72 73 74 75 76
 * Paths and destinations consist
 * of <node ID, MAC address> pairs. Node IDs 
 * themselves are MAC addresses.
 */
void
Kristin Wright's avatar
Kristin Wright committed
77
print_tdreply(const char *mesg, size_t nbytes)
Kristin Wright's avatar
Kristin Wright committed
78
{
Kristin Wright's avatar
Kristin Wright committed
79
	struct topd_nbor *p; 
Kristin Wright's avatar
Kristin Wright committed
80 81
	
	print_tdinq(mesg);
Kristin Wright's avatar
Kristin Wright committed
82
	p = (struct topd_nbor *) (mesg + sizeof(topd_inqid_t));
Kristin Wright's avatar
Kristin Wright committed
83

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

Kristin Wright's avatar
Kristin Wright committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
		fprintf(stderr, "ROUTE\t\t\t\tDEST\n");
		fprintf(stderr, "[");
		print_haddr(p->tdnbor_pnode, ETHADDRSIZ);
		fprintf(stderr, "-");
		print_haddr(p->tdnbor_pif, ETHADDRSIZ);
		fprintf(stderr, "] ");
		fprintf(stderr, "[");	
		print_haddr(p->tdnbor_dnode, ETHADDRSIZ);
		fprintf(stderr, "-");
		print_haddr(p->tdnbor_dif, ETHADDRSIZ);
		fprintf(stderr, "]\n\n");

		p++;
	}
Kristin Wright's avatar
Kristin Wright committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}

/*
 * 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;

Kristin Wright's avatar
Kristin Wright committed
117 118 119 120 121
	fprintf(stderr, "\nINQ:%u.%u TTL:%d FACTOR:%d NODE:",
		ntohl(tip->tdi_tv.tv_sec), 
		ntohl(tip->tdi_tv.tv_usec),
		ntohs(tip->tdi_ttl), 
		ntohs(tip->tdi_factor)); 
Kristin Wright's avatar
Kristin Wright committed
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
	print_nodeID(tip->tdi_nodeID);
}

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