Author: mladen.turk(a)jboss.com
Date: 2007-09-12 13:57:27 -0400 (Wed, 12 Sep 2007)
New Revision: 1022
Modified:
trunk/sight/native/os/linux/netadapter.c
Log:
Implement Linux NetworkAdapter - stage 3
Modified: trunk/sight/native/os/linux/netadapter.c
===================================================================
--- trunk/sight/native/os/linux/netadapter.c 2007-09-12 15:15:56 UTC (rev 1021)
+++ trunk/sight/native/os/linux/netadapter.c 2007-09-12 17:57:27 UTC (rev 1022)
@@ -149,11 +149,16 @@
int type;
int index;
int flags;
- char ip4a[128];
- char ip6a[128];
+ int mtu;
+ char ip4a[64];
+ char ip6a[64];
char maca[64];
char mask[64];
char badr[64];
+ char p4ds[64];
+ char s4ds[64];
+ char p6ds[64];
+ char s6ds[64];
} net_ifc_data_t;
typedef struct net_adapter_enum_t {
@@ -199,6 +204,40 @@
*sp++ = '\0';
}
+static void do_ifrec(net_ifc_data_t *id, int sd, const char *name)
+{
+ struct ifreq ifi;
+ struct sockaddr_in *sa;
+
+ memset(&ifi, 0, sizeof(struct ifreq));
+ strcpy(ifi.ifr_name, name);
+
+ if (!ioctl(sd, SIOCGIFINDEX, (char *)&ifi))
+ id->index = ifi.ifr_ifindex;
+ else
+ id->index = -1;
+
+ if (!ioctl(sd, SIOCGIFFLAGS, (char *)&ifi))
+ id->flags = ifi.ifr_flags;
+ /* Can we have IPV4 without MAC address ? */
+ if (!ioctl(sd, SIOCGIFHWADDR, (char *)&ifi))
+ make_mac(ifi.ifr_hwaddr.sa_data, id->maca);
+
+ if (!ioctl(sd, SIOCGIFNETMASK, (char *)&ifi)) {
+ sa = (struct sockaddr_in *)&ifi.ifr_netmask;
+ inet_ntop(AF_INET, &sa->sin_addr, id->mask, 64);
+ }
+
+ if ((id->flags & IFF_BROADCAST) &&
+ !ioctl(sd, SIOCGIFBRDADDR, (char *)&ifi)) {
+ sa = (struct sockaddr_in *)&ifi.ifr_broadaddr;
+ inet_ntop(AF_INET, &sa->sin_addr, id->badr, 64);
+ }
+ if (!ioctl(sd, SIOCGIFMTU, (char *)&ifi))
+ id->mtu = ifi.ifr_mtu;
+}
+
+
SIGHT_EXPORT_DECLARE(jlong, NetworkAdapter, enum0)(SIGHT_STDARGS,
jlong pool)
{
@@ -247,54 +286,19 @@
cache_entry_t *ce;
char las[128] = "";
struct sockaddr_in *sa;
- printf("IPV4: %10s\n", ifr->ifr_name);
sa = (struct sockaddr_in *)&ifr->ifr_addr;
inet_ntop(AF_INET, &sa->sin_addr, las, 64);
ce = cache_add(e->ifc, ifr->ifr_name);
if (!(id = (net_ifc_data_t *)ce->data)) {
- struct ifreq ifi;
if (!(id = (net_ifc_data_t *)sight_calloc(_E,
sizeof(net_ifc_data_t),
THROW_FMARK))) {
free(ifc.ifc_buf);
goto cleanup;
}
- memset(&ifi, 0, sizeof(struct ifreq));
- strcpy(ifi.ifr_name, ifr->ifr_name);
-
- if (ioctl(e->sd, SIOCGIFINDEX, (char *)&ifi) < 0) {
- throwAprException(_E, apr_get_os_error());
- free(ifc.ifc_buf);
- free(id);
- goto cleanup;
- }
- id->index = ifi.ifr_ifindex;
-
- if (ioctl(e->sd, SIOCGIFFLAGS, (char *)&ifi) < 0) {
- throwAprException(_E, apr_get_os_error());
- free(ifc.ifc_buf);
- free(id);
- goto cleanup;
- }
- id->flags = ifi.ifr_flags;
+ do_ifrec(id, e->sd, ifr->ifr_name);
strcpy(id->ip4a, las);
-
- if (ioctl(e->sd, SIOCGIFHWADDR, (char *)&ifi) >= 0) {
- /* Can we have IPV4 without MAC address ? */
- make_mac(ifi.ifr_hwaddr.sa_data, id->maca);
- }
-
- if (ioctl(e->sd, SIOCGIFNETMASK, (char *)&ifi) >= 0) {
- sa = (struct sockaddr_in *)&ifi.ifr_netmask;
- inet_ntop(AF_INET, &sa->sin_addr, id->mask, 64);
- }
-
- if ((id->flags & IFF_BROADCAST) &&
- ioctl(e->sd, SIOCGIFBRDADDR, (char *)&ifi) >= 0) {
- sa = (struct sockaddr_in *)&ifi.ifr_broadaddr;
- inet_ntop(AF_INET, &sa->sin_addr, id->badr, 64);
- }
id->type = AF_INET;
ce->data = id;
}
@@ -315,39 +319,14 @@
*pname = '\0';
ce = cache_add(e->ifc, i4a->arr.ca[i]);
if (!(id = (net_ifc_data_t *)ce->data)) {
- struct ifreq ifi;
- struct sockaddr_in *sa;
-
if (!(id = (net_ifc_data_t *)sight_calloc(_E,
sizeof(net_ifc_data_t),
THROW_FMARK))) {
sight_arr_free(i4a);
goto cleanup;
}
- strcpy(ifi.ifr_name, i4a->arr.ca[i]);
+ do_ifrec(id, e->sd, i4a->arr.ca[i]);
id->type = AF_INET;
- if (ioctl(e->sd, SIOCGIFINDEX, (char *)&ifi) >= 0)
- id->index = ifi.ifr_ifindex;
- else
- id->index = -1;
- if (ioctl(e->sd, SIOCGIFFLAGS, (char *)&ifi) >= 0)
- id->flags = ifi.ifr_flags;
-
- if (ioctl(e->sd, SIOCGIFHWADDR, (char *)&ifi) >= 0) {
- /* Can we have IPV4 without MAC address ? */
- make_mac(ifi.ifr_hwaddr.sa_data, id->maca);
- }
-
- if (ioctl(e->sd, SIOCGIFNETMASK, (char *)&ifi) >= 0) {
- sa = (struct sockaddr_in *)&ifi.ifr_netmask;
- inet_ntop(AF_INET, &sa->sin_addr, id->mask, 64);
- }
-
- if ((id->flags & IFF_BROADCAST) &&
- ioctl(e->sd, SIOCGIFBRDADDR, (char *)&ifi) >= 0) {
- sa = (struct sockaddr_in *)&ifi.ifr_broadaddr;
- inet_ntop(AF_INET, &sa->sin_addr, id->badr, 64);
- }
ce->data = id;
}
}
@@ -365,6 +344,8 @@
al = sscanf(i6a->arr.ca[i],
"%32s %02x %02x %02x %02x %16s",
ias, &ifidx, &plen, &scope, &dads,
ifi.ifr_name);
+ if (al != 6)
+ continue;
sight_hex2bin(ias, in6.s6_addr, 16);
sight_inet_ntop6(in6.s6_addr, las, 64);
ce = cache_add(e->ifc, ifi.ifr_name);
@@ -381,10 +362,12 @@
goto cleanup;
}
strcpy(id->ip6a, las);
- if (ioctl(e->sd, SIOCGIFHWADDR, (char *)&ifi) >= 0) {
+ if (!ioctl(e->sd, SIOCGIFHWADDR, (char *)&ifi)) {
make_mac(ifi.ifr_hwaddr.sa_data, las);
strcpy(id->maca, las);
}
+ if (!ioctl(e->sd, SIOCGIFMTU, (char *)&ifi))
+ id->mtu = ifi.ifr_mtu;
id->type = AF_INET6;
id->index = ifidx;
id->flags = IFF_UP;
@@ -434,8 +417,9 @@
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(0001, thiz, e->ifc->list[e->idx]->key);
SET_IFIELD_S(0002, thiz, e->ifc->list[e->idx]->key);
-
+
if (id->maca[0]) {
addr = sight_new_netaddr_class(_E, _O);
if (!addr || (*_E)->ExceptionCheck(_E))
@@ -496,6 +480,7 @@
SET_IFIELD_O(0006, thiz, aarr);
(*_E)->DeleteLocalRef(_E, aarr);
}
+ SET_IFIELD_I(0009, thiz, id->mtu);
cleanup:
e->idx++;