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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
|
//=============================================================================
/**
* @file oid.cpp
*
* This module contains the implementation of the oid class. This
* includes all protected and public member functions. The oid class
* may be compiled stand alone without the use of any other library.
*
*
* @author Peter E MellquistMichael R MacFaden mrm@cisco.com - rework & ACE port
*/
//=============================================================================
/*===================================================================
Copyright (c) 1996
Hewlett-Packard Company
ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
Permission to use, copy, modify, distribute and/or sell this software
and/or its documentation is hereby granted without fee. User agrees
to display the above copyright notice and this license notice in all
copies of the software and any documentation of the software. User
agrees to assume all liability for the use of the software; Hewlett-Packard
makes no representations about the suitability of this software for any
purpose. It is provided "AS-IS" without warranty of any kind,either express
or implied. User hereby grants a royalty-free license to any and all
derivatives based upon this software code base.
=====================================================================*/
//---------[ external C libaries used ]--------------------------------
#include "asnmp/oid.h" // include def for oid class
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_Memory.h"
#include "ace/OS_NS_ctype.h"
enum Defs {SNMPBUFFSIZE=300,
SNMPCHARSIZE=15}; // max oid value (4294967295UL)
#define NO_MEM_STR "ERROR: Oid::to_string: memory allocation failure"
//=============[Oid::get_syntax(void)]====================================
SmiUINT32 Oid::get_syntax()
{
return sNMP_SYNTAX_OID;
}
//=============[Oid::Oid( const char *dotted_string ]=====================
// constructor using a dotted string
//
// do a string to oid using the string passed in
Oid::Oid( const char * dotted_oid_string, size_t size)
{
// can't init enum SmiValue so just memset it clean
set_null();
size_t z;
if ((z = ACE_OS::strlen(dotted_oid_string)) == 0) {
set_invalid();
return;
}
if (size == (unsigned int)-1)
size = z;
if (size > z)
size = z;
char *ptr = (char *)dotted_oid_string;;
if (size < z) {
// create new buffer if needed
ACE_NEW(ptr, char [size]);
// sz should be in StrToOid?
ACE_OS::memcpy( (void *)ptr, dotted_oid_string, size);
}
size_t byte_counter;
if (StrToOid( (char *) ptr, &smival.value.oid, byte_counter) < 0)
set_invalid();
if (ptr != dotted_oid_string)
delete [] ptr;
}
//=============[Oid::Oid( const Oid &oid) ]================================
// constructor using another oid object
//
// do an oid copy using the oid object passed in
Oid::Oid ( const Oid &oid)
: SnmpSyntax (oid)
{
set_null();
// allocate some memory for the oid
// in this case the size to allocate is the same
// size as the source oid
if (oid.smival.value.oid.len) {
ACE_NEW(smival.value.oid.ptr, SmiUINT32[ oid.smival.value.oid.len]);
size_t byte_counter;
OidCopy( (SmiLPOID) &(oid.smival.value.oid),(SmiLPOID)
&smival.value.oid, byte_counter);
}
}
//=============[Oid::Oid( const unsigned long *raw_oid, int oid_len) ]====
// constructor using raw numeric form
//
// copy the integer values into the private member
Oid::Oid(const unsigned long *raw_oid, size_t oid_len)
{
set_null();
smival.syntax = sNMP_SYNTAX_OID;
set_invalid();
if (raw_oid && oid_len > 0) {
ACE_NEW(smival.value.oid.ptr, SmiUINT32[ oid_len]);
smival.value.oid.len = oid_len;
for (size_t i=0; i < oid_len; i++)
smival.value.oid.ptr[i] = raw_oid[i];
}
}
//=============[Oid::~Oid]==============================================
// destructor
//
// free up the descriptor space
Oid::~Oid()
{
// free up the octet deep memory
if ( smival.value.oid.ptr ) {
set_invalid();
}
// free up the output string
if ( iv_str != 0)
delete [] iv_str;
}
//=============[Oid::operator = const char * dotted_string ]==============
// assignment to a string operator overloaded
//
// free the existing oid
// create the new oid from the string
// return this object
void Oid::set_data( const char *dotted_oid_string)
{
// delete the old value
if ( smival.value.oid.ptr ) {
set_invalid();
}
// assign the new value
size_t byte_counter;
if (StrToOid( (char *) dotted_oid_string, &smival.value.oid, byte_counter) <0)
set_invalid();
}
//=============[Oid:: operator = const Oid &oid ]==========================
// assignment to another oid object overloaded
//
// free the existing oid
// create a new one from the object passed in
// TODO: measure perf vs memory of no realloc in case where len >= oid.len
Oid& Oid::operator=( const Oid &oid)
{
// protect against assignment from self
if ( this == &oid)
return *this;
set_invalid();
// check for zero len on source
if ( oid.smival.value.oid.len == 0)
return *this;
const SmiLPOID srcOid = (SmiLPOID) &(oid.smival.value.oid);
init_value(srcOid, oid.smival.value.oid.len);
return *this;
}
// assign this object the oid, set to invalid if copy fails
void Oid::init_value(const SmiLPOID srcOid, size_t len)
{
// allocate some memory for the oid
ACE_NEW(smival.value.oid.ptr, SmiUINT32[ len]);
size_t byte_counter;
OidCopy( srcOid, (SmiLPOID) &smival.value.oid, byte_counter);
}
void Oid::init_value(const unsigned long *raw_oid, size_t oid_len)
{
if (smival.value.oid.ptr)
delete [] smival.value.oid.ptr;
ACE_NEW(smival.value.oid.ptr, SmiUINT32[ oid_len]);
ACE_OS::memcpy((SmiLPBYTE) smival.value.oid.ptr,
(SmiLPBYTE) raw_oid,
(size_t) (oid_len * sizeof(SmiUINT32)));
smival.value.oid.len = oid_len;
}
//==============[Oid:: operator += const char *a ]=========================
// append operator, appends a string
//
// allocate some space for a max oid string
// extract current string into space
// concat new string
// free up existing oid
// make a new oid from string
// delete allocated space
Oid& Oid::operator+=( const char *a)
{
unsigned long n;
if (!a)
return *this;
if ( *a=='.')
a++;
size_t sz = ACE_OS::strlen(a);
if (valid()) {
n = (smival.value.oid.len *SNMPCHARSIZE) + smival.value.oid.len + 1 + sz;
char *ptr;
ACE_NEW_RETURN(ptr, char[ n], *this);
size_t byte_counter;
if (OidToStr(&smival.value.oid, n,ptr, byte_counter) > 0) {
delete [] ptr;
set_invalid();
return *this;
}
if (ACE_OS::strlen(ptr))
ACE_OS::strcat(ptr,".");
ACE_OS::strcat(ptr,a);
if ( smival.value.oid.len !=0) {
set_invalid();
}
if (StrToOid( (char *) ptr, &smival.value.oid, byte_counter) < 0) {
set_invalid();
}
delete [] ptr;
}
else {
size_t byte_counter;
if (StrToOid( (char *) a, &smival.value.oid, byte_counter) < 0) {
set_invalid();
}
}
return *this;
}
//=============[ bool operator == oid,oid ]=================================
// equivlence operator overloaded
bool operator==( const Oid &lhs, const Oid &rhs)
{
// ensure same len, then use left_comparison
if (rhs.length() != lhs.length())
return false;
if( lhs.left_comparison( rhs.length(), rhs) == 0)
return true;
else
return false;
}
//==============[ bool operator!=( Oid &x,Oid &y) ]=======================
//not equivlence operator overloaded
bool operator!=( const Oid &lhs,const Oid &rhs)
{
return (!(lhs == rhs));
}
//==============[ bool operator<( Oid &x,Oid &y) ]========================
// less than < overloaded
bool operator<( const Oid &lhs,const Oid &rhs)
{
int result;
// call left_comparison with the current
// Oidx, Oidy and len of Oidx
if ((result = lhs.left_comparison( rhs.length(), rhs)) < 0)
return true;
else if (result > 0)
return false;
else{
// if here, equivalent substrings, call the shorter one <
if (lhs.length() < rhs.length())
return true;
else
return false;
}
}
//==============[ bool operator<=( Oid &x,Oid &y) ]=======================
// less than <= overloaded
bool operator<=( const Oid &x,const Oid &y)
{
if ( (x < y) || (x == y) )
return true;
else
return false;
}
//==============[ bool operator>( Oid &x,Oid &y) ]========================
// greater than > overloaded
bool operator>( const Oid &x,const Oid &y)
{
// just invert existing <=
if (!(x<=y))
return true;
else
return false;
}
//==============[ bool operator>=( Oid &x,Oid &y) ]=======================
// greater than >= overloaded
bool operator>=( const Oid &x,const Oid &y)
{
// just invert existing <
if (!(x<y))
return true;
else
return false;
}
//===============[Oid::oidval ]=============================================
// return the WinSnmp oid part
SmiLPOID Oid::oidval()
{
return (SmiLPOID) &smival.value.oid;
}
//===============[Oid::set_data ]==---=====================================
// copy data from raw form...
void Oid::set_data( const unsigned long *raw_oid, const size_t oid_len)
{
if (smival.value.oid.len < oid_len) {
if ( smival.value.oid.ptr) {
set_invalid();
}
}
init_value(raw_oid, oid_len);
}
//===============[Oid::len ]================================================
// return the len of the oid
size_t Oid::length() const
{
return smival.value.oid.len;
}
//===============[Oid::trim( unsigned int) ]============================
// trim off the n leftmost values of an oid
// Note!, does not adjust actual space for
// speed
void Oid::trim( const size_t n)
{
// verify that n is legal
if ((n<=smival.value.oid.len)&&(n>0)) {
smival.value.oid.len -= n;
if (smival.value.oid.len == 0) {
set_invalid();
}
}
}
//===============[Oid::set_invalid() ]====================
// make this object invalid by resetting all values
void Oid::set_invalid() {
delete [] smival.value.oid.ptr;
smival.value.oid.ptr = 0;
smival.value.oid.len = 0;
delete [] iv_str;
iv_str = 0;
}
//===============[Oid::set_null() ]====================
void Oid::set_null() {
smival.syntax = sNMP_SYNTAX_OID;
smival.value.oid.ptr = 0;
smival.value.oid.len = 0;
iv_str = 0;
}
//===============[Oid::operator += const unsigned int) ]====================
// append operator, appends an int
//
// allocate some space for a max oid string
// extract current string into space
// concat new string
// free up existing oid
// make a new oid from string
// delete allocated space
Oid& Oid::operator+=( const unsigned long i)
{
unsigned long n = (smival.value.oid.len * SNMPCHARSIZE)
+ ( smival.value.oid.len -1) + 1 + 4;
char buffer[SNMPBUFFSIZE];
// two cases: null oid, existing oid
if (valid()) {
// allocate some temporary space
char *ptr;
ACE_NEW_RETURN(ptr, char[ n], *this);
size_t byte_counter;
if (OidToStr(&smival.value.oid, n, ptr, byte_counter) < 0) {
set_invalid();
delete [] ptr;
return *this;
}
if (ACE_OS::strlen(ptr))
ACE_OS::strcat(ptr,".");
if (ACE_OS::sprintf( buffer,"%lu",i) != -1) {
ACE_OS::strcat(ptr, buffer);
if ( smival.value.oid.ptr ) {
set_invalid();
}
if (StrToOid( (char *) ptr, &smival.value.oid, byte_counter) < 0) {
set_invalid();
}
delete [] ptr;
}
}
else {
init_value((const unsigned long *)&i, (size_t)1);
}
return *this;
}
//===============[Oid::operator += const Oid) ]========================
// append operator, appends an Oid
//
// allocate some space for a max oid string
// extract current string into space
// concat new string
// free up existing oid
// make a new oid from string
// delete allocated space
Oid& Oid::operator+=( const Oid &o)
{
SmiLPUINT32 new_oid;
if (o.smival.value.oid.len == 0)
return *this;
ACE_NEW_RETURN(new_oid,
SmiUINT32[ smival.value.oid.len + o.smival.value.oid.len], *this);
if (smival.value.oid.ptr) {
ACE_OS::memcpy((SmiLPBYTE) new_oid,
(SmiLPBYTE) smival.value.oid.ptr,
(size_t) (smival.value.oid.len*sizeof(SmiUINT32)));
delete [] smival.value.oid.ptr;
}
// out with the old, in with the new...
smival.value.oid.ptr = new_oid;
ACE_OS::memcpy((SmiLPBYTE) &new_oid[smival.value.oid.len],
(SmiLPBYTE) o.smival.value.oid.ptr,
(size_t) (o.smival.value.oid.len*sizeof(SmiUINT32)));
smival.value.oid.len += o.smival.value.oid.len;
return *this;
}
// return string portion of the oid
//
const char * Oid::to_string()
{
unsigned long n;
if (!valid())
return ""; // be consistent with other classes
// the worst case char len of an oid can be..
// oid.len*3 + dots in between if each oid is XXXX
// so.. size = (len*4) + (len-1) + 1 , extra for a null
n = (smival.value.oid.len *SNMPCHARSIZE) + ( smival.value.oid.len -1) + 1 ;
if (n == 0)
n = 1; // need at least 1 byte for a null string
// adjust the len of output array in case size was adjusted
if ( iv_str != 0)
delete [] iv_str;
// allocate some space for the output string
ACE_NEW_RETURN(iv_str, char[ n], "");
// convert to an output string
size_t how_many;
if ( valid() && iv_str != 0)
if (OidToStr(&smival.value.oid,n,iv_str, how_many) < 0)
return "ERROR: Oid::OidToStr failed";
return iv_str;
}
//==============[Oid::suboid( unsigned int start, n) ]=============
int Oid::suboid(Oid& new_oid, size_t start, size_t how_many)
{
if (how_many == 0)
return 0;
else
if (how_many == (size_t)-1)
how_many = length();
else
if (how_many > length())
how_many = length();
// reset new_oid
new_oid.set_invalid();
size_t new_size = how_many - start;
if (new_size == 0)
new_size++;
new_oid.smival.value.oid.len = new_size;
ACE_NEW_RETURN(new_oid.smival.value.oid.ptr,
SmiUINT32 [ new_oid.smival.value.oid.len], -1);
// copy source to destination
ACE_OS::memcpy( (SmiLPBYTE) new_oid.smival.value.oid.ptr,
(SmiLPBYTE) (smival.value.oid.ptr + start),
new_size * sizeof(SmiLPBYTE));
return 0;
}
//=============[Oid::StrToOid( char *string, SmiLPOID dst) ]==============
// convert a string to an oid
int Oid::StrToOid( const char *string, SmiLPOID dstOid, size_t& how_many)
{
size_t index = 0;
size_t number = 0;
// make a temp buffer to copy the data into first
SmiLPUINT32 temp;
unsigned long nz;
if (string && *string) {
nz = ACE_OS::strlen( string);
}
else {
dstOid->len = 0;
dstOid->ptr = 0;
return -1;
}
ACE_NEW_RETURN(temp, SmiUINT32[ nz], -1);
while (*string!=0 && index<nz) {
// init the number for each token
number = 0;
// skip over the dot
if (*string=='.')
string++;
// grab a digit token and convert it to a long int
while (ACE_OS::ace_isdigit(*string))
number=number*10 + *(string++)-'0';
// check for invalid chars
if (*string!=0 && *string!='.') {
// Error: Invalid character in string
delete [] temp;
return -1;
}
// stuff the value into the array
temp[index] = number;
index++; // bump the counter
}
// get some space for the real oid
ACE_NEW_RETURN(dstOid->ptr, SmiUINT32[ index], -1);
// TODO: make tmp autoptr type delete [] temp to prevent leak
// copy in the temp data
ACE_OS::memcpy((SmiLPBYTE) dstOid->ptr, (SmiLPBYTE) temp,
(size_t) (index*sizeof(SmiUINT32)));
// set the len of the oid
dstOid->len = index;
// free up temp data
delete [] temp;
how_many = index;
return 0;
}
//===============[Oid::OidCopy( source, destination) ]====================
// Copy an oid, return bytes copied
int Oid::OidCopy( SmiLPOID srcOid, SmiLPOID dstOid, size_t& how_many_bytes)
{
// check source len ! zero
if (srcOid->len == 0)
return -1;
// copy source to destination
ACE_OS::memcpy((SmiLPBYTE) dstOid->ptr,
(SmiLPBYTE) srcOid->ptr,
(size_t) (srcOid->len * sizeof(SmiUINT32)));
//set the new len
dstOid->len = srcOid->len;
how_many_bytes = srcOid->len;
return 0;
}
//===============[Oid::left_comparison( n, Oid) ]=================================
// compare the n leftmost values of two oids ( left-to_right )
//
// self == Oid then return 0, they are equal
// self < Oid then return -1, <
// self > Oid then return 1, >
int Oid::left_comparison( const unsigned long n, const Oid &o) const
{
unsigned long z;
unsigned long len = n;
int reduced_len = 0;
// 1st case they both are null
if (( len==0)&&( this->smival.value.oid.len==0))
return 0; // equal
// verify that n is valid, must be >= 0
if ( len <=0)
return 1; // ! equal
// only compare for the minimal length
if (len > this->smival.value.oid.len) {
len = this->smival.value.oid.len;
reduced_len = 1;
}
if (len > o.smival.value.oid.len) {
len = o.smival.value.oid.len;
reduced_len = 1;
}
z = 0;
while(z < len) {
if ( this->smival.value.oid.ptr[z] < o.smival.value.oid.ptr[z])
return -1; // less than
if ( this->smival.value.oid.ptr[z] > o.smival.value.oid.ptr[z])
return 1; // greater than
z++;
}
// if we truncated the len then these may not be equal
if (reduced_len) {
if (this->smival.value.oid.len < o.smival.value.oid.len)
return -1;
if (this->smival.value.oid.len > o.smival.value.oid.len)
return 1;
}
return 0; // equal
}
//===============[Oid::left_comparison( n, Oid) ]=================================
// compare the n rightmost bytes (right-to-left)
// returns 0, equal
// returns -1, <
// returns 1 , >
int Oid::right_comparison( const unsigned long n, const Oid &o) const
{
// oid to compare must have at least the same number
// of sub-ids to comparison else the argument Oid is
// less than THIS
if ( o.length() < n)
return -1;
// also can't compare argument oid for sub-ids which
// THIS does not have
if ( this->length() < n)
return -1;
int start = (int) this->length();
int end = (int) start - (int) n;
for ( int z=start;z< end;z--)
{
if ( o.smival.value.oid.ptr[z] < this->smival.value.oid.ptr[z])
return -1;
if ( o.smival.value.oid.ptr[z] > this->smival.value.oid.ptr[z])
return 1;
}
return 0; // they are equal
}
//================[ Oid::valid() ]========================================
// is the Oid object valid
// returns validity
int Oid::valid() const
{
return ( smival.value.oid.ptr ? 1 : 0 );
}
//================[Oid::OidToStr ]=========================================
// convert an oid to a string
int Oid::OidToStr( SmiLPOID srcOid, unsigned long size,
char *string, size_t& how_many_bytes)
{
unsigned long index = 0;
unsigned totLen = 0;
char szNumber[SNMPBUFFSIZE];
// init the string
string[totLen] = 0;
// verify there is something to copy
if (srcOid->len == 0)
return -1;
// loop through and build up a string
for (index=0; index < srcOid->len; index++) {
// convert data element to a string
if (ACE_OS::sprintf( szNumber,"%lu", srcOid->ptr[index]) == -1)
return -1;
// verify len is not over
if (totLen + ACE_OS::strlen(szNumber) + 1 >= size)
return -2;
// if not at end, pad with a dot
if (totLen!=0)
string[totLen++] = '.';
// copy the string token into the main string
ACE_OS::strcpy(string + totLen, szNumber);
// adjust the total len
totLen += ACE_OS::strlen(szNumber);
}
how_many_bytes = totLen + 1;
return 0;
}
//================[ general Value = operator ]========================
SnmpSyntax& Oid::operator=( SnmpSyntax &val)
{
// protect against assignment from self
if ( this == &val )
return *this;
// blow away old value
smival.value.oid.len = 0;
if (smival.value.oid.ptr) {
set_invalid();
}
// assign new value
if (val.valid()) {
switch (val.get_syntax()) {
case sNMP_SYNTAX_OID:
set_data( ((Oid &)val).smival.value.oid.ptr,
(unsigned int)((Oid &)val).smival.value.oid.len);
break;
}
}
return *this;
}
//================[ [] operator ]=====================================
unsigned long& Oid::operator[](size_t position)
{
return smival.value.oid.ptr[position];
}
//================[ clone ]===========================================
SnmpSyntax *Oid::clone() const
{
return (SnmpSyntax *) new Oid(*this);
}
|