|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
Linux is 本机ip question! ! !
Want 本机ip, code is posted before the forum, slightly changed a bit.
My question is,
A total of three joints, eth0, eth1, lo, are the up.
However, procedures are shown DOWN.
I netdevice a man, no problem, gdb a bit and found running ifr_flags struct ifreq does not seem to have a ifru_flags, but also for the translation of this is not the past.
This is how it all about?
As follows : (rh8.0)
#include "Stdio.h>;
#include "Sys/types.h>;
#include "Sys/param.h>;
#include "Sys/ioctl.h>;
#include "Sys/socket.h>;
#include "Net/if.h>;
#include "Netinet/in.h>;
#include "Net/if_arp.h>;
Void get_ip ()
{
Struct ifreq if_buf[16];
Struct ifconf ifc;
Int fd, interface;
If ((fd = socket (AF_INET, SOCK_DGRAM, 0)) "; 0)
{
Ifc.ifc_len = sizeof (if_buf);
Ifc.ifc_buf = (caddr_t) if_buf;
If (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
{
Ifc.ifc_len/sizeof interface = (struct ifreq);
Printf ( "%d interfaces\n" interface);
While (interface-- "; 0)
{
Printf ( "net device %s\n" if_buf[interface].ifr_name);
If (if_buf[interface].ifr_flags & IFF_UP)
Puts ( "the interface status is UP");
Else
Puts ( "the interface status is DOWN");
If (! (Ioctl (fd, SIOCGIFADDR, (char *) &if_buf[interface])))
{
Puts ( "IP address is :");
Puts (inet_ntoa (((struct sockaddr_in*) (&if_buf[interface].ifr_addr)) ->;sin_addr));
Puts ( "");
//puts (Buf[interface].ifr_addr.sa_data);
}
}
}
}
}
Main ()
{
Get_ip ();
} |
Linux is 本机ip question! ! !
# IFF_UP 0x1 interface is up */ LEAVES OF 13 SPECIES OF LAURACEAE
Struct ifreq
{
# 6 IFHWADDRLEN
# 16 IFNAMSIZ
Union
{
Char ifrn_name[IFNAMSIZ]; LEAVES OF 13 SPECIES OF LAURACEAE if name, eg "en0" */
Ifr_ifrn;)
Union (
Struct sockaddr ifru_addr;
Struct sockaddr ifru_dstaddr;
Struct sockaddr ifru_broadaddr;
Struct sockaddr ifru_netmask;
Struct sockaddr ifru_hwaddr;
Short ifru_flags;
Int ifru_ivalue;
Int ifru_mtu;
Struct ifmap ifru_map;
Just fits the size */ char ifru_slave[IFNAMSIZ]; LEAVES OF 13 SPECIES OF LAURACEAE
Char ifru_newname[IFNAMSIZ];
Char * ifru_data;
Struct if_settings ifru_settings;
Ifr_ifru;)
};
# Ifr_flags ifr_ifru.ifru_flags LEAVES OF 13 SPECIES OF LAURACEAE flags */
Since the definition is ifru_flags |
Linux is 本机ip question! ! !
Thank you! ! !
I saw if.h that is the case.
But the question is why the results are down?
Ah, I ifconfig is up? Faint
This is how it all about? |
| |