mirror of https://github.com/sipwise/jitsi.git
Add getHardwareAddress to the NetworkAddress service that wraps NetworkInterface.getHardwareAddress if it exists otherwise via a JNI.
parent
5fba65be8c
commit
fbaa48424b
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file HardwareAddressRetriever.h
|
||||
* \brief Hardware address retriever.
|
||||
* \author Sebastien Vincent
|
||||
* \date 2010
|
||||
*/
|
||||
|
||||
#ifndef HARDWARE_ADDRESS_RETRIEVER_H
|
||||
#define HARDWARE_ADDRESS_RETRIEVER_H
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
/**
|
||||
* \brief Returns the hardware address for the specified interface.
|
||||
* \param env JNI environment
|
||||
* \param ifName name of the interface that we want to get the hardware address
|
||||
* \return byte array representing hardware address or NULL if not found or
|
||||
* system related errors happen.
|
||||
*/
|
||||
jbyteArray getHardwareAddress(JNIEnv* env, jstring ifName);
|
||||
|
||||
#endif /* HARDWARE_ADDRESS_RETRIEVER_H */
|
||||
|
||||
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file HardwareAddressRetriever_unix.c
|
||||
* \brief Hardware address retriever (Unix specific code).
|
||||
* \author Sebastien Vincent
|
||||
* \date 2010
|
||||
*/
|
||||
|
||||
#if !defined(_WIN32) && !defined(_WIN64)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <net/if.h>
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__APPLE__)
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/route.h>
|
||||
#include <net/if_types.h>
|
||||
#endif
|
||||
|
||||
#include "HardwareAddressRetriever.h"
|
||||
|
||||
jbyteArray getHardwareAddress(JNIEnv* env, jstring ifName)
|
||||
{
|
||||
int sock = -1;
|
||||
struct ifreq ifr;
|
||||
jbyteArray hwaddr = NULL;
|
||||
char* name = NULL;
|
||||
jbyte* addr = NULL;
|
||||
int hwlen = 6;
|
||||
|
||||
name = (char*)(*env)->GetStringUTFChars(env, ifName, NULL);
|
||||
|
||||
if(!name)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
if(sock == -1)
|
||||
{
|
||||
(*env)->ReleaseStringUTFChars(env, ifName, name);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(&ifr, 0x00, sizeof(struct ifreq));
|
||||
|
||||
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
|
||||
ifr.ifr_name[IFNAMSIZ - 1] = 0x00;
|
||||
|
||||
if(ioctl(sock, SIOCGIFHWADDR, &ifr) != 0)
|
||||
{
|
||||
(*env)->ReleaseStringUTFChars(env, ifName, name);
|
||||
close(sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
close(sock);
|
||||
addr = (const jbyte*)ifr.ifr_hwaddr.sa_data;
|
||||
|
||||
#else /* BSD like */
|
||||
|
||||
struct ifaddrs* addrs = NULL;
|
||||
struct ifaddrs* ifa = NULL;
|
||||
jbyte buf[hwlen];
|
||||
|
||||
if(getifaddrs(&addrs) != -1)
|
||||
{
|
||||
for(ifa = addrs ; ifa != NULL ; ifa = ifa->ifa_next)
|
||||
{
|
||||
if(ifa->ifa_addr->sa_family == AF_LINK && !strcmp(ifa->ifa_name, name))
|
||||
{
|
||||
struct sockaddr_dl* sdl = (struct sockaddr_dl*)ifa->ifa_addr;
|
||||
|
||||
if(sdl->sdl_type == IFT_ETHER)
|
||||
{
|
||||
memcpy(buf, LLADDR(sdl), hwlen);
|
||||
addr = buf;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
freeifaddrs(addrs);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if(addr)
|
||||
{
|
||||
hwaddr = (*env)->NewByteArray(env, hwlen);
|
||||
|
||||
if(hwaddr)
|
||||
{
|
||||
/* copy the hardware address and return it */
|
||||
(*env)->SetByteArrayRegion(env, hwaddr, 0, hwlen, addr);
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
(*env)->ReleaseStringUTFChars(env, ifName, name);
|
||||
return hwaddr;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file HardwareAddressRetriever_win.c
|
||||
* \brief Hardware address retriever (Windows specific code).
|
||||
* \author Sebastien Vincent
|
||||
* \date 2010
|
||||
*/
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
/**
|
||||
* \def WIN32_LEAN_AND_MEAN
|
||||
* \brief Exclude not commonly used headers from win32 API.
|
||||
*
|
||||
* It excludes some unused stuff from windows headers and
|
||||
* by the way code compiles faster.
|
||||
*/
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include <ws2tcpip.h>
|
||||
#include <iphlpapi.h>
|
||||
|
||||
#include "HardwareAddressRetriever.h"
|
||||
|
||||
jbyteArray getHardwareAddress(JNIEnv* env, jstring ifName)
|
||||
{
|
||||
MIB_IFTABLE* ifTable = NULL;
|
||||
ULONG size = 15000;
|
||||
int found = 0;
|
||||
jbyteArray hwaddr = NULL;
|
||||
DWORD ret = 0;
|
||||
DWORD i = 0;
|
||||
WCHAR* wname = NULL;
|
||||
MIB_IFROW ifi;
|
||||
jclass clazz = (*env)->GetObjectClass(env, ifName);
|
||||
jmethodID method = (*env)->GetMethodID(env, clazz, "compareTo", "(Ljava/lang/String;)I");
|
||||
|
||||
if(method == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(&ifi, 0x00, sizeof(MIB_IFROW));
|
||||
|
||||
do
|
||||
{
|
||||
ifTable = malloc(size);
|
||||
|
||||
if(!ifTable)
|
||||
{
|
||||
/* out of memory */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = GetIfTable(ifTable, &size, 1);
|
||||
|
||||
}while(ret == ERROR_INSUFFICIENT_BUFFER);
|
||||
|
||||
if(ret != ERROR_SUCCESS)
|
||||
{
|
||||
free(ifTable);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(i = 0 ; i < ifTable->dwNumEntries ; i++)
|
||||
{
|
||||
jstring tmp = NULL;
|
||||
ifi = ifTable->table[i];
|
||||
|
||||
if(ifi.dwType == IF_TYPE_OTHER)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
/* jstring created by NewStringUTF will be garbage collected at
|
||||
* the end of the function
|
||||
*/
|
||||
tmp = (*env)->NewStringUTF(env, ifi.bDescr);
|
||||
|
||||
if(!tmp)
|
||||
{
|
||||
/* printf("error\n"); */
|
||||
continue;
|
||||
}
|
||||
|
||||
if((*env)->CallIntMethod(env, ifName, method, tmp) == 0)
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(found)
|
||||
{
|
||||
DWORD hwlen = ifi.dwPhysAddrLen;
|
||||
|
||||
if(hwlen > 0)
|
||||
{
|
||||
hwaddr = (*env)->NewByteArray(env, hwlen);
|
||||
|
||||
if(hwaddr)
|
||||
{
|
||||
/* copy the hardware address and return it */
|
||||
(*env)->SetByteArrayRegion(env, hwaddr, 0, hwlen, ifi.bPhysAddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* cleanup */
|
||||
free(ifTable);
|
||||
|
||||
return hwaddr;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever.h
|
||||
* \brief Hardware address retriever.
|
||||
* \author Sebastien Vincent
|
||||
* \date 2010
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "HardwareAddressRetriever.h"
|
||||
|
||||
/**
|
||||
* \brief Returns the byte array representing hardware address.
|
||||
* \param env JVM environment
|
||||
* \param clazz Java class
|
||||
* \param ifName name of the interface
|
||||
* \return byte array representing the hardware address or NULL if anything
|
||||
* goes wrong
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL Java_net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever_getHardwareAddress
|
||||
(JNIEnv* env, jclass clazz, jstring ifName)
|
||||
{
|
||||
return getHardwareAddress(env, ifName);
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever */
|
||||
|
||||
#ifndef _Included_net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever
|
||||
#define _Included_net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever
|
||||
* Method: getHardwareAddress
|
||||
* Signature: (Ljava/lang/String;)[B
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL Java_net_java_sip_communicator_impl_netaddr_HardwareAddressRetriever_getHardwareAddress
|
||||
(JNIEnv *, jclass, jstring);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
|
||||
*
|
||||
* Distributable under LGPL license.
|
||||
* See terms of license at gnu.org.
|
||||
*/
|
||||
package net.java.sip.communicator.impl.netaddr;
|
||||
|
||||
/**
|
||||
* Class to retrieve hardware address of a specific interface.
|
||||
*
|
||||
* We know that starting Java 6, NetworkInterface has getHardwareAddress method
|
||||
* but as we still support Java 5 we have to do it ourself.
|
||||
*
|
||||
* @author Sebastien Vincent
|
||||
*/
|
||||
public class HardwareAddressRetriever
|
||||
{
|
||||
/* load library */
|
||||
static
|
||||
{
|
||||
System.loadLibrary("hwaddressretriever");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hardware address of a particular interface.
|
||||
*
|
||||
* @param ifName name of the interface
|
||||
* @return byte array representing the hardware address of the interface or
|
||||
* null if interface is not found or other system related errors
|
||||
*/
|
||||
public static native byte[] getHardwareAddress(String ifName);
|
||||
}
|
||||
Loading…
Reference in new issue