<commit_before>package ipv4

import (
	"network/ethernet"
	"network/ipv4/arpv4"
	"network/ipv4/ipv4src"
	"network/ipv4/ipv4tps"

	"golang.org/x/net/ipv4"
)

type IP_Writer struct {
	nw          *ethernet.Network_Writer
	version     uint8
	dst, src    *ipv4tps.IPaddress
	headerLen   uint16
	ttl         uint8
	protocol    uint8
	identifier  uint16
	maxFragSize uint16
}

func NewIP_Writer(dst *ipv4tps.IPaddress, protocol uint8) (*IP_Writer, error) {
	// create its own network_writer
	nw, err := ethernet.NewNetwork_Writer()
	if err != nil {
		return nil, err
	}

	/*dstIPAddr, err := net.ResolveIPAddr("ip", dst)
	if err != nil {
		//fmt.Println(err)
		return nil, err
	}
	fmt.Println("Full Address: ", dstIPAddr)*/

	/*err = syscall.Connect(fd, addr)
	if err != nil {
		return nil, errors.New("Failed to connect.")
	}*/

	return &IP_Writer{
		//fd:          fd,
		//sockAddr:    addr,
		nw:          nw,
		version:     ipv4.Version,
		headerLen:   IP_HEADER_LEN,
		dst:         dst,
		src:         ipv4src.GlobalSource_IP_Table.Query(dst),
		ttl:         DEFAULT_TTL,
		protocol:    protocol,
		identifier:  20000, // TODO generate this properly
		maxFragSize: MTU,   // TODO determine this dynamically with LLDP
	}, nil
}

func (ipw *IP_Writer) WriteTo(p []byte) error {
	//logs.Trace.Println("IP Preparing to Write:", p)
	//	logs.Info.Println("IPv4 WriteTo request")

	header := make([]byte, ipw.headerLen)
	header[0] = (byte)((ipw.version << 4) + (uint8)(ipw.headerLen/4)) // Version, IHL
	header[1] = 0
	id := ipw.identifier
	header[4] = byte(id >> 8) // Identification
	header[5] = byte(id)
	ipw.identifier++
	header[8] = (byte)(ipw.ttl)      // Time to Live
	header[9] = (byte)(ipw.protocol) // Protocol

	// Src and Dst IPs
	//fmt.Println(srcIP)
	//fmt.Println(srcIP[12])
	//fmt.Println(srcIP[13])
	//fmt.Println(srcIP[14])
	//fmt.Println(srcIP[15])
	//fmt.Println(dstIP)
	header[12] = ipw.src.IP[0]
	header[13] = ipw.src.IP[1]
	header[14] = ipw.src.IP[2]<commit_msg>Fixed another data race<commit_after>package ipv4

import (
	"network/ethernet"
	"network/ipv4/arpv4"
	"network/ipv4/ipv4src"
	"network/ipv4/ipv4tps"

	"golang.org/x/net/ipv4"
)

type IP_Writer struct {
	nw          *ethernet.Network_Writer
	version     uint8
	dst, src    *ipv4tps.IPaddress
	headerLen   uint16
	ttl         uint8
	protocol    uint8
	identifier  uint16
	maxFragSize uint16
}

func NewIP_Writer(dst *ipv4tps.IPaddress, protocol uint8) (*IP_Writer, error) {
	// create its own network_writer
	nw, err := ethernet.NewNetwork_Writer()
	if err != nil {
		return nil, err
	}

	/*dstIPAddr, err := net.ResolveIPAddr("ip", dst)
	if err != nil {
		//fmt.Println(err)
		return nil, err
	}
	fmt.Println("Full Address: ", dstIPAddr)*/

	/*err = syscall.Connect(fd, addr)
	if err != nil {
		return nil, errors.New("Failed to connect.")
	}*/

	return &IP_Writer{
		//fd:          fd,
		//sockAddr:    addr,
		nw:          nw,
		version:     ipv4.Version,
		headerLen:   IP_HEADER_LEN,
		dst:         dst,
		src:         ipv4src.GlobalSource_IP_Table.Query(dst),
		ttl:         DEFAULT_TTL,
		protocol:    protocol,
		identifier:  20000, // TODO generate this properly
		maxFragSize: MTU,   // TODO determine this dynamically with LLDP
	}, nil
}

func (ipw *IP_Writer) getID() uint16 {
	id := ipw.identifier
	ipw.identifier++
	return id
}

func (ipw *IP_Writer) WriteTo(p []byte) error {
	//logs.Trace.Println("IP Preparing to Write:", p)
	//	logs.Info.Println("IPv4 WriteTo request")

	header := make([]byte, ipw.headerLen)
	header[0] = (byte)((ipw.version << 4) + (uint8)(ipw.headerLen/4)) // Version, IHL
	header[1] = 0
	id := ipw.getID()
	header[4] = byte(id >> 8) // Identification
	header[5] = byte(id)
	header[8] = (byte)(ipw.ttl)      // Time to Live
	header[9] = (byte)(ipw.protocol) // Protocol

	// Src and Dst IPs
	//fmt.Println(srcIP)
	//fmt.Println(srcIP[12])
	//fmt.Println(srcIP[13])
	//fmt.Println(srcIP[14])
	//fmt.Println(srcIP[15])
	//fmt.Println(dstIP)
	header[12] = ipw.src.IP[0]
	header[13] = ipw.src.IP[1]
	header[14] = ipw.src.IP[2]
{'fraction_non_alphanumeric': 0.15460852329038652, 'fraction_numerical': 0.028245787908820614, 'mean_word_length': 3.1362704918032787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5282420', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1619, 'n_words': 343}