MOD aaa-12 获取全部接口的时候,没有ip也可以获取到
SOL 获取全部接口的时候,没有ip也可以获取到 修改人:zhangliang 检视人:zhangliang
This commit is contained in:
parent
6e7d13ba52
commit
dc059f9613
|
@ -3,9 +3,12 @@
|
|||
#include "ipconfig.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_set_up(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();
|
||||
#endif
|
||||
|
|
|
@ -14,6 +14,35 @@
|
|||
#include <netdb.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;
|
||||
}
|
||||
|
||||
|
||||
/* call ioctl system call */
|
||||
ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret)
|
||||
|
@ -104,3 +133,36 @@ ret_code if_set_down(char *ifname, int *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;
|
||||
}
|
||||
|
||||
|
|
|
@ -166,7 +166,7 @@ ret_code ip_config_json_parse_array(pointer input, uint *conf_type,
|
|||
}
|
||||
|
||||
|
||||
ret_code ip_config_format_json(int config_type, ip_config_t *config_buff,
|
||||
ret_code ip_config_format_json(ip_config_t *config_buff,
|
||||
pointer output, int *outlen)
|
||||
{
|
||||
ip_config_string_t tem_buff = {0};
|
||||
|
@ -174,7 +174,6 @@ ret_code ip_config_format_json(int config_type, ip_config_t *config_buff,
|
|||
//cJSON *json_obj = NULL;
|
||||
char *json_ip;
|
||||
|
||||
ip_config->config_type = config_type;
|
||||
ip_config->family = AF_INET;
|
||||
ip_config->prefixlen = config_buff->prefixlen;
|
||||
|
||||
|
@ -188,7 +187,6 @@ ret_code ip_config_format_json(int config_type, ip_config_t *config_buff,
|
|||
return RET_NOMEM;
|
||||
}
|
||||
|
||||
s2j_json_set_basic_element(json_obj, ip_config, int, config_type);
|
||||
s2j_json_set_basic_element(json_obj, ip_config, string, ifname);
|
||||
s2j_json_set_basic_element(json_obj, ip_config, int, family);
|
||||
s2j_json_set_basic_element(json_obj, ip_config, string, ipaddr);
|
||||
|
@ -204,7 +202,7 @@ ret_code ip_config_format_json(int config_type, ip_config_t *config_buff,
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_code ip_config_format_json_array(int config_type, ip_config_t *config_buff,
|
||||
ret_code ip_config_format_json_array(ip_config_t *config_buff,
|
||||
int count, pointer output, int *outlen)
|
||||
{
|
||||
ip_config_string_t tem_buff = {0};
|
||||
|
@ -224,7 +222,6 @@ ret_code ip_config_format_json_array(int config_type, ip_config_t *config_buff,
|
|||
{
|
||||
memset(ip_config, 0, sizeof(ip_config_string_t));
|
||||
|
||||
ip_config->config_type = config_type;
|
||||
ip_config->family = AF_INET;
|
||||
ip_config->prefixlen = config_buff[i].prefixlen;
|
||||
strncpy(ip_config->ifname, config_buff[i].ifname, INTERFACE_NAMSIZ - 1);
|
||||
|
@ -239,7 +236,6 @@ ret_code ip_config_format_json_array(int config_type, ip_config_t *config_buff,
|
|||
|
||||
cJSON_AddItemToArray(json_array, json_obj);
|
||||
|
||||
s2j_json_set_basic_element(json_obj, ip_config, int, config_type);
|
||||
s2j_json_set_basic_element(json_obj, ip_config, string, ifname);
|
||||
s2j_json_set_basic_element(json_obj, ip_config, int, family);
|
||||
s2j_json_set_basic_element(json_obj, ip_config, string, ipaddr);
|
||||
|
@ -343,10 +339,15 @@ ret_code if_get_prefix_all(pointer output, int *output_len, int *code)
|
|||
ifc.ifc_len = MAX_IF_NUM * sizeof(struct ifreq);
|
||||
ifc.ifc_buf = (char *)ifreq;
|
||||
|
||||
ret = if_ioctl(SIOCGIFCONF, (caddr_t)&ifc, code);
|
||||
ASSERT_RET(ret);
|
||||
if_count = if_readlist((caddr_t)&ifreq, MAX_IF_NUM);
|
||||
|
||||
if(if_count == 0)
|
||||
{
|
||||
ret = if_ioctl(SIOCGIFCONF, (caddr_t)&ifc, code);
|
||||
ASSERT_RET(ret);
|
||||
|
||||
if_count = ifc.ifc_len / (sizeof(struct ifreq));
|
||||
if_count = ifc.ifc_len / (sizeof(struct ifreq));
|
||||
}
|
||||
|
||||
rpc_log_info("if num is %d\n", if_count);
|
||||
|
||||
|
@ -377,11 +378,11 @@ ret_code if_get_prefix_all(pointer output, int *output_len, int *code)
|
|||
ret = if_ioctl(SIOCGIFNETMASK, (caddr_t)&netmask, &mask_ret);
|
||||
ASSERT_RET_NO(ret);
|
||||
|
||||
addr = ( struct sockaddr_in * )&(netmask.ifr_netmask );
|
||||
addr = ( struct sockaddr_in * )&(netmask.ifr_netmask);
|
||||
ip_conf[i].prefixlen = ip_masklen(addr->sin_addr);
|
||||
}
|
||||
|
||||
ip_config_format_json_array(CM_CONFIG_GET, ip_conf, if_count, output, output_len);
|
||||
ip_config_format_json_array(ip_conf, if_count, output, output_len);
|
||||
|
||||
rpc_free(ip_conf);
|
||||
return RET_OK;
|
||||
|
@ -553,7 +554,7 @@ ret_code ip_config_get(uint source,
|
|||
RET_ERR_FORMART(ret, code, output, *output_len);
|
||||
ASSERT_RET(ret);
|
||||
|
||||
ip_config_format_json(CM_CONFIG_GET, ip_conf, output, output_len);
|
||||
ip_config_format_json(ip_conf, output, output_len);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue