Module: IPCalc

Defined in:
lib/ipcalc.rb

Class Method Summary collapse

Class Method Details

.bits_to_netmask(bits) ⇒ Object



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
# File 'lib/ipcalc.rb', line 3

def IPCalc.bits_to_netmask(bits)
	bits=bits.to_i()
	if bits <=0
		return "0.0.0.0"
	end
	if bits >= 32
		return "255.255.255.255"
	end

	cpt=0
	cpt_byte=8
	result=""
	result_val=0
	while bits >= 0 ||cpt <= 32
		val=1
		val=0 unless bits > 0
		if cpt%8==0
			if cpt != 0
			result+="#{result_val}"
			result+="." unless cpt == 32
			cpt_byte=8
			result_val=0
			end
		end
		result_val+=2**(cpt_byte-1)*val
		cpt_byte-=1
		cpt+=1
		bits-=1
	end
	return result
end

.class_type_default_mask(addr) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ipcalc.rb', line 202

def IPCalc.class_type_default_mask(addr)
	#verify if addr is an 4 dot format string
	if !isValidIPv4(addr)
		return nil
	end
	class_name="class"
	mask=addr
	if addr.to_s().match('^\d+$')
		mask=bits_to_netmask(netmask)
	end

	bitmask=to_bitwise(mask)
	if bitmask.to_s().match('^0')
		return 8
	elsif bitmask.to_s().match('^10')
		return 16
	elsif bitmask.to_s().match('^110')
		return 24
	elsif bitmask.to_s().match('^1110')
		return 4
	elsif bitmask.to_s().match('^1111')
		return 4
	end
end

.class_type_name(network) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ipcalc.rb', line 179

def IPCalc.class_type_name(network)
	class_name="class"
	mask=network
	if network.to_s().match('^\d+$')
		mask=bits_to_netmask(network)
	end
	if mask==nil
		return nil
	end
	bitmask=to_bitwise(mask)
	if bitmask.to_s().match('^0')
		return class_name+" "+"A"
	elsif bitmask.to_s().match('^10')
		return class_name+" "+"B"
	elsif bitmask.to_s().match('^110')
		return class_name+" "+"C"
	elsif bitmask.to_s().match('^1110')
		return class_name+" "+"D"
	elsif bitmask.to_s().match('^1111')
		return class_name+" "+"E"
	end
end

.get_broadcast(network, mask) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ipcalc.rb', line 122

def IPCalc.get_broadcast(network, mask)
	#verify if network is an 4 dot format string
	if !isValidIPv4(network)
		return nil
	end
	wildcard=get_wildcard(mask)
	wildcard_t=wildcard.split(/\./)
	addr_t=network.split(/\./)
	cpt=0
	for wildc in wildcard_t
		if wildc=="0"
			wildcard_t[cpt]=addr_t[cpt] unless addr_t[cpt] == "0"
		end
		cpt+=1
	end
	return wildcard_t.join(".")

end

.get_network_addr(addr, mask) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ipcalc.rb', line 104

def IPCalc.get_network_addr(addr, mask)
	#verify if addr is an 4 dot format string
	if !isValidIPv4(addr)
		return nil
	end
	wildcard=get_wildcard(mask)
	wildcard_t=wildcard.split(/\./)
	addr_t=addr.split(/\./)
	cpt=0
	for wildc in wildcard_t
		if wildc != "0"
			addr_t[cpt]="0"
		end
		cpt+=1
	end
	return addr_t.join(".")
end

.get_network_and_mask(addr) ⇒ Object



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
# File 'lib/ipcalc.rb', line 77

def IPCalc.get_network_and_mask(addr)
	#verify if addr is an 4 dot format string
	if !isValidIPv4(addr)
		return nil
	end
	result_t=[]
	mask=class_type_default_mask(addr)
	wildcard=get_wildcard(mask)
	wildcard_t=wildcard.split(/\./)
	addr_t=addr.split(/\./)
	cpt=0
	for wildc in wildcard_t
		if wildc != "0"
			addr_t[cpt]="0"
		end
		cpt+=1
	end
	result=addr_t.join(".")
	if result == addr
		return nil
	else
		result_t.push result
		result_t.push mask
		return result_t
	end
end

.get_wildcard(mask) ⇒ Object

take an integer



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ipcalc.rb', line 59

def IPCalc.get_wildcard(mask)
	mask_local=mask
	if mask.to_s().match('^\d+$')
		mask_local=bits_to_netmask(mask)
	end
	#verify if mask_local is an 4 dot format
	if !isValidIPv4(mask_local)
		return nil
	end
	netmask_t=mask_local.split(/\./)
	result=""
	for val in netmask_t
		val_tmp=val.to_i()
		result+="#{255-val_tmp}."
	end
	return result.chop
end

.ipcalc_detail(addr_str) ⇒ Object

launch ipcalculator ip can be a 4 dot addr or addr/bitmask



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/ipcalc.rb', line 230

def IPCalc.ipcalc_detail(addr_str)
	addr=""
	mask=""
	if isValidIPv4(addr_str)
		#we need default mask
		addr=addr_str
		mask=class_type_default_mask(addr_str)
	elsif addr_str.match('^[\d.]+\/\d+$')
		addr,mask=addr_str.split(/\//)
	end
	result=
	"Address: "+addr+"\n"+
	"NetMask: "+mask+"\n"+
	"Wildcard: "+get_wildcard(mask)+"\n"+
	"Network: "+get_network_addr(addr,mask)+"\n"+
	"Broadcast: "+get_broadcast(addr,mask)+"\n"+
	"Hosts: "+nb_host(mask).to_s()+"\n"+
	"Type: "+class_type_name(addr)+"\n\n";

	#HostMin:
	#HostMax:
  return result
end

.nb_host(netmask) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ipcalc.rb', line 141

def IPCalc.nb_host(netmask)
	mask=netmask
	if !netmask.to_s().match('^\d+$')
		mask=netmask_to_bits(netmask)
	end
	if mask==nil
		return nil
	end
	mask=mask.to_i()
	result=2**(32-mask)-2
	result=1 if result<=0
	return result
end

.netmask_to_bits(netmask) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ipcalc.rb', line 35

def IPCalc.netmask_to_bits(netmask)
	#verify if netmask is an 4 dot format
	if netmask.nil? or netmask.empty? or !isValidIPv4(netmask)
		return nil
	end
	netmask_t=netmask.split(/\./)
	result_val=0

	for val in netmask_t
   	cpt_byte=8
		val_tmp=val.to_i()
		while val_tmp > 0
			val_tmp-=2**(cpt_byte-1)
			cpt_byte-=1
			result_val+=1
		end
	end
	return result_val
end

.to_bitwise(four_dot_addr) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ipcalc.rb', line 155

def IPCalc.to_bitwise(four_dot_addr)
	#verify if four_dot_addr is an 4 dot format string
	if !isValidIPv4(four_dot_addr)
		return nil
	end
	four_dot_addr_t=four_dot_addr.split(/\./)
	result=""
	for four_dot_split in four_dot_addr_t
		byte_val=8
		while four_dot_split.to_i>0
			if four_dot_split.to_i >= 2**(byte_val-1)
				result+="1"
			four_dot_split=four_dot_split.to_i()-2**(byte_val-1)
			else
				result+="0"
			end
			byte_val-=1
		end
		result+="0"*byte_val
		result+="."
	end
	return result.chop
end