1// +build windows
2
3package libnetwork
4
5import (
6	"runtime"
7	"time"
8
9	"github.com/Microsoft/hcsshim"
10	"github.com/docker/libnetwork/drivers/windows"
11	"github.com/docker/libnetwork/ipamapi"
12	"github.com/docker/libnetwork/ipams/windowsipam"
13	"github.com/sirupsen/logrus"
14)
15
16func executeInCompartment(compartmentID uint32, x func()) {
17	runtime.LockOSThread()
18
19	if err := hcsshim.SetCurrentThreadCompartmentId(compartmentID); err != nil {
20		logrus.Error(err)
21	}
22	defer func() {
23		hcsshim.SetCurrentThreadCompartmentId(0)
24		runtime.UnlockOSThread()
25	}()
26
27	x()
28}
29
30func (n *network) startResolver() {
31	if n.networkType == "ics" {
32		return
33	}
34	n.resolverOnce.Do(func() {
35		logrus.Debugf("Launching DNS server for network %q", n.Name())
36		options := n.Info().DriverOptions()
37		hnsid := options[windows.HNSID]
38
39		if hnsid == "" {
40			return
41		}
42
43		hnsresponse, err := hcsshim.HNSNetworkRequest("GET", hnsid, "")
44		if err != nil {
45			logrus.Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
46			return
47		}
48
49		for _, subnet := range hnsresponse.Subnets {
50			if subnet.GatewayAddress != "" {
51				for i := 0; i < 3; i++ {
52					resolver := NewResolver(subnet.GatewayAddress, false, "", n)
53					logrus.Debugf("Binding a resolver on network %s gateway %s", n.Name(), subnet.GatewayAddress)
54					executeInCompartment(hnsresponse.DNSServerCompartment, resolver.SetupFunc(53))
55
56					if err = resolver.Start(); err != nil {
57						logrus.Errorf("Resolver Setup/Start failed for container %s, %q", n.Name(), err)
58						time.Sleep(1 * time.Second)
59					} else {
60						logrus.Debugf("Resolver bound successfully for network %s", n.Name())
61						n.resolver = append(n.resolver, resolver)
62						break
63					}
64				}
65			}
66		}
67	})
68}
69
70func defaultIpamForNetworkType(networkType string) string {
71	if windows.IsBuiltinLocalDriver(networkType) {
72		return windowsipam.DefaultIPAM
73	}
74	return ipamapi.DefaultIPAM
75}
76