1111
1212@implementation IPSubnetCalc
1313
14+ @synthesize classless;
15+
1416+ (unsigned int )numberize : (const char *)address
1517{
1618 unsigned int sin_addr;
1719
18- if ((inet_pton (AF_INET, address, &sin_addr)) <= 0 )
19- return (-1 );
20- return (ntohl (sin_addr));
20+ if (address != NULL )
21+ {
22+ if ((inet_pton (AF_INET, address, &sin_addr)) <= 0 )
23+ return (-1 );
24+ return (ntohl (sin_addr));
25+ }
26+ return (0 );
2127}
2228
2329+ (NSString *)denumberize : (unsigned int )address
2430{
2531 char *buffer;
2632 unsigned int addr_nl;
27- NSString *nstr;
28-
33+ NSString *nstr;
34+
2935 addr_nl = htonl (address);
3036 if (!(buffer = malloc (INET_ADDRSTRLEN * sizeof (char ))))
3137 return (NULL );
3238 if (!inet_ntop (AF_INET, &addr_nl, buffer, INET_ADDRSTRLEN * sizeof (char )))
39+ {
40+ free (buffer);
3341 return (NULL );
34- nstr = [NSString stringWithCString: buffer encoding: NSASCIIStringEncoding];
35- free (buffer);
42+ }
43+ nstr = [NSString stringWithCString: buffer encoding: NSASCIIStringEncoding];
44+ free (buffer);
3645 return (nstr);
3746}
3847
@@ -73,7 +82,7 @@ - (IBAction)setBitMap
7382 bitMap[DOT (2 )] = ' .' ;
7483 bitMap[DOT (3 )] = ' .' ;
7584 bitMap[35 ] = 0 ;
76-
85+
7786 for (i = 0 ; i < 35 ; i++)
7887 {
7988 if (i == DOT (1 ) || i == DOT (2 ) || i == DOT (3 ))
@@ -135,37 +144,35 @@ - (char)getClass:(unsigned int)address
135144 return (' d' );
136145}
137146
138- - (int )setClassInfo : (char )class : (const int )setDefaults
139- {
140- if (networkClass)
141- [networkClass release ];
147+ - (int )setClassInfo : (char )class defaults : (const int )setDefaults
148+ {
142149 switch (class)
143150 {
144151 case ' a' :
145- networkClass = [[ NSString alloc ] initWithString: @" A" ] ;
152+ networkClass = @" A" ;
146153 netBits = 8 ;
147154 netId = 0x01000000 ;
148155 mask = 0xff000000 ;
149156 break ;
150157 case ' b' :
151- networkClass = [[ NSString alloc ] initWithString: @" B" ] ;
158+ networkClass = @" B" ;
152159 netBits = 16 ;
153160 netId = 0x80000000 ;
154161 mask = 0xffff0000 ;
155162 break ;
156163 case ' c' :
157- networkClass = [[ NSString alloc ] initWithString: @" C" ] ;
164+ networkClass = @" C" ;
158165 netBits = 24 ;
159166 netId = 0xc0000000 ;
160167 mask = 0xffffff00 ;
161168 break ;
162- case ' d' :
163- networkClass = [[ NSString alloc ] initWithString: @" D" ] ;
164- break ;
169+ case ' d' :
170+ networkClass = @" D" ;
171+ break ;
165172 default :
166173 return (-1 );
167174 }
168- if (setDefaults)
175+ if (setDefaults)
169176 hostAddr = netId + 1 ;
170177 return (0 );
171178}
@@ -177,17 +184,17 @@ - (void)initByAddr:(unsigned int)address
177184 class = [self getClass: address];
178185 subnetBits = 0 ;
179186 hostAddr = address;
180- [self setClassInfo: class: 0 ];
187+ [self setClassInfo: class defaults :0 ];
181188 [self initNetwork ];
182189}
183190
184- - (void )initByAddrAndMask : (unsigned int )address : (unsigned int )addressMask
191+ - (void )initByAddrAndMask : (unsigned int )address mask : (unsigned int )addressMask
185192{
186193 char class;
187194
188195 class = [self getClass: address];
189196 hostAddr = address;
190- [self setClassInfo: class: 0 ];
197+ [self setClassInfo: class defaults :0 ];
191198 if (mask > addressMask)
192199 {
193200 mask = addressMask;
@@ -204,35 +211,41 @@ - (void)initByAddrAndMask:(unsigned int)address:(unsigned int)addressMask
204211
205212- (void )initAddress : (const char *)address
206213{
207- [self initByAddr: [IPSubnetCalc numberize: address]];
214+ if (address)
215+ {
216+ [self initByAddr: [IPSubnetCalc numberize: address]];
217+ }
208218
209219}
210220
211- - (void )initAddressAndMask : (const char *)address : (unsigned int )addressMask
221+ - (void )initAddressAndMask : (const char *)address mask : (unsigned int )addressMask
212222{
213- [self initByAddrAndMask: [IPSubnetCalc numberize: address]:addressMask];
223+ if (address)
224+ {
225+ [self initByAddrAndMask: [IPSubnetCalc numberize: address] mask: addressMask];
226+ }
214227
215228}
216229
217- - (void )initAddressAndMaskWithUnsignedInt : (unsigned int )address : (unsigned int )addressMask
230+ - (void )initAddressAndMaskWithUnsignedInt : (unsigned int )address mask : (unsigned int )addressMask
218231{
219- [self initByAddrAndMask: address: addressMask];
232+ [self initByAddrAndMask: address mask : addressMask];
220233
221234}
222235- (NSString *)subnetHostAddrRange
223236{
224237 NSString *strRange;
225238 unsigned int tmpmask;
226239
227- strRange = [[[ NSString alloc ] initWithString: [IPSubnetCalc denumberize: ((hostAddr & (mask | subnetMask)) + 1 )]] autorelease ];
240+ strRange = [[NSString alloc ] initWithString: [IPSubnetCalc denumberize: ((hostAddr & (mask | subnetMask)) + 1 )]];
228241 tmpmask = -1 ;
229242 tmpmask >>= maskBits;
230243 return ([strRange stringByAppendingFormat: @" - %@ " , [IPSubnetCalc denumberize: ((hostAddr | tmpmask) - 1 )]]);
231244}
232245
233246- (NSString *)bitMap
234247{
235- return ([NSString stringWithCString: bitMap encoding: NSASCIIStringEncoding]);
248+ return ([NSString stringWithCString: bitMap encoding: NSASCIIStringEncoding]);
236249}
237250
238251- (NSString *)binMap
@@ -241,8 +254,8 @@ - (NSString *)binMap
241254 unsigned int address;
242255 unsigned int mask_tmp = 1 ;
243256 int i;
244-
245- str_binmap = [[[ NSString alloc ] init ] autorelease ];
257+
258+ str_binmap = [[NSString alloc ] init ];
246259 address = hostAddr;
247260 mask_tmp <<= 31 ;
248261 for (i = 0 ; i < 32 ; i++)
@@ -251,7 +264,7 @@ - (NSString *)binMap
251264 str_binmap = [str_binmap stringByAppendingString: @" 1" ];
252265 else
253266 str_binmap = [str_binmap stringByAppendingString: @" 0" ];
254- address <<= 1 ;
267+ address <<= 1 ;
255268 if (i != 31 && ((i + 1 ) % 8 == 0 ))
256269 str_binmap = [str_binmap stringByAppendingString: @" ." ];
257270 }
@@ -263,14 +276,14 @@ - (NSString *)hexMap
263276 NSString *str_hexmap;
264277 unsigned int address;
265278 int i;
266-
267- str_hexmap = [[[ NSString alloc ] init ] autorelease ];
279+
280+ str_hexmap = [[NSString alloc ] init ];
268281 address = hostAddr;
269282 for (i = 0 ; i < 4 ; i++)
270283 {
271284 str_hexmap = [str_hexmap stringByAppendingFormat: @" %.2X " , ((address << (8 * i)) >> 24 )];
272285 if (i != 3 )
273- str_hexmap = [str_hexmap stringByAppendingString: @" ." ];
286+ str_hexmap = [str_hexmap stringByAppendingString: @" ." ];
274287 }
275288 return (str_hexmap);
276289}
@@ -312,7 +325,7 @@ - (NSString *)netId
312325
313326- (unsigned int )netIdIntValue
314327{
315- return (netId);
328+ return (netId);
316329}
317330
318331- (NSString *)subnetMask
@@ -322,7 +335,7 @@ - (NSString *)subnetMask
322335
323336- (unsigned int )subnetMaskIntValue
324337{
325- return (mask | subnetMask);
338+ return (mask | subnetMask);
326339}
327340
328341- (NSString *)subnetBroadcast
@@ -347,27 +360,36 @@ - (NSString *)networkClass
347360
348361- (unsigned int )netBits
349362{
350- return (netBits);
363+ return (netBits);
351364}
352365
353366- (NSString *)supernetRoute : (int )supernetMaskBits
354367{
355- unsigned int tmpmask = -1 ;
368+ unsigned int tmpmask = -1 ;
356369
357- tmpmask <<= (32 - supernetMaskBits);
370+ tmpmask <<= (32 - supernetMaskBits);
358371 return ([IPSubnetCalc denumberize: (hostAddr & tmpmask)]);
359372}
360373
361374- (NSString *)supernetAddrRange : (int )supernetMaskBits
362375{
363- NSString *strRange;
376+ NSString *strRange;
364377 unsigned int tmpmask = -1 ;
365378
366- tmpmask <<= (32 - supernetMaskBits);
367- strRange = [[[ NSString alloc ] initWithString: [IPSubnetCalc denumberize: (hostAddr & tmpmask)]] autorelease ];
379+ tmpmask <<= (32 - supernetMaskBits);
380+ strRange = [[NSString alloc ] initWithString: [IPSubnetCalc denumberize: (hostAddr & tmpmask)]];
368381 tmpmask = -1 ;
369382 tmpmask >>= supernetMaskBits;
370383 return ([strRange stringByAppendingFormat: @" - %@ " , [IPSubnetCalc denumberize: (hostAddr | tmpmask)]]);
371384}
372385
386+ - (id )init
387+ {
388+ self = [super init ];
389+ if (self) {
390+ [self setClassless: NO ];
391+ }
392+ return self;
393+ }
394+
373395@end
0 commit comments