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

📄 inet.mx

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 MX
📖 第 1 页 / 共 2 页
字号:
@' The contents of this file are subject to the MonetDB Public License@' Version 1.1 (the "License"); you may not use this file except in@' compliance with the License. You may obtain a copy of the License at@' http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html@'@' Software distributed under the License is distributed on an "AS IS"@' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the@' License for the specific language governing rights and limitations@' under the License.@'@' The Original Code is the MonetDB Database System.@'@' The Initial Developer of the Original Code is CWI.@' Portions created by CWI are Copyright (C) 1997-2007 CWI.@' All Rights Reserved.@f inet@a Fabian Groffen@v 1.0@* The inet moduleThe inet module contains a collection of functions that operate on IPv4addresses.  The most relevant functions are the `containment' functionsthat deal with subnet masks.  The functionality of this module isgreatly inspired by the PostgreSQL inet atom.@malatom inet:lng;@* Implementation CodeThe first 4 bytes of the used lng are in use by the four quads of theIPv4 address, stored in network order.  In the four bytes left,additional information is stored.Currently the fifth byte holds the number of bits from the IPv4 addressthat should match (ie. /8, /16, /24, /32) also known as subnet mask.The last byte holds whether inet atom represents the value nil or not.The value nil is represented as (per byte) 0000 0001.@htypedef struct _inet {	unsigned char q1;	unsigned char q2;	unsigned char q3;	unsigned char q4;	unsigned char mask;	unsigned char filler1;	unsigned char filler2;	unsigned char isnil;} inet;#define in_isnil(i) ((i)->q1 == 0 && (i)->q2 == 0 && (i)->q3 == 0 && (i)->q4 == 0 && (i)->mask == 0 && (i)->isnil != 0)#define in_setnil(i) (i)->q1 = (i)->q2 = (i)->q3 = (i)->q4 = (i)->mask = (i)->filler1 = (i)->filler2 = 0; (i)->isnil = 1#ifdef WIN32#ifndef LIBINET#define inet_export extern __declspec(dllimport)#else#define inet_export extern __declspec(dllexport)#endif#else#define inet_export extern#endifinet_export int INETfromString(str src, int *len, inet **retval);inet_export int INETtoString(str *retval, int *len, inet *handle);inet_export str INETnew(inet * retval, str *in);inet_export str INET_isnil(bit *retval, inet * val);inet_export str INET_comp_EQ(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_NEQ(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_LT(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_GT(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_LE(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_GE(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_CW(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_CWE(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_CS(bit *retval, inet * val1, inet *val2);inet_export str INET_comp_CSE(bit *retval, inet * val1, inet *val2);inet_export str INETbroadcast(inet * retval, inet *val);inet_export str INEThost(str *retval, inet *val);inet_export str INETmasklen(int *retval, inet *val);inet_export str INETsetmasklen(inet *retval, inet *val, int *mask);inet_export str INETnetmask(inet *retval, inet *val);inet_export str INEThostmask(inet *retval, inet *val);inet_export str INETnetwork(inet *retval, inet *val);inet_export str INETtext(str *retval, inet *val);inet_export str INETabbrev(str *retval, inet *val);@c#include "mal_config.h"#include <gdk.h>#include "mal.h"#include "mal_exception.h"#include "inet.h"@malcommand fromstr() address INETfromStringcomment "Convert a string to an inet";@c/** * Creates a new inet from the given string. * Warning: GDK function, does NOT pass a string by reference, and wants * a pointer to a pointer for the retval! * Returns the number of chars read */intINETfromString(str src, int *len, inet **retval){	int i, last, type;	lng parse;	char *endptr;	char sep;	last = 0;	type = 0;	if (*len < (int)sizeof(inet)) {		if (*retval != NULL)			GDKfree(*retval);		*retval = GDKmalloc(sizeof(inet));	}	/* handle the nil string */	if (strNil(src)) {		in_setnil(*retval);		return(0);	}	/* use the DIY technique to guarantee maximum cross-platform	 * portability */	for (i = 0; src[i] != '\0'; i++) {		if (src[i] == '.' || src[i] == '/') {			sep = src[i];			src[i] = '\0';			parse = strtol(src + last, &endptr, 10);			if (*endptr != '\0') {				/* this is for the cat his violin				throw(PARSE, "inet.fromStr", "Error while parsing, unexpected string '%s'", endptr);				*/				goto error;	/* yeah, I know, but I'm just simulating try-catch stuff in C now */			}			if (parse > 255 || parse < 0) {				/* this is for the cat his violin				throw(PARSE, "inet.fromStr", "Illegal quad value: %d", parse);				*/				goto error;	/* yeah, I know, but I'm just simulating try-catch stuff in C now */			}			switch (type) {				case 0:					(*retval)->q1 = (unsigned char) parse;				break;				case 1:					(*retval)->q2 = (unsigned char) parse;				break;				case 2:					(*retval)->q3 = (unsigned char) parse;				break;				case 3:					(*retval)->q4 = (unsigned char) parse;				break;			}			last = i + 1;			type++;			if (sep == '/') {				/* zero out (default) unused bytes */				switch (type) {					case 1:						(*retval)->q2 = (unsigned char) 0;					case 2:						(*retval)->q3 = (unsigned char) 0;					case 3:						(*retval)->q4 = (unsigned char) 0;					break;				}				/* force evaluation of the mask below when we break				 * out of this loop */				type = 4;				break;			}		}	}	/* parse the last quad	 * the contract is that the caller makes sure the string is	 * null-terminated here */	parse = strtol(src + last, &endptr, 10);	if (*endptr != '\0') {		/* this is for the cat his violin		throw(PARSE, "inet.fromStr", "Error while parsing, unexpected string '%s'", endptr);		*/		goto error;	/* yeah, I know, but I'm just simulating try-catch stuff in C now */	}	if (type == 3) {		if (parse > 255 || parse < 0) {			/* this is for the cat his violin			throw(PARSE, "inet.fromStr", "Illegal quad value: %d", parse);			*/			goto error;	/* yeah, I know, but I'm just simulating try-catch stuff in C now */		}		(*retval)->q4 = (unsigned char) parse;		/* default to an exact match (all bits) */		(*retval)->mask = (unsigned char) 32;	} else if (type == 4) {		if (parse < 0 || parse > 32) {			/* this is for the cat his violin			throw(PARSE, "inet.fromStr", "Illegal mask value: %d", parse);			*/			goto error;	/* yeah, I know, but I'm just simulating try-catch stuff in C now */		}		(*retval)->mask = (unsigned char) parse;	} else {		/* this is for the cat his violin		   throw(PARSE, "inet.fromStr", "Error while parsing, unexpected string '%s'", endptr);		   */		goto error;	/* yeah, I know, but I'm just simulating try-catch stuff in C now */	}	return(i);error: /* catch exception: return NULL */	in_setnil(*retval);	*len = 0;	/* signal INETnew something went wrong */	return(i - 1);}@malcommand tostr() address INETtoStringcomment "Convert inet to string equivalent";@c/** * Returns the string representation of the given inet value. * Warning: GDK function * Returns the length of the string */intINETtoString(str *retval, int *len, inet *handle){	inet *value = (inet *)handle;	if (*len < 19) {		if (*retval != NULL)			GDKfree(*retval);		*retval = GDKmalloc(sizeof(str) * (*len = 19));	}	if (in_isnil(value)) {		*len = snprintf(*retval, *len, "(nil)");	} else if (value->mask == 32) {		*len = snprintf(*retval, *len, "%d.%d.%d.%d", value->q1, value->q2, value->q3, value->q4);	} else {		*len = snprintf(*retval, *len, "%d.%d.%d.%d/%d", value->q1, value->q2, value->q3, value->q4, value->mask);	}	return(*len);}@malcommand new(s:str):inet address INETnewcomment "Create an inet from a string literal";@c/** * Returns a inet, parsed from a string.  The fromStr function is used * to parse the string. */strINETnew(inet *retval, str *in){	int pos;	int len = sizeof(inet);	pos = INETfromString(*in, &len, &retval);	if (len == 0)		throw(PARSE, "inet.new", "Error while parsing at char %d", pos + 1);	return (MAL_SUCCEED);}/* === Operators === */@malcommand isnil(v:inet):bit address INET_isnilcomment "Nil test for inet value";@c/** * Returns whether val represents a nil inet value */strINET_isnil(bit *retval, inet * val){	*retval = in_isnil(val);	return (MAL_SUCCEED);}@malcommand =(v:inet,w:inet):bit address INET_comp_EQcomment "Equality of two inets";@c/** * Returns whether val1 and val2 are equal. */strINET_comp_EQ(bit *retval, inet * val1, inet * val2){	if (in_isnil(val1) || in_isnil(val2)) {		*retval = bit_nil;	} else if (val1->q1 == val2->q1 && val1->q2 == val2->q2 && val1->q3 == val2->q3 && val1->q4 == val2->q4 && val1->mask == val2->mask) {		*retval = 1;	} else {		*retval = 0;	}	return (MAL_SUCCEED);}@malcommand !=(v:inet,w:inet):bit address INET_comp_NEQcomment "Inequality of two inets";@c/** * Returns whether val1 and val2 are not equal. */strINET_comp_NEQ(bit *retval, inet * val1, inet * val2){	if (in_isnil(val1) || in_isnil(val2)) {		*retval = bit_nil;	} else if (val1->q1 == val2->q1 && val1->q2 == val2->q2 && val1->q3 == val2->q3 && val1->q4 == val2->q4 && val1->mask == val2->mask) {		*retval = 0;	} else {		*retval = 1;	}	return (MAL_SUCCEED);}@malcommand <(v:inet,w:inet):bit address INET_comp_LTcomment "Whether v is less than w";@c/** * Returns whether val1 is smaller than val2. */strINET_comp_LT(bit *retval, inet * val1, inet * val2){	if (in_isnil(val1) || in_isnil(val2)) {		*retval = bit_nil;	} else if (val1->q1 < val2->q1) {		*retval = 1;	} else if (val1->q1 > val2->q1) {		*retval = 0;	} else if (val1->q2 < val2->q2) {		*retval = 1;	} else if (val1->q2 > val2->q2) {		*retval = 0;	} else if (val1->q3 < val2->q3) {		*retval = 1;	} else if (val1->q3 > val2->q3) {		*retval = 0;	} else if (val1->q4 < val2->q4) {		*retval = 1;	} else if (val1->q4 > val2->q4) {		*retval = 0;	} else if (val1->mask < val2->mask) {		*retval = 1;	} else {		*retval = 0;	}	return (MAL_SUCCEED);}@malcommand >(v:inet,w:inet):bit address INET_comp_GTcomment "Whether v is greater than w";@c/** * Returns whether val1 is greater than val2. */strINET_comp_GT(bit *retval, inet * val1, inet * val2){	return (INET_comp_LT(retval, val2, val1));}@malcommand <=(v:inet,w:inet):bit address INET_comp_LEcomment "Whether v is less than or equal to w";@c/** * Returns whether val1 is smaller than or equal to val2. */strINET_comp_LE(bit *retval, inet * val1, inet * val2){	bit ret;	INET_comp_LT(&ret, val1, val2);	if (ret == 0)		INET_comp_EQ(&ret, val1, val2);	*retval = ret;	return (MAL_SUCCEED);}@malcommand >=(v:inet,w:inet):bit address INET_comp_GEcomment "Whether v is equal to or greater than w";@c/** * Returns whether val1 is smaller than or equal to val2. */strINET_comp_GE(bit *retval, inet * val1, inet * val2){	bit ret;	/* warning: we use LT here with swapped arguments to avoid one	 * method invocation, since inet_comp_GT does the same */	INET_comp_LT(&ret, val2, val1);	if (ret == 0)		INET_comp_EQ(&ret, val1, val2);	*retval = ret;	return (MAL_SUCCEED);}@malcommand <<(v:inet,w:inet):bitaddress INET_comp_CWcomment "Whether v is contained within w";@c/** * Returns whether val1 is contained within val2 */strINET_comp_CW(bit *retval, inet * val1, inet * val2){	if (in_isnil(val1) || in_isnil(val2)) {		*retval = bit_nil;	} else if (val1->mask <= val2->mask) {		/* if the mask is bigger (less specific) or equal it can never		 * be contained within */		*retval = 0;	} else {		int mask;		unsigned char m[4] = { -1, -1, -1, -1 };		/* all operations here are done byte based, to avoid byte sex

⌨️ 快捷键说明

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