82 lines
2.0 KiB
C
82 lines
2.0 KiB
C
#include <stdio.h>
|
||
#include <string.h>
|
||
#include <stdint.h>
|
||
#include <time.h>
|
||
#include <sys/socket.h>
|
||
#include <netinet/in.h>
|
||
#include <arpa/inet.h>
|
||
#include <stdlib.h>
|
||
|
||
struct authfree_configure
|
||
{
|
||
char name[32]; //免认证规则名称
|
||
uint32_t sip; //免认证规则源ip
|
||
uint32_t dip; //免认证规则目的ip
|
||
int dport; //免认证规则目的端口号
|
||
time_t begin_time; //有效时间范围的起始时间
|
||
time_t over_time; //有效时间范围内的结束时间
|
||
};
|
||
|
||
//判断IPv4格式是否正确
|
||
int isIpV4Addr(const char *ipAddr)
|
||
{
|
||
int ip_part_1 = 0;
|
||
int ip_part_2 = 0;
|
||
int ip_part_3 = 0;
|
||
int ip_part_4 = 0;
|
||
char end_char = 0;
|
||
if((NULL == ipAddr) || (0 == strlen(ipAddr)))
|
||
{
|
||
return -1;
|
||
}
|
||
if(4 == sscanf(ipAddr,"%d.%d.%d.%d%c",&ip_part_1,&ip_part_2,&ip_part_3,&ip_part_4,&end_char))
|
||
{
|
||
if((ip_part_1 >= 0) && (ip_part_1 <= 255) &&
|
||
(ip_part_2 >= 0) && (ip_part_2 <= 255) &&
|
||
(ip_part_3 >= 0) && (ip_part_3 <= 255) &&
|
||
(ip_part_4 >= 0) && (ip_part_4 <= 255)
|
||
)
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
|
||
//判断免认证规则是否有效,所有条件有效则返回值为0,其中一项条件无效则返回值为1
|
||
int _valid_authfreerule(struct authfree_configure *s)
|
||
{
|
||
char sstr[32];
|
||
inet_ntop(AF_INET, (void *)&s->sip, sstr, 32);
|
||
char *sip_addr = sstr;
|
||
if(!isIpV4Addr(sip_addr))
|
||
{
|
||
char dstr[32];
|
||
inet_ntop(AF_INET, (void *)&s->dip, dstr, 32);
|
||
char *dip_addr = dstr;
|
||
if(!isIpV4Addr(dip_addr))
|
||
{
|
||
time_t lt;
|
||
lt = time(NULL);
|
||
if( (memcmp(&s->sip, &s->dip, 32) < 0) & (lt >= s->begin_time) & (lt <= s->over_time))
|
||
{
|
||
return 0;
|
||
}
|
||
else
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
|