-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackEnd.java
143 lines (137 loc) · 3.46 KB
/
BackEnd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Subnetter back end
* User: Robin Stark
* Date: 1/21/13
* Time: 1:54 AM
* Purpose: This calculates the range, decrement octet, and interval of any valid subnet mask
* Variables:
* mask - this is the ipv4 mask
* Interval - this is the interval between the subnets
* maskRanges - where the subnets start and end
* boundary - which octet is the decrement in
*/
public class BackEnd {
public final int NUM_OF_OCTETS=4;
private int[] mask;
private int interval;
public int[][] maskRanges;
private int boundary;
private boolean calculated;
int numRanges;
//Constructors
//Passing an IP mask
public BackEnd ()
{
mask = null;
interval = -1;
maskRanges=null;
boundary=-1;
calculated=false;
numRanges=-1;
}
public BackEnd (int[] a)
{
boolean valid = true;
//Check to make sure it's valid
for (int i=0; i < NUM_OF_OCTETS; i++)
{
if (a[i] > 255 || a[i] < 0)
{
//Invalid mask
mask=null;
valid=false;
break;
}
}
//If the IPv4 mask is valid, assign it
if(valid)
{
mask = new int[NUM_OF_OCTETS];
mask=a;
}
//We don't know any of this info yet
interval=-1;
maskRanges=null;
boundary =-1;
calculated=false;
numRanges=0;
}
//Getters
public int[] getmask(){return mask;}
public int getInterval() {return interval;}
public int[][] getmaskRanges() {return maskRanges;}
public boolean getCalculated () {return calculated;}
public int getBoundary() {return boundary;}
//Methods start here
//Calculate Subnet
public void Calculate (int[] m)
{
boolean valid = true;
//Check to make sure it's valid
for (int i=0; i < NUM_OF_OCTETS; i++)
{
if (m[i] > 255 || m[i] < 0)
{
//Invalid mask
mask=null;
valid=false;
break;
}
}
//If the IPv4 mask is valid, assign it
if(valid)
{
mask = new int[NUM_OF_OCTETS];
mask=m;
}
//Check to make sure the mask is valid
if (mask!=null)
{
setBoundary();
setInterval();
setmaskRanges();
}
//Find the maskRanges
}
//Find Octet and set boundary
private void setBoundary()
{
//Check to make sure the mask is valid
if (mask!=null)
{
for (int i=0; i < NUM_OF_OCTETS; i++)
{
if(mask[i]!= 255)
{
boundary =i;
break;
}
}
}
}
private void setInterval()
{
//Check to make sure that setBoundary has been run
if (boundary !=-1)
{
interval=256-mask[boundary];
}
}
private void setmaskRanges()
{
if (interval==-1) {return;}
for(int i=0; i < 255;i+=interval )
{
numRanges++;
}
numRanges++;
maskRanges = new int[numRanges+1][numRanges+1];
numRanges=0;
for (int i=0; i < 255; i+=interval)
{
maskRanges[numRanges][0]=i;
maskRanges[numRanges][1]=i+interval-1;
numRanges++;
}
}
}