Linux obtains IP and NIC information for the machine

Source: Internet
Author: User
Tags get ip 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 usage is simple, just 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_success 0#define getmac_error ( -1) #define GETMAC_EATTR ( -2) typedef 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_ifname 0#define Getmac_attr_is_promiSC 1#define getmac_attr_is_up 2#define getmac_attr_ipaddr 3#define getmac_attr_hdaddr 4extern int getmac_  Init (getmac_info_t * mi, getmac_error_t * err), extern int getmac_attr (getmac_info_t * mi, int i, int attr, void * value, getmac_error_t * err), extern void Getmac_fini (getmac_info_t * mi), #if defined (__cplusplus)} #endif #endif/* getmac_h_inc luded * *

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; } if (attr = = Getmac_attr_is_promisc | | attr = = GETMAC_ATTR_IS_UP | | attr = getmac_attr_ifname) {if (! mi-> Buf[i].ifr_name[0]) {if (IOCTL (MI-&GT;FD, Siocgifflags, (char *) & Mi->buf[i])) {err-&                Gt;errcode = errno;                  snprintf (err->errmsg, Getmac_max_msglen, "IOCTL (SIOCGIFFLAGS) error (%d):%s", Strerror (errno));            return getmac_error;            }} if (attr = = Getmac_attr_is_promisc) {/* Whether the net card status is Promisc */            if (Mi->buf[i].ifr_flags & Iff_promisc) {* ((Getmac_bool *) value) = Getmac_true;            } else {* ((Getmac_bool *) value) = Getmac_false;         }} else if (attr = = getmac_attr_is_up) {/* Whether the net card status is up */   if (Mi->buf[i].ifr_flags & iff_up) {* ((Getmac_bool *) value) = Getmac_true;            } else {* ((Getmac_bool *) value) = Getmac_false;        }} else {/* device name */strcpy (value, mi->buf[i].ifr_name);    } return getmac_success; } else if (attr = = Getmac_attr_ipaddr | | attr = = GETMAC_ATTR_HDADDR) {/* Get IP address of the net card */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; } if (attr = = getmac_attr_ipaddr) {strcpy (Value, Inet_ntoa ((struct sockaddr_in*) (& Mi->buf[i            ].IFR_ADDR)) (-&GT;SIN_ADDR));        return getmac_success; } else {/* 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*) (& BU                F[INTRFACE].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-&gt ; arp.arp_ha.sa_data[0], (unsigned char) mi->arp.arp_ha.sa_data[1], (unsig                        Ned Char) mi->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]);                return getmac_success;                }} 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; } sprintf ("%02x:%02x:%02x:%02x:%02x:%02x", (unsigned 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 char) m                I-&GT;BUF[I].IFR_ENADDR[4], (unsigned char) mi->buf[i].ifr_enaddr[5]); Return GETMAC_SUCCESS;    } 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; } sprintf (Value, "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char) Mi->buf[i].ifr_hwa  Ddr.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]);        return getmac_success;    } while (0);   #endif #endif}} else {err->errcode = getmac_eattr;     snprintf (err->errmsg, Getmac_max_msglen, "error attr specified");        return getmac_eattr;        }}void Getmac_fini (getmac_info_t * mi) {if (mi->fd! =-1) {close (MI-&GT;FD);    MI-&GT;FD =-1; }}



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.