blob: 6658febfdb1a94e1c0eab20b772f5f25f5625f81 [file] [log] [blame]
Matej Klotton0178d522016-11-04 11:11:44 +01001import socket
Klement Sekera7bb873a2016-11-18 07:38:42 +01002import sys
Klement Sekera0e3c0de2016-09-29 14:43:44 +02003from abc import abstractmethod, ABCMeta
Klement Sekera7bb873a2016-11-18 07:38:42 +01004from cStringIO import StringIO
5
6
7def ppp(headline, packet):
8 """ Return string containing the output of scapy packet.show() call. """
9 o = StringIO()
10 old_stdout = sys.stdout
11 sys.stdout = o
12 print(headline)
13 packet.show()
14 sys.stdout = old_stdout
15 return o.getvalue()
16
Damjan Marionf56b77a2016-10-03 19:44:57 +020017
Klement Sekera9225dee2016-12-12 08:36:58 +010018def ppc(headline, capture, limit=10):
19 """ Return string containing ppp() printout for a capture.
20
21 :param headline: printed as first line of output
22 :param capture: packets to print
23 :param limit: limit the print to # of packets
24 """
25 if not capture:
26 return headline
Klement Sekeradab231a2016-12-21 08:50:14 +010027 tail = ""
Klement Sekera9225dee2016-12-12 08:36:58 +010028 if limit < len(capture):
Klement Sekeradab231a2016-12-21 08:50:14 +010029 tail = "\nPrint limit reached, %s out of %s packets printed" % (
30 len(capture), limit)
31 limit = len(capture)
32 body = "".join([ppp("Packet #%s:" % count, p)
33 for count, p in zip(range(0, limit), capture)])
34 return "%s\n%s%s" % (headline, body, tail)
Klement Sekera9225dee2016-12-12 08:36:58 +010035
36
Klement Sekera0e3c0de2016-09-29 14:43:44 +020037class NumericConstant(object):
38 __metaclass__ = ABCMeta
39
40 desc_dict = {}
41
42 @abstractmethod
43 def __init__(self, value):
44 self._value = value
45
46 def __int__(self):
47 return self._value
48
49 def __long__(self):
50 return self._value
51
52 def __str__(self):
53 if self._value in self.desc_dict:
54 return self.desc_dict[self._value]
55 return ""
56
57
Matej Klotton0178d522016-11-04 11:11:44 +010058class Host(object):
59 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +020060
Klement Sekeraf62ae122016-10-11 11:47:09 +020061 @property
62 def mac(self):
63 """ MAC address """
64 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +020065
Klement Sekeraf62ae122016-10-11 11:47:09 +020066 @property
67 def ip4(self):
68 """ IPv4 address """
69 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +020070
Klement Sekeraf62ae122016-10-11 11:47:09 +020071 @property
Matej Klotton0178d522016-11-04 11:11:44 +010072 def ip4n(self):
73 """ IPv4 address """
74 return socket.inet_pton(socket.AF_INET, self._ip4)
75
76 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 def ip6(self):
78 """ IPv6 address """
79 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +020080
Klement Sekeraf62ae122016-10-11 11:47:09 +020081 def __init__(self, mac=None, ip4=None, ip6=None):
82 self._mac = mac
83 self._ip4 = ip4
84 self._ip6 = ip6