Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 1 | import socket |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 2 | import sys |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 3 | from abc import abstractmethod, ABCMeta |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 4 | from cStringIO import StringIO |
| 5 | |
| 6 | |
| 7 | def 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 Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 17 | |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 18 | def 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 Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 27 | tail = "" |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 28 | if limit < len(capture): |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 29 | 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 Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 35 | |
| 36 | |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 37 | def ip4_range(ip4, s, e): |
| 38 | tmp = ip4.rsplit('.', 1)[0] |
| 39 | return ("%s.%d" % (tmp, i) for i in range(s, e)) |
| 40 | |
| 41 | |
| 42 | def ip4n_range(ip4n, s, e): |
| 43 | ip4 = socket.inet_ntop(socket.AF_INET, ip4n) |
Klement Sekera | 72715ee | 2017-01-17 10:37:05 +0100 | [diff] [blame] | 44 | return (socket.inet_pton(socket.AF_INET, ip) |
| 45 | for ip in ip4_range(ip4, s, e)) |
Eyal Bari | d81da8c | 2017-01-11 13:39:54 +0200 | [diff] [blame] | 46 | |
| 47 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 48 | class NumericConstant(object): |
| 49 | __metaclass__ = ABCMeta |
| 50 | |
| 51 | desc_dict = {} |
| 52 | |
| 53 | @abstractmethod |
| 54 | def __init__(self, value): |
| 55 | self._value = value |
| 56 | |
| 57 | def __int__(self): |
| 58 | return self._value |
| 59 | |
| 60 | def __long__(self): |
| 61 | return self._value |
| 62 | |
| 63 | def __str__(self): |
| 64 | if self._value in self.desc_dict: |
| 65 | return self.desc_dict[self._value] |
| 66 | return "" |
| 67 | |
| 68 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 69 | class Host(object): |
| 70 | """ Generic test host "connected" to VPPs interface. """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 71 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 72 | @property |
| 73 | def mac(self): |
| 74 | """ MAC address """ |
| 75 | return self._mac |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 76 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 77 | @property |
| 78 | def ip4(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame^] | 79 | """ IPv4 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 80 | return self._ip4 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 81 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 82 | @property |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 83 | def ip4n(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame^] | 84 | """ IPv4 address of remote host - raw, suitable as API parameter.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 85 | return socket.inet_pton(socket.AF_INET, self._ip4) |
| 86 | |
| 87 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 88 | def ip6(self): |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame^] | 89 | """ IPv6 address - string """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 90 | return self._ip6 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 91 | |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame^] | 92 | @property |
| 93 | def ip6n(self): |
| 94 | """ IPv6 address of remote host - raw, suitable as API parameter.""" |
| 95 | return socket.inet_pton(socket.AF_INET6, self._ip6) |
| 96 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 97 | def __init__(self, mac=None, ip4=None, ip6=None): |
| 98 | self._mac = mac |
| 99 | self._ip4 = ip4 |
| 100 | self._ip6 = ip6 |