std::string CNetAdapter::getMacAddr(void) { long stat; u_char addr[6]; stat = mac_addr_sys( addr); if (0 == stat) { std::stringstream mac_tmp; for(int i=0;i<6;++i) mac_tmp<<std::hex<<std::setfill('0')<<std::setw(2)<<(int)addr[i]<<':'; return mac_tmp.str().substr(0,17); } else { return "not found"; } }
std::string CNetAdapter::getMacAddr(void) { long stat; u_char addr[6]; stat = mac_addr_sys( addr); if (0 == stat) { std::stringstream mac_tmp; for(int i=0;i<6;++i) mac_tmp<<std::hex<<std::setfill('0')<<std::setw(2)<<(int)addr[i]<<':'; return mac_tmp.str().substr(0,17); } else { printf("[neutrino] eth-id not detected...\n"); return ""; } }
/* * Class: org_jnetpcap_PcapUtils * Method: getHardwareAddress * Signature: (Ljava/lang/String;)[B */ JNIEXPORT jbyteArray JNICALL Java_org_jnetpcap_PcapUtils_getHardwareAddress (JNIEnv *env, jclass clazz, jstring jdevice) { #ifndef IFNAMSIZ #define IFNAMSIZ 512 #endif jbyteArray jba = NULL; char buf[IFNAMSIZ]; // convert from jstring to char * toCharArray(env, jdevice, buf); #ifdef WIN32 PIP_INTERFACE_INFO info = getIpInterfaceInfo(); if (info == NULL) { throwException(env, IO_EXCEPTION, "unable to retrieve interface info"); return NULL; } for (int i = 0; i < info->NumAdapters; i ++) { PIP_ADAPTER_INDEX_MAP map = &info->Adapter[i]; /* * Name is in wide character format. So convert to plain UTF8. */ int size=WideCharToMultiByte(0, 0, map->Name, -1, NULL, 0, NULL, NULL); char utf8[size + 1]; WideCharToMultiByte(0, 0, map->Name, -1, utf8, size, NULL, NULL); #ifdef DEBUG printf("#%d name=%s buf=%s\n", i, utf8, buf); fflush(stdout); #endif char *p1 = strchr(utf8, '{'); char *p2 = strchr(buf, '{'); if(p1 == NULL || p2 == NULL) { p1 = utf8; p2 = buf; } if (strcmp(p1, p2) == 0) { PMIB_IFROW row = getMibIfRow(map->Index); #ifdef DEBUG printf("FOUND index=%d len=%d\n", map->Index, row->dwPhysAddrLen); fflush(stdout); #endif jba = env->NewByteArray((jsize) row->dwPhysAddrLen); env->SetByteArrayRegion(jba, (jsize) 0, (jsize) row->dwPhysAddrLen, (jbyte *)row->bPhysAddr); free(row); } } free(info); #else u_char mac[6]; // MAC address is 6 bytes #if defined(Linux) || defined(HPUX) || defined(AIX) || defined(DARWIN) || \ defined(FREE_BSD) || defined(NET_BSD) || defined(OPEN_BSD) mac_addr_sys(buf, mac); #else mac_addr_dlpi(buf, mac); #endif jba = env->NewByteArray((jsize) 6); env->SetByteArrayRegion(jba, 0, 6, (jbyte *)mac); #endif return jba; }