summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/bmp_collector/bmpserver
blob: 5257df7530b64128807bc707b421e83ae7a051c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# SPDX-License-Identifier: ISC

# Copyright 2023 6WIND S.A.
# Authored by Farid Mihoub <farid.mihoub@6wind.com>
#
import argparse
# XXX: something more reliable should be used "Twisted" a great choice.
import socket
import sys

from bmp import BMPMsg

BGP_MAX_SIZE = 4096

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--address", type=str, default="0.0.0.0")
parser.add_argument("-p", "--port", type=int, default=1789)
parser.add_argument("-l", "--logfile", type=str, default="/var/log/bmp.log")

def main():
    args = parser.parse_args()
    ADDRESS, PORT = args.address, args.port
    LOG_FILE = args.logfile

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((ADDRESS, PORT))
        s.listen()
        connection, _ = s.accept()

        try:
            while True:
                data = connection.recv(BGP_MAX_SIZE)
                while len(data) > BMPMsg.MIN_LEN:
                    data = BMPMsg.dissect(data, log_file=LOG_FILE)
        except Exception as e:
            # XXX: do something
            pass
        except KeyboardInterrupt:
            # XXX: do something
            pass
        finally:
            connection.close()

if __name__ == "__main__":
    sys.exit(main())