/visualization/application/apply-model.rkt

http://github.com/tnelson/Margrave · Racket · 157 lines · 108 code · 19 blank · 30 comment · 15 complexity · 094d09fd96f3fd7c798f771b683992a3 MD5 · raw file

  1. #lang racket/gui
  2. (provide mg-model% apply-model apply-model/pos)
  3. (require "modelgraph.rkt" "visxml.rkt" xml "../../margrave-xml.rkt")
  4. ; Checks to see if a named entity matches a specific label (ipsrc or ipdest, generally)
  5. ; This is what we're looking for:
  6. ; <RELATION name="whatever">
  7. ; <TUPLE>
  8. ; <ATOM>ipsrc</ATOM>
  9. ; </TUPLE>
  10. ; </RELATION>
  11. (define (check-src-dest xml name label)
  12. (and (element? xml)
  13. (and (symbol=? (element-name xml) 'RELATION)
  14. (and (string=? (get-attribute-value xml 'name) name)
  15. (string=? (get-pc-data (get-child-element (get-child-element xml 'TUPLE) 'ATOM)) label)))))
  16. ; Returns a list of firewall policy decisions for a named entity
  17. ; This is from IDBOUTPUT, and we have to parse the string because the info we need is
  18. ; just in the pcdata of an <ANNOTATION> element
  19. (define (get-fwps loa pname)
  20. (cond [(empty? loa) empty]
  21. [else
  22. (let ([ss (regexp-split #rx":" (get-pc-data (first loa))) ])
  23. (if (string=? (first ss) pname)
  24. (cons (hash-ref results-hash (string->symbol (first (regexp-split #rx" is" (second ss))))) (get-fwps (rest loa) pname))
  25. (get-fwps (rest loa) pname))
  26. )
  27. ]))
  28. ; Begins at start and tries to find a path exhausting every node in lon.
  29. (define (edge-path mg start lon)
  30. (cond [(empty? lon) #f]
  31. [(= (length lon) 1)
  32. (begin
  33. (print (send start get-name))
  34. (print (send (first lon) get-name))
  35. (send (send mg find-edge start (first lon)) set-active! #t)
  36. (send (send mg find-edge start (first lon)) set-blocked! (list? (memq result-deny (send start get-results)))))]
  37. [else
  38. (let ([next (first (filter (lambda (n) (send mg find-edge start n)) lon))])
  39. (begin
  40. (send (send mg find-edge start next) set-active! #t)
  41. (send (send mg find-edge start next) set-blocked! (list? (memq result-deny (send start get-results))))
  42. (edge-path mg next (remv next lon))))]))
  43. ; This class stores the xml model and provides functions for deriving model information from xml data.
  44. (define mg-model%
  45. (class object%
  46. (init-field
  47. [keyword-map (make-hash)] ; Not every policy will use the same vocabulary
  48. [xml null]
  49. )
  50. ; Returns the list of policy decisions made by that entity or empty
  51. ; if the entity was not involved in the model
  52. (define/public (get-entity-data policyname)
  53. (get-fwps (get-child-elements xml 'ANNOTATION) policyname))
  54. ; Updates a modelgraph so the edges have correct active and blocked status
  55. ; It must return the model.
  56. (define/public (set-edge-data mg)
  57. (let ([srcl (filter (lambda (n) (send n is-source?)) (send mg get-nodes))]
  58. [destl (filter (lambda (n) (send n is-dest?)) (send mg get-nodes))]
  59. [active-ents (filter (lambda (n) (not (empty? (send n get-results)))) (send mg get-nodes))])
  60. (if (and (not (empty? srcl)) (not (empty? destl)))
  61. (begin
  62. (edge-path mg (first srcl) (append active-ents (list (first destl))) )
  63. mg) mg)
  64. ))
  65. ; Returns true if the supplied entity is the src host
  66. (define/public (is-src? entname)
  67. (ormap (lambda (x) (check-src-dest x entname (hash-ref keyword-map 'ipsrc))) (element-content xml)))
  68. ; Returns true if the supplied entity is the desination
  69. (define/public (is-dest? entname)
  70. (ormap (lambda (x) (check-src-dest x entname (hash-ref keyword-map 'ipdest))) (element-content xml)))
  71. (define/public (get-xml) xml)
  72. (super-new)
  73. ))
  74. ; Helper functions for apply-model
  75. ; Consumes a netgraph-edge and a model
  76. ; Returns a modelgraph-edge
  77. (define (convert-edge e model nodemap)
  78. (new modelgraph-edge%
  79. [from (hash-ref nodemap (send e get-from))]
  80. [to (hash-ref nodemap (send e get-to))]
  81. [active #f]
  82. [blocked #f]
  83. ))
  84. ; Consumes a netgraph-node and a model
  85. ; Returns a modelgraph-node
  86. (define (convert-node n model nodemap)
  87. (let ([newnode
  88. (new modelgraph-node%
  89. [name (send n get-name)]
  90. [type (send n get-type)]
  91. [policy (send n get-policy)]
  92. [vocabname (send n get-vocabname)]
  93. [subgraph (if (null? (send n get-subgraph)) null (apply-model (send n get-subgraph) model))]
  94. [source? (send model is-src? (send n get-vocabname))]
  95. [dest? (send model is-dest? (send n get-vocabname))]
  96. [results empty])])
  97. (begin
  98. (hash-set! nodemap n newnode)
  99. newnode)))
  100. ; Consumes a pos-netgraph-node and a model
  101. ; Returns a pos-modelgraph-node
  102. (define (convert-node/pos n model nodemap)
  103. (let ([newnode
  104. (new pos-modelgraph-node%
  105. [name (send n get-name)]
  106. [type (send n get-type)]
  107. [policy (send n get-policy)]
  108. [vocabname (send n get-vocabname)]
  109. [subgraph (if (null? (send n get-subgraph)) null (apply-model/pos (send n get-subgraph) model))]
  110. [results (if (null? (send n get-policy)) empty (send model get-entity-data (send n get-policy)))]
  111. [source? (send model is-src? (send n get-vocabname))]
  112. [dest? (send model is-dest? (send n get-vocabname))]
  113. [x (send n get-x)]
  114. [y (send n get-y)]
  115. )])
  116. (begin
  117. (hash-set! nodemap n newnode)
  118. newnode)))
  119. ; Consumes a netgraph and a model
  120. ; Returns a modelgraph with the model details applied to the nodes and edges
  121. (define (apply-model ng model)
  122. (let ( [nodemap (make-hash)] )
  123. (_apply-model ng model
  124. (lambda (n) (convert-node n model nodemap))
  125. (lambda (e) (convert-edge e model nodemap)))))
  126. ; Consumes a netgraph (with positional nodes) and a model
  127. ; Returns a modelgraph with position data on the nodes
  128. (define (apply-model/pos ng model)
  129. (let ( [nodemap (make-hash)] )
  130. (_apply-model ng model
  131. (lambda (n) (convert-node/pos n model nodemap))
  132. (lambda (e) (convert-edge e model nodemap)))))
  133. ; Helper for apply-model functions
  134. (define (_apply-model ng model nf ef)
  135. (send model set-edge-data (new modelgraph%
  136. [nodes (map nf (send ng get-nodes))]
  137. [edges (map ef (send ng get-edges))]
  138. )))