io.netflow.lib.FlowPacket.scala Maven / Gradle / Ivy
The newest version!
package io.netflow.lib
import java.net.{InetAddress, InetSocketAddress}
import java.time.{LocalDateTime, ZoneId}
import java.util.UUID
import io.netty.buffer.ByteBuf
trait Flow[T] {
def id: UUID
def version: String
def length: Int
def sender: InetSocketAddress
def senderIP: String = sender.getAddress.getHostAddress
def senderPort: Int = sender.getPort
}
/**
* Unified interface for UDP Packets generated by NetFlow and sFlow.
*/
trait FlowPacket {
def id: UUID
def version: String
def length: Int
def sender: InetSocketAddress
def senderIP: String = sender.getAddress.getHostAddress
def senderPort: Int = sender.getPort
def timestamp: LocalDateTime
def count: Int
def flows: List[Flow[_]]
}
/**
* Unified interface for accounting statistics (InetAddress and Networks) generated by NetFlow.
*/
trait NetFlowData[T] extends Flow[T] {
this: T with Flow[T] =>
def srcPort: Int
def dstPort: Int
def srcAS: Option[Int]
def dstAS: Option[Int]
def srcAddress: InetAddress
def dstAddress: InetAddress
def nextHop: Option[InetAddress]
def pkts: Long
def bytes: Long
def proto: Int
def tos: Int
def tcpflags: Int
def start: Option[LocalDateTime]
def stop: Option[LocalDateTime]
def duration: Option[Long] =
for { sto <- stop; sta <- start } yield sto.atZone(ZoneId.systemDefault()).toInstant.toEpochMilli - sta
.atZone(ZoneId.systemDefault())
.toInstant
.toEpochMilli
private lazy val nextHopIP = nextHop.map(_.getHostAddress)
protected def stringExtra = ""
override def toString: String =
"%s reported by %s:%s containing %s:%s%s -> %s -> %s:%s%s Proto %s - ToS %s - %s pkts - %s bytes %s".format(
version,
senderIP,
senderPort,
srcAddress.getHostAddress,
srcPort,
srcAS.filter(_ == 0).map(" (" + _ + ")").getOrElse(""),
nextHopIP.getOrElse("direct"),
dstAddress.getHostAddress,
dstPort,
dstAS.filter(_ == 0).map(" (" + _ + ")").getOrElse(""),
proto,
tos,
pkts,
bytes,
stringExtra
)
}
/**
* Wrapper class for NetFlow Bytes
* @param sender Sender Socket Address
* @param msg Netty ByteBuf
*/
case class NetFlow(sender: InetSocketAddress, msg: ByteBuf)