< prev index next >
src/solaris/native/java/net/NetworkInterface.c
Print this page
rev 11915 : 8163181: Further improvements for Unix NetworkInterface native implementation
Reviewed-by: chegar, msheppar
*** 78,87 ****
--- 78,93 ----
#define SIOCGLIFHWADDR _IOWR('i', 192, struct lifreq)
#endif
#define DEV_PREFIX "/dev/"
#endif
+ #ifdef LIFNAMSIZ
+ #define IFNAMESIZE LIFNAMSIZ
+ #else
+ #define IFNAMESIZE IFNAMSIZ
+ #endif
+
#define CHECKED_MALLOC3(_pointer, _type, _size) \
do { \
_pointer = (_type)malloc(_size); \
if (_pointer == NULL) { \
JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed"); \
*** 161,171 ****
static short translateIPv4AddressToPrefix(struct sockaddr_in *addr);
static short translateIPv6AddressToPrefix(struct sockaddr_in6 *addr);
static int getIndex(int sock, const char *ifname);
static int getFlags(int sock, const char *ifname, int *flags);
! static int getMacAddress(JNIEnv *env, int sock, const char *ifname,
const struct in_addr *addr, unsigned char *buf);
static int getMTU(JNIEnv *env, int sock, const char *ifname);
#if defined(__solaris__)
static int getMacFromDevice(JNIEnv *env, const char *ifname,
--- 167,177 ----
static short translateIPv4AddressToPrefix(struct sockaddr_in *addr);
static short translateIPv6AddressToPrefix(struct sockaddr_in6 *addr);
static int getIndex(int sock, const char *ifname);
static int getFlags(int sock, const char *ifname, int *flags);
! static int getMacAddress(JNIEnv *env, const char *ifname,
const struct in_addr *addr, unsigned char *buf);
static int getMTU(JNIEnv *env, int sock, const char *ifname);
#if defined(__solaris__)
static int getMacFromDevice(JNIEnv *env, const char *ifname,
*** 254,277 ****
netif *ifs, *curr;
jboolean isCopy;
const char* name_utf;
jobject obj = NULL;
! ifs = enumInterfaces(env);
! if (ifs == NULL) {
return NULL;
}
- name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! freeif(ifs);
! return NULL;
}
! // Search the list of interface based on name
curr = ifs;
while (curr != NULL) {
if (strcmp(name_utf, curr->name) == 0) {
break;
}
--- 260,288 ----
netif *ifs, *curr;
jboolean isCopy;
const char* name_utf;
jobject obj = NULL;
! if (name != NULL) {
! name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
! } else {
! JNU_ThrowNullPointerException(env, "network interface name is NULL");
return NULL;
}
if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return NULL;
! }
!
! ifs = enumInterfaces(env);
! if (ifs == NULL) {
! return NULL;
}
! // search the list of interfaces based on name
curr = ifs;
while (curr != NULL) {
if (strcmp(name_utf, curr->name) == 0) {
break;
}
*** 308,318 ****
ifs = enumInterfaces(env);
if (ifs == NULL) {
return NULL;
}
! // Search the list of interface based on index
curr = ifs;
while (curr != NULL) {
if (index == curr->index) {
break;
}
--- 319,329 ----
ifs = enumInterfaces(env);
if (ifs == NULL) {
return NULL;
}
! // search the list of interfaces based on index
curr = ifs;
while (curr != NULL) {
if (index == curr->index) {
break;
}
*** 322,332 ****
--- 333,345 ----
// if found create a NetworkInterface
if (curr != NULL) {
obj = createNetworkInterface(env, curr);
}
+ // release the interface list
freeif(ifs);
+
return obj;
}
/*
* Class: java_net_NetworkInterface
*** 335,351 ****
*/
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
(JNIEnv *env, jclass cls, jobject iaObj)
{
netif *ifs, *curr;
-
#if defined(AF_INET6)
int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
#else
int family = AF_INET;
#endif
-
jobject obj = NULL;
jboolean match = JNI_FALSE;
ifs = enumInterfaces(env);
if (ifs == NULL) {
--- 348,362 ----
*** 360,378 ****
while (addrP != NULL) {
if (family == addrP->family) {
if (family == AF_INET) {
int address1 = htonl(
! ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
int address2 = getInetAddress_addr(env, iaObj);
if (address1 == address2) {
match = JNI_TRUE;
break;
}
}
-
#if defined(AF_INET6)
if (family == AF_INET6) {
jbyte *bytes = (jbyte *)&(
((struct sockaddr_in6*)addrP->addr)->sin6_addr);
jbyte caddr[16];
--- 371,388 ----
while (addrP != NULL) {
if (family == addrP->family) {
if (family == AF_INET) {
int address1 = htonl(
! ((struct sockaddr_in *)addrP->addr)->sin_addr.s_addr);
int address2 = getInetAddress_addr(env, iaObj);
if (address1 == address2) {
match = JNI_TRUE;
break;
}
}
#if defined(AF_INET6)
if (family == AF_INET6) {
jbyte *bytes = (jbyte *)&(
((struct sockaddr_in6*)addrP->addr)->sin6_addr);
jbyte caddr[16];
*** 408,418 ****
--- 418,430 ----
// if found create a NetworkInterface
if (match) {
obj = createNetworkInterface(env, curr);
}
+ // release the interface list
freeif(ifs);
+
return obj;
}
/*
* Class: java_net_NetworkInterface
*** 429,439 ****
ifs = enumInterfaces(env);
if (ifs == NULL) {
return NULL;
}
! // count the interface
ifCount = 0;
curr = ifs;
while (curr != NULL) {
ifCount++;
curr = curr->next;
--- 441,451 ----
ifs = enumInterfaces(env);
if (ifs == NULL) {
return NULL;
}
! // count the interfaces
ifCount = 0;
curr = ifs;
while (curr != NULL) {
ifCount++;
curr = curr->next;
*** 444,455 ****
if (netIFArr == NULL) {
freeif(ifs);
return NULL;
}
! // Iterate through the interfaces, create a NetworkInterface instance
! // for each array element and populate the object.
curr = ifs;
arr_index = 0;
while (curr != NULL) {
jobject netifObj;
--- 456,467 ----
if (netIFArr == NULL) {
freeif(ifs);
return NULL;
}
! // iterate through the interfaces, create a NetworkInterface instance
! // for each array element and populate the object
curr = ifs;
arr_index = 0;
while (curr != NULL) {
jobject netifObj;
*** 463,473 ****
--- 475,487 ----
(*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);
curr = curr->next;
}
+ // release the interface list
freeif(ifs);
+
return netIFArr;
}
/*
* Class: java_net_NetworkInterface
*** 529,578 ****
jbyte caddr[4];
struct in_addr iaddr;
jbyteArray ret = NULL;
unsigned char mac[16];
int len;
- int sock;
jboolean isCopy;
! const char* name_utf;
! name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
! if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return NULL;
}
! if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
! (*env)->ReleaseStringUTFChars(env, name, name_utf);
! return NULL;
}
if (!IS_NULL(addrArray)) {
! (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
! addr = ((caddr[0]<<24) & 0xff000000);
! addr |= ((caddr[1] <<16) & 0xff0000);
! addr |= ((caddr[2] <<8) & 0xff00);
! addr |= (caddr[3] & 0xff);
! iaddr.s_addr = htonl(addr);
! len = getMacAddress(env, sock, name_utf, &iaddr, mac);
} else {
! len = getMacAddress(env, sock, name_utf, NULL, mac);
}
if (len > 0) {
! ret = (*env)->NewByteArray(env, len);
! if (IS_NULL(ret)) {
! /* we may have memory to free at the end of this */
! goto fexit;
! }
! (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
}
- fexit:
- // release the UTF string and interface list
- (*env)->ReleaseStringUTFChars(env, name, name_utf);
! close(sock);
! return ret;
}
/*
* Class: java_net_NetworkInterface
* Method: getMTU0
--- 543,591 ----
jbyte caddr[4];
struct in_addr iaddr;
jbyteArray ret = NULL;
unsigned char mac[16];
int len;
jboolean isCopy;
! const char *name_utf;
! if (name != NULL) {
! name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
! } else {
! JNU_ThrowNullPointerException(env, "network interface name is NULL");
! return NULL;
}
!
! if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return NULL;
}
if (!IS_NULL(addrArray)) {
! (*env)->GetByteArrayRegion(env, addrArray, 0, 4, caddr);
! addr = ((caddr[0]<<24) & 0xff000000);
! addr |= ((caddr[1] <<16) & 0xff0000);
! addr |= ((caddr[2] <<8) & 0xff00);
! addr |= (caddr[3] & 0xff);
! iaddr.s_addr = htonl(addr);
! len = getMacAddress(env, name_utf, &iaddr, mac);
} else {
! len = getMacAddress(env, name_utf, NULL, mac);
}
+
if (len > 0) {
! ret = (*env)->NewByteArray(env, len);
! if (!IS_NULL(ret)) {
! (*env)->SetByteArrayRegion(env, ret, 0, len, (jbyte *)(mac));
! }
}
! // release the UTF string and interface list
! (*env)->ReleaseStringUTFChars(env, name, name_utf);
!
! return ret;
}
/*
* Class: java_net_NetworkInterface
* Method: getMTU0
*** 580,608 ****
*/
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
(JNIEnv *env, jclass cls, jstring name, jint index)
{
jboolean isCopy;
! int ret = -1;
! int sock;
const char* name_utf = NULL;
if (name != NULL) {
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
} else {
JNU_ThrowNullPointerException(env, "network interface name is NULL");
return ret;
}
if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return ret;
}
if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
! (*env)->ReleaseStringUTFChars(env, name, name_utf);
! return JNI_FALSE;
}
ret = getMTU(env, sock, name_utf);
(*env)->ReleaseStringUTFChars(env, name, name_utf);
--- 593,621 ----
*/
JNIEXPORT jint JNICALL Java_java_net_NetworkInterface_getMTU0
(JNIEnv *env, jclass cls, jstring name, jint index)
{
jboolean isCopy;
! int sock, ret = -1;
const char* name_utf = NULL;
if (name != NULL) {
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
} else {
JNU_ThrowNullPointerException(env, "network interface name is NULL");
return ret;
}
+
if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return ret;
}
if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
! (*env)->ReleaseStringUTFChars(env, name, name_utf);
! return JNI_FALSE;
}
ret = getMTU(env, sock, name_utf);
(*env)->ReleaseStringUTFChars(env, name, name_utf);
*** 613,636 ****
/*** Private methods definitions ****/
static int getFlags0(JNIEnv *env, jstring name) {
jboolean isCopy;
! int ret, sock;
! const char* name_utf;
! int flags = 0;
if (name != NULL) {
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
} else {
JNU_ThrowNullPointerException(env, "network interface name is NULL");
return -1;
}
if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return -1;
}
if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
(*env)->ReleaseStringUTFChars(env, name, name_utf);
return -1;
}
--- 626,649 ----
/*** Private methods definitions ****/
static int getFlags0(JNIEnv *env, jstring name) {
jboolean isCopy;
! int ret, sock, flags = 0;
! const char *name_utf;
if (name != NULL) {
name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
} else {
JNU_ThrowNullPointerException(env, "network interface name is NULL");
return -1;
}
+
if (name_utf == NULL) {
! if (!(*env)->ExceptionCheck(env))
! JNU_ThrowOutOfMemoryError(env, NULL);
! return -1;
}
if ((sock = openSocketWithFallback(env, name_utf)) < 0) {
(*env)->ReleaseStringUTFChars(env, name, name_utf);
return -1;
}
*** 639,650 ****
close(sock);
(*env)->ReleaseStringUTFChars(env, name, name_utf);
if (ret < 0) {
! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
! "ioctl SIOCGLIFFLAGS failed");
return -1;
}
return flags;
}
--- 652,663 ----
close(sock);
(*env)->ReleaseStringUTFChars(env, name, name_utf);
if (ret < 0) {
! NET_ThrowByNameWithLastError
! (env, JNU_JAVANETPKG "SocketException", "getFlags() failed");
return -1;
}
return flags;
}
*** 665,694 ****
jint child_count, child_index;
netaddr *addrP;
netif *childP;
jobject tmp;
! // Create a NetworkInterface object and populate it
netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
CHECK_NULL_RETURN(netifObj, NULL);
name = (*env)->NewStringUTF(env, ifs->name);
CHECK_NULL_RETURN(name, NULL);
(*env)->SetObjectField(env, netifObj, ni_nameID, name);
(*env)->SetObjectField(env, netifObj, ni_descID, name);
(*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
(*env)->SetBooleanField(env, netifObj, ni_virutalID,
ifs->virtual ? JNI_TRUE : JNI_FALSE);
! //Count the number of address on this interface
addr_count = 0;
addrP = ifs->addr;
while (addrP != NULL) {
addr_count++;
addrP = addrP->next;
}
! // Create the array of InetAddresses
addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
if (addrArr == NULL) {
return NULL;
}
--- 678,707 ----
jint child_count, child_index;
netaddr *addrP;
netif *childP;
jobject tmp;
! // create a NetworkInterface object and populate it
netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
CHECK_NULL_RETURN(netifObj, NULL);
name = (*env)->NewStringUTF(env, ifs->name);
CHECK_NULL_RETURN(name, NULL);
(*env)->SetObjectField(env, netifObj, ni_nameID, name);
(*env)->SetObjectField(env, netifObj, ni_descID, name);
(*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
(*env)->SetBooleanField(env, netifObj, ni_virutalID,
ifs->virtual ? JNI_TRUE : JNI_FALSE);
! // count the number of addresses on this interface
addr_count = 0;
addrP = ifs->addr;
while (addrP != NULL) {
addr_count++;
addrP = addrP->next;
}
! // create the array of InetAddresses
addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
if (addrArr == NULL) {
return NULL;
}
*** 704,739 ****
jobject ibObj = NULL;
if (addrP->family == AF_INET) {
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
if (iaObj) {
! setInetAddress_addr(env, iaObj, htonl(
! ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
} else {
return NULL;
}
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj) {
! (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
! if (addrP->brdcast) {
jobject ia2Obj = NULL;
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
if (ia2Obj) {
! setInetAddress_addr(env, ia2Obj, htonl(
! ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
! (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
} else {
return NULL;
}
! }
! (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
! (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
} else {
return NULL;
}
}
-
#if defined(AF_INET6)
if (addrP->family == AF_INET6) {
int scope=0;
iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
if (iaObj) {
--- 717,751 ----
jobject ibObj = NULL;
if (addrP->family == AF_INET) {
iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
if (iaObj) {
! setInetAddress_addr(env, iaObj, htonl(
! ((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
} else {
return NULL;
}
ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
if (ibObj) {
! (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
! if (addrP->brdcast) {
jobject ia2Obj = NULL;
ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
if (ia2Obj) {
! setInetAddress_addr(env, ia2Obj, htonl(
! ((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
! (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
} else {
return NULL;
}
! }
! (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
! (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
} else {
return NULL;
}
}
#if defined(AF_INET6)
if (addrP->family == AF_INET6) {
int scope=0;
iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
if (iaObj) {
*** 765,775 ****
(*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
addrP = addrP->next;
}
! // See if there is any virtual interface attached to this one.
child_count = 0;
childP = ifs->childs;
while (childP) {
child_count++;
childP = childP->next;
--- 777,787 ----
(*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
addrP = addrP->next;
}
! // see if there is any virtual interface attached to this one.
child_count = 0;
childP = ifs->childs;
while (childP) {
child_count++;
childP = childP->next;
*** 778,798 ****
childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
if (childArr == NULL) {
return NULL;
}
! // Create the NetworkInterface instances for the sub-interfaces as well.
child_index = 0;
childP = ifs->childs;
while(childP) {
! tmp = createNetworkInterface(env, childP);
! if (tmp == NULL) {
! return NULL;
! }
! (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
! (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
! childP = childP->next;
}
(*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
(*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
(*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
--- 790,810 ----
childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
if (childArr == NULL) {
return NULL;
}
! // create the NetworkInterface instances for the sub-interfaces as well
child_index = 0;
childP = ifs->childs;
while(childP) {
! tmp = createNetworkInterface(env, childP);
! if (tmp == NULL) {
! return NULL;
! }
! (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
! (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
! childP = childP->next;
}
(*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
(*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
(*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
*** 802,851 ****
/*
* Enumerates all interfaces
*/
static netif *enumInterfaces(JNIEnv *env) {
! netif *ifs;
int sock;
- // Enumerate IPv4 addresses
sock = openSocket(env, AF_INET);
if (sock < 0 && (*env)->ExceptionOccurred(env)) {
return NULL;
}
ifs = enumIPv4Interfaces(env, sock, NULL);
close(sock);
if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
return NULL;
}
- // return partial list if an exception occurs in the middle of process ???
-
// If IPv6 is available then enumerate IPv6 addresses.
#if defined(AF_INET6)
-
// User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
// so we have to call ipv6_available()
if (ipv6_available()) {
! sock = openSocket(env, AF_INET6);
! if (sock < 0 && (*env)->ExceptionOccurred(env)) {
! freeif(ifs);
! return NULL;
! }
!
! ifs = enumIPv6Interfaces(env, sock, ifs);
! close(sock);
!
! if ((*env)->ExceptionOccurred(env)) {
! freeif(ifs);
! return NULL;
! }
! }
#endif
return ifs;
}
--- 814,859 ----
/*
* Enumerates all interfaces
*/
static netif *enumInterfaces(JNIEnv *env) {
! netif *ifs = NULL;
int sock;
sock = openSocket(env, AF_INET);
if (sock < 0 && (*env)->ExceptionOccurred(env)) {
return NULL;
}
+ // enumerate IPv4 addresses
ifs = enumIPv4Interfaces(env, sock, NULL);
close(sock);
+ // return partial list if an exception occurs in the middle of process ???
if (ifs == NULL && (*env)->ExceptionOccurred(env)) {
return NULL;
}
// If IPv6 is available then enumerate IPv6 addresses.
#if defined(AF_INET6)
// User can disable ipv6 explicitly by -Djava.net.preferIPv4Stack=true,
// so we have to call ipv6_available()
if (ipv6_available()) {
+ sock = openSocket(env, AF_INET6);
+ if (sock < 0 && (*env)->ExceptionOccurred(env)) {
+ freeif(ifs);
+ return NULL;
+ }
! ifs = enumIPv6Interfaces(env, sock, ifs);
! close(sock);
! if ((*env)->ExceptionOccurred(env)) {
! freeif(ifs);
! return NULL;
! }
! }
#endif
return ifs;
}
*** 862,872 ****
netaddr *next = addrP->next;
free(addrP);
addrP = next;
}
! // Don't forget to free the sub-interfaces.
if (currif->childs != NULL) {
freeif(currif->childs);
}
ifs = currif->next;
--- 870,880 ----
netaddr *next = addrP->next;
free(addrP);
addrP = next;
}
! // don't forget to free the sub-interfaces
if (currif->childs != NULL) {
freeif(currif->childs);
}
ifs = currif->next;
*** 880,909 ****
struct sockaddr *ifr_broadaddrP,
int family, short prefix)
{
netif *currif = ifs, *parent;
netaddr *addrP;
!
! #ifdef LIFNAMSIZ
! int ifnam_size = LIFNAMSIZ;
! char name[LIFNAMSIZ], vname[LIFNAMSIZ];
! #else
! int ifnam_size = IFNAMSIZ;
! char name[IFNAMSIZ], vname[IFNAMSIZ];
! #endif
!
char *name_colonP;
int isVirtual = 0;
int addr_size;
- int flags = 0;
// If the interface name is a logical interface then we remove the unit
// number so that we have the physical interface (eg: hme0:1 -> hme0).
// NetworkInterface currently doesn't have any concept of physical vs.
// logical interfaces.
! strncpy(name, if_name, ifnam_size);
! name[ifnam_size - 1] = '\0';
*vname = 0;
// Create and populate the netaddr node. If allocation fails
// return an un-updated list.
--- 888,908 ----
struct sockaddr *ifr_broadaddrP,
int family, short prefix)
{
netif *currif = ifs, *parent;
netaddr *addrP;
! char name[IFNAMESIZE], vname[IFNAMESIZE];
char *name_colonP;
int isVirtual = 0;
int addr_size;
// If the interface name is a logical interface then we remove the unit
// number so that we have the physical interface (eg: hme0:1 -> hme0).
// NetworkInterface currently doesn't have any concept of physical vs.
// logical interfaces.
! strncpy(name, if_name, IFNAMESIZE);
! name[IFNAMESIZE - 1] = '\0';
*vname = 0;
// Create and populate the netaddr node. If allocation fails
// return an un-updated list.
*** 921,930 ****
--- 920,930 ----
memcpy(addrP->addr, ifr_addrP, addr_size);
addrP->family = family;
addrP->mask = prefix;
addrP->next = 0;
+
// for IPv4 add broadcast address
if (family == AF_INET && ifr_broadaddrP != NULL) {
addrP->brdcast = (struct sockaddr *)
((char *)addrP + sizeof(netaddr) + addr_size);
memcpy(addrP->brdcast, ifr_broadaddrP, addr_size);
*** 933,956 ****
}
// Deal with virtual interface with colon notation e.g. eth0:1
name_colonP = strchr(name, ':');
if (name_colonP != NULL) {
// This is a virtual interface. If we are able to access the parent
// we need to create a new entry if it doesn't exist yet *and* update
// the 'parent' interface with the new records.
*name_colonP = 0;
if (getFlags(sock, name, &flags) < 0 || flags < 0) {
! // failed to access parent interface do not create parent.
! // We are a virtual interface with no parent.
! isVirtual = 1;
! *name_colonP = ':';
} else {
! // Got access to parent, so create it if necessary.
! // Save original name to vname and truncate name by ':'
! memcpy(vname, name, sizeof(vname) );
! vname[name_colonP - name] = ':';
}
}
// Check if this is a "new" interface. Use the interface name for
// matching because index isn't supported on Solaris 2.6 & 7.
--- 933,957 ----
}
// Deal with virtual interface with colon notation e.g. eth0:1
name_colonP = strchr(name, ':');
if (name_colonP != NULL) {
+ int flags = 0;
// This is a virtual interface. If we are able to access the parent
// we need to create a new entry if it doesn't exist yet *and* update
// the 'parent' interface with the new records.
*name_colonP = 0;
if (getFlags(sock, name, &flags) < 0 || flags < 0) {
! // failed to access parent interface do not create parent.
! // We are a virtual interface with no parent.
! isVirtual = 1;
! *name_colonP = ':';
} else {
! // Got access to parent, so create it if necessary.
! // Save original name to vname and truncate name by ':'
! memcpy(vname, name, sizeof(vname));
! vname[name_colonP - name] = ':';
}
}
// Check if this is a "new" interface. Use the interface name for
// matching because index isn't supported on Solaris 2.6 & 7.
*** 959,974 ****
break;
}
currif = currif->next;
}
! // If "new" then create an netif structure and insert it into the list.
if (currif == NULL) {
! CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
currif->name = (char *)currif + sizeof(netif);
! strncpy(currif->name, name, ifnam_size);
! currif->name[ifnam_size - 1] = '\0';
currif->index = getIndex(sock, name);
currif->addr = NULL;
currif->childs = NULL;
currif->virtual = isVirtual;
currif->next = ifs;
--- 960,975 ----
break;
}
currif = currif->next;
}
! // If "new" then create a netif structure and insert it into the list.
if (currif == NULL) {
! CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
currif->name = (char *)currif + sizeof(netif);
! strncpy(currif->name, name, IFNAMESIZE);
! currif->name[IFNAMESIZE - 1] = '\0';
currif->index = getIndex(sock, name);
currif->addr = NULL;
currif->childs = NULL;
currif->virtual = isVirtual;
currif->next = ifs;
*** 993,1009 ****
}
currif = currif->next;
}
if (currif == NULL) {
! CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
currif->name = (char *)currif + sizeof(netif);
! strncpy(currif->name, vname, ifnam_size);
! currif->name[ifnam_size - 1] = '\0';
currif->index = getIndex(sock, vname);
! currif->addr = NULL;
! // Need to duplicate the addr entry?
currif->virtual = 1;
currif->childs = NULL;
currif->next = parent->childs;
parent->childs = currif;
}
--- 994,1009 ----
}
currif = currif->next;
}
if (currif == NULL) {
! CHECKED_MALLOC3(currif, netif *, sizeof(netif) + IFNAMESIZE);
currif->name = (char *)currif + sizeof(netif);
! strncpy(currif->name, vname, IFNAMESIZE);
! currif->name[IFNAMESIZE - 1] = '\0';
currif->index = getIndex(sock, vname);
! currif->addr = NULL; // Need to duplicate the addr entry?
currif->virtual = 1;
currif->childs = NULL;
currif->next = parent->childs;
parent->childs = currif;
}
*** 1088,1099 ****
if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
// If EPROTONOSUPPORT is returned it means we don't have
// support for this proto so don't throw an exception.
if (errno != EPROTONOSUPPORT) {
! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
! "Socket creation failed");
}
return -1;
}
return sock;
--- 1088,1099 ----
if ((sock = JVM_Socket(proto, SOCK_DGRAM, 0)) < 0) {
// If EPROTONOSUPPORT is returned it means we don't have
// support for this proto so don't throw an exception.
if (errno != EPROTONOSUPPORT) {
! NET_ThrowByNameWithLastError
! (env, JNU_JAVANETPKG "SocketException", "Socket creation failed");
}
return -1;
}
return sock;
*** 1111,1127 ****
int sock;
if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
if (errno == EPROTONOSUPPORT) {
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
! "IPV6 Socket creation failed");
return -1;
}
} else { // errno is not NOSUPPORT
! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
! "IPV4 Socket creation failed");
return -1;
}
}
// Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
--- 1111,1127 ----
int sock;
if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
if (errno == EPROTONOSUPPORT) {
if ((sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
! NET_ThrowByNameWithLastError
! (env, JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed");
return -1;
}
} else { // errno is not NOSUPPORT
! NET_ThrowByNameWithLastError
! (env, JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed");
return -1;
}
}
// Linux starting from 2.6.? kernel allows ioctl call with either IPv4 or
*** 1166,1176 ****
ifreqP = ifc.ifc_req;
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
struct sockaddr addr, broadaddr, *broadaddrP = NULL;
short prefix = 0;
! // ignore non IPv4 interfaces
if (ifreqP->ifr_addr.sa_family != AF_INET) {
continue;
}
// save socket address
--- 1166,1176 ----
ifreqP = ifc.ifc_req;
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
struct sockaddr addr, broadaddr, *broadaddrP = NULL;
short prefix = 0;
! // ignore non IPv4 addresses
if (ifreqP->ifr_addr.sa_family != AF_INET) {
continue;
}
// save socket address
*** 1280,1302 ****
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
static struct ifreq ifr;
! int i;
memset((char *)&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
return -1;
}
memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
// all bytes to 0 means no hardware address
for (i = 0; i < IFHWADDRLEN; i++) {
if (buf[i] != 0)
--- 1280,1309 ----
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
static struct ifreq ifr;
! int i, sock;
!
! if ((sock = openSocketWithFallback(env, ifname)) < 0) {
! return -1;
! }
!
memset((char *)&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGIFHWADDR) failed");
+ close(sock);
return -1;
}
+ close(sock);
memcpy(buf, &ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
// all bytes to 0 means no hardware address
for (i = 0; i < IFHWADDRLEN; i++) {
if (buf[i] != 0)
*** 1404,1414 ****
ifreqP = ifc.ifc_req;
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
struct sockaddr addr, broadaddr, *broadaddrP = NULL;
short prefix = 0;
! // ignore non IPv4 interfaces
if (ifreqP->ifr_addr.sa_family != AF_INET) {
continue;
}
// save socket address
--- 1411,1421 ----
ifreqP = ifc.ifc_req;
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++, ifreqP++) {
struct sockaddr addr, broadaddr, *broadaddrP = NULL;
short prefix = 0;
! // ignore non IPv4 addresses
if (ifreqP->ifr_addr.sa_family != AF_INET) {
continue;
}
// save socket address
*** 1460,1470 ****
* Enumerates and returns all IPv6 interfaces on AIX.
*/
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
struct ifconf ifc;
struct ifreq *ifreqP;
! char *buf;
// call SIOCGSIZIFCONF to get size for SIOCGIFCONF buffer
if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
--- 1467,1477 ----
* Enumerates and returns all IPv6 interfaces on AIX.
*/
static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) {
struct ifconf ifc;
struct ifreq *ifreqP;
! char *buf, *cp, *cplimit;
// call SIOCGSIZIFCONF to get size for SIOCGIFCONF buffer
if (ioctl(sock, SIOCGSIZIFCONF, &(ifc.ifc_len)) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGSIZIFCONF) failed");
*** 1480,1500 ****
free(buf);
return ifs;
}
// iterate through each interface
! char *cp = (char *)ifc.ifc_req;
! char *cplimit = cp + ifc.ifc_len;
for (; cp < cplimit;
cp += (sizeof(ifreqP->ifr_name) +
MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
{
ifreqP = (struct ifreq *)cp;
short prefix = 0;
! // ignore non IPv6 interfaces
if (ifreqP->ifr_addr.sa_family != AF_INET6) {
continue;
}
// determine netmask
--- 1487,1508 ----
free(buf);
return ifs;
}
// iterate through each interface
! ifreqP = ifc.ifc_req;
! cp = (char *)ifc.ifc_req;
! cplimit = cp + ifc.ifc_len;
for (; cp < cplimit;
cp += (sizeof(ifreqP->ifr_name) +
MAX((ifreqP->ifr_addr).sa_len, sizeof(ifreqP->ifr_addr))))
{
ifreqP = (struct ifreq *)cp;
short prefix = 0;
! // ignore non IPv6 addresses
if (ifreqP->ifr_addr.sa_family != AF_INET6) {
continue;
}
// determine netmask
*** 1543,1553 ****
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
int size;
struct kinfo_ndd *nddp;
void *end;
--- 1551,1561 ----
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
int size;
struct kinfo_ndd *nddp;
void *end;
*** 1602,1625 ****
return if2.ifr_mtu;
}
static int getFlags(int sock, const char *ifname, int *flags) {
! struct ifreq if2;
! memset((char *)&if2, 0, sizeof(if2));
! strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
!
! if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
! return -1;
! }
!
! if (sizeof(if2.ifr_flags) == sizeof(short)) {
! *flags = (if2.ifr_flags & 0xffff);
! } else {
! *flags = if2.ifr_flags;
! }
! return 0;
}
#endif /* _AIX */
/** Solaris **/
--- 1610,1633 ----
return if2.ifr_mtu;
}
static int getFlags(int sock, const char *ifname, int *flags) {
! struct ifreq if2;
! memset((char *)&if2, 0, sizeof(if2));
! strncpy(if2.ifr_name, ifname, sizeof(if2.ifr_name) - 1);
!
! if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) {
! return -1;
! }
!
! if (sizeof(if2.ifr_flags) == sizeof(short)) {
! *flags = (if2.ifr_flags & 0xffff);
! } else {
! *flags = if2.ifr_flags;
! }
! return 0;
}
#endif /* _AIX */
/** Solaris **/
*** 1684,1694 ****
struct lifreq *ifreqP;
struct lifnum numifs;
char *buf = NULL;
unsigned i;
! // call SIOCGLIFNUM to get the size of SIOCGIFCONF buffer
numifs.lifn_family = AF_INET;
numifs.lifn_flags = 0;
if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
--- 1692,1702 ----
struct lifreq *ifreqP;
struct lifnum numifs;
char *buf = NULL;
unsigned i;
! // call SIOCGLIFNUM to get the interface count
numifs.lifn_family = AF_INET;
numifs.lifn_flags = 0;
if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
*** 1711,1721 ****
// iterate through each interface
ifreqP = ifc.lifc_req;
for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
struct sockaddr addr, *broadaddrP = NULL;
! // ignore non IPv4 interfaces
if (ifreqP->lifr_addr.ss_family != AF_INET) {
continue;
}
// save socket address
--- 1719,1729 ----
// iterate through each interface
ifreqP = ifc.lifc_req;
for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
struct sockaddr addr, *broadaddrP = NULL;
! // ignore non IPv4 addresses
if (ifreqP->lifr_addr.ss_family != AF_INET) {
continue;
}
// save socket address
*** 1760,1770 ****
struct lifreq *ifreqP;
struct lifnum numifs;
char *buf = NULL;
unsigned i;
! // call SIOCGLIFNUM to get the size of SIOCGLIFCONF buffer
numifs.lifn_family = AF_INET6;
numifs.lifn_flags = 0;
if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
--- 1768,1778 ----
struct lifreq *ifreqP;
struct lifnum numifs;
char *buf = NULL;
unsigned i;
! // call SIOCGLIFNUM to get the interface count
numifs.lifn_family = AF_INET6;
numifs.lifn_flags = 0;
if (ioctl(sock, SIOCGLIFNUM, (char *)&numifs) < 0) {
NET_ThrowByNameWithLastError
(env, JNU_JAVANETPKG "SocketException", "ioctl(SIOCGLIFNUM) failed");
*** 1786,1796 ****
// iterate through each interface
ifreqP = ifc.lifc_req;
for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
! // ignore non IPv6 interfaces
if (ifreqP->lifr_addr.ss_family != AF_INET6) {
continue;
}
// set scope ID to interface index
--- 1794,1804 ----
// iterate through each interface
ifreqP = ifc.lifc_req;
for (i = 0; i < numifs.lifn_count; i++, ifreqP++) {
! // ignore non IPv6 addresses
if (ifreqP->lifr_addr.ss_family != AF_INET6) {
continue;
}
// set scope ID to interface index
*** 1805,1815 ****
// if an exception occurred we return immediately
if ((*env)->ExceptionOccurred(env)) {
free(buf);
return ifs;
}
! }
// free buffer
free(buf);
return ifs;
}
--- 1813,1823 ----
// if an exception occurred we return immediately
if ((*env)->ExceptionOccurred(env)) {
free(buf);
return ifs;
}
! }
// free buffer
free(buf);
return ifs;
}
*** 1850,1862 ****
// Device is in /dev. e.g.: /dev/bge0
strcpy(style1dev, DEV_PREFIX);
strcat(style1dev, ifname);
if ((fd = open(style1dev, O_RDWR)) < 0) {
! // Can't open it. We probably are missing the privilege.
! // We'll have to try something else
! return 0;
}
dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
--- 1858,1870 ----
// Device is in /dev. e.g.: /dev/bge0
strcpy(style1dev, DEV_PREFIX);
strcat(style1dev, ifname);
if ((fd = open(style1dev, O_RDWR)) < 0) {
! // Can't open it. We probably are missing the privilege.
! // We'll have to try something else
! return 0;
}
dlpareq.dl_primitive = DL_PHYS_ADDR_REQ;
dlpareq.dl_addr_type = DL_CURR_PHYS_ADDR;
*** 1894,1952 ****
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
struct lifreq if2;
! int len, i;
// First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
// try the old way.
memset((char *)&if2, 0, sizeof(if2));
strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
if (ioctl(sock, SIOCGLIFHWADDR, &if2) != -1) {
struct sockaddr_dl *sp;
sp = (struct sockaddr_dl *)&if2.lifr_addr;
memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
return sp->sdl_alen;
}
// On Solaris we have to use DLPI, but it will only work if we have
// privileged access (i.e. root). If that fails, we try a lookup
// in the ARP table, which requires an IPv4 address.
! if ((len = getMacFromDevice(env, ifname, buf)) == 0) {
! // DLPI failed - trying to do arp lookup
!
struct arpreq arpreq;
struct sockaddr_in *sin;
struct sockaddr_in ipAddr;
! if (addr == NULL) {
! // No IPv4 address for that interface, so can't do an ARP lookup.
! return -1;
! }
!
! len = 6; //???
!
! sin = (struct sockaddr_in *)&arpreq.arp_pa;
! memset((char *)&arpreq, 0, sizeof(struct arpreq));
! ipAddr.sin_port = 0;
! ipAddr.sin_family = AF_INET;
! memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
! memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
! arpreq.arp_flags= ATF_PUBL;
!
! if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
! return -1;
! }
! memcpy(buf, &arpreq.arp_ha.sa_data[0], len);
}
// all bytes to 0 means no hardware address
for (i = 0; i < len; i++) {
if (buf[i] != 0)
return len;
--- 1902,1960 ----
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
struct lifreq if2;
! int len, i, sock;
!
! if ((sock = openSocketWithFallback(env, ifname)) < 0) {
! return -1;
! }
// First, try the new (S11) SIOCGLIFHWADDR ioctl(). If that fails
// try the old way.
memset((char *)&if2, 0, sizeof(if2));
strncpy(if2.lifr_name, ifname, sizeof(if2.lifr_name) - 1);
if (ioctl(sock, SIOCGLIFHWADDR, &if2) != -1) {
struct sockaddr_dl *sp;
sp = (struct sockaddr_dl *)&if2.lifr_addr;
memcpy(buf, &sp->sdl_data[0], sp->sdl_alen);
+ close(sock);
return sp->sdl_alen;
}
// On Solaris we have to use DLPI, but it will only work if we have
// privileged access (i.e. root). If that fails, we try a lookup
// in the ARP table, which requires an IPv4 address.
! if (((len = getMacFromDevice(env, ifname, buf)) == 0) && (addr != NULL)) {
struct arpreq arpreq;
struct sockaddr_in *sin;
struct sockaddr_in ipAddr;
! len = 6; //???
!
! sin = (struct sockaddr_in *)&arpreq.arp_pa;
! memset((char *)&arpreq, 0, sizeof(struct arpreq));
! ipAddr.sin_port = 0;
! ipAddr.sin_family = AF_INET;
! memcpy(&ipAddr.sin_addr, addr, sizeof(struct in_addr));
! memcpy(&arpreq.arp_pa, &ipAddr, sizeof(struct sockaddr_in));
! arpreq.arp_flags= ATF_PUBL;
! if (ioctl(sock, SIOCGARP, &arpreq) < 0) {
! close(sock);
! return -1;
! }
!
! memcpy(buf, &arpreq.arp_ha.sa_data[0], len);
}
+ close(sock);
// all bytes to 0 means no hardware address
for (i = 0; i < len; i++) {
if (buf[i] != 0)
return len;
*** 2030,2040 ****
}
for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
struct sockaddr *broadaddrP = NULL;
! // ignore non IPv4 interfaces
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
continue;
// set ifa_broadaddr, if there is one
if ((ifa->ifa_flags & IFF_POINTOPOINT) == 0 &&
--- 2038,2048 ----
}
for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
struct sockaddr *broadaddrP = NULL;
! // ignore non IPv4 addresses
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
continue;
// set ifa_broadaddr, if there is one
if ((ifa->ifa_flags & IFF_POINTOPOINT) == 0 &&
*** 2074,2084 ****
(env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
return ifs;
}
for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
! // ignore non IPv6 interfaces
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
continue;
// set scope ID to interface index
((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id =
--- 2082,2092 ----
(env, JNU_JAVANETPKG "SocketException", "getifaddrs() failed");
return ifs;
}
for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) {
! // ignore non IPv6 addresses
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6)
continue;
// set scope ID to interface index
((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id =
*** 2129,2154 ****
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, int sock, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
struct ifaddrs *ifa0, *ifa;
struct sockaddr *saddr;
int i;
! // Grab the interface list
if (!getifaddrs(&ifa0)) {
! // Cycle through the interfaces
for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
saddr = ifa->ifa_addr;
! // Link layer contains the MAC address
if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
! // Check the address is the correct length
if (sadl->sdl_alen == ETHER_ADDR_LEN) {
memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
freeifaddrs(ifa0);
return ETHER_ADDR_LEN;
}
--- 2137,2162 ----
* Gets the Hardware address (usually MAC address) for the named interface.
* On return puts the data in buf, and returns the length, in byte, of the
* MAC address. Returns -1 if there is no hardware address on that interface.
*/
static int getMacAddress
! (JNIEnv *env, const char *ifname, const struct in_addr *addr,
unsigned char *buf)
{
struct ifaddrs *ifa0, *ifa;
struct sockaddr *saddr;
int i;
! // grab the interface list
if (!getifaddrs(&ifa0)) {
! // cycle through the interfaces
for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) {
saddr = ifa->ifa_addr;
! // link layer contains the MAC address
if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) {
struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr;
! // check the address has the correct length
if (sadl->sdl_alen == ETHER_ADDR_LEN) {
memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN);
freeifaddrs(ifa0);
return ETHER_ADDR_LEN;
}
< prev index next >