| 1 | // Copyright 2015 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.24.2 context/" |
| 19 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 22 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.24.2 `net/http`/" |
| 24 | "net/rpcscip-go gomod github.com/golang/go/src go1.24.2 `net/rpc`/" |
| 25 | "osscip-go gomod github.com/golang/go/src go1.24.2 os/" |
| 26 | "os/signalscip-go gomod github.com/golang/go/src go1.24.2 `os/signal`/" |
| 27 | "path/filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/" |
| 28 | "runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/" |
| 29 | "syncscip-go gomod github.com/golang/go/src go1.24.2 sync/" |
| 30 | "syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/" |
| 31 | "timescip-go gomod github.com/golang/go/src go1.24.2 time/" |
| 32 | |
| 33 | "github.com/coreos/go-systemd/v22/activationscip-go gomod github.com/coreos/go-systemd/v22 v22.6.0 `github.com/coreos/go-systemd/v22/activation`/" |
| 34 | |
| 35 | "github.com/containernetworking/cni/pkg/skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/" |
| 36 | currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ "github.com/containernetworking/cni/pkg/types/100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/" |
| 37 | ) |
| 38 | |
| 39 | var errNoMoreTriesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/errNoMoreTries. = errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/.Newscip-go gomod github.com/golang/go/src go1.24.2 errors/New().("no more tries") |
| 40 | |
| 41 | type DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP# struct { |
| 42 | muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux. syncscip-go gomod github.com/golang/go/src go1.24.2 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex# |
| 43 | leasesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#leases. map[string]*DHCPLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease# |
| 44 | hostNetnsPrefixscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#hostNetnsPrefix. string |
| 45 | clientTimeoutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientTimeout. timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration# |
| 46 | clientResendMaxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientResendMax. timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration# |
| 47 | clientResendTimeoutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientResendTimeout. timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration# |
| 48 | broadcastscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#broadcast. bool |
| 49 | } |
| 50 | |
| 51 | func newDHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/newDHCP().(clientTimeout, clientResendMax timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration#, resendTimeout timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration#) *DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP# { |
| 52 | return &DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#{ |
| 53 | leasesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#leases.: make(map[string]*DHCPLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#), |
| 54 | clientTimeoutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientTimeout.: clientTimeout, |
| 55 | clientResendMaxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientResendMax.: clientResendMax, |
| 56 | clientResendTimeoutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientResendTimeout.: resendTimeout, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // TODO: current client ID is too long. At least the container ID should not be used directly. |
| 61 | // A separate issue is necessary to ensure no breaking change is affecting other users. |
| 62 | func generateClientIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/generateClientID().(containerID string, netName string, ifName string) string { |
| 63 | clientID := containerID + "/" + netName + "/" + ifName |
| 64 | // defined in RFC 2132, length size can not be larger than 1 octet. So we truncate 254 to make everyone happy. |
| 65 | if len(clientID) > 254 { |
| 66 | clientID = clientID[0:254] |
| 67 | } |
| 68 | return clientID |
| 69 | } |
| 70 | |
| 71 | // Allocate acquires an IP from a DHCP server for a specified container. |
| 72 | // The acquired lease will be maintained until Release() is called. |
| 73 | func (d *DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#) Allocatescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#Allocate().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#, result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#) error { |
| 74 | conf := NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/NetConf#{} |
| 75 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData., &conf); err != nil { |
| 76 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error parsing netconf: %v", err) |
| 77 | } |
| 78 | |
| 79 | opts, err := prepareOptionsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/prepareOptions().(args.Argsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Args., conf.IPAMscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/NetConf#IPAM..ProvideOptionsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/IPAMConfig#ProvideOptions., conf.IPAMscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/NetConf#IPAM..RequestOptionsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/IPAMConfig#RequestOptions.) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | clientID := generateClientIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/generateClientID().(args.ContainerIDscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#ContainerID., conf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#Name., args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.) |
| 85 | |
| 86 | // If we already have an active lease for this clientID, do not create |
| 87 | // another one |
| 88 | l := d.getLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#getLease().(clientID) |
| 89 | if l != nil { |
| 90 | l.Checkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#Check().() |
| 91 | } else { |
| 92 | hostNetns := d.hostNetnsPrefixscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#hostNetnsPrefix. + args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns. |
| 93 | l, err = AcquireLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/AcquireLease().(clientID, hostNetns, args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., |
| 94 | opts, |
| 95 | d.clientTimeoutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientTimeout., d.clientResendMaxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientResendMax., d.clientResendTimeoutscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clientResendTimeout., d.broadcastscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#broadcast.) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | ipn, err := l.IPNetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#IPNet().() |
| 102 | if err != nil { |
| 103 | l.Stopscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#Stop().() |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | d.setLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#setLease().(clientID, l) |
| 108 | |
| 109 | result.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs. = []*currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.IPConfigscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#{{ |
| 110 | Addressscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Address.: *ipn, |
| 111 | Gatewayscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/IPConfig#Gateway.: l.Gatewayscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#Gateway().(), |
| 112 | }} |
| 113 | result.Routesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Routes. = l.Routesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#Routes().() |
| 114 | if conf.IPAMscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/NetConf#IPAM..Priorityscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/IPAMConfig#Priority. != 0 { |
| 115 | for _, r := range result.Routesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#Routes. { |
| 116 | r.Priorityscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/Route#Priority. = conf.IPAMscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/NetConf#IPAM..Priorityscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/IPAMConfig#Priority. |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // Release stops maintenance of the lease acquired in Allocate() |
| 124 | // and sends a release msg to the DHCP server. |
| 125 | func (d *DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#) Releasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#Release().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#, _ *struct{}) error { |
| 126 | conf := NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/NetConf#{} |
| 127 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData., &conf); err != nil { |
| 128 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("error parsing netconf: %v", err) |
| 129 | } |
| 130 | |
| 131 | clientID := generateClientIDscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/generateClientID().(args.ContainerIDscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#ContainerID., conf.Namescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#Name., args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.) |
| 132 | if l := d.getLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#getLease().(clientID); l != nil { |
| 133 | l.Stopscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#Stop().() |
| 134 | d.clearLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clearLease().(clientID) |
| 135 | } |
| 136 | |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | func (d *DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#) getLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#getLease().(clientID string) *DHCPLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease# { |
| 141 | d.muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux..Lockscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex#Lock().() |
| 142 | defer d.muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux..Unlockscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex#Unlock().() |
| 143 | |
| 144 | // TODO(eyakubovich): hash it to avoid collisions |
| 145 | l, ok := d.leasesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#leases.[clientID] |
| 146 | if !ok { |
| 147 | return nil |
| 148 | } |
| 149 | return l |
| 150 | } |
| 151 | |
| 152 | func (d *DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#) setLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#setLease().(clientID string, l *DHCPLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCPLease#) { |
| 153 | d.muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux..Lockscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex#Lock().() |
| 154 | defer d.muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux..Unlockscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex#Unlock().() |
| 155 | |
| 156 | // TODO(eyakubovich): hash it to avoid collisions |
| 157 | d.leasesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#leases.[clientID] = l |
| 158 | } |
| 159 | |
| 160 | // func (d *DHCP) clearLease(contID, netName, ifName string) { |
| 161 | func (d *DHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#) clearLeasescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#clearLease().(clientID string) { |
| 162 | d.muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux..Lockscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex#Lock().() |
| 163 | defer d.muxscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#mux..Unlockscip-go gomod github.com/golang/go/src go1.24.2 sync/Mutex#Unlock().() |
| 164 | |
| 165 | // TODO(eyakubovich): hash it to avoid collisions |
| 166 | delete(d.leasesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#leases., clientID) |
| 167 | } |
| 168 | |
| 169 | func getListenerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/getListener().(socketPath string) (netscip-go gomod github.com/golang/go/src go1.24.2 net/.Listenerscip-go gomod github.com/golang/go/src go1.24.2 net/Listener#, error) { |
| 170 | l, err := activationscip-go gomod github.com/coreos/go-systemd/v22 v22.6.0 `github.com/coreos/go-systemd/v22/activation`/.Listenersscip-go gomod github.com/coreos/go-systemd/v22 v22.6.0 `github.com/coreos/go-systemd/v22/activation`/Listeners().() |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
| 175 | switch { |
| 176 | case len(l) == 0: |
| 177 | if err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.24.2 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Dir().(socketPath), 0o700); err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | return netscip-go gomod github.com/golang/go/src go1.24.2 net/.Listenscip-go gomod github.com/golang/go/src go1.24.2 net/Listen().("unix", socketPath) |
| 181 | |
| 182 | case len(l) == 1: |
| 183 | if l[0] == nil { |
| 184 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("LISTEN_FDS=1 but no FD found") |
| 185 | } |
| 186 | return l[0], nil |
| 187 | |
| 188 | default: |
| 189 | return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Too many (%v) FDs passed through socket activation", len(l)) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func runDaemonscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/runDaemon().( |
| 194 | pidfilePath, hostPrefix, socketPath string, |
| 195 | dhcpClientTimeout timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration#, resendMax timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration#, resendTimeout timescip-go gomod github.com/golang/go/src go1.24.2 time/.Durationscip-go gomod github.com/golang/go/src go1.24.2 time/Duration#, |
| 196 | broadcast bool, |
| 197 | ) error { |
| 198 | // since other goroutines (on separate threads) will change namespaces, |
| 199 | // ensure the RPC server does not get scheduled onto those |
| 200 | runtimescip-go gomod github.com/golang/go/src go1.24.2 runtime/.LockOSThreadscip-go gomod github.com/golang/go/src go1.24.2 runtime/LockOSThread().() |
| 201 | |
| 202 | // Write the pidfile |
| 203 | if pidfilePath != "" { |
| 204 | if !filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.IsAbsscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/IsAbs().(pidfilePath) { |
| 205 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Error writing pidfile %q: path not absolute", pidfilePath) |
| 206 | } |
| 207 | if err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.WriteFilescip-go gomod github.com/golang/go/src go1.24.2 os/WriteFile().(pidfilePath, []byte(fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("%d", osscip-go gomod github.com/golang/go/src go1.24.2 os/.Getpidscip-go gomod github.com/golang/go/src go1.24.2 os/Getpid().())), 0o644); err != nil { |
| 208 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Error writing pidfile %q: %v", pidfilePath, err) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | l, err := getListenerscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/getListener().(hostPrefix + socketPath) |
| 213 | if err != nil { |
| 214 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Error getting listener: %v", err) |
| 215 | } |
| 216 | |
| 217 | srv := httpscip-go gomod github.com/golang/go/src go1.24.2 `net/http`/.Serverscip-go gomod github.com/golang/go/src go1.24.2 `net/http`/Server#{} |
| 218 | exit := make(chan osscip-go gomod github.com/golang/go/src go1.24.2 os/.Signalscip-go gomod github.com/golang/go/src go1.24.2 os/Signal#, 1) |
| 219 | done := make(chan bool, 1) |
| 220 | signalscip-go gomod github.com/golang/go/src go1.24.2 `os/signal`/.Notifyscip-go gomod github.com/golang/go/src go1.24.2 `os/signal`/Notify().(exit, osscip-go gomod github.com/golang/go/src go1.24.2 os/.Interruptscip-go gomod github.com/golang/go/src go1.24.2 os/Interrupt., syscallscip-go gomod github.com/golang/go/src go1.24.2 syscall/.SIGTERMscip-go gomod github.com/golang/go/src go1.24.2 syscall/SIGTERM.) |
| 221 | |
| 222 | go func() { |
| 223 | <-exit |
| 224 | srv.Shutdownscip-go gomod github.com/golang/go/src go1.24.2 `net/http`/Server#Shutdown().(contextscip-go gomod github.com/golang/go/src go1.24.2 context/.TODOscip-go gomod github.com/golang/go/src go1.24.2 context/TODO().()) |
| 225 | osscip-go gomod github.com/golang/go/src go1.24.2 os/.Removescip-go gomod github.com/golang/go/src go1.24.2 os/Remove().(hostPrefix + socketPath) |
| 226 | osscip-go gomod github.com/golang/go/src go1.24.2 os/.Removescip-go gomod github.com/golang/go/src go1.24.2 os/Remove().(pidfilePath) |
| 227 | |
| 228 | done <- true |
| 229 | }() |
| 230 | |
| 231 | dhcp := newDHCPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/newDHCP().(dhcpClientTimeout, resendMax, resendTimeout) |
| 232 | dhcp.hostNetnsPrefixscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#hostNetnsPrefix. = hostPrefix |
| 233 | dhcp.broadcastscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/dhcp`/DHCP#broadcast. = broadcast |
| 234 | rpcscip-go gomod github.com/golang/go/src go1.24.2 `net/rpc`/.Registerscip-go gomod github.com/golang/go/src go1.24.2 `net/rpc`/Register().(dhcp) |
| 235 | rpcscip-go gomod github.com/golang/go/src go1.24.2 `net/rpc`/.HandleHTTPscip-go gomod github.com/golang/go/src go1.24.2 `net/rpc`/HandleHTTP().() |
| 236 | srv.Servescip-go gomod github.com/golang/go/src go1.24.2 `net/http`/Server#Serve().(l) |
| 237 | |
| 238 | <-done |
| 239 | return nil |
| 240 | } |