Add aaa-12 修改dhcp

SOL 修改dhcp
修改人:wuqihy
检视人:yinbin

Signed-off-by: wuqihy <wuqihy@cmhi.chinamobile.com>
This commit is contained in:
wuqihy 2019-09-09 12:25:10 +08:00
parent 7c501d5410
commit d1e3b83056
7 changed files with 89 additions and 41 deletions

View File

@ -133,9 +133,9 @@ ret_code dhcp_client_get(uint source,
char *lease_interface, *ip, *router; char *lease_interface, *ip, *router;
cJSON *json = NULL; cJSON *json = NULL;
char *out = NULL; char *out = NULL;
char line[1000]; char line[1000] = {0};
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return RET_ERR; return RET_ERR;
} }
fseek(fp,0,SEEK_END); fseek(fp,0,SEEK_END);

View File

@ -20,9 +20,9 @@ ret_code dhcp_dhcpd_lease_get_all(uint source, pointer output, int *output_len)
char *ip=NULL, *mac=NULL, *hname=NULL, *time=NULL; char *ip=NULL, *mac=NULL, *hname=NULL, *time=NULL;
cJSON *root = NULL, *arr = NULL, *json = NULL; cJSON *root = NULL, *arr = NULL, *json = NULL;
char *out = NULL; char *out = NULL;
char line[1000]; char line[1000] = {0};
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return RET_ERR; return RET_ERR;
} }
fseek(fp,0,SEEK_END); fseek(fp,0,SEEK_END);

View File

@ -206,7 +206,8 @@ ret_code dhcp_host_config_proc(uint source, uint config_type,
} }
/*运行配置文件*/ /*运行配置文件*/
system("service isc-dhcp-server restart"); //system("service isc-dhcp-server restart");
system("systemctl restart isc-dhcp-server.service");
INPUT_ERROR: INPUT_ERROR:
/*释放内存*/ /*释放内存*/
@ -246,10 +247,10 @@ ret_code dhcp_host_config_get(uint source,
char *name=NULL, *mac=NULL, *ip=NULL; char *name=NULL, *mac=NULL, *ip=NULL;
cJSON *json = NULL; cJSON *json = NULL;
char *out = NULL; char *out = NULL;
char line[1000]; char line[1000] = {0};
int len; int len;
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return RET_ERR; return RET_ERR;
} }
fseek(fp,0,SEEK_END); fseek(fp,0,SEEK_END);

View File

@ -46,7 +46,8 @@ int del_interface_dhcp_cb(BR_EVENT_TYPE event_type, br_event_t event_arg)
free(cmd); free(cmd);
} }
/*运行配置文件*/ /*运行配置文件*/
system("service isc-dhcp-server restart"); //system("service isc-dhcp-server restart");
system("systemctl restart isc-dhcp-server.service");
return ret; return ret;
} }
@ -57,10 +58,10 @@ char *getfileall(char *fname)
} }
FILE *fp; FILE *fp;
char *str; char *str;
char txt[1000]; char txt[1000] = {0};
int filesize; int filesize;
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return NULL; return NULL;
} }
@ -68,8 +69,11 @@ char *getfileall(char *fname)
filesize = ftell(fp); filesize = ftell(fp);
str=(char *)malloc(filesize); str=(char *)malloc(filesize);
str[0]=0; if(!str){
fclose(fp);
return NULL;
}
memset(str, 0, filesize);
rewind(fp); rewind(fp);
while((fgets(txt,1000,fp))!=NULL){ while((fgets(txt,1000,fp))!=NULL){
strcat(str,txt); strcat(str,txt);
@ -88,10 +92,10 @@ char *getfilefirstline(char *fname)
} }
FILE *fp; FILE *fp;
char *str; char *str;
char txt[1000]; char txt[1000] = {0};
int filesize; int filesize;
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return NULL; return NULL;
} }
@ -99,7 +103,11 @@ char *getfilefirstline(char *fname)
filesize = ftell(fp); filesize = ftell(fp);
str=(char *)malloc(filesize); str=(char *)malloc(filesize);
str[0]=0; if(!str){
fclose(fp);
return NULL;
}
memset(str, 0, filesize);
rewind(fp); rewind(fp);
@ -126,10 +134,10 @@ char *getfileall_with_linefeed(char *fname)
FILE *fp; FILE *fp;
char *str; char *str;
char *p; char *p;
char line[1000]; char line[1000] = {0};
int filesize; int filesize;
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return NULL; return NULL;
} }
@ -137,7 +145,11 @@ char *getfileall_with_linefeed(char *fname)
filesize = ftell(fp); filesize = ftell(fp);
str=(char *)malloc(filesize+100); str=(char *)malloc(filesize+100);
str[0]=0; if(!str){
fclose(fp);
return NULL;
}
memset(str, 0, filesize);
rewind(fp); rewind(fp);
while((fgets(line,1000,fp))!=NULL){ while((fgets(line,1000,fp))!=NULL){
@ -185,8 +197,8 @@ int check_mac(char *mac){
const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$"; const char * pattern = "^([A-Fa-f0-9]{2}[-,:]){5}[A-Fa-f0-9]{2}$";
const int cflags = REG_EXTENDED | REG_NEWLINE; const int cflags = REG_EXTENDED | REG_NEWLINE;
char ebuf[128]; char ebuf[128] = {0};
regmatch_t pmatch[1]; regmatch_t pmatch[1] = {0};
int nmatch = 10; int nmatch = 10;
regex_t reg; regex_t reg;
@ -214,12 +226,15 @@ failed:
return FALSE; return FALSE;
} }
int file_consist_str(char *fname, char *str){ int file_consist_str(char *fname, char *str){
if(!fname || !str){
return FALSE;
}
//读取文件 //读取文件
FILE *fp; FILE *fp;
//char *name, *mac, *ip; //char *name, *mac, *ip;
char line[1000]; char line[1000] = {0};
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return FALSE; return FALSE;
} }
fseek(fp,0,SEEK_END); fseek(fp,0,SEEK_END);
@ -241,12 +256,12 @@ int check_range(char *range, char *mask, char *subnet){
return FALSE; return FALSE;
} }
int len = strlen(range); int len = strlen(range);
char rangeARR[len+1]; char rangeARR[len+1] = {0};
strcpy(rangeARR, range); strcpy(rangeARR, range);
//1、分出两个ip & 判断是ip //1、分出两个ip & 判断是ip
struct in_addr low, high, subnetadd, maskaddr; struct in_addr low, high, subnetadd, maskaddr;
char *item; char *item = NULL;
item = strtok(rangeARR, " "); item = strtok(rangeARR, " ");
int code, lowsub, highsub; int code, lowsub, highsub;
if(NULL == item){ if(NULL == item){
@ -282,9 +297,9 @@ int check_range(char *range, char *mask, char *subnet){
} }
//3、后一个比前一个大 //3、后一个比前一个大
char lowARR[15]; char lowARR[15] = {0};
strcpy(lowARR, inet_ntoa(low)); strcpy(lowARR, inet_ntoa(low));
char highARR[15]; char highARR[15] = {0};
strcpy(highARR, inet_ntoa(high)); strcpy(highARR, inet_ntoa(high));
if(strcmp(lowARR, highARR) >= 0){ if(strcmp(lowARR, highARR) >= 0){
return FALSE; return FALSE;
@ -297,11 +312,11 @@ int check_dns(char *dns){
return FALSE; return FALSE;
} }
int len = strlen(dns); int len = strlen(dns);
char dnsARR[len+1]; char dnsARR[len+1] = {0};
strcpy(dnsARR, dns); strcpy(dnsARR, dns);
//拆分DNS并判断是否是ip //拆分DNS并判断是否是ip
char *item; char *item = NULL;
item = strtok(dnsARR, ","); item = strtok(dnsARR, ",");
if(NULL == item){ if(NULL == item){
return FALSE; return FALSE;
@ -325,6 +340,9 @@ int check_dns(char *dns){
} }
int check_lease(char *lease){ int check_lease(char *lease){
if(!lease){
return FALSE;
}
//是数字就可以 //是数字就可以
int i=0; int i=0;
while(lease[i] != '\0'){ while(lease[i] != '\0'){
@ -361,7 +379,7 @@ char *get_interface_subnet(char *interface) {
return NULL; return NULL;
} }
struct in_addr ipadd, maskadd, subnet; struct in_addr ipadd, maskadd, subnet;
char *ip, *mask; char *ip = NULL, *mask = NULL;
ip = get_interface_ip(interface); ip = get_interface_ip(interface);
mask = get_interface_mask(interface); mask = get_interface_mask(interface);
@ -412,6 +430,9 @@ char *get_interface_mask(char *interface) {
return NULL; return NULL;
} }
char *cmd = (char *)malloc(101); char *cmd = (char *)malloc(101);
if(!cmd){
return NULL;
}
memset(cmd, 0, 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 \"Mask\" | awk '{ print $4}' | awk -F: '{print $2}' > /tmp/mask", interface);
snprintf(cmd, 100, "ifconfig %s | grep \"netmask\" | awk '{ print $4}' > /tmp/mask", interface); snprintf(cmd, 100, "ifconfig %s | grep \"netmask\" | awk '{ print $4}' > /tmp/mask", interface);
@ -427,11 +448,11 @@ int check_servers(char *servers){
return FALSE; return FALSE;
} }
int len = strlen(servers); int len = strlen(servers);
char arr[len+1]; char arr[len+1] = {0};
strcpy(arr, servers); strcpy(arr, servers);
//拆分servers并判断是否是ip //拆分servers并判断是否是ip
char *item; char *item = NULL;
item = strtok(arr, " "); item = strtok(arr, " ");
if(NULL == item){ if(NULL == item){
return FALSE; return FALSE;
@ -446,6 +467,9 @@ int check_servers(char *servers){
} }
int check_segment(char *segment, char *mask){ int check_segment(char *segment, char *mask){
if(!segment || !mask){
return FALSE;
}
//1、是ip是网段ip //1、是ip是网段ip
struct in_addr segmentadd, maskadd; struct in_addr segmentadd, maskadd;
@ -463,7 +487,7 @@ int check_mask(char *mask)
{ {
if(check_ip(mask)) if(check_ip(mask))
{ {
unsigned int b = 0, i, n[4]; unsigned int b = 0, i, n[4] = {0};
sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]); sscanf(mask, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]);
for(i = 0; i < 4; ++i){ //将子网掩码存入32位无符号整型 for(i = 0; i < 4; ++i){ //将子网掩码存入32位无符号整型
b += n[i] << (i * 8); b += n[i] << (i * 8);

View File

@ -37,6 +37,10 @@ ret_code dhcp_relay_config_proc(uint source, uint config_type,
ret = RET_INPUTERR; ret = RET_INPUTERR;
goto INPUT_ERROR; goto INPUT_ERROR;
} }
if(!operate->valuestring){
ret = RET_INPUTERR;
goto INPUT_ERROR;
}
if(!strcmp(operate->valuestring, "close relay")){ if(!strcmp(operate->valuestring, "close relay")){
//删除配置文件 //删除配置文件
@ -70,7 +74,8 @@ ret_code dhcp_relay_config_proc(uint source, uint config_type,
snprintf(cmd, len, "sed -r -i 's/INTERFACES=\"[ \\ta-zA-Z0-9\\.]*\"/INTERFACES=\"%s\"/g' /etc/default/isc-dhcp-relay", interfaces->valuestring); //\" 不能写成\\" 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(cmd);
/*运行配置文件*/ /*运行配置文件*/
system("service isc-dhcp-relay restart"); //system("service isc-dhcp-relay restart");
system("systemctl restart isc-dhcp-relay.service");
} }
INPUT_ERROR: INPUT_ERROR:

View File

@ -40,6 +40,10 @@ ret_code dhcp_shared_network_config_proc(uint source, uint config_type,
{ {
return RET_INPUTERR; return RET_INPUTERR;
} }
if(!operate->valuestring)
{
return RET_INPUTERR;
}
shared = cJSON_GetObjectItem(root, "shared-network"); shared = cJSON_GetObjectItem(root, "shared-network");
if(NULL == shared) if(NULL == shared)
{ {
@ -203,6 +207,10 @@ ret_code dhcp_shared_network_config_proc(uint source, uint config_type,
goto INPUT_ERROR; goto INPUT_ERROR;
} }
char *strdns = (char *)malloc(80); char *strdns = (char *)malloc(80);
if(!strdns){
ret = RET_NULLP;
goto INPUT_ERROR;
}
memset(strdns, 0, 80); memset(strdns, 0, 80);
snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring); snprintf(strdns, 80, "option domain-name-servers %s;", dns->valuestring);
//printf("strdns: %s\n", strdns); //printf("strdns: %s\n", strdns);
@ -291,7 +299,7 @@ ret_code dhcp_shared_network_config_proc(uint source, uint config_type,
} }
} }
//从临时文件读取字符串,写入原配置文件 //从临时文件读取字符串,写入原配置文件
char *conf_after; char *conf_after = NULL;
char *temp_conf_file = "/tmp/subconf.conf"; char *temp_conf_file = "/tmp/subconf.conf";
conf_after = getfileall_with_linefeed(temp_conf_file); conf_after = getfileall_with_linefeed(temp_conf_file);
if(NULL != conf_after) if(NULL != conf_after)
@ -309,7 +317,8 @@ ret_code dhcp_shared_network_config_proc(uint source, uint config_type,
} }
} }
/*运行配置文件*/ /*运行配置文件*/
system("service isc-dhcp-server restart"); //system("service isc-dhcp-server restart");
system("systemctl restart isc-dhcp-server.service");
INPUT_ERROR: INPUT_ERROR:
if(cmd) if(cmd)
@ -328,9 +337,9 @@ ret_code dhcp_shared_network_config_get_all(uint source, pointer output, int *ou
char *name=NULL, *segment=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL; 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; cJSON *root = NULL, *arr=NULL, *json = NULL, *subnet = NULL;
char *out = NULL; char *out = NULL;
char line[1000]; char line[1000] = {0};
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("open file %s failed\n",fname);
return RET_ERR; return RET_ERR;
} }
fseek(fp,0,SEEK_END); fseek(fp,0,SEEK_END);

View File

@ -38,6 +38,10 @@ ret_code dhcp_subnet_config_proc(uint source, uint config_type,
{ {
return RET_INPUTERR; return RET_INPUTERR;
} }
if(!operate->valuestring)
{
return RET_INPUTERR;
}
subnet= cJSON_GetObjectItem(root, "subnet"); subnet= cJSON_GetObjectItem(root, "subnet");
if(NULL == subnet) if(NULL == subnet)
{ {
@ -236,6 +240,10 @@ ret_code dhcp_subnet_config_proc(uint source, uint config_type,
goto INPUT_ERROR; goto INPUT_ERROR;
} }
char *strlease = (char *)malloc(1000); char *strlease = (char *)malloc(1000);
if(!strlease){
ret = RET_NULLP;
goto INPUT_ERROR;
}
memset(strlease, 0, 1000); memset(strlease, 0, 1000);
snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring); snprintf(strlease, 1000, "max-lease-time %s;", lease->valuestring);
if(file_consist_str("/tmp/subconf.conf",strlease)){ if(file_consist_str("/tmp/subconf.conf",strlease)){
@ -264,7 +272,7 @@ ret_code dhcp_subnet_config_proc(uint source, uint config_type,
} }
} }
//从临时文件读取字符串,写入原配置文件 //从临时文件读取字符串,写入原配置文件
char *conf_after; char *conf_after = NULL;
char *temp_conf_file = "/tmp/subconf.conf"; char *temp_conf_file = "/tmp/subconf.conf";
conf_after = getfileall_with_linefeed(temp_conf_file); conf_after = getfileall_with_linefeed(temp_conf_file);
if(NULL != conf_after) if(NULL != conf_after)
@ -281,7 +289,8 @@ ret_code dhcp_subnet_config_proc(uint source, uint config_type,
} }
} }
/*运行配置文件*/ /*运行配置文件*/
system("service isc-dhcp-server restart"); //system("service isc-dhcp-server restart");
system("systemctl restart isc-dhcp-server.service");
INPUT_ERROR: INPUT_ERROR:
if(cmd) if(cmd)
@ -331,11 +340,11 @@ ret_code dhcp_subnet_config_get(uint source,
//读取配置文件 //读取配置文件
char *fname="/etc/dhcp/dhcpd.conf"; char *fname="/etc/dhcp/dhcpd.conf";
FILE *fp; FILE *fp = NULL;
char *subnet=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL; char *subnet=NULL, *netmask=NULL, *range=NULL, *dns=NULL, *submask=NULL, *routers=NULL, *lease=NULL;
cJSON *json = NULL; cJSON *json = NULL;
char *out = NULL; char *out = NULL;
char line[1000]; char line[1000] = {0};
if ((fp=fopen(fname,"r"))==NULL){ if ((fp=fopen(fname,"r"))==NULL){
printf("打开文件%s错误\n",fname); printf("打开文件%s错误\n",fname);
return RET_ERR; return RET_ERR;