How to find out if the eth0 mode is static or dhcp?
Asked Answered
M

3

6

I want to use a C program to get if the ip of the network interface is set manually or via dhcp.

I've tried to use the following code and it has worked in Debian, but it hasn't worked in OpenWrt. I want to know how to write a C program doing this in OpenWrt. I have tried to use this:

#include <stdio.h>
int main(void)
{
    FILE *fp;
    char buffer[80];
    fp=popen("cat /etc/network/interfaces |grep ^iface\\ br-lan | awk -F ' ' '{print $4}'","r");
    fgets(buffer, sizeof(buffer), fp);
    printf("%s", buffer);
    pclose(fp);
}

This code is working in Debian, but it isn't working normally in OpenWrt, so I want to know how to write a program to get the same result.

Monterrey answered 18/4, 2013 at 14:6 Comment(7)
good question(+1) but what have you tried?Humanly
there is a risk to get down voted if you do not provide what you have triedHumanly
Do you want it in general, or for a specific OS?Deafen
That's not a big problem. If the code is not read the file in the os, and just using linux system api. It will be fine.Monterrey
You may also have a look at /sys/class/net/eth0. I don;t know the syntax & portability though...Underwood
Use /proc/net and strace ifconfig to find out what it is doing...Kaylenekayley
@stephen: did you got the generic solution for this?? i also want answer for this question, by which method i can find out whether client is dhcp enabled or it gas static IP .Engvall
H
8

for OpenWRT you can get a such information with the following command:

$uci get network.lan.proto

so I take the program you put in your question and I change only the command used to get information:

#include <stdio.h> <br>
int main(void)
{
    FILE *fp;
    char buffer[80];
    fp=popen("uci get network.lan.proto","r");
    fgets(buffer, sizeof(buffer), fp);
    printf("%s", buffer);
    pclose(fp);
}

to see all network interfaces available in your OpenWRT you can use the following command:

$uci show network

You can avoid using calling linux command in your c by using the libuci. The libuci contains C function to execute uci commands without passing via popen ( popen is used to execute external command from shell).

The libuci exist by default in the development environment of OpenWRT, not need to download it, no need to build it and no need to install it on your OpenWRT machine

You can use libuci in this way

#include <uci.h>
void main()
{
    char path[]="network.lan.proto";
    char buffer[80];
    struct  uci_ptr ptr;
    struct  uci_context *c = uci_alloc_context();

    if(!c) return;

    if ((uci_lookup_ptr(c, &ptr, path, true) != UCI_OK) ||
        (ptr.o==NULL || ptr.o->v.string==NULL)) { 
        uci_free_context(c);
        return;
    }

    if(ptr.flags & UCI_LOOKUP_COMPLETE)
            strcpy(buffer, ptr.o->v.string);

    uci_free_context(c);

    printf("%s\n", buffer);
}

(Not tested)

and when you compile your program you have to add the -luci in the compilation command gcc

Humanly answered 18/4, 2013 at 14:32 Comment(2)
Hi MOHAMED thanks for you help! the first code using command uci is fine! The second code which i can't using it , because i must using -luci . Maybe i will try later ,and thanks againMonterrey
I would suggest maybe using the -P /var/state/ flag on the uci command which will look at the current state of the system instead of the default config. This way if for some odd reason someone changed the uci to use dhcp from static and no uci commit was executed it would be reflected. But yea second solution is best!Newtonnext
H
2

There's no required way for an OS to decide how an interface should be configured. The kernel (the Linux part of e.g. GNU/Linux) doesn't decide, it doesn't (and shouldn't) care, it just gets told which network addresses go with which interfaces by whatever configuration system the OS is using. OpenWRT's not GNU, it operates differently.

Homs answered 18/4, 2013 at 14:53 Comment(0)
D
1

There is AFAIK no definitive way.

Reading the interfaces file would be a hint only: there is no guarantee that the current seup came from there.

You could look at 'asking' the DBUS interface if there is one. You could check for a dhclient process running. You could check other files in /etc that specify network setup on different distros.

I think the most reliable option would be a multi-layered thing: check a whole host of hints to come up with the answer.

Another option: send a DHCP check packet to the dhcp server to verify the address.. if you don't get an answer though it could be that the network is down but was up when the address was allocated.

Doomsday answered 18/4, 2013 at 14:23 Comment(2)
I think you didn't know what i want. I just want to know how to programming to got the method of the network interface is dhcp or static. Not using shell programming and any other way, and just using linux c programing.Monterrey
I did realize that... but there is no record in the interface itself of what you want to know, so you are asking for something that isn't recorded.Doomsday

© 2022 - 2024 — McMap. All rights reserved.