網路IP和MAC位置
Code:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/netdevice.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#define BOOL int
#define TRUE 0
#define FALSE -1
#define MAX_LONG_LENGTH 1024
#define MAX_SHORT_LENGTH 128
BOOL get_mac_address_fun(char *name, char *mac)
{
BOOL ret = FALSE;
struct ifreq s;
BOOL fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
strcpy(s.ifr_name, name);
if (0 == ioctl(fd, SIOCGIFHWADDR, &s))
{
sprintf(mac, "%02x-%02x-%02x-%02x-%02x-%02x",
(unsigned char) s.ifr_addr.sa_data[0],
(unsigned char) s.ifr_addr.sa_data[1],
(unsigned char) s.ifr_addr.sa_data[2],
(unsigned char) s.ifr_addr.sa_data[3],
(unsigned char) s.ifr_addr.sa_data[4],
(unsigned char) s.ifr_addr.sa_data[5]);
ret = TRUE;
}
return ret;
}
BOOL get_ip_address_fun(char *name, char *ip)
{
int fd;
BOOL ret = FALSE;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd)
{
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, name, IFNAMSIZ-1);
ioctl(fd, SIOCGIFADDR, &ifr);
strcpy(ip, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
close(fd);
ret = TRUE;
}
return ret;
}
int main(int argc, char *argv[])
{
char ip[MAX_SHORT_LENGTH];
char mac[MAX_SHORT_LENGTH];
get_ip_address_fun("eth0", ip);
get_mac_address_fun("eth0", mac);
printf("ip : %s, mac : %s\n", ip, mac);
return 0;
}
Result:
ip : 192.168.1.100, mac : 00-00-00-55-55-55
沒有留言:
張貼留言