From 81b6e2b7e29fba8fbc481360110f5b2ad5c47d05 Mon Sep 17 00:00:00 2001
From: ChenLing <chenlinghy@cmhi.chinamobile.com>
Date: Thu, 20 Jun 2019 18:04:20 +0800
Subject: [PATCH] =?UTF-8?q?Mod=20=20aaa-12=20=E5=88=A4=E6=96=AD=E5=85=8D?=
 =?UTF-8?q?=E8=AE=A4=E8=AF=81=E8=A7=84=E5=88=99=E6=98=AF=E5=90=A6=E6=9C=89?=
 =?UTF-8?q?=E6=95=88=20RCA=EF=BC=9A=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9?=
 =?UTF-8?q?=E4=BA=BA=EF=BC=9Achenling=20=E6=A3=80=E8=A7=86=E4=BA=BA?=
 =?UTF-8?q?=EF=BC=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Product/user/user_auth/authfree_configure.c | 77 +++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 Product/user/user_auth/authfree_configure.c

diff --git a/Product/user/user_auth/authfree_configure.c b/Product/user/user_auth/authfree_configure.c
new file mode 100644
index 000000000..f0b04be43
--- /dev/null
+++ b/Product/user/user_auth/authfree_configure.c
@@ -0,0 +1,77 @@
+#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;
+    }    
+}
+