How Linux obtains native MAC/IP addresses

Source: Internet
Author: User

Function: Query native IP/MAC address, filter out 127.0.0.1 loop-back address

For: Linux, Ubuntu 16.04 Debug Pass


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>



void Getlocalmacip ()

{

int i;
int Sock_mac;
Char hname[128];
struct Hostent *hent;
struct IN_ADDR *paddr = 0;
struct Ifaddrs * ifaddrstruct=null;
struct Ifreq Ifr_mac;
void * TMPADDRPTR=NULL;

struct Ifreq buf[16];
struct ifconf ifc;
int interface_num;
Char nif_name[128] = {0};
Char addressbuffer[128];

unsigned char macaddr[6] = {0};
unsigned char *pfirstbyte;

//---------------------------------------------
Get device IP address, discard loopback
//---------------------------------------------
GetHostName (hname, sizeof (hname));

Hent = gethostbyname (hname);

printf ("[%s][%d] Host:%s\n", __file__, __line__, hent->h_name);

Getifaddrs (&ifaddrstruct);

while (Ifaddrstruct!=null)
{
if (ifaddrstruct->ifa_addr->sa_family==af_inet)
{
Tmpaddrptr = & (struct sockaddr_in *) ifaddrstruct->ifa_addr)->sin_addr;
Pfirstbyte = (unsigned char*) tmpaddrptr;

if (*pfirstbyte! = 127)
{
PADDR = tmpaddrptr;
Inet_ntop (Af_inet, Tmpaddrptr, Addressbuffer, 128);
printf ("[%s][%d] NIF%s address%s\n", __file__, __line__,
Ifaddrstruct->ifa_name, Addressbuffer);
//---------------------------------------------
Remember NIF name for future use
//---------------------------------------------
strcpy (Nif_name, ifaddrstruct->ifa_name);
}

}

ifaddrstruct=ifaddrstruct->ifa_next;
}


//---------------------------------------------
Get device MAC address, discard loopback
//---------------------------------------------
Sock_mac = socket (af_inet, SOCK_DGRAM, 0);

if (Sock_mac > 0)
{
Ifc.ifc_len = sizeof (BUF);
Ifc.ifc_req = BUF;
IOCTL (Sock_mac, siocgifconf, (char *) &AMP;IFC);

Interface_num = ifc.ifc_len/sizeof (struct ifreq);

while (interface_num--)
{
if (Nif_name[0] && strcmp (Nif_name, Buf[interface_num].ifr_name))
{
Continue
}

IOCTL (Sock_mac, SIOCGIFHWADDR, (char *) &buf[interface_num]);

memcpy (MACADDR, &buf[interface_num].ifr_hwaddr.sa_data[0], 6);

printf ("NIF%s", buf[interface_num].ifr_name);
printf ("Mac%02x:%02x:%02x:%02x:%02x:%02x \ n",
Macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
}

Close (SOCK_MAC);
}
}

How Linux obtains native MAC/IP addresses

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.