File: dhcp_client.go

package info (click to toggle)
mgmt 0.0.26.git.2024.10.25.85e1d6c0e8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,364 kB
  • sloc: sh: 2,471; yacc: 1,285; makefile: 543; python: 196; lisp: 77
file content (107 lines) | stat: -rw-r--r-- 2,730 bytes parent folder | download
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
// Mgmt
// Copyright (C) 2013-2024+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"context"
	"fmt"
	"log"
	"net"
	"os"

	"github.com/insomniacslk/dhcp/dhcpv4"
	"github.com/insomniacslk/dhcp/dhcpv4/nclient4"
)

const (
	iface   = "lo" // loopback for local testing
	address = "127.0.0.1"
)

func main() {
	if len(os.Args) < 2 || len(os.Args) > 3 {
		log.Printf("Usage: %s [port] <mac address>", os.Args[0])
		return
	}

	port := string(nclient4.ServerPort) // the default is 67
	if len(os.Args) >= 3 {
		port = os.Args[1]
	}
	hwAddr := os.Args[len(os.Args)-1] // argv[1]

	hw, err := net.ParseMAC(hwAddr)
	if err != nil {
		log.Printf("Invalid mac address: %v", err)
		return
	}

	addr := fmt.Sprintf("%s:%s", address, port)
	log.Printf("Connecting to: %s", addr)

	opts := []nclient4.ClientOpt{}
	{
		opt := nclient4.WithHWAddr(hw)
		opts = append(opts, opt)
	}
	{
		opt := nclient4.WithSummaryLogger()
		opts = append(opts, opt)
	}
	//{
	//	opt := nclient4.WithDebugLogger()
	//	opts = append(opts, opt)
	//}

	//c, err := nclient4.NewWithConn(conn net.PacketConn, ifaceHWAddr net.HardwareAddr, opts...)
	c, err := nclient4.New(iface, opts...)
	if err != nil {
		log.Printf("Error connecting to server: %v", err)
		return
	}
	defer func() {
		if err := c.Close(); err != nil {
			log.Printf("Error closing client: %v", err)
		}
	}()

	modifiers := []dhcpv4.Modifier{}
	//{
	//	mod := dhcpv4.WithYourIP(net.ParseIP(?))
	//	modifiers = append(modifiers, mod)
	//}
	//{
	//	mod := dhcpv4.WithClientIP(net.ParseIP(?))
	//	modifiers = append(modifiers, mod)
	//}
	// TODO: add modifiers

	log.Printf("Requesting...")
	ctx := context.Background()                // TODO: add to ^C handler
	lease, err := c.Request(ctx, modifiers...) // (lease *Lease, err error)
	if err != nil {
		log.Printf("Error requesting from server: %v", err)
		return
	}

	// Show the results of the D-O-R-A exchange.
	log.Printf("Offer: %+v", lease.Offer)
	log.Printf("Ack: %+v", lease.ACK)

	log.Printf("Done!")
}