Linux obtains IP and NIC information for the machine

Source: Internet
Author: User
Tags sprintf

Linux obtains IP and NIC information for the machine

The code comes from the network, I rewrite, have the United States dare not self-specialized, special share. The method is very easy to use, with 3 functions.

Header file Getmac.h:

/** * getmac.h * * 2014-07-08:init created * * #ifndef getmac_h_included#define getmac_h_included#if defined (__cplusplus) E Xtern "C" {#endif # include <stdio.h> #include <string.h> #include <errno.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># Ifdef solaris# include <sys/sockio.h> #endif # define getmac_max_interfaces 16#define Getmac_max_msglen 256#defi Ne getmac_noerror 0#define getmac_success getmac_noerror#define getmac_error ( -1) #define GETMAC_EATTR ( -2) Typede    f int getmac_bool; #define Getmac_true 1#define getmac_false 0typedef struct{int fd;    struct Ifreq buf[getmac_max_interfaces];    struct Arpreq ARP; int ifaces;}    getmac_info_t;typedef struct{int Errcode; Char Errmsg[getmac_max_msglen];} getmac_error_t, #define Getmac_attr_iff_UP 1 */Interface is up */#define GETMAC_ATTR_IFF_BROADCAST 2//Broadcast address valid */#define Getmac_attr_iff_debug 3/* Turn on Debugging */#define GETMAC_ATTR_IFF_LOOPBACK 4/* is a LOOPBACK n  ET */#define GETMAC_ATTR_IFF_POINTOPOINT 5/* Interface is point-to-point link */#define Getmac_attr_iff_notrailers 6/* Avoid use of trailers */#define GETMAC_ATTR_IFF_RUNNING 7 */Resources Allocated */#define GETMAC_AT Tr_iff_noarp 8 */NO Address Resolution Protocol */#define GETMAC_ATTR_IFF_PROMISC 9/* Receive all  Packets */#define GETMAC_ATTR_IFNAME/* Interface name, e.g. "En0".  */#define GETMAC_ATTR_IPADDR/* Address of Interface */#define GETMAC_ATTR_HWADDR//MAC Address */extern int Getmac_init (getmac_info_t * mi, getmac_error_t * err); extern int getmac_attr (getmac_info_t * mi, I NT I, int attr, void * value, getmac_error_t * err); extern VOID Getmac_fini (getmac_info_t * mi); #if defined (__cplusplus)} #endif #endif/* getmac_h_included * * 

C File GETMAC.C:

/** * GETMAC.C * * 2014-07-08:init created * * #include "getmac.h" int getmac_init (getmac_info_t * mi, getmac_error_t * err    ) {int FD;    struct ifconf ifc;    Bzero (MI, sizeof (getmac_info_t));    MI-&GT;FD =-1;    FD = socket (af_inet, SOCK_DGRAM, 0);        if (fd = =-1) {err->errcode = errno;        snprintf (err->errmsg, Getmac_max_msglen, "socket () error (%d):%s", Strerror (errno));    return getmac_error;    }/* Prepare to get Mac numb */Ifc.ifc_len = sizeof (MI-&GT;BUF);    Ifc.ifc_buf = (caddr_t) mi->buf;        if (IOCTL (FD, siocgifconf, (char *) &ifc)) {err->errcode = errno;        snprintf (err->errmsg, Getmac_max_msglen, "IOCTL () error (%d):%s", Strerror (errno));        Close (FD);    return getmac_error;    } mi->ifaces = ifc.ifc_len/sizeof (struct ifreq);    MI-&GT;FD = FD; /* Return numb of Mac */return mi->ifaces;}   int getmac_attr (getmac_info_t * mi, int i, int attr, void * value, getmac_error_t * err) { if (MI-&GT;FD = =-1) {snprintf (err->errmsg, Getmac_max_msglen, "Getmac_init should be invoked first");    return getmac_error;  }/* Initialize if not a valid name */if (! mi->buf[i].ifr_name[0]) {if (IOCTL (MI-&GT;FD, Siocgifflags,            (char *) & Mi->buf[i]) {err->errcode = errno;              snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGIFFLAGS) error (%d):%s", Strerror (errno));        return getmac_error;            } if (IOCTL (MI-&GT;FD, SIOCGIFADDR, (char *) & Mi->buf[i])) {err->errcode = errno;              snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGIFADDR) error (%d):%s", Strerror (errno));        return getmac_error;            }/* Get Hardware Address */#ifdef SOLARIS do {mi->arp.arp_pa.sa_family = af_inet;            mi->arp.arp_ha.sa_family = af_inet; (struct sockaddr_in *) & MI-&GT;ARP.ARP_PA)->sIn_addr.s_addr = ((struct sockaddr_in*) (& buf[i].ifr_addr))->sin_addr.s_addr;                if (IOCTL (MI-&GT;FD, Siocgarp, (char *) & Mi->arp))) {Err->errcode = errno;                  snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGARP) error (%d):%s", Strerror (errno));            return getmac_error; }} while (0), #else #if 0 Do {/* get HW ADDRESS of the net card */if (IOCTL (mi-&                GT;FD, SIOCGENADDR, (char *) & Buf[i])) {err->errcode = errno;                  snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGENADDR) error (%d):%s", Strerror (errno));            return getmac_error;    }} while (0); #else do {if (IOCTL (MI-&GT;FD, SIOCGIFHWADDR, (char *) & Mi->buf[i])) {err->                Errcode = errno; snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGIFHWADDR) error (%d):%s", streRror (errno));            return getmac_error;    }} while (0);    #endif #endif} err->errcode = Getmac_noerror; /* Get NET attribute */switch (attr) {case GETMAC_ATTR_IFF_UP:/* Interface is up */if (mi->buf[i].i            Fr_flags & iff_up) {* ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "Interface is up");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "Interface is isn't up");        } break;             Case GETMAC_ATTR_IFF_BROADCAST:/* Broadcast address valid */if (Mi->buf[i].ifr_flags & Iff_broadcast) {            * ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "broadcast address valid");            } else {* ((Getmac_bool *) value) = Getmac_false; snprintf (err->errmsg, Getmac_max_msglen, "BroadcasT address invalid ");        } break; Case GETMAC_ATTR_IFF_DEBUG:/* Turn on Debugging */if (Mi->buf[i].ifr_flags & iff_debug) {*            ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "Turn on Debugging");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "Turn off debugging");        } break;            Case Getmac_attr_iff_loopback:/* is a LOOPBACK net */if (Mi->buf[i].ifr_flags & Iff_loopback) {            * ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "is a loopback net");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "not a loopback net");        } break; Case Getmac_attr_iff_pointopoint:/* Interface is point-to-point link */if (Mi->buf[i].ifr_flagS & iff_pointopoint) {* ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "Interface is point-to-point link");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "Interface is not point-to-point link");        } break;             Case Getmac_attr_iff_notrailers:/* Avoid use of trailers */if (Mi->buf[i].ifr_flags & iff_notrailers) {            * ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "Avoid use of trailers");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "not Avoid use of trailers");        } break;            Case getmac_attr_iff_running:/* Resources Allocated */if (Mi->buf[i].ifr_flags & iff_running) {            * ((Getmac_bool *) value) = Getmac_true; snprintf (erR->errmsg, Getmac_max_msglen, "Resources allocated");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "Resources not Allocated");        } break;            Case GETMAC_ATTR_IFF_NOARP:/* No Address Resolution Protocol */if (Mi->buf[i].ifr_flags & Iff_noarp) {            * ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "No Address Resolution Protocol");            } else {* ((Getmac_bool *) value) = Getmac_false;        snprintf (err->errmsg, Getmac_max_msglen, "has address Resolution Protocol");    } break;            Case GETMAC_ATTR_IFF_PROMISC:/* Receive ALL packets */if (Mi->buf[i].ifr_flags & Iff_promisc) {            * ((Getmac_bool *) value) = Getmac_true;        snprintf (err->errmsg, Getmac_max_msglen, "Receive all packets"); } else {* ((Getmac_bool *) value) = GETMac_false;        snprintf (err->errmsg, Getmac_max_msglen, "not a promisc mode");    } break;        Case Getmac_attr_ifname:/* Interface name, e.g. "en0" */strcpy (value, mi->buf[i].ifr_name);        snprintf (err->errmsg, Getmac_max_msglen, "Interface name");        Break Case GETMAC_ATTR_IPADDR:/* Interface Address */strcpy (value, Inet_ntoa ((struct sockaddr_in*) (& mi->        BUF[I].IFR_ADDR)) (-&GT;SIN_ADDR));        snprintf (err->errmsg, Getmac_max_msglen, "Interface address");        Break            Case GETMAC_ATTR_HWADDR: #ifdef the SOLARIS do {mi->arp.arp_pa.sa_family = af_inet;            mi->arp.arp_ha.sa_family = af_inet; (struct sockaddr_in *) & MI-&GT;ARP.ARP_PA)->sin_addr.s_addr = ((struct sockaddr_in*) (& buf[i].ifr_addr))-            >sin_addr.s_addr;                if (IOCTL (MI-&GT;FD, Siocgarp, (char *) & Mi->arp))) {Err->errcode = errno; snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGARP) error (%d):%s", Strerror (errno));            return getmac_error; } else {sprintf (value, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char) mi->arp.arp _ha.sa_data[0], (unsigned char) mi->arp.arp_ha.sa_data[1], (unsigned char) mi-&gt ; arp.arp_ha.sa_data[2], (unsigned char) mi->arp.arp_ha.sa_data[3], (unsigned char            ) mi->arp.arp_ha.sa_data[4], (unsigned char) mi->arp.arp_ha.sa_data[5]); }} while (0) and #else #if 0 do {sprintf ("%02x:%02x:%02x:%02x:%02x:%02x", (unsign  Ed Char) mi->buf[i].ifr_enaddr[0], (unsigned char) mi->buf[i].ifr_enaddr[1], (unsigned Char) mi->buf[i].ifr_enaddr[2], (unsigned char) mi->buf[i].ifr_enaddr[3], (unsigned c HAR) Mi->buF[I].IFR_ENADDR[4], (unsigned char) mi->buf[i].ifr_enaddr[5]);    } while (0); #else do {sprintf (value, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char) mi->buf[i]  . ifr_hwaddr.sa_data[0], (unsigned char) mi->buf[i].ifr_hwaddr.sa_data[1], (unsigned char) MI-&GT;BUF[I].IFR_HWADDR.SA_DATA[2], (unsigned char) mi->buf[i].ifr_hwaddr.sa_data[3], (        unsigned char) mi->buf[i].ifr_hwaddr.sa_data[4], (unsigned char) mi->buf[i].ifr_hwaddr.sa_data[5]);    } while (0);        #endif #endif snprintf (err->errmsg, Getmac_max_msglen, "MAC address");    Break        Default:err->errcode = getmac_eattr;        snprintf (err->errmsg, Getmac_max_msglen, "error attr specified");    return getmac_eattr; } return getmac_success;}        void Getmac_fini (getmac_info_t * mi) {if (mi->fd! =-1) {close (MI-&GT;FD); Mi-> fd =-1; }}


Linux obtains IP and NIC information for the machine

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.