⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 network.c

📁 postgresql8.3.4源码,开源数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *	PostgreSQL type definitions for the INET and CIDR types. * *	$PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.72 2007/11/15 21:14:39 momjian Exp $ * *	Jon Postel RIP 16 Oct 1998 */#include "postgres.h"#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include "access/hash.h"#include "catalog/pg_type.h"#include "libpq/ip.h"#include "libpq/libpq-be.h"#include "libpq/pqformat.h"#include "miscadmin.h"#include "utils/builtins.h"#include "utils/inet.h"static int32 network_cmp_internal(inet *a1, inet *a2);static int	bitncmp(void *l, void *r, int n);static bool addressOK(unsigned char *a, int bits, int family);static int	ip_addrsize(inet *inetptr);static inet *internal_inetpl(inet *ip, int64 addend);/* *	Access macros.	We use VARDATA_ANY so that we can process short-header *	varlena values without detoasting them.  This requires a trick: *	VARDATA_ANY assumes the varlena header is already filled in, which is *	not the case when constructing a new value (until SET_INET_VARSIZE is *	called, which we typically can't do till the end).  Therefore, we *	always initialize the newly-allocated value to zeroes (using palloc0). *	A zero length word will look like the not-1-byte case to VARDATA_ANY, *	and so we correctly construct an uncompressed value. * *	Note that ip_maxbits() and SET_INET_VARSIZE() require *	the family field to be set correctly. */#define ip_family(inetptr) \	(((inet_struct *) VARDATA_ANY(inetptr))->family)#define ip_bits(inetptr) \	(((inet_struct *) VARDATA_ANY(inetptr))->bits)#define ip_addr(inetptr) \	(((inet_struct *) VARDATA_ANY(inetptr))->ipaddr)#define ip_maxbits(inetptr) \	(ip_family(inetptr) == PGSQL_AF_INET ? 32 : 128)#define SET_INET_VARSIZE(dst) \	SET_VARSIZE(dst, VARHDRSZ + offsetof(inet_struct, ipaddr) + \				ip_addrsize(dst))/* * Return the number of bytes of address storage needed for this data type. */static intip_addrsize(inet *inetptr){	switch (ip_family(inetptr))	{		case PGSQL_AF_INET:			return 4;		case PGSQL_AF_INET6:			return 16;		default:			return 0;	}}/* * Common INET/CIDR input routine */static inet *network_in(char *src, bool is_cidr){	int			bits;	inet	   *dst;	dst = (inet *) palloc0(sizeof(inet));	/*	 * First, check to see if this is an IPv6 or IPv4 address.	IPv6 addresses	 * will have a : somewhere in them (several, in fact) so if there is one	 * present, assume it's V6, otherwise assume it's V4.	 */	if (strchr(src, ':') != NULL)		ip_family(dst) = PGSQL_AF_INET6;	else		ip_family(dst) = PGSQL_AF_INET;	bits = inet_net_pton(ip_family(dst), src, ip_addr(dst),						 is_cidr ? ip_addrsize(dst) : -1);	if ((bits < 0) || (bits > ip_maxbits(dst)))		ereport(ERROR,				(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),		/* translator: first %s is inet or cidr */				 errmsg("invalid input syntax for type %s: \"%s\"",						is_cidr ? "cidr" : "inet", src)));	/*	 * Error check: CIDR values must not have any bits set beyond the masklen.	 */	if (is_cidr)	{		if (!addressOK(ip_addr(dst), bits, ip_family(dst)))			ereport(ERROR,					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),					 errmsg("invalid cidr value: \"%s\"", src),					 errdetail("Value has bits set to right of mask.")));	}	ip_bits(dst) = bits;	SET_INET_VARSIZE(dst);	return dst;}Datuminet_in(PG_FUNCTION_ARGS){	char	   *src = PG_GETARG_CSTRING(0);	PG_RETURN_INET_P(network_in(src, false));}Datumcidr_in(PG_FUNCTION_ARGS){	char	   *src = PG_GETARG_CSTRING(0);	PG_RETURN_INET_P(network_in(src, true));}/* * Common INET/CIDR output routine */static char *network_out(inet *src, bool is_cidr){	char		tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")];	char	   *dst;	int			len;	dst = inet_net_ntop(ip_family(src), ip_addr(src), ip_bits(src),						tmp, sizeof(tmp));	if (dst == NULL)		ereport(ERROR,				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),				 errmsg("could not format inet value: %m")));	/* For CIDR, add /n if not present */	if (is_cidr && strchr(tmp, '/') == NULL)	{		len = strlen(tmp);		snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(src));	}	return pstrdup(tmp);}Datuminet_out(PG_FUNCTION_ARGS){	inet	   *src = PG_GETARG_INET_P(0);	PG_RETURN_CSTRING(network_out(src, false));}Datumcidr_out(PG_FUNCTION_ARGS){	inet	   *src = PG_GETARG_INET_P(0);	PG_RETURN_CSTRING(network_out(src, true));}/* *		network_recv		- converts external binary format to inet * * The external representation is (one byte apiece for) * family, bits, is_cidr, address length, address in network byte order. * * Presence of is_cidr is largely for historical reasons, though it might * allow some code-sharing on the client side.	We send it correctly on * output, but ignore the value on input. */static inet *network_recv(StringInfo buf, bool is_cidr){	inet	   *addr;	char	   *addrptr;	int			bits;	int			nb,				i;	/* make sure any unused bits in a CIDR value are zeroed */	addr = (inet *) palloc0(sizeof(inet));	ip_family(addr) = pq_getmsgbyte(buf);	if (ip_family(addr) != PGSQL_AF_INET &&		ip_family(addr) != PGSQL_AF_INET6)		ereport(ERROR,				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),		/* translator: %s is inet or cidr */				 errmsg("invalid address family in external \"%s\" value",						is_cidr ? "cidr" : "inet")));	bits = pq_getmsgbyte(buf);	if (bits < 0 || bits > ip_maxbits(addr))		ereport(ERROR,				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),		/* translator: %s is inet or cidr */				 errmsg("invalid bits in external \"%s\" value",						is_cidr ? "cidr" : "inet")));	ip_bits(addr) = bits;	i = pq_getmsgbyte(buf);		/* ignore is_cidr */	nb = pq_getmsgbyte(buf);	if (nb != ip_addrsize(addr))		ereport(ERROR,				(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),		/* translator: %s is inet or cidr */				 errmsg("invalid length in external \"%s\" value",						is_cidr ? "cidr" : "inet")));	addrptr = (char *) ip_addr(addr);	for (i = 0; i < nb; i++)		addrptr[i] = pq_getmsgbyte(buf);	/*	 * Error check: CIDR values must not have any bits set beyond the masklen.	 */	if (is_cidr)	{		if (!addressOK(ip_addr(addr), bits, ip_family(addr)))			ereport(ERROR,					(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),					 errmsg("invalid external \"cidr\" value"),					 errdetail("Value has bits set to right of mask.")));	}	SET_INET_VARSIZE(addr);	return addr;}Datuminet_recv(PG_FUNCTION_ARGS){	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);	PG_RETURN_INET_P(network_recv(buf, false));}Datumcidr_recv(PG_FUNCTION_ARGS){	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);	PG_RETURN_INET_P(network_recv(buf, true));}/* *		network_send		- converts inet to binary format */static bytea *network_send(inet *addr, bool is_cidr){	StringInfoData buf;	char	   *addrptr;	int			nb,				i;	pq_begintypsend(&buf);	pq_sendbyte(&buf, ip_family(addr));	pq_sendbyte(&buf, ip_bits(addr));	pq_sendbyte(&buf, is_cidr);	nb = ip_addrsize(addr);	if (nb < 0)		nb = 0;	pq_sendbyte(&buf, nb);	addrptr = (char *) ip_addr(addr);	for (i = 0; i < nb; i++)		pq_sendbyte(&buf, addrptr[i]);	return pq_endtypsend(&buf);}Datuminet_send(PG_FUNCTION_ARGS){	inet	   *addr = PG_GETARG_INET_P(0);	PG_RETURN_BYTEA_P(network_send(addr, false));}Datumcidr_send(PG_FUNCTION_ARGS){	inet	   *addr = PG_GETARG_INET_P(0);	PG_RETURN_BYTEA_P(network_send(addr, true));}Datuminet_to_cidr(PG_FUNCTION_ARGS){	inet	   *src = PG_GETARG_INET_P(0);	inet	   *dst;	int			bits;	int			byte;	int			nbits;	int			maxbytes;	bits = ip_bits(src);	/* safety check */	if ((bits < 0) || (bits > ip_maxbits(src)))		elog(ERROR, "invalid inet bit length: %d", bits);	/* clone the original data */	dst = (inet *) palloc(VARSIZE_ANY(src));	memcpy(dst, src, VARSIZE_ANY(src));	/* zero out any bits to the right of the netmask */	byte = bits / 8;	nbits = bits % 8;	/* clear the first byte, this might be a partial byte */	if (nbits != 0)	{		ip_addr(dst)[byte] &= ~(0xFF >> nbits);		byte++;	}	/* clear remaining bytes */	maxbytes = ip_addrsize(dst);	while (byte < maxbytes)	{		ip_addr(dst)[byte] = 0;		byte++;	}	PG_RETURN_INET_P(dst);}Datuminet_set_masklen(PG_FUNCTION_ARGS){	inet	   *src = PG_GETARG_INET_P(0);	int			bits = PG_GETARG_INT32(1);	inet	   *dst;	if (bits == -1)		bits = ip_maxbits(src);	if ((bits < 0) || (bits > ip_maxbits(src)))		ereport(ERROR,				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),				 errmsg("invalid mask length: %d", bits)));	/* clone the original data */	dst = (inet *) palloc(VARSIZE_ANY(src));	memcpy(dst, src, VARSIZE_ANY(src));	ip_bits(dst) = bits;	PG_RETURN_INET_P(dst);}Datumcidr_set_masklen(PG_FUNCTION_ARGS){	inet	   *src = PG_GETARG_INET_P(0);	int			bits = PG_GETARG_INT32(1);	inet	   *dst;	int			byte;	int			nbits;	int			maxbytes;	if (bits == -1)		bits = ip_maxbits(src);	if ((bits < 0) || (bits > ip_maxbits(src)))		ereport(ERROR,				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),				 errmsg("invalid mask length: %d", bits)));	/* clone the original data */	dst = (inet *) palloc(VARSIZE_ANY(src));	memcpy(dst, src, VARSIZE_ANY(src));	ip_bits(dst) = bits;	/* zero out any bits to the right of the new netmask */	byte = bits / 8;	nbits = bits % 8;	/* clear the first byte, this might be a partial byte */	if (nbits != 0)	{		ip_addr(dst)[byte] &= ~(0xFF >> nbits);		byte++;	}	/* clear remaining bytes */	maxbytes = ip_addrsize(dst);	while (byte < maxbytes)	{		ip_addr(dst)[byte] = 0;		byte++;	}	PG_RETURN_INET_P(dst);}/* *	Basic comparison function for sorting and inet/cidr comparisons. * * Comparison is first on the common bits of the network part, then on * the length of the network part, and then on the whole unmasked address. * The effect is that the network part is the major sort key, and for * equal network parts we sort on the host part.  Note this is only sane * for CIDR if address bits to the right of the mask are guaranteed zero; * otherwise logically-equal CIDRs might compare different. */static int32network_cmp_internal(inet *a1, inet *a2){	if (ip_family(a1) == ip_family(a2))	{		int			order;		order = bitncmp(ip_addr(a1), ip_addr(a2),						Min(ip_bits(a1), ip_bits(a2)));		if (order != 0)			return order;		order = ((int) ip_bits(a1)) - ((int) ip_bits(a2));		if (order != 0)			return order;		return bitncmp(ip_addr(a1), ip_addr(a2), ip_maxbits(a1));	}	return ip_family(a1) - ip_family(a2);}Datumnetwork_cmp(PG_FUNCTION_ARGS){	inet	   *a1 = PG_GETARG_INET_P(0);	inet	   *a2 = PG_GETARG_INET_P(1);	PG_RETURN_INT32(network_cmp_internal(a1, a2));}/* *	Boolean ordering tests. */Datumnetwork_lt(PG_FUNCTION_ARGS){	inet	   *a1 = PG_GETARG_INET_P(0);	inet	   *a2 = PG_GETARG_INET_P(1);	PG_RETURN_BOOL(network_cmp_internal(a1, a2) < 0);}Datumnetwork_le(PG_FUNCTION_ARGS){	inet	   *a1 = PG_GETARG_INET_P(0);	inet	   *a2 = PG_GETARG_INET_P(1);	PG_RETURN_BOOL(network_cmp_internal(a1, a2) <= 0);}Datumnetwork_eq(PG_FUNCTION_ARGS){	inet	   *a1 = PG_GETARG_INET_P(0);	inet	   *a2 = PG_GETARG_INET_P(1);	PG_RETURN_BOOL(network_cmp_internal(a1, a2) == 0);}Datumnetwork_ge(PG_FUNCTION_ARGS){	inet	   *a1 = PG_GETARG_INET_P(0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -