How to obtain the MAC address and IP address of the Local Machine (without gethostbyname)

Source: Internet
Author: User
Tags get ip

/*
* Test. cpp
*
* Created on: 2011-10-26
* Author: Root
*/

# 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>
# Include <ARPA/inet. h>
# Include <unistd. h>

# Define maxinterfaces 16

Int main (INT argc, char ** argv)
{
Int FD, intrface, retn = 0;
Struct ifreq Buf [maxinterfaces];
Struct arpreq ARP;
Struct ifconf IFC;

FD = socket (af_inet, sock_dgram, 0 );

If (FD> = 0)
{
IFC. ifc_len = sizeof (BUF );
IFC. ifc_buf = (caddr_t) BUF;

If (! IOCTL (FD, siocgifconf, (char *) & IFC ))
{
Intrface = IFC. ifc_len/sizeof (struct ifreq );
Printf ("interface num is intrface = % d \ n", intrface );

While (intrface --> 0)
{
Printf ("net device % s \ n", Buf [intrface]. ifr_name );
/* Jugde whether the net card status is promisc */
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 );
}

/* Jugde whether the net card status is up */
If (BUF [intrface]. ifr_flags & iff_up)
{
Puts ("the interface status is up ");
} Else
{
Puts ("the interface status is down ");
}

/* Get IP of the net card */
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 );
}

/* Get HW address of the net card */
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 ("");
} Else
{
Char STR [256];

Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );

Perror (STR );
}
}
} Else
Perror ("CPM: IOCTL ");
} Else
Perror ("CPM: socket ");

Close (FD );
Return retn;
}

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.