2019-08-07 03:49:31 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include "rpc.h"
|
2019-08-07 10:29:41 +00:00
|
|
|
#include "netconfig.h"
|
2019-08-09 03:19:03 +00:00
|
|
|
#include "parsefile.h"
|
|
|
|
|
|
|
|
net_ifnum_t g_if_num = {.wan_num = 2, .lan_num = 4};
|
2019-08-07 10:29:41 +00:00
|
|
|
|
2019-08-07 03:49:31 +00:00
|
|
|
/* call ioctl system call */
|
|
|
|
ret_code if_ioctl(unsigned long request, caddr_t ifreq, int *ret)
|
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if(ret)
|
|
|
|
{
|
|
|
|
*ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock < 0)
|
|
|
|
{
|
|
|
|
rpc_log_error("Cannot create UDP socket");
|
2019-08-21 03:04:20 +00:00
|
|
|
return RET_SYSERR;
|
2019-08-07 03:49:31 +00:00
|
|
|
}
|
|
|
|
if ((err = ioctl(sock, request, ifreq)) < 0)
|
|
|
|
{
|
|
|
|
rpc_log_error("Ioctl error: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
if(ret)
|
|
|
|
{
|
|
|
|
*ret = err;
|
|
|
|
}
|
|
|
|
return RET_SYSERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set a certain interface flag. */
|
|
|
|
static int if_set_flag(char *ifname, short flag, int *code)
|
|
|
|
{
|
|
|
|
struct ifreq ifr = {0};
|
|
|
|
ret_code ret = RET_OK;
|
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
|
|
|
|
2019-08-07 06:36:23 +00:00
|
|
|
ret = if_ioctl(SIOCGIFFLAGS, (caddr_t)&ifr, code);
|
2019-08-07 03:49:31 +00:00
|
|
|
ASSERT_RET(ret);
|
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
|
|
|
ifr.ifr_flags |= flag;
|
|
|
|
|
2019-08-07 06:36:23 +00:00
|
|
|
ret = if_ioctl(SIOCSIFFLAGS, (caddr_t)&ifr, code);
|
2019-08-07 03:49:31 +00:00
|
|
|
ASSERT_RET(ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear a certain interface flag. */
|
|
|
|
static int if_clear_flag(char *ifname, short flag, int *code)
|
|
|
|
{
|
|
|
|
struct ifreq ifr = {0};
|
|
|
|
ret_code ret;
|
|
|
|
|
2019-08-07 06:36:23 +00:00
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
2019-08-07 03:49:31 +00:00
|
|
|
|
2019-08-07 06:36:23 +00:00
|
|
|
ret = if_ioctl(SIOCGIFFLAGS, (caddr_t)&ifr, code);
|
2019-08-07 03:49:31 +00:00
|
|
|
ASSERT_RET(ret);
|
|
|
|
|
2019-08-07 06:36:23 +00:00
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
2019-08-07 03:49:31 +00:00
|
|
|
ifr.ifr_flags &= ~flag;
|
|
|
|
|
2019-08-07 06:36:23 +00:00
|
|
|
ret = if_ioctl(SIOCSIFFLAGS, (caddr_t)&ifr, code);
|
2019-08-07 03:49:31 +00:00
|
|
|
ASSERT_RET(ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_code if_set_up(char *ifname, int *code)
|
|
|
|
{
|
|
|
|
return if_set_flag(ifname, (IFF_UP | IFF_RUNNING), code);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_code if_set_down(char *ifname, int *code)
|
|
|
|
{
|
|
|
|
return if_clear_flag(ifname, IFF_UP, code);
|
|
|
|
}
|
|
|
|
|
2019-08-09 03:19:03 +00:00
|
|
|
int if_lan_num()
|
|
|
|
{
|
|
|
|
return g_if_num.lan_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
int if_wan_num()
|
|
|
|
{
|
|
|
|
return g_if_num.wan_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_code if_num_init()
|
|
|
|
{
|
|
|
|
char role_str[IF_BUFF_LEN] = {0};
|
|
|
|
struct ifreq ifreq[MAX_IF_NUM];
|
|
|
|
int if_count = 0;
|
|
|
|
int lan_count = 0;
|
|
|
|
int wan_count = 0;
|
|
|
|
int i;
|
|
|
|
|
2019-09-17 07:23:14 +00:00
|
|
|
/*char ifname[MAX_IF_NUM][INTERFACE_NAMSIZ];*/
|
|
|
|
|
2019-08-09 03:19:03 +00:00
|
|
|
memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq));
|
|
|
|
|
|
|
|
if_count = if_read_dev_file(ifreq, MAX_IF_NUM);
|
|
|
|
for(i = 0; i < if_count; i++)
|
|
|
|
{
|
|
|
|
memset(role_str, 0, IF_BUFF_LEN);
|
|
|
|
|
|
|
|
if(if_role_file_get(ifreq[i].ifr_name, role_str) != RET_OK)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcasecmp(role_str, "lan"))
|
|
|
|
{
|
|
|
|
lan_count++;
|
|
|
|
}
|
|
|
|
else if(strcasecmp(role_str, "wan"))
|
|
|
|
{
|
|
|
|
wan_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lan_count != 0)
|
|
|
|
{
|
|
|
|
g_if_num.lan_num = lan_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wan_count != 0)
|
|
|
|
{
|
|
|
|
g_if_num.wan_num = wan_count;
|
|
|
|
}
|
|
|
|
|
2019-09-17 07:23:14 +00:00
|
|
|
/*memset(ifname, 0, sizeof(ifname));
|
|
|
|
wan_count = if_wan_get(ifname, MAX_IF_NUM);
|
|
|
|
printf("wan get cnt %d\n", wan_count);
|
|
|
|
for(i = 0; i < wan_count; i++)
|
|
|
|
{
|
|
|
|
printf("interface %s\n", ifname[i]);
|
|
|
|
}*/
|
2019-08-09 03:19:03 +00:00
|
|
|
return RET_OK;
|
|
|
|
}
|
2019-08-07 10:29:41 +00:00
|
|
|
|
2019-09-17 07:23:14 +00:00
|
|
|
int if_wan_get(char ifname[MAX_IF_NUM][INTERFACE_NAMSIZ], int max_port)
|
|
|
|
{
|
|
|
|
char role_str[IF_BUFF_LEN] = {0};
|
|
|
|
struct ifreq ifreq[MAX_IF_NUM];
|
|
|
|
int if_count = 0;
|
|
|
|
int wan_count = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq));
|
|
|
|
|
|
|
|
if_count = if_read_dev_file(ifreq, MAX_IF_NUM);
|
|
|
|
for(i = 0; i < if_count; i++)
|
|
|
|
{
|
|
|
|
memset(role_str, 0, IF_BUFF_LEN);
|
|
|
|
|
|
|
|
if(if_role_file_get(ifreq[i].ifr_name, role_str) != RET_OK)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wan_count >= max_port)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcasecmp(role_str, "wan"))
|
|
|
|
{
|
|
|
|
strncpy(ifname[wan_count], ifreq[i].ifr_name, INTERFACE_NAMSIZ - 1);
|
|
|
|
wan_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wan_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int if_lan_get(char ifname[MAX_IF_NUM][INTERFACE_NAMSIZ], int max_port)
|
|
|
|
{
|
|
|
|
char role_str[IF_BUFF_LEN] = {0};
|
|
|
|
struct ifreq ifreq[MAX_IF_NUM];
|
|
|
|
int if_count = 0;
|
|
|
|
int lan_count = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(&ifreq, 0, MAX_IF_NUM * sizeof(struct ifreq));
|
|
|
|
|
|
|
|
if_count = if_read_dev_file(ifreq, MAX_IF_NUM);
|
|
|
|
for(i = 0; i < if_count; i++)
|
|
|
|
{
|
|
|
|
memset(role_str, 0, IF_BUFF_LEN);
|
|
|
|
|
|
|
|
if(if_role_file_get(ifreq[i].ifr_name, role_str) != RET_OK)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lan_count >= max_port)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(strcasecmp(role_str, "lan"))
|
|
|
|
{
|
|
|
|
strncpy(ifname[lan_count], ifreq[i].ifr_name, INTERFACE_NAMSIZ - 1);
|
|
|
|
lan_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lan_count;
|
|
|
|
}
|
|
|
|
|