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

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

37 38 39 40
extern u_char *mac_list[MAX_NODES];
extern int num_nodes;


Kristin Wright's avatar
Kristin Wright committed
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
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
		 */
78
		 
Kristin Wright's avatar
Kristin Wright committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		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
98
make_inquiry(topd_inqid_t *tip, u_int16_t ttl, u_int16_t factor, int lans_exist) 
Kristin Wright's avatar
Kristin Wright committed
99
{
100
    struct timeval tv;
Kristin Wright's avatar
Kristin Wright committed
101 102 103 104 105 106 107 108
	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
109
	tip->tdi_tv.tv_sec  = htonl(tv.tv_sec);
Kristin Wright's avatar
Kristin Wright committed
110 111
	tip->tdi_tv.tv_usec = htonl(tv.tv_usec);

Kristin Wright's avatar
Kristin Wright committed
112 113 114 115
	/* ...then the ttl and factor... */
	tip->tdi_ttl     = htons(ttl);
	tip->tdi_factor  = htons(factor);

Kristin Wright's avatar
Kristin Wright committed
116 117 118
	/* ...and now our nodeID */
	nid = find_nodeID();
	memcpy((void *)tip->tdi_nodeID, nid, ETHADDRSIZ);
119
	bzero(tip->tdi_p_nodeIF,ETHADDRSIZ);
120
	tip->lans_exist = htons(lans_exist);
Kristin Wright's avatar
Kristin Wright committed
121 122 123
}

void
Kristin Wright's avatar
Kristin Wright committed
124
cli(int sockfd, const struct sockaddr *pservaddr, socklen_t servlen, 
125
    u_int16_t ttl, u_int16_t factor, int lans_exist)
Kristin Wright's avatar
Kristin Wright committed
126 127 128 129 130
{
        u_int32_t         n;
	char              recvline[MAXLINE + 1];
	topd_inqid_t      ti;
	
131
	make_inquiry(&ti, ttl, factor, lans_exist);
Kristin Wright's avatar
Kristin Wright committed
132

133
	printf("sending query to server:\n");
Kristin Wright's avatar
Kristin Wright committed
134
	sendto(sockfd, &ti, TOPD_INQ_SIZ, 0, pservaddr, servlen);
135
	print_tdinq((char *)&ti);
Kristin Wright's avatar
Kristin Wright committed
136
	n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
137
	fflush(stdin);
138 139 140 141
	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
142 143
}

Kristin Wright's avatar
Kristin Wright committed
144 145 146 147 148
/*
 * 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
149 150 151 152 153 154
int
main(int argc, char **argv)
{
	int sockfd;
	struct sockaddr_in servaddr;

155 156 157
	if (argc != 5) {
		fprintf(stderr, "usage: cli <Server IPaddress> <TTL> <factor> <lan
				present?1/0>\n");
Kristin Wright's avatar
Kristin Wright committed
158 159 160 161 162 163 164 165 166 167
		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);

168
	//printf("calling client\n");
169 170
	cli(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr), atoi(argv[2]),
		atoi(argv[3]), atoi(argv[4]));
Kristin Wright's avatar
Kristin Wright committed
171 172 173

	exit(0);
}