VirtualBox

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

Last change on this file since 17778 was 17778, checked in by vboxsync, 16 years ago

warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1/* $Id: VBoxNetAdpCtl.cpp 17778 2009-03-12 20:44:11Z 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 <string>
28#include <list>
29#include <assert.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34#include <sys/wait.h>
35
36#define VBOXADPCTL_IFCONFIG_PATH "/sbin/ifconfig"
37
38static void showUsage(void)
39{
40 fprintf(stderr, "Usage: VBoxNetAdpCtl <adapter> <address> ([netmask <address>] | remove)\n");
41}
42
43static int executeIfconfig(const char *pcszAdapterName, const char *pcszArg1,
44 const char *pcszArg2 = NULL,
45 const char *pcszArg3 = NULL,
46 const char *pcszArg4 = NULL)
47{
48 const char * const argv[] =
49 {
50 VBOXADPCTL_IFCONFIG_PATH,
51 pcszAdapterName,
52 pcszArg1, /* [address family] */
53 pcszArg2, /* address */
54 pcszArg3, /* ['netmask'] */
55 pcszArg4, /* [network mask] */
56 NULL /* terminator */
57 };
58 int rc = EXIT_SUCCESS;
59 pid_t childPid = fork();
60 switch (childPid)
61 {
62 case -1: /* Something went wrong. */
63 perror("fork() failed");
64 rc = EXIT_FAILURE;
65 break;
66 case 0: /* Child process. */
67 if (execv(VBOXADPCTL_IFCONFIG_PATH, (char * const*)argv) == -1)
68 rc = EXIT_FAILURE;
69 break;
70 default: /* Parent process. */
71 waitpid(childPid, &rc, 0);
72 break;
73 }
74
75 return rc;
76}
77
78static bool removeAddresses(const char *pszAdapterName)
79{
80 static char szCmd[1024], szBuf[1024];
81
82 snprintf(szCmd, sizeof(szCmd), VBOXADPCTL_IFCONFIG_PATH " %s", pszAdapterName);
83 FILE *fp = popen(szCmd, "r");
84
85 if (!fp)
86 return false;
87
88 std::list<std::string> Addresses;
89
90 while (fgets(szBuf, sizeof(szBuf), fp))
91 {
92 int cbSkipWS = strspn(szBuf, " \t");
93 assert(cbSkipWS < 20);
94 char *pszWord = strtok(szBuf + cbSkipWS, " ");
95 /* We are concerned with IPv6 address lines only. */
96 if (!pszWord || strcmp(pszWord, "inet6"))
97 continue;
98 pszWord = strtok(NULL, " ");
99 /* Skip link-local addresses. */
100 if (!pszWord || !strncmp(pszWord, "fe80", 4))
101 continue;
102 Addresses.push_back(std::string(pszWord));
103 }
104 pclose(fp);
105
106 std::list<std::string>::const_iterator it;
107 for (it = Addresses.begin(); it != Addresses.end(); it++)
108 {
109 if (executeIfconfig(pszAdapterName, "inet6", it->c_str(), "remove") != EXIT_SUCCESS)
110 return false;
111 }
112
113 return true;
114}
115
116
117int main(int argc, char *argv[])
118
119{
120 const char *pszAdapterName;
121 const char *pszAddress;
122 const char *pszNetworkMask = NULL;
123 const char *pszOption = NULL;
124 int rc = EXIT_SUCCESS;
125
126 switch (argc)
127 {
128 case 5:
129 if (strcmp("netmask", argv[3]))
130 {
131 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
132 showUsage();
133 return 1;
134 }
135 pszOption = "netmask";
136 pszNetworkMask = argv[4];
137 pszAdapterName = argv[1];
138 pszAddress = argv[2];
139 break;
140 case 4:
141 if (strcmp("remove", argv[3]))
142 {
143 fprintf(stderr, "Invalid argument: %s\n\n", argv[3]);
144 showUsage();
145 return 1;
146 }
147 pszOption = "remove";
148 pszAdapterName = argv[1];
149 pszAddress = argv[2];
150 break;
151 case 3:
152 pszAdapterName = argv[1];
153 pszAddress = argv[2];
154 break;
155 default:
156 fprintf(stderr, "Invalid number of arguments.\n\n");
157 /* Fall through */
158 case 1:
159 showUsage();
160 return 1;
161 }
162
163 if (strcmp("vboxnet0", pszAdapterName))
164 {
165 fprintf(stderr, "Setting configuration for %s is not supported.\n", pszAdapterName);
166 return 2;
167 }
168
169 if (strchr(pszAddress, ':'))
170 {
171 /*
172 * Before we set IPv6 address we'd like to remove
173 * all previously assigned addresses except the
174 * self-assigned one.
175 */
176 if (pszOption && !strcmp(pszOption, "remove"))
177 rc = executeIfconfig(pszAdapterName, "inet6", pszAddress, pszOption);
178 else if (!removeAddresses(pszAdapterName))
179 rc = EXIT_FAILURE;
180 else
181 rc = executeIfconfig(pszAdapterName, "inet6", pszAddress, pszOption, pszNetworkMask);
182 }
183 else
184 rc = executeIfconfig(pszAdapterName, pszAddress, pszOption, pszNetworkMask);
185 return rc;
186}
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