Skip to content

Commit 2fa8a63

Browse files
NattyNarwhalpkoning2
authored andcommitted
Allow for shared/bridged mode, show in SHOW ETHERNET
Show the allowed bridged network devices SHOW ETHERNET, as well as the shared/host networking modes. It also shows shared/host/device name for bridge in i.e. "HELP XS" output as well. Shared, bridged, and host modes have other options for configuration; i.e. isolation, IP ranges, etc. Some of these are not well documented, so should look into these. Bridged mode needs macOS 10.15.
1 parent 361ef76 commit 2fa8a63

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

sim_ether.c

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@
379379
#include <unistd.h>
380380
#endif
381381

382+
// Declare earlier than other implementations
383+
#ifdef HAVE_VMNET_NETWORK
384+
#include <vmnet/vmnet.h>
385+
#endif
386+
382387
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
383388

384389
/* Internal routine - forward declaration */
@@ -1201,11 +1206,36 @@ if (used < max) {
12011206
#endif
12021207
#ifdef HAVE_VMNET_NETWORK
12031208
if (used < max) {
1204-
sprintf(list[used].name, "%s", "vmnet:{optional-parameters}");
1205-
sprintf(list[used].desc, "%s", "Integrated vmnet.framework support");
1209+
sprintf(list[used].name, "%s", "vmnet:shared");
1210+
sprintf(list[used].desc, "%s", "Integrated NAT (vmnet.framework) support");
1211+
list[used].eth_api = ETH_API_VMN;
1212+
++used;
1213+
}
1214+
if (used < max) {
1215+
sprintf(list[used].name, "%s", "vmnet:host");
1216+
sprintf(list[used].desc, "%s", "Integrated host-only network (vmnet.framework) support");
12061217
list[used].eth_api = ETH_API_VMN;
12071218
++used;
12081219
}
1220+
// vmnet.framework has an allowed list of devices for bridging
1221+
// handy for user if we list them since ifconfig is noisy
1222+
__block int used_block;
1223+
{
1224+
xpc_object_t bridge_list = vmnet_copy_shared_interface_list();
1225+
1226+
xpc_array_apply(bridge_list, ^bool(size_t index, xpc_object_t value) {
1227+
if (used_block < max) {
1228+
sprintf(list[used_block].name, "%s%s", "vmnet:", xpc_string_get_string_ptr(value));
1229+
sprintf(list[used_block].desc, "%s", "Integrated bridged network (vmnet.framework) support");
1230+
list[used_block].eth_api = ETH_API_VMN;
1231+
++used_block;
1232+
}
1233+
return true;
1234+
});
1235+
used = used_block;
1236+
1237+
xpc_release(bridge_list);
1238+
}
12091239
#endif
12101240

12111241
if (used < max) {
@@ -1256,10 +1286,6 @@ extern "C" {
12561286
#include <dlfcn.h>
12571287
#endif
12581288

1259-
#ifdef HAVE_VMNET_NETWORK
1260-
#include <vmnet/vmnet.h>
1261-
#endif
1262-
12631289
#if defined(USE_SHARED) && (defined(_WIN32) || defined(SIM_HAVE_DLOPEN))
12641290
/* Dynamic DLL loading technique and modified source comes from
12651291
Etherial/WireShark capture_pcap.c */
@@ -2301,12 +2327,20 @@ memset(errbuf, 0, PCAP_ERRBUF_SIZE);
23012327
// Because vmnet operates via callbacks, set up a semaphore to block on
23022328
dispatch_semaphore_t cb_finished;
23032329

2330+
const char *devname = savname + sizeof("vmnet:") - 1;
2331+
23042332
if_desc = xpc_dictionary_create(NULL, NULL, 0);
23052333
// VMNET_HOST_MODE: Host and other guests
23062334
// VMNET_SHARED_MODE: NAT
23072335
// VMNET_BRIDGED_MODE: Requires macOS 10.15
2308-
// XXX: Support other modes.
2309-
xpc_dictionary_set_uint64(if_desc, vmnet_operation_mode_key, VMNET_HOST_MODE);
2336+
if (0 == strcmp(devname, "shared")) {
2337+
xpc_dictionary_set_uint64(if_desc, vmnet_operation_mode_key, VMNET_SHARED_MODE);
2338+
} else if (strlen(devname) == 0 || 0 == strcmp(devname, "host")) {
2339+
xpc_dictionary_set_uint64(if_desc, vmnet_operation_mode_key, VMNET_HOST_MODE);
2340+
} else if (strlen(devname) > 0) { // Bridged, this is the device name
2341+
xpc_dictionary_set_uint64(if_desc, vmnet_operation_mode_key, VMNET_BRIDGED_MODE);
2342+
xpc_dictionary_set_string(if_desc, vmnet_shared_interface_name_key, devname);
2343+
}
23102344

23112345
vmn_queue = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0);
23122346

@@ -2839,6 +2873,11 @@ fprintf (st, " eth2 vde:device{:switch-port-number} (Integrated VDE su
28392873
fprintf (st, " eth3 nat:{optional-nat-parameters} (Integrated NAT (SLiRP) support)\n");
28402874
#endif
28412875
fprintf (st, " eth4 udp:sourceport:remotehost:remoteport (Integrated UDP bridge support)\n");
2876+
#if defined(HAVE_VMNET_NETWORK)
2877+
fprintf (st, " eth6 vmnet:shared (Integrated NAT (vmnet.framework) support)\n");
2878+
fprintf (st, " eth7 vmnet:host (Integrated host-only (vmnet.framework) support)\n");
2879+
fprintf (st, " eth8 vmnet:device-name (Integrated bridged (vmnet.framework) support)\n");
2880+
#endif
28422881
fprintf (st, " sim> ATTACH %s eth0\n\n", dptr->name);
28432882
fprintf (st, "or equivalently:\n\n");
28442883
fprintf (st, " sim> ATTACH %s en0\n\n", dptr->name);

0 commit comments

Comments
 (0)