Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit 5c15182

Browse files
committed
support --vmnet-gateway=IP, e.g., 192.168.105.1
`vde_vmnet` binary itself does not have any default value, but the launchd plist file is updated to use 192.168.105.1 as the default, so as to avoid conflicting with VMware Fusion. Fix #7 Signed-off-by: Akihiro Suda <[email protected]>
1 parent 7d42f83 commit 5c15182

File tree

7 files changed

+88
-13
lines changed

7 files changed

+88
-13
lines changed

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ The following files will be installed:
2222
- `/usr/local/bin/vde_vmnet`
2323
- `/Library/LaunchDaemons/io.github.virtualsquare.vde-2.vde_switch.plist`
2424
- `/Library/LaunchDaemons/io.github.AkihiroSuda.vde_vmnet.plist`
25+
- Configured to use `192.168.105.0/24`. Modifiy the file if it conflicts with your local network.
2526

2627
See ["Testing without launchd"](#testing-without-launchd) if you don't prefer to use launchd.
2728

28-
29-
:warning: Known to conflict with VMware Fusion. See issue [#7](https://github.com/AkihiroSuda/vde_vmnet/issues/7).
30-
3129
## Usage
3230

3331
```console
@@ -75,7 +73,7 @@ vde_switch --unix /tmp/vde.ctl
7573
```
7674

7775
```console
78-
sudo vde_vmnet /tmp/vde.ctl
76+
sudo vde_vmnet --vmnet-gateway=192.168.105.1 /tmp/vde.ctl
7977
```
8078

8179
Note: make sure to run `vde_vmnet` with root (`sudo`). See [FAQs](#FAQs) for the reason.
@@ -125,14 +123,14 @@ On the other hand, `vde_vmnet` does not require the entire QEMU process to run a
125123

126124
- Decide a unique MAC address for the VM, e.g. `de:ad:be:ef:00:01`.
127125

128-
- Decide a static IP address, e.g., "192.168.60.100"
126+
- Decide a static IP address, e.g., "192.168.105.100"
129127

130128
- Create `/etc/bootptab` like this. Make sure not to drop the "%%" header.
131129
```
132130
# bootptab
133131
%%
134132
# hostname hwtype hwaddr ipaddr bootfile
135-
tmp-vm01 1 de:ad:be:ef:00:01 192.168.60.100
133+
tmp-vm01 1 de:ad:be:ef:00:01 192.168.105.100
136134
```
137135

138136
- Reload the DHCP daemon.

cli.c

+31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
#include <arpa/inet.h>
56
#include <getopt.h>
67

78
#include <availability.h>
@@ -30,6 +31,9 @@ static void print_usage(const char *argv0) {
3031
"--vmnet-mode=(host|shared|bridged) vmnet mode (default: \"shared\")\n");
3132
printf("--vmnet-interface=INTERFACE interface used for "
3233
"--vmnet=bridged, e.g., \"en0\"\n");
34+
printf("--vmnet-gateway=IP gateway used for "
35+
"--vmnet=(host|shared), e.g., \"192.168.105.1\" (default: decided by "
36+
"macOS)\n");
3337
printf("-h, --help display this help and exit\n");
3438
printf("-v, --version display version information and "
3539
"exit\n");
@@ -42,6 +46,7 @@ static void print_version() { puts(VERSION); }
4246
#define CLI_OPTIONS_ID_VDE_GROUP -42
4347
#define CLI_OPTIONS_ID_VMNET_MODE -43
4448
#define CLI_OPTIONS_ID_VMNET_INTERFACE -44
49+
#define CLI_OPTIONS_ID_VMNET_GATEWAY -45
4550
struct cli_options *cli_options_parse(int argc, char *argv[]) {
4651
struct cli_options *res = malloc(sizeof(*res));
4752
if (res == NULL) {
@@ -54,6 +59,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
5459
{"vmnet-mode", required_argument, NULL, CLI_OPTIONS_ID_VMNET_MODE},
5560
{"vmnet-interface", required_argument, NULL,
5661
CLI_OPTIONS_ID_VMNET_INTERFACE},
62+
{"vmnet-gateway", required_argument, NULL, CLI_OPTIONS_ID_VMNET_GATEWAY},
5763
{"help", no_argument, NULL, 'h'},
5864
{"version", no_argument, NULL, 'v'},
5965
{0, 0, 0, 0},
@@ -79,6 +85,9 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
7985
case CLI_OPTIONS_ID_VMNET_INTERFACE:
8086
res->vmnet_interface = strdup(optarg);
8187
break;
88+
case CLI_OPTIONS_ID_VMNET_GATEWAY:
89+
res->vmnet_gateway = strdup(optarg);
90+
break;
8291
case 'h':
8392
print_usage(argv[0]);
8493
cli_options_destroy(res);
@@ -115,6 +124,26 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
115124
"vmnet mode \"bridged\" require --vmnet-interface to be specified\n");
116125
goto error;
117126
}
127+
if (res->vmnet_gateway == NULL) {
128+
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
129+
fprintf(stderr,
130+
"WARNING: --vmnet-gateway=IP should be explicitly specified to "
131+
"avoid conflicting with other applications\n");
132+
}
133+
} else {
134+
if (res->vmnet_mode == VMNET_BRIDGED_MODE) {
135+
fprintf(stderr,
136+
"vmnet mode \"bridged\" conflicts with --vmnet-gateway\n");
137+
goto error;
138+
}
139+
struct in_addr dummy;
140+
if (!inet_aton(res->vmnet_gateway, &dummy)) {
141+
fprintf(stderr,
142+
"invalid address \"%s\" was specified for --vmnet-gateway\n",
143+
res->vmnet_gateway);
144+
goto error;
145+
}
146+
}
118147
return res;
119148
error:
120149
print_usage(argv[0]);
@@ -132,5 +161,7 @@ void cli_options_destroy(struct cli_options *x) {
132161
free(x->vde_switch);
133162
if (x->vmnet_interface != NULL)
134163
free(x->vmnet_interface);
164+
if (x->vmnet_gateway != NULL)
165+
free(x->vmnet_gateway);
135166
free(x);
136167
}

cli.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
#include <vmnet/vmnet.h>
55

66
struct cli_options {
7-
char *vde_group; // --vde-group
8-
operating_modes_t vmnet_mode; // --vmnet-mode
9-
char *vmnet_interface; // --vmnet-interface
10-
char *vde_switch; // arg
7+
// --vde-group
8+
char *vde_group;
9+
// --vmnet-mode, corresponds to vmnet_operation_mode_key
10+
operating_modes_t vmnet_mode;
11+
// --vmnet-interface, corresponds to vmnet_shared_interface_name_key
12+
char *vmnet_interface;
13+
// --vmnet-gateway, corresponds to vmnet_start_address_key
14+
char *vmnet_gateway;
15+
// arg
16+
char *vde_switch;
1117
};
1218

1319
struct cli_options *cli_options_parse(int argc, char *argv[]);

launchd/io.github.AkihiroSuda.vde_vmnet.plist

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<key>ProgramArguments</key>
1010
<array>
1111
<string>/usr/local/bin/vde_vmnet</string>
12+
<string>--vmnet-gateway=192.168.105.1</string>
1213
<string>/var/run/vde.ctl</string>
1314
</array>
1415
<key>StandardErrorPath</key>
@@ -28,4 +29,4 @@
2829
</dict>
2930
</dict>
3031
</dict>
31-
</plist>
32+
</plist>

main.c

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <stdlib.h>
44
#include <sys/uio.h>
55

6+
#include <arpa/inet.h>
7+
68
#include <vmnet/vmnet.h>
79

810
#include <libvdeplug.h>
@@ -163,6 +165,32 @@ static interface_ref start(VDECONN *vdeconn, struct cli_options *cliopt) {
163165
xpc_dictionary_set_string(dict, vmnet_shared_interface_name_key,
164166
cliopt->vmnet_interface);
165167
}
168+
if (cliopt->vmnet_gateway != NULL) {
169+
/* Set vmnet_start_address_key */
170+
xpc_dictionary_set_string(dict, vmnet_start_address_key,
171+
cliopt->vmnet_gateway);
172+
173+
/* Set vmnet_end_address_key with .254 */
174+
struct in_addr sin;
175+
if (!inet_aton(cliopt->vmnet_gateway, &sin)) {
176+
perror("inet_aton(cliopt->vmnet_gateway)");
177+
return NULL;
178+
}
179+
uint32_t h = ntohl(sin.s_addr);
180+
h &= 0xFFFFFF00;
181+
h |= 0x000000FE;
182+
sin.s_addr = htonl(h);
183+
184+
const char *end = inet_ntoa(sin); /* static storage, do not free */
185+
if (end == NULL) {
186+
perror("inet_ntoa");
187+
return NULL;
188+
}
189+
xpc_dictionary_set_string(dict, vmnet_end_address_key, end);
190+
191+
/* Set vmnet_subnet_mask_key */
192+
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, "255.255.255.0");
193+
}
166194

167195
uuid_t uuid;
168196
// TODO: support deterministic UUID and MAC address

test/test.ipxe

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ show netmask
1010
show dns
1111
show dhcp-server
1212
echo </vde_vmnet:iPXE-testing>
13-
# sleep for flushing the serial console
13+
14+
echo [testing Internet connection]
15+
imgfetch http://google.com/
16+
17+
echo [sleeping for flushing the serial console]
1418
sleep 1
19+
20+
echo [exiting]
1521
reboot

test/test.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ if ! grep -q "net0/mac:hex = 52:54:00:12:34:56" serial.log; then
2929
exit 1
3030
fi
3131

32-
if ! grep -q "net0.dhcp/ip:ipv4 = 192.168." serial.log; then
32+
if ! grep -q "net0.dhcp/ip:ipv4 = 192.168.105." serial.log; then
3333
echo >&2 "ERROR: net0.dhcp/ip:ipv4 not found"
3434
exit 1
3535
fi
3636

37+
if ! grep -q "net0.dhcp/gateway:ipv4 = 192.168.105.1" serial.log; then
38+
echo >&2 "ERROR: net0.dhcp/gateway:ipv4 not found"
39+
exit 1
40+
fi
41+
3742
rm -f serial.log

0 commit comments

Comments
 (0)