server4

package
v0.0.0-...-6ff4a6d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package server4 is a basic, extensible DHCPv4 server.

To use the DHCPv4 server code you have to call NewServer with two arguments:

  • an interface to listen on,
  • an address to listen on, and
  • a handler function, that will be called every time a valid DHCPv4 packet is received.

The address to listen on is used to know IP address, port and optionally the scope to create and UDP socket to listen on for DHCPv4 traffic.

The handler is a function that takes as input a packet connection, that can be used to reply to the client; a peer address, that identifies the client sending the request, and the DHCPv4 packet itself. Just implement your custom logic in the handler.

Optionally, NewServer can receive options that will modify the server object. Some options already exist, for example WithConn. If this option is passed with a valid connection, the listening address argument is ignored.

Example program:

package main

import (
	"log"
	"net"

	"github.com/csystem-it/dhcp/dhcpv4"
	"github.com/csystem-it/dhcp/dhcpv4/server4"
)

func handler(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4) {
	// this function will just print the received DHCPv4 message, without replying
	log.Print(m.Summary())
}

func main() {
	laddr := net.UDPAddr{
		IP:   net.ParseIP("127.0.0.1"),
		Port: 67,
	}
	server, err := server4.NewServer("eth0", &laddr, handler)
	if err != nil {
		log.Fatal(err)
	}

	// This never returns. If you want to do other stuff, dump it into a
	// goroutine.
	server.Serve()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewIPv4UDPConn

func NewIPv4UDPConn(iface string, addr *net.UDPAddr) (*net.UDPConn, error)

NewIPv4UDPConn returns a UDP connection bound to both the interface and port given based on a IPv4 DGRAM socket. The UDP connection allows broadcasting.

The interface must already be configured.

Types

type DebugLogger

type DebugLogger struct {
	// Printfer is used for actual output of the logger
	Printfer
}

DebugLogger is a wrapper for Printfer to implement interface Logger. DHCP messages are printed in the long format.

func (DebugLogger) PrintMessage

func (d DebugLogger) PrintMessage(prefix string, message *dhcpv4.DHCPv4)

PrintMessage prints a DHCP message in the long format via predefined Printfer

func (DebugLogger) Printf

func (d DebugLogger) Printf(format string, v ...interface{})

Printf prints a log message as-is via predefined Printfer

type EmptyLogger

type EmptyLogger struct{}

EmptyLogger prints nothing

func (EmptyLogger) PrintMessage

func (e EmptyLogger) PrintMessage(prefix string, message *dhcpv4.DHCPv4)

PrintMessage is just a dummy function that does nothing

func (EmptyLogger) Printf

func (e EmptyLogger) Printf(format string, v ...interface{})

Printf is just a dummy function that does nothing

type Handler

type Handler func(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4)

Handler is a type that defines the handler function to be called every time a valid DHCPv4 message is received

type Logger

type Logger interface {
	// PrintMessage print _all_ DHCP messages
	PrintMessage(prefix string, message *dhcpv4.DHCPv4)

	// Printf is use to print the rest debugging information
	Printf(format string, v ...interface{})
}

Logger is a handler which will be used to output logging messages

type Printfer

type Printfer interface {
	// Printf is the function for logging output. Arguments are handled in the manner of fmt.Printf.
	Printf(format string, v ...interface{})
}

Printfer is used for actual output of the logger. For example *log.Logger is a Printfer.

type Server

type Server struct {
	Handler Handler
	// contains filtered or unexported fields
}

Server represents a DHCPv4 server object

func NewServer

func NewServer(ifname string, addr *net.UDPAddr, handler Handler, opt ...ServerOpt) (*Server, error)

NewServer initializes and returns a new Server object

func (*Server) Close

func (s *Server) Close() error

Close sends a termination request to the server, and closes the UDP listener.

func (*Server) Serve

func (s *Server) Serve() error

Serve serves requests.

type ServerOpt

type ServerOpt func(s *Server)

ServerOpt adds optional configuration to a server.

func WithConn

func WithConn(c net.PacketConn) ServerOpt

WithConn configures the server with the given connection.

func WithDebugLogger

func WithDebugLogger() ServerOpt

WithDebugLogger logs multi-line full DHCPv4 messages when sent & received.

func WithLogger

func WithLogger(newLogger Logger) ServerOpt

WithLogger set the logger (see interface Logger).

func WithSummaryLogger

func WithSummaryLogger() ServerOpt

WithSummaryLogger logs one-line DHCPv4 message summaries when sent & received.

type ShortSummaryLogger

type ShortSummaryLogger struct {
	// Printfer is used for actual output of the logger
	Printfer
}

ShortSummaryLogger is a wrapper for Printfer to implement interface Logger. DHCP messages are printed in the short format.

func (ShortSummaryLogger) PrintMessage

func (s ShortSummaryLogger) PrintMessage(prefix string, message *dhcpv4.DHCPv4)

PrintMessage prints a DHCP message in the short format via predefined Printfer

func (ShortSummaryLogger) Printf

func (s ShortSummaryLogger) Printf(format string, v ...interface{})

Printf prints a log message as-is via predefined Printfer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL