|
| 1 | +package vmnet |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// dhcp queries the IP by DHCP |
| 9 | +func dhcpRequest(packet sendReceiver, clientMAC net.HardwareAddr) (net.IP, error) { |
| 10 | + broadcastMAC := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff} |
| 11 | + broadcastIP := []byte{0xff, 0xff, 0xff, 0xff} |
| 12 | + unknownIP := []byte{0, 0, 0, 0} |
| 13 | + |
| 14 | + dhcpRequest := NewDhcpRequest(clientMAC).Bytes() |
| 15 | + ipv4 := NewIpv4(broadcastIP, unknownIP) |
| 16 | + |
| 17 | + udpv4 := NewUdpv4(ipv4, 68, 67, dhcpRequest) |
| 18 | + ipv4.setData(udpv4.Bytes()) |
| 19 | + |
| 20 | + ethernet := NewEthernetFrame(broadcastMAC, clientMAC, 0x800) |
| 21 | + ethernet.setData(ipv4.Bytes()) |
| 22 | + finished := false |
| 23 | + go func() { |
| 24 | + for !finished { |
| 25 | + if _, err := packet.Send(ethernet.Bytes()); err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + time.Sleep(time.Second) |
| 29 | + } |
| 30 | + }() |
| 31 | + |
| 32 | + buf := make([]byte, 1500) |
| 33 | + for { |
| 34 | + n, err := packet.Recv(buf) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + response := buf[0:n] |
| 39 | + ethernet, err = ParseEthernetFrame(response) |
| 40 | + if err != nil { |
| 41 | + continue |
| 42 | + } |
| 43 | + for i, x := range ethernet.Dst { |
| 44 | + if i > len(clientMAC) || clientMAC[i] != x { |
| 45 | + // intended for someone else |
| 46 | + continue |
| 47 | + } |
| 48 | + } |
| 49 | + ipv4, err = ParseIpv4(ethernet.Data) |
| 50 | + if err != nil { |
| 51 | + // probably not an IPv4 packet |
| 52 | + continue |
| 53 | + } |
| 54 | + udpv4, err = ParseUdpv4(ipv4.Data) |
| 55 | + if err != nil { |
| 56 | + // probably not a UDPv4 packet |
| 57 | + continue |
| 58 | + } |
| 59 | + if udpv4.Src != 67 || udpv4.Dst != 68 { |
| 60 | + // not a DHCP response |
| 61 | + continue |
| 62 | + } |
| 63 | + if len(udpv4.Data) < 243 { |
| 64 | + // truncated |
| 65 | + continue |
| 66 | + } |
| 67 | + if udpv4.Data[240] != 53 || udpv4.Data[241] != 1 || udpv4.Data[242] != 2 { |
| 68 | + // not a DHCP offer |
| 69 | + continue |
| 70 | + } |
| 71 | + var ip net.IP |
| 72 | + ip = udpv4.Data[16:20] |
| 73 | + finished = true // will terminate sending goroutine |
| 74 | + return ip, nil |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// DhcpRequest is a simple DHCP request |
| 79 | +type DhcpRequest struct { |
| 80 | + MAC net.HardwareAddr |
| 81 | +} |
| 82 | + |
| 83 | +// NewDhcpRequest constructs a DHCP request |
| 84 | +func NewDhcpRequest(MAC net.HardwareAddr) *DhcpRequest { |
| 85 | + if len(MAC) != 6 { |
| 86 | + panic("MAC address must be 6 bytes") |
| 87 | + } |
| 88 | + return &DhcpRequest{MAC} |
| 89 | +} |
| 90 | + |
| 91 | +// Bytes returns the marshalled DHCP request |
| 92 | +func (d *DhcpRequest) Bytes() []byte { |
| 93 | + bs := []byte{ |
| 94 | + 0x01, // OP |
| 95 | + 0x01, // HTYPE |
| 96 | + 0x06, // HLEN |
| 97 | + 0x00, // HOPS |
| 98 | + 0x01, 0x00, 0x00, 0x00, // XID |
| 99 | + 0x00, 0x00, // SECS |
| 100 | + 0x80, 0x00, // FLAGS |
| 101 | + 0x00, 0x00, 0x00, 0x00, // CIADDR |
| 102 | + 0x00, 0x00, 0x00, 0x00, // YIADDR |
| 103 | + 0x00, 0x00, 0x00, 0x00, // SIADDR |
| 104 | + 0x00, 0x00, 0x00, 0x00, // GIADDR |
| 105 | + d.MAC[0], d.MAC[1], d.MAC[2], d.MAC[3], d.MAC[4], d.MAC[5], |
| 106 | + } |
| 107 | + bs = append(bs, make([]byte, 202)...) |
| 108 | + bs = append(bs, []byte{ |
| 109 | + 0x63, 0x82, 0x53, 0x63, // Magic cookie |
| 110 | + 0x35, 0x01, 0x01, // DHCP discover |
| 111 | + 0xff, // Endmark |
| 112 | + }...) |
| 113 | + return bs |
| 114 | +} |
0 commit comments