Link to home
Start Free TrialLog in
Avatar of cranium2003
cranium2003

asked on

How host can detect its nearer router?

hello,
  I want to write a program that will communicate with adjecent router thro' C program. Can i use socket program or write a Linux kernel module to achieve it?
    what data structure can i use to find nearest router?
Avatar of jlevie
jlevie

You won't easily be able "to find nearest router" unless said router is running a routing protocol like RIP, OSPF, etc., that broadcasts routing information. If you don't know what the IP of a local router is and the router isn't brodcasting routing information you'd have to try routing a packet through every local IP.
Avatar of cranium2003

ASKER

can it be possible through /proc/net/route on linux machine.
by reading a /proc/net/route file and sending my packet to each Gateway router
Maybe I misunderstood what you are after. I took the question title"How host can detect its nearer router?" to mean that you want to wite a program that will search out adjacent routers. But your last comment seems to indicate otherwise.

What, eaxtly, are you wanting to do in this code?
hi jleive,
       AS i told you already i want to write a program that will contact other routers and exchange my own data packet between two routers, these two routers are software made routers by making ip forwarding enabled. for that i ask that can i use /proc/net/route to check adjecent routers with ip address entry flag as gateway in /proc/net/route?
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello jlevie,
                 but picking that file will give only ip address in hex form and route -r gives ip table in decimal form. i want it to be in decimal form when i open a file /pro/net/route and read it. how can i do it?
There are a number of ways of converting the hex value to decimal octets. One way might look like:

#include <stdio.h>
#include <string.h>

main()
{
  char in[] = "7D00A8C0";
  char ou[16];
  int ip[4];

  sprintf(ou, "0x%c%c.0x%c%c.0x%c%c.0x%c%c",
      in[6], in[7], in[4], in[5], in[2], in[3], in[0], in[1]);
  sscanf(ou, "%x.%x.%x.%x", &ip[0], &ip[1], &ip[2], &ip[3]);
  printf("%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
  exit(0);
}