Debian Codegraph @

plugins/ipam/dhcp/options.go

raw symbols SCIP
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 main
16
17 import (
18 "fmt"
19 "net"
20 "strconv"
21
22 dhcp4 "github.com/insomniacslk/dhcp/dhcpv4"
23
24 "github.com/containernetworking/cni/pkg/types"
25 )
26
27 var optionNameToID = map[string]dhcp4.OptionCode{
28 "dhcp-client-identifier": dhcp4.OptionClientIdentifier,
29 "subnet-mask": dhcp4.OptionSubnetMask,
30 "routers": dhcp4.OptionRouter,
31 "host-name": dhcp4.OptionHostName,
32 "user-class": dhcp4.OptionUserClassInformation,
33 "vendor-class-identifier": dhcp4.OptionClassIdentifier,
34 }
35
36 func parseOptionName(option string) (dhcp4.OptionCode, error) {
37 if val, ok := optionNameToID[option]; ok {
38 return val, nil
39 }
40 i, err := strconv.ParseUint(option, 10, 8)
41 if err != nil {
42 return dhcp4.OptionPad, fmt.Errorf("Can not parse option: %w", err)
43 }
44 return dhcp4.GenericOptionCode(i), nil
45 }
46
47 func classfulSubnet(sn net.IP) net.IPNet {
48 return net.IPNet{
49 IP: sn,
50 Mask: sn.DefaultMask(),
51 }
52 }
53
54 func parseRoutes(opt []byte) []*types.Route {
55 // StaticRoutes format: pairs of:
56 // Dest = 4 bytes; Classful IP subnet
57 // Router = 4 bytes; IP address of router
58
59 routes := []*types.Route{}
60 for len(opt) >= 8 {
61 sn := opt[0:4]
62 r := opt[4:8]
63 rt := &types.Route{
64 Dst: classfulSubnet(sn),
65 GW: r,
66 }
67 routes = append(routes, rt)
68 opt = opt[8:]
69 }
70
71 return routes
72 }