VBoxNetAdpCtl.cpp revision 47c2a6d84685d16b7ef87c307331e5588d892ef0
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
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 * 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* Header Files *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync*******************************************************************************/
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** @todo Error codes must be moved to some header file */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync/** @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXNETADP_CTL_ADD _IOR('v', 1, VBOXNETADPREQ)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic void showUsage(void)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync const char * const argv[] =
0c4004948fca34f2db87e7b38013137e9472c306vboxsync case 0: /* Child process. */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /** @todo use execve like below? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (execv(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv) == -1)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync default: /* Parent process. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync char * const argv[] = { VBOXADPCTL_IFCONFIG_PATH, pszAdapterName, NULL };
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync /* child */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* parent */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* We are concerned with IPv6 address lines only. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Skip "addr:". */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Skip link-local addresses. */
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync for (int i = 0; i < cAddrs; i++)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return false;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return true;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync perror("VBoxNetAdpCtl: failed to open " VBOXNETADP_CTL_DEV_NAME);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsyncint checkAdapterName(const char *pcszNameIn, char *pszNameOut)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync || sscanf(pcszNameIn, "vboxnet%d", &iAdapterIndex) != 1
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, "Setting configuration for %s is not supported.\n", pcszNameIn);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fprintf(stderr, "Invalid adapter name %s.\n", pcszNameIn);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync bool fRemove = false;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = checkAdapterName(pszAdapterName, szAdapterName);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync snprintf(Req.szName, sizeof(Req.szName), "%s", szAdapterName);
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync /* Fall through */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync fprintf(stderr, "Invalid number of arguments.\n\n");
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync /* Fall through */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = checkAdapterName(pszAdapterName, szAdapterName);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, pszAddress);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = executeIfconfig(pszAdapterName, "delete", pszAddress);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync /* We are setting/replacing address. */
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * Before we set IPv6 address we'd like to remove
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * all previously assigned addresses except the
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync * self-assigned one.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = executeIfconfig(pszAdapterName, "inet6", "add", pszAddress, pszOption, pszNetworkMask);