AR150: Problem in getting IP address of wireless network interface

Background

I had bought a GL-iNet mini router, GL-AR150-Ext-2, and would like to deploy my own service program (in Linux C) in this router. I need to get the IP address (IPv4) of the wireless network interface, i.e., named wlan0, in my Linux C program. In general, this can be done by using the ioctl () function provided by Linux. My function to get IP address of a given network interface can be

/*
 * Returns information on a network interface given its name...
 */
struct sockaddr_in *get_if_info(char *ifname, int type)
{
    int skfd;
    struct sockaddr_in *ina;
    static struct ifreq ifr;

    /* Get address of interface... */
    skfd = socket(AF_INET, SOCK_DGRAM, 0);

    strcpy(ifr.ifr_name, ifname);
    if (ioctl(skfd, type, &ifr) < 0) {
	printf("Could not get address of %s \n", ifname);
	close(skfd);
	return NULL;
    } else {
	ina = (struct sockaddr_in *) &ifr.ifr_addr;
	close(skfd);
	return ina;
    }
}

Problem

However, when I called the C function above with the given network interface name wlan0, I got an error “Could not get address of wlan0”.

When I used SSH to log in the router and checked the configuration of the network interfaces provided by the router by using the Linux ifconfig command, I found that the configuration of the network interface, wlan0, only contains the IPv6 address information, i.e., inet6 addr (See the following text), while I could not find the IPv4 address information i.e., inet addr. (The device was running in the Router mode)

wlan0     Link encap:Ethernet  HWaddr E4:95:6E:44:EE:8F  
          inet6 addr: fe80::e695:6eff:fe44:ee8f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:14188 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9682 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2639309 (2.5 MiB)  TX bytes:5417952 (5.1 MiB)

Therefore, my problem is:

  • Why does the configuration of the wireless network interface wlan0 not give an IPv4 address (i.e., inet addr)? Commonly, when we use ifconfig command, we can see the inet addr of network interfaces of a device running Linux OS, such as :
eth0      Link encap:Ethernet  HWaddr E4:95:6E:44:EE:8F  
          inet addr:172.17.13.165  Bcast:172.17.13.255  Mask:255.255.255.0
          inet6 addr: 2001:da8:203:d508:e695:6eff:fe44:ee8f/64 Scope:Global
          inet6 addr: fe80::e695:6eff:fe44:ee8f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:37658 errors:0 dropped:33 overruns:0 frame:0
          TX packets:11244 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:37222188 (35.4 MiB)  TX bytes:2244877 (2.1 MiB)
          Interrupt:4 
  • Could I associate a specified IP address with wlan0? and How to do this? (Indeed, I know that we can use the command ifconfig to configure the IP address of the wireless network interface like this: ifconfig wlan0 192.168.10.1. But when I restart the network by service network restart, the given IP address configuration of wlan0 is removed.)

My another question is that

  • Could I use the router as a Client that is expected to be running in the Ad Hoc mode since I would like to establish an Wireless Ad Hoc Network? And how to do this? Or How can I configure the router to a Ad Hoc mode?

I am looking forward to hearing from someone with expertise.

Many thanks!

Jianshan

You’ll get an address for eth0, as this is the WAN port and is a routed interface at layer 3

if you look at eth1 (lan port), you’ll see there is no IPv4 assigned, as this is layer 2, same with wlan0 as they are switched interfaces, so no IP’s need to be assigned

As you might have noticed, wlan0 does present an ipv6 addr, and this is appropriate, but it’s a link local address, and as such, not routable to the internet.

for the client/clients associated to the wlan0, they’ll do the layer 3, so you can find approaches there.

Normally, wlan0 works in AP mode, bridging with eth1 to generate a network port br-lan, so the IP of wlan0 is the IP of the br-lan

pelase refer to this link:Internet - GL.iNet Docs

Thank you for your introduction. Your reply can really help me deepen my understanding of the network stack. :+1:

:ok_hand: Thanks very much for your reply and shared link!

1 Like

This might help…

#! /bin/ash
ifaces="wlan0"

for iface in $ifaces
do
  echo $iface
  stations=`iw dev $iface station dump | grep Station | awk '{print $2}'`

  for sta in $stations
  do
    echo "------------------------------------------------------"
    iw dev $iface station get $sta
    echo -e "\t ---"
    grep $sta /proc/net/arp | awk '{print "\t IP: "$1" (from ARP table)"}'
    grep -i $sta /var/dhcp.leases | awk '{print "\t IP: "$3" (from DHCP Lease)\n\t NAME: "$4" (from DHCP Lease)"}'
  done
  echo "------------------------------------------------------"
done
1 Like

Many thanks for your help. Your script is really helpful.٩( ๑╹ ꇴ╹)۶

Hehe - it’s not elegant, but it does work to answer your question.

:wink:

best - sfx