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]

Re: Retrieving default gateway?


On Tue, Oct 23, 2001 at 01:58:14PM -0500, Grant Edwards wrote:

> How does an application get info out of the routing table?
> 
> I'm using ioctl(s,SIOCGIFxxxx,...) to find out the IP address
> and network mask, but I can't figure how to find out the
> default gateway.

Since it really bugs me when I search an archive and find my
question but no answer, what I came up with is shown below. I'm
still trying to decide whether I should call RTFREE(). Since
this routine gets called many times, not calling RTFREE should
result in less work on subsequent calls. I think. Maybe.


#include <network.h>
#include <net/route.h>

extern struct rtentry *rtalloc1(struct sockaddr *dst, int report);

unsigned long myIpGate(void)
{
  struct rtentry *rt;
  struct sockaddr dst;
  
  memset(&dst,0,sizeof dst);
  dst.sa_family = PF_INET;
  
  rt = rtalloc1(&dst,0);

  if (rt && rt->rt_gateway)
    return ((struct sockaddr_in*)(rt->rt_gateway))->sin_addr.s_addr;
  else
    return 0;
}


-- 
Grant Edwards
grante@visi.com


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