1 | /* $Id: VBoxNetAdpCtl.cpp 17424 2009-03-05 21:04:23Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <string.h>
|
---|
30 | #include <unistd.h>
|
---|
31 | #include <sys/wait.h>
|
---|
32 |
|
---|
33 | #define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
|
---|
34 |
|
---|
35 | static void showUsage(void)
|
---|
36 | {
|
---|
37 | fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> [netmask <address>]\n");
|
---|
38 | }
|
---|
39 |
|
---|
40 | static int doExec(char *pszAdapterName, char *pszAddress, char *pszNetworkMask)
|
---|
41 | {
|
---|
42 | char *argv[] =
|
---|
43 | {
|
---|
44 | VBOXADPCTL_IFCONFIG_PATH,
|
---|
45 | pszAdapterName,
|
---|
46 | NULL, /* [address family] */
|
---|
47 | NULL, /* address */
|
---|
48 | NULL, /* ['netmask'] */
|
---|
49 | NULL, /* [network mask] */
|
---|
50 | NULL /* terminator */
|
---|
51 | };
|
---|
52 | int i = 2; /* index in argv. we start from address family */
|
---|
53 |
|
---|
54 | if (strchr(pszAddress, ':'))
|
---|
55 | argv[i++] = "inet6";
|
---|
56 | argv[i++] = pszAddress;
|
---|
57 | if (pszNetworkMask)
|
---|
58 | {
|
---|
59 | argv[i++] = "netmask";
|
---|
60 | argv[i++] = pszNetworkMask;
|
---|
61 | }
|
---|
62 |
|
---|
63 | return (execv(VBOXADPCTL_IFCONFIG_PATH, argv) == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
|
---|
64 | }
|
---|
65 |
|
---|
66 | static int executeIfconfig(char *pszAdapterName, char *pszAddress, char *pszNetworkMask)
|
---|
67 | {
|
---|
68 | int rc = EXIT_SUCCESS;
|
---|
69 | pid_t childPid = fork();
|
---|
70 | switch (childPid)
|
---|
71 | {
|
---|
72 | case -1: /* Something went wrong. */
|
---|
73 | perror("fork() failed");
|
---|
74 | rc = EXIT_FAILURE;
|
---|
75 | break;
|
---|
76 | case 0: /* Child process. */
|
---|
77 | rc = doExec(pszAdapterName, pszAddress, pszNetworkMask);
|
---|
78 | break;
|
---|
79 | default: /* Parent process. */
|
---|
80 | waitpid(childPid, &rc, 0);
|
---|
81 | break;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int main(int argc, char *argv[])
|
---|
88 |
|
---|
89 | {
|
---|
90 | char *pszAdapterName;
|
---|
91 | char *pszAddress;
|
---|
92 | char *pszNetworkMask = NULL;
|
---|
93 |
|
---|
94 | switch (argc)
|
---|
95 | {
|
---|
96 | case 5:
|
---|
97 | if (strcmp("netmask", argv[3]))
|
---|
98 | {
|
---|
99 | fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
|
---|
100 | showUsage();
|
---|
101 | return 1;
|
---|
102 | }
|
---|
103 | pszNetworkMask = argv[4];
|
---|
104 | /* Fall through */
|
---|
105 | case 3:
|
---|
106 | pszAdapterName = argv[1];
|
---|
107 | pszAddress = argv[2];
|
---|
108 | break;
|
---|
109 | default:
|
---|
110 | fprintf(stderr, "Invalid number of arguments.\n\n");
|
---|
111 | /* Fall through */
|
---|
112 | case 1:
|
---|
113 | showUsage();
|
---|
114 | return 1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (strcmp("vboxnet0", pszAdapterName))
|
---|
118 | {
|
---|
119 | fprintf(stderr, "Setting configuration for %s is not supported.\n", pszAdapterName);
|
---|
120 | return 2;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return executeIfconfig(pszAdapterName, pszAddress, pszNetworkMask);
|
---|
124 | }
|
---|