VirtualBox

Changeset 18802 in vbox for trunk/src/apps


Ignore:
Timestamp:
Apr 7, 2009 11:06:41 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
45795
Message:

2957: Dynamic add/remove via /dev/vboxnetctl ioctls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/apps/adpctl/VBoxNetAdpCtl.cpp

    r17855 r18802  
    3131#include <unistd.h>
    3232#include <sys/wait.h>
     33#include <sys/ioctl.h>
     34#include <fcntl.h>
     35
     36/* @todo Error codes must be moved to some header file */
     37#define ADPCTLERR_NO_CTL_DEV 3
     38#define ADPCTLERR_IOCTL_FAILED 4
     39
     40/* @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
     41#define VBOXNETADP_CTL_DEV_NAME    "/dev/vboxnetctl"
     42#define VBOXNETADP_NAME            "vboxnet"
     43#define VBOXNETADP_MAX_NAME_LEN    32
     44#define VBOXNETADP_CTL_ADD    _IOR('v', 1, VBOXNETADPREQ)
     45#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
     46typedef struct VBoxNetAdpReq
     47{
     48    char szName[VBOXNETADP_MAX_NAME_LEN];
     49} VBOXNETADPREQ;
     50typedef VBOXNETADPREQ *PVBOXNETADPREQ;
     51
    3352
    3453#define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
     
    4362{
    4463    fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
     64    fprintf(stderr, "     | VBoxNetAdpCtl add\n");
     65    fprintf(stderr, "     | VBoxNetAdpCtl <adapter> remove\n");
    4566}
    4667
     
    131152}
    132153
     154int doIOCtl(unsigned long uCmd, void *pData)
     155{
     156    int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
     157    if (fd == -1)
     158    {
     159        perror("VBoxNetAdpCtl: failed to open " VBOXNETADP_CTL_DEV_NAME);
     160        return ADPCTLERR_NO_CTL_DEV;
     161    }
     162
     163    int rc = ioctl(fd, uCmd, pData);
     164    if (rc == -1)
     165    {
     166        perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
     167        rc = ADPCTLERR_IOCTL_FAILED;
     168    }
     169   
     170    close(fd);
     171 
     172    return rc;
     173}
    133174
    134175int main(int argc, char *argv[])
     
    141182    int rc = EXIT_SUCCESS;
    142183    bool fRemove = false;
     184    VBOXNETADPREQ Req;
    143185
    144186    switch (argc)
     
    170212            pszAdapterName = argv[1];
    171213            pszAddress = argv[2];
    172             break;
     214            if (strcmp("remove", pszAddress) == 0)
     215            {
     216                strncpy(Req.szName, pszAdapterName, sizeof(Req.szName));
     217                return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
     218            }
     219            break;
     220        case 2:
     221            if (strcmp("add", argv[1]) == 0)
     222            {
     223                rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
     224                if (rc == 0)
     225                    puts(Req.szName);
     226                return rc;
     227            }
     228            /* Fall through */
    173229        default:
    174230            fprintf(stderr, "Invalid number of arguments.\n\n");
     
    179235    }
    180236
    181     if (strcmp("vboxnet0", pszAdapterName))
     237    if (strncmp("vboxnet", pszAdapterName, 7))
    182238    {
    183239        fprintf(stderr, "Setting configuration for %s is not supported.\n", pszAdapterName);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette