1 /* 2 * Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 #ifndef NETWORK_INTERFACE_H 27 #define NETWORK_INTERFACE_H 28 29 #include <iphlpapi.h> 30 #include "net_util.h" 31 32 /* 33 * Structures used when enumerating interfaces and addresses 34 */ 35 typedef struct _netaddr { 36 SOCKETADDRESS addr; /* IPv4 or IPv6 address */ 37 SOCKETADDRESS brdcast; 38 short mask; 39 struct _netaddr *next; 40 } netaddr; 41 42 typedef struct _netif { 43 char *name; 44 char *displayName; 45 DWORD dwIndex; /* Internal index */ 46 DWORD ifType; /* Interface type */ 47 int index; /* Friendly index */ 48 struct _netif *next; 49 50 /* Following fields used on Windows XP when IPv6 is used only */ 51 jboolean hasIpv6Address; /* true when following fields valid */ 52 jboolean dNameIsUnicode; /* Display Name is Unicode */ 53 int naddrs; /* Number of addrs */ 54 DWORD ipv6Index; 55 struct _netaddr *addrs; /* addr list for interfaces */ 56 } netif; 57 58 extern void free_netif(netif *netifP); 59 extern void free_netaddr(netaddr *netaddrP); 60 61 /* various JNI ids */ 62 extern jclass ni_class; /* NetworkInterface */ 63 64 extern jmethodID ni_ctor; /* NetworkInterface() */ 65 66 extern jfieldID ni_indexID; /* NetworkInterface.index */ 67 extern jfieldID ni_addrsID; /* NetworkInterface.addrs */ 68 extern jfieldID ni_bindsID; /* NetworkInterface.bindings */ 69 extern jfieldID ni_nameID; /* NetworkInterface.name */ 70 extern jfieldID ni_displayNameID; /* NetworkInterface.displayName */ 71 extern jfieldID ni_childsID; /* NetworkInterface.childs */ 72 73 extern jclass ni_iacls; /* InetAddress */ 74 extern jfieldID ni_iaAddr; /* InetAddress.address */ 75 76 extern jclass ni_ia4cls; /* Inet4Address */ 77 extern jmethodID ni_ia4Ctor; /* Inet4Address() */ 78 79 extern jclass ni_ia6cls; /* Inet6Address */ 80 extern jmethodID ni_ia6ctrID; /* Inet6Address() */ 81 extern jfieldID ni_ia6ipaddressID; 82 extern jfieldID ni_ia6ipaddressID; 83 84 extern jclass ni_ibcls; /* InterfaceAddress */ 85 extern jmethodID ni_ibctrID; /* InterfaceAddress() */ 86 extern jfieldID ni_ibaddressID; /* InterfaceAddress.address */ 87 extern jfieldID ni_ibbroadcastID; /* InterfaceAddress.broadcast */ 88 extern jfieldID ni_ibmaskID; /* InterfaceAddress.maskLength */ 89 90 int enumInterfaces_win(JNIEnv *env, netif **netifPP); 91 92 /* We have included iphlpapi.h which includes iptypes.h which has the definition 93 * for MAX_ADAPTER_DESCRIPTION_LENGTH (along with the other definitions in this 94 * ifndef block). Therefore if MAX_ADAPTER_DESCRIPTION_LENGTH is defined we can 95 * be sure that the other definitions are also defined */ 96 #ifndef MAX_ADAPTER_DESCRIPTION_LENGTH 97 98 /* 99 * Following includes come from iptypes.h 100 */ 101 102 #pragma warning(push) 103 #pragma warning(disable:4201) 104 105 #include <time.h> 106 107 // Definitions and structures used by getnetworkparams and getadaptersinfo apis 108 109 #define MAX_ADAPTER_DESCRIPTION_LENGTH 128 // arb. 110 #define MAX_ADAPTER_NAME_LENGTH 256 // arb. 111 #define MAX_ADAPTER_ADDRESS_LENGTH 8 // arb. 112 #define DEFAULT_MINIMUM_ENTITIES 32 // arb. 113 #define MAX_HOSTNAME_LEN 128 // arb. 114 #define MAX_DOMAIN_NAME_LEN 128 // arb. 115 #define MAX_SCOPE_ID_LEN 256 // arb. 116 117 // 118 // types 119 // 120 121 // Node Type 122 123 #define BROADCAST_NODETYPE 1 124 #define PEER_TO_PEER_NODETYPE 2 125 #define MIXED_NODETYPE 4 126 #define HYBRID_NODETYPE 8 127 128 // 129 // IP_ADDRESS_STRING - store an IP address as a dotted decimal string 130 // 131 132 typedef struct { 133 char String[4 * 4]; 134 } IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING; 135 136 // 137 // IP_ADDR_STRING - store an IP address with its corresponding subnet mask, 138 // both as dotted decimal strings 139 // 140 141 typedef struct _IP_ADDR_STRING { 142 struct _IP_ADDR_STRING* Next; 143 IP_ADDRESS_STRING IpAddress; 144 IP_MASK_STRING IpMask; 145 DWORD Context; 146 } IP_ADDR_STRING, *PIP_ADDR_STRING; 147 148 // 149 // ADAPTER_INFO - per-adapter information. All IP addresses are stored as 150 // strings 151 // 152 153 typedef struct _IP_ADAPTER_INFO { 154 struct _IP_ADAPTER_INFO* Next; 155 DWORD ComboIndex; 156 char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; 157 char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; 158 UINT AddressLength; 159 BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; 160 DWORD Index; 161 UINT Type; 162 UINT DhcpEnabled; 163 PIP_ADDR_STRING CurrentIpAddress; 164 IP_ADDR_STRING IpAddressList; 165 IP_ADDR_STRING GatewayList; 166 IP_ADDR_STRING DhcpServer; 167 BOOL HaveWins; 168 IP_ADDR_STRING PrimaryWinsServer; 169 IP_ADDR_STRING SecondaryWinsServer; 170 time_t LeaseObtained; 171 time_t LeaseExpires; 172 } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO; 173 174 #ifdef _WINSOCK2API_ 175 176 // 177 // The following types require Winsock2. 178 // 179 180 typedef enum { 181 IpPrefixOriginOther = 0, 182 IpPrefixOriginManual, 183 IpPrefixOriginWellKnown, 184 IpPrefixOriginDhcp, 185 IpPrefixOriginRouterAdvertisement, 186 } IP_PREFIX_ORIGIN; 187 188 typedef enum { 189 IpSuffixOriginOther = 0, 190 IpSuffixOriginManual, 191 IpSuffixOriginWellKnown, 192 IpSuffixOriginDhcp, 193 IpSuffixOriginLinkLayerAddress, 194 IpSuffixOriginRandom, 195 } IP_SUFFIX_ORIGIN; 196 197 typedef enum { 198 IpDadStateInvalid = 0, 199 IpDadStateTentative, 200 IpDadStateDuplicate, 201 IpDadStateDeprecated, 202 IpDadStatePreferred, 203 } IP_DAD_STATE; 204 205 typedef struct _IP_ADAPTER_UNICAST_ADDRESS { 206 union { 207 ULONGLONG Alignment; 208 struct { 209 ULONG Length; 210 DWORD Flags; 211 }; 212 }; 213 struct _IP_ADAPTER_UNICAST_ADDRESS *Next; 214 SOCKET_ADDRESS Address; 215 216 IP_PREFIX_ORIGIN PrefixOrigin; 217 IP_SUFFIX_ORIGIN SuffixOrigin; 218 IP_DAD_STATE DadState; 219 220 ULONG ValidLifetime; 221 ULONG PreferredLifetime; 222 ULONG LeaseLifetime; 223 } IP_ADAPTER_UNICAST_ADDRESS, *PIP_ADAPTER_UNICAST_ADDRESS; 224 225 typedef struct _IP_ADAPTER_ANYCAST_ADDRESS { 226 union { 227 ULONGLONG Alignment; 228 struct { 229 ULONG Length; 230 DWORD Flags; 231 }; 232 }; 233 struct _IP_ADAPTER_ANYCAST_ADDRESS *Next; 234 SOCKET_ADDRESS Address; 235 } IP_ADAPTER_ANYCAST_ADDRESS, *PIP_ADAPTER_ANYCAST_ADDRESS; 236 237 typedef struct _IP_ADAPTER_MULTICAST_ADDRESS { 238 union { 239 ULONGLONG Alignment; 240 struct { 241 ULONG Length; 242 DWORD Flags; 243 }; 244 }; 245 struct _IP_ADAPTER_MULTICAST_ADDRESS *Next; 246 SOCKET_ADDRESS Address; 247 } IP_ADAPTER_MULTICAST_ADDRESS, *PIP_ADAPTER_MULTICAST_ADDRESS; 248 249 // 250 // Per-address Flags 251 // 252 #define IP_ADAPTER_ADDRESS_DNS_ELIGIBLE 0x01 253 #define IP_ADAPTER_ADDRESS_TRANSIENT 0x02 254 255 typedef struct _IP_ADAPTER_DNS_SERVER_ADDRESS { 256 union { 257 ULONGLONG Alignment; 258 struct { 259 ULONG Length; 260 DWORD Reserved; 261 }; 262 }; 263 struct _IP_ADAPTER_DNS_SERVER_ADDRESS *Next; 264 SOCKET_ADDRESS Address; 265 } IP_ADAPTER_DNS_SERVER_ADDRESS, *PIP_ADAPTER_DNS_SERVER_ADDRESS; 266 267 typedef struct _IP_ADAPTER_PREFIX { 268 union { 269 ULONGLONG Alignment; 270 struct { 271 ULONG Length; 272 DWORD Flags; 273 }; 274 }; 275 struct _IP_ADAPTER_PREFIX *Next; 276 SOCKET_ADDRESS Address; 277 ULONG PrefixLength; 278 } IP_ADAPTER_PREFIX, *PIP_ADAPTER_PREFIX; 279 280 // 281 // Per-adapter Flags 282 // 283 #define IP_ADAPTER_DDNS_ENABLED 0x01 284 #define IP_ADAPTER_REGISTER_ADAPTER_SUFFIX 0x02 285 #define IP_ADAPTER_DHCP_ENABLED 0x04 286 #define IP_ADAPTER_RECEIVE_ONLY 0x08 287 #define IP_ADAPTER_NO_MULTICAST 0x10 288 #define IP_ADAPTER_IPV6_OTHER_STATEFUL_CONFIG 0x20 289 290 // 291 // OperStatus values from RFC 2863 292 // 293 typedef enum { 294 IfOperStatusUp = 1, 295 IfOperStatusDown, 296 IfOperStatusTesting, 297 IfOperStatusUnknown, 298 IfOperStatusDormant, 299 IfOperStatusNotPresent, 300 IfOperStatusLowerLayerDown 301 } IF_OPER_STATUS; 302 303 // 304 // Scope levels from RFC 2373 used with ZoneIndices array. 305 // 306 typedef enum { 307 ScopeLevelInterface = 1, 308 ScopeLevelLink = 2, 309 ScopeLevelSubnet = 3, 310 ScopeLevelAdmin = 4, 311 ScopeLevelSite = 5, 312 ScopeLevelOrganization = 8, 313 ScopeLevelGlobal = 14 314 } SCOPE_LEVEL; 315 316 typedef struct _IP_ADAPTER_ADDRESSES { 317 union { 318 ULONGLONG Alignment; 319 struct { 320 ULONG Length; 321 DWORD IfIndex; 322 }; 323 }; 324 struct _IP_ADAPTER_ADDRESSES *Next; 325 PCHAR AdapterName; 326 PIP_ADAPTER_UNICAST_ADDRESS FirstUnicastAddress; 327 PIP_ADAPTER_ANYCAST_ADDRESS FirstAnycastAddress; 328 PIP_ADAPTER_MULTICAST_ADDRESS FirstMulticastAddress; 329 PIP_ADAPTER_DNS_SERVER_ADDRESS FirstDnsServerAddress; 330 PWCHAR DnsSuffix; 331 PWCHAR Description; 332 PWCHAR FriendlyName; 333 BYTE PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH]; 334 DWORD PhysicalAddressLength; 335 DWORD Flags; 336 DWORD Mtu; 337 DWORD IfType; 338 IF_OPER_STATUS OperStatus; 339 DWORD Ipv6IfIndex; 340 DWORD ZoneIndices[16]; 341 PIP_ADAPTER_PREFIX FirstPrefix; 342 } IP_ADAPTER_ADDRESSES, *PIP_ADAPTER_ADDRESSES; 343 344 // 345 // Flags used as argument to GetAdaptersAddresses(). 346 // "SKIP" flags are added when the default is to include the information. 347 // "INCLUDE" flags are added when the default is to skip the information. 348 // 349 #define GAA_FLAG_SKIP_UNICAST 0x0001 350 #define GAA_FLAG_SKIP_ANYCAST 0x0002 351 #define GAA_FLAG_SKIP_MULTICAST 0x0004 352 #define GAA_FLAG_SKIP_DNS_SERVER 0x0008 353 #define GAA_FLAG_INCLUDE_PREFIX 0x0010 354 #define GAA_FLAG_SKIP_FRIENDLY_NAME 0x0020 355 356 #endif /* _WINSOCK2API_ */ 357 358 // 359 // IP_PER_ADAPTER_INFO - per-adapter IP information such as DNS server list. 360 // 361 362 typedef struct _IP_PER_ADAPTER_INFO { 363 UINT AutoconfigEnabled; 364 UINT AutoconfigActive; 365 PIP_ADDR_STRING CurrentDnsServer; 366 IP_ADDR_STRING DnsServerList; 367 } IP_PER_ADAPTER_INFO, *PIP_PER_ADAPTER_INFO; 368 369 // 370 // FIXED_INFO - the set of IP-related information which does not depend on DHCP 371 // 372 373 typedef struct { 374 char HostName[MAX_HOSTNAME_LEN + 4] ; 375 char DomainName[MAX_DOMAIN_NAME_LEN + 4]; 376 PIP_ADDR_STRING CurrentDnsServer; 377 IP_ADDR_STRING DnsServerList; 378 UINT NodeType; 379 char ScopeId[MAX_SCOPE_ID_LEN + 4]; 380 UINT EnableRouting; 381 UINT EnableProxy; 382 UINT EnableDns; 383 } FIXED_INFO, *PFIXED_INFO; 384 385 #pragma warning(pop) 386 387 #endif /*!MAX_ADAPTER_DESCRIPTION_LENGTH*/ 388 389 #ifndef IP_INTERFACE_NAME_INFO_DEFINED 390 #define IP_INTERFACE_NAME_INFO_DEFINED 391 392 typedef struct ip_interface_name_info { 393 ULONG Index; // Interface Index 394 ULONG MediaType; // Interface Types - see ipifcons.h 395 UCHAR ConnectionType; 396 UCHAR AccessType; 397 GUID DeviceGuid; // Device GUID is the guid of the device 398 // that IP exposes 399 GUID InterfaceGuid; // Interface GUID, if not GUID_NULL is the 400 // GUID for the interface mapped to the device. 401 } IP_INTERFACE_NAME_INFO, *PIP_INTERFACE_NAME_INFO; 402 403 #endif 404 405 406 /* from ipifcons.h */ 407 408 #ifndef IF_TYPE_PPP 409 #define IF_TYPE_PPP 23 410 #endif 411 412 #ifndef IF_TYPE_SOFTWARE_LOOPBACK 413 #define IF_TYPE_SOFTWARE_LOOPBACK 24 414 #endif 415 416 #ifndef IF_TYPE_SLIP 417 #define IF_TYPE_SLIP 28 418 #endif 419 420 #ifndef IF_TYPE_TUNNEL 421 #define IF_TYPE_TUNNEL 131 422 #endif 423 424 #endif