Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3211804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:46:20+00:00 2026-05-17T14:46:20+00:00

In Linux, after establishing a DHCP lease, when I run the command route ,

  • 0

In Linux, after establishing a DHCP lease, when I run the command route, the last line gives me the system’s default route and NIC.

e.g.,

Kernel IP routing table
Destination    Gateway         Genmask        Flags Metric Ref   Use IFace
142.157.138.0  *               255.255.254.0  U     2      0       0 wlan0
link-local     *               255.255.0.0    U     1000   0       0 wlan0
default        142.157.138.1   0.0.0.0        UG    0      0       0 wlan0

Is there a way to get the NIC marked “default” through a C API, instead of running route and parsing the output?

Thanks.

(Interesting in Linux and Darwin, eventually Windows.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:46:21+00:00Added an answer on May 17, 2026 at 2:46 pm

    I don’t know off-hand, but you could study the source. On my ubuntu box, route is provided by net-tools:

    ~$ which route
    /sbin/route
    ~$ dpkg -S /sbin/route
    net-tools: /sbin/route
    

    So you could make a directory somewhere, run apt-get source net-tools. Then I poked into the source, and route.c looked like a good place to start. If there’s no arguments passed to route, it calls route_info() for a given address family and options, so grep for that:

    ~/z$ grep -r route_info *
    lib/net-support.h:extern int route_info(const char *afname, int flags);
    lib/getroute.c: *960221 {1.02} Bernd Eckenfels:        renamed from route_info to getroute.c
    lib/getroute.c:int route_info(const char *afname, int options)
    netstat.c: *960204 {1.10} Bernd Eckenfels:        aftrans, usage, new route_info, 
    netstat.c:      i = route_info(afname, options);
    route.c: *       {1.79} Bernd Eckenfels:        route_info
    route.c:    i = route_info(afname, options);
    

    Great. lib/getroute.c looks promising. It looks like it’s choosing which function to call based of a table of function pointers, sorted by address family. Let’s find the IPv4 one:

    inet_aftype.rprint = INET_rprint;
    

    So we need to find the definition of INET_rprint. grep delivers: it’s in lib/inet_gr.c. The most interesting thing here is the fopen(_PATH_PROCNET_ROUTE, "r"), which is defined in lib/pathnames.h as /proc/net/route. proc_gen_fmt defined in lib/proc.c is used to make parsing the output easier.

    So your answer, at least on my GNU/Linux box, is to read /proc/net/route.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run my C#/C++ app on Linux after developing it on Windows.
After a linux kernel upgrade, my VMWare server cannot start until using vmware-config.pl to
Linux provides the stime(2) call to set the system time. However, while this will
I have a C++ program on Linux that crashes after some time with the
after installing XAMPP on linux i have seen the folder htdocs belongs to nobody.
After moving my mod_perl site from Linux hosting to FreeBSD, I have this error
After starting out with Python on Ubuntu Linux, I've now for a good while
Does anyone know where is the .htaccess file located after I install in linux
I am trying to get OpenGL and Glut running on Eclipse Linux FC13. After
Can linux context switch after unlock in the below code if so we have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.