// adapter with the same name exist, it is reused.
//
func CreateTUN(ifname string) (TUNDevice, error) {
+ return CreateTUNWithRequestedGUID(ifname, nil)
+}
+
+//
+// CreateTUNWithRequestedGUID creates a Wintun adapter with the given name and
+// a requested GUID. Should a Wintun adapter with the same name exist, it is reused.
+//
+func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (TUNDevice, error) {
var err error
var wt *wintun.Wintun
} else if err == windows.ERROR_ALREADY_EXISTS {
return nil, fmt.Errorf("Foreign network interface with the same name exists")
}
- wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", 0)
+ wt, _, err = wintun.CreateInterface("WireGuard Tunnel Adapter", requestedGUID, 0)
if err != nil {
return nil, fmt.Errorf("Unable to create Wintun interface: %v", err)
}
// description is a string that supplies the text description of the device.
// description is optional and can be "".
//
+// requestedGUID is the GUID of the created network interface, which then
+// influences NLA generation deterministically. If it is set to nil, the GUID
+// is chosen by the system at random, and hence a new NLA entry is created for
+// each new interface. It is called "requested" GUID because the API it uses
+// is completely undocumented, and so there could be minor interesting
+// complications with its usage.
+//
// hwndParent is a handle to the top-level window to use for any user
// interface that is related to non-device-specific actions (such as a select-
// device dialog box that uses the global class driver list). This handle is
//
// Function returns the network interface ID and a flag if reboot is required.
//
-func CreateInterface(description string, hwndParent uintptr) (*Wintun, bool, error) {
+func CreateInterface(description string, requestedGUID *windows.GUID, hwndParent uintptr) (*Wintun, bool, error) {
// Create an empty device info set for network adapter device class.
devInfoList, err := setupapi.SetupDiCreateDeviceInfoListEx(&deviceClassNetGUID, hwndParent, machineName)
if err != nil {
// Register device co-installers if any. (Ignore errors)
devInfoList.CallClassInstaller(setupapi.DIF_REGISTER_COINSTALLERS, deviceData)
+ if requestedGUID != nil {
+ key, err := devInfoList.OpenDevRegKey(deviceData, setupapi.DICS_FLAG_GLOBAL, 0, setupapi.DIREG_DRV, registry.SET_VALUE)
+ if err != nil {
+ return nil, false, fmt.Errorf("OpenDevRegKey failed: %v", err)
+ }
+ err = key.SetStringValue("NetSetupAnticipatedInstanceId", requestedGUID.String())
+ key.Close()
+ if err != nil {
+ return nil, false, fmt.Errorf("SetStringValue(NetSetupAnticipatedInstanceId) failed: %v", err)
+ }
+ }
+
// Install interfaces if any. (Ignore errors)
devInfoList.CallClassInstaller(setupapi.DIF_INSTALLINTERFACES, deviceData)