<commit_before>package dhcp

import (
	dhcp "github.com/krolaw/dhcp4"
	"github.com/mistifyio/mistify-agent/client"
	"github.com/mistifyio/mistify-agent/core"
	"github.com/mistifyio/mistify-agent/log"
	"net"
	"os"
	"time"
)

type Server struct {
	client *client.Client
}

func NewServer(endpoint string) *Server {
	server := &Server{}
	server.client = client.NewClient(endpoint)

	return server
}

func (s *Server) Run() {
	log.Info("Starting DHCP server, agent address is %s", s.client.Endpoint)

	err := dhcp.ListenAndServe(s)
	if err != nil {
		log.Error(err)
		os.Exit(1)
	}
}

func (s *Server) ServeDHCP(packet dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) dhcp.Packet {
	var replyType dhcp.MessageType
	var logMessage string

	switch msgType {
	case dhcp.Discover:
		replyType = dhcp.Offer
		logMessage = "Discover"
	case dhcp.Request:
		replyType = dhcp.ACK
		logMessage = "Request"
	default:
		return nil
	}

	mac := packet.CHAddr().String()

	log.Info("dhcp.%s: %+v\n", logMessage, mac)

	nic, err := s.getNic(mac)
	if err != nil {
		log.Error("Couldn't get NIC for MAC address %s: %s", mac, err.Error())
		return nil
	}

	log.Info("%+v\n", nic)

	replyOpts := dhcp.Options{
		dhcp.OptionRouter:           net.ParseIP(nic.Gateway).To4(),
		dhcp.OptionDomainNameServer: net.IP{8, 8, 8, 8},
		dhcp.OptionSubnetMask:       net.ParseIP(nic.Netmask).To4(),
	}

	reply := dhcp.ReplyPacket(packet, replyType, net.IP{0, 0, 0, 0}, net.ParseIP(nic.Address).To4(), time.Hour*24*7, replyOpts.SelectOrderOrAll(replyOpts[dhcp.OptionParameterRequestList]))
	return reply
}

func (server *Server) getNic(mac string) (*core.Nic, error) {
	guests, err := server.client.ListGuests()
	if err != nil {
		return nil, err
	}<commit_msg>MIST-137: listen on a specific interface<commit_after>package dhcp

import (
	dhcp "github.com/krolaw/dhcp4"
	"github.com/mistifyio/mistify-agent/client"
	"github.com/mistifyio/mistify-agent/core"
	"github.com/mistifyio/mistify-agent/log"
	"net"
	"os"
	"time"
)

type Server struct {
	client *client.Client
	iface  string
}

func NewServer(endpoint string, iface string) *Server {
	server := &Server{}
	server.client = client.NewClient(endpoint)
	server.iface = iface

	return server
}

func (s *Server) Run() {
	log.Info("Starting DHCP server on %s, agent address is %s\n", s.iface, s.client.Endpoint)
	err := dhcp.ListenAndServeIf(s.iface, s)
	if err != nil {
		log.Error(err)
		os.Exit(1)
	}
}

func (s *Server) ServeDHCP(packet dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options) dhcp.Packet {
	var replyType dhcp.MessageType
	var logMessage string

	switch msgType {
	case dhcp.Discover:
		replyType = dhcp.Offer
		logMessage = "Discover"
	case dhcp.Request:
		replyType = dhcp.ACK
		logMessage = "Request"
	default:
		return nil
	}

	mac := packet.CHAddr().String()

	log.Info("dhcp.%s: %+v\n", logMessage, mac)

	nic, err := s.getNic(mac)
	if err != nil {
		log.Error("Couldn't get NIC for MAC address %s: %s", mac, err.Error())
		return nil
	}

	log.Info("Returning IP <%s> for MAC address <%s>\n", nic.Address, mac)

	replyOpts := dhcp.Options{
		dhcp.OptionRouter:           net.ParseIP(nic.Gateway).To4(),
		dhcp.OptionDomainNameServer: net.IP{8, 8, 8, 8},
		dhcp.OptionSubnetMask:       net.ParseIP(nic.Netmask).To4(),
	}

	reply := dhcp.ReplyPacket(packet, replyType, net.IPv4zero, net.ParseIP(nic.Address).To4(), time.Hour*24*7, replyOpts.SelectOrderOrAll(replyOpts[dhcp.OptionParameterRequestList]))
	return reply
}

func (server *Server) getNic(mac string) (*core.Nic, error) {
	guests, err := server.client.ListGuests()
	if err != nil {
		return nil, err
	}
{'fraction_non_alphanumeric': 0.14810721193699916, 'fraction_numerical': 0.008842221608179055, 'mean_word_length': 4.149359886201991, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5781067', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1383, 'n_words': 305}