LINUX下獲取網卡IP地址和MAC地址,子網掩碼程序參考

/*
mode time:20120727
LINUX下獲取IP地址和MAC地址.程序相關結構體在程序後面。
 打印網卡的ip地址 子網掩碼  廣播地址 mac地址
環境:
	[root@bogon temp]# uname -a
	Linux bogon 2.6.31.5-127.fc12.i686.PAE #1 SMP Sat Nov 7 21:25:57 EST 2009 i686 i686 i386 GNU/Linux
*/
#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> 
#ifdef SOLARIS 
#include <sys/sockio.h> 
#endif 
#define MAXINTERFACES 16 /* 最大接口數 */

int  main(int argc, char **argv)
{ 
	register int fd, intrface, retn = 0; 
	struct ifreq buf[MAXINTERFACES]; /* ifreq結構數組 */
	struct arpreq arp; 
	struct ifconf ifc; 
	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 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); 
			puts("");
			//根據藉口信息循環獲取設備IP和MAC地址
			while ( (intrface--) > 0) 
			{ 
				//獲取設備名稱
				printf ("net device %s\n", buf[intrface].ifr_name); 

				//判斷網卡類型 
				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); 
				} 
				//判斷網卡狀態 
				if (buf[intrface].ifr_flags & IFF_UP) 
				{ 
					puts("the interface status is UP"); 
				} 
				else 
				{ 
					puts("the interface status is DOWN"); 
				} 
				//獲取當前網卡的IP地址 
				if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface]))) 
				{ 
					printf("IP address is:"); 
					puts((char *)inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr)); 
					//printf("\n%d\n"buf[intrface].ifr_addr))->sin_addr.s_addr); 
					//puts (buf[intrface].ifr_addr.sa_data); 
				} 
				else 
				{ 
					char str[256]; 
					sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name); 
					perror (str); 
				} 
				/* this section can't get Hardware Address,I don't know whether the reason is module driver*/ 
		#ifdef SOLARIS 
				//獲取MAC地址
				arp.arp_pa.sa_family = AF_INET; 
				arp.arp_ha.sa_family = AF_INET; 
				((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr; 
				if (!(ioctl (fd, SIOCGARP, (char *) &arp))) 
				{ 
					printf("HW address is:"); 
					//以十六進制顯示MAC地址
					printf("%02x:%02x:%02x:%02x:%02x:%02x\n", 
							(unsigned char)arp.arp_ha.sa_data[0], 
							(unsigned char)arp.arp_ha.sa_data[1], 
							(unsigned char)arp.arp_ha.sa_data[2], 
							(unsigned char)arp.arp_ha.sa_data[3], 
							(unsigned char)arp.arp_ha.sa_data[4], 
							(unsigned char)arp.arp_ha.sa_data[5]); 
					puts(""); 
					puts(""); 
				} 

#else 
#if 0 
				/*Get HW ADDRESS of the net card */ 
				if (!(ioctl (fd, SIOCGENADDR, (char *) &buf[intrface]))) 
				{ 
					printf("HW address is:"); 
					printf("%02x:%02x:%02x:%02x:%02x:%02x\n", 
							(unsigned char)buf[intrface].ifr_enaddr[0], 
							(unsigned char)buf[intrface].ifr_enaddr[1], 
							(unsigned char)buf[intrface].ifr_enaddr[2], 
							(unsigned char)buf[intrface].ifr_enaddr[3], 
							(unsigned char)buf[intrface].ifr_enaddr[4], 
							(unsigned char)buf[intrface].ifr_enaddr[5]); 
					puts(""); 
				} 
#endif 
				if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface]))) 
				{ 
					printf("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]); 
				} 
#endif 
				else 
				{ 
					char str[256]; 
					sprintf (str, "cpm: ioctl device %s", buf[intrface].ifr_name); 
					perror (str); 
				} 
				//子網掩碼
				 if (!(ioctl(fd, SIOCGIFNETMASK, (char *) &buf[intrface])))
        {
	            printf("MASK:%s",
                    (char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));
              puts(""); 
        }
       else
        {
           char str[256];
            sprintf(str, "SIOCGIFADDR ioctl %s", buf[intrface].ifr_name);
            perror(str);
       }
       //廣播地址
       if (! (ioctl(fd, SIOCGIFBRDADDR, (char *) &buf[intrface])))
				printf("Broadcast Address:%s\n",
					(char*)inet_ntoa(((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));

			puts(""); 
			puts(""); 
			} //while
		} else 
			perror ("cpm: ioctl"); 
	} else 
		perror ("cpm: socket"); 
	close (fd); 
	return retn; 
}
/*
#include <sys/ioctl.h>
int ioctl(int fd, int request, … * void *arg *);
返回:成功返回0,失敗返回-1

char *inet_ntoa (struct in_addr);
  返回點分十進制的字符串在靜態內存中的指針。
  所在頭文件:<arpa/inet.h> 函數功能:將網絡地址轉換成“.”點隔的字符串格式。
  所需庫: winsock.h

#include<linux.h>
struct ifconf 
{
	int	ifc_len;			/* size of buffer	*
	union 
	{
		char __user *ifcu_buf;
		struct ifreq __user *ifcu_req;
	} ifc_ifcu;
};

struct ifreq 
{
#define IFHWADDRLEN	6
	union
	{
		char	ifrn_name[IFNAMSIZ];		* if name, e.g. "en0" *
	} ifr_ifrn;
	
	union {
		struct	sockaddr ifru_addr;
		struct	sockaddr ifru_dstaddr;
		struct	sockaddr ifru_broadaddr;
		struct	sockaddr ifru_netmask;
		struct  sockaddr ifru_hwaddr;
		short	ifru_flags;
		int	ifru_ivalue;
		int	ifru_mtu;
		struct  ifmap ifru_map;
		char	ifru_slave[IFNAMSIZ];	/* Just fits the size *
		char	ifru_newname[IFNAMSIZ];
		void __user *	ifru_data;
		struct	if_settings ifru_settings;
	} ifr_ifru;
};

#define ifr_name	ifr_ifrn.ifrn_name	/* interface name 	*
#define ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address 		*
#define	ifr_addr	ifr_ifru.ifru_addr	/* address		*
#define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-p lnk	*
#define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address	*
#define	ifr_netmask	ifr_ifru.ifru_netmask	/* interface net mask	*
#define	ifr_flags	ifr_ifru.ifru_flags	/* flags		*
#define	ifr_metric	ifr_ifru.ifru_ivalue	/* metric		*
#define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu			*
#define ifr_map		ifr_ifru.ifru_map	/* device map		
#define ifr_slave	ifr_ifru.ifru_slave	/* slave device		*
#define	ifr_data	ifr_ifru.ifru_data	/* for use by interface	*
#define ifr_ifindex	ifr_ifru.ifru_ivalue	/* interface index	
#define ifr_bandwidth	ifr_ifru.ifru_ivalue    /* link bandwidth	*
#define ifr_qlen	ifr_ifru.ifru_ivalue	/* Queue length 	*
#define ifr_newname	ifr_ifru.ifru_newname	/* New name		*
#define ifr_settings	ifr_ifru.ifru_settings	/* Device/proto settings*

*/
[root@bogon temp]# gcc -g -o get_ip get_ip.c
[root@bogon temp]# ./get_ip
interface num is intrface=2

net device eth1
the interface status is UP
IP address is:192.168.94.125
HW address is:00:0c:29:26:e4:f2
MASK:255.255.255.0
Broadcast Address:192.168.94.255


net device lo
the interface status is UP
IP address is:127.0.0.1
HW address is:00:00:00:00:00:00
MASK:255.0.0.0
Broadcast Address:0.0.0.0



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章