-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers_pool.go
54 lines (50 loc) · 1.64 KB
/
workers_pool.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
package main
import (
"context"
_ "expvar"
"net"
"strconv"
dhcp "github.com/krolaw/dhcp4"
)
type job struct {
DHCPpacket dhcp.Packet
msgType dhcp.MessageType
Int *Interface
handler Handler
clientAddr net.Addr //remote client ip
srvAddr net.IP
localCtx context.Context
}
func doWork(id int, element job) {
var ans Answer
if ans = element.handler.ServeDHCP(element.localCtx, element.DHCPpacket, element.msgType, element.clientAddr, element.srvAddr); ans.D != nil {
// DHCP Relay
if element.Int.isRelay() {
switch element.msgType {
case dhcp.Discover:
sendUnicastDHCP(ans.D, element.Int.relayIP, element.Int.Ipv4, element.DHCPpacket.GIAddr(), bootp_client, bootp_server)
case dhcp.Offer:
client, _ := NewRawClient(element.Int.intNet)
client.sendDHCP(ans.MAC, ans.D, ans.IP, element.Int.Ipv4)
client.Close()
case dhcp.Request:
sendUnicastDHCP(ans.D, element.Int.relayIP, element.Int.Ipv4, element.DHCPpacket.GIAddr(), bootp_client, bootp_server)
case dhcp.ACK:
client, _ := NewRawClient(element.Int.intNet)
client.sendDHCP(ans.MAC, ans.D, ans.IP, element.Int.Ipv4)
client.Close()
}
} else {
// DHCP Server
ipStr, portStr, _ := net.SplitHostPort(element.clientAddr.String())
if !(element.DHCPpacket.GIAddr().Equal(net.IPv4zero) && net.ParseIP(ipStr).Equal(net.IPv4zero)) {
dstPort, _ := strconv.Atoi(portStr)
sendUnicastDHCP(ans.D, net.ParseIP(ipStr), element.Int.Ipv4, element.DHCPpacket.GIAddr(), bootp_server, dstPort)
} else {
client, _ := NewRawClient(element.Int.intNet)
client.sendDHCP(ans.MAC, ans.D, ans.IP, element.Int.Ipv4)
client.Close()
}
}
}
}