KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > NetworkInterface


1 /*
2  * @(#)NetworkInterface.java 1.17 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.net.SocketException JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.NoSuchElementException JavaDoc;
13 import sun.security.action.*;
14 import java.security.AccessController JavaDoc;
15
16 /**
17  * This class represents a Network Interface made up of a name,
18  * and a list of IP addresses assigned to this interface.
19  * It is used to identify the local interface on which a multicast group
20  * is joined.
21  *
22  * Interfaces are normally known by names such as "le0".
23  *
24  * @since 1.4
25  */

26 public final class NetworkInterface {
27     private String JavaDoc name;
28     private String JavaDoc displayName;
29     private int index;
30     private InetAddress JavaDoc addrs[];
31
32     static {
33     AccessController.doPrivileged(new LoadLibraryAction("net"));
34     init();
35     }
36
37     /**
38      * Returns an NetworkInterface object with index set to 0 and name to null.
39      * Setting such an interface on a MulticastSocket will cause the
40      * kernel to choose one interface for sending multicast packets.
41      *
42      */

43     NetworkInterface() {
44     }
45     
46     NetworkInterface(String JavaDoc name, int index, InetAddress JavaDoc[] addrs) {
47     this.name = name;
48     this.index = index;
49     this.addrs = addrs;
50     }
51
52     /**
53      * Get the name of this network interface.
54      *
55      * @return the name of this network interface
56      */

57     public String JavaDoc getName() {
58         return name;
59     }
60
61     /**
62      * Convenience method to return an Enumeration with all or a
63      * subset of the InetAddresses bound to this network interface.
64      * <p>
65      * If there is a security manager, its <code>checkConnect</code>
66      * method is called for each InetAddress. Only InetAddresses where
67      * the <code>checkConnect</code> doesn't throw a SecurityException
68      * will be returned in the Enumeration.
69      * @return an Enumeration object with all or a subset of the InetAddresses
70      * bound to this network interface
71      */

72     public Enumeration JavaDoc<InetAddress JavaDoc> getInetAddresses() {
73
74     class checkedAddresses implements Enumeration JavaDoc<InetAddress JavaDoc> {
75     
76         private int i=0, count=0;
77         private InetAddress JavaDoc local_addrs[];
78     
79         checkedAddresses() {
80         local_addrs = new InetAddress JavaDoc[addrs.length];
81             
82         SecurityManager JavaDoc sec = System.getSecurityManager();
83         for (int j=0; j<addrs.length; j++) {
84             try {
85             if (sec != null) {
86                 sec.checkConnect(addrs[j].getHostAddress(), -1);
87             }
88             local_addrs[count++] = addrs[j];
89             } catch (SecurityException JavaDoc e) { }
90         }
91         
92         }
93     
94         public InetAddress JavaDoc nextElement() {
95         if (i < count) {
96             return local_addrs[i++];
97         } else {
98             throw new NoSuchElementException JavaDoc();
99         }
100         }
101     
102         public boolean hasMoreElements() {
103         return (i < count);
104         }
105     }
106     return new checkedAddresses();
107
108     }
109
110     /**
111      * Get the index of this network interface.
112      *
113      * @return the index of this network interface
114      */

115     int getIndex() {
116     return index;
117     }
118
119     /**
120      * Get the display name of this network interface.
121      * A display name is a human readable String describing the network
122      * device.
123      *
124      * @return the display name of this network interface,
125      * or null if no display name is available.
126      */

127     public String JavaDoc getDisplayName() {
128     return displayName;
129     }
130  
131     /**
132      * Searches for the network interface with the specified name.
133      *
134      * @param name
135      * The name of the network interface.
136      *
137      * @return A <tt>NetworkInterface</tt> with the specified name,
138      * or <tt>null</tt> if there is no network interface
139      * with the specified name.
140      *
141      * @throws SocketException
142      * If an I/O error occurs.
143      *
144      * @throws NullPointerException
145      * If the specified name is <tt>null</tt>.
146      */

147     public static NetworkInterface JavaDoc getByName(String JavaDoc name) throws SocketException JavaDoc {
148     if (name == null)
149         throw new NullPointerException JavaDoc();
150     return getByName0(name);
151     }
152
153     /**
154      * Get a network interface given its index.
155      *
156      * @param index an integer, the index of the interface
157      * @return the NetworkInterface obtained from its index
158      * @exception SocketException if an I/O error occurs.
159      */

160     native static NetworkInterface JavaDoc getByIndex(int index)
161     throws SocketException JavaDoc;
162
163     /**
164      * Convenience method to search for a network interface that
165      * has the specified Internet Protocol (IP) address bound to
166      * it.
167      * <p>
168      * If the specified IP address is bound to multiple network
169      * interfaces it is not defined which network interface is
170      * returned.
171      *
172      * @param addr
173      * The <tt>InetAddress</tt> to search with.
174      *
175      * @return A <tt>NetworkInterface</tt>
176      * or <tt>null</tt> if there is no network interface
177      * with the specified IP address.
178      *
179      * @throws SocketException
180      * If an I/O error occurs.
181      *
182      * @throws NullPointerException
183      * If the specified address is <tt>null</tt>.
184      */

185     public static NetworkInterface JavaDoc getByInetAddress(InetAddress JavaDoc addr) throws SocketException JavaDoc {
186     if (addr == null)
187         throw new NullPointerException JavaDoc();
188     return getByInetAddress0(addr);
189     }
190
191     /**
192      * Returns all the interfaces on this machine. Returns null if no
193      * network interfaces could be found on this machine.
194      *
195      * NOTE: can use getNetworkInterfaces()+getInetAddresses()
196      * to obtain all IP addresses for this node
197      *
198      * @return an Enumeration of NetworkInterfaces found on this machine
199      * @exception SocketException if an I/O error occurs.
200      */

201
202     public static Enumeration JavaDoc<NetworkInterface JavaDoc> getNetworkInterfaces()
203     throws SocketException JavaDoc {
204     final NetworkInterface JavaDoc[] netifs = getAll();
205
206     // specified to return null if no network interfaces
207
if (netifs == null)
208         return null;
209     
210     return new Enumeration JavaDoc<NetworkInterface JavaDoc>() {
211         private int i = 0;
212         public NetworkInterface JavaDoc nextElement() {
213         if (netifs != null && i < netifs.length) {
214             NetworkInterface JavaDoc netif = netifs[i++];
215             return netif;
216         } else {
217             throw new NoSuchElementException JavaDoc();
218         }
219         }
220
221         public boolean hasMoreElements() {
222         return (netifs != null && i < netifs.length);
223         }
224     };
225     }
226
227     private native static NetworkInterface JavaDoc[] getAll()
228     throws SocketException JavaDoc;
229
230     private native static NetworkInterface JavaDoc getByName0(String JavaDoc name)
231     throws SocketException JavaDoc;
232
233     private native static NetworkInterface JavaDoc getByInetAddress0(InetAddress JavaDoc addr)
234     throws SocketException JavaDoc;
235
236     
237     /**
238      * Compares this object against the specified object.
239      * The result is <code>true</code> if and only if the argument is
240      * not <code>null</code> and it represents the same NetworkInterface
241      * as this object.
242      * <p>
243      * Two instances of <code>NetworkInterface</code> represent the same
244      * NetworkInterface if both name and addrs are the same for both.
245      *
246      * @param obj the object to compare against.
247      * @return <code>true</code> if the objects are the same;
248      * <code>false</code> otherwise.
249      * @see java.net.InetAddress#getAddress()
250      */

251     public boolean equals(Object JavaDoc obj) {
252     if ((obj == null) || !(obj instanceof NetworkInterface JavaDoc)) {
253         return false;
254     }
255     NetworkInterface JavaDoc netIF = (NetworkInterface JavaDoc)obj;
256     if (name != null ) {
257         if (netIF.getName() != null) {
258         if (!name.equals(netIF.getName())) {
259             return false;
260         }
261         } else {
262         return false;
263         }
264     } else {
265         if (netIF.getName() != null) {
266         return false;
267         }
268     }
269     Enumeration JavaDoc newAddrs = netIF.getInetAddresses();
270     int i = 0;
271     for (i = 0; newAddrs.hasMoreElements();newAddrs.nextElement(), i++);
272     if (addrs == null) {
273         if (i != 0) {
274         return false;
275         }
276     } else {
277         /*
278          * Compare number of addresses (in the checked subset)
279          */

280         int count = 0;
281         Enumeration JavaDoc e = getInetAddresses();
282         for (; e.hasMoreElements(); count++) {
283         e.nextElement();
284         }
285         if (i != count) {
286         return false;
287         }
288         }
289     newAddrs = netIF.getInetAddresses();
290     for (; newAddrs.hasMoreElements();) {
291         boolean equal = false;
292         Enumeration JavaDoc thisAddrs = getInetAddresses();
293         InetAddress JavaDoc newAddr = (InetAddress JavaDoc)newAddrs.nextElement();
294         for (; thisAddrs.hasMoreElements();) {
295         InetAddress JavaDoc thisAddr = (InetAddress JavaDoc)thisAddrs.nextElement();
296         if (thisAddr.equals(newAddr)) {
297             equal = true;
298         }
299         }
300         if (!equal) {
301         return false;
302         }
303     }
304     return true;
305     }
306
307     public int hashCode() {
308     int count = 0;
309     if (addrs != null) {
310         for (int i = 0; i < addrs.length; i++) {
311         count += addrs[i].hashCode();
312         }
313     }
314     return count;
315     }
316
317     public String JavaDoc toString() {
318     String JavaDoc result = "name:";
319     result += name == null? "null": name;
320     if (displayName != null) {
321         result += " (" + displayName + ")";
322     }
323     result += " index: "+index+" addresses:\n";
324     for (Enumeration JavaDoc e = getInetAddresses(); e.hasMoreElements(); ) {
325         InetAddress JavaDoc addr = (InetAddress JavaDoc)e.nextElement();
326         result += addr+";\n";
327     }
328     return result;
329     }
330     private static native void init();
331
332 }
333
Popular Tags