Types¶
In general, the whole Python API works only with lists of bytes. For example, mac address of “00:00:00:0a:0b:01” would be used like that: [0, 0, 0, 10, 11, 1]. Same for IPv4 (list of 4 bytes) and IPv6 (list of 16 bytes). In order to make the transformation easier, we supplied EMUType’s.
Every type can accept string / list of bytes / type object in the constructor and able to convert it using V() to list or S() as string.
Also each type supports enumeration i.e: Mac(‘00:00:00:70:00:01’)[2].S() -> ‘00:00:00:70:00:03’
Here is a simple example of usage:
#!/usr/bin/python
import emu_path
from trex.emu.trex_emu_conversions import *
mac_v = Mac([1, 2, 3, 4, 5, 0])
mac_s = Mac(Mac('ff:ff:ff:ff:ff:00')) # can accept same object as well
ipv4_s = Ipv4('224.0.0.255', mc = True) # mc for multicast validation
ipv4_v = Ipv4([10, 1, 0 ,1])
ipv6_s = Ipv6('FF00:AAAA::1234', mc = True)
ipv6_v = Ipv6([255, 255, 170, 170, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 52])
list_types = [mac_s, mac_v, ipv4_s, ipv4_v , ipv6_s, ipv6_v]
incs = [1, 10, -1, -10]
for obj in list_types:
print("This is my numeric val: %s" % obj.num_val)
print('str: %s' % obj.S()) # convert to string
print('bytes: %s' % obj.V()) # convert to list of bytes
for i in incs:
print("inc by %s: %s" %(i, obj[i]))
print('-' * 42)
Mac¶
- class trex.emu.trex_emu_conversions.Mac(mac)[source]¶
Creating a Mac object.
- parameters
- mac: string / list of bytes / Mac object
Valid mac representation. i.e: ‘00:00:00:00:00:00’, [0, 0, 0, 0, 0, 1] or Mac(‘00:00:00:00:00:00’)
- raises
- exe:’TRexError’
If mac is not valid.
- S()[source]¶
- Convert object to string representation.
- returns
string: string representation of the mac address
- V()¶
Convert the object to a list of bytes.
- returns
list: list of bytes representing the object.
Ipv4¶
- class trex.emu.trex_emu_conversions.Ipv4(ipv4, mc=False)[source]¶
Creating a ipv4 object.
- parameters
- ipv4: string / list of bytes / ipv4 object
Valid ipv4 representation. i.e: ‘224.0.1.2’, [224, 0, 1, 2] or ipv4(‘224.0.1.2’)
- mc: bool
Check if the address is a valid IPv4 multicast.
- raises
- exe:’TRexError’
If ipv4 is not valid.
- S()[source]¶
- Convert object to string representation.
- returns
string: string representation of the ipv4 address
- V()¶
Convert the object to a list of bytes.
- returns
list: list of bytes representing the object.
IPv6¶
- class trex.emu.trex_emu_conversions.Ipv6(ipv6, mc=False)[source]¶
Creating a ipv6 object.
- parameters
- ipv6: string / list of bytes / ipv6 object
Valid ipv6 representation. i.e: ‘::FF00’, [0, .., 255, 255, 0, 0] or ipv6(‘::FF00’)
- mc: bool
Check if the address is a valid ipv6 multicast.
- raises
- exe:’TRexError’
If ipv6 is not valid.
- S()[source]¶
- Convert object to string representation.
- returns
string: string representation of the ipv6 address
- V()¶
Convert the object to a list of bytes.
- returns
list: list of bytes representing the object.