From f80ec7665e15107fa1c1f20ee755b7be9b138d53 Mon Sep 17 00:00:00 2001 From: wuqihy Date: Wed, 21 Aug 2019 18:32:05 +0800 Subject: [PATCH] =?UTF-8?q?Add=20aaa-12=20=E6=B7=BB=E5=8A=A0dhcp=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=20SOL=20=E6=B7=BB=E5=8A=A0=E7=9A=84dhcp=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=20=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Awuqihy=20?= =?UTF-8?q?=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9Ayinbin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuqihy --- Common/config_manager.h | 10 + Platform/build/user.configm.Makefile | 4 +- .../dhcp_config/dhcp_client_config.c | 209 ++++++++ .../dhcp_config/dhcp_dhcpd_lease.c | 109 ++++ .../dhcp_config/dhcp_host_config.c | 316 ++++++++++++ .../config-server/dhcp_config/dhcp_lib.c | 425 ++++++++++++++++ .../dhcp_config/dhcp_relay_config.c | 84 ++++ .../dhcp_config/dhcp_shared_network_config.c | 467 ++++++++++++++++++ .../dhcp_config/dhcp_subnet_config.c | 452 +++++++++++++++++ .../configm/config-server/include/configm.h | 61 +++ .../include/dhcp_client_config.h | 18 + .../config-server/include/dhcp_dhcpd_lease.h | 13 + .../config-server/include/dhcp_host_config.h | 26 + .../configm/config-server/include/dhcp_lib.h | 62 +++ .../config-server/include/dhcp_relay_config.h | 17 + .../include/dhcp_shared_network_config.h | 17 + .../include/dhcp_subnet_config.h | 22 + 17 files changed, 2310 insertions(+), 2 deletions(-) create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_lib.c create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c create mode 100644 Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c create mode 100644 Platform/user/configm/config-server/include/dhcp_client_config.h create mode 100644 Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h create mode 100644 Platform/user/configm/config-server/include/dhcp_host_config.h create mode 100644 Platform/user/configm/config-server/include/dhcp_lib.h create mode 100644 Platform/user/configm/config-server/include/dhcp_relay_config.h create mode 100644 Platform/user/configm/config-server/include/dhcp_shared_network_config.h create mode 100644 Platform/user/configm/config-server/include/dhcp_subnet_config.h diff --git a/Common/config_manager.h b/Common/config_manager.h index 64d418177..942311ae3 100755 --- a/Common/config_manager.h +++ b/Common/config_manager.h @@ -26,6 +26,8 @@ /*nat config */ #define NAT_CONFIG_MODULE 0x00000008 +/*DHCP CONFIG*/ +#define DHCP_CONFIG_MODULE 0x00000006 /************************* 模块定义结束 **********************/ /************************ config id定义 **********************/ @@ -51,6 +53,14 @@ #define NAT4_CONFIG (uint64)((uint64)NAT_CONFIG_MODULE<<32|1) +#define DHCP_SUBNET_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|1) +#define DHCP_HOST_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|2) +#define DHCP_SHARED_NETWORK_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|3) +#define DHCP_RELAY_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|4) +#define DHCP_CLIENT_CONFIG (uint64)((uint64)DHCP_CONFIG_MODULE<<32|5) +#define DHCP_DHCPD_LEASE (uint64)((uint64)DHCP_CONFIG_MODULE<<32|6) + + /************************ config id定义 end**********************/ #endif diff --git a/Platform/build/user.configm.Makefile b/Platform/build/user.configm.Makefile index 2fd52fb67..c42d77d53 100755 --- a/Platform/build/user.configm.Makefile +++ b/Platform/build/user.configm.Makefile @@ -36,8 +36,8 @@ COMMON_SRCS = configserver.c \ web_config/authfree.c web_config/auth_parameters.c\ user_manager_config/user_group_config.c user_manager_config/user_account_config.c user_manager_config/usermanager-server/array_index.c \ user_manager_config/usermanager-server/user_group.c user_manager_config/usermanager-server/user_mod.c user_manager_config/usermanager-server/user.c \ - log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c - + log_config/log_config_console.c log_config/log_config_init.c log_config/log_config_cm.c log_config/log_config_monitor.c log_config/log_config_remote.c log_config/log_config_file.c\ + dhcp_config/dhcp_client_config.c dhcp_config/dhcp_dhcpd_lease.c dhcp_config/dhcp_host_config.c dhcp_config/dhcp_lib.c dhcp_config/dhcp_relay_config.c dhcp_config/dhcp_shared_network_config.c dhcp_config/dhcp_subnet_config.c\ # MRS Board Source Files PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c new file mode 100644 index 000000000..a03643662 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_client_config.c @@ -0,0 +1,209 @@ +#include "dhcp_client_config.h" + +ret_code dhcp_client_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + ret = RET_INPUTERR; + return ret; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate){ + *config_type = CM_CONFIG_SET; + } + return ret; +} + + +ret_code dhcp_client_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *interfaces=NULL; + + int len = 50; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + interfaces = cJSON_GetObjectItem(root, "interfaces"); + if(NULL == interfaces) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + count = cJSON_GetArraySize(interfaces); + if(!strcmp(operate->valuestring, "obtain ip")){ + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(interfaces, i); + if(NULL == one) + { + continue; + } + cJSON *name = cJSON_GetObjectItem(one, "name"); + if(NULL == name) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "dhclient %s", name->valuestring); + system(cmd); + //system("dhclient %s", name->valuestring); + } + } + if(!strcmp(operate->valuestring, "lease ip")){ + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(interfaces, i); + if(NULL == one) + { + continue; + } + cJSON *name = cJSON_GetObjectItem(one, "name"); + if(NULL == name) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "dhclient -r %s", name->valuestring); + system(cmd); + //system("dhclient -r %s", name->valuestring); + } + } + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + + } + return ret; + +} + +ret_code dhcp_client_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *interface=NULL; + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + interface = cJSON_GetObjectItem(root, "interface"); + if(NULL == interface) + { + return RET_INPUTERR; + } + + //ȡļroot@ubuntu:/var/lib/dhcp# cat dhclient.leases һ + char *fname="/var/lib/dhcp/dhclient.leases"; + FILE *fp; + char *lease_interface, *ip, *router; + cJSON *json = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + lease_interface = (char *)malloc(20); + memset(lease_interface, 0, 20); + ip = (char *)malloc(20); + memset(ip, 0, 20); + router = (char *)malloc(20); + memset(router, 0, 20); + root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "interface", interface->valuestring); + + + while((fgets(line,1000,fp))!=NULL){ + char *startINTER = strstr(line, "interface"); + char *endINTER = strstr(line, ";"); + if((startINTER!=NULL) && (endINTER!=NULL)){ + memcpy(lease_interface,startINTER+11,endINTER-startINTER-12); + if(!strcmp(interface->valuestring, lease_interface)){ + memset(ip, 0, 20); + memset(router, 0, 20); + } + //printf("ip: %s\n", ip); + //json = cJSON_CreateObject(); + //cJSON_AddStringToObject(json , "ip", ip); + } + if(lease_interface != NULL){ + // + if(strcmp(interface->valuestring, lease_interface)){ + continue; + } + } + char *startIP = strstr(line, "fixed-address"); + char *endIP = strstr(line, ";"); + if((startIP!=NULL) && (endIP!=NULL)){ + memcpy(ip,startIP+14,endIP-startIP-14); + //printf("ip: %s\n", ip); + //json = cJSON_CreateObject(); + //cJSON_AddStringToObject(json , "ip", ip); + } + char *startROUTER = strstr(line, "option routers"); + char *endROUTER = strstr(line, ";"); + if((startROUTER!=NULL) && (endROUTER!=NULL)){ + memcpy(router,startROUTER+15,endROUTER-startROUTER-15); + //printf("mac: %s\n", mac); + //cJSON_AddStringToObject(json , "mac", mac); + } + } + if(ip != NULL){ + cJSON_AddStringToObject(root, "ip", ip); + memset(ip, 0, 20); + free(ip); + } + if(router != NULL){ + cJSON_AddStringToObject(root, "router", router); + memset(router, 0, 20); + free(router); + } + if(lease_interface != NULL){ + memset(lease_interface, 0, 20); + free(lease_interface); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; +} diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c b/Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c new file mode 100644 index 000000000..cdec7c562 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_dhcpd_lease.c @@ -0,0 +1,109 @@ +#include "dhcp_dhcpd_lease.h" + +ret_code dhcp_dhcpd_lease_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + *config_type = CM_CONFIG_GET_ALL; + return ret; +} + +ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len) +{ + //ip_config_t *ip_conf; + ret_code ret = RET_OK; + + + char *fname="/var/lib/dhcp/dhcpd.leases"; + FILE *fp; + char *ip=NULL, *mac=NULL, *hname=NULL, *time=NULL; + cJSON *root = NULL, *arr = NULL, *json = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + ip = (char *)malloc(20); + memset(ip, 0, 20); + mac = (char *)malloc(20); + memset(mac, 0, 20); + hname = (char *)malloc(100); + memset(hname, 0, 100); + time = (char *)malloc(100); + memset(time, 0, 100); + root = cJSON_CreateObject(); + cJSON_AddItemToObject(root, "lease", arr = cJSON_CreateArray()); + + + while((fgets(line,1000,fp))!=NULL){ + char *startIP = strstr(line, "lease"); + char *endIP = strstr(line, "{"); + if((startIP!=NULL) && (endIP!=NULL)){ + memcpy(ip,startIP+6,endIP-startIP-7); + //printf("ip: %s\n", ip); + json = cJSON_CreateObject(); + cJSON_AddStringToObject(json , "ip", ip); + } + char *startMAC = strstr(line, "hardware ethernet"); + char *endMAC = strstr(line, ";"); + if((startMAC!=NULL) && (endMAC!=NULL)){ + memcpy(mac,startMAC+18,endMAC-startMAC-18); + //printf("mac: %s\n", mac); + cJSON_AddStringToObject(json , "mac", mac); + } + char *startNAME = strstr(line, "client-hostname"); + char *endNAME = strstr(line, ";"); + if((startNAME!=NULL) && (endNAME!=NULL)){ + memcpy(hname,startNAME+17,endNAME-startNAME-18); + //printf("name: %s\n", hname); + cJSON_AddStringToObject(json , "host-name", hname); + } + char *startTIME = strstr(line, "ends"); + char *endTIME = strstr(line, ";"); + if((startTIME!=NULL) && (endTIME!=NULL)){ + memcpy(time,startTIME+7,endTIME-startTIME-7); + //printf("time: %s\n", time); + cJSON_AddStringToObject(json , "end-time", time); + } + char *end = strstr(line, "}"); + if(end != NULL){ + //printf("\n"); + cJSON_AddItemToArray(arr, json); + memset(ip, 0, 20); + memset(mac, 0, 20); + memset(hname, 0, 100); + memset(time, 0, 100); + } + } + fclose(fp); + if(ip != NULL){ + memset(ip, 0, 20); + free(ip); + } + if(mac != NULL){ + memset(mac, 0, 20); + free(mac); + } + if(hname != NULL){ + memset(hname, 0, 100); + free(hname); + } + if(time != NULL){ + memset(time, 0, 100); + free(time); + } + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + //out = cJSON_Print(root); + //printf("%s\n",out); + return ret; + +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c new file mode 100644 index 000000000..4ac986da2 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_host_config.c @@ -0,0 +1,316 @@ +#include "dhcp_host_config.h" + +ret_code dhcp_host_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate) { + *config_type = CM_CONFIG_SET; + } + return ret; +} + +/*inputʽ: +{ + ""operate" : "add", + ""host": [ + { + "host-name" : "h1", + "hardware-ethernet" : "00:00:00:00:00:01", + "fixed-address" : "10.0.0.1" + }, + { + "host-name" : "h2", + "hardware-ethernet" : "00:00:00:00:00:02", + "fixed-address" : "10.0.0.2" + }, + ] +} +*/ +ret_code dhcp_host_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *host=NULL; + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + return RET_INPUTERR; + } + host = cJSON_GetObjectItem(root, "host"); + if(NULL == host) + { + return RET_INPUTERR; + } + count = cJSON_GetArraySize(host); + int len = 500; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(host, i); + if(NULL == one) + { + continue; + } + cJSON *hostName = cJSON_GetObjectItem(one, "host-name"); + if(NULL == hostName) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check host name + if(!check_name(hostName->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + cJSON *macAddr = cJSON_GetObjectItem(one, "hardware-ethernet"); + cJSON *fixedAddr = cJSON_GetObjectItem(one, "fixed-address"); + + /*дļ*/ + memset(cmd, 0, len + 1); + if(!strcmp(operate->valuestring, "add")) + { + if((NULL == macAddr) || (NULL == fixedAddr)) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //ж֮ǰǷùhost + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring); + system(cmd); + if(NULL != getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + //check hardware ethernet and fixed address + if((!check_mac(macAddr->valuestring)) || (!check_ip(fixedAddr->valuestring))){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //echo 'xxx' >> targetfile + /* + host h1 { + hardware ethernet 08:00:07:26:c0:a5; + fixed-address 10.0.7.2; + } + */ + //echo յҪ-e ﲻҪ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo -e 'host %s {\\n hardware ethernet %s;\\n fixed-address %s;\\n}' >> /etc/dhcp/dhcpd.conf", + hostName->valuestring, macAddr->valuestring, fixedAddr->valuestring); + system(cmd); + } + + if(!strcmp(operate->valuestring, "del")) + { + //ж֮ǰǷùhost + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*host[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", hostName->valuestring); + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + //system("sed -r -i '/^#*$/d' /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "mod")) + { + snprintf(cmd, len, "grep -Pzo 'host[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", hostName->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL != macAddr) + { + if(file_consist_str("/tmp/subconf.conf", macAddr->valuestring)){ + //printf("mac already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check mac + if(!check_mac(macAddr->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/hardware[ \\t]+ethernet[ \\ta-fA-F0-9:]+;/hardware ethernet %s;/g' /tmp/subconf.conf", macAddr->valuestring); + system(cmd); + } + if(NULL != fixedAddr) + { + if(file_consist_str("/tmp/subconf.conf", fixedAddr->valuestring)){ + //printf("fixed ip already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(!check_ip(fixedAddr->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/fixed-address[ \\t0-9\\.]+;/fixed-address %s;/g' /tmp/subconf.conf", fixedAddr->valuestring); + system(cmd); + } + //ʱļȡַдԭļ + char *conf_after; + char *temp_conf_file = "/tmp/subconf.conf"; + conf_after = getfileall_with_linefeed(temp_conf_file); + if(NULL != conf_after) + { + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*host[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}/%s/g' /etc/dhcp/dhcpd.conf", hostName->valuestring, conf_after); + system(cmd); + } + if(conf_after){ + free(conf_after); + conf_after = NULL; + } + system("rm -rf /tmp/subconf.conf"); + + } + + } + + /*ļ*/ + system("service isc-dhcp-server restart"); + +INPUT_ERROR: + /*ͷڴ*/ + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + //ret = RET_INPUTERR; + return ret; +} + +ret_code dhcp_host_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *host=NULL; + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + host = cJSON_GetObjectItem(root, "host-name"); + if(NULL == host) + { + return RET_INPUTERR; + } + if(!check_name(host->valuestring)){ + return RET_INPUTERR; + } + + //ȡļ + char *fname="/etc/dhcp/dhcpd.conf"; + FILE *fp; + char *name=NULL, *mac=NULL, *ip=NULL; + cJSON *json = NULL; + char *out = NULL; + char line[1000]; + int len; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + len = strlen(host->valuestring); + name = (char *)malloc(len+1); + memset(name, 0, 20); + ip = (char *)malloc(20); + memset(ip, 0, 20); + mac = (char *)malloc(20); + memset(mac, 0, 20); + root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "host-name", host->valuestring); + + while((fgets(line,1000,fp))!=NULL){ + char *startNAME = strstr(line, "host"); + char *endNAME = strstr(line, "{"); + if((startNAME!=NULL) && (endNAME!=NULL)){ + memcpy(name,startNAME+5,endNAME-startNAME-6); + if(!strcmp(host->valuestring, name)){ + memset(ip, 0, 20); + memset(mac, 0, 20); + } + } + if(name != NULL){ + // + if(strcmp(host->valuestring, name)){ + continue; + } + } + char *startIP = strstr(line, "fixed-address"); + char *endIP = strstr(line, ";"); + if((startIP!=NULL) && (endIP!=NULL)){ + memcpy(ip,startIP+14,endIP-startIP-14); + } + char *startMAC = strstr(line, "hardware ethernet"); + char *endMAC = strstr(line, ";"); + if((startMAC!=NULL) && (endMAC!=NULL)){ + memcpy(mac,startMAC+18,endMAC-startMAC-18); + } + } + if(ip != NULL){ + cJSON_AddStringToObject(root, "fixed-address", ip); + memset(ip, 0, 20); + free(ip); + } + if(mac != NULL){ + cJSON_AddStringToObject(root, "hardware-ethernet", mac); + memset(mac, 0, 20); + free(mac); + } + if(name != NULL){ + memset(name, 0, 20); + free(name); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c b/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c new file mode 100644 index 000000000..c25d43113 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_lib.c @@ -0,0 +1,425 @@ +#include "dhcp_lib.h" + + +char *getfileall(char *fname) +{ + if(NULL == fname){ + return NULL; + } + FILE *fp; + char *str; + char txt[1000]; + int filesize; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return NULL; + } + + fseek(fp,0,SEEK_END); + + filesize = ftell(fp); + str=(char *)malloc(filesize); + str[0]=0; + + rewind(fp); + while((fgets(txt,1000,fp))!=NULL){ + strcat(str,txt); + } + fclose(fp); + if(!strcmp(str, "")){ + return NULL; + } + return str; +} + +char *getfilefirstline(char *fname) +{ + if(NULL == fname){ + return NULL; + } + FILE *fp; + char *str; + char txt[1000]; + int filesize; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return NULL; + } + + fseek(fp,0,SEEK_END); + + filesize = ftell(fp); + str=(char *)malloc(filesize); + str[0]=0; + + rewind(fp); + + while((fgets(txt,1000,fp))!=NULL){ + int len = strlen(txt); + if(txt[len-1] == '\n'){ + txt[len-1] = '\0'; + } + strcat(str,txt); + break; + } + fclose(fp); + if(!strcmp(str, "")){ + return NULL; + } + return str; +} + +char *getfileall_with_linefeed(char *fname) +{ + if(NULL == fname){ + return NULL; + } + FILE *fp; + char *str; + char *p; + char line[1000]; + int filesize; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return NULL; + } + + fseek(fp,0,SEEK_END); + + filesize = ftell(fp); + str=(char *)malloc(filesize+100); + str[0]=0; + + rewind(fp); + while((fgets(line,1000,fp))!=NULL){ + int len = strlen(line); + if(line[len-1] == '\n'){ + line[len-1] = ' '; + //printf("%s\n", line); + p = line+len; + stpcpy(p, "\\n"); + //printf("%s\n", line); + } + strcat(str,line); + } + fclose(fp); + //printf("%s\n", str); + if(!strcmp(str, "")){ + return NULL; + } + return str; +} + +int check_name(char *name){ + if(NULL == name){ + return FALSE; + } + //1+ĸ + int i=0; + while(name[i] != '\0'){ + if(((name[i]>='0') && (name[i]<='9')) || ((name[i]>='a') && (name[i]<='z')) || ((name[i]>='A') && (name[i]<='Z'))){ + i++; + continue; + } + return FALSE; + } + //2Ψһaddʱж + + return TRUE; +} + +int check_mac(char *mac){ + if(NULL == mac){ + return FALSE; + } + int status; + const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$"; + const int cflags = REG_EXTENDED | REG_NEWLINE; + + char ebuf[128]; + regmatch_t pmatch[1]; + int nmatch = 10; + regex_t reg; + + + status = regcomp(®, pattern, cflags);//ģʽ + if(status != 0) { + regerror(status, ®, ebuf, sizeof(ebuf)); + //fprintf(stderr, "regcomp fail: %s , pattern '%s' \n",ebuf, pattern); + goto failed; + } + + status = regexec(®, mac, nmatch, pmatch,0);//ִʽͻıȽ, + if(status != 0) { + regerror(status, ®, ebuf, sizeof(ebuf)); + //fprintf(stderr, "regexec fail: %s , mac:\"%s\" \n", ebuf, mac); + goto failed; + } + + //printf("[%s] match success.\n", __FUNCTION__); + regfree(®); + return TRUE; + +failed: + regfree(®); + return FALSE; +} +int file_consist_str(char *fname, char *str){ + //ȡļ + FILE *fp; + //char *name, *mac, *ip; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return FALSE; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + while((fgets(line,1000,fp))!=NULL){ + char *p = strstr(line, str); + if(p){ + return TRUE; + } + } + return FALSE; +} + +int check_range(char *range, char *mask, char *subnet){ + if((NULL==range) || (NULL==mask) || (NULL==subnet)){ + return FALSE; + } + int len = strlen(range); + char rangeARR[len+1]; + strcpy(rangeARR, range); + + //1ֳip & жip + struct in_addr low, high, subnetadd, maskaddr; + char *item; + item = strtok(rangeARR, " "); + int code, lowsub, highsub; + if(NULL == item){ + return FALSE; + } + code = inet_aton(item, &low); + if(!(code)){ + return FALSE; + } + item = strtok(NULL, " "); + if(NULL == item){ + return FALSE; + } + code = inet_aton(item, &high); + if(!(code)){ + return FALSE; + } + item = strtok(NULL, " "); + if(NULL != item){ + return FALSE; + } + + // 2subnetΧ + inet_aton(subnet, &subnetadd); + inet_aton(mask, &maskaddr); + lowsub = low.s_addr & maskaddr.s_addr; + if(lowsub != subnetadd.s_addr){ + return FALSE; + } + highsub = high.s_addr & maskaddr.s_addr; + if(highsub != subnetadd.s_addr){ + return FALSE; + } + + //3һǰһ + char lowARR[15]; + strcpy(lowARR, inet_ntoa(low)); + char highARR[15]; + strcpy(highARR, inet_ntoa(high)); + if(strcmp(lowARR, highARR) >= 0){ + return FALSE; + } + return TRUE; +} + +int check_dns(char *dns){ + if(NULL == dns){ + return FALSE; + } + int len = strlen(dns); + char dnsARR[len+1]; + strcpy(dnsARR, dns); + + //DNSжǷip + char *item; + item = strtok(dnsARR, ","); + if(NULL == item){ + return FALSE; + } + if(!check_ip(item)){ + return FALSE; + } + item = strtok(NULL, ","); + //ֻһdns + if(NULL == item){ + return TRUE; + } + if(!check_ip(item)){ + return FALSE; + } + item = strtok(NULL, ","); + if(NULL != item){ + return FALSE; + } + return TRUE; +} + +int check_lease(char *lease){ + //־Ϳ + int i=0; + while(lease[i] != '\0'){ + if((lease[i]>='0') && (lease[i]<='9')){ + i++; + continue; + } + return FALSE; + } + return TRUE; +} + +int check_ip(char *ip){ + if(NULL == ip){ + return FALSE; + } + struct in_addr ipadd; + //int s_addr; + int code; + code = inet_aton(ip, &ipadd); + /* + int s_addr; + s_addr = htonl(ipadd.s_addr); + printf("[test]ipadd.s_addr=%x, s_addr=%x\n", ipadd.s_addr, s_addr); + ipadd.s_addr = s_addr;*/ + if((1 == code)){ + return TRUE; + } + return FALSE; +} + +char *get_interface_subnet(char *interface) { + if(NULL == interface){ + return NULL; + } + struct in_addr ipadd, maskadd, subnet; + char *ip, *mask; + ip = get_interface_ip(interface); + mask = get_interface_mask(interface); + + if(ip == NULL){ + return NULL; + } + inet_aton(ip, &ipadd); + if(mask == NULL){ + return NULL; + } + inet_aton(mask, &maskadd); + subnet.s_addr = ipadd.s_addr & maskadd.s_addr; + if(ip != NULL){ + free(ip); + ip = NULL; + } + if(mask != NULL){ + free(mask); + mask = NULL; + } + //printf ("%s\n", inet_ntoa(subnet)); + char *str = (char *)malloc(15); + memset(str, 0, 15); + strcpy(str, inet_ntoa(subnet)); + return str; +} + +char *get_interface_ip(char *interface) { + if(NULL == interface){ + return NULL; + } + char *cmd = (char *)malloc(101); + if(!cmd){ + return NULL; + } + memset(cmd, 0, 101); + //snprintf(cmd, 100, "ifconfig %s | grep \"inet addr\" | awk '{ print $2}' | awk -F: '{print $2}' > /tmp/ip", interface); + snprintf(cmd, 100, "ifconfig %s | grep \"inet\" | awk '{ print $2}' > /tmp/ip", interface); + system(cmd); + memset(cmd, 0, 101); + free(cmd); + return getfilefirstline("/tmp/ip"); +} + +char *get_interface_mask(char *interface) { + if(NULL == interface){ + return NULL; + } + char *cmd = (char *)malloc(101); + memset(cmd, 0, 101); + //snprintf(cmd, 100, "ifconfig %s | grep \"Mask\" | awk '{ print $4}' | awk -F: '{print $2}' > /tmp/mask", interface); + snprintf(cmd, 100, "ifconfig %s | grep \"netmask\" | awk '{ print $4}' > /tmp/mask", interface); + system(cmd); + memset(cmd, 0, 101); + free(cmd); + return getfilefirstline("/tmp/mask"); +} + +int check_servers(char *servers){ + if(NULL == servers){ + return FALSE; + } + int len = strlen(servers); + char arr[len+1]; + strcpy(arr, servers); + + //serversжǷip + char *item; + item = strtok(arr, " "); + if(NULL == item){ + return FALSE; + } + while(item != NULL){ + if(!check_ip(item)){ + return FALSE; + } + item = strtok(NULL, " "); + } + return TRUE; +} + +int check_segment(char *segment, char *mask){ + //1ipip + struct in_addr segmentadd, maskadd; + + if(check_ip(segment)){ + inet_aton(segment, &segmentadd); + inet_aton(mask, &maskadd); + if((maskadd.s_addr & segmentadd.s_addr) == segmentadd.s_addr){ + return TRUE; + } + } + return FALSE; +} + +int check_mask(char *mask) +{ + if(check_ip(mask)) + { + unsigned int b = 0, i, n[4]; + sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]); + for(i = 0; i < 4; ++i){ //32λ޷ + b += n[i] << (i * 8); + } + b = ~b + 1; + if((b & (b - 1)) == 0){ //жǷΪ2^n + return TRUE; + } + } + return FALSE; +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c new file mode 100644 index 000000000..cca16cb41 --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_relay_config.c @@ -0,0 +1,84 @@ +#include "dhcp_relay_config.h" + +ret_code dhcp_relay_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + *config_type = CM_CONFIG_SET; + return ret; +} + + +ret_code dhcp_relay_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + //int i, count; + cJSON *root=NULL, *operate=NULL, *interfaces=NULL, *servers=NULL; + int len = 100; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + ret = RET_ERR; + goto INPUT_ERROR; + } + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + if(!strcmp(operate->valuestring, "close relay")){ + //ɾļ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/SERVERS=\"[ \\t0-9\\.]*\"/SERVERS=\"\"/g' /etc/default/isc-dhcp-relay"); //\" д\\" + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"\"/g' /etc/default/isc-dhcp-relay"); //\" д\\" + system(cmd); + system("service isc-dhcp-relay stop"); + } + if(!strcmp(operate->valuestring, "start relay")){ + interfaces = cJSON_GetObjectItem(root, "interfaces"); + servers = cJSON_GetObjectItem(root, "servers"); + if((NULL == interfaces) || (NULL == servers)) + { + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //Ȳcheck interface + //check servers + if(!check_servers(servers->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/SERVERS=\"[ \\t0-9\\.]*\"/SERVERS=\"%s\"/g' /etc/default/isc-dhcp-relay", servers->valuestring); //\" д\\" + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"%s\"/g' /etc/default/isc-dhcp-relay", interfaces->valuestring); //\" д\\" + system(cmd); + /*ļ*/ + system("service isc-dhcp-relay restart"); + } + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + return ret; + +} diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c new file mode 100644 index 000000000..5627a088e --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_shared_network_config.c @@ -0,0 +1,467 @@ +#include "dhcp_subnet_config.h" + +ret_code dhcp_shared_network_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate){ + *config_type = CM_CONFIG_SET; + } + else { + *config_type = CM_CONFIG_GET_ALL; + } + return ret; +} + +ret_code dhcp_shared_network_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *shared=NULL, *name=NULL, *subnet=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *routers=NULL, *dns=NULL, *lease=NULL; + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + return RET_INPUTERR; + } + shared = cJSON_GetObjectItem(root, "shared-network"); + if(NULL == shared) + { + return RET_INPUTERR; + } + + count = cJSON_GetArraySize(shared); + int len = 5000; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + + for(i = 0; i < count; i++) + + { + cJSON *one = cJSON_GetArrayItem(shared, i); + if(NULL == one) + { + continue; + } + name = cJSON_GetObjectItem(one, "name"); + if(NULL == name){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(!check_name(name->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + subnet = cJSON_GetObjectItem(one, "subnet"); + if((!strcmp(operate->valuestring, "add")) || (!strcmp(operate->valuestring, "mod"))){ + if(NULL == subnet){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + segment = cJSON_GetObjectItem(subnet, "segment"); + netmask = cJSON_GetObjectItem(subnet, "netmask"); + range = cJSON_GetObjectItem(subnet, "range"); + routers = cJSON_GetObjectItem(subnet, "routers"); + dns = cJSON_GetObjectItem(subnet, "domain-name-servers"); + //submask = cJSON_GetObjectItem(subnet, "subnet-mask"); + lease = cJSON_GetObjectItem(subnet, "max-lease-time"); + } + //дļ + memset(cmd, 0, len + 1); + if(!strcmp(operate->valuestring, "add")) + { + if((NULL==segment) || (NULL==netmask) || (NULL==range) || (NULL==routers) || (NULL==dns)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check mask + if(!check_mask(netmask->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check segment + if(!check_segment(segment->valuestring, netmask->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check range + if(!check_range(range->valuestring, netmask->valuestring, segment->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check routers + if(!check_ip(routers->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check dns + if(!check_dns(dns->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check lease + if(lease && !check_lease(lease->valuestring)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //ж֮ǰǷshared-networksubnet + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*shared-network[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", name->valuestring); + system(cmd); + if(NULL != getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", segment->valuestring, netmask->valuestring); + system(cmd); + if(NULL != getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo -e 'shared-network %s {\\n subnet %s netmask %s {\\n range %s;\\n option domain-name-servers %s;\\n option subnet-mask %s;\\n option routers %s;' >> /etc/dhcp/dhcpd.conf", + name->valuestring, segment->valuestring, netmask->valuestring, range->valuestring, dns->valuestring, netmask->valuestring, routers->valuestring); + system(cmd); + if(NULL != lease){ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo ' max-lease-time %s;' >> /etc/dhcp/dhcpd.conf", lease->valuestring); + system(cmd); + } + system("echo -e ' }\\n}' >> /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "del")){ + //ж֮ǰǷ + snprintf(cmd, len, "grep -Pzo '[\\s\\t]*shared-network[\\s\\t]+%s[\\s\\t]*{' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", name->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + system("rm -rf /tmp/subconf.conf"); + memset(cmd, 0, len + 1); + //snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{\\n[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\-]+\\}\\n[ \\t]*\\}//g' /etc/dhcp/dhcpd.conf", + //name->valuestring, segment->valuestring, netmask->valuestring); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\{\\-]+\\}[ \\t\\n]*\\}/#/g' /etc/dhcp/dhcpd.conf", name->valuestring); //\\-÷ + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "mod")){ + snprintf(cmd, len, "grep -Pzo 'shared-network[\\s\\t]+%s[\\s\\t]*{[\\n\\s\\t]*subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\,\\-]+}[\\n\\s\\t]*}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", + name->valuestring, segment->valuestring, netmask->valuestring); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL != range) + { + if(!check_range(range->valuestring, netmask->valuestring, segment->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(file_consist_str("/tmp/subconf.conf",range->valuestring)){ + //printf("range already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/range[ \\t0-9\\.]+;/range %s;/g' /tmp/subconf.conf", range->valuestring); + system(cmd); + } + if(NULL != dns) + { + if(!check_dns(dns->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strdns = (char *)malloc(80); + memset(strdns, 0, 80); + snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring); + //printf("strdns: %s\n", strdns); + if(file_consist_str("/tmp/subconf.conf",strdns)){ + //printf("dns already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+domain-name-servers[ \\t0-9\\.\\,]+;/option domain-name-servers %s;/g' /tmp/subconf.conf", dns->valuestring); + system(cmd); + } + + if(NULL != routers) + { + if(!check_ip(routers->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strrouters = (char *)malloc(40); + memset(strrouters, 0, 40); + snprintf(strrouters, 40, "option routers %s;", routers->valuestring); + //printf("strdns: %s\n", strdns); + if(file_consist_str("/tmp/subconf.conf",strrouters)){ + //printf("dns already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strrouters){ + memset(strrouters, 0, 40); + free(strrouters); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strrouters){ + memset(strrouters, 0, 40); + free(strrouters); + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+routers[ \\t0-9\\.]+;/option routers %s;/g' /tmp/subconf.conf", routers->valuestring); + system(cmd); + } + if(NULL != lease) + { + if(!check_lease(lease->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strlease = (char *)malloc(1000); + memset(strlease, 0, 1000); + snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring); + if(file_consist_str("/tmp/subconf.conf",strlease)){ + //printf("lease already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + /*lease 0ǻָĬϣҪɾļеmax-lease-time*/ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i 's/max-lease-time[ \\t0-9]+;/#/g' /tmp/subconf.conf"); + system(cmd); + system("sed -r -i '/#/d' /tmp/subconf.conf"); + if(strcmp(lease->valuestring, "0")){ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/\\}[ \\t\\n]*\\}/ max-lease-time %s;\\n \\}\\n\\}/g' /tmp/subconf.conf", lease->valuestring); //Եʱһ滻\\} + //printf("cmd: %s\n", cmd); + system(cmd); + } + } + //ʱļȡַдԭļ + char *conf_after; + char *temp_conf_file = "/tmp/subconf.conf"; + conf_after = getfileall_with_linefeed(temp_conf_file); + if(NULL != conf_after) + { + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*shared-network[ \\t]+%s[ \\t]*\\{[ \\t\\n]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}[ \\t\\n]*\\}/%s/g' /etc/dhcp/dhcpd.conf", + name->valuestring, segment->valuestring, netmask->valuestring, conf_after); + system(cmd); + } + if(conf_after){ + free(conf_after); + conf_after = NULL; + } + system("rm -rf /tmp/subconf.conf"); + } + } + /*ļ*/ + system("service isc-dhcp-server restart"); + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + + return ret; +} + +ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *output_len){ + ret_code ret = RET_OK; + char *fname="/etc/dhcp/dhcpd.conf"; + FILE *fp; + char *name=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL; + cJSON *root = NULL, *arr=NULL, *json = NULL, *subnet = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + name = (char *)malloc(100); + memset(name, 0, 100); + segment = (char *)malloc(20); + memset(segment, 0, 20); + netmask = (char *)malloc(20); + memset(netmask, 0, 20); + range = (char *)malloc(40); + memset(range, 0, 40); + dns = (char *)malloc(40); + memset(dns, 0, 40); + submask = (char *)malloc(20); + memset(submask, 0, 20); + routers= (char *)malloc(20); + memset(routers, 0, 20); + lease = (char *)malloc(1000); + memset(lease, 0, 1000); + + root = cJSON_CreateObject(); + cJSON_AddItemToObject(root, "shared-network", arr = cJSON_CreateArray()); + int count = 0; //}ĸ + + while((fgets(line,1000,fp))!=NULL){ + char *startNAME = strstr(line, "shared-network"); + char *endNAME = strstr(line, "{"); + if((startNAME!=NULL) && (endNAME!=NULL)){ + memcpy(name,startNAME+15,endNAME-startNAME-16); + //printf("ip: %s\n", ip); + json = cJSON_CreateObject(); + cJSON_AddStringToObject(json , "name", name); + cJSON_AddItemToObject(json, "subnet", subnet = cJSON_CreateObject()); + memset(segment, 0, 20); + memset(netmask, 0, 20); + memset(range, 0, 40); + memset(dns, 0, 40); + memset(submask, 0, 20); + memset(routers, 0, 20); + memset(lease, 0, 1000); + } + char *startSUB = strstr(line, "subnet"); + char *startMASK = strstr(line, "netmask"); + char *endMASK = strstr(line, "{"); + if((startSUB!=NULL) && (startMASK!=NULL) && (endMASK!=NULL)){ + memcpy(segment,startSUB+7,startMASK-startSUB-8); + cJSON_AddStringToObject(subnet, "segment", segment); + memcpy(netmask,startMASK+8,endMASK-startMASK-9); + cJSON_AddStringToObject(subnet, "netmask", netmask); + } + char *startRANGE = strstr(line, "range"); + char *endRANGE = strstr(line, ";"); + if((startRANGE!=NULL) && (endRANGE!=NULL)){ + memcpy(range,startRANGE+6,endRANGE-startRANGE-6); + cJSON_AddStringToObject(subnet, "range", range); + } + char *startDNS = strstr(line, "option domain-name-servers"); + char *endDNS = strstr(line, ";"); + if((startDNS!=NULL) && (endDNS!=NULL)){ + memcpy(dns,startDNS+27,endDNS-startDNS-27); + cJSON_AddStringToObject(subnet, "domain-name-servers", dns); + } + char *startSUNMASK = strstr(line, "option subnet-mask"); + char *endSUBMASK = strstr(line, ";"); + if((startSUNMASK!=NULL) && (endSUBMASK!=NULL)){ + memcpy(submask,startSUNMASK+19,endSUBMASK-startSUNMASK-19); + cJSON_AddStringToObject(subnet, "subnet-mask", submask); + } + char *startROUTERS = strstr(line, "option routers"); + char *endROUTERS = strstr(line, ";"); + if((startROUTERS!=NULL) && (endROUTERS!=NULL)){ + memcpy(routers,startROUTERS+15,endROUTERS-startROUTERS-15); + cJSON_AddStringToObject(subnet, "routers", routers); + } + char *startLEASE = strstr(line, "max-lease-time"); + char *endLEASE = strstr(line, ";"); + if((startLEASE!=NULL) && (endLEASE!=NULL)){ + memcpy(lease,startLEASE+15,endLEASE-startLEASE-15); + cJSON_AddStringToObject(subnet, "max-lease-time", lease); + } + char *end = strstr(line, "}"); + if(end && strcmp(name, "")){ + count++; + if(count == 2){ + count = 0; + cJSON_AddItemToArray(arr, json); + //printf("=====================\n"); + memset(name, 0, 100); + } + } + } + if(name != NULL){ + memset(name, 0, 100); + free(name); + } + if(range != NULL){ + memset(range, 0, 40); + free(range); + } + if(dns != NULL){ + memset(dns, 0, 40); + free(dns); + } + if(submask != NULL){ + memset(submask, 0, 20); + free(submask); + } + if(routers != NULL){ + memset(routers, 0, 20); + free(routers); + } + if(lease != NULL){; + memset(lease, 0, 1000); + free(lease); + } + if(segment != NULL){ + memset(segment, 0, 20); + free(segment); + } + if(netmask != NULL){ + memset(netmask, 0, 20); + free(netmask); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; + +} + diff --git a/Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c b/Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c new file mode 100644 index 000000000..e032b4bfc --- /dev/null +++ b/Platform/user/configm/config-server/dhcp_config/dhcp_subnet_config.c @@ -0,0 +1,452 @@ +#include "dhcp_subnet_config.h" + +ret_code dhcp_subnet_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *operate=NULL; + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(operate) { + *config_type = CM_CONFIG_SET; + } + return ret; +} + +ret_code dhcp_subnet_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + int i, count; + cJSON *root=NULL, *operate=NULL, *subnet=NULL; + char *segment=NULL, *mask=NULL, *router=NULL; + + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + operate = cJSON_GetObjectItem(root, "operate"); + if(NULL == operate) + { + return RET_INPUTERR; + } + subnet= cJSON_GetObjectItem(root, "subnet"); + if(NULL == subnet) + { + return RET_INPUTERR; + } + + count = cJSON_GetArraySize(subnet); + int len = 500; + char *cmd = (char *)malloc(len + 1); + if(NULL == cmd) + { + return RET_ERR; + } + for(i = 0; i < count; i++) + { + cJSON *one = cJSON_GetArrayItem(subnet, i); + if(NULL == one) + { + continue; + } + cJSON *interface = cJSON_GetObjectItem(one, "interface"); + if(NULL == interface){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + cJSON *range = cJSON_GetObjectItem(one, "range"); + //cJSON *routers = cJSON_GetObjectItem(one, "routers"); + cJSON *dns = cJSON_GetObjectItem(one, "domain-name-servers"); + cJSON *lease = cJSON_GetObjectItem(one, "max-lease-time"); + + //ݽӿڻȡipΡ롢ip ֮Է棬ڳʱͷ + segment = get_interface_subnet(interface->valuestring); + if(NULL == segment){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + mask = get_interface_mask(interface->valuestring); + if(NULL == mask){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + router = get_interface_ip(interface->valuestring); + if(NULL == router){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + + //дļ + memset(cmd, 0, len + 1); + if(!strcmp(operate->valuestring, "add")) + { + //жǷѾ + if(file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){ + //printf("interface is already configured\n"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL == range){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + //check range + if(!check_range(range->valuestring, mask,segment)){ + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(lease && !check_lease(lease->valuestring)){ + ret = RET_INPUTERR; + //system("echo '}' >> /etc/dhcp/dhcpd.conf"); + goto INPUT_ERROR; + } + if(dns && !check_dns(dns->valuestring)){ + ret = RET_INPUTERR; + //system("echo '}' >> /etc/dhcp/dhcpd.conf"); + goto INPUT_ERROR; + } + /* + subnet 10.0.7.0 netmask 255.255.255.0 { + range 10.0.7.1 10.0.7.10; + option domain-name-servers 202.206.192.33; + option subnet-mask 255.255.255.0; + option routers 10.0.7.1; + max-lease-time 7200; + } + */ + + //ӽӿ + snprintf(cmd, len, "sed -r -i 's/INTERFACESv4=\"/INTERFACESv4=\"%s /g' /etc/default/isc-dhcp-server", interface->valuestring); + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo -e 'subnet %s netmask %s {\\n range %s;\\n option subnet-mask %s;\\n option routers %s;' >> /etc/dhcp/dhcpd.conf", + segment, mask, range->valuestring, mask, router); + system(cmd); + if(NULL != lease){ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo ' max-lease-time %s;' >> /etc/dhcp/dhcpd.conf", lease->valuestring); + system(cmd); + } + if(NULL != dns){ + memset(cmd, 0, len + 1); + snprintf(cmd, len, "echo ' option domain-name-servers %s;' >> /etc/dhcp/dhcpd.conf", dns->valuestring); + system(cmd); + } + system("echo '}' >> /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "del")){ + if(!file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){ + //printf("interface does not exist\n"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + snprintf(cmd, len, "sed -r -i 's/%s //g' /etc/default/isc-dhcp-server", interface->valuestring); + system(cmd); + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/#/g' /etc/dhcp/dhcpd.conf", segment, mask); + system(cmd); + system("sed -r -i '/#/d' /etc/dhcp/dhcpd.conf"); + //system("sed -r -i '/^#*$/d' /etc/dhcp/dhcpd.conf"); + } + if(!strcmp(operate->valuestring, "mod")){ + if(!file_consist_str("/etc/default/isc-dhcp-server", interface->valuestring)){ + //printf("interface does not exist\n"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + snprintf(cmd, len, "grep -Pzo 'subnet[\\s\\t]+%s[\\s\\t]+netmask[\\s\\t]+%s[\\s\\t]*{[a-zA-Z#:;\\s\\t\\n0-9\\.\\,\\-]+}' /etc/dhcp/dhcpd.conf > /tmp/subconf.conf", segment, mask); + system(cmd); + if(NULL == getfileall("/tmp/subconf.conf")){ + //printf("configuration do not exist\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(NULL != range) + { + if(!check_range(range->valuestring, mask,segment)){ + system("rm -rf /tmp/subconf.conf"); + goto INPUT_ERROR; + } + if(file_consist_str("/tmp/subconf.conf",range->valuestring)){ + //printf("range already exists\n"); + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/range[ \\t0-9\\.]+;/range %s;/g' /tmp/subconf.conf", range->valuestring); + system(cmd); + } + if(NULL != dns) + { + if(!check_dns(dns->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strdns = (char *)malloc(80); + memset(strdns, 0, 80); + snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring); + if(file_consist_str("/tmp/subconf.conf",strdns)){ + //printf("dns already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strdns){ + memset(strdns, 0, 80); + free(strdns); + } + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+domain-name-servers[ \\t0-9\\.\\,]+;/option domain-name-servers %s;/g' /tmp/subconf.conf", dns->valuestring); + system(cmd); + } + /* + if(NULL != subnetMask) + { + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+subnet-mask[ \\t0-9\\.]+;/option subnet-mask %s;/g' /tmp/subconf.conf", subnetMask->valuestring); + system(cmd); + } + if(NULL != routers) + { + memset(cmd, 0, len + 1); + snprintf(cmd, len, "sed -r -i 's/option[ \\t]+routers[ \\t0-9\\.]+;/option routers %s;/g' /tmp/subconf.conf", routers->valuestring); + system(cmd); + }*/ + if(NULL != lease) + { + if(!check_lease(lease->valuestring)){ + system("rm -rf /tmp/subconf.conf"); + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + char *strlease = (char *)malloc(1000); + memset(strlease, 0, 1000); + snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring); + if(file_consist_str("/tmp/subconf.conf",strlease)){ + //printf("lease already exists\n"); + system("rm -rf /tmp/subconf.conf"); + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + ret = RET_INPUTERR; + goto INPUT_ERROR; + } + if(strlease){ + memset(strlease, 0, 1000); + free(strlease); + } + /*lease 0ǻָĬϣҪɾļеmax-lease-time*/ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i 's/max-lease-time[ \\t0-9]+;/#/g' /tmp/subconf.conf"); + system(cmd); + system("sed -r -i '/#/d' /tmp/subconf.conf"); + if(strcmp(lease->valuestring, "0")){ + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*\\}/ max-lease-time %s;\\n\\}/g' /tmp/subconf.conf", lease->valuestring); //Եʱһ滻\\} + system(cmd); + } + } + //ʱļȡַдԭļ + char *conf_after; + char *temp_conf_file = "/tmp/subconf.conf"; + conf_after = getfileall_with_linefeed(temp_conf_file); + if(NULL != conf_after) + { + memset(cmd, 0, len+1); + snprintf(cmd, len, "sed -r -i ':a;N;$!ba;s/[ \\t]*subnet[ \\t]+%s[ \\t]+netmask[ \\t]+%s[ \\t]*\\{[a-zA-Z#:; \\t\\n0-9\\.\\,\\-]+\\}/%s/g' /etc/dhcp/dhcpd.conf", segment, mask, conf_after); + system(cmd); + } + if(conf_after){ + free(conf_after); + conf_after = NULL; + } + system("rm -rf /tmp/subconf.conf"); + } + } + /*ļ*/ + system("service isc-dhcp-server restart"); + +INPUT_ERROR: + if(cmd) + { + memset(cmd, 0, len + 1); + free(cmd); + } + if(segment){ + free(segment); + } + if(mask){ + free(mask); + } + if(router){ + free(router); + } + //ret = RET_INPUTERR; + return ret; +} + +ret_code dhcp_subnet_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len) +{ + ret_code ret = RET_OK; + cJSON *root=NULL, *interface=NULL; + /*json*/ + root = cJSON_Parse(input); + if(NULL == root) + { + return RET_INPUTERR; + } + interface = cJSON_GetObjectItem(root, "interface"); + if(NULL == interface) + { + return RET_INPUTERR; + } + //ȡӿκ + char *segment = get_interface_subnet(interface->valuestring); + if(NULL == segment){ + return RET_INPUTERR; + } + char *mask = get_interface_mask(interface->valuestring); + if(NULL == mask){ + return RET_INPUTERR; + } + + //ȡļ + char *fname="/etc/dhcp/dhcpd.conf"; + FILE *fp; + char *subnet=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL; + cJSON *json = NULL; + char *out = NULL; + char line[1000]; + if ((fp=fopen(fname,"r"))==NULL){ + printf("ļ%s\n",fname); + return RET_ERR; + } + fseek(fp,0,SEEK_END); + rewind(fp); + + subnet = (char *)malloc(20); + memset(subnet, 0, 20); + netmask = (char *)malloc(20); + memset(netmask, 0, 20); + range = (char *)malloc(40); + memset(range, 0, 40); + dns = (char *)malloc(40); + memset(dns, 0, 40); + submask = (char *)malloc(20); + memset(submask, 0, 20); + routers= (char *)malloc(20); + memset(routers, 0, 20); + lease = (char *)malloc(1000); + memset(lease, 0, 1000); + + root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "segment", segment); + cJSON_AddStringToObject(root, "netmask", mask); + + while((fgets(line,1000,fp))!=NULL){ + char *startSUB = strstr(line, "subnet"); + char *startMASK = strstr(line, "netmask"); + char *endMASK = strstr(line, "{"); + if((startSUB!=NULL) && (startMASK!=NULL) && (endMASK!=NULL)){ + memcpy(subnet,startSUB+7,startMASK-startSUB-8); + memcpy(netmask,startMASK+8,endMASK-startMASK-9); + if(!strcmp(subnet, segment) && !strcmp(netmask, mask)){ + memset(range, 0, 40); + memset(dns, 0, 40); + memset(submask, 0, 20); + memset(routers, 0, 20); + memset(lease, 0, 1000); + } + } + if((subnet!= NULL) || (netmask!=NULL)){ + // + if(strcmp(subnet, segment) || strcmp(netmask, mask)){ + continue; + } + } + char *startRANGE = strstr(line, "range"); + char *endRANGE = strstr(line, ";"); + if((startRANGE!=NULL) && (endRANGE!=NULL)){ + memcpy(range,startRANGE+6,endRANGE-startRANGE-6); + } + char *startDNS = strstr(line, "option domain-name-servers"); + char *endDNS = strstr(line, ";"); + if((startDNS!=NULL) && (endDNS!=NULL)){ + memcpy(dns,startDNS+27,endDNS-startDNS-27); + } + char *startSUNMASK = strstr(line, "option subnet-mask"); + char *endSUBMASK = strstr(line, ";"); + if((startSUNMASK!=NULL) && (endSUBMASK!=NULL)){ + memcpy(submask,startSUNMASK+19,endSUBMASK-startSUNMASK-19); + } + char *startROUTERS = strstr(line, "option routers"); + char *endROUTERS = strstr(line, ";"); + if((startROUTERS!=NULL) && (endROUTERS!=NULL)){ + memcpy(routers,startROUTERS+15,endROUTERS-startROUTERS-15); + } + char *startLEASE = strstr(line, "max-lease-time"); + char *endLEASE = strstr(line, ";"); + if((startLEASE!=NULL) && (endLEASE!=NULL)){ + memcpy(lease,startLEASE+15,endLEASE-startLEASE-15); + } + } + if(range != NULL){ + cJSON_AddStringToObject(root, "range", range); + memset(range, 0, 40); + free(range); + } + if(dns != NULL){ + cJSON_AddStringToObject(root, "domain-name-servers", dns); + memset(dns, 0, 40); + free(dns); + } + if(submask != NULL){ + cJSON_AddStringToObject(root, "subnet-mask", submask); + memset(submask, 0, 20); + free(submask); + } + if(routers != NULL){ + cJSON_AddStringToObject(root, "routers", routers); + memset(routers, 0, 20); + free(routers); + } + if(lease != NULL){ + cJSON_AddStringToObject(root, "max-lease-time", lease); + memset(lease, 0, 1000); + free(lease); + } + if(subnet != NULL){ + memset(subnet, 0, 20); + free(subnet); + } + if(netmask != NULL){ + memset(netmask, 0, 20); + free(netmask); + } + fclose(fp); + out = cJSON_PrintUnformatted(root); + *output_len = strlen(out) + 1; + memcpy(output, out, *output_len); + free(out); + return ret; +} + diff --git a/Platform/user/configm/config-server/include/configm.h b/Platform/user/configm/config-server/include/configm.h index 6d0ce19a0..92c132699 100755 --- a/Platform/user/configm/config-server/include/configm.h +++ b/Platform/user/configm/config-server/include/configm.h @@ -11,6 +11,13 @@ #include "../web_config/authfree.h" #include "../web_config/auth_parameters.h" +#include "dhcp_client_config.h" +#include "dhcp_host_config.h" +#include "dhcp_lib.h" +#include "dhcp_relay_config.h" +#include "dhcp_shared_network_config.h" +#include "dhcp_subnet_config.h" +#include "dhcp_dhcpd_lease.h" #define CONFIG_INIT_ARRAY \ {\ @@ -162,6 +169,60 @@ log_file_config_proc, \ NULL, \ NULL \ + },\ + {\ + DHCP_HOST_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_host_config_chk, \ + dhcp_host_config_proc, \ + dhcp_host_config_get, \ + NULL \ + },\ + {\ + DHCP_SUBNET_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_subnet_config_chk, \ + dhcp_subnet_config_proc, \ + dhcp_subnet_config_get, \ + NULL \ + },\ + {\ + DHCP_RELAY_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_relay_config_chk, \ + dhcp_relay_config_proc, \ + NULL, \ + NULL \ + },\ + {\ + DHCP_CLIENT_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_client_config_chk, \ + dhcp_client_config_proc, \ + dhcp_client_get, \ + NULL \ + },\ + {\ + DHCP_DHCPD_LEASE, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_dhcpd_lease_config_chk, \ + NULL, \ + NULL, \ + dhcp_dhcpd_lease_get_all \ + },\ + {\ + DHCP_SHARED_NETWORK_CONFIG, \ + CONFIG_FROM_WEB, \ + FALSE, \ + dhcp_shared_network_config_chk, \ + dhcp_shared_network_config_proc, \ + NULL, \ + dhcp_shared_network_config_get_all \ }\ } diff --git a/Platform/user/configm/config-server/include/dhcp_client_config.h b/Platform/user/configm/config-server/include/dhcp_client_config.h new file mode 100644 index 000000000..46f59c084 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_client_config.h @@ -0,0 +1,18 @@ +#ifndef DHCP_CLIENT_CONFIG_H_ +#define DHCP_CLIENT_CONFIG_H_ +#include "dhcp_lib.h" + + +ret_code dhcp_client_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_client_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_client_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); +#endif + diff --git a/Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h b/Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h new file mode 100644 index 000000000..d08619260 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_dhcpd_lease.h @@ -0,0 +1,13 @@ +#ifndef DHCP_DHCPD_LEASE_H_ +#define DHCP_DHCPD_LEASE_H_ +#include "dhcp_lib.h" + + +ret_code dhcp_dhcpd_lease_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len); + +#endif + diff --git a/Platform/user/configm/config-server/include/dhcp_host_config.h b/Platform/user/configm/config-server/include/dhcp_host_config.h new file mode 100644 index 000000000..4ee9aaaf7 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_host_config.h @@ -0,0 +1,26 @@ +#ifndef DHCP_HOST_CONFIG_H_ +#define DHCP_HOST_CONFIG_H_ +#include "dhcp_lib.h" + + + +/* +struct host { + char *host_name; + char *hardware_ethernet; + char *fixed_address; +}*/ + +ret_code dhcp_host_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_host_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_host_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + +#endif diff --git a/Platform/user/configm/config-server/include/dhcp_lib.h b/Platform/user/configm/config-server/include/dhcp_lib.h new file mode 100644 index 000000000..8ee1c12a9 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_lib.h @@ -0,0 +1,62 @@ +#ifndef DHCP_LIB_H_ +#define DHCP_LIB_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "rpc_common.h" +#include +#include "ipconfig.h" + +#include "configm.h" +#include "rpc.h" +#include "netconfig.h" + +char *getfileall(char *fname); + +char *getfilefirstline(char *fname); + + +char *getfileall_with_linefeed(char *fname); + +int check_name(char *name); + +int check_mac(char *mac); + +int file_consist_str(char *fname, char *str); + +int check_range(char *range, char *mask, char *subnet); + +int check_dns(char *dns); + +int check_lease(char *lease); + +int check_ip(char *ip); + + +char *get_interface_subnet(char *interface); + +char *get_interface_ip(char *interface) ; + +char *get_interface_mask(char *interface); + +int check_servers(char *servers); + + +int check_segment(char *segment, char *mask); + +int check_mask(char *mask); + + + +#endif + + diff --git a/Platform/user/configm/config-server/include/dhcp_relay_config.h b/Platform/user/configm/config-server/include/dhcp_relay_config.h new file mode 100644 index 000000000..7eec755df --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_relay_config.h @@ -0,0 +1,17 @@ +#ifndef DHCP_RELAY_CONFIG_H_ +#define DHCP_RELAY_CONFIG_H_ +#include "dhcp_lib.h" + + + + +ret_code dhcp_relay_config_chk(uint source,uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_relay_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + + +#endif diff --git a/Platform/user/configm/config-server/include/dhcp_shared_network_config.h b/Platform/user/configm/config-server/include/dhcp_shared_network_config.h new file mode 100644 index 000000000..780b67c9b --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_shared_network_config.h @@ -0,0 +1,17 @@ +#ifndef DHCP_SHARED_NETWORK_CONFIG_H_ +#define DHCP_SHARED_NETWORK_CONFIG_H_ +#include "dhcp_lib.h" + + +ret_code dhcp_shared_network_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_shared_network_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *output_len); + +#endif + diff --git a/Platform/user/configm/config-server/include/dhcp_subnet_config.h b/Platform/user/configm/config-server/include/dhcp_subnet_config.h new file mode 100644 index 000000000..3f65d6ca1 --- /dev/null +++ b/Platform/user/configm/config-server/include/dhcp_subnet_config.h @@ -0,0 +1,22 @@ +#ifndef DHCP_SUBNET_CONFIG_H_ +#define DHCP_SUBNET_CONFIG_H_ +#include "dhcp_lib.h" + + + +ret_code dhcp_subnet_config_chk(uint source, uint *config_type, + pointer input, int *input_len, + pointer output, int *output_len); + +ret_code dhcp_subnet_config_proc(uint source, uint config_type, + pointer input, int input_len, + pointer output, int *output_len); + +ret_code dhcp_subnet_config_get(uint source, + pointer input, int input_len, + pointer output, int *output_len); + + +#endif + +