-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
80 lines (68 loc) · 1.69 KB
/
test.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
package main
import (
"fmt"
"net"
)
// do like a dhcp client
func testdhcpClient(message []byte) *DHCPv4 {
// https://colobu.com/2016/10/19/Go-UDP-Programming/
srcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 68}
dstAddr := &net.UDPAddr{IP: net.IPv4bcast, Port: 67}
conn, err := net.ListenUDP("udp", srcAddr)
if err != nil {
fmt.Println(err)
}
defer conn.Close()
n, err := conn.WriteToUDP(message, dstAddr)
if err != nil {
fmt.Println(err)
}
data := make([]byte, 1024)
n, raddr, err := conn.ReadFrom(data)
if err != nil {
fmt.Println(err)
}
fmt.Printf("client read %d\n%s\nfrom %v\n", n, data, raddr)
// b := make([]byte, 1)
// os.Stdin.Read(b)
p, ok := FromBytes(data)
if !ok {
return p
}
fmt.Println("xid", p.xid, "yiaddr", p.yiaddr, "chaddr", p.chaddr)
fmt.Println("MessageType", p.options[53])
// for k, v := range p.options {
// fmt.Println(k, v)
// }
return p
}
func testsend() {
p := NewRequest()
testdhcpClient(p.ToBytes())
fmt.Println("send Request")
// dhcp Discovery
// discovery := NewDiscovery()
// offer := dhcpClient(discovery.ToBytes())
// fmt.Println("client send Discovery")
// // dhcp request
// req := NewRequest()
// req.xid = offer.xid
// req.chaddr = offer.chaddr
// req.options[OptionRequestedIPAddress] = offer.yiaddr[:]
// req.options[OptionServerIdentifier] = offer.options[OptionServerIdentifier]
// udpSend(req.ToBytes())
}
// https://github.com/aler9/howto-udp-broadcast-golang
func testlisten() {
pc, err := net.ListenPacket("udp4", ":67")
if err != nil {
panic(err)
}
defer pc.Close()
buf := make([]byte, 1024)
n, addr, err := pc.ReadFrom(buf)
if err != nil {
panic(err)
}
fmt.Printf("%s sent this: %s\n", addr, buf[:n])
}