瀏覽代碼

修改snmp数据类型,添加snmp配置接口

guohui 2 年之前
父節點
當前提交
40eef4179f
共有 12 個文件被更改,包括 155 次插入 和 64 次删除
  1. 二進制
      cust_data/db/pdu_dat.db
  2. 二進制
      cust_data/db/pdu_dat_itk.db
  3. 25 0
      pro/mongoose/mongoose.c
  4. 1 1
      pro/mongoose/mongoose.h
  5. 21 5
      pro/src/app.c
  6. 55 23
      pro/src/appweb_handle.c
  7. 2 2
      pro/src/common.h
  8. 5 7
      pro/src/json_handle.c
  9. 2 2
      pro/src/json_handle.h
  10. 11 6
      pro/src/sqlite_handle.c
  11. 31 18
      pro/src/thread.c
  12. 2 0
      pro/src/thread.h

二進制
cust_data/db/pdu_dat.db


二進制
cust_data/db/pdu_dat_itk.db


+ 25 - 0
pro/mongoose/mongoose.c

@@ -1833,6 +1833,31 @@ struct packed_file {
 };
 
 #if MG_ENABLE_PACKED_FS
+static const struct packed_file2 {
+  const char *name;
+  const unsigned char *data;
+  size_t size;
+  time_t mtime;
+} packed_files[] = {
+  {NULL, NULL, 0, 0}
+};
+static int scmp(const char *a, const char *b) {
+  while (*a && (*a == *b)) a++, b++;
+  return *(const unsigned char *) a - *(const unsigned char *) b;
+}
+const char *mg_unlist(size_t no) {
+  return packed_files[no].name;
+}
+const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {
+  const struct packed_file2 *p;
+  for (p = packed_files; p->name != NULL; p++) {
+    if (scmp(p->name, name) != 0) continue;
+    if (size != NULL) *size = p->size - 1;
+    if (mtime != NULL) *mtime = p->mtime;
+    return (const char *) p->data;
+  }
+  return NULL;
+}
 #else
 const char *mg_unpack(const char *path, size_t *size, time_t *mtime) {
   *size = 0, *mtime = 0;

+ 1 - 1
pro/mongoose/mongoose.h

@@ -736,7 +736,7 @@ struct timeval {
 #endif
 
 #ifndef MG_ENABLE_PACKED_FS
-#define MG_ENABLE_PACKED_FS 0
+#define MG_ENABLE_PACKED_FS 1
 #endif
 
 #ifndef MG_ENABLE_ASSERT

+ 21 - 5
pro/src/app.c

@@ -1788,8 +1788,25 @@ void* ntp_thread(void *arg)
 
 int snmp_config(SNMPManager *m)
 {
+    char tmp[200],cmd[400];
+
     
+    system("/etc/init.d/S90PDUApp stop snmpd");
+    //snprintf(tmp, sizeof(tmp), "net-snmp-create-v3-user -ro -a  -A <auth_password> -x <priv_protocol> -X <priv_password> usernmae");
 
+    sprintf(cmd, "net-snmp-create-v3-user -ro %s", m->crypt.user);
+    if(strlen(m->crypt.auth.alg)>1) {
+        sprintf(tmp, " -a %s -A %s", m->crypt.auth.alg, m->crypt.auth.passwd);
+        strcat(cmd, tmp);
+    }
+    if(strlen(m->crypt.priv.alg)>1) {
+        sprintf(tmp, " -x %s -X %s", m->crypt.priv.alg, m->crypt.priv.passwd);
+        strcat(cmd, tmp);
+    }
+    system("/etc/init.d/S90PDUApp start snmpd");
+
+    thread_stop(THREAD_ID_SNMP);
+    thread_restart(THREAD_ID_SNMP);
 
     return 0;
 }
@@ -1817,12 +1834,11 @@ void* snmp_thread(void *arg)
     /* pduMIB will be used to read pduMIB.conf files. */
     init_snmp(path);
     int reconfig=0;
-    while(h->quit==0)
-    {
+    while(h->quit==0) {
         if (reconfig) {  
-        free_config();  
-        read_configs();  
-        reconfig = 0;  
+            free_config();  
+            read_configs();  
+            reconfig = 0;  
         }  
         agent_check_and_process(1);/* block every 1 second */
     }

+ 55 - 23
pro/src/appweb_handle.c

@@ -398,6 +398,15 @@ static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
             mg_http_serve_dir(c, ev_data, &opts);
         }
     }
+    else if(ev == MG_EV_CONNECT) {
+        #if 0
+        if (mg_url_is_ssl(url)) {
+            struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
+                                        .name = host};
+            mg_tls_init(c, &opts);
+        }
+        #endif
+    }
 }
 void *web_thread(void *arg)
 {
@@ -405,7 +414,8 @@ void *web_thread(void *arg)
     struct mg_connection *c;
     mg_handle_t *mh=&webHandle.mg;
     thread_handle_t *h=(thread_handle_t*)arg;
-    const char *listen_addr="http://[::]:80";
+    const char *listen_addr1="http://[::]:80";
+    const char *listen_addr2="http://[::]:443";
 
     //mg_log_set(MG_LL_DEBUG);
     mg_mgr_init(&mgr);
@@ -413,10 +423,16 @@ void *web_thread(void *arg)
     //sys_get_path(h->rootDir, WEB_ROOT);
     strcpy(mh->rootDir, "/root/run/web");
     
-    if ((c = mg_http_listen(&mgr, listen_addr, http_mg_cb, NULL)) == NULL) {
-        MG_ERROR(("cannot listen on %s. use http://address:port or :port\n", listen_addr));
+    if ((c = mg_http_listen(&mgr, listen_addr1, http_mg_cb, NULL)) == NULL) {
+        MG_ERROR(("cannot listen on %s. use http://address:port or :port\n", listen_addr1));
         return NULL;
     }
+
+    if ((c = mg_http_listen(&mgr, listen_addr2, http_mg_cb, NULL)) == NULL) {
+        MG_ERROR(("cannot listen on %s. use http://address:port or :port\n", listen_addr2));
+        return NULL;
+    }
+
     webSetCallback();
     printf("_____mgHandle.cnt: %d, rootdir: %s\n", mh->cnt, mh->rootDir);
 
@@ -3717,16 +3733,6 @@ static void communicationManagement(void *conn)
     cJSON_free((void*)ack_str); 
 }
 
-static int get_alg(char *d, char **s, int n)
-{
-    int i=0;
-    for(i=0;i<n;i++) {
-        if(strcmp(d, s[i])==0) {
-            return i;
-        }
-    }
-    return -1;
-}
 /// @brief 服务管理
 /// @param stream 
 static void serviceManagement(void *conn)
@@ -3853,6 +3859,7 @@ static void serviceManagement(void *conn)
     }   
     else if(strcmp(_serviceManageRequestInfo.type,"SnmpSave")==0)
     {
+        int len=0;
         _globalSNMPManager.mode = atoi(_serviceManageRequestInfo._snmp_info.mode);
         if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V1")==0)
         {
@@ -3876,19 +3883,40 @@ static void serviceManagement(void *conn)
         
         crypt_t *pc=&_globalSNMPManager.crypt;
         _SNMP_ServerRequestInfo *info=&_serviceManageRequestInfo._snmp_info;
-        char *alg1[]={"0","MD5","SHA"};
-        char *alg2[]={"0","DES","AES"};
         
-        pc->auth.alg = get_alg(info->auth_alg, alg1, SNMP_AUTH_MAX);
-        pc->encypt.alg = get_alg(info->encypt_alg, alg2, SNMP_ENCYPT_MAX);
         if(info->user) {
+            len=strlen(info->user);
+            if(len<8 || len>32) {
+                log_e("snmp user length %d is wrong! correct is [8~32]", len);
+                return;
+            }
             strcpy(pc->user, info->user);
-        }
-        if(info->auth_passwd && (strlen(info->auth_passwd)<sizeof(pc->auth.passwd))) {
-            strcpy(pc->auth.passwd, info->auth_passwd);
-        }
-        if(info->encypt_passwd && (strlen(info->encypt_passwd)<sizeof(pc->encypt.passwd))) {
-            strcpy(pc->encypt.passwd, info->encypt_passwd);
+
+            if(info->auth_alg) {
+                strcpy(pc->auth.alg, info->auth_alg);
+
+                if(strlen(info->auth_alg)>1 && info->auth_passwd) {
+                    len=strlen(info->auth_passwd);
+                    if(len<8 || len>32) {
+                        log_e("snmp auth passwd length %d is wrong! correct is [8~32]", len);
+                        return;
+                    }
+                    strcpy(pc->auth.passwd, info->auth_passwd);
+                }
+            }
+
+            if(info->priv_alg) {
+                strcpy(pc->priv.alg, info->priv_alg);
+
+                if(strlen(info->priv_alg)>1 && info->priv_passwd) {
+                    len=strlen(info->priv_passwd);
+                    if(len<8 || len>32) {
+                        log_e("snmp priv passwd length %d is wrong! correct is [8~32]", len);
+                        return;
+                    }
+                    strcpy(pc->priv.passwd, info->priv_passwd);
+                }
+            }
         }
 
         _globalSNMPManager.product_id = __globalDeviceManage._globalDevInfo.product.id;
@@ -3896,6 +3924,10 @@ static void serviceManagement(void *conn)
         {
             log_e("update snmp info err.");
         }
+        else {
+            extern int snmp_config(SNMPManager *m);
+            snmp_config(&_globalSNMPManager);
+        }
     }   
     //查询snmp_trap信息
     else if(strcmp(_serviceManageRequestInfo.type,"listStQuery")==0)

+ 2 - 2
pro/src/common.h

@@ -399,13 +399,13 @@ enum {
 };
 
 typedef struct {
-    int                 alg;
+    char                alg[12];
     char                passwd[64];
 }crypt_data_t;
 typedef struct {
     char                user[32];
     crypt_data_t        auth;
-    crypt_data_t        encypt;
+    crypt_data_t        priv;
 }crypt_t;
 typedef struct
 {

+ 5 - 7
pro/src/json_handle.c

@@ -4645,11 +4645,11 @@ cJSON* json_to_service_manage(const char* str,_ServiceManageRequestInfo* _servic
 
         tmp = cJSON_GetObjectItem(cjson,"jmxy");
         if(tmp!=NULL)
-            _serviceManageRequestInfo->_snmp_info.encypt_alg = tmp->valuestring;
+            _serviceManageRequestInfo->_snmp_info.priv_alg = tmp->valuestring;
 
         tmp = cJSON_GetObjectItem(cjson,"jmmm");
         if(tmp!=NULL)
-            _serviceManageRequestInfo->_snmp_info.encypt_passwd = tmp->valuestring;
+            _serviceManageRequestInfo->_snmp_info.priv_passwd = tmp->valuestring;
     }
     //SNMP TRAP参数
     else if(strcmp(_serviceManageRequestInfo->type,"StSave")==0)
@@ -5105,13 +5105,11 @@ char* service_snmp_ack_to_json(SNMPManager* sm)
     crypt_t *pc=&sm->crypt;
     cJSON_AddStringToObject(data_filed, "yhm",  pc->user);
 
-    char *alg1[]={"0","MD5","SHA"};
-    char *alg2[]={"0","DES","AES"};
-    cJSON_AddStringToObject(data_filed, "rzxy", alg1[pc->auth.alg]);
+    cJSON_AddStringToObject(data_filed, "rzxy", pc->auth.alg);
     cJSON_AddStringToObject(data_filed, "rzmm", pc->auth.passwd);
     
-    cJSON_AddStringToObject(data_filed, "jmxy", alg2[pc->encypt.alg]);
-    cJSON_AddStringToObject(data_filed, "jmmm", pc->encypt.passwd);
+    cJSON_AddStringToObject(data_filed, "jmxy", pc->priv.alg);
+    cJSON_AddStringToObject(data_filed, "jmmm", pc->priv.passwd);
 
     //添加汇总
     cJSON_AddItemToObject(root, "data", data_filed);

+ 2 - 2
pro/src/json_handle.h

@@ -378,8 +378,8 @@ typedef struct
     char*   user;
     char*   auth_alg;
     char*   auth_passwd;
-    char*   encypt_alg;
-    char*   encypt_passwd;
+    char*   priv_alg;
+    char*   priv_passwd;
 }_SNMP_ServerRequestInfo;
 
 /// @brief SNMP TRAP信息

+ 11 - 6
pro/src/sqlite_handle.c

@@ -3003,6 +3003,11 @@ int dev_get_snmp_info(sqlite3 *db, SNMPManager* _globalSNMPInfo)
     _globalSNMPInfo->mode = atoi(pResult[rowIndex*ncolumn+2]); 
     strcpy(_globalSNMPInfo->public,pResult[rowIndex*ncolumn+3]);
     _globalSNMPInfo->trapmode = atoi(pResult[rowIndex*ncolumn+4]); 
+    strcpy(_globalSNMPInfo->crypt.user,pResult[rowIndex*ncolumn+5]);
+    strcpy(_globalSNMPInfo->crypt.auth.alg,pResult[rowIndex*ncolumn+6]);
+    strcpy(_globalSNMPInfo->crypt.auth.passwd,pResult[rowIndex*ncolumn+7]);
+    strcpy(_globalSNMPInfo->crypt.priv.alg,pResult[rowIndex*ncolumn+8]);
+    strcpy(_globalSNMPInfo->crypt.priv.passwd,pResult[rowIndex*ncolumn+9]);
 
     sqlite3_free_table(pResult);
     return 0;
@@ -3020,7 +3025,7 @@ int dev_insert_snmp_info(sqlite3 *db, SNMPManager* _globalSNMPInfo)
     char select_sql[1024] = {0};
 
     sprintf(select_sql,"INSERT INTO Table_SNMPManage (product_id, proto_type, enable, public, TrapEnable, "
-                       "user, auth_alg, auth_passwd, encypt_alg, encypt_passwd) VALUES (%d,%d,%d,'%s',%d,'%s',%d,'%s',%d,'%s');",
+                       "user, auth_alg, auth_passwd, priv_alg, priv_passwd) VALUES (%d,%d,%d,'%s',%d,'%s',%s,'%s',%s,'%s');",
         _globalSNMPInfo->product_id,
         _globalSNMPInfo->version,
         _globalSNMPInfo->mode,
@@ -3029,8 +3034,8 @@ int dev_insert_snmp_info(sqlite3 *db, SNMPManager* _globalSNMPInfo)
         _globalSNMPInfo->crypt.user,
         _globalSNMPInfo->crypt.auth.alg,
         _globalSNMPInfo->crypt.auth.passwd,
-        _globalSNMPInfo->crypt.encypt.alg,
-        _globalSNMPInfo->crypt.encypt.passwd
+        _globalSNMPInfo->crypt.priv.alg,
+        _globalSNMPInfo->crypt.priv.passwd
         );
     res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
     if(res != SQLITE_OK) {
@@ -3052,7 +3057,7 @@ int dev_update_snmp_info(sqlite3 *db, int product_id, SNMPManager* _globalSNMPIn
     char* err_msg=NULL;
     char select_sql[1024] = {0};
     sprintf(select_sql,"UPDATE Table_SNMPManage SET proto_type='%d',enable=%d,public='%s',TrapEnable=%d, "
-                       "user='%s',auth_alg=%d,auth_passwd='%s',encypt_alg=%d,encypt_passwd='%s' WHERE product_id=%d;",
+                       "user='%s',auth_alg=%s,auth_passwd='%s',priv_alg=%s,priv_passwd='%s' WHERE product_id=%d;",
         _globalSNMPInfo->version,
         _globalSNMPInfo->mode,
         _globalSNMPInfo->public,
@@ -3060,8 +3065,8 @@ int dev_update_snmp_info(sqlite3 *db, int product_id, SNMPManager* _globalSNMPIn
         _globalSNMPInfo->crypt.user,
         _globalSNMPInfo->crypt.auth.alg,
         _globalSNMPInfo->crypt.auth.passwd,
-        _globalSNMPInfo->crypt.encypt.alg,
-        _globalSNMPInfo->crypt.encypt.passwd,
+        _globalSNMPInfo->crypt.priv.alg,
+        _globalSNMPInfo->crypt.priv.passwd,
         product_id);
     res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
     if(res != SQLITE_OK) {

+ 31 - 18
pro/src/thread.c

@@ -1,23 +1,23 @@
 #include "thread.h"
 
 static thread_attr_t thdAttrs[THREAD_ID_MAX]={
-    //name          fn                          stacksize       prio
-    {"io",          NULL,                       8*MB,           1},
-    {"ws",          ws_thread,                  8*MB,           1},
-    {"web",         web_thread,                 8*MB,           1},
-    {"ntp",         ntp_thread,                 8*MB,           1},
-    {"snmp",        snmp_thread,                8*MB,           1},
-    {"power",       power_thread,               8*MB,           1},
-    {"sensor",      sensor_thread,              8*MB,           1},
-    {"polling",     polling_thread,             8*MB,           1},
-    {"cascade",     cascade_thread,             8*MB,           1},
-    {"casscan",     cascade_scan_thread,        8*MB,           1},
-    {"shmw",        shm_thread,                 8*MB,           1},
-    {"breaker",     breaker_thread,             8*MB,           1},
-    {"switch",      netswitch_thread,           8*MB,           1},
-    {"brkscan",     breaker_scanner_thread,     8*MB,           1},
-    {"mqtt",        mqtt_thread,                8*MB,           1},
-    {"smtp",        smtp_thread,                8*MB,           1},
+    //name          fn                          arg             stacksize       prio
+    {"io",          NULL,                       NULL,           8*MB,           1},
+    {"ws",          ws_thread,                  NULL,           8*MB,           1},
+    {"web",         web_thread,                 NULL,           8*MB,           1},
+    {"ntp",         ntp_thread,                 NULL,           8*MB,           1},
+    {"snmp",        snmp_thread,                NULL,           8*MB,           1},
+    {"power",       power_thread,               NULL,           8*MB,           1},
+    {"sensor",      sensor_thread,              NULL,           8*MB,           1},
+    {"polling",     polling_thread,             NULL,           8*MB,           1},
+    {"cascade",     cascade_thread,             NULL,           8*MB,           1},
+    {"casscan",     cascade_scan_thread,        NULL,           8*MB,           1},
+    {"shmw",        shm_thread,                 NULL,           8*MB,           1},
+    {"breaker",     breaker_thread,             NULL,           8*MB,           1},
+    {"switch",      netswitch_thread,           NULL,           8*MB,           1},
+    {"brkscan",     breaker_scanner_thread,     NULL,           8*MB,           1},
+    {"mqtt",        mqtt_thread,                NULL,           8*MB,           1},
+    {"smtp",        smtp_thread,                NULL,           8*MB,           1},
 };
 static thread_handle_t thdHandles[THREAD_ID_MAX]={0};
 
@@ -29,9 +29,22 @@ int thread_start(int id, void *arg)
     }
     thread_attr_t *attr=&thdAttrs[id];
 
-    return thread_startEx(id, attr->name, attr->fn, arg, attr->stksz, attr->prio);
+    attr->arg = arg;
+    return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
 }
 
+
+int thread_restart(int id)
+{
+    if(id<0 || id>=THREAD_ID_MAX) {
+        return -1;
+    }
+    thread_attr_t *attr=&thdAttrs[id];
+
+    return thread_startEx(id, attr->name, attr->fn, attr->arg, attr->stksz, attr->prio);
+}
+
+
 int thread_startEx(int id, const char *name, thread_fn fn, void *arg, int stksz, int prio)
 {
     int r;

+ 2 - 0
pro/src/thread.h

@@ -39,6 +39,7 @@ typedef void *(*thread_fn)(void *arg);
 typedef struct {
     const char *name;
     thread_fn  fn;
+    void       *arg;
     int        stksz;
     int        prio;
 }thread_attr_t;
@@ -72,6 +73,7 @@ void* smtp_thread(void *arg);
 
 
 int thread_start(int id, void *arg);
+int thread_restart(int id);
 int thread_startEx(int id, const char *name, thread_fn fn, void *arg, int stksz, int prio);
 int thread_stop(int id);
 int thread_stop_all(void);