[ECOS] Set route to interface

Gary Thomas gary@mlbassoc.com
Mon Dec 30 07:14:00 GMT 2002


On Mon, 2002-12-30 at 07:03, Roland Caßebohm wrote:
> On Montag, 30. Dezember 2002 13:49, Gary Thomas wrote:
> > On Mon, 2002-12-30 at 03:25, Roland Caßebohm wrote:
> > > 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};
> >
> > These constructs are not portable at all.  It would be much better to
> > use one of the address conversion functions to set these fields.
> Yes, this was just to make a short test.
> 
> >
> > > 	int s;
> > >
> > > 	/* verify the destination is directly reachable */
> > > 	if ((ifa_ifwithnet(so_peer)) == NULL) {
> >
> > Note: calling functions internal to the stack is very bad practice.
> >
> > > 		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;
> >
> > Have you tried this?
> >  		route.rt_flags = RTF_UP|RTF_GATEWAY;
> Yes, but it doesn't work. In both cases the ioctl() call succeds, but sendto() 
> to 192.168.1.30 fails.
> 

How about RTF_STATIC instead?

Also, have you tried printing out the routing tables, to see if they
look correct?  I added some functions which do that recently.

> >
> > > 		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
> 
> 
> -- 
> Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
> and search the list archive: http://sources.redhat.com/ml/ecos-discuss
-- 
------------------------------------------------------------
Gary Thomas                 |
MLB Associates              |  Consulting for the
+1 (970) 229-1963           |    Embedded world
http://www.mlbassoc.com/    |
email: <gary@mlbassoc.com>  |
gpg: http://www.chez-thomas.org/gary/gpg_key.asc
------------------------------------------------------------


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



More information about the Ecos-discuss mailing list