forw_requests.c 4.17 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: forw_requests.c
 *   -- Author: Kristin Wright <kwright@cs.utah.edu> 
 *
 * ---------------------------
 *
21
 * $Id: forw_requests.c,v 1.11 2001-08-04 22:58:30 ikumar Exp $
Kristin Wright's avatar
Kristin Wright committed
22 23
 */

24 25
#include <math.h>

Kristin Wright's avatar
Kristin Wright committed
26 27 28 29 30 31 32 33
#include "discvr.h"
#include "packet.h"
#include "util.h"

/*
 * Send a request on to each interface 
 */
char recvbuf[BUFSIZ];
34
extern u_char myNodeID[ETHADDRSIZ];
35 36
extern u_char parent_nodeIF[ETHADDRSIZ];

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
static double start = 0.0;

double
tod(void)
{
        double s;
	struct timeval tv;

	gettimeofday(&tv, 0);
	s = tv.tv_sec;
	s += (1e-6 * tv.tv_usec);
	return (s - start);
}


52 53 54 55 56 57 58 59 60 61 62 63
struct ifi_info *
get_ifi_struct(int sock, struct ifi_info * ifihead)
{
	struct ifi_info * ifi=NULL;

	for (ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next) 
	{
		if(ifi->sock == sock) return ifi;	
	}
	return NULL;
}

64
/*
65 66 67 68 69 70 71 72
void
addMyID(char* mesg, int size)
{
	struct topd_nbor *p;
	p = (struct topd_nbor *) (mesg + sizeof(topd_inqid_t));

	memcpy(p->tdnbor_dnode,myNodeID,ETHADDRSIZ);
}
73
*/
74 75


76 77 78
int *
forward_request(struct ifi_info *ifihead, const struct in_pktinfo *pktinfo, 
		 const char *mesg, int mesglen, int *ptrSockNum) 
Kristin Wright's avatar
Kristin Wright committed
79 80
{
	int                     s, n;
81
    const int               on = 1;
Kristin Wright's avatar
Kristin Wright committed
82
	char                    ifname[IFNAMSIZ];
83
	struct sockaddr_in      sin,tempAddr;
84
	struct ifi_info         *ifi;
85 86
	int 			*sock_list=NULL, *temp_sock_list=NULL;
	struct topd_inqid *temp_mesg;
87
	int t_int=0,i=0;
88 89 90 91

	bzero(&tempAddr, sizeof(tempAddr));
	tempAddr.sin_family      = AF_INET;
	tempAddr.sin_addr.s_addr = htonl(INADDR_ANY);
92
	tempAddr.sin_port        = htons(0);
Kristin Wright's avatar
Kristin Wright committed
93
	
94 95
	temp_mesg = (struct topd_inqid *)mesg;
	t_int = ntohs(temp_mesg->tdi_ttl);
96
    t_int=t_int-1;
97 98
	temp_mesg->tdi_ttl = htons(t_int);

99 100 101 102 103 104
	(*ptrSockNum) = 0;
	for (ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next) 
	{
		// Skip all the interfaces which are not useful
		if( (ifi->ifi_flags & !IFF_UP) || (ifi->ifi_flags & IFF_LOOPBACK) ||
		    (strcmp(ifi->ifi_name, if_indextoname(pktinfo->ipi_ifindex, ifname)) == 0) ||
105 106 107 108
			/*(strcmp(ifi->ifi_name,"fxp4")==0)*/
			(strncmp(inet_ntoa(((struct sockaddr_in *)(ifi->ifi_addr))->sin_addr),"155.101.132",11)==0)
			)

109 110 111
		{
			continue;
		}
112

113 114
		memcpy(temp_mesg->tdi_p_nodeIF,ifi->ifi_haddr,ETHADDRSIZ);
		//memcpy(&(temp_mesg->tdi_p_nodeIF),&myNodeID,ETHADDRSIZ);
115 116

		printf("Forwarding the query to interface: \"%s\"\n",ifi->ifi_name);
117
		temp_sock_list = sock_list;
118 119
		sock_list = (int *)malloc(sizeof(int)*((*ptrSockNum)+1));
		for(i=0;i<(*ptrSockNum);i++)
120
		{
121
			//printf("copying: %d\n",temp_sock_list[i]);
122 123 124
			sock_list[i] = temp_sock_list[i];
		}
		free(temp_sock_list);
125 126 127 128 129
	    sock_list[(*ptrSockNum)] = s = socket(AF_INET, SOCK_DGRAM, 0);
		(*ptrSockNum)++;
	    if (s == -1) 
		{
			perror("Unable to get socket");
Kristin Wright's avatar
Kristin Wright committed
130 131
			exit(1);
		}
132
		if(bind(s, (struct sockaddr *) &tempAddr, sizeof(tempAddr))<0)
133 134 135
	    {
        	perror("Problem in bind call");
       	} 
136
		ifi->sock = s;
137 138 139 140
		if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) != 0) 
		{
	        perror("setsockopt died.");
			// Do something intelligent. -lkw 
Kristin Wright's avatar
Kristin Wright committed
141 142 143 144 145
		} 
		bzero(&sin, sizeof(sin));
		sin.sin_len = htons(sizeof sin);
		sin.sin_family = PF_INET;
		sin.sin_port = htons(SERV_PORT);
146 147 148
		sin.sin_addr = ((struct sockaddr_in *)(ifi->ifi_brdaddr))->sin_addr;
        printf("The dest. address: %s\n", inet_ntoa(sin.sin_addr));
		printf("Forwarding the enquiry: ");
149
		print_tdinq(mesg);
Kristin Wright's avatar
Kristin Wright committed
150 151
		n = sendto(s, mesg, mesglen, 0, 
			   (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
152 153 154
		if (n != mesglen) 
		{
	        perror("Didn't send all of packet");
Kristin Wright's avatar
Kristin Wright committed
155 156
			exit(1);
		} 
157
	}
Kristin Wright's avatar
Kristin Wright committed
158

159
	return sock_list;
Kristin Wright's avatar
Kristin Wright committed
160

161
	//************************************************************************/
Kristin Wright's avatar
Kristin Wright committed
162
}