Skip to content

Commit

Permalink
Merge pull request #90 from synfinatic/no-listen
Browse files Browse the repository at this point in the history
add --no-listen to disable listening on udp ports
  • Loading branch information
synfinatic authored Apr 14, 2022
2 parents 34b9e18 + b18fd1c commit 7f7ef71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 9 additions & 5 deletions cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
log "github.com/sirupsen/logrus"
)

const SendBufferSize = 100
const (
SEND_BUFFER_SIZE = 100
MAX_PACKET_SIZE = 8192
)

// Struct containing everything for an interface
type Listen struct {
Expand Down Expand Up @@ -93,7 +96,7 @@ func newListener(netif *net.Interface, promisc bool, ports []int32, to time.Dura
timeout: to,
promisc: promisc,
handle: nil,
sendpkt: make(chan Send, SendBufferSize),
sendpkt: make(chan Send, SEND_BUFFER_SIZE),
clients: clients,
}
log.Debugf("Listen: %s", spew.Sdump(new))
Expand Down Expand Up @@ -419,9 +422,8 @@ func isValidLayerType(layertype layers.LinkType) bool {
return false
}

const MAX_PACKET_SIZE = 8192

// SinkUdpPackets opens a UDP socket for broadcast packets and sends them to /dev/null
// creates a go-routine for each interface/port combo so we don't block
func (l *Listen) SinkUdpPackets() error {
addrs, err := l.netif.Addrs()
if err != nil {
Expand All @@ -431,6 +433,7 @@ func (l *Listen) SinkUdpPackets() error {
for _, addr := range addrs {
addrs := addr.String()

// skip anything that doesn't look like a unicast IPv4 address
if addrs == "0.0.0.0" || addrs == "" || strings.Contains(addrs, ":") {
continue
}
Expand All @@ -449,9 +452,10 @@ func (l *Listen) SinkUdpPackets() error {
if err := conn.SetReadBuffer(MAX_PACKET_SIZE); err != nil {
return err
}

go func() {
buff := make([]byte, MAX_PACKET_SIZE)
for {
buff := make([]byte, MAX_PACKET_SIZE)
_, _, err := conn.ReadFromUDP(buff)
if err != nil {
log.WithError(err).Warnf("Unable to read broadcast packet")
Expand Down
9 changes: 6 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type CLI struct {
PcapPath string `kong:"short='d',default='/root',help='Directory to write debug pcap files'"`
ListInterfaces bool `kong:"short='l',help='List available interfaces and exit'"`
Version bool `kong:"short='v',help='Print version information'"`
NoListen bool `kong:"help='Do not actively listen on UDP port(s)'"`
}

func init() {
Expand Down Expand Up @@ -160,9 +161,11 @@ func main() {
}

// Sink broadcast messages
for _, l := range listeners {
if err := l.SinkUdpPackets(); err != nil {
log.WithError(err).Fatalf("Unable to init SinkUdpPackets")
if !cli.NoListen {
for _, l := range listeners {
if err := l.SinkUdpPackets(); err != nil {
log.WithError(err).Fatalf("Unable to init SinkUdpPackets")
}
}
}

Expand Down

0 comments on commit 7f7ef71

Please sign in to comment.