NetParamsTest.java revision 6073:cea72c2bf071
1/* 2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4691932 26 * @summary Programmatic access to network parameters 27 */ 28 29import java.net.*; 30import java.util.Enumeration; 31 32public class NetParamsTest { 33 private static void printIF(NetworkInterface netif) throws SocketException { 34 System.out.println(netif.getName() + " : "); 35 System.out.println("\tStatus: " + (netif.isUp() ? " UP" : "DOWN")); 36 byte[] mac = netif.getHardwareAddress(); 37 if (mac != null) { 38 System.out.print("\tHardware Address: "); 39 for (byte b : mac) { 40 System.out.print(Integer.toHexString(b) + ":"); 41 } 42 System.out.println(); 43 } 44 System.out.println("\tLoopback: " + netif.isLoopback()); 45 System.out.println("\tPoint to Point: " + netif.isPointToPoint()); 46 System.out.println("\tVirtual: " + netif.isVirtual()); 47 if (netif.isVirtual()) { 48 NetworkInterface parent = netif.getParent(); 49 String parentName = parent == null ? "null" : parent.getName(); 50 System.out.println("\tParent Interface: " + parentName); 51 } 52 System.out.println("\tMulticast: " + netif.supportsMulticast()); 53 54 System.out.println("\tMTU: " + netif.getMTU()); 55 System.out.println("\tBindings:"); 56 java.util.List<InterfaceAddress> binds = netif.getInterfaceAddresses(); 57 for (InterfaceAddress b : binds) { 58 System.out.println("\t\t" + b); 59 } 60 Enumeration<NetworkInterface> ifs = netif.getSubInterfaces(); 61 while(ifs.hasMoreElements()) { 62 NetworkInterface subif = ifs.nextElement(); 63 printIF(subif); 64 } 65 } 66 67 public static void main(String[] args) throws Exception { 68 Enumeration<NetworkInterface> ifs = NetworkInterface.getNetworkInterfaces(); 69 while (ifs.hasMoreElements()) { 70 NetworkInterface netif = ifs.nextElement(); 71 printIF(netif); 72 } 73 } 74} 75