aboutsummaryrefslogtreecommitdiff
path: root/rpki/rtr/main.py
blob: 6add407d30389857f51a141344fe17f9506d8bd9 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# $Id$
#
# Copyright (C) 2014  Dragon Research Labs ("DRL")
# Portions copyright (C) 2009-2013  Internet Systems Consortium ("ISC")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL AND ISC DISCLAIM ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL DRL OR
# ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

"""
RPKI-Router protocol implementation.  See RFC 6810 et sequalia in fine
RFC and Internet-Draft repositories near you.
"""

import os
import sys
import time
import logging
import logging.handlers
import argparse

from rpki.rtr.server    import argparse_setup as argparse_setup_server
from rpki.rtr.client    import argparse_setup as argparse_setup_client
from rpki.rtr.generator import argparse_setup as argparse_setup_generator


class Formatter(logging.Formatter):

  converter = time.gmtime

  def __init__(self, debug, fmt, datefmt):
    self.debug = debug
    super(Formatter, self).__init__(fmt, datefmt)

  def format(self, record):
    if getattr(record, "connection", None) is None:
      record.connection = ""
    return super(Formatter, self).format(record)

  def formatException(self, ei):
    if self.debug:
      return super(Formatter, self).formatException(ei)
    else:
      return str(ei[1])

def main():

  os.environ["TZ"] = "UTC"
  time.tzset()

  if "rpki.rtr.bgpdump" in sys.modules:
    from rpki.rtr.bgpdump import argparse_setup as argparse_setup_bgpdump
  else:
    def argparse_setup_bgpdump(ignored):
      pass

  argparser = argparse.ArgumentParser(description = __doc__)
  argparser.add_argument("--debug", action = "store_true", help = "debugging mode")
  argparser.add_argument("--log-level", default = logging.DEBUG,
                         choices = ("debug", "info", "warning", "error", "critical"),
                         type = lambda s: int(getattr(logging, s.upper())))
  argparser.add_argument("--log-to",
                         choices = ("syslog", "stderr"))
  subparsers = argparser.add_subparsers(title = "Commands", metavar = "", dest = "mode")
  argparse_setup_server(subparsers)
  argparse_setup_client(subparsers)
  argparse_setup_generator(subparsers)
  argparse_setup_bgpdump(subparsers)
  args = argparser.parse_args()

  fmt = "rpki-rtr/" + args.mode + "%(connection)s[%(process)d] %(message)s"

  if (args.log_to or args.default_log_to) == "stderr":
    handler = logging.StreamHandler()
    fmt = "%(asctime)s " + fmt
  elif os.path.exists("/dev/log"):
    handler = logging.handlers.SysLogHandler("/dev/log")
  else:
    handler = logging.handlers.SysLogHandler()

  handler.setFormatter(Formatter(args.debug, fmt, "%Y-%m-%dT%H:%M:%SZ"))
  logging.root.addHandler(handler)
  logging.root.setLevel(args.log_level)

  return args.func(args)