Solved

Add/deleting an entry in the Routing table.

Posted on 2002-03-29
10
2,275 Views
Last Modified: 2013-12-23
Hi,

I want to add/delete a entry to/from unix routing table, using ioctl(..., SIOCADDRT, ...).

I am using SunOS 5.6.

Can anyone give me an working example of such program (URL is also okay), which will complie and run without any problem..

-- arupam

0
Comment
Question by:arupam
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:The--Captain
ID: 6927630
If you're looking for a somewhat generic solution, look into one of the publicly available routing daemons (routed, gated, etc) - the source for those should have many examples of the function calls you are looking for.  I can look up the URLs for you really want...

-Jon

0
 

Author Comment

by:arupam
ID: 6927797
Hi Jon,

Thanks for Ur reply,

Actually, I wanted to cange the routing table thru the code. I had written a code, In some cases it is working fine. But some cases it is code is masking last few bytes of the destiation in the routing table.

For example, if I try with the destination address "x.y.z.0" and the gateway "a.b.c.d", I am getting the following entry in the routing table:

  Destination           Gateway           Flags  Ref   Use   Interface
-------------------- -------------------- ----- ----- ------ ---------
   x.y.0.0             a.b.c.d           UG       0      0  

But some cases, I am getting

  Destination           Gateway           Flags  Ref   Use   Interface
-------------------- -------------------- ----- ----- ------ ---------
   x.y.z.0             a.b.c.d           UG       0      0  


Where the only change is the destination IpAddress x.y.z.0. Is the code taking some default mask (may be depending on the IpAddress class?).

here is part of my code:
    struct rtentry *rt;
    struct sockaddr_in dest;
    struct sockaddr_in gway;

    rt = (struct rtentry*) malloc (sizeof (struct rtentry));
    memset (rt, 0, (sizeof (struct rtentry)));

    memset (&gway, 0, sizeof (struct sockaddr_in));
    gway.sin_family = AF_INET;
    gway.sin_addr.s_addr = inet_addr (My_gateway)

    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = inet_addr (My_dest);

    rt->rt_gateway = *((struct sockaddr *)&gway);
    rt->rt_dst = *((struct sockaddr *)&dest);
    rt->rt_flags = RTF_UP | RTF_GATEWAY ;
    rt->rt_use = 0;

    if (ioctl (skfd, SIOCADDRT, rt))
    {
        perror ("SIOCADDRT Error!");
        close (skfd);
        exit (1);
    }

If I try the same thing on the prompt, "route add -net x.y.z.0 -netmask 255.255.254.0 a.b.c.d" It works fine..

What can be the problem and how can I solve this?

Also can we specify any netmasks in the code for SunOS 5.6 or 5.8? The structure rtentry does not have any netmask field.

Regards,
Arupam


0
 
LVL 1

Expert Comment

by:alextr
ID: 7246637
The problem is related to the netmask you didn't assign in your ioctl. If you set the entry by command line (route add....) you are setting also the netmask
Probably when you get x.y.0.0 you are trying to assing a class B destination network
In rt_flags you should use RTF_MASK
0
 
LVL 16

Expert Comment

by:The--Captain
ID: 7248226
Do you own a copy of "Unix Network Programming" by Stevens?  Seems to be one of the more definitive works on the subject...

I would be happy to confirm alex's musings if I could find my own copy (currently buried).

I will see if I can dig it up.

Cheers,
-Jon
0
 

Author Comment

by:arupam
ID: 7259076
Hi all,

Thanks for Ur replies. Now I am using routing sockets, and it works fine!

-- Arupam

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 1

Expert Comment

by:alextr
ID: 7259100
Ok, but in any case I think you should assign points to someone

Bye and enjoy your job ;-)
0
 
LVL 16

Expert Comment

by:The--Captain
ID: 7261204
I found my copy of the Stevens tome, if you still need specific examples.

In any case, I agree w/ alex.

Cheers,
-Jon
0
 
LVL 16

Expert Comment

by:The--Captain
ID: 7266552
Stevens doesn't talk about much other than SIOCADDRT - are you using a different method, or simply a routing that everntually calls SIOCADDRT?

Please elaborate your solution.

Thanks,
-Jon
0
 

Accepted Solution

by:
arupam earned 0 total points
ID: 7295908
Hi Jon,

What I did is:
-- open a routing socket via socket call.
-- Make a structure which contains the rt_msghdr and the dest, getway and mask address.
-- Fill proper values in the structures.
-- Write it to the socket...

Very easy. :-)

I wanted to give some points to someone, but the comments were not close enough to any solution. RTF_MASK also did not help me much.. So I cannot accept them as solution... :-)

-- Arupam

0
 

Expert Comment

by:CleanupPing
ID: 9157223
arupam:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now