设为首页 加入收藏

TOP

Linux 获得机器的IP和网卡信息(一)
2014-11-23 21:40:04 来源: 作者: 【 】 浏览:49
Tags:Linux 获得 机器 网卡 信息

Linux 获得机器的IP和网卡信息代码来自于网络,我改写了,有美不敢自专,特分享之。用法很简单,就3个函数。


头文件getmac.h:


/**
* getmac.h
*
* 2014-07-08: init created
*/
#ifndef GETMAC_H_INCLUDED
#define GETMAC_H_INCLUDED


#if defined(__cplusplus)
extern "C" {
#endif


#include
#include
#include


#include
#include


#include
#include
#include
#include
#include
#include
#include



#ifdef SOLARIS
# include
#endif


#define GETMAC_MAX_INTERFACES 16


#define GETMAC_MAX_MSGLEN 256



#define GETMAC_NOERROR 0
#define GETMAC_SUCCESS GETMAC_NOERROR
#define GETMAC_ERROR (-1)
#define GETMAC_EATTR (-2)



typedef int GETMAC_BOOL;


#define GETMAC_TRUE 1
#define GETMAC_FALSE 0



typedef 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 net */
#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_ATTR_IFF_NOARP 8 /* No address resolution protocol */
#define GETMAC_ATTR_IFF_PROMISC 9 /* Receive all packets */


#define GETMAC_ATTR_IFNAME 10 /* Interface name, e.g. "en0". */
#define GETMAC_ATTR_IPADDR 11 /* Address of interface */
#define GETMAC_ATTR_HWADDR 12 /* MAC address */



extern 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_INCLUDED */



C文件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->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->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->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->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->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->fd, SIOCGIFADDR, (char *) & mi->buf[i])) {
err->errcode = errno;
snprintf (err->errmsg, GETMAC_MAX

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇使用 Protocol Buffers 代替 JSON.. 下一篇理解 Linux 条件变量

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: