1 /* Copyright (C) 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
19 #include <sys/param.h>
20 #include <sys/types.h>
21 #ifdef HAVE_NETINET_IN_H
22 #include <netinet/in.h>
24 #ifdef HAVE_ARPA_INET_H
25 #include <arpa/inet.h>
30 #ifdef HAVE_SYS_IOCTL_H
31 #define BSD_COMP /* Get FIONREAD on Solaris2. */
32 #include <sys/ioctl.h>
40 #include <java/net/NetworkInterface.h>
41 #include <java/net/Inet4Address.h>
42 #include <java/net/SocketException.h>
43 #include <java/util/Vector.h>
46 java::net::NetworkInterface::getRealNetworkInterfaces ()
49 int num_interfaces
= 0;
50 struct ifconf if_data
;
51 struct ifreq
* if_record
;
52 ::java::util::Vector
* ht
= new ::java::util::Vector ();
55 if_data
.ifc_buf
= NULL
;
57 // Open a (random) socket to have a file descriptor for the ioctl calls.
58 fd
= _Jv_socket (PF_INET
, SOCK_DGRAM
, htons (IPPROTO_IP
));
61 throw new ::java::net::SocketException
;
63 // Get all interfaces. If not enough buffers are available try it
64 // with a bigger buffer size.
69 if_data
.ifc_len
= sizeof (struct ifreq
) * num_interfaces
;
71 (char*) _Jv_Realloc (if_data
.ifc_buf
, if_data
.ifc_len
);
73 // Try to get all local interfaces.
74 if (::ioctl (fd
, SIOCGIFCONF
, &if_data
) < 0)
75 throw new java::net::SocketException
;
77 while (if_data
.ifc_len
>= (sizeof (struct ifreq
) * num_interfaces
));
79 // Get addresses of all interfaces.
80 if_record
= if_data
.ifc_req
;
82 for (int n
= 0; n
< if_data
.ifc_len
; n
+= sizeof (struct ifreq
))
86 memset (&ifr
, 0, sizeof (ifr
));
87 strcpy (ifr
.ifr_name
, if_record
->ifr_name
);
89 // Try to get the IPv4-address of the local interface
90 if (::ioctl (fd
, SIOCGIFADDR
, &ifr
) < 0)
91 throw new java::net::SocketException
;
94 struct sockaddr_in sa
= *((sockaddr_in
*) &(ifr
.ifr_addr
));
96 jbyteArray baddr
= JvNewByteArray (len
);
97 memcpy (elements (baddr
), &(sa
.sin_addr
), len
);
98 jstring if_name
= JvNewStringLatin1 (if_record
->ifr_name
);
99 Inet4Address
* address
=
100 new java::net::Inet4Address (baddr
, JvNewStringLatin1 (""));
101 ht
->add (new NetworkInterface (if_name
, address
));
106 // FIXME: read /proc/net/if_inet6 (on Linux 2.4)
109 _Jv_Free (if_data
.ifc_buf
);