参考:如何做web的访问控制机制(ACL)?
地址:https://qingmu.blog.csdn.net/article/details/108286660?spm=1001.2014.3001.5502
目录
- ACL含义
- 例子
- 具体实现
ACL含义
对于一个服务器来说,我们不能让随随便便一个IP都可以访问我们的服务器,我们需要控制其访问的IP
例子
加入我们只能让C类段网络访问我们的服务器,那么我们就要对其进行访问控制
我们让C类段网络IP(192.168.1.1)与其掩码(255.255.255.0)做一个按位与,和访问的IP与这个掩码也做一个按位与的操作,其结果相同我们才能让其访问。
具体实现
int access_ornot(const char *destip) // 0 -> not 1 -> ok
{//192.168.1.1/255.255.255.0char ipinfo[16],maskinfo[16];char *p,*ip=ipinfo,*mask=maskinfo;char count=0;char *maskget=Getconfig("mask");const char *destipconst,*ipinfoconst,*maskinfoconst;if(maskget==""){printf("ok:%s\n",maskget);return 1;} p=maskget;/* get ipinfo[] start */while(*p!='/'){if(*p=='.')++count;*ip++=*p++;}while(count<3){*ip++='.';*ip++='0';++count;}*ip='\0';/* get ipinfo[] end *//* get maskinfo[] start */++p;while(*p!='\0'){if(*p=='.')++count;*mask++=*p++;}while(count<3){*mask++='.';*mask++='0';++count;}*mask='\0';/* get maskinfo[] end */destipconst=destip;ipinfoconst=ipinfo;maskinfoconst=maskinfo;return ipadd_to_longlong(ipinfoconst)==(ipadd_to_longlong(maskinfoconst)&ipadd_to_longlong(destipconst));
}