164 lines
3.1 KiB
C
164 lines
3.1 KiB
C
#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"
|
|
#include "netconfig.h"
|
|
#include "parsefile.h"
|
|
|
|
net_ifnum_t g_if_num = {.wan_num = 2, .lan_num = 4};
|
|
|
|
/* 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");
|
|
return RET_SOCKERR;
|
|
}
|
|
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);
|
|
|
|
ret = if_ioctl(SIOCGIFFLAGS, (caddr_t)&ifr, code);
|
|
ASSERT_RET(ret);
|
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
|
ifr.ifr_flags |= flag;
|
|
|
|
ret = if_ioctl(SIOCSIFFLAGS, (caddr_t)&ifr, code);
|
|
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;
|
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
|
|
|
ret = if_ioctl(SIOCGIFFLAGS, (caddr_t)&ifr, code);
|
|
ASSERT_RET(ret);
|
|
|
|
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
|
|
ifr.ifr_flags &= ~flag;
|
|
|
|
ret = if_ioctl(SIOCSIFFLAGS, (caddr_t)&ifr, code);
|
|
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);
|
|
}
|
|
|
|
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;
|
|
|
|
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;
|
|
}
|
|
|
|
return RET_OK;
|
|
}
|
|
|