Lasso Soft Inc. > Home

[Xaren_Client_IP]

LinkXaren_Client_IP
AuthorAdam Randall
CategoryClient
Version8.x
LicensePublic Domain
Posted13 May 2008
Updated13 May 2008
More by this author...

Description

The client_ip type that is provided with Lasso Professional 8 and beyond is lacking in the amount of control you have over an IP. You can only check if an IP is in a 32, 24, 16, or 8 subnet. You should be able to validate against any CIDR or subnet notation. That's what this tag provides.

Sample Usage

// returns true for IPs 10.0.1.0 through 10.0.1.31
xaren_client_ip == '10.0.1.0/27'

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

[

	namespace_using( '_global_Xaren_Client_' );

		log_critical( 'Loading Namespace: Xaren_Client_' );

		define_type( 'IP' );

			local( 'ip' );

			define_tag( 'onCreate',
				-optional = 'ip' );

				if( ! local_defined( 'ip' ) );
					self->'ip' = ( _client_ip_old->size ? _client_ip_old | false );
				else( #ip->type == 'string' && ! self->aton( #ip ) );
					self->'ip' = false;
				else( #ip->type == 'string' );
					self->'ip' = #ip;
				else( #ip->type == 'integer' );
					local( 'ntoa' = self->ntoa( #ip ) );
					self->'ip' = ( #ntoa === false ? false | #ntoa );
				else;
					self->'ip' = false;
				/if;

			/define_tag;

			define_tag( 'onConvert' );
				self->'ip' === false ?
					return( false );
				return( params->get(1) == 'integer' ? self->aton( self->'ip' ) | string( self->'ip' ) );
			/define_tag;

			define_tag( 'onCompare' );
				self->'ip' === false ?
					return( true );

				local(
					'i'    = null,
					't'    = null,
					'rhs'  = params->get(1),
					'ip'   = null,
					'cidr' = null );

				// make sure all characters are okay
				string_replaceregexp( -find = '[.0-9/]+', -replace = '', #rhs )->size ?
					return( true );

				if( #rhs->find( '/' ) > 0 );
					#t = #rhs->split( '/' );
					#t->size > 2 ? return( true ); // return if the input is invalid

					! string_isdigit( #t->get(2) ) ?
						#t->get(2) = self->subnettocidr( #t->get(2) );

					#t->get(2) === false ? return( true );

					return( ! self->within( #t->get(1), integer( #t->get(2) ) ) );
				else;
					return( ! self->within( #rhs, 32) );
				/if;

				return( true );
			/define_tag;


			define_tag( 'aton',
				-optional = 'ip',
				-type     = 'string' );

				if( ! local_defined( 'ip' ) );
					self->'ip' === false ?
						return( false );

					local( 'ip' = self->'ip' );
				else( string_replaceregexp( -find = '[.0-9]', -replace = '', #ip )->size > 0 );
					return( false );
				/if;

				local(
					'parts' = #ip->split( '.' ),
					'n'     = null );

				#parts->size < 4 ? return( false );

				#n = ( integer( #parts->get(1) ) * 256 * 256 * 256 ) +
						 ( integer( #parts->get(2) ) * 256 * 256 ) +
						 ( integer( #parts->get(3) ) * 256 ) +
						 integer( #parts->get(4) );

				return( #n > 4294967295 ? false | #n );

			/define_tag;


			define_tag( 'ntoa',
				-required = 'long',
				-type     = 'integer',
				-copy );

				if( #long < 0 || #long > 4294967295 ?
					return( false );

				local( 'ip' = '' );

				loop( 4 );
					#ip = string( integer( #long )->bitand( 255 ) & ) + ( loop_count > 1 ? '.' #ip );
					#long->bitshiftright( 8 );
				/loop;

				return( #ip );

			/define_tag;


			define_tag( 'subnettocidr',
				-required = 'ip' );

				#ip->isa( 'integer' ) ? return( @#ip );

				// set this up in case we are doing a lot of lookups
				! var_defined( '__cidrs__' ) ?
					var(
						'__cidrs__' = map(
							'255.255.255.255' = 32,
							'255.255.255.254' = 31,
							'255.255.255.252' = 30,
							'255.255.255.248' = 29,
							'255.255.255.240' = 28,
							'255.255.255.224' = 27,
							'255.255.255.192' = 26,
							'255.255.255.128' = 25,
							'255.255.255.0'   = 24,
							'255.255.254.0'   = 23,
							'255.255.252.0'   = 22,
							'255.255.248.0'   = 21,
							'255.255.240.0'   = 20,
							'255.255.224.0'   = 19,
							'255.255.192.0'   = 18,
							'255.255.128.0'   = 17,
							'255.255.0.0'     = 16,
							'255.254.0.0'     = 15,
							'255.252.0.0'     = 14,
							'255.248.0.0'     = 13,
							'255.240.0.0'     = 12,
							'255.224.0.0'     = 11,
							'255.192.0.0'     = 10,
							'255.128.0.0'     =  9,
							'255.0.0.0'       =  8,
							'254.0.0.0'       =  7,
							'252.0.0.0'       =  6,
							'248.0.0.0'       =  5,
							'240.0.0.0'       =  4,
							'224.0.0.0'       =  3,
							'192.0.0.0'       =  2,
							'128.0.0.0'       =  1,
							'0.0.0.0'         =  0 ) );

				local( 'cidr' = $__cidrs__->find( #ip ) );

				return( #cidr->isa( 'integer' ) ? #cidr | false );

			/define_tag;


			define_tag( 'within',
				-required = 'net',
				-type     = 'string',
				-copy,
				-required = 'cidr',
				-type     = 'integer' );

				self->'ip' === false ?
					return( false );

				#cidr < 0 || #cidr > 32 ?
					return( false );

				local(
					'ip'        = self->aton( self->'ip' ),
					'rede'      = self->aton( #net ),
					'real_mask' = null,
					'netmask'   = null,
					'res'       = null );

				#real_mask = 0;
				#netmask   = #rede;
				#res       = #ip;

				#rede === false ?
					return( false );

				#real_mask->bitnot & bitshiftleft( 32 - #cidr );

				#netmask->bitand( #real_mask );
				#res->bitand( #real_mask );

				return( #res == #netmask );

			/define_tag;

		/define_type;

	/namespace_using;
]

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft