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 <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include "rpc.h"
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|