summaryrefslogtreecommitdiff
path: root/ASNMP/asnmp/asn1.cpp
blob: da2e936349bbb927831e8fb36512e0ab5873a416 (plain)
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
// $Id$

// ============================================================================
//
// = LIBRARY
//    asnmp
//
// = FILENAME
//     asn1.cpp
//
// = DESCRIPTION
//  The Vb class is an encapsulation of the snmp variable binding.
// This module contains the class definition for the variable binding (VB)
// class. The VB class is an encapsulation of a SNMP VB. A VB object is
// composed of one SNMP++ Oid and one SMI value. The Vb class utilizes Oid
// objects and thus requires the Oid class. To use this class,
// set oid, value then call valid() to be sure object was constructed correctly.
//
// = AUTHOR
//   S. Waldbusser (assumed)
//   Michael R MacFaden  mrm@cisco.com - rework & ACE port
// ============================================================================
/**********************************************************************
// *
   * Abstract Syntax Notation One, ASN.1
   * As defined in ISO/IS 8824 and ISO/IS 8825
   * This implements a subset of the above International Standards that
   * is sufficient to implement SNMP.
   *
   * Encodes abstract data types into a machine independent stream of bytes.
   *
   Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University

   All Rights Reserved

   Permission to use, copy, modify, and distribute this software and its 
   documentation for any purpose and without fee is hereby granted, 
   provided that the above copyright notice appear in all copies and that
   both that copyright notice and this permission notice appear in 
   supporting documentation, and that the name of CMU not be
   used in advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.  

   CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
   CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
   ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
   WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
   ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
   SOFTWARE.
   ******************************************************************/

#include "asnmp/asn1.h"
#include "asnmp/snmp.h"

ACE_RCSID(asnmp, asn1, "$Id$")

/*
 * parse_int - pulls a long out of an ASN int type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the end of this object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::parse_int( u_char *data, 
			      int *datalength, 
			      u_char *type, 
			      long int *intp, 
			      int intsize)
{
  ACE_TRACE("asn1::parse_int");
  /*
   * ASN.1 integer ::= 0x02 asnlength byte {byte}*
   */
  u_char *bufp = data;
  u_long	    asn_length;
  long   value = 0;

  if (intsize != sizeof (long)){
    ASNERROR("not long");
    return NULL;
  }
  *type = *bufp++;
  bufp =asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL){
    ASNERROR("bad length");
    return NULL;
  }
  if ((int)(asn_length + (bufp - data)) > *datalength){
    ASNERROR("overflow of message");
    return NULL;
  }
  if ((int)asn_length > intsize){
    ASNERROR("I don't support such large integers");
    return NULL;
  }
  *datalength -= (int)asn_length + (bufp - data);
  if (*bufp & 0x80)
    value = -1; /* integer is negative */
  while(asn_length--)
    value = (value << 8) | *bufp++;
  *intp = value;
  return bufp;
};


/*
 * parse_unsigned_int - pulls an u_long out of an ASN int type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the end of this object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::parse_unsigned_int( u_char *data,	
				       int *datalength,
				       u_char *type,
				       u_long *intp,
				       int	intsize)
{
  ACE_TRACE("asn1::parse_unsigned_int");
  /*
   * ASN.1 integer ::= 0x02 asnlength byte {byte}*
   */
  u_char *bufp = data;
  u_long	    asn_length;
  u_long value = 0;

  if (intsize != sizeof (long)){
    ASNERROR("not long");
    return NULL;
  }
  *type = *bufp++;
  bufp = asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL){
    ASNERROR("bad length");
    return NULL;
  }
  if ((int)(asn_length + (bufp - data)) > *datalength){
    ASNERROR("overflow of message");
    return NULL;
  }
  if (((int)asn_length > (intsize + 1)) ||
      (((int)asn_length == intsize + 1) && *bufp != 0x00)){
    ASNERROR("I don't support such large integers");
    return NULL;
  }
  *datalength -= (int)asn_length + (bufp - data);
  if (*bufp & 0x80)
    value = (u_long) -1; 
  while(asn_length--)
    value = (value << 8) | *bufp++;
  *intp = value;
  return bufp;
};


/*
 * build_int - builds an ASN object containing an integer.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the end of this object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::build_int( u_char *data,
			      int *datalength,
			      u_char type,
			      long *intp,
			      int intsize)
{
  ACE_TRACE("asn1::build_int");
  /*
   * ASN.1 integer ::= 0x02 asnlength byte {byte}*
   */

  long integer;
  u_long mask;

  if (intsize != sizeof (long))
    return NULL;
  integer = *intp;
  /*
   * Truncate "unnecessary" bytes off of the most significant end of this
   * 2's complement integer.  There should be no sequence of 9
   * consecutive 1's or 0's at the most significant end of the
   * integer.
   */
  mask = 0x1FF << ((8 * (sizeof(long) - 1)) - 1);
  /* mask is 0xFF800000 on a big-endian machine */
  while((((integer & mask) == 0) || ((integer & mask) == mask))
	&& intsize > 1){
    intsize--;
    integer <<= 8;
  }
  data = asn1::build_header(data, datalength, type, intsize);
  if (data == NULL)
    return NULL;
  if (*datalength < intsize)
    return NULL;
  *datalength -= intsize;
  mask = 0xFF << (8 * (sizeof(long) - 1));
  /* mask is 0xFF000000 on a big-endian machine */
  while(intsize--){
    *data++ = (u_char)((integer & mask) >> (8 * (sizeof(long) - 1)));
    integer <<= 8;
  }
  return data;
};


/*
 * build_unsigned_int - builds an ASN object containing an integer.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the end of this object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::build_unsigned_int( u_char *data,
				       int *datalength,
				       u_char type,
				       u_long *intp,
				       int intsize)
{
  ACE_TRACE("asn1::build_unsigned_int");
  /*
   * ASN.1 integer ::= 0x02 asnlength byte {byte}*
   */

  u_long integer;
  u_long mask;
  int add_null_byte = 0;

  if (intsize != sizeof (long))
    return NULL;
  integer = *intp;
  mask = 0xFF << (8 * (sizeof(long) - 1));
  /* mask is 0xFF000000 on a big-endian machine */
  if ((u_char)((integer & mask) >> (8 * (sizeof(long) - 1))) & 0x80){
    /* if MSB is set */
    add_null_byte = 1;
    intsize++;
  }
  /*
   * Truncate "unnecessary" bytes off of the most significant end of this 2's complement integer.
   * There should be no sequence of 9 consecutive 1's or 0's at the most significant end of the
   * integer.
   */
  mask = 0x1FF << ((8 * (sizeof(long) - 1)) - 1);
  /* mask is 0xFF800000 on a big-endian machine */
  while((((integer & mask) == 0) || ((integer & mask) == mask)) && intsize > 1){
    intsize--;
    integer <<= 8;
  }
  data = asn1::build_header(data, datalength, type, intsize);
  if (data == NULL)
    return NULL;
  if (*datalength < intsize)
    return NULL;
  *datalength -= intsize;
  if (add_null_byte == 1){
    *data++ = '\0';
    intsize--;
  }
  mask = 0xFF << (8 * (sizeof(long) - 1));
  /* mask is 0xFF000000 on a big-endian machine */
  while(intsize--){
    *data++ = (u_char)((integer & mask) >> (8 * (sizeof(long) - 1)));
    integer <<= 8;
  }
  return data;
};


/*
 * parse_string - pulls an octet string out of an ASN octet string type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  "string" is filled with the octet string.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::parse_string( u_char	*data,
				 int *datalength,
				 u_char *type,
				 u_char *string,
				 int *strlength)
{
  ACE_TRACE("asn1::parse_string");
  /*
   * ASN.1 octet string ::= primstring | cmpdstring
   * primstring ::= 0x04 asnlength byte {byte}*
   * cmpdstring ::= 0x24 asnlength string {string}*
   */
  u_char *bufp = data;
  u_long	    asn_length;

  *type = *bufp++;
  bufp = asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL)
    return NULL;
  if ((int)(asn_length + (bufp - data)) > *datalength){
    ASNERROR("overflow of message");
    return NULL;
  }
  if ((int)asn_length > *strlength){
    ASNERROR("I don't support such long strings");
    return NULL;
  }
  // fixed
  ACE_OS::memcpy((char *)string, (char *)bufp,  (int)asn_length);
  *strlength = (int)asn_length;
  *datalength -= (int)asn_length + (bufp - data);
  return bufp + asn_length;
};


/*
 * build_string - Builds an ASN octet string object containing the input string.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::build_string( u_char *data,
				 int *datalength,
				 u_char type,
				 u_char *string,
				 int strlength)
{
  ACE_TRACE("asn1::build_string");
  /*
   * ASN.1 octet string ::= primstring | cmpdstring
   * primstring ::= 0x04 asnlength byte {byte}*
   * cmpdstring ::= 0x24 asnlength string {string}*
   * This code will never send a compound string.
   */
  data = asn1::build_header(data, datalength, type, strlength);
  if (data == NULL)
    return NULL;
  if (*datalength < strlength)
    return NULL;
  // fixed
  ACE_OS::memcpy((u_char *)data,(u_char *)string, strlength);
  *datalength -= strlength;
  return data + strlength;
};


/*
 * parse_header - interprets the ID and length of the current object.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   in this object following the id and length.
 *
 *  Returns a pointer to the first byte of the contents of this object.
 *  Returns NULL on any error.
 */
u_char *asn1::parse_header( u_char *data,
				int *datalength,
				u_char *type)
{
  ACE_TRACE("asn1::parse_header");
  u_char *bufp = data;
  register int header_len;
  u_long	    asn_length;

  /* this only works on data types < 30, i.e. no extension octets */
  if (IS_EXTENSION_ID(*bufp)){
    ASNERROR("can't process ID >= 30");
    return NULL;
  }
  *type = *bufp;
  bufp = asn1::parse_length(bufp + 1, &asn_length);
  if (bufp == NULL)
    return NULL;
  header_len = bufp - data;
  if ((int)(header_len + asn_length) > *datalength){
    ASNERROR("asn length too long");
    return NULL;
  }
  *datalength = (int)asn_length;
  return bufp;
}

/*
 * asn1::build_header - builds an ASN header for an object with the ID and
 * length specified.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   in this object following the id and length.
 *
 *  This only works on data types < 30, i.e. no extension octets.
 *  The maximum length is 0xFFFF;
 *
 *  Returns a pointer to the first byte of the contents of this object.
 *  Returns NULL on any error.
 */
u_char * asn1::build_header( u_char *data,
				 int *datalength,
				 u_char type,
				 int length)
{
  ACE_TRACE("asn1::build_header");
  if (*datalength < 1)
    return NULL;
  *data++ = type;
  (*datalength)--;
  return asn1::build_length(data, datalength, length);
  
}

/*
 * asn_build_sequence - builds an ASN header for a sequence with the ID and
 * length specified.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   in this object following the id and length.
 *
 *  This only works on data types < 30, i.e. no extension octets.
 *  The maximum length is 0xFFFF;
 *
 *  Returns a pointer to the first byte of the contents of this object.
 *  Returns NULL on any error.
 */
u_char * asn1::build_sequence( u_char *data,
				   int *datalength,
				   u_char type,
				   int length)
{
  ACE_TRACE("asn1::build_sequence");
  *datalength -= 4;
  if (*datalength < 0){
    *datalength += 4;	/* fix up before punting */
    return NULL;
  }
  *data++ = type;
  *data++ = (u_char)(0x02 | ASN_LONG_LEN);
  *data++ = (u_char)((length >> 8) & 0xFF);
  *data++ = (u_char)(length & 0xFF);
  return data;
};

/*
 * parse_length - interprets the length of the current object.
 *  On exit, length contains the value of this length field.
 *
 *  Returns a pointer to the first byte after this length
 *  field (aka: the start of the data field).
 *  Returns NULL on any error.
 */
u_char * asn1::parse_length( u_char *data,
				 u_long  *length)
{
  ACE_TRACE("asn1::parse_length");
  u_char lengthbyte = *data;

  if (lengthbyte & ASN_LONG_LEN){
    lengthbyte &= ~ASN_LONG_LEN;	/* turn MSb off */
    if (lengthbyte == 0){
      ASNERROR("We don't support indefinite lengths");
      return NULL;
    }
    if (lengthbyte > sizeof(long)){
      ASNERROR("we can't support data lengths that long");
      return NULL;
    }
    // fixed
    ACE_OS::memcpy((char *)length, (char *)data + 1, (int)lengthbyte);
    *length = ntohl(*length);
    *length >>= (8 * ((sizeof *length) - lengthbyte));
    return data + lengthbyte + 1;
  } else { /* short asnlength */
    *length = (long)lengthbyte;
    return data + 1;
  }
};

u_char *asn1::build_length( u_char *data,
				int *datalength,
				int length)
{
  ACE_TRACE("asn1::build_length");
  u_char    *start_data = data;

  /* no indefinite lengths sent */
  if (length < 0x80){
    if (*datalength < 1){
      ASNERROR("build_length");
      return NULL;
    }	    
    *data++ = (u_char)length;
  } else if (length <= 0xFF){
    if (*datalength < 2){
      ASNERROR("build_length");
      return NULL;
    }	    
    *data++ = (u_char)(0x01 | ASN_LONG_LEN);
    *data++ = (u_char)length;
  } else { /* 0xFF < length <= 0xFFFF */
    if (*datalength < 3){
      ASNERROR("build_length");
      return NULL;
    }	    
    *data++ = (u_char)(0x02 | ASN_LONG_LEN);
    *data++ = (u_char)((length >> 8) & 0xFF);
    *data++ = (u_char)(length & 0xFF);
  }
  *datalength -= (data - start_data);
  return data;

}

/*
 * parse_objid - pulls an object indentifier out of an ASN object identifier type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  "objid" is filled with the object identifier.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char *asn1::parse_objid( u_char *data,
			       int *datalength,
			       u_char *type,
			       oid *objid,
			       int *objidlength)
{
  ACE_TRACE("asn1::parse_objid");
  /*
   * ASN.1 objid ::= 0x06 asnlength subidentifier {subidentifier}*
   * subidentifier ::= {leadingbyte}* lastbyte
   * leadingbyte ::= 1 7bitvalue
   * lastbyte ::= 0 7bitvalue
   */
  u_char *bufp = data;
  oid *oidp = objid + 1;
  u_long subidentifier;
  long   length;
  u_long	    asn_length;

  *type = *bufp++;
  bufp = asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL)
    return NULL;
  if ((int)asn_length + (bufp - data) > *datalength){
    ASNERROR("overflow of message");
    return NULL;
  }
  *datalength -= (int)asn_length + (bufp - data);

  /* Handle invalid object identifier encodings of the form 06 00 robustly */
  if (asn_length == 0)
    objid[0] = objid[1] = 0;

  length = asn_length;
  (*objidlength)--;	/* account for expansion of first byte */
  while (length > 0 && (*objidlength)-- > 0){
    subidentifier = 0;
    do {	/* shift and add in low order 7 bits */
      subidentifier = (subidentifier << 7) + (*(u_char *)bufp & ~ASN_BIT8);
      length--;
    } while (*(u_char *)bufp++ & ASN_BIT8);	/* last byte has high bit clear */
    if (subidentifier > (u_long)MAX_SUBID){
      ASNERROR("subidentifier too long");
      return NULL;
    }
    *oidp++ = (oid)subidentifier;
  }

  /*
   * The first two subidentifiers are encoded into the first component
   * with the value (X * 40) + Y, where:
   *	X is the value of the first subidentifier.
   *  Y is the value of the second subidentifier.
   */
  subidentifier = (u_long)objid[1];
  if (subidentifier == 0x2B){
    objid[0] = 1;
    objid[1] = 3;
  } else {
    objid[1] = (u_char)(subidentifier % 40);
    objid[0] = (u_char)((subidentifier - objid[1]) / 40);
  }

  *objidlength = (int)(oidp - objid);
  return bufp;
}

/*
 * build_objid - Builds an ASN object identifier object containing the
 * input string.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char *asn1::build_objid( u_char *data,
			       int *datalength,
			       u_char type,
			       oid *objid,
			       int objidlength)
{
  ACE_TRACE("asn1::build_objid");
  /*
   * ASN.1 objid ::= 0x06 asnlength subidentifier {subidentifier}*
   * subidentifier ::= {leadingbyte}* lastbyte
   * leadingbyte ::= 1 7bitvalue
   * lastbyte ::= 0 7bitvalue
   */
  u_char buf[MAX_OID_LEN];
  u_char *bp = buf;
  oid *op = objid;
  int    asnlength;
  u_long subid, mask, testmask;
  int bits, testbits;

  if (objidlength < 2){
    *bp++ = 0;
    objidlength = 0;
  } else {
    *bp++ = (u_char) (op[1] + (op[0] * 40));
    objidlength -= 2;
    op += 2;
  }

  while(objidlength-- > 0){
    subid = *op++;
    if (subid < 127){ /* off by one? */
      *bp++ = (u_char )subid;
    } else {
      mask = 0x7F; /* handle subid == 0 case */
      bits = 0;
      /* testmask *MUST* !!!! be of an u_type */
      for(testmask = 0x7F, testbits = 0; testmask != 0;
	  testmask <<= 7, testbits += 7){
	if (subid & testmask){	/* if any bits set */
	  mask = testmask;
	  bits = testbits;
	}
      }
      /* mask can't be zero here */
      for(;mask != 0x7F; mask >>= 7, bits -= 7){
	/* fix a mask that got truncated above */
	if (mask == 0x1E00000)  
	  mask = 0xFE00000;
	*bp++ = (u_char)(((subid & mask) >> bits) | ASN_BIT8);
      }
      *bp++ = (u_char)(subid & mask);
    }
  }
  asnlength = bp - buf;
  data = asn1::build_header(data, datalength, type, asnlength);
  if (data == NULL)
    return NULL;
  if (*datalength < asnlength)
    return NULL;
  // fixed
  ACE_OS::memcpy((char *)data, (char *)buf,  asnlength);
  *datalength -= asnlength;
  return data + asnlength;
}

/*
 * parse_null - Interprets an ASN null type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char *asn1::parse_null(u_char *data, 
				int *datalength, 
				u_char *type)
{
  ACE_TRACE("asn1::parse_null");
  /*
   * ASN.1 null ::= 0x05 0x00
   */
  u_char   *bufp = data;
  u_long	    asn_length;

  *type = *bufp++;
  bufp = asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL)
    return NULL;
  if (asn_length != 0){
    ASNERROR("Malformed NULL");
    return NULL;
  }
  *datalength -= (bufp - data);
  return bufp + asn_length;
}


/*
 * build_null - Builds an ASN null object.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char *asn1::build_null( u_char *data,
			      int *datalength,
			      u_char type)
{
  ACE_TRACE("asn1::build_null");
  /*
   * ASN.1 null ::= 0x05 0x00
   */
  return asn1::build_header(data, datalength, type, 0);
};

/*
 * parse_bitstring - pulls a bitstring out of an ASN bitstring type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  "string" is filled with the bit string.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char *asn1::parse_bitstring( u_char *data,
				   int *datalength,
				   u_char *type,
				   u_char *string,
				   int *strlength)
{
  ACE_TRACE("asn1::parse_bitstring");
  /*
   * bitstring ::= 0x03 asnlength unused {byte}*
   */
  u_char *bufp = data;
  u_long	    asn_length;

  *type = *bufp++;
  bufp = asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL)
    return NULL;
  if ((int)(asn_length + (bufp - data)) > *datalength){
    ASNERROR("overflow of message");
    return NULL;
  }
  if ((int) asn_length > *strlength){
    ASNERROR("I don't support such long bitstrings");
    return NULL;
  }
  if (asn_length < 1){
    ASNERROR("Invalid bitstring");
    return NULL;
  }
  if (*bufp > 7){
    ASNERROR("Invalid bitstring");
    return NULL;
  }
  // fixed
  ACE_OS::memcpy((char *)string,(char *)bufp,  (int)asn_length);
  *strlength = (int)asn_length;
  *datalength -= (int)asn_length + (bufp - data);
  return bufp + asn_length;
}


/*
 * build_bitstring - Builds an ASN bit string object containing the
 * input string.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the beginning of the next object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char *asn1::build_bitstring( u_char *data,
				   int *datalength,
				   u_char type,	
				   u_char *string,
				   int strlength)
{
  ACE_TRACE("asn1::build_bitstring");
  /*
   * ASN.1 bit string ::= 0x03 asnlength unused {byte}*
   */
  if (strlength < 1 || *string || *string > 7){
    ASNERROR("Building invalid bitstring");
    return NULL;
  }
  data = asn1::build_header(data, datalength, type, strlength);
  if (data == NULL)
    return NULL;
  if (*datalength < strlength)
    return NULL;
  // fixed
  ACE_OS::memcpy((char *)data,(char *)string, strlength);
  *datalength -= strlength;
  return data + strlength;
}


/*
 * parse_unsigned_int64 - pulls a 64 bit u_long out of an ASN int
 * type.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the end of this object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::parse_unsigned_int64(u_char *data,
					 int *datalength,
					 u_char *type,
					 struct counter64 *cp,
					 int countersize)
{
  ACE_TRACE("asn1::parse_unsigned_int64");
  /*
   * ASN.1 integer ::= 0x02 asnlength byte {byte}*
   */
  u_char *bufp = data;
  u_long	    asn_length;
  u_long low = 0, high = 0;
  int intsize = 4;
  
  if (countersize != sizeof(struct counter64)){
    ASNERROR("not right size");
    return NULL;
  }
  *type = *bufp++;
  bufp = asn1::parse_length(bufp, &asn_length);
  if (bufp == NULL){
    ASNERROR("bad length");
    return NULL;
  }
  if ((int)(asn_length + (bufp - data)) > *datalength){
    ASNERROR("overflow of message");
    return NULL;
  }
  if (((int)asn_length > (intsize * 2 + 1)) ||
      (((int)asn_length == (intsize * 2) + 1) && *bufp != 0x00)){
    ASNERROR("I don't support such large integers");
    return NULL;
  }
  *datalength -= (int)asn_length + (bufp - data);
  if (*bufp & 0x80){
    low = (u_long) -1; // integer is negative 
    high = (u_long) -1; 
  }
  while(asn_length--){
    high = (high << 8) | ((low & 0xFF000000) >> 24);
    low = (low << 8) | *bufp++;
  }
  cp->low = low;
  cp->high = high;
  return bufp;
}


/*
 * build_unsigned_int64 - builds an ASN object containing a 64 bit integer.
 *  On entry, datalength is input as the number of valid bytes following
 *   "data".  On exit, it is returned as the number of valid bytes
 *   following the end of this object.
 *
 *  Returns a pointer to the first byte past the end
 *   of this object (i.e. the start of the next object).
 *  Returns NULL on any error.
 */
u_char * asn1::build_unsigned_int64( u_char *data,
					 int *datalength,
					 u_char	type,
					 struct counter64 *cp,
					 int countersize)
{
  ACE_TRACE("asn1::build_unsigned_int64");
  /*
   * ASN.1 integer ::= 0x02 asnlength byte {byte}*
   */

  u_long low, high;
  u_long mask, mask2;
  int add_null_byte = 0;
  int intsize;

  if (countersize != sizeof (struct counter64))
    return NULL;
  intsize = 8;
  low = cp->low;
  high = cp->high;
  mask = 0xFF << (8 * (sizeof(long) - 1));
  /* mask is 0xFF000000 on a big-endian machine */
  if ((u_char)((high & mask) >> (8 * (sizeof(long) - 1))) & 0x80){
    /* if MSB is set */
    add_null_byte = 1;
    intsize++;
  }
  /*
   * Truncate "unnecessary" bytes off of the most significant end of this 2's
   * complement integer.
   * There should be no sequence of 9 consecutive 1's or 0's at the most
   * significant end of the integer.
   */
  mask2 = 0x1FF << ((8 * (sizeof(long) - 1)) - 1);
  /* mask2 is 0xFF800000 on a big-endian machine */
  while((((high & mask2) == 0) || ((high & mask2) == mask2))
	&& intsize > 1){
    intsize--;
    high = (high << 8)
      | ((low & mask) >> (8 * (sizeof(long) - 1)));
    low <<= 8;
  }
  data = asn1::build_header(data, datalength, type, intsize);
  if (data == NULL)
    return NULL;
  if (*datalength < intsize)
    return NULL;
  *datalength -= intsize;
  if (add_null_byte == 1){
    *data++ = '\0';
    intsize--;
  }
  while(intsize--){
    *data++ = (u_char)((high & mask) >> (8 * (sizeof(long) - 1)));
    high = (high << 8)
      | ((low & mask) >> (8 * (sizeof(long) - 1)));
    low <<= 8;
    
  }
  return data;
}


// create a pdu
struct snmp_pdu * cmu_snmp::pdu_create( int command)
{
  ACE_TRACE("cmu_snmp::snmp_pdu_create");
  struct snmp_pdu *pdu;

  ACE_NEW_RETURN(pdu, snmp_pdu, 0); 
  ACE_OS::memset((char *)pdu, 0,sizeof(struct snmp_pdu));
  pdu->command = command;
  pdu->errstat = 0;
  pdu->errindex = 0;
  pdu->enterprise = NULL;
  pdu->enterprise_length = 0;
  pdu->variables = NULL;
  return pdu;
}

// release a pdu from memory
void cmu_snmp::free_pdu( struct snmp_pdu *pdu)
{
  ACE_TRACE("cmu_snmp::free_pdu");
  struct variable_list *vp, *ovp;

  vp = pdu->variables;
  while(vp){
    // release the oid part
    if (vp->name)
      delete [] vp->name;
    // if deep data, then release as well
    if (vp->val.string)
      delete [] vp->val.string;
    ovp = vp;
    // go to the next one
    vp = vp->next_variable;
    // release up vb itself
    delete ovp;
  }
  // if enterprise release it up
  if (pdu->enterprise)
    delete [] pdu->enterprise;
  // release up pdu itself
  delete pdu;
}


// add a null var to a pdu
void cmu_snmp::add_var(struct snmp_pdu *pdu, 
		  oid *name, 
		  int name_length,
		  SmiVALUE *smival)
{
  ACE_TRACE("cmu_snmp::add_var");

  struct variable_list *vars;

  // if we don't have a vb list ,create one
  if (pdu->variables == NULL) {
    ACE_NEW(pdu->variables, variable_list);
    vars = pdu->variables;
  }
  else 
    { // we have one, find the end
      for(vars = pdu->variables; vars->next_variable; vars = vars->next_variable);
      // create one
      ACE_NEW(vars->next_variable, variable_list);
      // bump ptr
      vars = vars->next_variable;
    }

  // add the oid with no data
  vars->next_variable = NULL;

  // hook in the Oid portion
  ACE_NEW(vars->name, oid[(name_length)]);

  // fixed
  ACE_OS::memcpy((char *)vars->name,(char *)name, name_length * sizeof(oid));
  vars->name_length = name_length;

  // hook in the SMI value
  switch( smival->syntax)
    {
      // null , do nothing
    case sNMP_SYNTAX_NULL:
    case sNMP_SYNTAX_NOSUCHOBJECT:
    case sNMP_SYNTAX_NOSUCHINSTANCE:
    case sNMP_SYNTAX_ENDOFMIBVIEW:
      {
	vars->type = (u_char) smival->syntax;
	vars->val.string = NULL;
	vars->val_len = 0;
      }
      break;

      // octects
    case sNMP_SYNTAX_OCTETS:
    case sNMP_SYNTAX_OPAQUE:
    case sNMP_SYNTAX_IPADDR:
      {
	vars->type = (u_char) smival->syntax;
	ACE_NEW(vars->val.string, 
                u_char[(unsigned)smival->value.string.len]);
	vars->val_len = (int) smival->value.string.len;
	ACE_OS::memcpy( (u_char *) vars->val.string,
		       (u_char *) smival->value.string.ptr,
		       (unsigned) smival->value.string.len);
      }
      break;

      // oid
    case sNMP_SYNTAX_OID:
      {
	vars->type = (u_char) smival->syntax;
        vars->val_len = (int) smival->value.oid.len * sizeof(oid);
	ACE_NEW(vars->val.objid, oid[(unsigned)vars->val_len]);
	ACE_OS::memcpy((u_long *)vars->val.objid,
		       (u_long *)smival->value.oid.ptr,
		       (unsigned) vars->val_len);
      }
      break;


      
    case sNMP_SYNTAX_TIMETICKS:
    case sNMP_SYNTAX_CNTR32:
    case sNMP_SYNTAX_GAUGE32:
    case sNMP_SYNTAX_UINT32:
      {
	long templong;
	vars->type = (u_char) smival->syntax;
	ACE_NEW(vars->val.integer, long);
	vars->val_len = sizeof(long);
	templong = (long) smival->value.uNumber;
	ACE_OS::memcpy( (long*) vars->val.integer,
		       (long*) &templong,
		       sizeof(long));
      }
      break;

    case sNMP_SYNTAX_INT32:
      {
	long templong;
	vars->type = (u_char) smival->syntax;
	ACE_NEW(vars->val.integer, long);
	vars->val_len = sizeof(long);
	templong = (long) smival->value.sNumber;
	ACE_OS::memcpy( (long*) vars->val.integer,
		       (long*) &templong,
		       sizeof(long));
      }
      break;

      // 64 bit counter
    case sNMP_SYNTAX_CNTR64:
      {
	vars->type = ( u_char) smival->syntax;
	ACE_NEW(vars->val.counter64, counter64);
	vars->val_len = sizeof(struct counter64);
	ACE_OS::memcpy( (struct counter64*) vars->val.counter64,
		       (SmiLPCNTR64) &(smival->value.hNumber),
		       sizeof( SmiCNTR64));
      }
      break;

    } // end switch

}

// build the authentication
// works for v1 or v2c
u_char *cmu_snmp::auth_build( u_char *data,
			       int *length,
			       long int version,
			       u_char *community,
			       int community_len,
			       int messagelen)
{
  ACE_TRACE("cmu_snmp::auth_build");
  u_char *params;
  int     plen;

  params = community;
  plen = community_len;

  data = asn1::build_sequence(data, 
			    length, 
			    (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 
			    messagelen + plen + 5);
  if (data == NULL){
    ASNERROR("buildheader");
    return NULL;
  }
  data = asn1::build_int(data, 
		       length,
		       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&version, 
		       sizeof(version));
  if (data == NULL){
    ASNERROR("buildint");
    return NULL;
  }

  data = asn1::build_string(data, 
			  length,
			  (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), 
			  params, 
			  plen );
  if (data == NULL){
    ASNERROR("buildstring");
    return NULL;
  }

  return (u_char *)data;
};


// build a variable binding
u_char * cmu_snmp::build_var_op(u_char *data, oid * var_name, 
				  int *var_name_len, 
				  u_char var_val_type, 
				  int var_val_len, u_char *var_val, 
				  int *listlength)

{
  ACE_TRACE("cmu_snmp::build_var_op");
  int dummyLen, headerLen;
  u_char *dataPtr;

  dummyLen = *listlength;
  dataPtr = data;

  data += 4;
  dummyLen -=4;
  if (dummyLen < 0)
    return NULL;

  headerLen = data - dataPtr;
  *listlength -= headerLen;
  data = asn1::build_objid( data, listlength,
		 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
			 var_name, *var_name_len);
  if (data == NULL) {
    ASNERROR("");
    return NULL;
  }

  // based on the type...
  switch(var_val_type) {
  case ASN_INTEGER:
    data = asn1::build_int( data, listlength, var_val_type, (long *)var_val, 
			 var_val_len);
    break;

  case SMI_GAUGE:
  case SMI_COUNTER:
  case SMI_TIMETICKS:
  case SMI_UINTEGER:
    data = asn1::build_unsigned_int( data, 
				  listlength, 
				  var_val_type,
				  (u_long *)var_val, 
				  var_val_len);
    break;

  case SMI_COUNTER64:
    data = asn1::build_unsigned_int64(data, 
				    listlength, 
				    var_val_type,
				    (struct counter64 *)var_val,
				    var_val_len);
    break;

  case ASN_OCTET_STR:
  case SMI_IPADDRESS:
  case SMI_OPAQUE:
  case SMI_NSAP:
    data = asn1::build_string(data, listlength, var_val_type,
			    var_val, var_val_len);
    break;

  case ASN_OBJECT_ID:
    data = asn1::build_objid(data, listlength, var_val_type,
			   (oid *)var_val, var_val_len / sizeof(oid));
    break;

  case ASN_NULL:
    data = asn1::build_null(data, listlength, var_val_type);
    break;

  case ASN_BIT_STR:
    data = asn1::build_bitstring(data, listlength, var_val_type,
			       var_val, var_val_len);
    break;

  case SNMP_NOSUCHOBJECT:
  case SNMP_NOSUCHINSTANCE:
  case SNMP_ENDOFMIBVIEW:
    data = asn1::build_null(data, listlength, var_val_type);
    break;

  default:
    ASNERROR("wrong type");
    return NULL;
  }
  if (data == NULL) {
    ASNERROR("");
    return NULL;
  }
  dummyLen = (data - dataPtr) - headerLen;

  asn1::build_sequence(dataPtr, &dummyLen,
		     (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 
		     dummyLen);
  return data;
}


// serialize the pdu
int cmu_snmp::build( struct snmp_pdu *pdu, u_char *packet, 
	       int *out_length, long version,
	       u_char* community, int community_len)
{
  ACE_TRACE("cmu_snmp::build");
  u_char buf[SNMP_MSG_LENGTH];
  u_char  *cp;
  struct variable_list *vp;
  int length;
  int totallength;

  length = *out_length;
  cp = packet;
  for(vp = pdu->variables; vp; vp = vp->next_variable) {
    cp = cmu_snmp::build_var_op( cp, vp->name, 
			   &vp->name_length, vp->type, 
			   vp->val_len, (u_char *)vp->val.string, 
			   &length);
    if (cp == NULL)
      return -1;
  }
  totallength = cp - packet;

  length = SNMP_MSG_LENGTH;
  
  // encode the total len
  cp = asn1::build_header( buf, &length, 
			(u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 
			totallength);
  if (cp == NULL)
    return -1;
  ACE_OS::memcpy( (char *)cp, (char *)packet,totallength);
  totallength += cp - buf;

  length = *out_length;
  if (pdu->command != TRP_REQ_MSG) {

    // request id 
    cp = asn1::build_int( packet, 
		       &length,
	       (u_char )(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&pdu->reqid, 
		       sizeof(pdu->reqid));
    if (cp == NULL)
      return -1;

    // error status 
    cp = asn1::build_int(cp, 
		       &length,
	       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&pdu->errstat, sizeof(pdu->errstat));
    if (cp == NULL)
      return -1;

    // error index 
    cp = asn1::build_int(cp, 
		       &length,
	       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&pdu->errindex, sizeof(pdu->errindex));
    if (cp == NULL)
      return -1;
  } 
  else {	// this is a trap message 

    // enterprise 
    cp = asn1::build_objid( packet, 
			 &length,
		 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
			 (oid *)pdu->enterprise, 
			 pdu->enterprise_length);
    if (cp == NULL)
      return -1;

    // agent-addr 
    cp = asn1::build_string(cp, 
			    &length,
			    // HDN Fixed to use correct tag
			    (u_char)SMI_IPADDRESS,
			    //(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR),
			  (u_char *)&pdu->agent_addr.sin_addr.s_addr, 
			  sizeof(pdu->agent_addr.sin_addr.s_addr));
    if (cp == NULL)
      return -1;

    // generic trap 
    cp = asn1::build_int(cp, 
		       &length,
	       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&pdu->trap_type, 
		       sizeof(pdu->trap_type));
    if (cp == NULL)
      return -1;

    // specific trap 
    cp = asn1::build_int( cp, 
		       &length,
	       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&pdu->specific_type, 
		       sizeof(pdu->specific_type));
    if (cp == NULL)
      return -1;

    // timestamp  
    cp = asn1::build_int(cp, 
		       &length,
			 // HDN Fixed to use correct tag
			 (u_char)SMI_TIMETICKS,
			 //(u_char )(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       (long *)&pdu->time, 
		       sizeof(pdu->time));
    if (cp == NULL)
      return -1;
  }

  if (length < totallength)
    return -1;
  // fixed
  ACE_OS::memcpy((char *)cp, (char *)buf, totallength);
  totallength += cp - packet;

  length = SNMP_MSG_LENGTH;
  cp = asn1::build_header(buf, 
			&length, 
			(u_char)pdu->command, 
			totallength);
  if (cp == NULL)
    return -1;
  if (length < totallength)
    return -1;
  // fixed
  ACE_OS::memcpy((char *)cp, (char *)packet, totallength);
  totallength += cp - buf;

  length = *out_length;

  cp = cmu_snmp::auth_build( packet, 
		       &length, 
		       version, 
		       community,
		       community_len,
		       totallength );
  if (cp == NULL)
    return -1;
  if ((*out_length - (cp - packet)) < totallength)
    return -1;
  // fixed
  ACE_OS::memcpy((char *)cp, (char *)buf, totallength);
  totallength += cp - packet;
  *out_length = totallength;

  return 0;
}

// parse the authentication header
u_char *cmu_snmp::auth_parse(u_char *data,
                               int *length, u_char *sid,
			       int *slen, long	*version)
{
  ACE_TRACE("cmu_snmp::auth_parse");
  u_char type;

  // get the type
  data = asn1::parse_header( data, length, &type);
  if (data == NULL){
    ASNERROR("bad header");
    return NULL;
  }

  if (type != (ASN_SEQUENCE | ASN_CONSTRUCTOR)) {
    ASNERROR("wrong auth header type");
    return NULL;
  }

  // get the version
  data = asn1::parse_int(data, length, &type, version, sizeof(*version));
  if (data == NULL) {
    ASNERROR("bad parse of version");
    return NULL;
  }

  // get the community name
  data = asn1::parse_string(data, length, &type, sid, slen);
  if (data == NULL) {
    ASNERROR("bad parse of community");
    return NULL;
  }

  return (u_char *)data;
}

/* u_char *data,  // IN - pointer to the start of object 
  oid	    *var_name,	    // OUT - object id of variable 
  int	    *var_name_len,  // IN/OUT - length of variable name 
  u_char  *var_val_type, // OUT - type of variable 
                                 (int or octet string) (one byte) 
  int	    *var_val_len,     // OUT - length of variable
  u_char  **var_val,  // OUT - pointer to ASN1 encoded value of variable
*/

u_char *
cmu_snmp::parse_var_op( u_char *data, oid *var_name,	    
                       int *var_name_len,  u_char  *var_val_type, 
                       int *var_val_len, u_char  **var_val, 
                       int  *listlength)
{
  ACE_TRACE("cmu_snmp::parse_var_op");
  u_char var_op_type;
  int	var_op_len = *listlength;
  u_char *var_op_start = data;

  data = asn1::parse_header(data, &var_op_len, &var_op_type);
  if (data == NULL){
    ASNERROR("");
    return NULL;
  }
  if (var_op_type != (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR))
    return NULL;
  data = asn1::parse_objid(data, &var_op_len, &var_op_type, var_name, var_name_len);
  if (data == NULL) {
    ASNERROR("");
    return NULL;
  }
  if (var_op_type != (u_char)
                     (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID))
    return NULL;
  *var_val = data;	/* save pointer to this object */
  /* find out what type of object this is */
  data = asn1::parse_header(data, &var_op_len, var_val_type);
  if (data == NULL) {
    ASNERROR("");
    return NULL;
  }
  *var_val_len = var_op_len;
  data += var_op_len;
  *listlength -= (int)(data - var_op_start);
  return data;
}



// build a pdu from a data and length
int cmu_snmp::parse( struct snmp_pdu *pdu,
	       u_char  *data,
	       u_char *community_name,
	       u_long &community_len,
	       snmp_version &spp_version,
	       int length)
{
  ACE_TRACE("cmu_snmp::parse");
  u_char  msg_type;
  u_char  type;
  u_char  *var_val;
  long    version;
  int	    len, four;
  u_char community[256];
  int community_length = 256;
  struct variable_list *vp = 0;
  oid	    objid[MAX_NAME_LEN], *op;

  // authenticates message and returns length if valid 
  data = cmu_snmp::auth_parse(data, 
			 &length, 
			 community, 
			 &community_length, 
			 &version);
  if (data == NULL)
    return -1;

  // copy the returned community name
  ACE_OS::memcpy( (u_char *) community_name, 
		 (u_char *) community, 
		 community_length);
  community_len = (long) community_length;

  if( version != SNMP_VERSION_1 && version != SNMP_VERSION_2C ) {
    ASNERROR("Wrong version");
    return -1;
  }

  spp_version = (snmp_version) version;

  data = asn1::parse_header(data, 
			  &length, 
			  &msg_type);
  if (data == NULL)
    return -1;
  pdu->command = msg_type;

  if (pdu->command != TRP_REQ_MSG){
    // get the rid
    data = asn1::parse_int(data, 
			 &length, &type, 
			 (long *)&pdu->reqid, 
			 sizeof(pdu->reqid));
    if (data == NULL)
      return -1;
    // get the error status
    data = asn1::parse_int(data, 
			 &length, 
			 &type, 
			 (long *)&pdu->errstat, 
			 sizeof(pdu->errstat));
    if (data == NULL)
      return -1;
    // get the error index
    data = asn1::parse_int(data, 
			 &length, 
			 &type, 
			 (long *)&pdu->errindex, 
			 sizeof(pdu->errindex));
    if (data == NULL)
      return -1;
  } 
  else {  // is a trap

    // get the enterprise
    pdu->enterprise_length = MAX_NAME_LEN;
    data = asn1::parse_objid(data, 
			   &length, 
			   &type, 
			   objid, 
			   &pdu->enterprise_length);
    if (data == NULL)
      return -1;

    ACE_NEW_RETURN(pdu->enterprise, 
                   oid[pdu->enterprise_length*sizeof(oid)],-1);

    // fixed
    ACE_OS::memcpy((char *)pdu->enterprise,(char *)objid,  
                   pdu->enterprise_length * sizeof(oid));

    // get source address
    four = 4;
    data = asn1::parse_string(data, &length, &type, 
                            (u_char *)&pdu->agent_addr.sin_addr.s_addr, 
			    &four);
    if (data == NULL)
      return -1;

    // get trap type
    data = asn1::parse_int(data, &length, &type, (long *)&pdu->trap_type, 
			 sizeof(pdu->trap_type));
    if (data == NULL)
      return -1;

    // trap type
    data = asn1::parse_int(data, &length, &type, (long *)&pdu->specific_type, 
			 sizeof(pdu->specific_type));
    if (data == NULL)
      return -1;

    // timestamp
    data = asn1::parse_int(data, &length, &type, (long *)&pdu->time, 
                            sizeof(pdu->time));
    if (data == NULL)
      return -1;
  }

  // get the vb list
  data = asn1::parse_header(data, &length, &type);
  if (data == NULL)
    return -1;

  if (type != (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR))
    return -1;

  while((int)length > 0) {
    if (pdu->variables == NULL) {
      ACE_NEW_RETURN(pdu->variables, variable_list, -1);
      vp = pdu->variables;
    } else {
      ACE_NEW_RETURN(vp->next_variable, variable_list, -1);
      vp = vp->next_variable;
    }
    vp->next_variable = NULL;
    vp->val.string = NULL;
    vp->name = NULL;
    vp->name_length = MAX_NAME_LEN;
    data = cmu_snmp::parse_var_op( data, objid, 
			     &vp->name_length, &vp->type, 
			     &vp->val_len, &var_val, 
			     (int *)&length);
     if (data == NULL)
      return -1;

    ACE_NEW_RETURN(op, oid[(unsigned)vp->name_length * sizeof(oid)], -1);

    // fixed
    ACE_OS::memcpy((char *)op, (char *)objid, vp->name_length * sizeof(oid));
    vp->name = op;

    len = SNMP_MSG_LENGTH;
    switch((short)vp->type) {
    case ASN_INTEGER:
    case SMI_COUNTER:
    case SMI_GAUGE:
    case SMI_TIMETICKS:
    case SMI_UINTEGER:
      ACE_NEW_RETURN(vp->val.integer,long, -1);
      vp->val_len = sizeof(long);
      asn1::parse_int(var_val, &len, &vp->type, (long *)vp->val.integer, sizeof(vp->val.integer));
      break;
    case SMI_COUNTER64:
      ACE_NEW_RETURN(vp->val.counter64, counter64, -1);
      vp->val_len = sizeof(struct counter64);
      asn1::parse_unsigned_int64(var_val, &len, &vp->type,
			       (struct counter64 *)vp->val.counter64,
			       sizeof(*vp->val.counter64));
      break;
      
    case ASN_OCTET_STR:
    case SMI_IPADDRESS:
    case SMI_OPAQUE:
    case SMI_NSAP:
      ACE_NEW_RETURN(vp->val.string, u_char[(unsigned)vp->val_len + 1], -1);
      asn1::parse_string(var_val, &len, &vp->type, vp->val.string, 
                         &vp->val_len);
      break;

    case ASN_OBJECT_ID:
      vp->val_len = MAX_NAME_LEN;
      asn1::parse_objid(var_val, &len, &vp->type, objid, &vp->val_len);
      //vp->val_len *= sizeof(oid);

      ACE_NEW_RETURN(vp->val.objid, oid[(unsigned)vp->val_len*sizeof(oid)], -1);

      // fixed
      ACE_OS::memcpy((char *)vp->val.objid,
		     (char *)objid,
		     vp->val_len * sizeof(oid));
      break;

    case SNMP_NOSUCHOBJECT:
    case SNMP_NOSUCHINSTANCE:
    case SNMP_ENDOFMIBVIEW:
    case ASN_NULL:
      break;
    default:
      ASNERROR("bad type returned ");
      break;
    }
  }
  return 0;
}