From 66f71d33d516e2a7b16153173f55f9a251e6c553 Mon Sep 17 00:00:00 2001 From: agadiyar Date: Thu, 13 Jun 2019 16:44:45 -0500 Subject: [PATCH] new example decrementTTL --- examples/.gitignore | 1 + examples/Dockerfile | 1 + examples/Makefile | 3 ++- examples/decrementTTL.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 examples/decrementTTL.go diff --git a/examples/.gitignore b/examples/.gitignore index f3341d52..eda6c703 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -14,3 +14,4 @@ devbind OSforwarding generate jumbo +decrementTTL diff --git a/examples/Dockerfile b/examples/Dockerfile index 8a44e6da..4d612694 100644 --- a/examples/Dockerfile +++ b/examples/Dockerfile @@ -22,3 +22,4 @@ COPY netlink . COPY generate . COPY OSforwarding . COPY jumbo . +COPY decrementTTL . diff --git a/examples/Makefile b/examples/Makefile index b41703ab..3aaaef21 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -7,7 +7,8 @@ IMAGENAME = nff-go-examples EXECUTABLES = dump clonablePcapDumper kni copy errorHandling timer \ createPacket sendFixedPktsNumber gtpu pingReplay \ netlink gopacketParserExample devbind generate \ - OSforwarding jumbo + OSforwarding jumbo decrementTTL + SUBDIRS = tutorial antiddos demo fileReadWrite firewall forwarding ipsec lb .PHONY: dpi nffPktgen diff --git a/examples/decrementTTL.go b/examples/decrementTTL.go new file mode 100644 index 00000000..cd2708a9 --- /dev/null +++ b/examples/decrementTTL.go @@ -0,0 +1,40 @@ +// Copyright 2017 Intel Corporation. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "flag" + + "github.com/intel-go/nff-go/flow" + "github.com/intel-go/nff-go/packet" +) + +// Main function for constructing packet processing graph. +func main() { + inPort := flag.Uint("inPort", 0, "port for receiver") + outPort := flag.Uint("outPort", 1, "port for sender") + flag.Parse() + + flow.SystemInit(nil) + inputFlow, _ := flow.SetReceiver(uint16(*inPort)) + flow.SetHandlerDrop(inputFlow, decrementTTL, nil) + flow.SetSender(inputFlow, uint16(*outPort)) + flow.SystemStart() +} + +func decrementTTL(current *packet.Packet, c flow.UserContext) bool { + current.ParseL3() // must parse before header can be read + header := current.GetIPv4() + if header == nil { // not IPv4 + return false + } + + header.TimeToLive-- + if header.TimeToLive == 0 { // TTL exceeded, drop + return false + } else { + return true + } +}