xref: /aosp_15_r20/external/openthread/tests/scripts/thread-cert/sniffer.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2016, The OpenThread Authors.
4*cfb92d14SAndroid Build Coastguard Worker#  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker#
6*cfb92d14SAndroid Build Coastguard Worker#  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker#  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker#  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker#  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker#     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker#     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker#  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker#     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker#     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker#
17*cfb92d14SAndroid Build Coastguard Worker#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker#  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker#
29*cfb92d14SAndroid Build Coastguard Worker
30*cfb92d14SAndroid Build Coastguard Workerimport collections
31*cfb92d14SAndroid Build Coastguard Workerimport io
32*cfb92d14SAndroid Build Coastguard Workerimport logging
33*cfb92d14SAndroid Build Coastguard Workerimport os
34*cfb92d14SAndroid Build Coastguard Workerimport sys
35*cfb92d14SAndroid Build Coastguard Workerimport time
36*cfb92d14SAndroid Build Coastguard Worker
37*cfb92d14SAndroid Build Coastguard Workerimport pcap
38*cfb92d14SAndroid Build Coastguard Workerimport threading
39*cfb92d14SAndroid Build Coastguard Workerimport traceback
40*cfb92d14SAndroid Build Coastguard Worker
41*cfb92d14SAndroid Build Coastguard Workertry:
42*cfb92d14SAndroid Build Coastguard Worker    import Queue
43*cfb92d14SAndroid Build Coastguard Workerexcept ImportError:
44*cfb92d14SAndroid Build Coastguard Worker    import queue as Queue
45*cfb92d14SAndroid Build Coastguard Worker
46*cfb92d14SAndroid Build Coastguard Workerimport message
47*cfb92d14SAndroid Build Coastguard Workerimport sniffer_transport
48*cfb92d14SAndroid Build Coastguard Worker
49*cfb92d14SAndroid Build Coastguard Worker
50*cfb92d14SAndroid Build Coastguard Workerclass Sniffer:
51*cfb92d14SAndroid Build Coastguard Worker    """ Class representing the Sniffing node, whose main task is listening
52*cfb92d14SAndroid Build Coastguard Worker        and logging message exchange performed by other nodes.
53*cfb92d14SAndroid Build Coastguard Worker    """
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker    logger = logging.getLogger("sniffer.Sniffer")
56*cfb92d14SAndroid Build Coastguard Worker
57*cfb92d14SAndroid Build Coastguard Worker    RECV_BUFFER_SIZE = 4096
58*cfb92d14SAndroid Build Coastguard Worker
59*cfb92d14SAndroid Build Coastguard Worker    def __init__(self, message_factory):
60*cfb92d14SAndroid Build Coastguard Worker        """
61*cfb92d14SAndroid Build Coastguard Worker        Args:
62*cfb92d14SAndroid Build Coastguard Worker            message_factory (MessageFactory): Class producing messages from data bytes.
63*cfb92d14SAndroid Build Coastguard Worker        """
64*cfb92d14SAndroid Build Coastguard Worker
65*cfb92d14SAndroid Build Coastguard Worker        self._message_factory = message_factory
66*cfb92d14SAndroid Build Coastguard Worker
67*cfb92d14SAndroid Build Coastguard Worker        self._pcap = pcap.PcapCodec(os.getenv('TEST_NAME', 'current'))
68*cfb92d14SAndroid Build Coastguard Worker        if __name__ == '__main__':
69*cfb92d14SAndroid Build Coastguard Worker            sys.stdout.buffer.write(self._pcap.encode_header())
70*cfb92d14SAndroid Build Coastguard Worker            sys.stdout.buffer.flush()
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Worker        # Create transport
73*cfb92d14SAndroid Build Coastguard Worker        transport_factory = sniffer_transport.SnifferTransportFactory()
74*cfb92d14SAndroid Build Coastguard Worker        self._transport = transport_factory.create_transport()
75*cfb92d14SAndroid Build Coastguard Worker
76*cfb92d14SAndroid Build Coastguard Worker        self._thread = None
77*cfb92d14SAndroid Build Coastguard Worker        self._thread_alive = threading.Event()
78*cfb92d14SAndroid Build Coastguard Worker        self._thread_alive.clear()
79*cfb92d14SAndroid Build Coastguard Worker
80*cfb92d14SAndroid Build Coastguard Worker        self._buckets = collections.defaultdict(Queue.Queue)
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Worker    def _sniffer_main_loop(self):
83*cfb92d14SAndroid Build Coastguard Worker        """ Sniffer main loop. """
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker        self.logger.debug("Sniffer started.")
86*cfb92d14SAndroid Build Coastguard Worker
87*cfb92d14SAndroid Build Coastguard Worker        while self._thread_alive.is_set():
88*cfb92d14SAndroid Build Coastguard Worker            data, nodeid = self._transport.recv(self.RECV_BUFFER_SIZE)
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Worker            pkt = self._pcap.append(data)
91*cfb92d14SAndroid Build Coastguard Worker            if __name__ == '__main__':
92*cfb92d14SAndroid Build Coastguard Worker                try:
93*cfb92d14SAndroid Build Coastguard Worker                    sys.stdout.buffer.write(pkt)
94*cfb92d14SAndroid Build Coastguard Worker                    sys.stdout.flush()
95*cfb92d14SAndroid Build Coastguard Worker                except BrokenPipeError:
96*cfb92d14SAndroid Build Coastguard Worker                    self._thread_alive.clear()
97*cfb92d14SAndroid Build Coastguard Worker                    break
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Worker            # Ignore any exceptions
100*cfb92d14SAndroid Build Coastguard Worker            if self._message_factory is not None:
101*cfb92d14SAndroid Build Coastguard Worker                try:
102*cfb92d14SAndroid Build Coastguard Worker                    messages = self._message_factory.create(io.BytesIO(data))
103*cfb92d14SAndroid Build Coastguard Worker                    self.logger.debug("Received messages: {}".format(messages))
104*cfb92d14SAndroid Build Coastguard Worker                    for msg in messages:
105*cfb92d14SAndroid Build Coastguard Worker                        self._buckets[nodeid].put(msg)
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker                except Exception as e:
108*cfb92d14SAndroid Build Coastguard Worker                    # Just print the exception to the console
109*cfb92d14SAndroid Build Coastguard Worker                    self.logger.error("EXCEPTION: %s" % e)
110*cfb92d14SAndroid Build Coastguard Worker                    traceback.print_exc()
111*cfb92d14SAndroid Build Coastguard Worker
112*cfb92d14SAndroid Build Coastguard Worker        self.logger.debug("Sniffer stopped.")
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Worker    def start(self):
115*cfb92d14SAndroid Build Coastguard Worker        """ Start sniffing. """
116*cfb92d14SAndroid Build Coastguard Worker
117*cfb92d14SAndroid Build Coastguard Worker        self._thread = threading.Thread(target=self._sniffer_main_loop)
118*cfb92d14SAndroid Build Coastguard Worker        self._thread.daemon = True
119*cfb92d14SAndroid Build Coastguard Worker
120*cfb92d14SAndroid Build Coastguard Worker        self._transport.open()
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker        self._thread_alive.set()
123*cfb92d14SAndroid Build Coastguard Worker        self._thread.start()
124*cfb92d14SAndroid Build Coastguard Worker
125*cfb92d14SAndroid Build Coastguard Worker    def stop(self):
126*cfb92d14SAndroid Build Coastguard Worker        """ Stop sniffing. """
127*cfb92d14SAndroid Build Coastguard Worker
128*cfb92d14SAndroid Build Coastguard Worker        self._thread_alive.clear()
129*cfb92d14SAndroid Build Coastguard Worker
130*cfb92d14SAndroid Build Coastguard Worker        self._transport.close()
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Worker        self._thread.join(timeout=1)
133*cfb92d14SAndroid Build Coastguard Worker        self._thread = None
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Worker    def set_lowpan_context(self, cid, prefix):
136*cfb92d14SAndroid Build Coastguard Worker        self._message_factory.set_lowpan_context(cid, prefix)
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Worker    def get_messages_sent_by(self, nodeid):
139*cfb92d14SAndroid Build Coastguard Worker        """ Get sniffed messages.
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker        Note! This method flushes the message queue so calling this
142*cfb92d14SAndroid Build Coastguard Worker        method again will return only the newly logged messages.
143*cfb92d14SAndroid Build Coastguard Worker
144*cfb92d14SAndroid Build Coastguard Worker        Args:
145*cfb92d14SAndroid Build Coastguard Worker            nodeid (int): node id
146*cfb92d14SAndroid Build Coastguard Worker
147*cfb92d14SAndroid Build Coastguard Worker        Returns:
148*cfb92d14SAndroid Build Coastguard Worker            MessagesSet: a set with received messages.
149*cfb92d14SAndroid Build Coastguard Worker        """
150*cfb92d14SAndroid Build Coastguard Worker        bucket = self._buckets[nodeid]
151*cfb92d14SAndroid Build Coastguard Worker        messages = []
152*cfb92d14SAndroid Build Coastguard Worker
153*cfb92d14SAndroid Build Coastguard Worker        while not bucket.empty():
154*cfb92d14SAndroid Build Coastguard Worker            messages.append(bucket.get_nowait())
155*cfb92d14SAndroid Build Coastguard Worker
156*cfb92d14SAndroid Build Coastguard Worker        return message.MessagesSet(messages)
157*cfb92d14SAndroid Build Coastguard Worker
158*cfb92d14SAndroid Build Coastguard Worker
159*cfb92d14SAndroid Build Coastguard Workerdef run_sniffer():
160*cfb92d14SAndroid Build Coastguard Worker    sniffer = Sniffer(None)
161*cfb92d14SAndroid Build Coastguard Worker    sniffer.start()
162*cfb92d14SAndroid Build Coastguard Worker    while sniffer._thread_alive.is_set():
163*cfb92d14SAndroid Build Coastguard Worker        try:
164*cfb92d14SAndroid Build Coastguard Worker            time.sleep(1)
165*cfb92d14SAndroid Build Coastguard Worker        except KeyboardInterrupt:
166*cfb92d14SAndroid Build Coastguard Worker            break
167*cfb92d14SAndroid Build Coastguard Worker
168*cfb92d14SAndroid Build Coastguard Worker    sniffer.stop()
169*cfb92d14SAndroid Build Coastguard Worker
170*cfb92d14SAndroid Build Coastguard Worker
171*cfb92d14SAndroid Build Coastguard Workerif __name__ == '__main__':
172*cfb92d14SAndroid Build Coastguard Worker    run_sniffer()
173