Dead
[official-gcc.git] / gomp-20050608-branch / libjava / java / net / natVMNetworkInterfaceWin32.cc
blobd42f33de90df779c344454450c23dc8cfa0616e6
1 /* Copyright (C) 2003, 2005 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
7 details. */
9 #include <config.h>
10 #include <platform.h>
12 #undef STRICT
14 #include <java/net/NetworkInterface.h>
15 #include <java/net/Inet4Address.h>
16 #include <java/net/SocketException.h>
17 #include <java/net/VMNetworkInterface.h>
18 #include <java/util/Vector.h>
20 /* As of this writing, NetworkInterface.java has
21 getName() == getDisplayName() and only one IP address
22 per interface. If this changes, we'll need to use
23 iphlpapi (not supported on Win95) to retrieve richer
24 adapter information via GetAdaptersInfo(). In this
25 module, we provide the necessary hooks to detect the
26 presence of iphlpapi and use it if necessary, but
27 comment things out for now to avoid compiler warnings. */
29 enum {MAX_INTERFACES = 50};
31 typedef int
32 (*PfnGetRealNetworkInterfaces) (jstring* pjstrName,
33 java::net::InetAddress** ppAddress);
35 static int
36 winsock2GetRealNetworkInterfaces (jstring* pjstrName,
37 java::net::InetAddress** ppAddress)
39 // FIXME: Add IPv6 support.
41 INTERFACE_INFO arInterfaceInfo[MAX_INTERFACES];
43 // Open a (random) socket to have a file descriptor for the WSAIoctl call.
44 SOCKET skt = ::socket (AF_INET, SOCK_DGRAM, 0);
45 if (skt == INVALID_SOCKET)
46 _Jv_ThrowSocketException ();
48 DWORD dwOutBufSize;
49 int nRetCode = ::WSAIoctl (skt, SIO_GET_INTERFACE_LIST,
50 NULL, 0, &arInterfaceInfo, sizeof(arInterfaceInfo),
51 &dwOutBufSize, NULL, NULL);
53 if (nRetCode == SOCKET_ERROR)
55 DWORD dwLastErrorCode = WSAGetLastError ();
56 ::closesocket (skt);
57 _Jv_ThrowSocketException (dwLastErrorCode);
60 // Get addresses of all interfaces.
61 int nNbInterfaces = dwOutBufSize / sizeof(INTERFACE_INFO);
62 int nCurETHInterface = 0;
63 for (int i=0; i < nNbInterfaces; ++i)
65 int len = 4;
66 jbyteArray baddr = JvNewByteArray (len);
67 SOCKADDR_IN* pAddr = (SOCKADDR_IN*) &arInterfaceInfo[i].iiAddress;
68 memcpy (elements (baddr), &(pAddr->sin_addr), len);
70 // Concoct a name for this interface. Since we don't
71 // have access to the real name under Winsock 2, we use
72 // "lo" for the loopback interface and ethX for the
73 // real ones.
74 TCHAR szName[30];
75 u_long lFlags = arInterfaceInfo[i].iiFlags;
77 if (lFlags & IFF_LOOPBACK)
78 _tcscpy (szName, _T("lo"));
79 else
81 _tcscpy (szName, _T("eth"));
82 wsprintf(szName+3, _T("%d"), nCurETHInterface++);
85 jstring if_name = _Jv_Win32NewString (szName);
86 java::net::Inet4Address* address =
87 new java::net::Inet4Address (baddr, JvNewStringLatin1 (""));
88 pjstrName[i] = if_name;
89 ppAddress[i] = address;
92 ::closesocket (skt);
94 return nNbInterfaces;
98 static int
99 iphlpapiGetRealNetworkInterfaces (jstring* pjstrName,
100 java::net::InetAddress** ppAddress)
102 return 0;
106 static PfnGetRealNetworkInterfaces
107 determineGetRealNetworkInterfacesFN ()
109 /* FIXME: Try to dynamically load iphlpapi.dll and
110 detect the presence of GetAdaptersInfo() using
111 GetProcAddress(). If successful, return
112 iphlpapiGetRealNetworkInterfaces; if not,
113 return winsock2GetRealNetworkInterfaces */
114 return &winsock2GetRealNetworkInterfaces;
117 ::java::util::Vector*
118 java::net::VMNetworkInterface::getInterfaces ()
120 // This next declaration used to be a static local,
121 // but this introduced a dependency on libsupc++ due
122 // to _cxa_guard_acquire and _cxa_guard_release.
123 // When Win95 is gone and we eventually get rid of
124 // winsock2GetRealNetworkInterfaces, we can rework
125 // all of this. Alternatively, we could move this all
126 // to win32.cc and initialize this at startup time,
127 // but that seems more trouble than it's worth at
128 // the moment.
129 PfnGetRealNetworkInterfaces pfn =
130 determineGetRealNetworkInterfacesFN ();
132 jstring arIFName[MAX_INTERFACES];
133 InetAddress* arpInetAddress[MAX_INTERFACES];
134 ::java::util::Vector* ht = new ::java::util::Vector ();
136 int nNbInterfaces = (*pfn) (arIFName, arpInetAddress);
137 for (int i=0; i < nNbInterfaces; ++i)
139 ht->add (new java::net::NetworkInterface (arIFName[i],
140 arpInetAddress[i]));
143 return ht;