File: ip.y

package info (click to toggle)
golang-github-malfunkt-iprange 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 116 kB
  • sloc: yacc: 86; makefile: 2
file content (109 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
%{

package iprange

import (
    "encoding/binary"
    "net"

    "github.com/pkg/errors"
)

type AddressRangeList []AddressRange

type AddressRange struct {
    Min net.IP
    Max net.IP
}

type octetRange struct {
    min byte
    max byte
}

%}

%union {
    num         byte
    octRange    octetRange
    addrRange   AddressRange
    result      AddressRangeList
}

%token  <num> num
%type   <addrRange> address target
%type   <octRange>  term octet_range
%type   <result>    result

%%

result: target
            {
                $$ = append($$, $1)
                iplex.(*ipLex).output = $$
            }
      | result comma target
            {
                $$ = append($1, $3)
                iplex.(*ipLex).output = $$
            }

comma: ',' | ',' ' '

target:     address '/' num
                {
                    mask := net.CIDRMask(int($3), 32)
                    min := $1.Min.Mask(mask)
                    maxInt := binary.BigEndian.Uint32([]byte(min)) +
                                0xffffffff -
                                binary.BigEndian.Uint32([]byte(mask))
                    maxBytes := make([]byte, 4)
                    binary.BigEndian.PutUint32(maxBytes, maxInt)
                    maxBytes = maxBytes[len(maxBytes)-4:]
                    max := net.IP(maxBytes)
                    $$ = AddressRange {
                        Min: min.To4(),
                        Max: max.To4(),
                    }
                }
      |     address
                {
                    $$ = $1
                }

address:    term '.' term '.' term '.' term
                {
                    $$ = AddressRange {
                        Min: net.IPv4($1.min, $3.min, $5.min, $7.min).To4(),
                        Max: net.IPv4($1.max, $3.max, $5.max, $7.max).To4(),
                    }
                }

term:   num         { $$ = octetRange { $1, $1 } }
    |   '*'         { $$ = octetRange { 0, 255 } }
    |   octet_range { $$ = $1 }

octet_range:    num '-' num { $$ = octetRange { $1, $3 } }

%%

// ParseList takes a list of target specifications and returns a list of ranges,
// even if the list contains a single element.
func ParseList(in string) (AddressRangeList, error) {
    lex := &ipLex{line: []byte(in)}
    errCode := ipParse(lex)
    if errCode != 0 || lex.err != nil {
        return nil, errors.Wrap(lex.err, "could not parse target")
    }
    return lex.output, nil
}

// Parse takes a single target specification and returns a range. It effectively calls ParseList
// and returns the first result
func Parse(in string) (*AddressRange, error) {
    l, err := ParseList(in)
    if err != nil {
        return nil, err
    }
    return &l[0], nil
}