VirtualBox

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

Last change on this file since 33313 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Id: VBoxNetAdpCtl.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * Apps - VBoxAdpCtl, Configuration tool for vboxnetX adapters.
4 */
5
6/*
7 * Copyright (C) 2009 Oracle Corporation
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
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/wait.h>
28#include <sys/ioctl.h>
29#include <fcntl.h>
30#ifdef RT_OS_SOLARIS
31# include <sys/ioccom.h>
32#endif
33
34/** @todo Error codes must be moved to some header file */
35#define ADPCTLERR_BAD_NAME 2
36#define ADPCTLERR_NO_CTL_DEV 3
37#define ADPCTLERR_IOCTL_FAILED 4
38
39/** @todo These are duplicates from src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdpInternal.h */
40#define VBOXNETADP_CTL_DEV_NAME "/dev/vboxnetctl"
41#define VBOXNETADP_NAME "vboxnet"
42#define VBOXNETADP_MAX_NAME_LEN 32
43#define VBOXNETADP_CTL_ADD _IOR('v', 1, VBOXNETADPREQ)
44#define VBOXNETADP_CTL_REMOVE _IOW('v', 2, VBOXNETADPREQ)
45typedef struct VBoxNetAdpReq
46{
47 char szName[VBOXNETADP_MAX_NAME_LEN];
48} VBOXNETADPREQ;
49typedef VBOXNETADPREQ *PVBOXNETADPREQ;
50
51
52#define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
53
54#if defined(RT_OS_LINUX)
55# define VBOXADPCTL_DEL_CMD "del"
56# define VBOXADPCTL_ADD_CMD "add"
57#elif defined(RT_OS_SOLARIS)
58# define VBOXADPCTL_DEL_CMD "removeif"
59# define VBOXADPCTL_ADD_CMD "addif"
60#else
61# define VBOXADPCTL_DEL_CMD "delete"
62# define VBOXADPCTL_ADD_CMD "add"
63#endif
64
65static void showUsage(void)
66{
67 fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
68 fprintf(stderr, " | VBoxNetAdpCtl add\n");
69 fprintf(stderr, " | VBoxNetAdpCtl <adapter> remove\n");
70}
71
72static int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
73 const char *pcszArg2 = NULL,
74 const char *pcszArg3 = NULL,
75 const char *pcszArg4 = NULL,
76 const char *pcszArg5 = NULL)
77{
78 const char * const argv[] =
79 {
80 VBOXADPCTL_IFCONFIG_PATH,
81 pcszAdapterName,
82 pcszArg1, /* [address family] */
83 pcszArg2, /* address */
84 pcszArg3, /* ['netmask'] */
85 pcszArg4, /* [network mask] */
86 pcszArg5, /* [network mask] */
87 NULL /* terminator */
88 };
89 char * const envp[] = { (char*)"LC_ALL=C", NULL };
90 int rc = EXIT_SUCCESS;
91 pid_t childPid = fork();
92 switch (childPid)
93 {
94 case -1: /* Something went wrong. */
95 perror("fork() failed");
96 rc = EXIT_FAILURE;
97 break;
98 case 0: /* Child process. */
99 if (execve(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv, envp) == -1)
100 rc = EXIT_FAILURE;
101 break;
102 default: /* Parent process. */
103 waitpid(childPid, &rc, 0);
104 break;
105 }
106
107 return rc;
108}
109
110#define MAX_ADDRESSES 128
111#define MAX_ADDRLEN 64
112
113static bool removeAddresses(char *pszAdapterName)
114{
115 char szBuf[1024];
116 char aszAddresses[MAX_ADDRESSES][MAX_ADDRLEN];
117 int rc;
118 int fds[2];
119 char * const argv[] = { (char*)VBOXADPCTL_IFCONFIG_PATH, pszAdapterName, NULL };
120 char * const envp[] = { (char*)"LC_ALL=C", NULL };
121
122 memset(aszAddresses, 0, sizeof(aszAddresses));
123
124 rc = pipe(fds);
125 if (rc < 0)
126 return false;
127
128 pid_t pid = fork();
129 if (pid < 0)
130 return false;
131
132 if (pid == 0)
133 {
134 /* child */
135 close(fds[0]);
136 close(STDOUT_FILENO);
137 rc = dup2(fds[1], STDOUT_FILENO);
138 if (rc >= 0)
139 execve(VBOXADPCTL_IFCONFIG_PATH, argv, envp);
140 return false;
141 }
142
143 /* parent */
144 close(fds[1]);
145 FILE *fp = fdopen(fds[0], "r");
146 if (!fp)
147 return false;
148
149 int cAddrs;
150 for (cAddrs = 0; cAddrs < MAX_ADDRESSES && fgets(szBuf, sizeof(szBuf), fp);)
151 {
152 int cbSkipWS = strspn(szBuf, " \t");
153 char *pszWord = strtok(szBuf + cbSkipWS, " ");
154 /* We are concerned with IPv6 address lines only. */
155 if (!pszWord || strcmp(pszWord, "inet6"))
156 continue;
157#ifdef RT_OS_LINUX
158 pszWord = strtok(NULL, " ");
159 /* Skip "addr:". */
160 if (!pszWord || strcmp(pszWord, "addr:"))
161 continue;
162#endif
163 pszWord = strtok(NULL, " ");
164 /* Skip link-local addresses. */
165 if (!pszWord || !strncmp(pszWord, "fe80", 4))
166 continue;
167 strncpy(aszAddresses[cAddrs++], pszWord, MAX_ADDRLEN-1);
168 }
169 fclose(fp);
170
171 for (int i = 0; i < cAddrs; i++)
172 {
173 if (executeIfconfig(pszAdapterName, "inet6",
174 VBOXADPCTL_DEL_CMD, aszAddresses[i]) != EXIT_SUCCESS)
175 return false;
176 }
177
178 return true;
179}
180
181int doIOCtl(unsigned long uCmd, void *pData)
182{
183 int fd = open(VBOXNETADP_CTL_DEV_NAME, O_RDWR);
184 if (fd == -1)
185 {
186 perror("VBoxNetAdpCtl: failed to open " VBOXNETADP_CTL_DEV_NAME);
187 return ADPCTLERR_NO_CTL_DEV;
188 }
189
190 int rc = ioctl(fd, uCmd, pData);
191 if (rc == -1)
192 {
193 perror("VBoxNetAdpCtl: ioctl failed for " VBOXNETADP_CTL_DEV_NAME);
194 rc = ADPCTLERR_IOCTL_FAILED;
195 }
196
197 close(fd);
198
199 return rc;
200}
201
202int checkAdapterName(const char *pcszNameIn, char *pszNameOut)
203{
204 int iAdapterIndex = -1;
205
206 if ( strlen(pcszNameIn) >= VBOXNETADP_MAX_NAME_LEN
207 || sscanf(pcszNameIn, "vboxnet%d", &iAdapterIndex) != 1
208 || iAdapterIndex < 0 || iAdapterIndex > 99 )
209 {
210 fprintf(stderr, "Setting configuration for %s is not supported.\n", pcszNameIn);
211 return ADPCTLERR_BAD_NAME;
212 }
213 sprintf(pszNameOut, "vboxnet%d", iAdapterIndex);
214 if (strcmp(pszNameOut, pcszNameIn))
215 {
216 fprintf(stderr, "Invalid adapter name %s.\n", pcszNameIn);
217 return ADPCTLERR_BAD_NAME;
218 }
219
220 return 0;
221}
222
223int main(int argc, char *argv[])
224
225{
226 char szAdapterName[VBOXNETADP_MAX_NAME_LEN];
227 char *pszAdapterName = NULL;
228 const char *pszAddress = NULL;
229 const char *pszNetworkMask = NULL;
230 const char *pszOption = NULL;
231 int rc = EXIT_SUCCESS;
232 bool fRemove = false;
233 VBOXNETADPREQ Req;
234
235 switch (argc)
236 {
237 case 5:
238 {
239 /* Add a netmask to existing interface */
240 if (strcmp("netmask", argv[3]))
241 {
242 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
243 showUsage();
244 return 1;
245 }
246 pszOption = "netmask";
247 pszNetworkMask = argv[4];
248 pszAdapterName = argv[1];
249 pszAddress = argv[2];
250 break;
251 }
252
253 case 4:
254 {
255 /* Remove a single address from existing interface */
256 if (strcmp("remove", argv[3]))
257 {
258 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
259 showUsage();
260 return 1;
261 }
262 fRemove = true;
263 pszAdapterName = argv[1];
264 pszAddress = argv[2];
265 break;
266 }
267
268 case 3:
269 {
270 /* Remove an existing interface */
271 pszAdapterName = argv[1];
272 pszAddress = argv[2];
273 if (strcmp("remove", pszAddress) == 0)
274 {
275 rc = checkAdapterName(pszAdapterName, szAdapterName);
276 if (rc)
277 return rc;
278#ifdef RT_OS_SOLARIS
279 return 1;
280#else
281 memset(&Req, '\0', sizeof(Req));
282 snprintf(Req.szName, sizeof(Req.szName), "%s", szAdapterName);
283 return doIOCtl(VBOXNETADP_CTL_REMOVE, &Req);
284#endif
285 }
286 break;
287 }
288
289 case 2:
290 {
291 /* Create a new interface */
292 if (strcmp("add", argv[1]) == 0)
293 {
294#ifdef RT_OS_SOLARIS
295 return 1;
296#else
297 memset(&Req, '\0', sizeof(Req));
298 rc = doIOCtl(VBOXNETADP_CTL_ADD, &Req);
299 if (rc == 0)
300 puts(Req.szName);
301#endif
302 return rc;
303 }
304 /* Fall through */
305 }
306
307 default:
308 fprintf(stderr, "Invalid number of arguments.\n\n");
309 /* Fall through */
310 case 1:
311 showUsage();
312 return 1;
313 }
314
315 rc = checkAdapterName(pszAdapterName, szAdapterName);
316 if (rc)
317 return rc;
318
319 pszAdapterName = szAdapterName;
320
321 if (fRemove)
322 {
323 if (strchr(pszAddress, ':'))
324 rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_DEL_CMD, pszAddress);
325 else
326 {
327#if defined(RT_OS_LINUX)
328 rc = executeIfconfig(pszAdapterName, "0.0.0.0");
329#else
330 rc = executeIfconfig(pszAdapterName, VBOXADPCTL_DEL_CMD, pszAddress);
331#endif
332
333#ifdef RT_OS_SOLARIS
334 /* On Solaris we can unplumb the ipv4 interface */
335 executeIfconfig(pszAdapterName, "inet", "unplumb");
336#endif
337 }
338 }
339 else
340 {
341 /* We are setting/replacing address. */
342 if (strchr(pszAddress, ':'))
343 {
344#ifdef RT_OS_SOLARIS
345 /* On Solaris we need to plumb the interface first if it's not already plumbed. */
346 if (executeIfconfig(pszAdapterName, "inet6") != 0)
347 executeIfconfig(pszAdapterName, "inet6", "plumb", "up");
348#endif
349 /*
350 * Before we set IPv6 address we'd like to remove
351 * all previously assigned addresses except the
352 * self-assigned one.
353 */
354 if (!removeAddresses(pszAdapterName))
355 rc = EXIT_FAILURE;
356 else
357 rc = executeIfconfig(pszAdapterName, "inet6", VBOXADPCTL_ADD_CMD, pszAddress, pszOption, pszNetworkMask);
358 }
359 else
360 {
361#ifdef RT_OS_SOLARIS
362 /* On Solaris we need to plumb the interface first if it's not already plumbed. */
363 if (executeIfconfig(pszAdapterName, "inet") != 0)
364 executeIfconfig(pszAdapterName, "plumb", "up");
365#endif
366 rc = executeIfconfig(pszAdapterName, pszAddress, pszOption, pszNetworkMask);
367 }
368 }
369 return rc;
370}
371
Note: See TracBrowser for help on using the repository browser.

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