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.6 1998/10/20 23:03:19 momjian Exp $
6
+ * $Id: inet.c,v 1.7 1998/10/21 02:48:22 momjian Exp $
7
7
*/
8
8
9
9
#include <sys/types.h>
@@ -57,9 +57,7 @@ inet_in(char *src)
57
57
}
58
58
/* First, try for an IP V4 address: */
59
59
ip_family (dst ) = AF_INET ;
60
- #ifdef BAD
61
- bits = inet_net_pton (ip_family (dst ), src , & ip_v4addr (dst ), ip_addrsize (dst ), NULL );
62
- #endif
60
+ bits = inet_net_pton (ip_family (dst ), src , & ip_v4addr (dst ), ip_addrsize (dst ));
63
61
if ((bits < 0 ) || (bits > 32 ))
64
62
{
65
63
/* Go for an IPV6 address here, before faulting out: */
@@ -87,14 +85,12 @@ inet_out(inet *src)
87
85
if (ip_family (src ) == AF_INET )
88
86
{
89
87
/* It's an IP V4 address: */
90
- #ifdef BAD
91
- if (inet_net_ntop (AF_INET , & ip_v4addr (src ), 4 , ip_bits (src ),
88
+ if (inet_net_ntop (AF_INET , & ip_v4addr (src ), ip_bits (src ),
92
89
tmp , sizeof (tmp )) < 0 )
93
90
{
94
91
elog (ERROR , "unable to print address (%s)" , strerror (errno ));
95
92
return (NULL );
96
93
}
97
- #endif
98
94
}
99
95
else
100
96
{
@@ -272,38 +268,41 @@ inet_cmp(inet *a1, inet *a2)
272
268
text *
273
269
inet_netmask (inet * ip )
274
270
{
275
- char * dst ,
271
+ text * ret ;
272
+ int len ;
273
+ char * ptr ,
276
274
tmp [sizeof ("255.255.255.255/32" )];
277
275
278
276
if (ip_family (ip ) == AF_INET )
279
277
{
280
278
/* It's an IP V4 address: */
281
- int addr = -1 << (32 - ip_bits (ip ));
279
+ int addr = htonl (( -1 << (32 - ip_bits (ip ))) & 0xffffffff );
282
280
283
281
/* 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 )
282
+ if (inet_net_ntop (AF_INET , & addr , 32 , tmp , sizeof (tmp )) < 0 )
286
283
{
287
284
elog (ERROR , "unable to print netmask (%s)" , strerror (errno ));
288
285
return (NULL );
289
286
}
290
- #endif
291
287
}
292
288
else
293
289
{
294
290
/* Go for an IPV6 address here, before faulting out: */
295
291
elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
296
292
return (NULL );
297
293
}
298
- dst = palloc (strlen (tmp ) + 1 );
299
- if (dst == NULL )
294
+ if ((ptr = strchr (tmp , '/' )) != NULL )
295
+ * ptr = 0 ;
296
+ len = VARHDRSZ + strlen (tmp );
297
+ ret = palloc (len );
298
+ if (ret == NULL )
300
299
{
301
300
elog (ERROR , "unable to allocate memory in inet_netmask()" );
302
301
return (NULL );
303
302
}
304
- strcpy ( dst , tmp ) ;
305
- return ( dst );
306
-
303
+ VARSIZE ( ret ) = len ;
304
+ strcpy ( VARDATA ( ret ), tmp );
305
+ return ( ret );
307
306
}
308
307
309
308
int4
@@ -315,37 +314,41 @@ inet_masklen(inet *ip)
315
314
text *
316
315
inet_broadcast (inet * ip )
317
316
{
318
- char * dst ,
319
- tmp [sizeof ("255.255.255.255/32" )];
317
+ text * ret ;
318
+ int len ;
319
+ char * ptr ,
320
+ tmp [sizeof ("255.255.255.255/32" )] = "Hello" ;
320
321
321
322
if (ip_family (ip ) == AF_INET )
322
323
{
323
324
/* It's an IP V4 address: */
324
- int addr = ip_v4addr (ip ) | ~( -1 << ( 32 - ip_bits (ip )));
325
- #ifdef BAD
325
+ int addr = htonl ( ntohl ( ip_v4addr (ip )) | ( 0xffffffff >> ip_bits (ip )));
326
+ /* int addr = htonl(ip_v4addr(ip) | (0xffffffff >> ip_bits(ip))); */
326
327
327
- if (inet_cidr_ntop (AF_INET ,& addr ,4 , ip_bits ( ip ), tmp ,sizeof (tmp )) < 0 )
328
+ if (inet_net_ntop (AF_INET , & addr , 32 , tmp , sizeof (tmp )) < 0 )
328
329
{
329
330
elog (ERROR , "unable to print address (%s)" , strerror (errno ));
330
331
return (NULL );
331
332
}
332
- #endif
333
333
}
334
334
else
335
335
{
336
336
/* Go for an IPV6 address here, before faulting out: */
337
337
elog (ERROR , "unknown address family (%d)" , ip_family (ip ));
338
338
return (NULL );
339
339
}
340
- dst = palloc (strlen (tmp ) + 1 );
341
- if (dst == NULL )
340
+ if ((ptr = strchr (tmp , '/' )) != NULL )
341
+ * ptr = 0 ;
342
+ len = VARHDRSZ + strlen (tmp );
343
+ ret = palloc (len );
344
+ if (ret == NULL )
342
345
{
343
- elog (ERROR , "unable to allocate memory in inet_out ()" );
346
+ elog (ERROR , "unable to allocate memory in inet_broadcast ()" );
344
347
return (NULL );
345
348
}
346
- strcpy ( dst , tmp ) ;
347
- return ( dst );
348
-
349
+ VARSIZE ( ret ) = len ;
350
+ strcpy ( VARDATA ( ret ), tmp );
351
+ return ( ret );
349
352
}
350
353
351
354
/*
0 commit comments