3
3
* is for IP V4 CIDR notation, but prepared for V6: just
4
4
* add the necessary bits where the comments indicate.
5
5
*
6
- * $Id: inet.c,v 1.2 1998/10/08 02:08:44 momjian Exp $
6
+ * $Id: inet.c,v 1.3 1998/10/12 04:07:46 momjian Exp $
7
7
*/
8
8
9
9
#include <sys/types.h>
@@ -57,7 +57,9 @@ inet_in(char *src)
57
57
}
58
58
/* First, try for an IP V4 address: */
59
59
ip_family (dst ) = AF_INET ;
60
- bits = inet_net_pton (ip_family (dst ), src , & ip_v4addr (dst ), ip_addrsize (dst ));
60
+ #ifdef BAD
61
+ bits = inet_net_pton (ip_family (dst ), src , & ip_v4addr (dst ), ip_addrsize (dst ), NULL );
62
+ #endif
61
63
if ((bits < 0 ) || (bits > 32 ))
62
64
{
63
65
/* Go for an IPV6 address here, before faulting out: */
@@ -84,13 +86,15 @@ inet_out(inet *src)
84
86
85
87
if (ip_family (src ) == AF_INET )
86
88
{
89
+ #ifdef BAD
87
90
/* It's an IP V4 address: */
88
- if (inet_net_ntop (AF_INET , & ip_v4addr (src ), ip_bits (src ),
91
+ if (inet_net_ntop (AF_INET , & ip_v4addr (src ), 4 , ip_bits (src ),
89
92
tmp , sizeof (tmp )) < 0 )
90
93
{
91
94
elog (ERROR , "unable to print address (%s)" , strerror (errno ));
92
95
return (NULL );
93
96
}
97
+ #endif
94
98
}
95
99
else
96
100
{
@@ -265,6 +269,194 @@ inet_cmp(inet *a1, inet *a2)
265
269
return 0 ;
266
270
}
267
271
272
+ text *
273
+ inet_netmask (inet * ip )
274
+ {
275
+ char * dst ,
276
+ tmp [sizeof ("255.255.255.255/32" )];
277
+
278
+ if (ip_family (ip ) == AF_INET )
279
+ {
280
+ /* It's an IP V4 address: */
281
+ int addr = -1 << (32 - ip_bits (ip ));
282
+
283
+ /* a little wasteful by why reinvent the wheel? */
284
+ #ifdef BAD
285
+ if (inet_cidr_ntop (AF_INET , & addr , 4 , -1 , tmp , sizeof (tmp )) < 0 )
286
+ {
287
+ elog (ERROR , "unable to print netmask (%s)" , strerror (errno ));
288
+ return (NULL );
289
+ }
290
+ #endif
291
+ }
292
+ else
293
+ {
294
+ /* Go for an IPV6 address here, before faulting out: */
295
+ elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
296
+ return (NULL );
297
+ }
298
+ dst = palloc (strlen (tmp ) + 1 );
299
+ if (dst == NULL )
300
+ {
301
+ elog (ERROR , "unable to allocate memory in inet_netmask()" );
302
+ return (NULL );
303
+ }
304
+ strcpy (dst , tmp );
305
+ return (dst );
306
+
307
+ }
308
+
309
+ int4
310
+ inet_masklen (inet * ip )
311
+ {
312
+ return ip_bits (ip );
313
+ }
314
+
315
+ text *
316
+ inet_host (inet * ip )
317
+ {
318
+ char * dst ,
319
+ tmp [sizeof ("255.255.255.255/32" )];
320
+
321
+ if (ip_family (ip ) == AF_INET )
322
+ {
323
+ #ifdef BAD
324
+ /* It's an IP V4 address: */
325
+ if (inet_cidr_ntop (AF_INET , & ip_v4addr (ip ), 4 , -1 ,
326
+ tmp , sizeof (tmp )) < 0 )
327
+ {
328
+ elog (ERROR , "unable to print host (%s)" , strerror (errno ));
329
+ return (NULL );
330
+ }
331
+ #endif
332
+ }
333
+ else
334
+ {
335
+ /* Go for an IPV6 address here, before faulting out: */
336
+ elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
337
+ return (NULL );
338
+ }
339
+ dst = palloc (strlen (tmp ) + 1 );
340
+ if (dst == NULL )
341
+ {
342
+ elog (ERROR , "unable to allocate memory in inet_out()" );
343
+ return (NULL );
344
+ }
345
+ strcpy (dst , tmp );
346
+ return (dst );
347
+
348
+ }
349
+
350
+ text *
351
+ inet_network_without_bits (inet * ip )
352
+ {
353
+ char * dst ,
354
+ tmp [sizeof ("255.255.255.255/32" )];
355
+
356
+ if (ip_family (ip ) == AF_INET )
357
+ {
358
+ /* It's an IP V4 address: */
359
+ int addr = ip_v4addr (ip ) & (-1 << (32 - ip_bits (ip )));
360
+ #ifdef BAD
361
+
362
+ if (inet_cidr_ntop (AF_INET , & addr , (int )(ip_bits (ip )/8 ), -1 ,
363
+ tmp , sizeof (tmp )) < 0 )
364
+ {
365
+ elog (ERROR , "unable to print address (%s)" , strerror (errno ));
366
+ return (NULL );
367
+ }
368
+ #endif
369
+ }
370
+ else
371
+ {
372
+ /* Go for an IPV6 address here, before faulting out: */
373
+ elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
374
+ return (NULL );
375
+ }
376
+ dst = palloc (strlen (tmp ) + 1 );
377
+ if (dst == NULL )
378
+ {
379
+ elog (ERROR , "unable to allocate memory in inet_out()" );
380
+ return (NULL );
381
+ }
382
+ strcpy (dst , tmp );
383
+ return (dst );
384
+
385
+ }
386
+
387
+ text *
388
+ inet_network_with_bits (inet * ip )
389
+ {
390
+ char * dst ,
391
+ tmp [sizeof ("255.255.255.255/32" )];
392
+
393
+ if (ip_family (ip ) == AF_INET )
394
+ {
395
+ /* It's an IP V4 address: */
396
+ int addr = ip_v4addr (ip ) & (-1 << (32 - ip_bits (ip )));
397
+ #ifdef BAD
398
+
399
+ if (inet_cidr_ntop (AF_INET , & addr , (int )(ip_bits (ip )/8 ),
400
+ ip_bits (ip ), tmp , sizeof (tmp )) < 0 )
401
+ {
402
+ elog (ERROR , "unable to print address (%s)" , strerror (errno ));
403
+ return (NULL );
404
+ }
405
+ #endif
406
+ }
407
+ else
408
+ {
409
+ /* Go for an IPV6 address here, before faulting out: */
410
+ elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
411
+ return (NULL );
412
+ }
413
+ dst = palloc (strlen (tmp ) + 1 );
414
+ if (dst == NULL )
415
+ {
416
+ elog (ERROR , "unable to allocate memory in inet_out()" );
417
+ return (NULL );
418
+ }
419
+ strcpy (dst , tmp );
420
+ return (dst );
421
+
422
+ }
423
+
424
+ text *
425
+ inet_broadcast (inet * ip )
426
+ {
427
+ char * dst ,
428
+ tmp [sizeof ("255.255.255.255/32" )];
429
+
430
+ if (ip_family (ip ) == AF_INET )
431
+ {
432
+ /* It's an IP V4 address: */
433
+ int addr = ip_v4addr (ip ) | ~(-1 << (32 - ip_bits (ip )));
434
+ #ifdef BAD
435
+
436
+ if (inet_cidr_ntop (AF_INET ,& addr ,4 ,ip_bits (ip ),tmp ,sizeof (tmp )) < 0 )
437
+ {
438
+ elog (ERROR , "unable to print address (%s)" , strerror (errno ));
439
+ return (NULL );
440
+ }
441
+ #endif
442
+ }
443
+ else
444
+ {
445
+ /* Go for an IPV6 address here, before faulting out: */
446
+ elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
447
+ return (NULL );
448
+ }
449
+ dst = palloc (strlen (tmp ) + 1 );
450
+ if (dst == NULL )
451
+ {
452
+ elog (ERROR , "unable to allocate memory in inet_out()" );
453
+ return (NULL );
454
+ }
455
+ strcpy (dst , tmp );
456
+ return (dst );
457
+
458
+ }
459
+
268
460
/*
269
461
* Bitwise comparison for V4 addresses. Add V6 implementation!
270
462
*/
@@ -285,3 +477,4 @@ v4bitncmp(unsigned int a1, unsigned int a2, int bits)
285
477
return (1 );
286
478
return (0 );
287
479
}
480
+
0 commit comments