From 94d24a8b19c016b4e7c8a7a016c6a3b3bdc9f22a Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Sep 2016 16:30:45 +0200 Subject: [PATCH] first working jsonschema (written in yaml...) for the protocol part only. --- .idea/npd.iml | 2 +- .idea/workspace.xml | 179 +++++++++++++++++++++++++++++--------------- example.yml | 31 ++++---- schema.yml | 76 +++++++++++++------ validate.py | 23 +++--- 5 files changed, 201 insertions(+), 110 deletions(-) diff --git a/.idea/npd.iml b/.idea/npd.iml index 6711606..f049a7d 100644 --- a/.idea/npd.iml +++ b/.idea/npd.iml @@ -2,7 +2,7 @@ - + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3e79ea0..5e67a97 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,11 @@ - - + + + + + @@ -23,28 +26,18 @@ - + - - + + - + - - - - - - - - - - @@ -60,15 +53,17 @@ - @@ -95,32 +90,32 @@ - + - - - - - @@ -128,33 +123,34 @@ - - - - - - - - - - - + + + - + + + + + + + + + + + - + - + - + + + + - - - @@ -342,12 +346,22 @@ + + + + + + + + + + - + @@ -356,35 +370,82 @@ - - + - + + + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example.yml b/example.yml index 406a447..063c260 100644 --- a/example.yml +++ b/example.yml @@ -1,49 +1,49 @@ # Example YAML protocol structure - +--- protocol: &tcp name: TCP fields: - - + - field: source port length: 2 - - + - field: destination port length: 2 - - + - field: sequence number length: 4 - - + - field: acknowledgment number length: 4 - - + - field: data offset bitfield: yes length: 4 - - + - field: reserved bitfield: yes length: 6 - - + - field: URG bitfield: yes length: 1 - - + - field: ACK bitfield: yes length: 1 - - + - field: PSH bitfield: yes length: 1 - - + - field: RST bitfield: yes length: 1 - - + - field: SYN bitfield: yes length: 1 - - + - field: FIN bitfield: yes length: 1 @@ -63,8 +63,3 @@ protocol: &tcp length: 0 data: true osilayer: 4 - - - - - diff --git a/schema.yml b/schema.yml index ef24994..d7473fc 100644 --- a/schema.yml +++ b/schema.yml @@ -1,25 +1,55 @@ -type: //rec -required: +--- +#title: protocol-schema +type: object +properties: protocol: - type: //rec + type: object + properties: + name: + type: string + longname: + type: string + fields: + type: array + items: + $ref: "#/definitions/field" + osilayer: + type: integer + minimum: 1 + maximum: 7 required: - name: //str - longname: //str - osilayer: //int - fields: &fields - type: //arr - contents: { - type: //rec - required: - field: //str - desc: //str - length: //int - optional: - bitfield: //bool - optional: //bool - nextlayer: //bool - data: //bool - reference: //str - reflenght: //str - subfields: *fields - } + - name + - fields + - osilayer + +definitions: + field: + type: object + properties: + field: + type: string + desc: + type: string + length: + type: integer + bitfield: + type: boolean + optional: + type: boolean + repeatable: + type: boolean + nextlayer: + type: boolean + data: + type: boolean + reference: + type: string + reflength: + type: string + subfields: + type: array + items: + $ref: "#/definitions/field" + required: + - field + - length diff --git a/validate.py b/validate.py index b9de05e..bc1dadb 100644 --- a/validate.py +++ b/validate.py @@ -1,19 +1,24 @@ -import Rx + import sys +from jsonschema import validate +import yaml -def validate(argv): - rx = Rx.Factory(register_core_types=True) - schemafile = "schema.yaml" + +def main(argv): + schemafile = "schema.yml" schema = None - data = None + schemadata = None with open(schemafile, "r") as f: - data = f.read() - schema = rx.make_schema(data) + schemadata = yaml.load(f) + data = None + with open(argv[1], "r") as f: + data = yaml.load(f) # get all yml files from protocols # validate with - schema.validate(data) + validate(data, schemadata) + if __name__ == '__main__': - validate(sys.argv) + main(sys.argv)