VirtualBox

source: vbox/trunk/src/apps/adpctl/VBoxNetAdpCtl.cpp@ 22000

Last change on this file since 22000 was 18864, checked in by vboxsync, 15 years ago

Proper build fix for Solaris (take three).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: VBoxNetAdpCtl.cpp 18864 2009-04-10 12:03:59Z 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 <assert.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <sys/wait.h>
33#include <sys/ioctl.h>
34#include <fcntl.h>
35#ifdef RT_OS_SOLARIS
36# include <sys/ioccom.h>
37#endif
38
39/* @todo Error codes must be moved to some header file */
40#define ADPCTLERR_NO_CTL_DEV 3
41#define ADPCTLERR_IOCTL_FAILED 4
42
43/* @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
44#define VBOXNETADP_CTL_DEV_NAME "/dev/vboxnetctl"
45#define VBOXNETADP_NAME "vboxnet"
46#define VBOXNETADP_MAX_NAME_LEN 32
47#define VBOXNETADP_CTL_ADD _IOR('v', 1, VBOXNETADPREQ)
48#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
49typedef struct VBoxNetAdpReq
50{
51 char szName[VBOXNETADP_MAX_NAME_LEN];
52} VBOXNETADPREQ;
53typedef VBOXNETADPREQ *PVBOXNETADPREQ;
54
55
56#define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
57
58#ifdef RT_OS_LINUX
59#define VBOXADPCTL_DEL_CMD "del"
60#else
61#define VBOXADPCTL_DEL_CMD "delete"
62#endif
63
64static void showUsage(void)
65{
66 fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
67 fprintf(stderr, " | VBoxNetAdpCtl add\n");
68 fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
69}
70
71static int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
72 const char *pcszArg2 = NULL,
73 const char *pcszArg3 = NULL,
74 const char *pcszArg4 = NULL,
75 const char *pcszArg5 = NULL)
76{
77 const char * const argv[] =
78 {
79 VBOXADPCTL_IFCONFIG_PATH,
80 pcszAdapterName,
81 pcszArg1, /* [address family] */
82 pcszArg2, /* address */
83 pcszArg3, /* ['netmask'] */
84 pcszArg4, /* [network mask] */
85 pcszArg5, /* [network mask] */
86 NULL /* terminator */
87 };
88 int rc = EXIT_SUCCESS;
89 pid_t childPid = fork();
90 switch (childPid)
91 {
92 case -1: /* Something went wrong. */
93 perror("fork() failed");
94 rc = EXIT_FAILURE;
95 break;
96 case 0: /* Child process. */
97 if (execv(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv) == -1)
98 rc = EXIT_FAILURE;
99 break;
100 default: /* Parent process. */
101 waitpid(childPid, &rc, 0);
102 break;
103 }
104
105 return rc;
106}
107
108#define MAX_ADDRESSES 128
109#define MAX_ADDRLEN 64
110
111static bool removeAddresses(const char *pszAdapterName)
112{
113 char szCmd[1024], szBuf[1024];
114 char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
115
116 memset(aszAddresses, 0, sizeof(aszAddresses));
117 snprintf(szCmd, sizeof(szCmd), VBOXADPCTL_IFCONFIG_PATH " %s", pszAdapterName);
118 FILE *fp = popen(szCmd, "r");
119
120 if (!fp)
121 return false;
122
123 int cAddrs;
124 for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
125 {
126 int cbSkipWS = strspn(szBuf, " \t");
127#if 0 /* Don't use this! assert() breaks the mac build. Use IPRT or be a rectangular building thing. */
128 assert(cbSkipWS < 20);
129#endif
130 char *pszWord = strtok(szBuf + cbSkipWS, " ");
131 /* We are concerned with IPv6 address lines only. */
132 if (!pszWord || strcmp(pszWord, "inet6"))
133 continue;
134#ifdef RT_OS_LINUX
135 pszWord = strtok(NULL, " ");
136 /* Skip "addr:". */
137 if (!pszWord || strcmp(pszWord, "addr:"))
138 continue;
139#endif
140 pszWord = strtok(NULL, " ");
141 /* Skip link-local addresses. */
142 if (!pszWord || !strncmp(pszWord, "fe80", 4))
143 continue;
144 strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
145 }
146 pclose(fp);
147
148 for (int i = 0; i < cAddrs; i++)
149 {
150 if (executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
151 return false;
152 }
153
154 return true;
155}
156
157int doIOCtl(unsigned long uCmd, void *pData)
158{
159 int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
160 if (fd == -1)
161 {
162 perror("VBoxNetAdpCtl: failed to open " VBOXNETADP_CTL_DEV_NAME);
163 return ADPCTLERR_NO_CTL_DEV;
164 }
165
166 int rc = ioctl(fd, uCmd, pData);
167 if (rc == -1)
168 {
169 perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
170 rc = ADPCTLERR_IOCTL_FAILED;
171 }
172
173 close(fd);
174
175 return rc;
176}
177
178int main(int argc, char *argv[])
179
180{
181 const char *pszAdapterName;
182 const char *pszAddress;
183 const char *pszNetworkMask = NULL;
184 const char *pszOption = NULL;
185 int rc = EXIT_SUCCESS;
186 bool fRemove = false;
187 VBOXNETADPREQ Req;
188
189 switch (argc)
190 {
191 case 5:
192 if (strcmp("netmask", argv[3]))
193 {
194 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
195 showUsage();
196 return 1;
197 }
198 pszOption = "netmask";
199 pszNetworkMask = argv[4];
200 pszAdapterName = argv[1];
201 pszAddress = argv[2];
202 break;
203 case 4:
204 if (strcmp("remove", argv[3]))
205 {
206 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
207 showUsage();
208 return 1;
209 }
210 fRemove = true;
211 pszAdapterName = argv[1];
212 pszAddress = argv[2];
213 break;
214 case 3:
215 pszAdapterName = argv[1];
216 pszAddress = argv[2];
217 if (strcmp("remove", pszAddress) == 0)
218 {
219 strncpy(Req.szName, pszAdapterName, sizeof(Req.szName));
220 return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
221 }
222 break;
223 case 2:
224 if (strcmp("add", argv[1]) == 0)
225 {
226 rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
227 if (rc == 0)
228 puts(Req.szName);
229 return rc;
230 }
231 /* Fall through */
232 default:
233 fprintf(stderr, "Invalid number of arguments.\n\n");
234 /* Fall through */
235 case 1:
236 showUsage();
237 return 1;
238 }
239
240 if (strncmp("vboxnet", pszAdapterName, 7))
241 {
242 fprintf(stderr, "Setting configuration for %s is not supported.\n", pszAdapterName);
243 return 2;
244 }
245
246 if (fRemove)
247 {
248 if (strchr(pszAddress, ':'))
249 rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, pszAddress);
250 else
251 {
252#ifdef RT_OS_LINUX
253 rc = executeIfconfig(pszAdapterName, "0.0.0.0");
254#else
255 rc = executeIfconfig(pszAdapterName, "delete", pszAddress);
256#endif
257 }
258 }
259 else
260 {
261 /* We are setting/replacing address. */
262 if (strchr(pszAddress, ':'))
263 {
264 /*
265 * Before we set IPv6 address we'd like to remove
266 * all previously assigned addresses except the
267 * self-assigned one.
268 */
269 if (!removeAddresses(pszAdapterName))
270 rc = EXIT_FAILURE;
271 else
272 rc = executeIfconfig(pszAdapterName, "inet6", "add", pszAddress, pszOption, pszNetworkMask);
273 }
274 else
275 rc = executeIfconfig(pszAdapterName, pszAddress, pszOption, pszNetworkMask);
276 }
277 return rc;
278}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use