Browse code

daemon/libnetwork/types: remove GetIPCopy; use slices.Clone

We can replace this utility with slices.Clone, which provides the
same functionality.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/08/10 21:44:41
Showing 3 changed files
... ...
@@ -1181,10 +1181,10 @@ func TestSetDefaultGw(t *testing.T) {
1181 1181
 	}
1182 1182
 
1183 1183
 	ipam4 := getIPv4Data(t)
1184
-	gw4 := types.GetIPCopy(ipam4[0].Pool.IP).To4()
1184
+	gw4 := slices.Clone(ipam4[0].Pool.IP).To4()
1185 1185
 	gw4[3] = 254
1186 1186
 	ipam6 := getIPv6Data(t)
1187
-	gw6 := types.GetIPCopy(ipam6[0].Pool.IP)
1187
+	gw6 := slices.Clone(ipam6[0].Pool.IP)
1188 1188
 	gw6[15] = 0x42
1189 1189
 
1190 1190
 	option := map[string]any{
... ...
@@ -372,7 +372,7 @@ func (ep *Endpoint) Gateway() net.IP {
372 372
 		return net.IP{}
373 373
 	}
374 374
 
375
-	return types.GetIPCopy(ep.joinInfo.gw)
375
+	return slices.Clone(ep.joinInfo.gw)
376 376
 }
377 377
 
378 378
 // GatewayIPv6 returns the IPv6 gateway assigned by the driver.
... ...
@@ -385,7 +385,7 @@ func (ep *Endpoint) GatewayIPv6() net.IP {
385 385
 		return net.IP{}
386 386
 	}
387 387
 
388
-	return types.GetIPCopy(ep.joinInfo.gw6)
388
+	return slices.Clone(ep.joinInfo.gw6)
389 389
 }
390 390
 
391 391
 // SetGateway sets the default IPv4 gateway when a container joins the endpoint.
... ...
@@ -393,7 +393,7 @@ func (ep *Endpoint) SetGateway(gw net.IP) error {
393 393
 	ep.mu.Lock()
394 394
 	defer ep.mu.Unlock()
395 395
 
396
-	ep.joinInfo.gw = types.GetIPCopy(gw)
396
+	ep.joinInfo.gw = slices.Clone(gw)
397 397
 	return nil
398 398
 }
399 399
 
... ...
@@ -402,7 +402,7 @@ func (ep *Endpoint) SetGatewayIPv6(gw6 net.IP) error {
402 402
 	ep.mu.Lock()
403 403
 	defer ep.mu.Unlock()
404 404
 
405
-	ep.joinInfo.gw6 = types.GetIPCopy(gw6)
405
+	ep.joinInfo.gw6 = slices.Clone(gw6)
406 406
 	return nil
407 407
 }
408 408
 
... ...
@@ -510,7 +510,7 @@ func (epj *endpointJoinInfo) CopyTo(dstEpj *endpointJoinInfo) error {
510 510
 	copy(dstEpj.StaticRoutes, epj.StaticRoutes)
511 511
 	dstEpj.driverTableEntries = make([]*tableEntry, len(epj.driverTableEntries))
512 512
 	copy(dstEpj.driverTableEntries, epj.driverTableEntries)
513
-	dstEpj.gw = types.GetIPCopy(epj.gw)
514
-	dstEpj.gw6 = types.GetIPCopy(epj.gw6)
513
+	dstEpj.gw = slices.Clone(epj.gw)
514
+	dstEpj.gw6 = slices.Clone(epj.gw6)
515 515
 	return nil
516 516
 }
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"bytes"
6 6
 	"fmt"
7 7
 	"net"
8
+	"slices"
8 9
 	"strconv"
9 10
 	"strings"
10 11
 	"syscall"
... ...
@@ -111,9 +112,9 @@ func (p PortBinding) ContainerAddr() (net.Addr, error) {
111 111
 func (p *PortBinding) GetCopy() PortBinding {
112 112
 	return PortBinding{
113 113
 		Proto:       p.Proto,
114
-		IP:          GetIPCopy(p.IP),
114
+		IP:          slices.Clone(p.IP),
115 115
 		Port:        p.Port,
116
-		HostIP:      GetIPCopy(p.HostIP),
116
+		HostIP:      slices.Clone(p.HostIP),
117 117
 		HostPort:    p.HostPort,
118 118
 		HostPortEnd: p.HostPortEnd,
119 119
 	}
... ...
@@ -214,16 +215,6 @@ func ParseProtocol(s string) Protocol {
214 214
 	}
215 215
 }
216 216
 
217
-// GetIPCopy returns a copy of the passed IP address
218
-func GetIPCopy(from net.IP) net.IP {
219
-	if from == nil {
220
-		return nil
221
-	}
222
-	to := make(net.IP, len(from))
223
-	copy(to, from)
224
-	return to
225
-}
226
-
227 217
 // GetIPNetCopy returns a copy of the passed IP Network
228 218
 func GetIPNetCopy(from *net.IPNet) *net.IPNet {
229 219
 	if from == nil {
... ...
@@ -231,7 +222,7 @@ func GetIPNetCopy(from *net.IPNet) *net.IPNet {
231 231
 	}
232 232
 	bm := make(net.IPMask, len(from.Mask))
233 233
 	copy(bm, from.Mask)
234
-	return &net.IPNet{IP: GetIPCopy(from.IP), Mask: bm}
234
+	return &net.IPNet{IP: slices.Clone(from.IP), Mask: bm}
235 235
 }
236 236
 
237 237
 // GetIPNetCanonical returns the canonical form for the passed network
... ...
@@ -292,7 +283,7 @@ func GetHostPartIP(ip net.IP, mask net.IPMask) (net.IP, error) {
292 292
 	}
293 293
 
294 294
 	// Compute host portion
295
-	out := GetIPCopy(ip)
295
+	out := slices.Clone(ip)
296 296
 	for i := 0; i < len(mask[ms:]); i++ {
297 297
 		out[is+i] &= ^mask[ms+i]
298 298
 	}
... ...
@@ -311,7 +302,7 @@ func GetBroadcastIP(ip net.IP, mask net.IPMask) (net.IP, error) {
311 311
 	}
312 312
 
313 313
 	// Compute broadcast address
314
-	out := GetIPCopy(ip)
314
+	out := slices.Clone(ip)
315 315
 	for i := 0; i < len(mask[ms:]); i++ {
316 316
 		out[is+i] |= ^mask[ms+i]
317 317
 	}
... ...
@@ -348,7 +339,7 @@ func (r *StaticRoute) GetCopy() *StaticRoute {
348 348
 	return &StaticRoute{
349 349
 		Destination: GetIPNetCopy(r.Destination),
350 350
 		RouteType:   r.RouteType,
351
-		NextHop:     GetIPCopy(r.NextHop),
351
+		NextHop:     slices.Clone(r.NextHop),
352 352
 	}
353 353
 }
354 354