summaryrefslogtreecommitdiffstats
path: root/scripts/external_libs/scapy-2.3.1/python2/scapy/layers/hsrp.py
blob: 7193b97e6a9c3cf0d3458e2f594875b129072012 (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
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
## This file is part of Scapy
## See http://www.secdev.org/projects/scapy for more informations
## Copyright (C) Philippe Biondi <phil@secdev.org>
## This program is published under a GPLv2 license

#############################################################################
##                                                                         ##
## hsrp.py --- HSRP  protocol support for Scapy                            ##
##                                                                         ##
## Copyright (C) 2010  Mathieu RENARD mathieu.renard(at)gmail.com          ##
##                                                                         ##
## This program is free software; you can redistribute it and/or modify it ##
## under the terms of the GNU General Public License version 2 as          ##
## published by the Free Software Foundation; version 2.                   ##
##                                                                         ##
## This program is distributed in the hope that it will be useful, but     ##
## WITHOUT ANY WARRANTY; without even the implied warranty of              ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       ##
## General Public License for more details.                                ##
##                                                                         ##
#############################################################################
## HSRP Version 1
##  Ref. RFC 2281
## HSRP Version 2
##  Ref. http://www.smartnetworks.jp/2006/02/hsrp_8_hsrp_version_2.html
##
## $Log: hsrp.py,v $
## Revision 0.2  2011/05/01 15:23:34  mrenard
##   Cleanup code

"""
HSRP (Hot Standby Router Protocol): proprietary redundancy protocol for Cisco routers.
"""

from scapy.fields import *
from scapy.packet import *
from scapy.layers.inet import UDP


class HSRP(Packet):
    name = "HSRP"
    fields_desc = [
        ByteField("version", 0),
        ByteEnumField("opcode", 0, {0: "Hello", 1: "Coup", 2: "Resign", 3: "Advertise"}),
        ByteEnumField("state", 16, {0: "Initial", 1: "Learn", 2: "Listen", 4: "Speak", 8: "Standby", 16: "Active"}),
        ByteField("hellotime", 3),
        ByteField("holdtime", 10),
        ByteField("priority", 120),
        ByteField("group", 1),
        ByteField("reserved", 0),
        StrFixedLenField("auth", "cisco" + "\00" * 3, 8),
        IPField("virtualIP", "192.168.1.1")]

    def guess_payload_class(self, payload):
        if self.underlayer.len > 28:
            return HSRPmd5
        else:
            return Packet.guess_payload_class(self, payload)


class HSRPmd5(Packet):
    name = "HSRP MD5 Authentication"
    fields_desc = [
        ByteEnumField("type", 4, {4: "MD5 authentication"}),
        ByteField("len", None),
        ByteEnumField("algo", 0, {1: "MD5"}),
        ByteField("padding", 0x00),
        XShortField("flags", 0x00),
        IPField("sourceip", None),
        XIntField("keyid", 0x00),
        StrFixedLenField("authdigest", "\00" * 16, 16)]

    def post_build(self, p, pay):
        if self.len is None and pay:
            l = len(pay)
            p = p[:1] + hex(l)[30:] + p[30:]
        return p

bind_layers(UDP, HSRP, dport=1985, sport=1985)
ge.$callback_package.$callback callback) { synchronized (callbacks) { callbacks.put(jvpp.$name(), callback); } } """) def generate_jvpp(func_list, base_package, dto_package, callback_package, callback_facade_package, inputfile): """ Generates callback facade """ print "Generating JVpp callback facade" if os.path.exists(callback_facade_package): util.remove_folder(callback_facade_package) os.mkdir(callback_facade_package) methods = [] methods_impl = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): # TODO handle notifications continue camel_case_name = util.underscore_to_camelcase(func['name']) camel_case_name_upper = util.underscore_to_camelcase_upper(func['name']) if util.is_reply(camel_case_name): continue # Strip suffix for dump calls callback_type = get_request_name(camel_case_name_upper, func['name']) + callback_gen.callback_suffix if len(func['args']) == 0: methods.append(no_arg_method_template.substitute(name=camel_case_name, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) else: methods.append(method_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) methods_impl.append(method_impl_template.substitute(name=camel_case_name, request=camel_case_name_upper, base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=callback_type)) join = os.path.join(callback_facade_package, "CallbackJVpp.java") jvpp_file = open(join, 'w') jvpp_file.write( jvpp_ifc_template.substitute(inputfile=inputfile, methods="\n".join(methods), base_package=base_package, dto_package=dto_package, callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacade.java"), 'w') jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile, methods="\n".join(methods_impl), base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() generate_callback(func_list, base_package, dto_package, callback_package, callback_facade_package, inputfile) jvpp_facade_callback_template = Template(""" package $base_package.$callback_facade_package; /** * <p>JVppGlobalCallback implementation for Java Callback API. * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile * <br>(python representation of vpe.api generated by vppapigen). */ public final class CallbackJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback { private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requests; public CallbackJVppFacadeCallback(final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requestMap) { this.requests = requestMap; } $methods } """) jvpp_facade_callback_method_template = Template(""" @Override @SuppressWarnings("unchecked") public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { $base_package.$callback_package.$callback callback; synchronized(requests) { callback = ($base_package.$callback_package.$callback) requests.remove(reply.context); } if(callback != null) { callback.on$callback_dto(reply); } } """) def generate_callback(func_list, base_package, dto_package, callback_package, callback_facade_package, inputfile): callbacks = [] for func in func_list: if util.is_notification(func['name']) or util.is_ignored(func['name']): # TODO handle notifications continue camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) if not util.is_reply(camel_case_name_with_suffix): continue callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package, dto_package=dto_package, callback_package=callback_package, callback=util.remove_reply_suffix(camel_case_name_with_suffix) + callback_gen.callback_suffix, callback_dto=camel_case_name_with_suffix)) jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacadeCallback.java"), 'w') jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile, base_package=base_package, dto_package=dto_package, callback_package=callback_package, methods="".join(callbacks), callback_facade_package=callback_facade_package)) jvpp_file.flush() jvpp_file.close() # Returns request name or special one from unconventional_naming_rep_req map def get_request_name(camel_case_dto_name, func_name): if func_name in reverse_dict(util.unconventional_naming_rep_req): request_name = util.underscore_to_camelcase_upper(reverse_dict(util.unconventional_naming_rep_req)[func_name]) else: request_name = camel_case_dto_name return remove_suffix(request_name) def reverse_dict(map): return dict((v, k) for k, v in map.iteritems()) def remove_suffix(name): if util.is_reply(name): return util.remove_reply_suffix(name) else: if util.is_dump(name): return util.remove_suffix(name, util.dump_suffix) else: return name