Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
js-9401 committed Oct 30, 2024
0 parents commit f823b0a
Show file tree
Hide file tree
Showing 33 changed files with 7,875 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Python files
__pycache__/
*.pyc
*.pyd
*.pyo

# Archive files
*.bz2
*.gz
*.zip
*.pickle

# Temp files
*.swo
*.swp
*.swpx
*.swx
*.tmp

# Log files
*.log
/log/

# Binaries
/bin/
/dist/
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (C) 2014-2024 Leitwert GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
307 changes: 307 additions & 0 deletions README.md

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "ftlbgp"
description = "A Python parser for BGP data in MRT or looking glass format"
authors = [{name = "Johann SCHLAMP", email = "[email protected]"}]
keywords = ["MRT", "BGP", "parser"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Intended Audience :: Telecommunications Industry",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
readme = "README.md"
requires-python = ">=3.6"
dynamic = ["version"]

[project.urls]
Homepage = "https://github.com/leitwert-net/ftlbgp"
Issues = "https://github.com/leitwert-net/ftlbgp/issues"

[tool.hatch.version]
path = "src/ftlbgp/version.py"
54 changes: 54 additions & 0 deletions src/ftlbgp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# pylint: disable=I0011,I0020,I0021,I0023
# pylint: disable=W0149,W0717,R0902,R0904,R0911,R0912,R0913,R0914,R0915,R1702,R1734,R1735,R2044,R6103,C0103,C0209,C2001
# pylint: enable=I0011
""" ftlbgp
Copyright (C) 2014-2024 Leitwert GmbH
This software is distributed under the terms of the MIT license.
It can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
Author Johann SCHLAMP <[email protected]>
"""

__author__ = 'Johann SCHLAMP'
__copyright__ = 'Copyright (C) 2014-2024 Leitwert GmbH'
__license__ = 'MIT license'

__all__ = [
'MrtParser',
'LglParser',
'BgpParser',
'FtlError',
'FtlFileError',
'FtlFormatError',
'FtlDataError'
]

# Local imports
from .version import __version__
from .parser import FtlParser
from .model.error import FtlError
from .model.error import FtlFileError
from .model.error import FtlFormatError
from .model.error import FtlDataError
from .data.mrt.unpack import unpack_mrt_data
from .data.lgl.unpack import unpack_lgl_data


@FtlParser(unpack_mrt_data)
def MrtParser():
""" Parse BGP data in MRT format.
"""


@FtlParser(unpack_lgl_data)
def LglParser():
""" Parse BGP data in looking glass format.
"""


@FtlParser(unpack_mrt_data, unpack_lgl_data)
def BgpParser():
""" Parse BGP data in MRT or looking glass format.
"""
71 changes: 71 additions & 0 deletions src/ftlbgp/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# pylint: disable=I0011,I0020,I0021,I0023
# pylint: disable=W0149,W0717,R0902,R0904,R0911,R0912,R0913,R0914,R0915,R1702,R1734,R1735,R2044,R6103,C0103,C0209,C2001
# pylint: enable=I0011
""" ftlbgp
Copyright (C) 2014-2024 Leitwert GmbH
This software is distributed under the terms of the MIT license.
It can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
Author Johann SCHLAMP <[email protected]>
"""

# System imports
import argparse
import sys

# Local imports
from . import __version__
from . import BgpParser
from . import FtlError
from . import FtlDataError


def main():
""" Main routine.
"""
# Prepare command line arguments
description = f'ftlbgp [v{__version__}] - Parse BGP data in MRT or looking glass format'
parser = argparse.ArgumentParser(prog='ftlbgp', description=description)
argparse._VersionAction.__call__ = lambda *_: (help(BgpParser), parser.exit()) # pylint: disable=protected-access
parser.add_argument('filelist', metavar='<FILE>', nargs='+', help='input file with BGP data (supports bz2/gz)')
parser.add_argument('--pkg-help', action='version', help='show package help for BgpParser usage')
parser.add_argument('--json', default=False, action='store_true', help='output BGP records in JSON format')
parser.format_help = lambda: argparse.ArgumentParser.format_help(parser) + '\n'
parser._get_formatter = lambda: parser.formatter_class(prog='python3 -m ftlbgp') # pylint: disable=protected-access
args = parser.parse_args()

# Initialize BGP parser
with BgpParser(named_records=args.json, serialize=True, raise_on_errors=False,
bgp_records=BgpParser.bgp.records.route,
bgp_route=(
BgpParser.bgp.route.human.DEFAULT
& ~BgpParser.bgp.route.human.type
& ~BgpParser.bgp.route.human.peer_bgp_id
)) as parse:

# Parse input files
for filename in args.filelist:
try:
# Iterate and print records
for record in parse(filename):
print(record)

# Ignore non-critical data errors
except FtlDataError:
continue

# Abort on unknown errors
except FtlError as error:
print(str(error))
sys.exit(1)

# Handle command line induced errors
except (BrokenPipeError, KeyboardInterrupt):
return


# Main routine
if __name__ == '__main__':
main()
12 changes: 12 additions & 0 deletions src/ftlbgp/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
# pylint: disable=I0011,I0020,I0021,I0023
# pylint: disable=W0149,W0717,R0902,R0904,R0911,R0912,R0913,R0914,R0915,R1702,R1734,R1735,R2044,R6103,C0103,C0209,C2001
# pylint: enable=I0011
""" ftlbgp
Copyright (C) 2014-2024 Leitwert GmbH
This software is distributed under the terms of the MIT license.
It can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
Author Johann SCHLAMP <[email protected]>
"""
59 changes: 59 additions & 0 deletions src/ftlbgp/data/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
# pylint: disable=I0011,I0020,I0021,I0023
# pylint: disable=W0149,W0717,R0902,R0904,R0911,R0912,R0913,R0914,R0915,R1702,R1734,R1735,R2044,R6103,C0103,C0209,C2001
# pylint: enable=I0011
# flake8: noqa
""" ftlbgp
Copyright (C) 2014-2024 Leitwert GmbH
This software is distributed under the terms of the MIT license.
It can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
Author Johann SCHLAMP <[email protected]>
"""

# System imports
import socket
import struct
from datetime import datetime

# IP protocols
IPV4 = 4
IPV6 = 6

# IP protocol strings
IPV4_STR = 'ipv4'
IPV6_STR = 'ipv6'

# Address families
AF_INET = int(socket.AF_INET)
AF_INET6 = int(socket.AF_INET6)

# AFI values
AFI_IPV4 = 1 # Defined by IANA
AFI_IPV6 = 2 # Defined by IANA

# Socket/struct functions
socket_inet_pton = socket.inet_pton
socket_inet_ntop = socket.inet_ntop
struct_unpack = struct.unpack
struct_pack = struct.pack

# Struct bytes
STRUCT_2B = '!H'
STRUCT_4B = '!I'
STRUCT_8B = '!Q'
STRUCT_2B2B = '!HH'
STRUCT_8B8B = '!QQ'
STRUCT_2B2B2B = '!HHH'
STRUCT_4B4B4B = '!III'

# Strings
UTF8 = 'utf-8'

# Datetime functions
datetime_utcfromtimestamp = datetime.utcfromtimestamp

# Datetime formats
DATETIME_FORMAT_USEC = '%Y-%m-%d %H:%M:%S.%f'
DATETIME_FORMAT_MIN = '%Y-%m-%d %H:%M'
12 changes: 12 additions & 0 deletions src/ftlbgp/data/lgl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
# pylint: disable=I0011,I0020,I0021,I0023
# pylint: disable=W0149,W0717,R0902,R0904,R0911,R0912,R0913,R0914,R0915,R1702,R1734,R1735,R2044,R6103,C0103,C0209,C2001
# pylint: enable=I0011
""" ftlbgp
Copyright (C) 2014-2024 Leitwert GmbH
This software is distributed under the terms of the MIT license.
It can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
Author Johann SCHLAMP <[email protected]>
"""
Loading

0 comments on commit f823b0a

Please sign in to comment.