This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Set route to interface


Hi,

how can I set a route for a specific ip-address outside of the actual 
configured network to a interface.

For example eCos is configured as follows:

ip-address: 192.168.50.100
netmask:    255.255.255.0
gateway:    192.168.50.1

Now I want to send an udp-message to 192.168.1.30. I thought I could just add 
a route for destination 192.168.1.30 to "eth0". If I try this under linux it 
seems to work.
->  route add -host 192.168.1.30 dev eth0

Under eCos I have tried the following (peer is 192.168.1.30):

int addroute(struct sockaddr peer)
{
	struct sockaddr *so_peer=&peer;
	struct sockaddr_in *so_in_peer=(struct sockaddr_in *)&peer;
	struct ecos_rtentry route;
	struct sockaddr_in a;
	struct sockaddr_in *addrp=&a;
	char mask[4]={255,255,255,255};
	char local[4]={192,168,50,100};
	int s;

	/* verify the destination is directly reachable */
	if ((ifa_ifwithnet(so_peer)) == NULL) {

		s = socket(AF_INET, SOCK_DGRAM, 0);
   		if (s < 0) {
			VS_PERROR(1,"socket");
			return VS_EERROR;
		}

		memset(&route, 0, sizeof(route));
		addrp->sin_family = AF_INET;
		addrp->sin_port = 0;
		addrp->sin_len = sizeof(*addrp);
		addrp->sin_addr = so_in_peer->sin_addr;
		memcpy(&route.rt_dst, addrp, sizeof(*addrp));
		memcpy(&addrp->sin_addr.s_addr,mask,4);
		memcpy(&route.rt_genmask, addrp, sizeof(*addrp));
		/* use local ip as gateway to set route to "eth0" */
		memcpy(&addrp->sin_addr.s_addr,local,4);
		memcpy(&route.rt_gateway, addrp, sizeof(*addrp));

		route.rt_dev = "eth0";
		route.rt_flags = RTF_UP;
		route.rt_metric = 0;

		if (ioctl(s, SIOCADDRT, &route)) {
	                perror("SIOCADDRT");
			if (errno != EEXIST) {
				close(s);
				return -1;
			}
		}
	}

	close(s);
	return 0;
}

Thank you,

Roland

-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]