-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrfc1157.php
563 lines (526 loc) · 12.7 KB
/
rfc1157.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
/**
* phpsnmp - a PHP SNMP library
*
* Copyright (C) 2004 David Eder <david@eder,us>
*
* Based on snmp - a Python SNMP library
* Copyright (C) 2003 Unicity Pty Ltd <libsnmp@unicity.com.au>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @author David Eder <david@eder.us>
* @copyright 2004 David Eder
* @package phpSNMP
* @subpackage rfc1157
* @version .7
*/
/**
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1155.php');
define('ASN_TAG_GET', 0x00);
define('ASN_TAG_GETNEXT', 0x01);
define('ASN_TAG_RESPONSE', 0x02);
define('ASN_TAG_SET', 0x03);
define('ASN_TAG_TRAP', 0x04);
$ASN_TAG_DICT[0xa0] = 'rfc1157_Get';
$ASN_TAG_DICT[0xa1] = 'rfc1157_GetNext';
$ASN_TAG_DICT[0xa2] = 'rfc1157_Response';
$ASN_TAG_DICT[0xa3] = 'rfc1157_Set';
$ASN_TAG_DICT[0xa4] = 'rfc1157_TrapPDU';
$GLOBALS["ASN_TAG_DICT"] = $ASN_TAG_DICT;
/**
* Error Status
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_ErrorStatus extends rfc1155_Integer
{
/**
* Constructor
*
* @param integer $value
*/
public function __construct($value)
{
parent::__construct($value);
}
/**
* ToString
*
* @return string value of this object
*/
public function toString()
{
switch($this->value)
{
case 0: return 'No Error';
case 1: return 'Response message would have been too large';
case 2: return 'There is no such variable name in this MIB';
case 3: return 'The value given has the wrong type';
case 4: return 'Object is Read Only';
}
return 'An unknown error occurred';
}
}
/**
* Variable Binding
*
* This binds a name to an object
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_VarBind extends rfc1155_Sequence
{
/**
* Constructor
*
* @param rfc1155_ObjectID $name
* @param rfc1155_Asn1Object $value
*/
public function __construct($name=NULL, $value=NULL)
{
if($name && !is_a($name, 'rfc1155_ObjectID'))
trigger_error('name must be an rfc1155_ObjectID', E_USER_WARNING);
if($value && !is_a($value, 'rfc1155_Asn1Object'))
trigger_error('value must be an rfc1155_Asn1Object', E_USER_WARNING);
parent::__construct(array($name, $value));
}
}
/**
* Variable Binding List
*
* A Sequence of VarBinds
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_VarBindList extends rfc1155_SequenceOf
{
/**
* Constructor
*
* @param array $value of rfc1157_VarBind
*/
public function __construct($value=array())
{
parent::__construct('rfc1157_VarBind', $value);
}
}
/**
* Message
*
* A Message is the base comms type for all SNMP messages
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_Message extends rfc1155_Sequence
{
/**
* Constructor
*
* @param integer $version
* @param string $community
* @param rfc1157_PDU $pdu
*/
public function __construct($version=0, $community='public', $pdu=NULL)
{
parent::__construct();
if(is_null($pdu)) $pdu = new rfc1157_PDU();
$this->value = array(new rfc1155_Integer($version), new rfc1155_OctetString($community), $pdu);
}
/**
* Get/Set version
*
* @param integer $value
* @return integer
*/
public function version($value=NULL)
{
if(!is_null($value)) $this->value[0] = new rfc1155_Integer($value);
return $this->value[0]->value;
}
/**
* Get/Set community
*
* @param string $value
* @return string
*/
public function community($value=NULL)
{
if(!is_null($value)) $this->value[1] = new rfc1155_OctetString($value);
return $this->value[1]->value;
}
/**
* Get/Set PDU
*
* @param rfc1157_PDU $value
* @return rfc1157_PDU
*/
public function pdu($value=NULL)
{
if(!is_null($value)) $this->value[2] = $value;
return $this->value[2];
}
/**
* Decode Stream
*
* decode() an octet stream into a sequence of Asn1Objects
*
* @param string $stream
* @return rfc1157_Message
*/
public function decode($stream)
{
$this->value = parent::decode($stream);
if(count($this->value) != 1)
trigger_error('Malformed Message: More than one object decoded.', E_USER_WARNING);
$this->value = $this->value[0]->value;
if(count($this->value) != 3)
trigger_error('Malformed Message: Incorrect sequence length ' . count($this->value[0]->value), E_USER_WARNING);
return $this;
}
}
/**
* PDU
*
* Base clss for a non-trap PDU
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_PDU extends rfc1155_Sequence // Base class for a non-trap PDU
{
/**
* Constructor
*
* @param integer $requestID
* @param integer $errorStatus
* @param integer $errorIndex
* @param array $varBindList
*/
public function __construct($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
{
/* this allows you to create a new object with no arguments, arguments of the class ultimately desired (eg Integer)
or, to make like easier, it will convert basic strings and ints into the ultimately desired objects. */
parent::__construct();
$this->asnTagClass = ASN_TAG_CLASS_CONTEXT;
$this->value = array(new rfc1155_Integer($requestID), new rfc1157_ErrorStatus($errorStatus),
new rfc1155_Integer($errorIndex), new rfc1157_VarBindList($varBindList));
}
/**
* Get/Set Request ID
*
* @param integer $value
* @return integer
*/
public function requestID($value=NULL)
{
if(!is_null($value)) $this->value[0] = new rfc1155_Integer($value);
return $this->value[0]->value;
}
/**
* Get/Set Error Status
*
* @param integer $value
* @return integer
*/
public function errorStatus($value=NULL)
{
if(!is_null($value)) $this->value[1] = new rfc1157_ErrorStatus($value);
return $this->value[1]->value;
}
/**
* Get Error String
*
* @return string
*/
public function errorString()
{
return $this->value[1]->toString();
}
/**
* Get/Set Error Index
*
* @param integer $value
* @return integer
*/
public function errorIndex($value=NULL)
{
if(!is_null($value)) $this->value[2] = new rfc1155_Integer($value);
return $this->value[2]->value;
}
/**
* Get/Set Var Bind List
*
* @param rfc1157_VarBindList $value
* @return rfc1157_VarBindList
*/
public function varBindList($value=NULL)
{
if(!is_null($value)) $this->value[3] = new rfc1157_VarBindList($value);
return $this->value[3]->value;
}
/**
* Decode into a PDU Object
*
* @param string $stream
* @return rfc1157_PDU
*/
public function decodeContents($stream)
{
parent::decodeContents($stream);
if(count($this->value) != 4)
trigger_error('Malformed PDU: Incorrect length ' . count($this->value), E_USER_WARNING);
$this->value[1] = new rfc1157_ErrorStatus($this->value[1]->value);
$this->value[3] = new rfc1157_VarBindList($this->value[3]->value);
return $this;
}
}
/**
* GET request
*
* A Get Request PDU
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_Get extends rfc1157_PDU
{
/**
* Constructor
*
* @param integer $requestID
* @param integer $errorStatus
* @param integer $errorIndex
* @param array $varBindList
*/
public function __construct($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
{
parent::__construct($requestID, $errorStatus, $errorIndex, $varBindList);
$this->asnTagNumber = ASN_TAG_GET;
}
}
/**
* GETNEXT request
*
* A GetNext Request PDU
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_GetNext extends rfc1157_PDU
{
/**
* Constructor
*
* @param integer $requestID
* @param integer $errorStatus
* @param integer $errorIndex
* @param array $varBindList
*/
public function __construct($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
{
parent::__construct($requestID, $errorStatus, $errorIndex, $varBindList);
$this->asnTagNumber = ASN_TAG_GETNEXT;
}
}
/**
* RESPONSE request
*
* A Response PDU
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_Response extends rfc1157_PDU
{
/**
* Constructor
*
* @param integer $requestID
* @param integer $errorStatus
* @param integer $errorIndex
* @param array $varBindList
*/
public function __construct($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
{
parent::__construct($requestID, $errorStatus, $errorIndex, $varBindList);
$this->asnTagNumber = ASN_TAG_GET;
}
}
/**
* SET request
*
* A Set Request PDU
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_Set extends rfc1157_PDU
{
/**
* Constructor
*
* @param integer $requestID
* @param integer $errorStatus
* @param integer $errorIndex
* @param array $varBindList
*/
public function __construct($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
{
parent::__construct($requestID, $errorStatus, $errorIndex, $varBindList);
$this->asnTagNumber = ASN_TAG_SET;
}
}
define('TRAP_COLDSTART', 0);
define('TRAP_WARMSTART', 1);
define('TRAP_LINKDOWN', 2);
define('TRAP_LINKUP', 3);
define('TRAP_AUTH_FAIL', 4);
define('TRAP_EGP_NEIGHBOR_LOSS', 5);
define('TRAP_ENTERPRISE_SPECIFIC', 6);
/**
* Generic Trap
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_GenericTrap extends rfc1155_Integer
{
var $genericTraps;
/**
* Constructor
*
* @param integer $value
*/
public function __construct($value)
{
parent::__construct($value);
}
}
/**
* Trap PDU
*
* @package phpSNMP
* @subpackage rfc1157
*/
class rfc1157_TrapPDU extends rfc1155_Sequence
{
/**
* Constructor
*
* @param string $enterprise
* @param string $agentAddr
* @param integer $genericTrap
* @param integer $specificTrap
* @param integer $timestamp
* @param array $varBindList
*/
public function __construct($enterprise=NULL, $agentAddr=NULL, $genericTrap=NULL, $specificTrap=NULL, $timestamp=NULL, $varBindList=array())
{
parent::__construct();
$this->asnTagClass = ASN_TAG_CLASS_CONTEXT;
$this->asnTagNumber = ASN_TAG_TRAP;
$this->value = array(new rfc1155_ObjectID($enterprise), new rfc1155_NetworkAddress($agentAddr),
new rfc1157_GenericTrap($genericTrap), new rfc1155_Integer($specificTrap),
new rfc1155_TimeTicks($timestamp), new rfc1157_VarBindList($varBindList));
}
/**
* Get/Set Enterprise OID
*
* @param string $value
* @return string
*/
public function enterprise($value=NULL)
{
if(!is_null($value)) $this->value[0] = new rfc1155_ObjectID($value);
return $this->value[0]->value;
}
/**
* Get/Set Agent Address
*
* @param string $value
* @return string
*/
public function agentAddr($value=NULL)
{
if(!is_null($value)) $this->value[1] = new rfc1155_NetworkAddress($value);
return $this->value[1]->value;
}
/**
* Get/Set Generic Trap
*
* @param integer $value
* @return integer
*/
public function genericTrap($value=NULL)
{
if(!is_null($value)) $this->value[2]->value = $value;
return $this->value[2]->value;
}
/**
* Get/Set Specific Trap
*
* @param integer $value
* @return integer
*/
public function specificTrap($value=NULL)
{
if(!is_null($value)) $this->value[3]->value = $value;
return $this->value[3]->value;
}
/**
* Get/Set Timestamp
*
* @param integer $value
* @return integer
*/
public function timestamp($value=NULL)
{
if(!is_null($value)) $this->value[4]->value = $value;
return $this->value[4]->value;
}
/**
* Get/Set Var Bind List
*
* @param rfc1157_VarBindList $value
* @return rfc1157_VarBindList
*/
public function VarBindList($value=NULL)
{
if(!is_null($value)) $this->value[5] = $value;
return $this->value[5];
}
/**
* Decode into a Get PDU Object
*
* @param string $stream
* @return rfc1157_TrapPDU
*/
public function decodeContents($stream)
{
parent::decodeContents($stream);
if(count($this->value) != 6)
trigger_error('Malformed TrapPDU: Incorrect length ' . count($this->value), E_USER_WARNING);
$this->value[1] = new rfc1155_NetworkAddress($this->value[1]->value);
$this->value[5] = new rfc1157_VarBindList($this->value[5]->value);
return $this;
}
}
?>