-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (156 loc) · 4.22 KB
/
main.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
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
_ "github.com/mattn/go-sqlite3"
)
const (
appname = "SDHCP"
version = "0.1"
)
var configfile string
// Config is read from a json file
var Config map[string]interface{}
// http://www.networksorcery.com/enp/protocol/bootp/option001.htm
// var defaultSubnetMask net.IPMask = net.IPv4Mask(255, 255, 255, 0) // Mask 4 byte.
var defaultRouter net.IP = net.ParseIP("192.168.20.1")[12:] // net.ipv4 has IPv6len
var defaultSubnetMask net.IPMask = defaultRouter.DefaultMask()
var defaultDNS = []net.IP{defaultRouter}
var defaultServerIdentifier net.IP = defaultRouter
var defaultLeaseTime uint32 = 60
var defaultDatabase string = "./network.db"
var defaultTable string = "192.168.20.1/24"
// use macaddr to get ip from sqlite database
func matchAddress(mac net.HardwareAddr) (net.IP, bool) {
var ip string
queryaddr := fmt.Sprintf("%s", mac)
// db field `mac` like 60-AB-67-F8-D5-6C
queryaddr = strings.ReplaceAll(queryaddr, ":", "-")
queryaddr = strings.ToUpper(queryaddr)
// DB must exist!
//https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go/12518877
if _, err := os.Stat(defaultDatabase); os.IsNotExist(err) {
logErr(err)
}
db, err := sql.Open("sqlite3", defaultDatabase)
logErr(err)
defer db.Close()
sql := fmt.Sprintf("SELECT ip FROM `%s` WHERE mac=?;", defaultTable)
stmt, err := db.Prepare(sql)
logErr(err)
defer stmt.Close()
err = stmt.QueryRow(queryaddr).Scan(&ip)
if err != nil {
return nil, false
}
return net.ParseIP(ip), true
}
// just send DHCP BOOTREPLY in broadcast
func udpSend(socket *net.UDPConn, b []byte) {
dstaddr := &net.UDPAddr{IP: net.IPv4bcast, Port: 68}
_, err := socket.WriteToUDP(b, dstaddr)
logErr(err)
}
func listenServer() {
port := 67
addr := &net.UDPAddr{IP: nil, Port: port} // IP: net.IPv4zero
socket, err := net.ListenUDP("udp", addr)
logErr(err)
log.Println("listening....", port)
defer socket.Close()
for {
data := make([]byte, DHCPMAXLEN)
read, remoteAddr, err := socket.ReadFromUDP(data)
if err != nil {
log.Println("ReadFromUDP:", err)
}
log.Printf("Read %d From %v ", read, remoteAddr)
if read < 240 {
continue // Packet too small to be DHCP
}
go process(socket, data[:read]) // main loop will not panic
}
}
func process(socket *net.UDPConn, data []byte) {
p, ok := FromBytes(data)
if !ok {
log.Println("incomplete packet!")
return
}
messagetype, ok := p.options[OptionDHCPMessageType]
if !ok {
log.Println("NO DHCPMessageType!")
return
}
log.Printf("xid: %s, %v,\n", p.xid, messagetype)
ip, ok := matchAddress(p.chaddr)
if ok {
log.Printf("MATCH: %s %s\n", p.chaddr, ip)
} else {
log.Printf("NO FOUND: %s\n", p.chaddr)
return
}
switch messagetype {
case Discover:
log.Println("Discover -> Offer")
offer := NewOffer(p, ip)
udpSend(socket, offer.ToBytes())
case Request:
log.Println("Request -> Ack")
ack := NewAck(p, ip)
udpSend(socket, ack.ToBytes())
case Decline:
log.Println("Some one already user thie IP")
case Release:
log.Println("Release IP")
case Inform:
log.Println("Just note it")
default:
log.Println("? unknow messagetype", messagetype)
}
}
func main() {
flag.StringVar(&configfile, "c", "config.json", "json config file")
v := flag.Bool("v", false, "version")
flag.Parse()
if *v { // show version
fmt.Println(appname, version)
os.Exit(0)
}
// read json and set Config
dat, err := ioutil.ReadFile(configfile)
logErr(err)
err = json.Unmarshal(dat, &Config)
logErr(err)
defaultRouter = net.ParseIP(Config["route"].(string))[12:]
m := net.ParseIP(Config["mask"].(string))[12:]
defaultSubnetMask = net.IPv4Mask(m[0], m[1], m[2], m[3])
if val, ok := Config["dns"]; ok {
dnslist := val.([]interface{})
for i, d := range dnslist {
if i == 0 {
defaultDNS[0] = net.ParseIP(d.(string))[12:]
} else {
defaultDNS = append(defaultDNS, net.ParseIP(d.(string))[12:])
}
}
}
defaultServerIdentifier = defaultRouter
defaultLeaseTime = uint32(Config["leasetime"].(float64))
if val, ok := Config["database"]; ok {
defaultDatabase = val.(string)
}
if val, ok := Config["table"]; ok {
defaultTable = val.(string)
}
// testsend()
// testlisten()
listenServer()
}