KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > InetMask


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.LinkedList JavaDoc;
26
27 /**
28  * @author nieuviar
29  *
30  */

31 public class InetMask {
32     byte[] bits;
33     int mask;
34
35     /**
36      * Generate an IP mask from its textual representation. The special case of a
37      * single address is taken into account.
38      */

39     public InetMask(String JavaDoc textual) throws UnknownHostException JavaDoc {
40         int i = textual.indexOf('/');
41         String JavaDoc ip = null;
42         if (i < 0) {
43             InetAddress JavaDoc a = InetAddress.getByName(ip);
44             bits = a.getAddress();
45             mask = bits.length * 8;
46         } else {
47             ip = textual.substring(0, i);
48             mask = Integer.parseInt(textual.substring(i + 1));
49             InetAddress JavaDoc a = InetAddress.getByName(ip);
50             bits = a.getAddress();
51         }
52     }
53
54     public boolean match(InetAddress JavaDoc a) {
55         byte[] b = a.getAddress();
56         int l = b.length;
57         if (l != bits.length) return false;
58         int m = mask;
59         for (int i=0; i<l; i++) {
60             if (m < 8) {
61                 int v1 = b[i];
62                 if (v1 < 0) v1 += 256;
63                 int v2 = bits[i];
64                 if (v2 < 0) v2 += 256;
65                 int shift = 8 - m;
66                 return (v1 >> shift) == (v2 >> shift);
67             }
68             m -= 8;
69             if (b[i] != bits[i]) return false;
70         }
71         return false;
72     }
73
74     /**
75      * Use JDK 1.4 methods.
76      * @return List of local addresses (java.net.InetAddress) matching this InetMask.
77      */

78     public LinkedList JavaDoc filterLocal() {
79         LinkedList JavaDoc l = new LinkedList JavaDoc();
80         try {
81             Enumeration JavaDoc en;
82             Class JavaDoc cl;
83             Object JavaDoc[] obj0 = {
84             };
85             cl = Class.forName("java.net.NetworkInterface");
86             Method JavaDoc meth = cl.getMethod("getNetworkInterfaces", new Class JavaDoc[0]);
87             Method JavaDoc getInet = cl.getMethod("getInetAddresses", new Class JavaDoc[0]);
88             en = (Enumeration JavaDoc) meth.invoke(cl, obj0);
89             while (en.hasMoreElements()) {
90                 Object JavaDoc o = en.nextElement();
91                 Enumeration JavaDoc enum2 = (Enumeration JavaDoc) getInet.invoke(o, obj0);
92                 while (enum2.hasMoreElements()) {
93                     InetAddress JavaDoc a = (InetAddress JavaDoc) enum2.nextElement();
94                     if (match(a)) {
95                         l.add(a);
96                     }
97                 }
98             }
99         } catch (Exception JavaDoc e) {
100             if (l.isEmpty()) {
101                 return l;
102             } else {
103                 return new LinkedList JavaDoc();
104             }
105         }
106         return l;
107     }
108 }
109
Popular Tags