Trying to add some error checking to matches and targets
authorSapan Bhatia <sapanb@cs.princeton.edu>
Thu, 10 Sep 2009 19:51:41 +0000 (19:51 +0000)
committerSapan Bhatia <sapanb@cs.princeton.edu>
Thu, 10 Sep 2009 19:51:41 +0000 (19:51 +0000)
sfatables/commands/Add.py
sfatables/exec.py
sfatables/sfatables
sfatables/targets/RESTRICT_TO_NODES.xml
sfatables/targets/__init__.py
sfatables/xmlextension.py
sfatables/xmlrule.py

index 5064ee6..a8a63dc 100644 (file)
@@ -26,18 +26,18 @@ class Add(Command):
 
         return "sfatables-%d-%s"%(last_rule_number+1,type)
 
-    def call(self, command_options, match_options, target_options):
-        filename = match_dir + "/"+match_options.match_name+".xml"
+    def call_gen(self, dir, options):
+        filename = dir + "/"+options.name+".xml"
         xmldoc = libxml2.parseFile(filename)
     
         p = xmldoc.xpathNewContext()
 
-        supplied_arguments = match_options.arguments
+        supplied_arguments = options.arguments
         for option in supplied_arguments:
             option_name = option['name']
-            option_value = getattr(match_options,option_name)
+            option_value = getattr(options,option_name)
 
-            if (hasattr(match_options,option_name)):
+            if (hasattr(options,option_name)):
                 context = p.xpathEval("//rule/argument[name='%s']"%option_name)
                 if (not context):
                     raise Exception('Unknown option %s for match %s'%(option,option['name']))
@@ -55,3 +55,10 @@ class Add(Command):
         xmldoc.freeDoc()
 
         return True
+
+    def call(self, command_options, match_options, target_options):
+        ret = self.call_gen(match_dir, match_options)
+        if (ret):
+            ret = self.call_gen(target_dir, target_options)
+
+        return ret
index 979245d..ef0933d 100644 (file)
@@ -19,7 +19,7 @@ class SFATablesRules:
         chain_dir_path = "%s/%s"%(sfatables_config,chain_name)
         rule_list = List().get_rule_list(chain_dir_path)
         for rule_number in rule_list:
-            self.sorted_rule_list.append(XMLRule(rule_number))
+            self.sorted_rule_list.append(XMLRule(chain_name, rule_number))
         return
 
     def apply(self, rspec):
index f4bb8a8..2fd244d 100755 (executable)
@@ -14,6 +14,7 @@ import libxml2
 from optparse import OptionParser
 from sfatables import commands, matches, targets
 from sfatables.xmlextension import Xmlextension
+from sfatables.globals import *
 
 def load_commands(module, list):
     command_dict={}
@@ -25,11 +26,11 @@ def load_commands(module, list):
 
     return command_dict
 
-def load_xml_extensions(module, list):
+def load_xml_extensions(module, dir, list):
     ext_dict={}
 
     for ext_name in list:
-        module = Xmlextension(ext_name)
+        module = Xmlextension(dir, ext_name)
         ext_dict[ext_name]=module
 
     return ext_dict
@@ -86,20 +87,20 @@ def main():
     if (command.matches):
         if (len(pargs)<2):
             raise Exception("Must specify match for this command")
-        match_dict = load_xml_extensions("sfatables.matches",matches.all)
+        match_dict = load_xml_extensions("sfatables.matches",match_dir, matches.all)
         match_parser = create_parser_xml_ext(match_dict)
         matches_str = ",".join(match_dict.keys())
-        match_parser.add_option('-m','--match',dest='match_name',help='Match name (one of %s)'%matches_str, metavar = 'MATCH')
+        match_parser.add_option('-m','--match',dest='name',help='Match name (one of %s)'%matches_str, metavar = 'MATCH')
         (match_options, args) = match_parser.parse_args(pargs[1])
         try:
-            match_name = match_options.match_name
+            name = match_options.name
         except Exception:
             print "Must specify match name with -m"
 
-        if (match_dict.has_key(match_name)):
-            setattr(match_options, 'arguments', match_dict[match_name].arguments)
+        if (match_dict.has_key(name)):
+            setattr(match_options, 'arguments', match_dict[name].arguments)
         else:
-            raise Exception('Match %s not found'%match_name)
+            raise Exception('Match %s not found'%name)
 
     else:
         match_options=None
@@ -107,7 +108,7 @@ def main():
     if (command.targets):
         if (len(pargs)<3):
             raise Exception("Must specify a target for this command")
-        target_dict = load_xml_extensions("sfatables.targets",targets.all)
+        target_dict = load_xml_extensions("sfatables.targets",target_dir,targets.all)
         target_parser = create_parser_xml_ext(target_dict)
         targets_str = ",".join(target_dict.keys())
         target_parser.add_option('-j','--jump',dest='target_name',help='Target name (one of %s)'%targets, metavar = 'TARGET')
@@ -117,10 +118,10 @@ def main():
         except Exception:
             print "Must specify target name with -m"
 
-#        if (target_dict.has_key(target_name)):
-#            setattr(target_options, 'arguments', target_dict[target_name].arguments)
-#        else:
-#            raise Exception('Target %s not found'%target_name)
+        if (target_dict.has_key(target_name)):
+            setattr(target_options, 'arguments', target_dict[target_name].arguments)
+        else:
+            raise Exception('Target %s not found'%target_name)
 
     else:
         target_options = None
@@ -129,3 +130,5 @@ def main():
 
 if __name__=='__main__':
     main()
+if __name__=='__main__':
+    main()
index 538db28..c31b67e 100644 (file)
@@ -12,7 +12,6 @@
             <help>Prefix of nodes to blacklist for this match.</help>
             <operand>PREFIX</operand>
         </argument>
-
     </rule>
     <processor filename="restrict_to_nodes.xsl"/>
-</match>
+</target>
index 9f3ca20..19e5911 100644 (file)
@@ -1,2 +1,3 @@
 all="""
+RESTRICT_TO_NODES
 """.split()
index 84e336f..412419b 100644 (file)
@@ -13,8 +13,10 @@ class Xmlextension:
     operand = "VALUE"
     arguments = []
 
-    def __init__(self, component_name):
-        filename = match_dir+"/"+component_name+".xml"
+    def __init__(self, dir, component_name):
+        filename = dir+"/"+component_name+".xml"
+        import pdb
+        pdb.set_trace()
         self.xmldoc = libxml2.parseFile(filename)
 
         # TODO: Check xmldoc against a schema
index 8f36c74..df63c9a 100644 (file)
@@ -11,7 +11,7 @@ class XMLRule:
     context = {'match':None,'target':None}
 
     def load_xml_extension (self, type, chain, rule_number):
-        filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s.xml"%(rule_number,type)
+        filename = sfatables_config+"/"+chain+"/"+"sfatables-%d-%s"%(rule_number,type)
 
         self.xmldoc = libxml2.parseFile(filename)
         p = self.xmldoc.xpathNewContext()
@@ -21,7 +21,7 @@ class XMLRule:
 
         processor = p.xpathEval('//processor/@filename')
 
-        self.processor[type] = processor[0].content
+        self.processors[type] = processor[0].content
         self.arguments[type] = p.xpathEval('//rule')
 
         p.xpathFreeContext()