Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
capture and inject on the same pcap handle
Browse files Browse the repository at this point in the history
...since there's no need to have separate handles

Also, make router.Start() perform all startup, including starting a
sniffer goroutine. The main goroutine is now kept alive by the signal
handling loop.
  • Loading branch information
rade committed Aug 19, 2014
1 parent b4635bc commit 1ef941e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
31 changes: 16 additions & 15 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func (router *Router) UsingPassword() bool {
}

func (router *Router) Start() {
handle := router.createPCap(true, 65535, router.BufSz)
router.ConnectionMaker = StartConnectionMaker(router)
router.Topology = StartTopology(router)
router.UDPListener = router.listenUDP(Port)
router.UDPListener = router.listenUDP(Port, handle)
router.listenTCP(Port)
router.sniff(handle)
}

func (router *Router) Status() string {
Expand All @@ -56,12 +58,9 @@ func (router *Router) Status() string {
return buf.String()
}

func (router *Router) Sniff() {
func (router *Router) sniff(handle *pcap.Handle) {
log.Println("Sniffing traffic on", router.Iface)

handle := router.createPCap(true, 65535, router.BufSz)
defer handle.Close()

dec := NewEthernetDecoder()

localAddrs, err := router.Iface.Addrs()
Expand All @@ -72,12 +71,15 @@ func (router *Router) Sniff() {
if router.Macs.Enter(mac, router.Ourself) {
log.Println("Discovered our MAC", mac)
}
for {
pkt, _, err := handle.ReadPacketData()
checkFatal(err)
router.LogFrame("Sniffed", pkt, nil)
checkWarn(router.handleCapturedPacket(pkt, dec, checkFrameTooBig))
}
go func() {
defer handle.Close()
for {
pkt, _, err := handle.ReadPacketData()
checkFatal(err)
router.LogFrame("Sniffed", pkt, nil)
checkWarn(router.handleCapturedPacket(pkt, dec, checkFrameTooBig))
}
}()
}

func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecoder, checkFrameTooBig func(error) error) error {
Expand Down Expand Up @@ -144,7 +146,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) {
NewLocalConnection(connRemote, UnknownPeerName, tcpConn, nil, router)
}

func (router *Router) listenUDP(localPort int) *net.UDPConn {
func (router *Router) listenUDP(localPort int, handle *pcap.Handle) *net.UDPConn {
localAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprint(":", localPort))
checkFatal(err)
conn, err := net.ListenUDP("udp4", localAddr)
Expand All @@ -156,13 +158,12 @@ func (router *Router) listenUDP(localPort int) *net.UDPConn {
// This one makes sure all packets we send out do not have DF set on them.
err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_MTU_DISCOVER, syscall.IP_PMTUDISC_DONT)
checkFatal(err)
go router.udpReader(conn)
go router.udpReader(conn, handle)
return conn
}

func (router *Router) udpReader(conn *net.UDPConn) {
func (router *Router) udpReader(conn *net.UDPConn, handle *pcap.Handle) {
defer conn.Close()
handle := router.createPCap(false, 0, 0)
dec := NewEthernetDecoder()
handleUDPPacket := router.handleUDPPacketFunc(dec, handle)
buf := make([]byte, MaxUDPPacketSize)
Expand Down
25 changes: 11 additions & 14 deletions weave/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func main() {

router := weave.NewRouter(iface, ourName, []byte(password), connLimit, bufSz*1024*1024, logFrame)
router.Start()
startSignalHandlers(router)
for _, peer := range peers {
targetPeer := peer
go func() {
Expand All @@ -124,23 +123,21 @@ func main() {
}
}()
}
router.Sniff()
handleSignals(router)
}

func startSignalHandlers(router *weave.Router) {
func handleSignals(router *weave.Router) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGQUIT, syscall.SIGUSR1)
buf := make([]byte, 1<<20)
go func() {
for {
sig := <-sigs
switch sig {
case syscall.SIGQUIT:
runtime.Stack(buf, true)
log.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf)
case syscall.SIGUSR1:
log.Printf("=== received SIGUSR1 ===\n*** status...\n%s\n*** end\n", router.Status())
}
for {
sig := <-sigs
switch sig {
case syscall.SIGQUIT:
runtime.Stack(buf, true)
log.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf)
case syscall.SIGUSR1:
log.Printf("=== received SIGUSR1 ===\n*** status...\n%s\n*** end\n", router.Status())
}
}()
}
}

0 comments on commit 1ef941e

Please sign in to comment.