Author: mladen.turk(a)jboss.com
Date: 2007-09-11 13:18:14 -0400 (Tue, 11 Sep 2007)
New Revision: 1020
Modified:
trunk/sight/native/os/linux/netadapter.c
trunk/sight/native/share/cache.c
Log:
Implement Linux NetworkAdapter - stage 1
Modified: trunk/sight/native/os/linux/netadapter.c
===================================================================
--- trunk/sight/native/os/linux/netadapter.c 2007-09-11 14:32:36 UTC (rev 1019)
+++ trunk/sight/native/os/linux/netadapter.c 2007-09-11 17:18:14 UTC (rev 1020)
@@ -30,6 +30,11 @@
#include "sight_types.h"
#include "sight_private.h"
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <sys/ioctl.h>
+
+
/*
* Network adapter implementation
*/
@@ -137,33 +142,163 @@
sight_unload_class(_E, &_clazzn);
}
+#define PROC_INET6_FILE "/proc/net/if_inet6"
+
+typedef struct net_ifc_data_t {
+ int type;
+} net_ifc_data_t;
+
+typedef struct net_adapter_enum_t {
+ int sd;
+ int idx;
+ cache_table_t *ifc;
+} net_adapter_enum_t;
+
+
+static void c_destroy(const char *key, void *data)
+{
+ net_ifc_data_t *d = (net_ifc_data_t *)data;
+ if (d) {
+
+ free(d);
+ }
+}
+
SIGHT_EXPORT_DECLARE(jlong, NetworkAdapter, enum0)(SIGHT_STDARGS,
jlong pool)
{
- UNREFERENCED_STDARGS;
+ net_adapter_enum_t *e;
+ int i;
+ struct ifconf ifc;
+ struct ifreq *ifr;
+ sight_arr_t *i6a;
+
+ UNREFERENCED_O;
UNREFERENCED(pool);
+ if (!(e = (net_adapter_enum_t *)sight_calloc(_E,
+ sizeof(net_adapter_enum_t),
+ THROW_FMARK))) {
+ return 0;
+ }
+ if (!(e->ifc = cache_new(16))) {
+ throwAprMemoryException(_E, THROW_FMARK,
+ apr_get_os_error());
+ free(e);
+ return 0;
+ }
+ if ((e->sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+ throwAprException(_E, apr_get_os_error());
+ goto cleanup;
+ }
+ ifc.ifc_buf = NULL;
+ if (ioctl(e->sd, SIOCGIFCONF, (char *)&ifc) < 0) {
+ throwAprException(_E, apr_get_os_error());
+ goto cleanup;
+ }
+ if (!(ifc.ifc_buf = sight_calloc(_E, ifc.ifc_len, THROW_FMARK))) {
+ goto cleanup;
+ }
+ if (ioctl(e->sd, SIOCGIFCONF, (char *)&ifc) < 0) {
+ throwAprException(_E, apr_get_os_error());
+ free(ifc.ifc_buf);
+ goto cleanup;
+ }
+ /* Add interfaces to the cache */
+ ifr = ifc.ifc_req;
+ for (i = 0; i < ifc.ifc_len; i += sizeof(struct ifreq), ifr++) {
+ cache_entry_t *ce;
+ ce = cache_add(e->ifc, ifr->ifr_name);
+ if (!ce->data) {
+ net_ifc_data_t *id;
+ if (!(id = (net_ifc_data_t *)sight_calloc(_E,
+ sizeof(net_ifc_data_t),
+ THROW_FMARK))) {
+ free(ifc.ifc_buf);
+ goto cleanup;
+ }
+ id->type = AF_INET;
+ ce->data = id;
+ }
+ }
+
+ free(ifc.ifc_buf);
+ /* Now add IPV6 entries */
+ if ((i6a = sight_arr_rload(PROC_INET6_FILE)) != NULL) {
+ for (i = 0; i < i6a->siz; i++) {
+ cache_entry_t *ce;
+ int al, plen, scope, dads, ifidx;
+ char name[20];
+ struct in6_addr in6;
+
+ al = sscanf(i6a->arr.ca[i],
+ "%08X%08X%08X%08X %02x %02x %02x %02x %20s",
+ &in6.s6_addr32[0], &in6.s6_addr32[1],
+ &in6.s6_addr32[2], &in6.s6_addr32[3],
+ &ifidx, &plen, &scope, &dads, &name);
+
+ ce = cache_add(e->ifc, name);
+ if (!ce->data) {
+ net_ifc_data_t *id;
+ if (!(id = (net_ifc_data_t *)sight_calloc(_E,
+ sizeof(net_ifc_data_t),
+ THROW_FMARK))) {
+ sight_arr_free(i6a);
+ goto cleanup;
+ }
+ id->type = AF_INET6;
+ ce->data = id;
+ }
+ }
+ sight_arr_free(i6a);
+ }
+ return P2J(e);
+
+cleanup:
+ if (e->sd >= 0)
+ close(e->sd);
+ cache_free(e->ifc, c_destroy);
+ free(e);
return 0;
}
SIGHT_EXPORT_DECLARE(jint, NetworkAdapter, enum1)(SIGHT_STDARGS,
jlong handle)
{
- UNREFERENCED_STDARGS;
- UNREFERENCED(handle);
- return 0;
+ net_adapter_enum_t *e = J2P(handle, net_adapter_enum_t *);
+
+ UNREFERENCED_O;
+ if (!e)
+ return 0;
+ else
+ return e->ifc->siz;
}
SIGHT_EXPORT_DECLARE(void, NetworkAdapter, enum2)(SIGHT_STDARGS,
jobject thiz,
jlong handle)
{
- UNREFERENCED_STDARGS;
- UNREFERENCED(handle);
+ net_adapter_enum_t *e = J2P(handle, net_adapter_enum_t *);
+ net_ifc_data_t *id;
+
+ UNREFERENCED_O;
+ if (!e || e->idx > e->ifc->siz)
+ return;
+ id = (net_ifc_data_t *)e->ifc->list[e->idx]->data;
+ SET_IFIELD_S(0000, thiz, e->ifc->list[e->idx]->key);
+ /* TODO: Figure out the Description */
+ SET_IFIELD_S(0002, thiz, e->ifc->list[e->idx]->key);
+ e->idx++;
}
SIGHT_EXPORT_DECLARE(void, NetworkAdapter, enum3)(SIGHT_STDARGS,
jlong handle)
{
+ net_adapter_enum_t *e = J2P(handle, net_adapter_enum_t *);
+
UNREFERENCED_STDARGS;
- UNREFERENCED(handle);
+ if (e) {
+ close(e->sd);
+ cache_free(e->ifc, c_destroy);
+ free(e);
+ }
}
Modified: trunk/sight/native/share/cache.c
===================================================================
--- trunk/sight/native/share/cache.c 2007-09-11 14:32:36 UTC (rev 1019)
+++ trunk/sight/native/share/cache.c 2007-09-11 17:18:14 UTC (rev 1020)
@@ -41,7 +41,7 @@
cache_table_t *cache_new(size_t init)
{
- cache_table_t *t = (cache_table_t *)malloc(sizeof(cache_table_t));
+ cache_table_t *t = (cache_table_t *)calloc(1, sizeof(cache_table_t));
if (!t)
return NULL;
t->siz = 0;