MCPcopy Index your code
hub / github.com/coder/coder / convertRouterConfig

Function convertRouterConfig

vpn/router.go:37–109  ·  view source on GitHub ↗
(cfg router.Config)

Source from the content-addressed store, hash-verified

35}
36
37func convertRouterConfig(cfg router.Config) *NetworkSettingsRequest {
38 v4LocalAddrs := make([]string, 0)
39 v4SubnetMasks := make([]string, 0)
40 v6LocalAddrs := make([]string, 0)
41 v6PrefixLengths := make([]uint32, 0)
42 for _, addrs := range cfg.LocalAddrs {
43 switch {
44 case addrs.Addr().Is4():
45 v4LocalAddrs = append(v4LocalAddrs, addrs.Addr().String())
46 v4SubnetMasks = append(v4SubnetMasks, prefixToSubnetMask(addrs))
47 case addrs.Addr().Is6():
48 v6LocalAddrs = append(v6LocalAddrs, addrs.Addr().String())
49 // #nosec G115 - Safe conversion as IPv6 prefix lengths are always within uint32 range (0-128)
50 v6PrefixLengths = append(v6PrefixLengths, uint32(addrs.Bits()))
51 default:
52 continue
53 }
54 }
55 v4Routes := make([]*NetworkSettingsRequest_IPv4Settings_IPv4Route, 0)
56 v6Routes := make([]*NetworkSettingsRequest_IPv6Settings_IPv6Route, 0)
57 for _, route := range cfg.Routes {
58 switch {
59 case route.Addr().Is4():
60 v4Routes = append(v4Routes, convertToIPV4Route(route))
61 case route.Addr().Is6():
62 v6Routes = append(v6Routes, convertToIPV6Route(route))
63 default:
64 continue
65 }
66 }
67 v4ExcludedRoutes := make([]*NetworkSettingsRequest_IPv4Settings_IPv4Route, 0)
68 v6ExcludedRoutes := make([]*NetworkSettingsRequest_IPv6Settings_IPv6Route, 0)
69 for _, route := range cfg.LocalRoutes {
70 switch {
71 case route.Addr().Is4():
72 v4ExcludedRoutes = append(v4ExcludedRoutes, convertToIPV4Route(route))
73 case route.Addr().Is6():
74 v6ExcludedRoutes = append(v6ExcludedRoutes, convertToIPV6Route(route))
75 default:
76 continue
77 }
78 }
79
80 var v4Settings *NetworkSettingsRequest_IPv4Settings
81 if len(v4LocalAddrs) > 0 || len(v4Routes) > 0 || len(v4ExcludedRoutes) > 0 {
82 v4Settings = &NetworkSettingsRequest_IPv4Settings{
83 Addrs: v4LocalAddrs,
84 SubnetMasks: v4SubnetMasks,
85 IncludedRoutes: v4Routes,
86 ExcludedRoutes: v4ExcludedRoutes,
87 Router: "", // NA
88 }
89 }
90
91 var v6Settings *NetworkSettingsRequest_IPv6Settings
92 if len(v6LocalAddrs) > 0 || len(v6Routes) > 0 || len(v6ExcludedRoutes) > 0 {
93 v6Settings = &NetworkSettingsRequest_IPv6Settings{
94 Addrs: v6LocalAddrs,

Callers 2

SetMethod · 0.85
TestConvertRouterConfigFunction · 0.85

Calls 5

prefixToSubnetMaskFunction · 0.85
convertToIPV4RouteFunction · 0.85
convertToIPV6RouteFunction · 0.85
AddrMethod · 0.45
StringMethod · 0.45

Tested by 1

TestConvertRouterConfigFunction · 0.68