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

Commit

Permalink
refactor: don't pass around router.Ourself
Browse files Browse the repository at this point in the history
  • Loading branch information
rade committed Aug 19, 2014
1 parent 3116602 commit b4635bc
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,25 @@ func (router *Router) Sniff() {
handle := router.createPCap(true, 65535, router.BufSz)
defer handle.Close()

ourself := router.Ourself
dec := NewEthernetDecoder()

localAddrs, err := router.Iface.Addrs()
checkFatal(err)
checkFrameTooBig := dec.CheckFrameTooBigFunc(router.Iface.HardwareAddr, localAddrs, handle)

mac := router.Iface.HardwareAddr
if router.Macs.Enter(mac, ourself) {
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, ourself, checkFrameTooBig))
checkWarn(router.handleCapturedPacket(pkt, dec, checkFrameTooBig))
}
}

func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecoder, ourself *Peer, checkFrameTooBig func(error) error) error {
func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecoder, checkFrameTooBig func(error) error) error {
dec.DecodeLayers(frameData)
decodedLen := len(dec.decoded)
if decodedLen == 0 {
Expand All @@ -92,18 +91,18 @@ func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecode
// We need to filter out frames we injected ourselves. For such
// frames, the srcMAC will have been recorded as associated with a
// different peer.
if found && srcPeer != ourself {
if found && srcPeer != router.Ourself {
return nil
}
if router.Macs.Enter(srcMac, ourself) {
if router.Macs.Enter(srcMac, router.Ourself) {
log.Println("Discovered local MAC", srcMac)
}
if dec.DropFrame() {
return nil
}
dstMac := dec.eth.DstMAC
dstPeer, found := router.Macs.Lookup(dstMac)
if found && dstPeer == ourself {
if found && dstPeer == router.Ourself {
return nil
}
df := decodedLen == 2 && (dec.ip.Flags&layers.IPv4DontFragment != 0)
Expand All @@ -113,9 +112,9 @@ func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecode
router.LogFrame("Forwarding", frameData, &dec.eth)
}
if !found || dec.BroadcastFrame() {
return checkFrameTooBig(ourself.Broadcast(df, frameData, dec))
return checkFrameTooBig(router.Ourself.Broadcast(df, frameData, dec))
} else {
return checkFrameTooBig(ourself.Forward(dstPeer, df, frameData, dec))
return checkFrameTooBig(router.Ourself.Forward(dstPeer, df, frameData, dec))
}
}

Expand Down

0 comments on commit b4635bc

Please sign in to comment.