-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
196 lines (155 loc) · 4.02 KB
/
app.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
package main
import (
"golang.org/x/net/ipv4"
"errors"
"log"
"net"
"path/filepath"
)
type App struct {
ipnet2pool map[HashableIpNet]*Pool
interfaces map[string]struct{}
}
func NewApp() *App {
return &App{
ipnet2pool: map[HashableIpNet]*Pool{},
interfaces: map[string]struct{}{},
}
}
func (a *App) InitConf(conf *Conf) error {
for _, iface := range conf.Interfaces {
a.interfaces[iface] = struct{}{}
}
if len(a.interfaces) == 0 {
return errors.New("No interfaces configured")
}
for _, pc := range conf.Pools {
pool, err := pc.ToPool()
if err != nil {
return err
}
pool.Persistence = NewFilePersistence(filepath.Join(conf.Leasedir, pool.Name+".json"))
count, err := pool.LoadLeases()
if err != nil {
return err
}
if count == 1 {
log.Printf("Loaded pool %v with %v lease", pool.Name, count)
} else {
log.Printf("Loaded pool %v with %v leases", pool.Name, count)
}
err = a.insertPool(pool)
if err != nil {
return err
}
}
return nil
}
func (a *App) insertPool(p *Pool) error {
ipnet := HashableIpNet{
IP: IpToFixedV4(p.Network),
Mask: IpToFixedV4(p.Netmask),
}
if _, ok := a.ipnet2pool[ipnet]; ok {
return errors.New("Duplicate IP network between pools")
}
a.ipnet2pool[ipnet] = p
return nil
}
func (a *App) oObToInterface(oob []byte) (*net.Interface, error) {
cm := &ipv4.ControlMessage{}
if err := cm.Parse(oob); err != nil {
return nil, err
}
iface, err := net.InterfaceByIndex(cm.IfIndex)
if err != nil {
return nil, err
}
return iface, nil
}
// For non-relayed requests: find a pool by comparing nets to local nic
// IPs
func (a *App) findPoolByInterface(iface *net.Interface) (*Pool, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
// FIXME: should we verify addr.Network() is first "ip+net" ?
_, ipnet, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
hipnet, err := IpNet2HashableIpNet(ipnet)
if err != nil {
continue
}
if pool, ok := a.ipnet2pool[hipnet]; ok {
return pool, nil
}
}
return nil, errors.New("Not found")
}
// For relayed requests: find a pool by comparing giaddr to configured
// pool nets
func (a *App) findPoolbyGiaddr(giaddr FixedV4) (*Pool, error) {
for _, pool := range a.ipnet2pool {
ipnet := &net.IPNet{
IP: pool.Network,
Mask: net.IPMask([]byte(pool.Netmask)),
}
if ipnet.Contains(giaddr.NetIp()) {
return pool, nil
}
}
return nil, errors.New("Not found")
}
func (a *App) DispatchMessage(myBuf, myOob []byte, remote *net.UDPAddr, localSocket *net.UDPConn) {
// Sanity remote port check
if remote.Port != 67 && remote.Port != 68 {
log.Printf("Ignoring DHCP packet with source port %d rather than 67 or 68", remote.Port)
return
}
var err error
// Grab iface and verify we're configured to work on it
iface, err := a.oObToInterface(myOob)
if err != nil {
log.Printf("Failed parsing interface out of OOB: %v", err)
return
}
if _, ok := a.interfaces[iface.Name]; !ok {
log.Printf("Ignoring DHCP traffic on unconfigured interface %v", iface.Name)
return
}
// Parse entire dhcp message
message, err := ParseDhcpMessage(myBuf)
if err != nil {
log.Printf("Failed parsing dhcp packet: %v", err)
return
}
var pool *Pool
// Relayed request. Find pool based on giaddr
if !message.Header.GatewayAddr.Empty() {
pool, err = a.findPoolbyGiaddr(message.Header.GatewayAddr)
if err != nil {
log.Printf("Can't find pool based on IPs bound to %v", iface.Name)
return
}
} else {
pool, err = a.findPoolByInterface(iface)
if err != nil {
log.Printf("Can't find pool based on IPs bound to %v", iface.Name)
return
}
}
handler := NewRequestHandler(message, pool)
response := handler.Handle()
if response != nil {
// In the case of a relayed request, send the response unicast to the relaying server
if !message.Header.GatewayAddr.Empty() {
handler.sendMessageRelayed(response, message.Header.GatewayAddr, localSocket)
} else {
handler.sendMessageBroadcast(response, localSocket)
}
}
}