Skip to content

Commit 7f7ef71

Browse files
authored
Merge pull request #90 from synfinatic/no-listen
add --no-listen to disable listening on udp ports
2 parents 34b9e18 + b18fd1c commit 7f7ef71

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

cmd/listen.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import (
1818
log "github.com/sirupsen/logrus"
1919
)
2020

21-
const SendBufferSize = 100
21+
const (
22+
SEND_BUFFER_SIZE = 100
23+
MAX_PACKET_SIZE = 8192
24+
)
2225

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

422-
const MAX_PACKET_SIZE = 8192
423-
424425
// SinkUdpPackets opens a UDP socket for broadcast packets and sends them to /dev/null
426+
// creates a go-routine for each interface/port combo so we don't block
425427
func (l *Listen) SinkUdpPackets() error {
426428
addrs, err := l.netif.Addrs()
427429
if err != nil {
@@ -431,6 +433,7 @@ func (l *Listen) SinkUdpPackets() error {
431433
for _, addr := range addrs {
432434
addrs := addr.String()
433435

436+
// skip anything that doesn't look like a unicast IPv4 address
434437
if addrs == "0.0.0.0" || addrs == "" || strings.Contains(addrs, ":") {
435438
continue
436439
}
@@ -449,9 +452,10 @@ func (l *Listen) SinkUdpPackets() error {
449452
if err := conn.SetReadBuffer(MAX_PACKET_SIZE); err != nil {
450453
return err
451454
}
455+
452456
go func() {
457+
buff := make([]byte, MAX_PACKET_SIZE)
453458
for {
454-
buff := make([]byte, MAX_PACKET_SIZE)
455459
_, _, err := conn.ReadFromUDP(buff)
456460
if err != nil {
457461
log.WithError(err).Warnf("Unable to read broadcast packet")

cmd/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type CLI struct {
3131
PcapPath string `kong:"short='d',default='/root',help='Directory to write debug pcap files'"`
3232
ListInterfaces bool `kong:"short='l',help='List available interfaces and exit'"`
3333
Version bool `kong:"short='v',help='Print version information'"`
34+
NoListen bool `kong:"help='Do not actively listen on UDP port(s)'"`
3435
}
3536

3637
func init() {
@@ -160,9 +161,11 @@ func main() {
160161
}
161162

162163
// Sink broadcast messages
163-
for _, l := range listeners {
164-
if err := l.SinkUdpPackets(); err != nil {
165-
log.WithError(err).Fatalf("Unable to init SinkUdpPackets")
164+
if !cli.NoListen {
165+
for _, l := range listeners {
166+
if err := l.SinkUdpPackets(); err != nil {
167+
log.WithError(err).Fatalf("Unable to init SinkUdpPackets")
168+
}
166169
}
167170
}
168171

0 commit comments

Comments
 (0)