MOD aaa-12 桥接口自测问题解决,初始化interface配置文件

SOL  桥接口自测问题解决
修改人:zhangliang
检视人:zhangliang
This commit is contained in:
zhanglianghy 2019-08-08 11:57:20 +08:00
parent 1b0c0687b6
commit 9595330e0a
7 changed files with 136 additions and 103 deletions

View File

@ -188,7 +188,6 @@
NAT4_CONFIG, \ NAT4_CONFIG, \
CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \ CONFIG_FROM_WEB|CONFIG_FROM_NETOPEER, \
FALSE, \ FALSE, \
FALSE, \
nat_config_chk, \ nat_config_chk, \
nat_config_proc, \ nat_config_proc, \
NULL, \ NULL, \

View File

@ -3,12 +3,9 @@
#include "ipconfig.h" #include "ipconfig.h"
#include "brconfig.h" #include "brconfig.h"
#define PATH_PROCNET_DEV "/proc/net/dev"
ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret); ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret);
ret_code if_set_up(char *ifname, int *code); ret_code if_set_up(char *ifname, int *code);
ret_code if_set_down(char *ifname, int *code); ret_code if_set_down(char *ifname, int *code);
int if_readlist(caddr_t ifcfg, int max_port);
ret_code net_main(); ret_code net_main();
#endif #endif

View File

@ -1,9 +1,14 @@
#ifndef PARSEFILE_H_ #ifndef PARSEFILE_H_
#define PARSEFILE_H_ #define PARSEFILE_H_
#include <net/if.h>
#include "rpc_common.h" #include "rpc_common.h"
#define IFCONFIG_PATH "/etc/network/interfaces" #define PROC_NET_DEV "/proc/net/dev"
#define ETC_NETWORK_IFS "/etc/network/interfaces"
#define ETC_PRODUCT "/etc/product.conf"
#define IF_BUFF_LEN 128 #define IF_BUFF_LEN 128
ret_code if_conf_file_add(char *if_name); ret_code if_conf_file_add(char *if_name);
@ -15,5 +20,7 @@ void ip_conf_file_set(char *if_name, char *conf_name, char *conf_buff);
void ip_conf_file_del(char *if_name, char *conf_buff); void ip_conf_file_del(char *if_name, char *conf_buff);
ret_code del_sub_string(char *str_in,char *str_sub); ret_code del_sub_string(char *str_in,char *str_sub);
int if_read_from_file(struct ifreq *ifcfg, int max_port);
ret_code if_conf_file_init();
#endif #endif

View File

@ -7,7 +7,6 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -16,34 +15,6 @@
#include "rpc.h" #include "rpc.h"
#include "netconfig.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;
}
/* call ioctl system call */ /* call ioctl system call */
ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret) ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret)
{ {
@ -133,36 +104,4 @@ ret_code if_set_down(char *ifname, int *code)
return if_clear_flag(ifname, IFF_UP, code); return if_clear_flag(ifname, IFF_UP, code);
} }
int if_readlist(caddr_t 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(PATH_PROCNET_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;
}

View File

@ -339,7 +339,7 @@ ret_code if_get_prefix_all(pointer output, int *output_len, int *code)
ifc.ifc_len = MAX_IF_NUM * sizeof(struct ifreq); ifc.ifc_len = MAX_IF_NUM * sizeof(struct ifreq);
ifc.ifc_buf = (char *)ifreq; ifc.ifc_buf = (char *)ifreq;
if_count = if_readlist((caddr_t)&ifreq, MAX_IF_NUM); if_count = if_read_from_file(&ifreq, MAX_IF_NUM);
if(if_count == 0) if(if_count == 0)
{ {

View File

@ -1,9 +1,12 @@
#include "rpc.h" #include "rpc.h"
#include "parsefile.h"
#include "netconfig.h" #include "netconfig.h"
ret_code net_main() ret_code net_main()
{ {
br_bridge_init(); /* 初始化/etc/network/interfaces配置文件 */
if_conf_file_init();
br_bridge_init();
} }

View File

@ -5,9 +5,94 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h>
#include "rpc.h"
#include "parsefile.h" #include "parsefile.h"
#include "rpc.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_from_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;
}
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) int conf_file_write(char *conf_path, char *sum_buf)
@ -152,7 +237,7 @@ int conf_value_in_block_exist(char *conf_path, char *conf_buff)
} }
/* 设置指定配置块中的配置 */ /* 设置指定配置块中的配置 */
int conf_value_in_block_set_ex(char *conf_path, int conf_value_in_block_set(char *conf_path,
char *start_str, char *end_str, char *start_str, char *end_str,
char *conf_name, char *conf_buff) char *conf_name, char *conf_buff)
{ {
@ -270,8 +355,8 @@ next_while:
return conf_file_write(conf_path, sum_buf); return conf_file_write(conf_path, sum_buf);
} }
/* 设置指定配置块中的配置 */ /* 设置指定配置块中的配置, 如果整个接口配置块都没有,则添加接口配置块 */
int conf_value_in_block_set(char *conf_path, int conf_value_in_block_set_add(char *conf_path,
char *start_str, char *end_str, char *start_str, char *end_str,
char *conf_name, char *conf_buff) char *conf_name, char *conf_buff)
{ {
@ -654,7 +739,7 @@ ret_code if_conf_file_add(char *if_name)
sprintf(auto_str, "auto %s", if_name); sprintf(auto_str, "auto %s", if_name);
return conf_value_block_add(IFCONFIG_PATH, auto_str); return conf_value_block_add(ETC_NETWORK_IFS, auto_str);
} }
/* 删除整个接口配置块 */ /* 删除整个接口配置块 */
@ -664,7 +749,7 @@ ret_code if_conf_file_del(char *if_name)
sprintf(auto_str, "auto %s", if_name); sprintf(auto_str, "auto %s", if_name);
return conf_value_block_del(IFCONFIG_PATH, auto_str, "auto"); return conf_value_block_del(ETC_NETWORK_IFS, auto_str, "auto");
} }
/* 设置接口中的配置 */ /* 设置接口中的配置 */
@ -674,8 +759,8 @@ ret_code if_conf_file_set(char *if_name, char *conf_name, char *conf_buff)
sprintf(auto_str, "auto %s", if_name); sprintf(auto_str, "auto %s", if_name);
return conf_value_in_block_set_ex(IFCONFIG_PATH, auto_str, return conf_value_in_block_set(ETC_NETWORK_IFS, auto_str,
"auto", conf_name, conf_buff); "auto", conf_name, conf_buff);
} }
/* 获取接口中的配置 */ /* 获取接口中的配置 */
@ -685,18 +770,10 @@ ret_code if_conf_file_get(char *if_name, char *conf_name, char *conf_buff)
sprintf(auto_str, "auto %s", if_name); sprintf(auto_str, "auto %s", if_name);
return conf_value_in_block_get(IFCONFIG_PATH, auto_str, return conf_value_in_block_get(ETC_NETWORK_IFS, auto_str,
"auto", conf_name, conf_buff); "auto", conf_name, conf_buff);
} }
ret_code if_conf_file_exist(char *if_name, char *conf_buff)
{
char auto_str[IF_BUFF_LEN] = {0};
sprintf(auto_str, "auto %s", if_name);
return conf_value_in_block_exist(IFCONFIG_PATH, auto_str);
}
/* /*
* *
@ -715,8 +792,8 @@ void ip_conf_file_set(char *if_name, char *conf_name, char *conf_buff)
sprintf(iface_str, "iface %s inet", if_name); sprintf(iface_str, "iface %s inet", if_name);
sprintf(static_name, "iface %s inet static\n", if_name); sprintf(static_name, "iface %s inet static\n", if_name);
conf_value_in_block_set(IFCONFIG_PATH, auto_str, "auto", iface_str, static_name); conf_value_in_block_set_add(ETC_NETWORK_IFS, auto_str, "auto", iface_str, static_name);
conf_value_in_block_set(IFCONFIG_PATH, auto_str, "auto", conf_name, conf_buff); conf_value_in_block_set_add(ETC_NETWORK_IFS, auto_str, "auto", conf_name, conf_buff);
return; return;
} }
@ -733,30 +810,41 @@ void ip_conf_file_del(char *if_name, char *conf_buff)
sprintf(auto_str, "auto %s", if_name); sprintf(auto_str, "auto %s", if_name);
conf_value_in_block_del(IFCONFIG_PATH, auto_str, "auto", conf_buff); conf_value_in_block_del(ETC_NETWORK_IFS, auto_str, "auto", conf_buff);
return; return;
} }
ret_code del_sub_string(char *str_in,char *str_sub) /* 获取接口中的配置 */
ret_code if_role_file_get(char *if_name, char *conf_buff)
{ {
char* str_out = (char *)malloc(strlen(str_in) + 1); char start_str[IF_BUFF_LEN] = {0};
char* p;
if(str_out == NULL)
{
return RET_NULLP;
}
while((p = strstr(str_in, str_sub)) != NULL) sprintf(start_str, "interface %s", if_name);
{
*p = '\0';
strcpy(str_out, p + strlen(str_sub)); if(conf_value_in_block_get(ETC_PRODUCT, start_str,
strcat(str_in, str_out); "interface", "role", conf_buff) == RET_NOTFOUND)
{
strcpy(conf_buff, "lan");
} }
return RET_OK;
return RET_OK;
} }
ret_code if_conf_file_init()
{
struct ifreq ifreq[MAX_IF_NUM];
int if_count = 0;
int i;
memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq));
if_count = if_read_from_file(ifreq, MAX_IF_NUM);
for(i = 0; i < if_count; i++)
{
if_conf_file_add(ifreq[i].ifr_name);
}
}