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

Commit eb3e640

Browse files
committed
New INET functions from D'Arcy J.M. Cain
1 parent 03ab5f0 commit eb3e640

File tree

5 files changed

+235
-10
lines changed

5 files changed

+235
-10
lines changed

src/backend/utils/adt/cash.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* workings can be found in the book "Software Solutions in C" by
1010
* Dale Schumacher, Academic Press, ISBN: 0-12-632360-7.
1111
*
12-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.26 1998/09/01 04:32:26 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/cash.c,v 1.27 1998/10/12 04:07:44 momjian Exp $
1313
*/
1414

1515
#include <stdio.h>
@@ -672,7 +672,7 @@ cashsmaller(Cash *c1, Cash *c2)
672672
* This converts a int4 as well but to a representation using words
673673
* Obviously way North American centric - sorry
674674
*/
675-
const char *
675+
text *
676676
cash_words_out(Cash *value)
677677
{
678678
static char buf[128];
@@ -681,7 +681,8 @@ cash_words_out(Cash *value)
681681
Cash m1;
682682
Cash m2;
683683
Cash m3;
684-
684+
text *result;
685+
685686
/* work with positive numbers */
686687
if (*value < 0)
687688
{
@@ -718,8 +719,16 @@ cash_words_out(Cash *value)
718719
strcat(buf, (int) (*value / 100) == 1 ? " dollar and " : " dollars and ");
719720
strcat(buf, num_word(m0));
720721
strcat(buf, m0 == 1 ? " cent" : " cents");
722+
723+
/* capitalize output */
721724
*buf = toupper(*buf);
722-
return buf;
725+
726+
/* make a text type for output */
727+
result = (text *) palloc(strlen(buf) + VARHDRSZ);
728+
VARSIZE(result) = strlen(buf) + VARHDRSZ;
729+
StrNCpy(VARDATA(result), buf, strlen(buf));
730+
731+
return result;
723732
} /* cash_words_out() */
724733

725734

src/backend/utils/adt/inet.c

Lines changed: 196 additions & 3 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.2 1998/10/08 02:08:44 momjian Exp $
6+
* $Id: inet.c,v 1.3 1998/10/12 04:07:46 momjian Exp $
77
*/
88

99
#include <sys/types.h>
@@ -57,7 +57,9 @@ inet_in(char *src)
5757
}
5858
/* First, try for an IP V4 address: */
5959
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
6163
if ((bits < 0) || (bits > 32))
6264
{
6365
/* Go for an IPV6 address here, before faulting out: */
@@ -84,13 +86,15 @@ inet_out(inet *src)
8486

8587
if (ip_family(src) == AF_INET)
8688
{
89+
#ifdef BAD
8790
/* 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),
8992
tmp, sizeof(tmp)) < 0)
9093
{
9194
elog(ERROR, "unable to print address (%s)", strerror(errno));
9295
return (NULL);
9396
}
97+
#endif
9498
}
9599
else
96100
{
@@ -265,6 +269,194 @@ inet_cmp(inet *a1, inet *a2)
265269
return 0;
266270
}
267271

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+
268460
/*
269461
* Bitwise comparison for V4 addresses. Add V6 implementation!
270462
*/
@@ -285,3 +477,4 @@ v4bitncmp(unsigned int a1, unsigned int a2, int bits)
285477
return (1);
286478
return (0);
287479
}
480+

src/include/catalog/pg_proc.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_proc.h,v 1.72 1998/10/08 00:19:40 momjian Exp $
9+
* $Id: pg_proc.h,v 1.73 1998/10/12 04:07:48 momjian Exp $
1010
*
1111
* NOTES
1212
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2097,6 +2097,19 @@ DESCR("is-supernet");
20972097
DATA(insert OID = 930 ( inet_supeq PGUID 11 f t f 2 f 16 "869 869" 100 0 0 100 foo bar ));
20982098
DESCR("is-supernet-or-equal");
20992099

2100+
DATA(insert OID = 940 ( inet_netmask PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
2101+
DESCR("netmask of inet address");
2102+
DATA(insert OID = 941 ( inet_masklen PGUID 11 f t f 1 f 23 "869" 100 0 0 100 foo bar ));
2103+
DESCR("netmask length");
2104+
DATA(insert OID = 942 ( inet_host PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
2105+
DESCR("host adress");
2106+
DATA(insert OID = 943 ( inet_network_without_bits PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
2107+
DESCR("netmask without bits");
2108+
DATA(insert OID = 944 ( inet_network_with_bits PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
2109+
DESCR("netmask with bits");
2110+
DATA(insert OID = 945 ( inet_broadcast PGUID 11 f t f 1 f 25 "869" 100 0 0 100 foo bar ));
2111+
DESCR("broadcast address");
2112+
21002113

21012114
/*
21022115
* prototypes for functions pg_proc.c

src/include/utils/builtins.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: builtins.h,v 1.60 1998/10/08 18:30:49 momjian Exp $
9+
* $Id: builtins.h,v 1.61 1998/10/12 04:07:51 momjian Exp $
1010
*
1111
* NOTES
1212
* This should normally only be included by fmgr.h.
@@ -515,6 +515,9 @@ char *inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size);
515515
/* inet_net_pton.c */
516516
int inet_net_pton(int af, const char *src, void *dst, size_t size);
517517

518+
char *inet_cidr_ntop(int af, const void *src, size_t len, int bits, char *dst, size_t size);
519+
int inet_cidr_pton(int af, const void *src, void *dst, size_t size, int *used);
520+
518521
/* inet.c */
519522
inet *inet_in(char *str);
520523
char *inet_out(inet * addr);
@@ -530,6 +533,13 @@ bool inet_sup(inet * a1, inet * a2);
530533
bool inet_supeq(inet * a1, inet * a2);
531534
int4 inet_cmp(inet * a1, inet * a2);
532535

536+
text *inet_netmask(inet * addr);
537+
int4 inet_masklen(inet * addr);
538+
text *inet_host(inet * addr);
539+
text *inet_network_without_bits(inet * addr);
540+
text *inet_network_with_bits(inet * addr);
541+
text *inet_broadcast(inet * addr);
542+
533543

534544
/* mac.c */
535545
macaddr *macaddr_in(char *str);

src/include/utils/cash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ extern Cash *int2_mul_cash(int2 s, Cash *c);
4444
extern Cash *cashlarger(Cash *c1, Cash *c2);
4545
extern Cash *cashsmaller(Cash *c1, Cash *c2);
4646

47-
extern const char *cash_words_out(Cash *value);
47+
extern text *cash_words_out(Cash *value);
4848

4949
#endif /* CASH_H */

0 commit comments

Comments
 (0)