当前位置: 技术问答>linux和unix
solaris 获取mac地址全0的问题??
来源: 互联网 发布时间:2016-10-17
本文导语: 本帖最后由 chygao 于 2010-08-30 10:24:16 编辑 在jpcap.jpcapCaptor.getDeviceList()在opensolaris和solaris8下获得的mac地址全0,不解,请高手指点!! /** Get Interface List **/ JNIEXPORT jobjectArray JNICALL Java_jpcap_JpcapCaptor_getDeviceList (JNIEnv...
/**
Get Interface List
**/
JNIEXPORT jobjectArray JNICALL Java_jpcap_JpcapCaptor_getDeviceList
(JNIEnv *env, jclass cl)
{
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_addr_t *a;
pcap_t *tmp_pcap;
char errbuf[PCAP_ERRBUF_SIZE];
int i=0,j=0,k=0;
jobjectArray devices=NULL;
jobjectArray addresses=NULL;
jobject device=NULL;
jobject address=NULL;
int linktype;
jstring lname,ldesc;
#ifdef WIN32
u_long size=0;
PIP_INTERFACE_INFO pInfo = NULL;
MIB_IFROW MibIfRow;
char **devnames;
char *p1,*p2,*p3;
#else
#ifdef SIOCGIFHWADDR // Linux
int sd;
struct ifreq ifr;
u_char buf[6];
#else //FreeBSD
struct ifaddrs *ifa, *ifa0;
struct sockaddr_dl* dl;
getifaddrs(&ifa0);
#endif
#endif
Interface=FindClass("jpcap/NetworkInterface");
deviceConstMID=(*env)->GetMethodID(env,Interface,"","(Ljava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;[B[Ljpcap/NetworkInterfaceAddress;)V");
IAddress=FindClass("jpcap/NetworkInterfaceAddress");
addressConstMID=(*env)->GetMethodID(env,IAddress,"","([B[B[B[B)V");
(*env)->ExceptionDescribe(env);
/* Retrieve the device list */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %sn", errbuf);
return NULL;
}
//count # of devices
for(i=0,d=alldevs;d;d=d->next,i++);
//create array
devices=(*env)->NewObjectArray(env,(jsize)i,Interface,NULL);
#ifdef WIN32
//obtain necessary size
GetInterfaceInfo(NULL, &size);
//allocate memory
pInfo = (PIP_INTERFACE_INFO) malloc (size);
if(GetInterfaceInfo(pInfo, &size)!=NO_ERROR){
Throw(IOException,"GetInterfaceInfo failed.");
return NULL;
}
#endif
/* Set Interface data */
for(i=0,d=alldevs;d;d=d->next)
{
jbyteArray mac=(*env)->NewByteArray(env,6);
//set mac
#ifdef WIN32
// compare the device names obtained from Pcap and from IP Helper
// in order to identify MAC address
// since device name differs in 9x and NT/XP, compare name
// from the end (not sure if this works in every case. I hope it does..)
p1=d->name;
while(*p1!=0) p1++; //find the end
//convert wchar to char
devnames=(char **)malloc(sizeof(char *)*pInfo->NumAdapters);
for(j=0;jNumAdapters;j++){
size=WideCharToMultiByte(0,0,pInfo->Adapter[j].Name,-1,NULL,0,NULL,NULL);
devnames[j]=(char *)malloc(size);
WideCharToMultiByte(0,0,pInfo->Adapter[j].Name,-1,devnames[j],size,NULL,NULL);
//printf("%sn",devnames[j]);
}
for(j=0;jNumAdapters;j++){
p2=p1;
p3=devnames[j];
while(*p3!=0) p3++; //find the end
k=0;
//printf("%s,%s:%dn",d->name,devnames[j],j);
while(*p2==*p3){
p2--; p3--; k++;
//printf("%c,%c,%dn",*p2,*p3,k);
}
if(kAdapter[j].Index;
GetIfEntry(&MibIfRow);
(*env)->SetByteArrayRegion(env,mac,0,MibIfRow.dwPhysAddrLen,MibIfRow.bPhysAddr);
}
#else
#ifdef SIOCGIFHWADDR //Linux
/* make socket */
sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sd