/
node.go
251 lines (218 loc) · 5.04 KB
/
node.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import "net"
import "fmt"
import "os"
import "io"
import "bytes"
import "strings"
const (
fieldSep = " "
fieldCount = 4
maxNameLen = 255
sizeIP6 = 16
sizeIP4 = 4
sizeAddrs = 2*sizeIP6 + 1*sizeIP4
maxSize = 1 + maxNameLen + sizeAddrs
)
var (
NameTooShort = os.NewError("Name too short")
NameTooLong = os.NewError("Name too long")
ExtIPInvalid = os.NewError("External IP address is invalid")
IntIP4Invalid = os.NewError("Internal IPv4 address is invalid")
IntIP6Invalid = os.NewError("Internal IPv6 address is invalid")
)
type Node struct {
Name string
ExtIP, IntIP4, IntIP6 net.IP
}
func NewNode(name string, extIP, intIP4, intIP6 net.IP) *Node {
return &Node{Name: name, ExtIP: extIP, IntIP4: intIP4, IntIP6: intIP6}
}
func NewNodeString(line string) (*Node, os.Error) {
line = strings.TrimSpace(line)
fields := strings.Split(line, fieldSep, fieldCount)
if len(fields) != fieldCount {
return nil, os.NewError("Expected four space seperated fields")
}
name := fields[0]
extIP := net.ParseIP(fields[1])
if extIP == nil {
return nil, os.NewError("External IP address is invalid")
}
intIP4 := net.ParseIP(fields[2])
if intIP4 == nil {
return nil, os.NewError("Interal IPv4 address is invalid")
}
intIP4 = intIP4.To4()
if intIP4 == nil {
return nil, os.NewError("Interal IPv4 address is a non IPv4 IP address")
}
intIP6 := net.ParseIP(fields[3])
if intIP6 == nil {
return nil, os.NewError("Internal IPv6 address is invalid")
}
if intIP6.To4() != nil {
return nil, os.NewError("Internal IPv6 address is an IPv4 address")
}
return NewNode(name, extIP, intIP4, intIP6), nil
}
func NewNodeReader(r io.Reader) (*Node, os.Error) {
buf := make([]byte, maxSize)
if _, err := r.Read(buf[0:1]); err != nil {
return nil, err
}
nameLen := int(buf[0])
if nameLen == 0 {
return nil, NameTooShort
}
size := 1 + nameLen + sizeAddrs
if _, err := r.Read(buf[1:size]); err != nil {
return nil, err
}
pos := 1
name := string(buf[pos : pos+nameLen])
pos += nameLen
extIP := net.IP(buf[pos : pos+sizeIP6])
pos += sizeIP6
intIP4 := net.IP(buf[pos : pos+sizeIP4])
pos += sizeIP4
intIP6 := net.IP(buf[pos : pos+sizeIP6])
pos += sizeIP6
return NewNode(name, extIP, intIP4, intIP6), nil
}
func (node *Node) ToBuffer() (*bytes.Buffer, os.Error) {
name := node.Name
nameLen := len(name)
buf := bytes.NewBufferString("")
if nameLen > maxNameLen {
return nil, NameTooLong
}
if nameLen < 1 {
return nil, NameTooShort
}
extIP := node.ExtIP.To16()
intIP4 := node.IntIP4.To4()
intIP6 := node.IntIP6.To16()
if extIP == nil {
return nil, ExtIPInvalid
}
if intIP4 == nil {
return nil, IntIP4Invalid
}
if intIP6 == nil {
return nil, IntIP6Invalid
}
if err := buf.WriteByte(byte(nameLen)); err != nil {
return nil, err
}
if _, err := buf.Write([]byte(name)); err != nil {
return nil, err
}
if _, err := buf.Write(extIP); err != nil {
return nil, err
}
if _, err := buf.Write(intIP4); err != nil {
return nil, err
}
if _, err := buf.Write(intIP6); err != nil {
return nil, err
}
return buf, nil
}
func (node *Node) String() string {
return node.Name + " " + node.ExtIP.String() + " " + node.IntIP4.String() + " " + node.IntIP6.String()
}
func scan(node *Node, name string, extIP, intIP4, intIP6 net.IP) os.Error {
if len(name) < 1 {
return NameTooShort
}
if len(name) > maxNameLen {
return NameTooLong
}
fmt.Print("name = ", name)
fmt.Print(", ext = ", extIP)
fmt.Println()
if extIP == nil {
return ExtIPInvalid
}
if intIP4 == nil {
return IntIP4Invalid
}
intIP4 = intIP4.To4()
if intIP4 == nil {
return IntIP4Invalid
}
if intIP6 == nil {
return IntIP6Invalid
}
intIP6 = intIP6.To16()
if intIP6 == nil {
return IntIP6Invalid
}
node.Name = name
node.ExtIP = extIP
node.IntIP4 = intIP4
node.IntIP6 = intIP6
return nil
}
func dup(bytes []byte) []byte {
copied := make([]byte, len(bytes))
copy(copied, bytes)
return copied
}
func (node *Node) Scan(state fmt.ScanState, verb int) os.Error {
nameBytes, err := state.Token(true, nil)
if err != nil {
return err
}
if len(nameBytes) == 0 {
return os.EOF
}
nameBytes = dup(nameBytes)
extIPBytes, err := state.Token(true, nil)
if err != nil {
return err
}
extIPBytes = dup(extIPBytes)
intIP4Bytes, err := state.Token(true, nil)
if err != nil {
return err
}
intIP4Bytes = dup(intIP4Bytes)
intIP6Bytes, err := state.Token(true, nil)
if err != nil {
return err
}
intIP6Bytes = dup(intIP6Bytes)
name := string(nameBytes)
if len(name) < 1 {
return NameTooShort
}
if len(name) > maxNameLen {
return NameTooLong
}
extIP := net.ParseIP(string(extIPBytes))
if extIP == nil {
return ExtIPInvalid
}
intIP4 := net.ParseIP(string(intIP4Bytes))
if intIP4 == nil {
return IntIP4Invalid
}
intIP4 = intIP4.To4()
if intIP4 == nil {
return IntIP6Invalid
}
intIP6 := net.ParseIP(string(intIP6Bytes))
if intIP6 == nil {
return IntIP6Invalid
}
if intIP6.To4() != nil {
return IntIP6Invalid
}
node.Name = name
node.ExtIP = extIP
node.IntIP4 = intIP4
node.IntIP6 = intIP6
return nil
}