secgateway/Platform/user/configm/config-server/netconfig/parsefile.c

896 lines
20 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include "rpc.h"
#include "parsefile.h"
#include "netconfig.h"
static char *if_parse_name(char *name, char *p)
{
while (isspace(*p))
p++;
while (*p) {
if (isspace(*p))
break;
if (*p == ':') { /* could be an alias */
char *dot = p, *dotname = name;
*name++ = *p++;
while (isdigit(*p))
*name++ = *p++;
if (*p != ':') { /* it wasn't, backup */
p = dot;
name = dotname;
}
if (*p == '\0')
return NULL;
p++;
break;
}
*name++ = *p++;
}
*name++ = '\0';
return p;
}
int if_read_dev_file(struct ifreq *ifcfg, int max_port)
{
struct ifreq *ifreq = (struct ifreq *)ifcfg;
char name[128] = {0};
char buf[512] = {0};
int i = 0;
FILE *fh;
fh = fopen(PROC_NET_DEV, "r");
if (!fh) {
return 0;
}
fgets(buf, sizeof buf, fh); /* eat line */
fgets(buf, sizeof buf, fh);
while (fgets(buf, sizeof buf, fh)) {
if(i >= max_port){
break;
}
memset(name, 0, 128);
if_parse_name(name, buf);
if(strlen(name) != 0){
strncpy(ifreq[i].ifr_name, name, sizeof(ifreq[i].ifr_name));
i++;
}
}
fclose(fh);
return i;
}
int if_read_conf_file(struct ifreq *ifcfg, int max_port)
{
struct ifreq *ifreq = (struct ifreq *)ifcfg;
char name[INTERFACE_NAMSIZ] = {0};
char buf[512] = {0};
char *p = NULL;
int i = 0, j = 0;
FILE *fh;
fh = fopen(ETC_NETWORK_IFS, "r");
if (!fh) {
return 0;
}
while (fgets(buf, sizeof(buf), fh)) {
p = NULL;
if(i >= max_port){
break;
}
if(p = strstr(buf, "iface"))
{
p += strlen("iface");
while (isspace(*p)){
p++;
}
j = 0;
memset(name, 0, INTERFACE_NAMSIZ);
while(!isspace(*p)){
name[j++] = *p++;
if(j >= INTERFACE_NAMSIZ){
break;
}
}
name[INTERFACE_NAMSIZ - 1] = '\0';
strncpy(ifreq[i].ifr_name, name, sizeof(ifreq[i].ifr_name));
i++;
}
}
fclose(fh);
return i;
}
ret_code del_sub_string(char *str_in,char *str_sub)
{
char* str_out = (char *)malloc(strlen(str_in) + 1);
char* p;
if(str_out == NULL)
{
return RET_NULLP;
}
while((p = strstr(str_in, str_sub)) != NULL)
{
*p = '\0';
strcpy(str_out, p + strlen(str_sub));
strcat(str_in, str_out);
}
return RET_OK;
}
/* 缓存字符串保存到配置文件中 */
int conf_file_write(char *conf_path, char *sum_buf)
{
FILE *fp;
fp = fopen(conf_path,"w+");
if(fp == NULL)
{
rpc_log_error("OPEN CONFIG FALID\n");
return RET_ERR;
}
fseek(fp,0,SEEK_SET);
fputs(sum_buf,fp);
fclose(fp);
return RET_OK;
}
/* 设置指定配置块中的配置 */
int conf_value_in_block_get(char *conf_path,
char *start_str, char *end_str,
char *conf_name, char *conf_buff)
{
char config_linebuf[IF_BUFF_LEN];
boolean next_flag = FALSE;
char *start_line = NULL;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
rpc_log_error("OPEN CONFIG %s FALID\n", conf_path);
return RET_ERR;
}
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
break;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
goto next_while;
}
/* 没有找到配置块,则继续循环 */
if(start_line == NULL)
{
start_line = strstr(config_linebuf, start_str);
goto next_while;
}
/* 配置块结束 */
if(strstr(config_linebuf, end_str))
{
break;
}
if(strstr(config_linebuf, conf_name))
{
next_flag = TRUE;
strncpy(conf_buff, config_linebuf, IF_BUFF_LEN);
break;
}
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
fclose(f);
if(next_flag == TRUE)
{
rpc_log_dbg("---get_buf---->%s<----------\n",conf_buff);
return RET_OK;
}
return RET_NOTFOUND;
}
/* 设置指定配置块中的配置 */
int conf_value_in_block_exist(char *conf_path, char *conf_buff)
{
char config_linebuf[IF_BUFF_LEN];
boolean next_flag = FALSE;
char *start_line = NULL;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
rpc_log_error("OPEN CONFIG %s FALID\n", conf_path);
return RET_ERR;
}
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
{
if(strstr(config_linebuf, conf_buff))
{
next_flag = TRUE;
break;
}
memset(config_linebuf, 0, sizeof(config_linebuf));
}
fclose(f);
if(next_flag == TRUE)
{
rpc_log_dbg("---get_buf---->%s<----------\n",conf_buff);
return RET_OK;
}
return RET_NOTFOUND;
}
/* 设置指定配置块中的配置 */
int conf_value_in_block_set(char *conf_path,
char *start_str, char *end_str,
char *conf_name, char *conf_buff)
{
char config_linebuf[IF_BUFF_LEN];
int configbuf_lenth = strlen(conf_buff) + 5;
long config_lenth = 0;
boolean next_flag = FALSE;
char *start_line = NULL;
char *config_line = NULL;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
rpc_log_error("OPEN CONFIG %s FALID\n", conf_path);
return RET_ERR;
}
fseek(f, 0, SEEK_END);
config_lenth = ftell(f);
char sum_buf[config_lenth + configbuf_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 没有找到配置块,则继续循环 */
if(start_line == NULL)
{
start_line = strstr(config_linebuf, start_str);
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 配置块结束 */
if(strstr(config_linebuf, end_str))
{
if(config_line == NULL)
{
strcat(sum_buf, conf_buff);
}
strcat(sum_buf, config_linebuf);
next_flag = TRUE;
goto next_while;
}
if(config_line == NULL)
{
config_line = strstr(config_linebuf, conf_name);
/* 找到配置行 */
if(config_line)
{
next_flag = TRUE;
strcat(sum_buf, conf_buff);
goto next_while;
}
}
strcat(sum_buf, config_linebuf);
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
fclose(f);
if( next_flag == FALSE )
{
/* 整个配置块都没有 */
if(start_line == NULL)
{
return RET_NOTFOUND;
}
if(config_line == NULL)
{
strcat(sum_buf, conf_buff);
}
}
remove(conf_path);
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
return conf_file_write(conf_path, sum_buf);
}
/* 设置指定配置块中的配置, 如果整个接口配置块都没有,则添加接口配置块 */
int conf_value_in_block_set_add(char *conf_path,
char *start_str, char *end_str,
char *conf_name, char *conf_buff)
{
char config_linebuf[IF_BUFF_LEN];
int configbuf_lenth = 0;
long config_lenth = 0;
boolean next_flag = FALSE;
char *start_line = NULL;
char *config_line = NULL;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
rpc_log_error("OPEN CONFIG %s FALID\n", conf_path);
return RET_ERR;
}
fseek(f, 0, SEEK_END);
config_lenth = ftell(f);
configbuf_lenth = strlen(start_str) + strlen(conf_buff) + 5;
char sum_buf[config_lenth + configbuf_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 没有找到配置块,则继续循环 */
if(start_line == NULL)
{
start_line = strstr(config_linebuf, start_str);
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 配置块结束 */
if(strstr(config_linebuf, end_str))
{
if(config_line == NULL)
{
strcat(sum_buf, conf_buff);
}
strcat(sum_buf, config_linebuf);
next_flag = TRUE;
goto next_while;
}
if(config_line == NULL)
{
config_line = strstr(config_linebuf, conf_name);
/* 找到配置行 */
if(config_line)
{
next_flag = TRUE;
strcat(sum_buf, conf_buff);
goto next_while;
}
}
strcat(sum_buf, config_linebuf);
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
/* 整个配置块都没有,则新创建该配置块 */
if( next_flag == FALSE )
{
if(start_line == NULL)
{
strcat(sum_buf, start_str);
strcat(sum_buf, "\n");
}
if(config_line == NULL)
{
strcat(sum_buf, conf_buff);
}
}
remove(conf_path);
fclose(f);
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
return conf_file_write(conf_path, sum_buf);
}
/* 删除指定配置块中的配置 */
int conf_value_in_block_del(char *conf_path, char *start_str,
char *end_str, char *conf_buff)
{
char config_linebuf[IF_BUFF_LEN];
int configbuf_lenth = strlen(conf_buff) + 5;
long congig_lenth = 0;
boolean next_flag = FALSE;
char *start_line = NULL;
FILE *f = fopen(conf_path, "r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID\n");
return RET_ERR;
}
fseek(f, 0, SEEK_END);
congig_lenth = ftell(f);
char sum_buf[congig_lenth + configbuf_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN,f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 没有找到接口配置块,则继续循环 */
if(start_line == NULL)
{
start_line = strstr(config_linebuf, start_str);
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 已经是下一个接口了, 则表示无法找到*/
if(strstr(config_linebuf, end_str))
{
strcat(sum_buf, config_linebuf);
next_flag = TRUE;
goto next_while;
}
/* 找到配置行 */
if(strstr(config_linebuf, conf_buff))
{
next_flag = TRUE;
}
else
{
strcat(sum_buf, config_linebuf);
}
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
remove(conf_path);
fclose(f);
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
return conf_file_write(conf_path, sum_buf);
}
/* 设置指定配置块中的配置 */
int conf_value_block_add(char *conf_path, char *conf_buff)
{
char config_linebuf[IF_BUFF_LEN];
int configbuf_lenth = strlen(conf_buff) + 5;
long config_lenth = 0;
boolean next_flag = FALSE;
FILE *f;
f = fopen(conf_path,"r+");
if(f == NULL)
{
rpc_log_error("OPEN CONFIG %s FALID\n", conf_path);
return RET_ERR;
}
fseek(f, 0, SEEK_END);
config_lenth = ftell(f);
char sum_buf[config_lenth + configbuf_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN, f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 找到配置块,则继续循环 */
if(strstr(config_linebuf, conf_buff))
{
next_flag = TRUE;
strcat(sum_buf, config_linebuf);
break;
}
strcat(sum_buf, config_linebuf);
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
fclose(f);
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
/* 整个配置块都没有,则新创建该配置块 */
if( next_flag == FALSE )
{
strcat(sum_buf, conf_buff);
strcat(sum_buf, "\n");
remove(conf_path);
return conf_file_write(conf_path, sum_buf);
}
return RET_EXIST;
}
int conf_value_block_del(char *conf_path, char *start_str, char *end_str)
{
char config_linebuf[IF_BUFF_LEN];
long congig_lenth = 0;
boolean next_flag = FALSE;
char *start_line = NULL;
FILE *f = fopen(conf_path, "r+");
if(f == NULL)
{
printf("OPEN CONFIG FALID\n");
return RET_ERR;
}
fseek(f, 0, SEEK_END);
congig_lenth = ftell(f);
char sum_buf[congig_lenth];
memset(sum_buf, 0, sizeof(sum_buf));
fseek(f, 0, SEEK_SET);
memset(config_linebuf, 0, sizeof(config_linebuf));
while(fgets(config_linebuf, IF_BUFF_LEN,f) != NULL)
{
/* 该做的事情已经做完 */
if(next_flag == TRUE)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 判断是否是空行 */
if(strlen(config_linebuf) < 3)
{
strcat(sum_buf, config_linebuf);
goto next_while;
}
/* 没有找到接口配置块,则继续循环 */
if(start_line == NULL)
{
start_line = strstr(config_linebuf, start_str);
if(start_line == NULL)
{
strcat(sum_buf, config_linebuf);
}
goto next_while;
}
/* 已经到了end, 删除结束*/
if(strstr(config_linebuf, end_str))
{
strcat(sum_buf, config_linebuf);
next_flag = TRUE;
goto next_while;
}
if(next_flag == TRUE)
{
strcat(sum_buf, config_linebuf);
}
next_while:
if(fgetc(f)==EOF)
{
break;
}
fseek(f,-1,SEEK_CUR);
memset(config_linebuf, 0, sizeof(config_linebuf));
}
remove(conf_path);
fclose(f);
rpc_log_dbg("---sum_buf---->%s<----------\n",sum_buf);
return conf_file_write(conf_path, sum_buf);
}
/* 增加接口配置块 */
ret_code if_conf_file_add(char *if_name)
{
char auto_str[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
return conf_value_block_add(ETC_NETWORK_IFS, auto_str);
}
/* 删除整个接口配置块 */
ret_code if_conf_file_del(char *if_name)
{
char auto_str[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
return conf_value_block_del(ETC_NETWORK_IFS, auto_str, "auto");
}
/* 设置接口中的配置 */
ret_code if_conf_file_set(char *if_name, char *conf_name, char *conf_buff)
{
char auto_str[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
return conf_value_in_block_set(ETC_NETWORK_IFS, auto_str,
"auto", conf_name, conf_buff);
}
/* 获取接口中的配置 */
ret_code if_conf_file_get(char *if_name, char *conf_name, char *conf_buff)
{
char auto_str[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
return conf_value_in_block_get(ETC_NETWORK_IFS, auto_str,
"auto", conf_name, conf_buff);
}
/*
*
*
*1 2 3
*
*/
void ip_conf_file_set(char *if_name, char *conf_name, char *conf_buff)
{
char auto_str[IF_BUFF_LEN] = {0};
char iface_str[IF_BUFF_LEN] = {0};
char static_name[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
sprintf(iface_str, "iface %s inet", if_name);
sprintf(static_name, "iface %s inet static\n", if_name);
conf_value_in_block_set_add(ETC_NETWORK_IFS, auto_str, "auto", iface_str, static_name);
conf_value_in_block_set_add(ETC_NETWORK_IFS, auto_str, "auto", conf_name, conf_buff);
return;
}
/*
*
*
*1
*
*/
void ip_conf_file_del(char *if_name, char *conf_buff)
{
char auto_str[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
conf_value_in_block_del(ETC_NETWORK_IFS, auto_str, "auto", conf_buff);
return;
}
/* 获取接口中的配置 */
ret_code if_role_file_get(char *if_name, char *conf_buff)
{
char start_str[IF_BUFF_LEN] = {0};
char *p;
sprintf(start_str, "interface %s", if_name);
if(conf_value_in_block_get(ETC_PRODUCT, start_str,
"interface", "role", conf_buff) == RET_OK)
{
p = conf_buff + strlen("role");
while(*p != '\0' && isspace(*p))
{
p++;
}
if(*p != '\0')
{
strncpy(conf_buff, p, IF_BUFF_LEN);
return RET_OK;
}
}
return RET_NOTFOUND;
}