github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/internal/enforcer/utils/packetgen/interfaces.go (about) 1 package packetgen 2 3 import ( 4 "github.com/google/gopacket" 5 "github.com/google/gopacket/layers" 6 ) 7 8 //PacketFlowType type for different types of flows 9 type PacketFlowType uint8 10 11 const ( 12 //PacketFlowTypeGenerateGoodFlow is used to generate a good floe 13 PacketFlowTypeGenerateGoodFlow PacketFlowType = iota 14 //PacketFlowTypeGoodFlowTemplate will have a good flow from a hardcoded template 15 PacketFlowTypeGoodFlowTemplate 16 //PacketFlowTypeMultipleGoodFlow will have two flows 17 PacketFlowTypeMultipleGoodFlow 18 //PacketFlowTypeMultipleIntervenedFlow will have two flows intervened to eachothers 19 PacketFlowTypeMultipleIntervenedFlow 20 ) 21 22 //EthernetPacketManipulator interface is used to create/manipulate Ethernet packet 23 type EthernetPacketManipulator interface { 24 //Used to create an Ethernet layer 25 AddEthernetLayer(srcMACstr string, dstMACstr string) error 26 //Used to return Ethernet packet created 27 GetEthernetPacket() layers.Ethernet 28 } 29 30 //IPPacketManipulator interface is used to create/manipulate IP packet 31 type IPPacketManipulator interface { 32 //Used to create an IP layer 33 AddIPLayer(srcIPstr string, dstIPstr string) error 34 //Used to return IP packet created 35 GetIPPacket() layers.IPv4 36 //Used to return IP checksum 37 GetIPChecksum() uint16 38 } 39 40 //TCPPacketManipulator interface is used to create/manipulate TCP packet 41 type TCPPacketManipulator interface { 42 //Used to create a TCP layer 43 AddTCPLayer(srcPort layers.TCPPort, dstPort layers.TCPPort) error 44 //Used to return TCP packet 45 GetTCPPacket() layers.TCP 46 //Used to return TCP Sequence number 47 GetTCPSequenceNumber() uint32 48 //Used to return TCP Acknowledgement number number 49 GetTCPAcknowledgementNumber() uint32 50 //Used to return TCP window 51 GetTCPWindow() uint16 52 //Used to return TCP Syn flag 53 GetTCPSyn() bool 54 //Used to return TCP Ack flag 55 GetTCPAck() bool 56 //Used to return TCP Fin flag 57 GetTCPFin() bool 58 //Used to return TCP Checksum 59 GetTCPChecksum() uint16 60 //Used to set TCP Sequence number 61 SetTCPSequenceNumber(seqNum uint32) 62 //Used to set TCP Acknowledgement number 63 SetTCPAcknowledgementNumber(ackNum uint32) 64 //Used to set TCP Window 65 SetTCPWindow(window uint16) 66 //Used to set TCP Syn flag to true 67 SetTCPSyn() 68 //Used to set TCP Syn and Ack flag to true 69 SetTCPSynAck() 70 //Used to set TCP Ack flag to true 71 SetTCPAck() 72 //Used to set TCP Cwr flag to true 73 SetTCPCwr() 74 //Used to set TCP Ece flag to true 75 SetTCPEce() 76 //Used to set TCP Urg flag to true 77 SetTCPUrg() 78 //Used to set TCP Psh flag to true 79 SetTCPPsh() 80 //Used to set TCP Rst flag to true 81 SetTCPRst() 82 //Used to set TCP Fin flag to true 83 SetTCPFin() 84 //Used to add TCP Payload 85 NewTCPPayload(newPayload string) error 86 } 87 88 //PacketHelper interface is a helper for packets and packet flows 89 //Optional: not needed for actual usage 90 type PacketHelper interface { 91 ToBytes() ([]byte, error) 92 AddPacket(packet gopacket.Packet) 93 DecodePacket() PacketManipulator 94 } 95 96 //PacketManipulator is an interface for packet manipulations 97 //Composition of Ethernet, IP and TCP Manipulator interface 98 type PacketManipulator interface { 99 EthernetPacketManipulator 100 IPPacketManipulator 101 TCPPacketManipulator 102 PacketHelper 103 } 104 105 //PacketFlowManipulator is an interface for packet flow manipulations 106 //Used to create/manipulate packet flows 107 type PacketFlowManipulator interface { 108 //Used to create a flow of TCP packets 109 GenerateTCPFlow(pt PacketFlowType) (PacketFlowManipulator, error) 110 //Used to return first TCP Syn packet 111 GetFirstSynPacket() PacketManipulator 112 //Used to return first TCP SynAck packet 113 GetFirstSynAckPacket() PacketManipulator 114 //Used to return first TCP Ack packet 115 GetFirstAckPacket() PacketManipulator 116 //Used to return all the TCP Syn packets from the flow 117 GetSynPackets() PacketFlowManipulator 118 //Used to return all the TCP SynAck packets from the flow 119 GetSynAckPackets() PacketFlowManipulator 120 //Used to return all the TCP Ack packets from the flow 121 GetAckPackets() PacketFlowManipulator 122 //Used to return all the packets upto first TCP SynAck packet from the flow 123 GetUptoFirstSynAckPacket() PacketFlowManipulator 124 //Used to return all the packets upto first TCP Ack packet from the flow 125 GetUptoFirstAckPacket() PacketFlowManipulator 126 //Used to return Nth packet from the flow 127 GetNthPacket(index int) PacketManipulator 128 //Used to return length of the flow 129 GetNumPackets() int 130 //Used to add a new packet to the flow 131 AppendPacket(p PacketManipulator) int 132 } 133 134 //Packet is a custom type which holds the packets and implements PacketManipulator 135 type Packet struct { 136 ethernetLayer *layers.Ethernet 137 ipLayer *layers.IPv4 138 tcpLayer *layers.TCP 139 packet gopacket.Packet 140 } 141 142 //PacketFlow is a custom type which holds the packet attributes and the flow 143 //Implements PacketFlowManipulator interface 144 type PacketFlow struct { 145 sMAC, dMAC string 146 sIP, dIP string 147 sPort, dPort layers.TCPPort 148 flow []PacketManipulator 149 }