-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.lisp
72 lines (60 loc) · 3.26 KB
/
request.lisp
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
;;;; -*- Mode: Lisp -*-
;;;; $Id$
(in-package :snmp)
(defun snmp-report (session &key context)
(declare (type v3-session session))
(let ((message (make-instance 'v3-message
:report t
:session session
:context (or context *default-context*)
:pdu (make-instance 'get-request-pdu))))
(let ((reply (send-snmp-message session message)))
(map 'list #'(lambda (x) (coerce x 'list))
(variable-bindings-of (pdu-of reply))))))
(defgeneric snmp-request (session request bindings &key &allow-other-keys)
(:documentation "General SNMP request operation"))
(defmethod snmp-request ((session session) (request symbol) (bindings list)
&key context)
(when bindings
(let ((vb (mapcar #'(lambda (x) (if (consp x)
(list (oid (first x)) (second x))
(list (oid x) nil)))
bindings)))
;; Get a report first if the session is new created.
(when (and (= (version-of session) +snmp-version-3+)
(need-report-p session))
(snmp-report session :context context))
(let ((message (make-instance (gethash (type-of session) *session->message*)
:session session
:context (or context *default-context*)
:pdu (make-instance request
:variable-bindings vb))))
(let ((reply (send-snmp-message session message)))
(when reply
(map 'list #'(lambda (x) (coerce x 'list))
(variable-bindings-of (pdu-of reply)))))))))
(defmethod snmp-request ((host string) request bindings &key context)
(with-open-session (s host)
(snmp-request s request bindings :context context)))
(defmethod snmp-request (session request (binding string) &key context)
(snmp-request session request (list (list (oid binding) nil)) :context context))
(defmethod snmp-request (session request (binding vector) &key context)
(snmp-request session request (list (list (oid binding) nil)) :context context))
(defmethod snmp-request (session request (binding object-id) &key context)
(snmp-request session request (list (list binding nil)) :context context))
(defmethod snmp-request (session request (binding simple-oid) &key context)
(snmp-request session request (list (list binding nil)) :context context))
(defun snmp-get (session bindings &key context)
(let ((result (mapcar #'second
(snmp-request session 'get-request-pdu bindings
:context context))))
(if (consp bindings) result (car result))))
(defun snmp-get-next (session bindings &key context)
(let ((result (snmp-request session 'get-next-request-pdu bindings
:context context)))
(if (consp bindings) result (car result))))
(defun snmp-set (session bindings &key context)
(let ((result (mapcar #'second
(snmp-request session 'set-request-pdu bindings
:context context))))
(if (consp bindings) result (car result))))