cli.c 3.88 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: cli.c
 *   -- Author: Kristin Wright <kwright@cs.utah.edu> 
 *
 * ---------------------------
 *
Mike Hibler's avatar
Mike Hibler committed
13
 * $Id: cli.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
 */

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

20 21 22 23
extern u_char *mac_list[MAX_NODES];
extern int num_nodes;


Kristin Wright's avatar
Kristin Wright committed
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
u_char *
find_nodeID(void)
{
        int                     i;
	struct sockaddr         *sa;
	char                    *ptr;
	u_char                  *myNodeIDtmp = 0; 
  	struct ifi_info         *ifi, *ifihead;

	/* 
	 * Get interface info for all inet4 interfaces 
	 * and don't return aliases. 
	 */
	for (ifihead = ifi = get_ifi_info(AF_INET, 0); 
		 ifi != NULL; ifi = ifi->ifi_next) {

		printf("%s: <", ifi->ifi_name);
		if (ifi->ifi_flags & IFF_UP)		printf("UP ");
		if (ifi->ifi_flags & IFF_BROADCAST)	printf("BCAST ");
		if (ifi->ifi_flags & IFF_MULTICAST)	printf("MCAST ");
		if (ifi->ifi_flags & IFF_LOOPBACK)	printf("LOOP ");
		if (ifi->ifi_flags & IFF_POINTOPOINT)	printf("P2P ");
		printf(">\n");

		if ( (i = ifi->ifi_hlen) > 0) {
			ptr = ifi->ifi_haddr;
			do {
				printf("%s%x", (i == ifi->ifi_hlen) ? "  " : ":", *ptr++);
			} while (--i > 0);
			
		}

		/* 
		 * We update myNodeIDtmp in block separate from above 
		 * since the above is just a debug clause and may be
		 * compiled out eventually. -lkw
		 */
61
		 
Kristin Wright's avatar
Kristin Wright committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		if ( ifi->ifi_hlen > 0) {
		        myNodeIDtmp = max_haddr(myNodeIDtmp, ifi->ifi_haddr);
		}

		if ( (sa = ifi->ifi_addr) != NULL)
			printf("  IP addr: %s\n", sock_ntop(sa, sa->sa_len));
		if ( (sa = ifi->ifi_brdaddr) != NULL)
			printf("  broadcast addr: %s\n", sock_ntop(sa, sa->sa_len));
		if ( (sa = ifi->ifi_dstaddr) != NULL)
			printf("  destination addr: %s\n", sock_ntop(sa, sa->sa_len));
	}

        fprintf(stderr, "My node id:");
	print_nodeID(myNodeIDtmp);

	return myNodeIDtmp;
}                            

void
81
make_inquiry(topd_inqid_t *tip, u_int16_t ttl, u_int16_t factor, int lans_exist) 
Kristin Wright's avatar
Kristin Wright committed
82
{
83
    struct timeval tv;
Kristin Wright's avatar
Kristin Wright committed
84 85 86 87 88 89 90 91
	u_char         *nid;

	/* First goes the the time of day... */
	if (gettimeofday(&tv, NULL) == -1) {
	        perror("Unable to get time-of-day.");
		exit(1);
	}

Kristin Wright's avatar
Kristin Wright committed
92
	tip->tdi_tv.tv_sec  = htonl(tv.tv_sec);
Kristin Wright's avatar
Kristin Wright committed
93 94
	tip->tdi_tv.tv_usec = htonl(tv.tv_usec);

Kristin Wright's avatar
Kristin Wright committed
95 96 97 98
	/* ...then the ttl and factor... */
	tip->tdi_ttl     = htons(ttl);
	tip->tdi_factor  = htons(factor);

Kristin Wright's avatar
Kristin Wright committed
99 100 101
	/* ...and now our nodeID */
	nid = find_nodeID();
	memcpy((void *)tip->tdi_nodeID, nid, ETHADDRSIZ);
102
	bzero(tip->tdi_p_nodeIF,ETHADDRSIZ);
103
	tip->lans_exist = htons(lans_exist);
Kristin Wright's avatar
Kristin Wright committed
104 105 106
}

void
Kristin Wright's avatar
Kristin Wright committed
107
cli(int sockfd, const struct sockaddr *pservaddr, socklen_t servlen, 
108
    u_int16_t ttl, u_int16_t factor, int lans_exist)
Kristin Wright's avatar
Kristin Wright committed
109 110 111 112 113
{
        u_int32_t         n;
	char              recvline[MAXLINE + 1];
	topd_inqid_t      ti;
	
114
	make_inquiry(&ti, ttl, factor, lans_exist);
Kristin Wright's avatar
Kristin Wright committed
115

116
	printf("sending query to server:\n");
Kristin Wright's avatar
Kristin Wright committed
117
	sendto(sockfd, &ti, TOPD_INQ_SIZ, 0, pservaddr, servlen);
118
	print_tdinq((char *)&ti);
Kristin Wright's avatar
Kristin Wright committed
119
	n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
120
	fflush(stdin);
121 122 123 124
	printf("Receiving in client:==>\n");
	print_tdreply(recvline, n);
	gen_nam_file(recvline, n,"td1.nam");
	printf("Done!\n");
Kristin Wright's avatar
Kristin Wright committed
125 126
}

Kristin Wright's avatar
Kristin Wright committed
127 128 129 130 131
/*
 * Note that the TTL is a function of the network diameter that
 * we're interested in. The factor parameter is a function of the
 * network topology and performance.
 */ 
Kristin Wright's avatar
Kristin Wright committed
132 133 134 135 136 137
int
main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;

138 139 140
	if (argc != 5) {
		fprintf(stderr, "usage: cli <Server IPaddress> <TTL> <factor> <lan
				present?1/0>\n");
Kristin Wright's avatar
Kristin Wright committed
141 142 143 144 145 146 147 148 149 150
		exit(1);
	}

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);

151
	//printf("calling client\n");
152 153
	cli(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr), atoi(argv[2]),
		atoi(argv[3]), atoi(argv[4]));
Kristin Wright's avatar
Kristin Wright committed
154 155 156

	exit(0);
}