Skip to content

Instantly share code, notes, and snippets.

@fortitudepub
Created December 23, 2016 08:51
Show Gist options
  • Save fortitudepub/2baf1bb8f06c5956e701e837d276928c to your computer and use it in GitHub Desktop.
Save fortitudepub/2baf1bb8f06c5956e701e837d276928c to your computer and use it in GitHub Desktop.
Scala actor with pcap thread send packet to itself.
package com.netease.cns.proton.agent.dhcp
import akka.actor.{Actor, ActorLogging}
import com.netease.cns.proton.agent.dhcp.DHCPPktRxActor.PcapRxPacket
import org.pcap4j.core.PcapNetworkInterface.PromiscuousMode
import org.pcap4j.core.Pcaps
import org.pcap4j.packet.Packet
object DHCPPktRxActor {
// TODO: define a packet which will be processed by each NetworkDHCPActor.
case class PcapRxPacket(pkt: Packet)
}
class DHCPPktRxActor(dummyInterface: String) extends Actor with ActorLogging {
// Start a thread which capture packet on dummy interface and turn to raw event
// to us.
val snapLen = 65536;
val mode = PromiscuousMode.PROMISCUOUS
val timeout = 10;
val intf = Pcaps.getDevByName(dummyInterface)
val handle = intf.openLive(snapLen, mode, timeout)
val thread = new Thread(new Runnable {
override def run(): Unit = {
println(s"handle blocking mode is ${handle.getBlockingMode().toString}")
while (true) {
val packet = handle.getNextPacketEx()
self ! new PcapRxPacket(packet)
}
}
})
thread.start()
override def receive: Receive = {
case PcapRxPacket(pkt) =>
log.debug(s"received dhcp packet ${pkt.toString}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment