Source code: com/voytechs/jnetstream/primitive/address/IpNetmask.java
1 /*
2 * File: IpNetmask.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: IpNetmask.java,v 1.1.1.1 2003/09/22 16:32:11 voytechs Exp $
6 ********************************************
7 Copyright (C) 2003 Mark Bednarczyk
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ********************************************
23 * $Log: IpNetmask.java,v $
24 * Revision 1.1.1.1 2003/09/22 16:32:11 voytechs
25 * Initial import.
26 *
27 */
28 package com.voytechs.jnetstream.primitive.address;
29
30 import java.lang.*;
31 import java.util.*;
32 import java.net.*;
33
34 /**
35 * @author Mark Bednarczyk
36 * @version $Revision: 1.1.1.1 $
37 */
38 public class IpNetmask
39 extends Address {
40 /* Internal attributes */
41
42 private int bitsInMask = -1;
43
44 /**
45 *
46 * @param
47 * @exception
48 */
49 public IpNetmask(String netmask) {
50 super();
51
52 setAddress(parseByteArray(netmask, 4, '.'));
53 }
54
55 public IpNetmask(int bitsInMask) {
56 super(parseBitCount(bitsInMask));
57
58 this.bitsInMask = bitsInMask;
59 }
60
61 /**
62 * Converts into a long for a IP V4 netmask. Throws
63 * an exception if bit count is greater than 32.
64 * This is only a V4 convention.
65 */
66 public static byte[] parseBitCount(int bits) {
67
68 if(bits > 32)
69 throw new IllegalArgumentException("Illegal number of bits for a IP v4 netmask. 32 bits MAX.");
70
71 long mask = 0x0100000000L;
72 mask >>= bits;
73 mask -= 1;
74 mask = ~mask & 0xFFFFFFFFL;
75
76
77 return(longToByteArray(mask));
78 }
79
80 /**
81 * Return the number of bits turn on in a netmask. This is use full
82 * if you want to display the netmask as a slash with number of bits in mask.
83 *
84 * @return number of bits that are on in the netmask.
85 */
86 public int getBitsInNetmask() {
87 return(this.bitsInMask);
88 }
89
90 /**
91 * This method applies the netmask to another IpNumber object
92 * such as IpAddress or IpNetwork. Netmask is ANDed with every byte
93 * in the corresponding object. If number of bytes in the netmask
94 * does not match the number in the IpNumber object then an IllegalArgumentException
95 * is thrown.
96 * @return A new byte array is returned with mask applied to the ip number object.
97 */
98 public byte [] applyNetmask(Address ip) {
99 if(this.address.length != ip.address.length)
100 throw new IllegalArgumentException("Can not apply netmask. " +
101 "Netmask and IP number byte lengths do not match. " +
102 "Excecting 4 or 16");
103
104 return(IpNumber.AND(this.address, ip.address));
105 }
106
107 public byte [] applyNetmask(byte[] ip) {
108 if(this.address.length != ip.length)
109 throw new IllegalArgumentException("Can not apply netmask. " +
110 "Netmask and IP number byte lengths do not match. " +
111 "Excecting 4 or 16");
112
113 return(IpNumber.AND(this.address, ip));
114 }
115
116 private static int countBits(byte[] byteArray) {
117
118 int bitsInMask = 0;
119
120 for(int j = 0; j < byteArray.length; j ++) {
121 int b = IpNumber.getByte(byteArray[j]);
122 for(int i = 0; i < 8; i ++) {
123 if( ((0x80>>i) & b) != 0)
124 bitsInMask ++;
125 }
126 }
127
128 return(bitsInMask);
129 }
130
131 public String toString() {
132 if(bitsInMask == -1)
133 bitsInMask = countBits(address);
134
135 return("" + bitsInMask);
136 }
137
138
139 /**
140 * Test function for IpNetmask
141 * @param args command line arguments
142 */
143 public static void main(String [] args) {
144 }
145
146 } /* END OF: IpNetmask */