Author: ctian Date: Sun Apr 8 08:26:19 2007 New Revision: 12
Added: trunk/asn1.lisp Modified: trunk/README trunk/changelog trunk/classes.lisp trunk/constants.lisp trunk/net-snmp.asd trunk/package.lisp trunk/snmp-api.lisp Log: * Release 0.10 * Add SNMPv3 (authNoPriv) support * One time get multiple value (use snmp-msg-get-list)
Modified: trunk/README ============================================================================== --- trunk/README (original) +++ trunk/README Sun Apr 8 08:26:19 2007 @@ -6,15 +6,17 @@ ;;; NetEase.com, Inc. (http://corp.netease.com)
;;; This package only support: -;;; * Version: SNMPv1, SNMPv2c -;;; * PDU Type: GET -;;; * Just print the result to stdout... +;;; * Version: SNMPv1, SNMPv2c, SNMPv3(authNoPriv) +;;; * PDU Type: GET(single, multiple) +;;; * Return string or number.
;;; I use the CFFI for portable CL support, see http://common-lisp.net/project/cffi/ ;;; Known work on: SBCL and CLISP
;;; Sample usage:
+(in-package :cl-user) + (defun test () (let ((s (make-instance 'snmp:snmp-session :peername "localhost" @@ -22,3 +24,15 @@ :version snmp:+snmp-version-2c+))) (snmp:snmp-msg-get s "sysDescr.0") (snmp:snmp-msg-get "localhost" ".1.3.6.1.2.1.1.1.0"))) + +;;; SNMPv3 and GETs Example +(defun v3-test () + (let ((s (make-instance 'snmp:snmp-session + :peername "binghe.people.163.org" + :version snmp:+snmp-version-3+ + :security-name "binghe" + :password "bBtB0bxs" + :security-auth-proto :hmac-sha1))) + (snmp:snmp-msg-get-list s + '("sysDescr.0" "sysContact.0")))) +;; => ("Linux binghe.people.163.org 2.6.18-4-686 #1 SMP Wed Feb 21 16:06:54 UTC 2007 i686" "Chun Tian (binghe) binghe@163.org")
Added: trunk/asn1.lisp ============================================================================== --- (empty file) +++ trunk/asn1.lisp Sun Apr 8 08:26:19 2007 @@ -0,0 +1,36 @@ +(in-package :org.net-snmp) + +(eval-when (:compile-toplevel :load-toplevel :execute) + (defclass oid () + ((name :type string :reader oid-name :initarg :name) + (length :type integer :reader oid-length) + c-oids + c-oid-len)) + + (defmethod shared-initialize :after ((instance oid) slot-names &rest initargs) + (declare (ignore slot-names initargs)) + (with-slots (name length c-oids c-oid-len) instance + (progn + (setf c-oids (foreign-alloc 'c-oid :count +max-oid-len+) + c-oid-len (foreign-alloc 'c-size-type :initial-element +max-oid-len+)) + (if (and (> (length name) 0) + (eq (elt name 0) #.)) + (c-read-objid name c-oids c-oid-len) + (c-get-node name c-oids c-oid-len)) + (setf length (mem-ref c-oid-len 'c-size-type))))) + + (defmacro snmp-var->value (var) + (let ((v (gensym))) + `(let ((,v ,var)) + (case (foreign-slot-value ,v 'c-variable-list 'c-type) + ;; ASN_OCTET_STR + (,+asn-octet-str+ + (foreign-string-to-lisp + (foreign-slot-value ,v 'c-variable-list 'c-val) + (foreign-slot-value ,v 'c-variable-list 'c-val-len))) + ;; ASN_COUNTER + (,+asn-counter+ + (mem-ref (foreign-slot-value ,v 'c-variable-list 'c-val) + :uint32)) + (otherwise :others)))))) +
Modified: trunk/changelog ============================================================================== --- trunk/changelog (original) +++ trunk/changelog Sun Apr 8 08:26:19 2007 @@ -1,3 +1,9 @@ +Sun Apr 8 20:11:52 CST 2007, ctian + + * Release 0.10 + * SNMPv3 Support (only authNoPriv, both MD5 and SHA1) + * Add a snmp-msg-get-list to request more than one variable per time + Sat Mar 31 00:37:03 CST 2007, ctian * Release 0.01
Modified: trunk/classes.lisp ============================================================================== --- trunk/classes.lisp (original) +++ trunk/classes.lisp Sun Apr 8 08:26:19 2007 @@ -1,5 +1,17 @@ (in-package :org.net-snmp)
+(eval-when (:compile-toplevel :load-toplevel :execute) + (let ((md5 (make-instance 'oid :name ".1.3.6.1.6.3.10.1.1.2")) + (sha1 (make-instance 'oid :name ".1.3.6.1.6.3.10.1.1.3"))) + (defparameter +usm-hmac-md5-auth-protocol+ (slot-value md5 'c-oids)) + (defparameter +usm-hmac-md5-auth-protocol-len+ (oid-length md5)) + (defparameter +usm-hmac-sha1-auth-protocol+ (slot-value sha1 'c-oids)) + (defparameter +usm-hmac-sha1-auth-protocol-len+ (oid-length sha1)))) + +;;; +;;; SNMP Session +;;; + (defclass snmp-session () ((peername :reader snmp-peername :initarg :peername @@ -13,38 +25,79 @@ :initarg :community :type string :initform "public") + (security-name :reader snmp-security-name + :initarg :security-name + :type string) + (security-level :reader snmp-security-level + :initarg :security-level + :type string + :initform +snmp-sec-level-authnopriv+) + (security-auth-proto :reader snmp-security-auth-proto + :initarg :security-auth-proto + :type (member :hmac-md5 :hmac-sha1) + :initform :hmac-md5) + (passphrase :initarg :passphrase + :type string + :initform "binghe") c-session))
(defmethod shared-initialize :after ((instance snmp-session) slot-names &rest initargs) (declare (ignore slot-names initargs)) - (with-slots (peername version community c-session) instance + (with-slots (peername version c-session) instance (progn (setf c-session (foreign-alloc 'c-snmp-session)) (c-snmp-session-init c-session) - (with-foreign-slots ((c-peername c-version c-community c-community-len) - c-session c-snmp-session) - (setf c-peername (foreign-string-alloc peername) - c-version version - c-community (foreign-string-alloc community) - c-community-len (length community)))))) - -(defclass oid () - ((name :type string :reader oid-name :initarg :name) - (length :type integer :reader oid-length) - c-oids - c-oid-len)) - -(defmethod shared-initialize :after ((instance oid) slot-names &rest initargs) - (declare (ignore slot-names initargs)) - (with-slots (name length c-oids c-oid-len) instance - (progn - (setf c-oids (foreign-alloc 'c-oid :count +max-oid-len+) - c-oid-len (foreign-alloc 'c-size-type :initial-element +max-oid-len+)) - (if (and (> (length name) 0) - (eq (elt name 0) #.)) - (c-read-objid name c-oids c-oid-len) - (c-get-node name c-oids c-oid-len)) - (setf length (mem-ref c-oid-len 'c-size-type))))) + (cond ((or (= version +snmp-version-1+) + (= version +snmp-version-2c+)) + (with-slots (community) instance + (with-foreign-slots ((c-peername c-version c-community c-community-len) + c-session c-snmp-session) + (setf c-peername (foreign-string-alloc peername) + c-version version + c-community (foreign-string-alloc community) + c-community-len (length community))))) + ;; SNMPv3 support + ((= version +snmp-version-3+) + (with-slots (security-name security-level security-auth-proto passphrase) + instance + (with-foreign-slots ((c-peername + c-version + c-security-name + c-security-name-len + c-security-level + c-security-auth-proto + c-security-auth-proto-len + c-security-auth-key-len) + c-session c-snmp-session) + (progn + (setf c-peername (foreign-string-alloc peername) + c-version version + c-security-name security-name + c-security-name-len (length security-name) + ;; we only support authNoPriv now + c-security-level +snmp-sec-level-authnopriv+ + c-security-auth-key-len +usm-auth-ku-len+) + (case security-auth-proto + (:hmac-md5 + (setf c-security-auth-proto +usm-hmac-md5-auth-protocol+ + c-security-auth-proto-len +usm-hmac-md5-auth-protocol-len+)) + (:hmac-sha1 + (setf c-security-auth-proto +usm-hmac-sha1-auth-protocol+ + c-security-auth-proto-len +usm-hmac-sha1-auth-protocol-len+))) + (let ((c-passphrase (foreign-string-alloc passphrase))) + (if (/= (c-generate-ku c-security-auth-proto + c-security-auth-proto-len + c-passphrase + (length passphrase) + (foreign-slot-pointer c-session + 'c-snmp-session + 'c-security-auth-key) + (foreign-slot-pointer c-session + 'c-snmp-session + 'c-security-auth-key-len)) + +snmp-err-success+) + (error "Error generating Ku from authentication pass phrase."))))))) + (t (error "unknown snmp version!"))))))
(defmethod snmp-msg-get ((s snmp-session) (o oid)) (car (snmp-msg-get-list s (list o)))) @@ -87,16 +140,3 @@
(defmethod snmp-msg-get-list ((s string) (oids list)) (snmp-msg-get-list (make-instance 'snmp-session :peername s) oids)) - -(defmacro snmp-var->value (var) - (let ((v (gensym))) - `(let ((,v ,var)) - (case (foreign-slot-value ,v 'c-variable-list 'c-type) - (,+asn-octet-str+ - (foreign-string-to-lisp - (foreign-slot-value ,v 'c-variable-list 'c-val) - (foreign-slot-value ,v 'c-variable-list 'c-val-len))) - (,+asn-counter32+ - (mem-ref (foreign-slot-value ,v 'c-variable-list 'c-val) - :uint32)) - (otherwise :others)))))
Modified: trunk/constants.lisp ============================================================================== --- trunk/constants.lisp (original) +++ trunk/constants.lisp Sun Apr 8 08:26:19 2007 @@ -16,7 +16,6 @@ (defconstant +asn-object-id+ #x06) (defconstant +asn-sequence+ #x10) (defconstant +asn-set+ #x11) -(defconstant +asn-counter32+ #x41)
(defconstant +asn-universal+ #b00000000) (defconstant +asn-application+ #b01000000) @@ -26,6 +25,19 @@ (defconstant +asn-primitive+ #b00000000) (defconstant +asn-constructor+ #b00100000)
+;; defined types (from the SMI, RFC 1157) +(defconstant +asn-ipaddress+ (logior +asn-application+ 0)) +(defconstant +asn-counter+ (logior +asn-application+ 1)) +(defconstant +asn-gauge+ (logior +asn-application+ 2)) +(defconstant +asn-unsigned+ (logior +asn-application+ 2)) +(defconstant +asn-timeticks+ (logior +asn-application+ 3)) +(defconstant +asn-opaque+ (logior +asn-application+ 4)) + +;; defined types (from the SMI, RFC 1442) +(defconstant +asn-nsap+ (logior +asn-application+ 5)) +(defconstant +asn-counter64+ (logior +asn-application+ 6)) +(defconstant +asn-uinteger+ (logior +asn-application+ 7)) + ;;; from snmp.h (defconstant +snmp-version-1+ 0) (defconstant +snmp-version-2c+ 1) @@ -76,6 +88,7 @@ (defconstant +snmp-stat-error+ 1) (defconstant +snmp-stat-timeout+ 2)
+(defconstant +snmp-err-success+ 0) (defconstant +snmp-err-noerror+ 0) (defconstant +snmp-err-toobig+ 1) (defconstant +snmp-err-nosuchname+ 2)
Modified: trunk/net-snmp.asd ============================================================================== --- trunk/net-snmp.asd (original) +++ trunk/net-snmp.asd Sun Apr 8 08:26:19 2007 @@ -7,11 +7,12 @@
(defsystem net-snmp :description "Common Lisp interface for Net-SNMP" - :version "0.01" + :version "0.10" :author "Chun Tian (binghe)" :depends-on (:cffi) :components ((:file "package") (:file "constants" :depends-on ("package")) (:file "typedefs" :depends-on ("package")) + (:file "asn1" :depends-on ("constants" "typedefs")) (:file "snmp-api" :depends-on ("constants" "typedefs")) (:file "classes" :depends-on ("snmp-api"))))
Modified: trunk/package.lisp ============================================================================== --- trunk/package.lisp (original) +++ trunk/package.lisp Sun Apr 8 08:26:19 2007 @@ -11,7 +11,8 @@ snmp-msg-get-list ;; constants +snmp-version-1+ - +snmp-version-2c+)) + +snmp-version-2c+ + +snmp-version-3+))
(in-package :org.net-snmp)
Modified: trunk/snmp-api.lisp ============================================================================== --- trunk/snmp-api.lisp (original) +++ trunk/snmp-api.lisp Sun Apr 8 08:26:19 2007 @@ -1,7 +1,5 @@ (in-package :org.net-snmp)
-;;(defcvar ("usmHMACMD5AuthProtocol" *c-usm-hmac-md5-auth-protocol*) :pointer) - (eval-when (:compile-toplevel :load-toplevel) (defcfun ("init_snmp" c-snmp-init) :void (type :string)))
@@ -75,3 +73,10 @@ (objidlen :ulong) (variable :pointer))
+(defcfun ("generate_Ku" c-generate-ku) :int + (hashtype :pointer) + (hashtype-len :uint) + (p :pointer) + (pplen :ulong) + (ku :pointer) + (kulen :pointer))
cl-net-snmp-cvs@common-lisp.net