Sponsored Content
Top Forums Programming retrieving network interface list Post 302401656 by xyzt on Sunday 7th of March 2010 07:34:36 AM
Old 03-07-2010
retrieving network interface list

Hello
I have a problem with retreiving network interface list using IOCTLs. I have 4 interfaces:
Code:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:1d:ba:39:f2:8d brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:21:5c:0d:ad:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.5/24 brd 192.168.2.255 scope global wlan0
    inet6 fe80::221:5cff:fe0d:ad15/64 scope link
       valid_lft forever preferred_lft forever
4: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN
    link/ether ae:97:70:65:ed:95 brd ff:ff:ff:ff:ff:ff

But in my program just the lo interface is found. Why can't I get all interfaces?
Here's the code:
Code:
struct interface {
	char			ifname[MAXINTFNAME_LEN];
	uint8_t			addr[MACADDR_LEN];
	int			if_index;
	struct sockaddr_in	if_broadaddr;
};

int get_interface_info(struct interface* interf)
{
	struct ifreq ifr;
	struct ifreq *ifr_arr;
	struct ifconf ifc;
	int found = 0;

	char buf[1024] = {'\0'};
	int s = 0, ret = 0, i = 0;


	if (!interf || interf->ifname==NULL)
		return -1;

	memset(&ifr, 0, sizeof ifr);
	memset(&ifc, 0, sizeof ifc);

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s==-1) {
		return -1;
	}

	ifc.ifc_len = sizeof(buf);
        ifc.ifc_buf = buf;
	ioctl(s, SIOCGIFCONF, &ifc);

	ifr_arr = ifc.ifc_req;
	for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr_arr++) {
		if (!strcmp(ifr_arr->ifr_name, interf->ifname)) {
			strcpy(ifr.ifr_name, interf->ifname);
			if (0 == (ret=ioctl(s, SIOCGIFFLAGS, &ifr))) {
				if (0 == (ret=ioctl(s, SIOCGIFHWADDR, &ifr))) {
					memcpy(interf->addr, ifr.ifr_hwaddr.sa_data, MACADDR_LEN);
					if (0 ==(ret=ioctl(s, SIOCGIFINDEX, &ifr))) {
						interf->if_index = ifr.ifr_ifindex;
					}
					if (0 == (ret=ioctl(s, SIOCGIFBRDADDR, &ifr))) {
						memset(&(interf->if_broadaddr), 0x0, sizeof(interf->if_broadaddr));
						memcpy(&(interf->if_broadaddr), (struct sockaddr_in*)(&(ifr.ifr_ifru.ifru_broadaddr)), sizeof(ifr.ifr_ifru.ifru_broadaddr));
					}
				}
			}
			found = 1;
			break;
		}
	}
	close(s);
	if (!found)
		return -2;
	return ret;
}

My system is Linux 2.6.31
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Network Interface

Hi ! Does anyone know how can i get information about my network interface ... if it works in half or full duplex mode !! Thx in adivance ! Witt (4 Replies)
Discussion started by: witt
4 Replies

2. UNIX for Dummies Questions & Answers

network interface questions

I can't remember or figure out how to do this. What is the command(s) in Solaris 8 sparc to show the values operating a NIC, specifically the negotiated transfer rate (10/100) and mode (full/half duplex)? Also, how can I force 100 Mb full (the network is just a crossover cable, and the other box is... (3 Replies)
Discussion started by: 98_1LE
3 Replies

3. Solaris

Network Interface Issue

Hi all, Here im facing problem with NIC Interface with my X86 Version of Solaris Express, even after loading module for that Interface. So please check this below mentioned log for your use and get back to me ASAP. - - - -- --... (0 Replies)
Discussion started by: baraghun
0 Replies

4. AIX

AIX platform: network interface list

Hi, I am trying to list all the interface present in the given system. Here is sample program. i.e. cat 1.c #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <sys/dlpi.h> #include <net/if.h> /*#include <sys/sockio.h>*/ #include <unistd.h> #include... (3 Replies)
Discussion started by: jgobbur
3 Replies

5. Solaris

configure zones to have different network interface and network

i need to configure a zone to use different interface (bge2) than global and have connected to completely different network switch & to use its own defaultrouter and hosts file .. is it possible ..if so ..how ? Thanks (9 Replies)
Discussion started by: skamal4u
9 Replies

6. UNIX and Linux Applications

Access to network interface (Mac-network)

Hi, I'm a italian student. For my thesis I develop a gateway with protocol 6lowpan. For that I must access to network interface to develope my personal stack based on standard 802.15.4. Can you help me? I need an explanation for that. (0 Replies)
Discussion started by: berny88
0 Replies

7. IP Networking

Network interface-

Hello, Please what's the difference between: AND Thank you so much (3 Replies)
Discussion started by: chercheur857
3 Replies

8. IP Networking

network interface -

Hello, Please, how can i create a network interface with a routable IP address on linux (ubuntu) ? AND How can i desactivate an interface? Thank you so much for help. (1 Reply)
Discussion started by: chercheur857
1 Replies

9. Solaris

No network cable But Network interface is UP and Running

I've one Netra 240 After changing main board and system configuration card reader, Network is not accessible any more, Network interfaces are always UP and Running even when there is no cable connected to Network interfaces. I tried to restart and plumb/unplumb with no luck. ifconfig -a... (7 Replies)
Discussion started by: samer.odeh
7 Replies

10. UNIX for Dummies Questions & Answers

Adding a network interface to a bonded interface

I have a RHEL 5 system with a bonded interface configure using only one network port (eth0). So I have config file for ifcfg-bond0 and ifcfg-eth. I'd like to configure eth5 to be the second SLAVE in the bond. My question is, after I modify ifcfg-eth5, can I add eth5 to the bond0 interface without... (1 Reply)
Discussion started by: westmoreland
1 Replies
aoe-interfaces(8)					      System Manager's Manual						 aoe-interfaces(8)

NAME
aoe-interfaces - restrict aoe driver to specified network interfaces SYNOPSIS
aoe-interfaces [dev1] [dev2 ...] aoe-interfaces -c DESCRIPTION
The aoe-interfaces command tells the aoe driver to ignore ATA over Ethernet (AoE) traffic on all but the specified network interfaces. It is analogous to the aoe_iflist module load option. If neither the aoe_iflist module load option nor the aoe-interfaces command are used, the aoe driver will use any network interface for AoE traffic. Using aoe-interfaces to limit AoE to only trusted networks prevents the case where a rogue AoE target appears on a public net- work, diverting data from the legitimate AoE target. Such an imposter target effectively corrupts the data on the legitimate target. If the aoe driver is a module, then calling aoe-interfaces without arguments will display the current interfaces list. If it hasn't been set then the output will be blank. It's good to run the aoe-discover command after setting the AoE interfaces list. OPTIONS
-c The -c flag will clear the interface access list, permitting any interface to be used. EXAMPLE
In this example, the root user on a host named nai loads the aoe module with only eth0 allowable for AoE traffic. After remembering that shelf 7 is on eth3, this sysadmin uses aoe-interfaces to add eth3 to the list of allowable network interfaces and then calls aoe-discover to ask the aoe driver to look for new AoE devices. nai:~# modprobe aoe aoe_iflist="eth0" nai:~# aoe-stat e10.9 eth0 up nai:~# aoe-interfaces eth0 eth3 nai:~# aoe-discover nai:~# aoe-stat e7.0 eth3 up e7.1 eth3 up e7.2 eth3 up e7.3 eth3 up e7.4 eth3 up e7.5 eth3 up e7.6 eth3 up e7.7 eth3 up e7.8 eth3 up e7.9 eth3 up e10.9 eth0 up nai:~# aoe-interfaces eth0 eth3 SEE ALSO
aoe-discover(8), aoe-stat(8), aoetools(8). AUTHOR
Ed L. Cashin (ecashin@coraid.com) aoe-interfaces(8)
All times are GMT -4. The time now is 08:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy