*** ./backend/utils/adt/inet_net_pton.c.orig Tue Jul 4 23:00:06 2000 --- ./backend/utils/adt/inet_net_pton.c Wed Jul 5 11:11:32 2000 *************** *** 101,107 **** tmp, dirty, bits; ! const u_char *odst = dst; ch = *src++; if (ch == '0' && (src[0] == 'x' || src[0] == 'X') --- 101,107 ---- tmp, dirty, bits; ! u_char *odst = dst; ch = *src++; if (ch == '0' && (src[0] == 'x' || src[0] == 'X') *************** *** 213,218 **** --- 213,226 ---- /* If imputed mask is narrower than specified octets, widen. */ if (bits >= 8 && bits < ((dst - odst) * 8)) bits = (dst - odst) * 8; + } + /* Zero host bits if any */ + n = bits/8; + if( n < (dst - odst) ) + { + odst[n++] &= UCHAR_MAX<<(8 - (bits % 8)); + for (;n < (dst - odst); n++) + odst[n]='\0'; } /* Extend network to cover the actual mask. */ while (bits > ((dst - odst) * 8)) *** ./backend/utils/adt/network.c.orig Tue Jul 4 23:02:01 2000 --- ./backend/utils/adt/network.c Tue Jul 4 23:35:21 2000 *************** *** 18,23 **** --- 18,24 ---- #include "postgres.h" #include "utils/builtins.h" + static int v4bitcmp(unsigned int a1, unsigned int a2); static int v4bitncmp(unsigned int a1, unsigned int a2, int bits); /* *************** *** 137,143 **** return FALSE; if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) { ! int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)); return ((order < 0) || ((order == 0) && (ip_bits(a1) < ip_bits(a2)))); } --- 138,144 ---- return FALSE; if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) { ! int order = v4bitcmp(ip_v4addr(a1), ip_v4addr(a2)); return ((order < 0) || ((order == 0) && (ip_bits(a1) < ip_bits(a2)))); } *************** *** 166,172 **** if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) { return ((ip_bits(a1) == ip_bits(a2)) ! && (v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a1)) == 0)); } else { --- 167,173 ---- if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) { return ((ip_bits(a1) == ip_bits(a2)) ! && (v4bitcmp(ip_v4addr(a1), ip_v4addr(a2)) == 0)); } else { *************** *** 192,198 **** return FALSE; if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) { ! int order = v4bitncmp(ip_v4addr(a1), ip_v4addr(a2), ip_bits(a2)); return ((order > 0) || ((order == 0) && (ip_bits(a1) > ip_bits(a2)))); } --- 193,199 ---- return FALSE; if ((ip_family(a1) == AF_INET) && (ip_family(a2) == AF_INET)) { ! int order = v4bitcmp(ip_v4addr(a1), ip_v4addr(a2)); return ((order > 0) || ((order == 0) && (ip_bits(a1) > ip_bits(a2)))); } *************** *** 341,353 **** if ((ptr = strchr(tmp, '/')) != NULL) *ptr = 0; ! len = VARHDRSZ + strlen(tmp) + 1; ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_host()"); VARSIZE(ret) = len; ! strcpy(VARDATA(ret), tmp); return (ret); } --- 342,354 ---- if ((ptr = strchr(tmp, '/')) != NULL) *ptr = 0; ! len = VARHDRSZ + strlen(tmp); ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_host()"); VARSIZE(ret) = len; ! memcpy(VARDATA(ret), tmp, len-VARHDRSZ); return (ret); } *************** *** 391,403 **** if ((ptr = strchr(tmp, '/')) != NULL) *ptr = 0; ! len = VARHDRSZ + strlen(tmp) + 1; ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_broadcast()"); VARSIZE(ret) = len; ! strcpy(VARDATA(ret), tmp); return (ret); } --- 392,404 ---- if ((ptr = strchr(tmp, '/')) != NULL) *ptr = 0; ! len = VARHDRSZ + strlen(tmp); ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_broadcast()"); VARSIZE(ret) = len; ! memcpy(VARDATA(ret), tmp, len-VARHDRSZ); return (ret); } *************** *** 424,436 **** /* Go for an IPV6 address here, before faulting out: */ elog(ERROR, "unknown address family (%d)", ip_family(ip)); ! len = VARHDRSZ + strlen(tmp) + 1; ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_network()"); VARSIZE(ret) = len; ! strcpy(VARDATA(ret), tmp); return (ret); } --- 425,437 ---- /* Go for an IPV6 address here, before faulting out: */ elog(ERROR, "unknown address family (%d)", ip_family(ip)); ! len = VARHDRSZ + strlen(tmp); ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_network()"); VARSIZE(ret) = len; ! memcpy(VARDATA(ret), tmp, len-VARHDRSZ); return (ret); } *************** *** 461,479 **** if ((ptr = strchr(tmp, '/')) != NULL) *ptr = 0; ! len = VARHDRSZ + strlen(tmp) + 1; ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_netmask()"); VARSIZE(ret) = len; ! strcpy(VARDATA(ret), tmp); return (ret); } /* * Bitwise comparison for V4 addresses. Add V6 implementation! */ static int v4bitncmp(unsigned int a1, unsigned int a2, int bits) --- 462,491 ---- if ((ptr = strchr(tmp, '/')) != NULL) *ptr = 0; ! len = VARHDRSZ + strlen(tmp); ret = palloc(len); if (ret == NULL) elog(ERROR, "unable to allocate memory in network_netmask()"); VARSIZE(ret) = len; ! memcpy(VARDATA(ret), tmp, len-VARHDRSZ); return (ret); } /* * Bitwise comparison for V4 addresses. Add V6 implementation! */ + + static int + v4bitcmp(unsigned int a1, unsigned int a2) + { + a1 = ntohl(a1); + a2 = ntohl(a2); + if (a1 < a2) + return (-1); + else + return (a1 > a2); + } static int v4bitncmp(unsigned int a1, unsigned int a2, int bits)