<commit_before>package tcp

import (
	"fmt"
	"network/ipv4"
	"sync"

	"github.com/hsheth2/logs"
)

// Global src, dst port and ip registry for TCP binding
type portManagerType struct {
	tcpReader ipv4.Reader
	incoming  map[uint16](map[uint16](map[ipv4.Hash](chan *packet))) // dst, src port, remote ip
	lock      *sync.RWMutex
}

var portManager = func() *portManagerType { // TODO use an init function
	ipr, err := ipv4.NewReader(ipv4.IPAll, ipv4.IPProtoTCP)
	if err != nil {
		logs.Error.Println(err)
		return nil
	}

	m := &portManagerType{
		tcpReader: ipr,
		incoming:  make(map[uint16](map[uint16](map[ipv4.Hash](chan *packet)))),
		lock:      &sync.RWMutex{},
	}
	go m.readAll()
	return m
}()

func (m *portManagerType) bind(rport, lport uint16, ip *ipv4.Address) (chan *packet, error) {
	// race prevention
	m.lock.Lock()
	defer m.lock.Unlock()

	// lport is the local one here, rport is the remote
	//ch logs.Info.Println("Attempting to bind to rport", rport, "lport", lport, "ip", ip.Hash())
	if _, ok := m.incoming[lport]; !ok {
		m.incoming[lport] = make(map[uint16](map[ipv4.Hash](chan *packet)))
	}
<commit_msg>Get unused port function added to portmanager<commit_after>package tcp

import (
	"fmt"
	"network/ipv4"
	"sync"

	"github.com/hsheth2/logs"
)

// Global src, dst port and ip registry for TCP binding
type portManagerType struct {
	tcpReader ipv4.Reader
	incoming  map[uint16](map[uint16](map[ipv4.Hash](chan *packet))) // dst, src port, remote ip
	lock      *sync.RWMutex
}

func (m *portManagerType) getUnusedPort() (uint16, error) {
	for i := uint16(32768); i <= uint16(61000); i++ {
		for k := range m.incoming {
			if _, exists := incoming["foo"]; !exists {
				return i, nil
			}
		}
	}
	return nil, errors.New("No ports available to bind to")
}

var portManager = func() *portManagerType { // TODO use an init function
	ipr, err := ipv4.NewReader(ipv4.IPAll, ipv4.IPProtoTCP)
	if err != nil {
		logs.Error.Println(err)
		return nil
	}

	m := &portManagerType{
		tcpReader: ipr,
		incoming:  make(map[uint16](map[uint16](map[ipv4.Hash](chan *packet)))),
		lock:      &sync.RWMutex{},
	}
	go m.readAll()
	return m
}()

func (m *portManagerType) bind(rport, lport uint16, ip *ipv4.Address) (chan *packet, error) {
	// race prevention
	m.lock.Lock()
	defer m.lock.Unlock()

	// lport is the local one here, rport is the remote
	//ch logs.Info.Println("Attempting to bind to rport", rport, "lport", lport, "ip", ip.Hash())
	if _, ok := m.incoming[lport]; !ok {
		m.incoming[lport] = make(map[uint16](map[ipv4.Hash](chan *packet)))
	}

{'fraction_non_alphanumeric': 0.15670183665494333, 'fraction_numerical': 0.023446658851113716, 'mean_word_length': 3.970873786407767, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3056744', 'n_tokens_mistral': 1019, 'n_tokens_neox': 988, 'n_words': 253}