<commit_before>package dhcp4

import (
	"net"
)

type Handler interface {
	ServeDHCP(req Packet, msgType MessageType, options Options) Packet
}

// A Server defines parameters for running a DHCP server.
type Server struct {
	Handler  Handler
	ServerIP net.IP // Used to bind to interface to send broadcast packets from
}

// ListenAndServe listens on the UDP network address s.Addr and then
// calls Serve to handle requests on incoming packets.  If
// s.Addr is blank, ":67" is used.
func (s *Server) ListenAndServe() error {
	l, err := net.ListenPacket("udp4", ":67")
	if err != nil {
		return err
	}
	defer l.Close()
	return s.Serve(l, 68)
}

func (s *Server) Serve(l net.PacketConn, replyPort int) error {
	var srcAddr *net.UDPAddr
	if s.ServerIP != nil {
		srcAddr = &net.UDPAddr{IP: s.ServerIP}
	}
	buffer := make([]byte, 1500)
	for {
		n, _, err := l.ReadFrom(buffer)
		if err != nil {
			return err
		}
		if n < 240 {
			continue
		}
		p := Packet(buffer[:n])
		options := p.ParseOptions()
		msgType := options[OptionDHCPMessageType]
		if len(msgType) != 1 {
			return nil
		}

		// TODO consider more packet validity checks
		if res := s.Handler.ServeDHCP(p, MessageType(msgType[0]), options); res != nil {
			dstIP := net.IPv4(255, 255, 255, 255)
			/*if !p.Broadcast() {
				dstIP = net.IP(p.CIAddr())
			}*/
			r, err := net.DialUDP("udp", srcAddr, &net.UDPAddr{IP: dstIP, Port: replyPort})
			if err == nil {
				defer r.Close()
				r.Write(res)
			}

		}
	}
	return nil
}

// ListenAndServe listens on the UDP network address addr
// and then calls Serve with handler to handle requests
// on incoming packets.<commit_msg>Fix/Stop multiple dials that never close<commit_after>package dhcp4

import (
	"fmt"
	"net"
)

type Handler interface {
	ServeDHCP(req Packet, msgType MessageType, options Options) Packet
}

// A Server defines parameters for running a DHCP server.
type Server struct {
	Handler  Handler
	ServerIP net.IP // Used to bind to interface to send broadcast packets from
}

// ListenAndServe listens on the UDP network address s.Addr and then
// calls Serve to handle requests on incoming packets.  If
// s.Addr is blank, ":67" is used.
func (s *Server) ListenAndServe() error {
	l, err := net.ListenPacket("udp4", ":67")
	if err != nil {
		return err
	}
	defer l.Close()
	return s.Serve(l, 68)
}

func (s *Server) Serve(l net.PacketConn, replyPort int) error {
	var srcAddr *net.UDPAddr
	if s.ServerIP != nil {
		srcAddr = &net.UDPAddr{IP: s.ServerIP}
	}
	buffer := make([]byte, 1500)
	r, err := net.DialUDP("udp", srcAddr, &net.UDPAddr{IP: net.IPv4(255, 255, 255, 255), Port: replyPort})
	if err != nil {
		return err
	}
	defer r.Close()
	for {
		n, _, err := l.ReadFrom(buffer)
		if err != nil {
			return err
		}
		if n < 240 {
			continue
		}
		p := Packet(buffer[:n])
		options := p.ParseOptions()
		msgType := options[OptionDHCPMessageType]
		if len(msgType) != 1 {
			return nil
		}
		// TODO consider more packet validity checks
		if res := s.Handler.ServeDHCP(p, MessageType(msgType[0]), options); res != nil {
			if _, e := r.Write(res); e != nil {
				fmt.Println("Write Error:", e.Error())
			}
		}
	}
	return nil
}

// ListenAndServe listens on the UDP network address addr
// and then calls Serve with handler to handle requests
// on incoming packets.
{'fraction_non_alphanumeric': 0.12860175917500757, 'fraction_numerical': 0.018198362147406732, 'mean_word_length': 3.3394736842105264, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3331034', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1142, 'n_words': 380}