#!/apps/rebol/rebol -qw

REBOL [

Title: "SNMP v1 Trap model"

Date: 30-Oct-2000

Author: "Phil Hayes"

Version: 0.1

Owner: "The Cnut Archetype Ltd - www.cnut.dircon.co.uk"

Rights: "Copyright (C) The Cnut Archetype Ltd 2000"

Purpose: {

This script allows SNMP v1 MIB II traps to be built using

a simple data model.

}

Comment: {

N.B. This version does not allow variable bindings to be issued.

Version 0.2 will allow variable bindings to be sent with enterprise specific

traps.

}

History: [

0.1 [ 30-Oct-2000 "First Draft" "PH" ]

]

Language: 'English

]

; Constants

trapV1-pdu: #{a4}

int-type: #{02}

octet-type: #{04}

objid-type: #{06}

header-type: #{30}

ipaddr-type: #{40}

timeticks-type: #{43}

coldStart: 0

warmStart: 1

linkDown: 2

linkUp: 3

authFailure: 4

egpNeighborLoss: 5

enterpriseSpecific: 6

; Protocal Data Unit ( PDU ) var

pdu-var: make object! [

pv-type: none

pv-length: none

pv-value: none

func1: func [ n [ integer! ] /local r i ] [

r: copy #{}

i: to-integer ( n / 128 )

if ( i < 128 ) [

i: i + 128

]

insert r to-binary to-block i

i: to-integer ( n // 128 )

append r ( to-binary to-block i )

return r

]

SetValue: func [ val /local v1 i ] [

switch/default type?/word val [

integer! [ 

pv-type: int-type

pv-value: make binary! to-block val

]

string! [ 

pv-type: octet-type

pv-value: make binary! val

]

tuple! [ 

pv-type: ipaddr-type

v1: to-string val

replace/all v1 "." " "

pv-value: to-binary to-block v1

]

issue! [

pv-type: objid-type

replace/all val "." " "

pv-value: copy #{2b06010401}

foreach v1 to-block val [

i: to-integer v1

either i > 127 [

append pv-value ( func1 i )

] [

append pv-value to-binary to-block i

]

]

]

] [ 

pv-type: #{00}

pv-value: #{00}

]

pv-length: make binary! to-block length? pv-value 

; print [ "pdu-var data [" self/GetData "]" ]

; print [ "pdu-var length [" self/GetLength "]" ]

]

GetData: func [ /local v1 ] [

v1: make binary! []

append v1 pv-type

append v1 pv-length

append v1 pv-value

return v1

]

GetLength: func [] [

return length? self/GetData

]

]

size-var: make object! [

type: #{82}

filler: #{00}

length: none

SetLength: func [ len [integer!] ] [

length: make binary! to-block len

; print [ "size-var data [" self/GetData "]" ]

; print [ "size-var length [" self/GetLength "]" ]

]

GetData: func [ /local v1 ] [

v1: make binary! []

append v1 type

append v1 filler

append v1 length

return v1

]

GetLength: func [] [

return to-integer length

]

]

var-bind: make object! [

vb-header: header-type

vb-size: make size-var []

GetData: func [ /local v1 ] [

v1: make binary! []

append v1 vb-header

append v1 vb-size/GetData

return v1 

]

]

trap-pdu: make object! [

pdu-type: trapV1-pdu

size: make size-var []

enterprise: make pdu-var []

agent-addr: make pdu-var []

generic-trap: make pdu-var []

specific-trap: make pdu-var []

timestamp: make pdu-var []

varbindings: make var-bind []

SetSize: func [] [

size/SetLength self/GetLength

]

GetLength: func [ /local v1 ] [

v1: copy #{}

append v1 enterprise/GetData

append v1 agent-addr/GetData

append v1 generic-trap/GetData

append v1 specific-trap/GetData

append v1 timestamp/GetData

append v1 varbindings/GetData

return length? v1

]

GetData: func [ /local v1 ] [

v1: copy #{}

append v1 pdu-type

append v1 size/GetData

append v1 enterprise/GetData

append v1 agent-addr/GetData

append v1 generic-trap/GetData

append v1 specific-trap/GetData

append v1 timestamp/GetData

append v1 varbindings/GetData

return v1

]

SetTimeTicks: func [ i [integer!] ] [

timestamp/pv-type: timeticks-type

timestamp/pv-value: make binary! to-block i

; timestamp/pv-value: #{092ac9bc}

timestamp/pv-length: make binary! to-block length? timestamp/pv-value 

]

]

snmp-msg: make object! [

sm-header: header-type

sm-size: make size-var []

sm-version: make pdu-var []

sm-community: make pdu-var []

sm-pdu: make trap-pdu []

SetSize: func [] [

sm-size/SetLength self/GetLength

]

GetLength: func [ /local i ] [

i: sm-version/GetLength + sm-community/GetLength + length? sm-pdu/GetData

; print [ "snmp-msg length [" i "]" ]

return i

]

GetData: func [ /local v1 ] [

v1: copy #{}

append v1 sm-header

append v1 sm-size/GetData

append v1 sm-version/GetData

append v1 sm-community/GetData

append v1 sm-pdu/GetData

return v1

]

]




**********************************************************************
This email and any attachments may be confidential and the subject of 
legal professional privilege.  Any disclosure, use, storage or copying 
of this email without the consent of the sender is strictly prohibited.
Please notify the sender immediately if you are not the intended 
recipient and then delete the email from your inbox and do not 
disclose the contents to another person, use, copy or store the 
information in any medium. 
**********************************************************************
-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to