Packet library API Reference¶
Packet class¶
- class os_ken.lib.packet.packet.Packet(data=None, protocols=None, parse_cls=<class 'os_ken.lib.packet.ethernet.ethernet'>)¶
A packet decoder/encoder class.
An instance is used to either decode or encode a single packet.
data is a bytearray to describe a raw datagram to decode. When decoding, a Packet object is iteratable. Iterated values are protocol (ethernet, ipv4, ...) headers and the payload. Protocol headers are instances of subclass of packet_base.PacketBase. The payload is a bytearray. They are iterated in on-wire order.
data should be omitted when encoding a packet.
- add_protocol(proto)¶
Register a protocol proto for this packet.
This method is legal only when encoding a packet.
When encoding a packet, register a protocol (ethernet, ipv4, ...) header to add to this packet. Protocol headers should be registered in on-wire order before calling self.serialize.
- classmethod from_jsondict(dict_, decode_string=<function b64decode>, **additional_args)¶
Create an instance from a JSON style dict.
Instantiate this class with parameters specified by the dict.
This method takes the following arguments.
Argument
Descrpition
dict_
A dictionary which describes the parameters. For example, {"Param1": 100, "Param2": 200}
decode_string
(Optional) specify how to decode strings. The default is base64. This argument is used only for attributes which don't have explicit type annotations in _TYPE class attribute.
additional_args
(Optional) Additional kwargs for constructor.
- get_protocol(protocol)¶
Returns the firstly found protocol that matches to the specified protocol.
- get_protocols(protocol)¶
Returns a list of protocols that matches to the specified protocol.
- serialize()¶
Encode a packet and store the resulted bytearray in self.data.
This method is legal only when encoding a packet.
Stream Parser class¶
- class os_ken.lib.packet.stream_parser.StreamParser¶
Streaming parser base class.
An instance of a subclass of this class is used to extract messages from a raw byte stream.
It's designed to be used for data read from a transport which doesn't preserve message boundaries. A typical example of such a transport is TCP.
- exception TooSmallException¶
- parse(data)¶
Tries to extract messages from a raw byte stream.
The data argument would be python bytes newly read from the input stream.
Returns an ordered list of extracted messages. It can be an empty list.
The rest of data which doesn't produce a complete message is kept internally and will be used when more data is come. I.e. next time this method is called again.
- abstract try_parse(q)¶
Try to extract a message from the given bytes.
This is an override point for subclasses.
This method tries to extract a message from bytes given by the argument.
Raises TooSmallException if the given data is not enough to extract a complete message but there's still a chance to extract a message if more data is come later.
List of the sub-classes:
Protocol Header classes¶
- Packet Base Class
- ARP
- BFD
- BGP
AdminReset
AdminShutdown
AttrFlagError
AttrLenError
AuthFailure
BGPEvpnEsImportRTExtendedCommunity
BGPEvpnEsiLabelExtendedCommunity
BGPEvpnMacMobilityExtendedCommunity
BGPFlowSpecRedirectCommunity
BGPFlowSpecTPIDActionCommunity
BGPFlowSpecTrafficActionCommunity
BGPFlowSpecTrafficMarkingCommunity
BGPFlowSpecTrafficRateCommunity
BGPFlowSpecVlanActionCommunity
BGPKeepAlive
BGPMessage
BGPNotification
BGPOpen
BGPPathAttributePmsiTunnel
BGPRouteRefresh
BGPUpdate
BadBgpId
BadLen
BadMsg
BadNotification
BadPeerAs
BgpExc
CollisionResolution
ConnRejected
EvpnASBasedEsi
EvpnArbitraryEsi
EvpnEsi
EvpnEthernetAutoDiscoveryNLRI
EvpnEthernetSegmentNLRI
EvpnInclusiveMulticastEthernetTagNLRI
EvpnIpPrefixNLRI
EvpnL2BridgeEsi
EvpnLACPEsi
EvpnMacBasedEsi
EvpnMacIPAdvertisementNLRI
EvpnNLRI
EvpnRouterIDEsi
EvpnUnknownEsi
EvpnUnknownNLRI
FiniteStateMachineError
FlowSpecComponentUnknown
FlowSpecDSCP
FlowSpecDestPort
FlowSpecDestPrefix
FlowSpecDestinationMac
FlowSpecEtherType
FlowSpecFragment
FlowSpecIPProtocol
FlowSpecIPv4NLRI
FlowSpecIPv6DestPrefix
FlowSpecIPv6FlowLabel
FlowSpecIPv6Fragment
FlowSpecIPv6NLRI
FlowSpecIPv6SrcPrefix
FlowSpecIcmpCode
FlowSpecIcmpType
FlowSpecInnerVLANCoS
FlowSpecInnerVLANID
FlowSpecL2VPNNLRI
FlowSpecLLCControl
FlowSpecLLCDSAP
FlowSpecLLCSSAP
FlowSpecNextHeader
FlowSpecPacketLen
FlowSpecPort
FlowSpecSNAP
FlowSpecSourceMac
FlowSpecSrcPort
FlowSpecSrcPrefix
FlowSpecTCPFlags
FlowSpecVLANCoS
FlowSpecVLANID
FlowSpecVPNv4NLRI
FlowSpecVPNv6NLRI
HoldTimerExpired
InvalidNetworkField
InvalidNextHop
InvalidOriginError
MalformedAsPath
MalformedAttrList
MalformedOptionalParam
MaxPrefixReached
MissingWellKnown
NotSync
OptAttrError
OtherConfChange
OutOfResource
PeerDeConfig
PmsiTunnelIdUnknown
RouteTargetMembershipNLRI
RoutingLoop
StreamParser
UnRegWellKnowAttr
UnacceptableHoldTime
UnsupportedOptParam
UnsupportedVersion
- BMP
- BPDU
- CFM
- DHCP
- DHCP6
- Ethernet
- Geneve
- GRE
- ICMP
- ICMPv6
- IGMP
- IPv4
- IPv6
- LLC
- LLDP
- MPLS
- OpenFlow
- OSPF
- PBB
- SCTP
cause_cookie_while_shutdown
cause_invalid_param
cause_invalid_stream_id
cause_missing_param
cause_no_userdata
cause_out_of_resource
cause_protocol_violation
cause_restart_with_new_addr
cause_stale_cookie
cause_unrecognized_chunk
cause_unrecognized_param
cause_unresolvable_addr
cause_user_initiated_abort
chunk_abort
chunk_cookie_ack
chunk_cookie_echo
chunk_cwr
chunk_data
chunk_ecn_echo
chunk_error
chunk_heartbeat
chunk_heartbeat_ack
chunk_init
chunk_init_ack
chunk_sack
chunk_shutdown
chunk_shutdown_ack
chunk_shutdown_complete
param_cookie_preserve
param_ecn
param_heartbeat
param_host_addr
param_ipv4
param_ipv6
param_state_cookie
param_supported_addr
param_unrecognized_param
sctp
- Slow
- TCP
- UDP
- VLAN
- VRRP
- VXLAN
- Zebra
InterfaceLinkParams
NextHopBlackhole
NextHopIFIndex
NextHopIFName
NextHopIPv4
NextHopIPv4IFIndex
NextHopIPv4IFName
NextHopIPv6
NextHopIPv6IFIndex
NextHopIPv6IFName
RegisteredNexthop
ZebraBfdClientRegister
ZebraBfdDestinationDeregister
ZebraBfdDestinationRegister
ZebraBfdDestinationReply
ZebraBfdDestinationUpdate
ZebraHello
ZebraIPv4ImportLookup
ZebraIPv4NexthopAdd
ZebraIPv4NexthopDelete
ZebraIPv4NexthopLookup
ZebraIPv4NexthopLookupMRib
ZebraIPv4RouteAdd
ZebraIPv4RouteDelete
ZebraIPv4RouteIPv6NexthopAdd
ZebraIPv6ImportLookup
ZebraIPv6NexthopAdd
ZebraIPv6NexthopDelete
ZebraIPv6NexthopLookup
ZebraIPv6RouteAdd
ZebraIPv6RouteDelete
ZebraImportCheckUpdate
ZebraImportRouteRegister
ZebraImportRouteUnregister
ZebraInterfaceAdd
ZebraInterfaceAddressAdd
ZebraInterfaceAddressDelete
ZebraInterfaceBfdDestinationUpdate
ZebraInterfaceDelete
ZebraInterfaceDisableRadv
ZebraInterfaceDown
ZebraInterfaceEnableRadv
ZebraInterfaceLinkParams
ZebraInterfaceNbrAddressAdd
ZebraInterfaceNbrAddressDelete
ZebraInterfaceUp
ZebraInterfaceVrfUpdate
ZebraMessage
ZebraMplsLabelsAdd
ZebraMplsLabelsDelete
ZebraNexthopRegister
ZebraNexthopUnregister
ZebraNexthopUpdate
ZebraRedistributeAdd
ZebraRedistributeDefaultAdd
ZebraRedistributeDefaultDelete
ZebraRedistributeDelete
ZebraRedistributeIPv4Add
ZebraRedistributeIPv4Delete
ZebraRedistributeIPv6Add
ZebraRedistributeIPv6Delete
ZebraRouterIDAdd
ZebraRouterIDDelete
ZebraRouterIDUpdate
ZebraUnknownMessage
ZebraVrfAdd
ZebraVrfDelete
ZebraVrfUnregister
zebra