Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "postgres.h"
- #include "fmgr.h"
- #include "catalog/pg_type.h"
- #include "utils/builtins.h"
- #include "utils/inet.h"
- #ifdef PG_MODULE_MAGIC
- PG_MODULE_MAGIC;
- #endif
- #define ip_bits(inetptr) \
- (((inet_struct *) VARDATA_ANY(inetptr))->bits)
- #define ip_addr(inetptr) \
- (((inet_struct *) VARDATA_ANY(inetptr))->ipaddr)
- Datum cidr_inc( PG_FUNCTION_ARGS);
- PG_FUNCTION_INFO_V1( cidr_inc );
- Datum
- cidr_inc( PG_FUNCTION_ARGS )
- {
- inet *src = PG_GETARG_INET_P(0);
- int bits = ip_bits(src); //netmask length
- inet *dst = (inet *) palloc(VARSIZE_ANY(src));
- memcpy(dst, src, VARSIZE_ANY(src));
- int i = bits / 8;
- ip_addr(dst)[i] += 1 << (8 - (bits % 8));
- while(i > 0 && ip_addr(dst)[i--] == 0){
- ++ip_addr(dst)[i];
- }
- PG_RETURN_INET_P( dst );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement