From a512338afdbec725a8182225c30867d707401a98 Mon Sep 17 00:00:00 2001 From: Shivani Bhardwaj Date: Fri, 23 Jun 2023 11:57:21 +0530 Subject: [PATCH] suricatasc: add line numbers in error messages --- python/suricata/sc/suricatasc.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/python/suricata/sc/suricatasc.py b/python/suricata/sc/suricatasc.py index b1a8d7f41e..d5f6f98f0e 100644 --- a/python/suricata/sc/suricatasc.py +++ b/python/suricata/sc/suricatasc.py @@ -22,6 +22,7 @@ import readline import select import sys from socket import AF_UNIX, error, socket +from inspect import currentframe from suricata.sc.specs import argsd @@ -30,6 +31,11 @@ VERSION = "0.2" INC_SIZE = 1024 +def get_linenumber(): + cf = currentframe() + return cf.f_back.f_lineno + + class SuricataException(Exception): """ Generic class for suricatasc exception @@ -136,7 +142,7 @@ class SuricataSC: def send_command(self, command, arguments=None): if command not in self.cmd_list and command != 'command-list': - raise SuricataCommandException("Command not found: {}".format(command)) + raise SuricataCommandException("L{}: Command not found: {}".format(get_linenumber(), command)) cmdmsg = {} cmdmsg['command'] = command @@ -156,7 +162,7 @@ class SuricataSC: else: cmdret = None if not cmdret: - raise SuricataReturnException("Unable to get message from server") + raise SuricataReturnException("L{}: Unable to get message from server".format(get_linenumber)) if self.verbose: print("RCV: "+ json.dumps(cmdret)) @@ -169,7 +175,7 @@ class SuricataSC: self.socket = socket(AF_UNIX) self.socket.connect(self.sck_path) except error as err: - raise SuricataNetException(err) + raise SuricataNetException("L{}: {}".format(get_linenumber(), err)) self.socket.settimeout(10) #send version @@ -187,13 +193,13 @@ class SuricataSC: cmdret = None if not cmdret: - raise SuricataReturnException("Unable to get message from server") + raise SuricataReturnException("L{}: Unable to get message from server".format(get_linenumber())) if self.verbose: print("RCV: "+ json.dumps(cmdret)) if cmdret["return"] == "NOK": - raise SuricataReturnException("Error: %s" % (cmdret["message"])) + raise SuricataReturnException("L{}: Error: {}".format(get_linenumber(), cmdret["message"])) cmdret = self.send_command("command-list") @@ -223,9 +229,9 @@ class SuricataSC: except IndexError: phrase = " at least" if required_args_count != len(cmd_specs) else "" msg = "Missing arguments: expected{} {}".format(phrase, required_args_count) - raise SuricataCommandException(msg) + raise SuricataCommandException("L{}: {}".format(get_linenumber(), msg)) except ValueError as ve: - raise SuricataCommandException("Erroneous arguments: {}".format(ve)) + raise SuricataCommandException("L{}: Erroneous arguments: {}".format(get_linenumber(), ve)) elif c < len(full_cmd): arguments[spec["name"]] = spec_type(full_cmd[c]) return cmd, arguments @@ -237,7 +243,7 @@ class SuricataSC: if cmd in self.fn_commands: cmd, arguments = getattr(self, "execute")(command=command) else: - raise SuricataCommandException("Unknown command: {}".format(command)) + raise SuricataCommandException("L{}: Unknown command: {}".format(get_linenumber(), command)) return cmd, arguments def interactive(self):