33 *is for IP V4 CIDR notation, but prepared for V6: just
44 *add the necessary bits where the comments indicate.
55 *
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 $
77 */
88
99#include <sys/types.h>
@@ -57,9 +57,7 @@ inet_in(char *src)
5757}
5858/* First, try for an IP V4 address: */
5959ip_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 ));
6361if ((bits < 0 )|| (bits > 32 ))
6462{
6563/* Go for an IPV6 address here, before faulting out: */
@@ -87,14 +85,12 @@ inet_out(inet *src)
8785if (ip_family (src )== AF_INET )
8886{
8987/* 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 ),
9289tmp ,sizeof (tmp ))< 0 )
9390{
9491elog (ERROR ,"unable to print address (%s)" ,strerror (errno ));
9592return (NULL );
9693}
97- #endif
9894}
9995else
10096{
@@ -272,38 +268,41 @@ inet_cmp(inet *a1, inet *a2)
272268text *
273269inet_netmask (inet * ip )
274270{
275- char * dst ,
271+ text * ret ;
272+ int len ;
273+ char * ptr ,
276274tmp [sizeof ("255.255.255.255/32" )];
277275
278276if (ip_family (ip )== AF_INET )
279277{
280278/* It's an IP V4 address: */
281- int addr = -1 << (32 - ip_bits (ip ));
279+ int addr = htonl (( -1 << (32 - ip_bits (ip ))) & 0xffffffff );
282280
283281/* 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 )
286283{
287284elog (ERROR ,"unable to print netmask (%s)" ,strerror (errno ));
288285return (NULL );
289286}
290- #endif
291287}
292288else
293289{
294290/* Go for an IPV6 address here, before faulting out: */
295291elog (ERROR ,"unknown address family (%d)" ,ip_family (ip ));
296292return (NULL );
297293}
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 )
300299{
301300elog (ERROR ,"unable to allocate memory in inet_netmask()" );
302301return (NULL );
303302}
304- strcpy ( dst , tmp ) ;
305- return ( dst );
306-
303+ VARSIZE ( ret ) = len ;
304+ strcpy ( VARDATA ( ret ), tmp );
305+ return ( ret );
307306}
308307
309308int4
@@ -315,37 +314,41 @@ inet_masklen(inet *ip)
315314text *
316315inet_broadcast (inet * ip )
317316{
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" ;
320321
321322if (ip_family (ip )== AF_INET )
322323{
323324/* 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))); */
326327
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 )
328329{
329330elog (ERROR ,"unable to print address (%s)" ,strerror (errno ));
330331return (NULL );
331332}
332- #endif
333333}
334334else
335335{
336336/* Go for an IPV6 address here, before faulting out: */
337337elog (ERROR ,"unknown address family (%d)" ,ip_family (ip ));
338338return (NULL );
339339}
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 )
342345{
343- elog (ERROR ,"unable to allocate memory ininet_out ()" );
346+ elog (ERROR ,"unable to allocate memory ininet_broadcast ()" );
344347return (NULL );
345348}
346- strcpy ( dst , tmp ) ;
347- return ( dst );
348-
349+ VARSIZE ( ret ) = len ;
350+ strcpy ( VARDATA ( ret ), tmp );
351+ return ( ret );
349352}
350353
351354/*