By the way, this instance and the previous one along with some others were uncovered by a prototype of an automated tool that searches a set of classes for interesting chaining instances.
This one's simple:
- java.net.NetworkInterface.getNetworkInterfaces() returns an Enumeration of network interfaces, in the form of NetworkInterface objects.
- NetworkInterface.toString() calls getInetAddresses()
- NetworkInterface.getInetAddresses() has a security check, so it can't be called directly
To create the chain:
- Put all NetworkInterface objects in a JList
- Make JList visible
To get the programmatic access to the values, we can set a DefaultListCellRenderer subclass as the renderer for the JList. The setText() method of our renderer receives all displayed values.
Here's an example that gets all the interface information and dumps it to the Java console. It probably gets repeated a few times because of how the Java GUI works:
001 package ex6.chaining.networkinterfaces;
002
003 import java.applet.Applet;
004 import java.net.NetworkInterface;
005 import java.util.Enumeration;
006 import java.util.Vector;
007
008 import javax.swing.DefaultListCellRenderer;
009 import javax.swing.JList;
010
011 public class Example extends Applet {
012 public void start() {
013 Vector interfaceList = new Vector();
014 try {
015 Enumeration en = NetworkInterface.getNetworkInterfaces();
016 while (en.hasMoreElements()) {
017 interfaceList.add(en.nextElement());
018 }
019 } catch (Exception e ) {
020 e.printStackTrace();
021 }
022 JList jlist = new JList(interfaceList);
023 jlist.setCellRenderer(new DefaultListCellRenderer() {
024
025 public void setText(String text) {
026 System.out.println("::" + text);
027 super.setText(text);
028 }
029 });
030 this.add(jlist);
031 }
032 }
It should vomit something like this on the Java console (System.out)
Linux:
::
::name:eth1 (eth1) index: 3 addresses:
/fe80:0:0:0:212:34ff:fe56:789a%3;
/172.21.0.108;
::name:lo (lo) index: 1 addresses:
/0:0:0:0:0:0:0:1%1;
/127.0.0.1;
...
Windows:
::
::name:lo (MS TCP Loopback interface) index: 1 addresses:
/127.0.0.1;
::name:eth0 (AMD PCNET Family PCI Ethernet Adapter - Miniporta do agendador de pacotes) index: 65539 addresses:
/172.21.0.110;
...
No comments:
Post a Comment