Obtain IP addresses and MAC addresses in Linux

Source: Internet
Author: User
# 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>
# Ifdef Solaris
# Include <sys/sockio. h>
# Endif
# Define maxinterfaces 16 Main (argc, argv)
Register int argc;
Register char * argv [];
{
Register int FD, intrface, retn = 0;
Struct ifreq Buf [maxinterfaces];
Struct arpreq ARP;
Struct ifconf IFC;
If (FD = socket (af_inet, sock_dgram, 0)> = 0)
{
IFC. ifc_len = sizeof Buf;
IFC. ifc_buf = (caddr_t) BUF;
If (! IOCTL (FD, siocgifconf, (char *) & IFC ))
{
// Obtain Interface Information
Intrface = IFC. ifc_len/sizeof (struct ifreq );
Printf ("interface num is intrface = % d/n", intrface );
// Obtain the device IP address and MAC address cyclically Based on the excuse Information
While (intrface --> 0)
{
// Obtain the device name
Printf ("net device % s/n", Buf [intrface]. ifr_name );
 
// Determine the NIC type
If (! (IOCTL (FD, siocgifflags, (char *) & Buf [intrface])
{
If (BUF [intrface]. ifr_flags & iff_promisc)
{
Puts ("the interface is promisc ");
Retn ++;
}
}
Else
{
Char STR [256];
Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );
Perror (STR );
}
// Determine the NIC status
If (BUF [intrface]. ifr_flags & iff_up)
{
Puts ("the interface status is up ");
}
Else
{
Puts ("the interface status is down ");
}
// Obtain the IP address of the current Nic
If (! (IOCTL (FD, siocgifaddr, (char *) & Buf [intrface])
{
Puts ("IP address is :");
Puts (inet_ntoa (struct sockaddr_in *) (& Buf [intrface]. ifr_addr)-> sin_addr ));
Puts ("");
// Puts (BUF [intrface]. ifr_addr.sa_data );
}
Else
{
Char STR [256];
Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );
Perror (STR );
}
/* This section can't get hardware address, I don't know whether the reason is module driver */
# Ifdef Solaris
// Obtain the MAC address
Arp. arp_pa.sa_family = af_inet;
Arp. arp_ha.sa_family = af_inet;
(Struct sockaddr_in *) & arp. arp_pa)-> sin_addr.s_addr = (struct sockaddr_in *) (& Buf [intrface]. ifr_addr)-> sin_addr.s_addr;
If (! (IOCTL (FD, siocgarp, (char *) & ARP )))
{
Puts ("HW address is :");
// Display the MAC address in hexadecimal format
Printf ("% 02x: % 02x: % 02x: % 02x: % 02x: % 02x/N ",
(Unsigned char) arp. arp_ha.sa_data [0],
(Unsigned char) arp. arp_ha.sa_data [1],
(Unsigned char) arp. arp_ha.sa_data [2],
(Unsigned char) arp. arp_ha.sa_data [3],
(Unsigned char) arp. arp_ha.sa_data [4],
(Unsigned char) arp. arp_ha.sa_data [5]);
Puts ("");
Puts ("");
}
# Else
# If 0
/* Get HW address of the net card */
If (! (IOCTL (FD, siocgenaddr, (char *) & Buf [intrface])
{
Puts ("HW address is :");
Printf ("% 02x: % 02x: % 02x: % 02x: % 02x: % 02x/N ",
(Unsigned char) BUF [intrface]. ifr_enaddr [0],
(Unsigned char) BUF [intrface]. ifr_enaddr [1],
(Unsigned char) BUF [intrface]. ifr_enaddr [2],
(Unsigned char) BUF [intrface]. ifr_enaddr [3],
(Unsigned char) BUF [intrface]. ifr_enaddr [4],
(Unsigned char) BUF [intrface]. ifr_enaddr [5]);
Puts ("");
Puts ("");
}
# Endif
If (! (IOCTL (FD, siocgifhwaddr, (char *) & Buf [intrface])
{
Puts ("HW address is :");
Printf ("% 02x: % 02x: % 02x: % 02x: % 02x: % 02x/N ",
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [0],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [1],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [2],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [3],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [4],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [5]);
Puts ("");
Puts ("");
}
# Endif
Else
{
Char STR [256];
Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );
Perror (STR );
}
} // While
} Else
Perror ("CPM: IOCTL ");
} Else
Perror ("CPM: socket ");
Close (FD );
Return retn;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.