Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f804212

Browse files
committed
INET fix from D'Arcy.
1 parent 30d5e18 commit f804212

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/backend/utils/adt/inet.c

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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: */
5959
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));
6361
if ((bits < 0) || (bits > 32))
6462
{
6563
/* Go for an IPV6 address here, before faulting out: */
@@ -87,14 +85,12 @@ inet_out(inet *src)
8785
if (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),
9289
tmp, sizeof(tmp)) < 0)
9390
{
9491
elog(ERROR, "unable to print address (%s)", strerror(errno));
9592
return (NULL);
9693
}
97-
#endif
9894
}
9995
else
10096
{
@@ -272,38 +268,41 @@ inet_cmp(inet *a1, inet *a2)
272268
text *
273269
inet_netmask(inet *ip)
274270
{
275-
char *dst,
271+
text *ret;
272+
int len;
273+
char *ptr,
276274
tmp[sizeof("255.255.255.255/32")];
277275

278276
if (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
{
287284
elog(ERROR, "unable to print netmask (%s)", strerror(errno));
288285
return (NULL);
289286
}
290-
#endif
291287
}
292288
else
293289
{
294290
/* Go for an IPV6 address here, before faulting out: */
295291
elog(ERROR, "unknown address family (%d)", ip_family(ip));
296292
return (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
{
301300
elog(ERROR, "unable to allocate memory in inet_netmask()");
302301
return (NULL);
303302
}
304-
strcpy(dst, tmp);
305-
return (dst);
306-
303+
VARSIZE(ret) = len;
304+
strcpy(VARDATA(ret), tmp);
305+
return (ret);
307306
}
308307

309308
int4
@@ -315,37 +314,41 @@ inet_masklen(inet *ip)
315314
text *
316315
inet_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

321322
if (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
{
329330
elog(ERROR, "unable to print address (%s)", strerror(errno));
330331
return (NULL);
331332
}
332-
#endif
333333
}
334334
else
335335
{
336336
/* Go for an IPV6 address here, before faulting out: */
337337
elog(ERROR, "unknown address family (%d)", ip_family(ip));
338338
return (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 in inet_out()");
346+
elog(ERROR, "unable to allocate memory in inet_broadcast()");
344347
return (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
/*

0 commit comments

Comments
 (0)