【代码保留】How to match the IP Address

今天看到微博上有人说有一种完美的IP判断方法,我就跑过去瞅了瞅,是PHP的,关键是用到了正则表达式。我本人对正则表达式可没好感了,因为它们的引擎需要解析那段规则(RegExp),然后把它们用通用的方式进行模式匹配,它的确给开发带来了便利,但对于判断一个字符串是否是IP地址的简单且通用的过程,我们有必要提供一种更定制化(量身定做)的方法用来做匹配。下面就是我用JavaScript写的一个DEMO,为了保证更好的移植性,代码以C风格书写,并考虑到一些C相关的特性(包括字符串char*无长度的概念,但也不可能直接用GCC来编译,毕竟是不同语言)

示例:(直接点击试用)

 

 1       < script language = " javascript "  type = " text/javascript " >
 2       function  isIp( ipStr ) {
 3           if ( ! ipStr)     return   false ;
 4          
 5           /* 1.check length; */
 6           /* const */   var  minLengthLimit  =   7 //  0.0.0.0
 7           /* const */   var  maxLengthLimit  =   15 //  255.255.255.255;
 8           var  length  =  stringGetLength(ipStr);
 9           if (length  <  minLengthLimit  ||  length  >  maxLengthLimit)
10          {
11               return   false ;
12          }
13          
14           /* 2.check base; */
15           var  lastIndex  =  length  -   1 ;
16           var  firstElement  =  ipStr.charAt( 0 );
17           var  lastElement  =  ipStr.charAt(lastIndex);
18           if ( firstElement  ==   " . "   ||  firstElement  <   " 0 "   ||  firstElement  >   " 9 "
19           ||  lastElement  ==   " . "   ||  lastElement  <   " 0 "   ||  lastElement  >   " 9 " ){ 
20               return   false ;
21          }
22 
23           var  curNum  =   "" ;
24           var  pointCounter  =   0 ;
25           var  numLength  =   0 // for C language
26           for ( var  i  =   0 ; i  <  length;  ++ i) {
27               var  c  =  ipStr.charAt(i);
28               if ( c  >=   " 0 "   &&  c  <=   " 9 " ) {
29                  curNum  +=  c;
30                   ++ numLength;
31                   if ( numLength  >   3 )   return   false ;
32              }
33               else   if ( c  ==   " . " ) {
34                   ++ pointCounter;
35                   if (pointCounter  >   3 )    
36                       return   false ;
37                   if (numLength  ==   3 ) {
38                       if ( ! checkLess255(curNum,  0 ))     return   false ;
39                  }
40                   if (pointCounter  ==   3 ) {
41                       var  lastNumLength  =  lastIndex  -  i;
42                       if (lastNumLength  >   3 )    return   false ;
43                       var  j  =  i  +   1 ;
44                       if (lastNumLength  ==   3 )
45                      {
46                           if ( ! checkLess255(ipStr, j))   return   false ;
47                           ++ j;
48                      }
49                       for (; j  <  length;  ++ j) {
50                          c  =  ipStr.charAt(j);
51                           if ( c  <   " 0 "   ||  c  >   " 9 " return   false ;
52                      }
53                       return   true ;
54                  }
55                  curNum  =   "" ;
56                  numLength  =   0 ;
57              }
58               else      return   false ;
59          }
60      }
61      
62       function  checkLess255( strNum, startIndex) {
63           var  numLength  =  stringGetLength( strNum )  -  startIndex;
64           if (numLength  >   3 )    return   false ;
65           if (numLength  ==   3 ) {
66               var  hundredPlace  =  strNum.charAt(startIndex);
67               if (hundredPlace  >   " 2 "   /* || hundredPlace < "0" */ )   return   false ;
68               else   if (hundredPlace  ==   " 2 " ) {
69                   var  tenPlace  =  strNum.charAt(startIndex  +   1 );
70                   if (tenPlace  >   " 5 " )   return   false ;
71                   else   if (tenPlace  ==   " 5 " ) {
72                       if (strNum.charAt(startIndex  +   2 >   " 5 " )   return   false ;
73                  }
74              }
75          }
76           return   true ;
77      }
78 
79       /* inline */   function  stringGetLength( str ) {
80           if ( ! str)    
81               return   0 ;
82           else
83          {
84               try
85              {
86                   return  str.length;
87              }
88               catch (e)
89              {
90                   return   0
91              }
92          }
93      }
94      
95       < / script>

PHP的那段代码我也复制过来了,正则嘛,总是特别短,精简,你一定也会很喜欢的,也许有的场合您更喜欢(同样也可以很容易转换成JavaScript):

 

 1  function  is_ip( $str ){
 2           $ip   =   explode (” . , $str );
 3           for ( $i = 0 ; $i < count ( $ip ); $i ++ )
 4         {
 5                 if ( $ip [ $i ] > 255 ){
 6                  return  ( 0 );
 7                 }
 8          }
 9           return   ereg (” ^ [ 0 - 9 ]{ 1 , 3 }\ . [ 0 - 9 ]{ 1 , 3 }\ . [ 0 - 9 ]{ 1 , 3 }\ . [ 0 - 9 ]{ 1 , 3 }$” , $str );
10  }

 

关键字:用C语言,判断字符串是不是IP

转载于:https://www.cnblogs.com/volnet/archive/2009/11/12/how-to-match-the-IP-Address.html

请翻译:202.192.1.5 is making SMTP connections which indicate that it is misconfigured. Some elements of your existing configuration create message characteristics identical to previously identified spam messages. Please align the mail erver's HELO/EHLO 'icoremail.net' with proper DNS (forward and reverse) values for a mail server. Here is an example: Correct HELO/DNS/rDNS alignment for domain example.com: - Mail server HELO: mail.example.com - Mail server IP: 192.0.2.12 - Forward DNS: mail.example.com -> 192.0.2.12 - Reverse DNS: 192.0.2.12 -> mail.example.com Correcting an invalid HELO or a HELO/forward DNS lookup mismatch will stop the IP from being listed again. Points to consider: * Alignment: it is strongly recommended that the forward DNS lookup (domain name to IP address) and rDNS (IP to domain) of your IP should match the HELO value set in your server, if possible * The IP and the HELO value should both have forward and rDNS, and should resolve in public DNS * Ensure that the domain used in HELO actually exists! Additional points: * According to RFC, the HELO must be a fully qualified domain name (FQDN): "hostname.example.com" is an FQDN and "example.com" is not an FQDN. * The domain used should belong to your organisation. * HELO is commonly a server setting, not DNS. Contact your hosting provider for assistance if needed. You can test a server's HELO configuration by sending an email from it to helocheck@abuseat.org. A bounce that contains the required information will be returned immediately. It will look like an error, it is not. Please examine the contents of this email. If all settings are correct, you have a different problem, probably malware/spambot. Again, the HELO we are seeing is 'icoremail.net'. The last detection was at 2023-05-27 13:35:00 (UTC). For information on misconfigured or hacked SMTP servers and networks, please see this FAQ: https://www.spamhaus.org/faq/section/Hacked...%20Here's%20help#539 CSS listings expire a few days after last detection. You can always open a ticket (or update an existing one) to inform us when and how the situation was been secured.
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值