当前位置: 技术问答>linux和unix
如何在sco openserver中通过c程序获得mac地址??
来源: 互联网 发布时间:2015-06-07
本文导语: 一定要c代码,谢谢,急! | 获得mac地址需要跟链路层会话。一般的SRV4系统使用DLPI接口。BSD系统使用ioctl 。我没用过SVR4系统,但我记得它支持DLPI的。下面是在solaris下通过dlpi接口取得MAC地址...
一定要c代码,谢谢,急!
|
获得mac地址需要跟链路层会话。一般的SRV4系统使用DLPI接口。BSD系统使用ioctl
。我没用过SVR4系统,但我记得它支持DLPI的。下面是在solaris下通过dlpi接口取得MAC地址的例子,你试试吧。
#include
#include
#include
#include
#include
unsigned long buf[0x1000];
dl_attach_req_t *atreq=(dl_attach_req_t *)buf;
dl_phys_addr_req_t *adreq=(dl_phys_addr_req_t *)buf;
dl_phys_addr_ack_t *adack=(dl_phys_addr_ack_t *)buf;
main()
{
int i;
int fd;
int flag=0;
unsigned char *p;
struct strbuf sb;
struct pollfd fds[1];
sb.buf=(char *)buf;
sb.maxlen=sizeof(buf);
/* Open device */
fd=open("/dev/hme", O_RDWR);
if (fddl_primitive=DL_ATTACH_REQ;
atreq->dl_ppa=0;
putmsg(fd, &sb, NULL, 0);
poll(fds, 1, 60000);
getmsg(fd, &sb, NULL, &flag);
/* Get physical address */
sb.len=sizeof(dl_phys_addr_req_t);
adreq->dl_primitive=DL_PHYS_ADDR_REQ;
adreq->dl_addr_type=DL_CURR_PHYS_ADDR;
putmsg(fd, &sb, NULL, 0);
poll(fds, 1, 60000);
sb.len=0;
getmsg(fd, &sb, NULL, &flag);
/* Close device */
close(fd);
/* Output */
printf("Physical Address: ", p[i]);
p=(unsigned char *)adack+adack->dl_addr_offset;
for (i=0; idl_addr_length; i++)
{
printf("%02X", p[i]);
}
printf("n");
return(0);
}
。我没用过SVR4系统,但我记得它支持DLPI的。下面是在solaris下通过dlpi接口取得MAC地址的例子,你试试吧。
#include
#include
#include
#include
#include
unsigned long buf[0x1000];
dl_attach_req_t *atreq=(dl_attach_req_t *)buf;
dl_phys_addr_req_t *adreq=(dl_phys_addr_req_t *)buf;
dl_phys_addr_ack_t *adack=(dl_phys_addr_ack_t *)buf;
main()
{
int i;
int fd;
int flag=0;
unsigned char *p;
struct strbuf sb;
struct pollfd fds[1];
sb.buf=(char *)buf;
sb.maxlen=sizeof(buf);
/* Open device */
fd=open("/dev/hme", O_RDWR);
if (fddl_primitive=DL_ATTACH_REQ;
atreq->dl_ppa=0;
putmsg(fd, &sb, NULL, 0);
poll(fds, 1, 60000);
getmsg(fd, &sb, NULL, &flag);
/* Get physical address */
sb.len=sizeof(dl_phys_addr_req_t);
adreq->dl_primitive=DL_PHYS_ADDR_REQ;
adreq->dl_addr_type=DL_CURR_PHYS_ADDR;
putmsg(fd, &sb, NULL, 0);
poll(fds, 1, 60000);
sb.len=0;
getmsg(fd, &sb, NULL, &flag);
/* Close device */
close(fd);
/* Output */
printf("Physical Address: ", p[i]);
p=(unsigned char *)adack+adack->dl_addr_offset;
for (i=0; idl_addr_length; i++)
{
printf("%02X", p[i]);
}
printf("n");
return(0);
}
|
up
|
http://chinaunix.net/forum/viewtopic.php?t=85716
|
实时下面的代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
unsigned char mac[6];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(mac, 0, sizeof(mac));
if (ioctl(sock, SIOCGIFHWADDR, &ifr)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ETH_NAME "eth0"
int main()
{
int sock;
struct sockaddr_in sin;
struct sockaddr sa;
struct ifreq ifr;
unsigned char mac[6];
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
memset(mac, 0, sizeof(mac));
if (ioctl(sock, SIOCGIFHWADDR, &ifr)