Module: BER

Extended by:
BER
Included in:
BER
Defined in:
lib/msnmp.rb

Defined Under Namespace

Classes: C64

Instance Method Summary collapse

Instance Method Details

#cat_enoid(enoids) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/msnmp.rb', line 151

def cat_enoid enoids
  s = ''
  for enoid in enoids
    vb = "\x06" + enc_len(enoid.size) + enoid + "\x05\x00"
    s = s + "\x30" + enc_len(vb.size) + vb
  end
  varbind = "\x30" + enc_len(s.size) + s
end

#dec_cnt(s) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/msnmp.rb', line 200

def dec_cnt s
  n = 0
  while s.size > 0
    n = n << 8 | s.getbyte(0)
    s = s[1..-1]
  end
  return n
end

#dec_cnt64(s) ⇒ Object



209
210
211
# File 'lib/msnmp.rb', line 209

def dec_cnt64 s
  C64.new(s)
end

#dec_int(s) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/msnmp.rb', line 187

def dec_int s
  if (s.getbyte(0) & 0x80) == 0x80
    n = -1
  else
    n = 0
  end
  while s.size > 0
    n = n << 8 |  s.getbyte(0)
    s = s[1..-1]
  end
  return n
end

#dec_msg(msg) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/msnmp.rb', line 237

def dec_msg msg
  if msg[1, 1] < "\x80"
    idx = msg.getbyte(6) + 7
    pdutype, pdu, msg = tlv msg[idx..-1]
  elsif msg[1, 1] == "\x81"
    idx = msg.getbyte(7) + 8
    pdutype, pdu, msg = tlv msg[idx..-1]
  else
    tag, val, msg = tlv msg
    tag, ver, msg = tlv val
    tag, comm, msg = tlv msg
    pdutype, pdu, msg = tlv msg
  end
  idlen = pdu.getbyte 1
  enc_reqid = pdu[0, idlen + 2]
  error_status = pdu.getbyte(idlen + 4)
  error_index = pdu.getbyte(idlen + 7)
  tag, varbind, msg = tlv pdu[idlen+8..-1]
  return enc_reqid, error_status, error_index, varbind
end

#dec_oid(msg) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/msnmp.rb', line 160

def dec_oid msg
  msg = msg.clone
  if msg.getbyte(0) == 0x2b
    oid = [1, 3]
    msg.slice! 0
  else
    oid = []
  end
  until msg.empty?
    c = msg.getbyte 0
    msg.slice! 0
    if c > 127
      id = 0
      while c > 127
        id = (id << 7) + (c & 0x7f)
        c = msg.getbyte 0
        msg.slice! 0
      end
      id = (id << 7) + c
      oid.push id
    else
	oid.push c
    end
  end
  return oid
end

#dec_varbind(msg) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/msnmp.rb', line 283

def dec_varbind msg
  list=[]
  val1 = tag = val2 = nil #XXX
  dec_varbind_block(msg) {|val1, tag, val2|
    list.push([val1,tag,val2])
  }
  return list
end

#dec_varbind_block(msg) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/msnmp.rb', line 258

def dec_varbind_block msg
  while msg.size > 0
    tag,val,msg=tlv(msg)
    tag,val1,msg0=tlv(val)
    tag,val2,msg1=tlv(msg0)
    case tag
    when 0x02
      val2=dec_int(val2)
    when 0x05 #Null
      val2=nil
    when 0x06 #OID
      #val2=dec_oid(val2)
    when 0x40 # IP Address
      val2 = val2.unpack('CCCC').join('.')
    when 0x41, 0x42, 0x43 #Counter or Gauge or Tick
      val2 = dec_cnt val2
    when 0x46 #Counter64
	val2 = dec_cnt64 val2
    when 0x80
      next # skip
    end
    yield val1, tag, val2
  end
end

#enc(tag, val) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/msnmp.rb', line 62

def enc tag, val
  case tag
  when 0x02
    return enc_int(val)
  when 0x04
    return enc_str(val)
  end
end

#enc_a_oid(a) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/msnmp.rb', line 103

def enc_a_oid a
  a.shift; a.shift
  e = '+' # 1.3
  until a.empty?
    subid = a.shift.to_i
    s = ''
    if subid == 0 then
      e += "\x00"
    else
      while subid > 0
        if s.empty?
          s = (subid & 0x7f).chr + s
        else
          s = (subid & 0x7f | 0x80).chr + s
        end
        subid = subid >> 7
      end
      e += s
    end
  end
  e
end

#enc_int(n) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/msnmp.rb', line 83

def enc_int n
  ebuf = ''
  if n < 0 then
    begin
      ebuf = (n & 0xff).chr + ebuf
      n = n >> 8
    end until (n == -1) and (ebuf.getbyte(0) & 0x80 == 0x80)
  else
    begin
      ebuf = (n & 0xff).chr + ebuf
      n = n >> 8
    end while n > 0
  end
  return "\x02" + enc_len(ebuf.size) + ebuf
end

#enc_len(n) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/msnmp.rb', line 71

def enc_len n
  if n < 128 then
    return n.chr
  end
  e_len = ''
  while n > 0
    e_len = (n & 0xff).chr + e_len
    n = n >> 8
  end
  return (e_len.size | 0x80).chr + e_len
end

#enc_oid(oid) ⇒ Object



131
132
133
134
# File 'lib/msnmp.rb', line 131

def enc_oid oid
  e = enc_v_oid oid
  return "\x06" + enc_len(e.size) + e
end

#enc_oid_list(oid_list) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/msnmp.rb', line 136

def enc_oid_list oid_list
  s = ''
  for e in oid_list
    vb = enc_oid(e) + "\x05\x00" # BER.enc_null
    s = s + "\x30" + enc_len(vb.size) + vb
  end
  varbind = "\x30" + enc_len(s.size) + s
end

#enc_str(s) ⇒ Object



99
100
101
# File 'lib/msnmp.rb', line 99

def enc_str s
  return "\x04" + enc_len(s.size) + s
end

#enc_v_oid(oid) ⇒ Object



126
127
128
129
# File 'lib/msnmp.rb', line 126

def enc_v_oid oid
  a = oid.split('.')
  enc_a_oid a
end

#enc_varbind(enoid) ⇒ Object



145
146
147
148
149
# File 'lib/msnmp.rb', line 145

def enc_varbind enoid
  s = "\x06" + enc_len(enoid.size) + enoid + "\x05\x00"
  s = "\x30" + enc_len(s.size) + s
  return "\x30" + enc_len(s.size) + s
end

#tlv(data) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/msnmp.rb', line 216

def tlv data
  tag = data.getbyte 0
  if (len = data.getbyte(1)) < 128
    val = data[2, len]
    remain = data[(len + 2)..-1]
  elsif len == 0x81
    len = data.getbyte 2
    val = data[3, len]
    remain = data[(len + 3)..-1]
  else
    n = len & 0x7f
    len = 0
    for i in 1..n
      len = len * 256 + data.getbyte(i + 1)
    end
    val = data[n + 2, len]
    remain = data[(len + n + 2).. -1]
  end
  return tag, val, remain
end