VBoxNetAdpCtl.cpp revision 47c2a6d84685d16b7ef87c307331e5588d892ef0
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* $Id$ */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @file
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * additional information or have any questions.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync*******************************************************************************/
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync#include <stdio.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <stdlib.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <string.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <unistd.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <sys/wait.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <sys/ioctl.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <fcntl.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#ifdef RT_OS_SOLARIS
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync# include <sys/ioccom.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#endif
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** @todo Error codes must be moved to some header file */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync#define ADPCTLERR_BAD_NAME 2
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define ADPCTLERR_NO_CTL_DEV 3
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define ADPCTLERR_IOCTL_FAILED 4
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync/** @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#define VBOXNETADP_CTL_DEV_NAME "/dev/vboxnetctl"
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#define VBOXNETADP_NAME "vboxnet"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXNETADP_MAX_NAME_LEN 32
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXNETADP_CTL_ADD _IOR('v', 1, VBOXNETADPREQ)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsynctypedef struct VBoxNetAdpReq
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync char szName[VBOXNETADP_MAX_NAME_LEN];
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync} VBOXNETADPREQ;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsynctypedef VBOXNETADPREQ *PVBOXNETADPREQ;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#ifdef RT_OS_LINUX
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXADPCTL_DEL_CMD "del"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#else
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXADPCTL_DEL_CMD "delete"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#endif
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void showUsage(void)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, " | VBoxNetAdpCtl add\n");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync const char *pcszArg2 = NULL,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync const char *pcszArg3 = NULL,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync const char *pcszArg4 = NULL,
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync const char *pcszArg5 = NULL)
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync{
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync const char * const argv[] =
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync VBOXADPCTL_IFCONFIG_PATH,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pcszAdapterName,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pcszArg1, /* [address family] */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pcszArg2, /* address */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pcszArg3, /* ['netmask'] */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pcszArg4, /* [network mask] */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pcszArg5, /* [network mask] */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync NULL /* terminator */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync };
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = EXIT_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pid_t childPid = fork();
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync switch (childPid)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync case -1: /* Something went wrong. */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync perror("fork() failed");
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = EXIT_FAILURE;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync break;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync case 0: /* Child process. */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /** @todo use execve like below? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (execv(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv) == -1)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = EXIT_FAILURE;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync default: /* Parent process. */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync waitpid(childPid, &rc, 0);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync break;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync }
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define MAX_ADDRESSES 128
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#define MAX_ADDRLEN 64
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic bool removeAddresses(char *pszAdapterName)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync char szBuf[1024];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int fds[2];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync char * const argv[] = { VBOXADPCTL_IFCONFIG_PATH, pszAdapterName, NULL };
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync char * const envp[] = { (char*)"LC_ALL=C", NULL };
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync memset(aszAddresses, 0, sizeof(aszAddresses));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = pipe(fds);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (rc < 0)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pid_t pid = fork();
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (pid < 0)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync if (pid == 0)
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync {
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync /* child */
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync close(fds[0]);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync close(STDOUT_FILENO);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = dup2(fds[1], STDOUT_FILENO);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (rc >= 0)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync execve(VBOXADPCTL_IFCONFIG_PATH, argv, envp);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* parent */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync close(fds[1]);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync FILE *fp = fdopen(fds[0], "r");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (!fp)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int cAddrs;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int cbSkipWS = strspn(szBuf, " \t");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync char *pszWord = strtok(szBuf + cbSkipWS, " ");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* We are concerned with IPv6 address lines only. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (!pszWord || strcmp(pszWord, "inet6"))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync continue;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#ifdef RT_OS_LINUX
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszWord = strtok(NULL, " ");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Skip "addr:". */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (!pszWord || strcmp(pszWord, "addr:"))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync continue;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#endif
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszWord = strtok(NULL, " ");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Skip link-local addresses. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (!pszWord || !strncmp(pszWord, "fe80", 4))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync continue;
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync fclose(fp);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync for (int i = 0; i < cAddrs; i++)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (executeIfconfig(pszAdapterName, "inet6",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return true;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsyncint doIOCtl(unsigned long uCmd, void *pData)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (fd == -1)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync perror("VBoxNetAdpCtl: failed to open " VBOXNETADP_CTL_DEV_NAME);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return ADPCTLERR_NO_CTL_DEV;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = ioctl(fd, uCmd, pData);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (rc == -1)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = ADPCTLERR_IOCTL_FAILED;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
f687f34bd232be13744edbc0cc5155fa5d4540edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync close(fd);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsyncint checkAdapterName(const char *pcszNameIn, char *pszNameOut)
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int iAdapterIndex = -1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if ( strlen(pcszNameIn) >= VBOXNETADP_MAX_NAME_LEN
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync || sscanf(pcszNameIn, "vboxnet%d", &iAdapterIndex) != 1
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync || iAdapterIndex < 0 || iAdapterIndex > 99 )
ead016c68c61b5f2e1fe4d237054eebea9327d4bvboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, "Setting configuration for %s is not supported.\n", pcszNameIn);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync return ADPCTLERR_BAD_NAME;
f687f34bd232be13744edbc0cc5155fa5d4540edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync sprintf(pszNameOut, "vboxnet%d", iAdapterIndex);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (strcmp(pszNameOut, pcszNameIn))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, "Invalid adapter name %s.\n", pcszNameIn);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return ADPCTLERR_BAD_NAME;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncint main(int argc, char *argv[])
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync char szAdapterName[VBOXNETADP_MAX_NAME_LEN];
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync char *pszAdapterName;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync const char *pszAddress;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync const char *pszNetworkMask = NULL;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync const char *pszOption = NULL;
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync int rc = EXIT_SUCCESS;
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync bool fRemove = false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBOXNETADPREQ Req;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync switch (argc)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case 5:
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (strcmp("netmask", argv[3]))
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync showUsage();
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync return 1;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync }
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync pszOption = "netmask";
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszNetworkMask = argv[4];
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pszAdapterName = argv[1];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszAddress = argv[2];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync case 4:
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync if (strcmp("remove", argv[3]))
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync {
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync showUsage();
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync return 1;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync fRemove = true;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszAdapterName = argv[1];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszAddress = argv[2];
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case 3:
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pszAdapterName = argv[1];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pszAddress = argv[2];
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (strcmp("remove", pszAddress) == 0)
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = checkAdapterName(pszAdapterName, szAdapterName);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (rc)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync memset(&Req, '\0', sizeof(Req));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync snprintf(Req.szName, sizeof(Req.szName), "%s", szAdapterName);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync break;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case 2:
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (strcmp("add", argv[1]) == 0)
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync {
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync memset(&Req, '\0', sizeof(Req));
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync if (rc == 0)
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync puts(Req.szName);
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync return rc;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync }
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync /* Fall through */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync default:
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync fprintf(stderr, "Invalid number of arguments.\n\n");
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync /* Fall through */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync case 1:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync showUsage();
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = checkAdapterName(pszAdapterName, szAdapterName);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (rc)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync return rc;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync pszAdapterName = szAdapterName;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (fRemove)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (strchr(pszAddress, ':'))
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, pszAddress);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync#ifdef RT_OS_LINUX
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = executeIfconfig(pszAdapterName, "0.0.0.0");
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync#else
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = executeIfconfig(pszAdapterName, "delete", pszAddress);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync#endif
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync }
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync }
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync /* We are setting/replacing address. */
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (strchr(pszAddress, ':'))
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync /*
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * Before we set IPv6 address we'd like to remove
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * all previously assigned addresses except the
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync * self-assigned one.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync */
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (!removeAddresses(pszAdapterName))
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = EXIT_FAILURE;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = executeIfconfig(pszAdapterName, "inet6", "add", pszAddress, pszOption, pszNetworkMask);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = executeIfconfig(pszAdapterName, pszAddress, pszOption, pszNetworkMask);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync