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
|
/******************************************************************************
** COPYRIGHT NOTICE
** (c) 2012 The Johns Hopkins University Applied Physics Laboratory
** All rights reserved.
**
** This material may only be used, modified, or reproduced by or for the
** U.S. Government pursuant to the license rights granted under
** FAR clause 52.227-14 or DFARS clauses 252.227-7013/7014
**
** For any other permissions, please contact the Legal Office at JHU/APL.
******************************************************************************/
/*****************************************************************************
**
** \file oid.c
**
** Description: This file contains the implementation of functions that
** operate on Object Identifiers (OIDs). This file also implements
** global values, including the OID nickname database.
**
** Notes:
** 1. In the current implementation, the nickname database is not
** persistent.
** 2. We do not support a "create" function for OIDs as, so far, any
** need to create OIDs can be met by calling the appropriate
** deserialize function.
**
** Assumptions:
** 1. We limit the size of an OID in the system to reduce the amount
** of pre-allocated memory in this embedded system. Non-embedded
** implementations may wish to dynamically allocate MIDs as they are
** received.
**
**
** Modification History:
** MM/DD/YY AUTHOR DESCRIPTION
** -------- ------------ ---------------------------------------------
** 10/29/12 E. Birrane Initial Implementation
** 11/14/12 E. Birrane Code review comments and documentation update.
*****************************************************************************/
#include "platform.h"
#include "shared/utils/utils.h"
#include "shared/primitives/oid.h"
Lyst nn_db;
ResourceLock nn_db_mutex;
/******************************************************************************
*
* \par Function Name: oid_add_param
*
* \par Adds a parameter to a parameterized OID.
*
* \retval 0 Failure
* !0 Success
*
* \param[in,out] oid The OID getting a new param.
* \param[in] value The value of the new parameter
* \param[in] len The length, in bytes, of the new parameter.
*
* \par Notes:
* 1. The new parameter is allocated into the OID and, upon exit,
* the passed-in value may be released if necessary.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 11/25/12 E. Birrane Initial implementation,
*****************************************************************************/
int oid_add_param(oid_t *oid, uint8_t *value, uint32_t len)
{
int result = 0;
uint8_t *cursor = NULL;
Sdnv len_sdnv;
datacol_entry_t *entry = NULL;
DTNMP_DEBUG_ENTRY("oid_add_param","(%#llx, %#llx, %ld)", oid, value, len);
/* Step 0: Sanity Check.*/
if((oid == NULL) || (value == NULL) || (len == 0))
{
DTNMP_DEBUG_ERR("oid_add_param","Bad Args", NULL);
DTNMP_DEBUG_EXIT("oid_add_param","->0.",NULL);
return 0;
}
/* Step 1: Make sure the OID is a parameterized one. */
if((oid->type != OID_TYPE_PARAM) &&
(oid->type != OID_TYPE_COMP_PARAM))
{
DTNMP_DEBUG_ERR("oid_add_param","Can't add parameter to OID of type %d.",
oid->type);
DTNMP_DEBUG_EXIT("oid_add_param","->0.",NULL);
return 0;
}
/* Step 2: Make sure this doesn't give us too many params. */
if((lyst_length(oid->params)+1) > MAX_OID_PARM)
{
DTNMP_DEBUG_ERR("oid_add_param","OID has %d params, no room for more.",
lyst_length(oid->params));
DTNMP_DEBUG_EXIT("oid_add_param","->0.",NULL);
return 0;
}
/* Step 3: Make sure we have room for the passed-in parameter. */
encodeSdnv(&len_sdnv, len);
if((oid_calc_size(oid) + len) > MAX_OID_SIZE)
{
DTNMP_DEBUG_ERR("oid_add_param","Parm too long. OID %d MAX %d Len %d",
oid_calc_size(oid), MAX_OID_SIZE, len);
DTNMP_DEBUG_EXIT("oid_add_param","->0.",NULL);
return 0;
}
/* Step 4: Allocate the entry. */
if((entry = (datacol_entry_t*)MTAKE(sizeof(datacol_entry_t))) == NULL)
{
DTNMP_DEBUG_ERR("oid_add_param","Can't alloc %d bytes.",
sizeof(datacol_entry_t));
DTNMP_DEBUG_EXIT("oid_add_param","->0.",NULL);
return 0;
}
entry->length = len;
if((entry->value = (uint8_t*) MTAKE(entry->length)) == NULL)
{
DTNMP_DEBUG_ERR("oid_add_param","Can't alloc %d bytes.",
entry->length);
MRELEASE(entry);
DTNMP_DEBUG_EXIT("oid_add_param","->0.",NULL);
return 0;
}
cursor = entry->value;
// memcpy(cursor, len_sdnv.text, len_sdnv.length);
// cursor += len_sdnv.length;
memcpy(cursor, value, len);
cursor += len;
lyst_insert_last(oid->params, entry);
DTNMP_DEBUG_EXIT("oid_add_param","->%d.",result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_calc_size
*
* \par Purpose: Calculate size of the OID once serialized. THe serialized OID
* does *not* include the OID type, but will include the following info
* based on type:
* - # Bytes in the OID (always)
* - # Parms (for any parameterized OID)
* - SDNV per parm (for any parameterized OID)
* - Nickname (for any compressed OID).
* Therefore, the largest possible OID has the following form:
*
* <..opt..> <..........required........> <..........optional..........>
* +--------+-------+-------+ +------+--------+-------+ +-------+
* |Nickname|# Bytes| Byte 1| ... |Byte N| # Parms| Parm 1| ... | Parm N|
* | (SDNV) | (SDNV)| (Byte)| |(Byte)| (SDNV) | (SDNV)| |(SDNV) |
* +--------+-------+-------+ +------+--------+-------+ +-------+
*
* \retval 0 - Error
* >0 The serialized size of the OID.
*
* \param[in] oid The OID whose serialized size is to be calculated.
* \param[in] expand_nn Whether to expand the nickname (!0) or not (0).
*
* \par Notes:
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
uint32_t oid_calc_size(oid_t *oid)
{
uint32_t size = 0;
Sdnv tmp;
datacol_entry_t *entry = NULL;
DTNMP_DEBUG_ENTRY("oid_calc_size","(%#llx)",(unsigned long)oid);
/* Step 0: Sanity Check. */
if(oid == NULL)
{
DTNMP_DEBUG_ERR("oid_calc_size","Bad Args.",NULL);
DTNMP_DEBUG_EXIT("oid_calc_size","->0",NULL);
return 0;
}
/* Step 1: How big is the nickname portion of the OID? */
if((oid->type == OID_TYPE_COMP_FULL) || (oid->type == OID_TYPE_COMP_PARAM))
{
encodeSdnv(&tmp, oid->nn_id);
size += tmp.length;
}
/* Step 2: Add the # bytes SDNV and the OID data size. */
size += oid->value_size;
DTNMP_DEBUG_INFO("oid_calc_size","val is %d\n", oid->value_size);
/*
* Step 3: If we have parameters, add them too. This is simple because we
* keep the serialized parameters around, including the SDNV at
* the beginning precisely to make subsequent serialization (and
* sizing) calculations easier.
*/
if(lyst_length(oid->params) > 0)
{
LystElt elt;
/* Step 3a: Make room for number of parameters. */
encodeSdnv(&tmp, lyst_length(oid->params));
size += tmp.length;
/* Step 3b: Add room for each parameter, which is a # bytes, followed
* by the bytes.
*/
for(elt = lyst_first(oid->params); elt; elt = lyst_next(elt))
{
entry = (datacol_entry_t *) lyst_data(elt);
if(entry != NULL)
{
encodeSdnv(&tmp, entry->length);
/*/todo update spec: parms is # bytes, followed by bytes. */
size += tmp.length + entry->length;
}
}
}
DTNMP_DEBUG_EXIT("oid_calc_size","->%d",size);
return size;
}
/******************************************************************************
*
* \par Function Name: oid_clear
*
* \par Purpose: Resets the values associated with an OID structure.
*
* \retval void
*
* \param[in,out] oid The OID being cleared.
*
* \par Notes:
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
void oid_clear(oid_t *oid)
{
DTNMP_DEBUG_ENTRY("oid_clear","(%#llx)", (unsigned long) oid);
/* Step 0: Sanity Check. */
if(oid == NULL)
{
DTNMP_DEBUG_ERR("oid_clear","Clearing NULL OID.", NULL);
DTNMP_DEBUG_EXIT("oid_clear","<->",NULL);
return;
}
/* Step 1: Free parameters list. */
utils_datacol_destroy(&(oid->params));
/* Step 2: Bzero rest of the OID. */
memset(oid, 0, sizeof(oid_t));
DTNMP_DEBUG_EXIT("oid_clear","<->", NULL);
}
/******************************************************************************
*
* \par Function Name: oid_compare
*
* \par Purpose: Determines equivalence of two OIDs.
*
* \retval !0 : Not equal (including error)
* 0 : oid1 == oid2
*
* \param[in] oid1 First OID being compared.
* \param[in] oid2 Second OID being compared.
* \param[in] use_parms Whether to compare parms as well.
*
* \par Notes:
* 1. This function should only check for equivalence (== 0), not order
* since we do not differentiate between oid1 < oid2 and error.
* 2. Can compare a non-NULL OID and a NULL OID. They will not be
* equivalent.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
int oid_compare(oid_t *oid1, oid_t *oid2, uint8_t use_parms)
{
int result = 0;
DTNMP_DEBUG_ENTRY("oid_compare","(%#llx, %#llx)",
(unsigned long) oid1,
(unsigned long) oid2);
/* Step 0: Sanity check and shallow comparison in one. */
if(((oid1 == NULL) || (oid2 == NULL)) ||
((oid1->value_size != oid2->value_size)) ||
((oid1->type != oid2->type)))
{
DTNMP_DEBUG_EXIT("oid_compare","->-1.", NULL);
return -1;
}
if(use_parms != 0)
{
if(lyst_length(oid1->params) != lyst_length(oid2->params))
{
DTNMP_DEBUG_EXIT("oid_compare","->-1.", NULL);
return -1;
}
}
/* Step 1: Compare the value version of the oid */
result = memcmp(oid1->value, oid2->value, oid1->value_size);
/* Step 2: If OIDs match so far, and if there are parameters, then we
* need to check the parameters...
*/
if(use_parms != 0)
{
if((result == 0) && (lyst_length(oid1->params) > 0))
{
result = utils_datacol_compare(oid1->params, oid2->params);
}
}
DTNMP_DEBUG_EXIT("oid_compare","->%d.", result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_copy
*
* \par Purpose: Duplicates an OID.
*
* \retval NULL: The copy failed
* !NULL: The deep-copied OID.
*
* \param[in] src_oid The OID being copied.
*
* \par Notes:
* 1. The desintation OID is allocated and must be released when done.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
oid_t *oid_copy(oid_t *src_oid)
{
oid_t *result = NULL;
DTNMP_DEBUG_ENTRY("oid_copy","(%#llx)", (unsigned long) src_oid);
/* Step 0: Sanity Check */
if(src_oid == NULL)
{
DTNMP_DEBUG_ERR("oid_copy","Cannot copy from NULL source OID.", NULL);
DTNMP_DEBUG_EXIT("oid_copy","->NULL",NULL);
return NULL;
}
/* Step 1: Allocate the new OID. */
if((result = (oid_t*) MTAKE(sizeof(oid_t))) == NULL)
{
DTNMP_DEBUG_ERR("oid_copy","Can't allocate %d bytes.", sizeof(oid_t));
DTNMP_DEBUG_EXIT("oid_copy","->NULL",NULL);
return NULL;
}
/* Step 2: Deep copy parameters. */
result->params = utils_datacol_copy(src_oid->params);
/* Step 3: Shallow copies the rest. */
result->type = src_oid->type;
result->nn_id = src_oid->nn_id;
result->value_size = src_oid->value_size;
memcpy(result->value, src_oid->value, result->value_size);
DTNMP_DEBUG_EXIT("oid_copy","->%d", result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_deserialize_comp
*
* \par Purpose: Extracts a compressed OID from a buffer.
*
* \retval NULL - Failure.
* !NULL - The extracted OID.
*
* \param[in] buffer The effective start of the buffer.
* \param[in] size The size of the buffer, in bytes. Don't go past this.
* \param[out] bytes_used The number of consumed buffer bytes.
*
* \par Notes:
* 1. The returned OID is dynamically allocated and must be freed when
* no longer needed.
* 2. The caller must increment the buffer pointed by the number of bytes
* used before trying to deserialize the next thing in the buffer.
* 3. We don't validate the oid here because if the oid is bad, we may
* need to plow through anyway to get to the next object in the buffer,
* so bailing early helps no-one. However, it behooves the caller to
* check that sanity of the OID with a call to oid_sanity_check.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
oid_t *oid_deserialize_comp(unsigned char *buffer,
uint32_t size,
uint32_t *bytes_used)
{
uvast nn_id = 0;
uint32_t bytes = 0;
oid_t *new_oid = NULL;
unsigned char *cursor = NULL;
DTNMP_DEBUG_ENTRY("oid_deserialize_comp","(%#llx,%d,%#llx)",
(unsigned long) buffer,
size,
(unsigned long) bytes_used);
/* Step 0: Sanity Checks. */
if((buffer == NULL) || (bytes_used == NULL) || (size == 0))
{
DTNMP_DEBUG_ERR("oid_deserialize_comp", "Bad args.", NULL);
DTNMP_DEBUG_EXIT("oid_deserialize_comp", "->NULL", NULL);
return NULL;
}
else
{
*bytes_used = 0;
cursor = buffer;
}
/* Step 1: Grab the nickname, which is in an SDNV. */
if((bytes = utils_grab_sdnv(cursor, size, &nn_id)) <= 0)
{
DTNMP_DEBUG_ERR("oid_deserialize_comp", "Can't grab nickname.", NULL);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_comp", "->0", NULL);
return 0;
}
else
{
cursor += bytes;
size -= bytes;
*bytes_used += bytes;
}
/* Step 2: grab the remaining OID, which looks just like a full OID. */
if((new_oid = oid_deserialize_full(cursor, size, &bytes)) == NULL)
{
DTNMP_DEBUG_ERR("oid_deserialize_comp", "Can't grab remaining OID.", NULL);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_comp", "->NULL", NULL);
return NULL;
}
else
{
cursor += bytes;
size -= bytes;
*bytes_used += bytes;
}
/* Step 3: Store extra information in the new OID. */
new_oid->nn_id = nn_id;
new_oid->type = OID_TYPE_COMP_FULL;
DTNMP_DEBUG_EXIT("oid_deserialize_comp","-> %x.", (unsigned long)new_oid);
return new_oid;
}
/******************************************************************************
*
* \par Function Name: oid_deserialize_comp_param
*
* \par Purpose: Extracts a compressed, parameterized OID from a buffer.
*
* \retval NULL - Failure.
* !NULL - The extracted OID.
*
* \param[in] buffer The effective start of the buffer.
* \param[in] size The size of the buffer, in bytes. Don't go past this.
* \param[out] bytes_used The number of consumed buffer bytes.
*
* \par Notes:
* 1. The returned OID is dynamically allocated and must be freed when
* no longer needed.
* 2. The caller must increment the buffer pointed by the number of bytes
* used before trying to deserialize the next thing in the buffer.
* 3. We don't validate the oid here because if the oid is bad, we may
* need to plow through anyway to get to the next object in the buffer,
* so bailing early helps no-one. However, it behooves the caller to
* check that sanity of the OID with a call to oid_sanity_check.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
oid_t *oid_deserialize_comp_param(unsigned char *buffer,
uint32_t size,
uint32_t *bytes_used)
{
uvast nn_id = 0;
uint32_t bytes = 0;
oid_t *new_oid = NULL;
unsigned char *cursor = NULL;
DTNMP_DEBUG_ENTRY("oid_deserialize_comp_param","(%#llx,%d,%#llx)",
(unsigned long) buffer,
size,
(unsigned long) bytes_used);
/* Step 0: Sanity Checks. */
if((buffer == NULL) || (bytes_used == NULL) || (size == 0))
{
DTNMP_DEBUG_ERR("oid_deserialize_comp_param", "Bad args.", NULL);
DTNMP_DEBUG_EXIT("oid_deserialize_comp_param", "->NULL", NULL);
return NULL;
}
else
{
*bytes_used = 0;
cursor = buffer;
}
/* Step 1: Grab the nickname, which is in an SDNV. */
if((bytes = utils_grab_sdnv(cursor, size, &nn_id)) <= 0)
{
DTNMP_DEBUG_ERR("oid_deserialize_comp_param", "Can't grab nickname.", NULL);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_comp_param", "->0", NULL);
return NULL;
}
else
{
cursor += bytes;
size -= bytes;
*bytes_used += bytes;
}
/* Step 2: grab the parameterized, remaining OID. */
if((new_oid = oid_deserialize_param(cursor, size, &bytes)) == NULL)
{
DTNMP_DEBUG_ERR("oid_deserialize_comp_param", "Can't grab remaining OID.",
NULL);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_comp_param", "->NULL", NULL);
return NULL;
}
else
{
cursor += bytes;
size -= bytes;
*bytes_used += bytes;
}
/* Step 3: Store extra information in the new OID. */
new_oid->nn_id = nn_id;
new_oid->type = OID_TYPE_COMP_PARAM;
DTNMP_DEBUG_EXIT("oid_deserialize_comp_param","-> %x.", (unsigned long)new_oid);
return new_oid;
}
/******************************************************************************
*
* \par Function Name: oid_deserialize_full
*
* \par Purpose: Extracts a regular OID from a buffer.
*
* \todo We currently assume that the length field is a single byte (0-127).
* We need to code this to follow BER rules for large definite data
* form. Like SDNV but not quite: if high-bit of first byte set, bits
* 7-1 give # octets that comprise length. Then, concatenate, big-endian,
* those octets to build length. Ex: length 435 = 0x8201B3
*
* \retval NULL - Failure.
* !NULL - The extracted OID.
*
* \param[in] buffer The effective start of the buffer.
* \param[in] size The size of the buffer, in bytes. Don't go past this.
* \param[out] bytes_used The number of consumed buffer bytes.
*
* \par Notes:
* 1. The returned OID is dynamically allocated and must be freed when
* no longer needed.
* 2. The caller must increment the buffer pointed by the number of bytes
* used before trying to deserialize the next thing in the buffer.
* 3. We don't validate the oid here because if the oid is bad, we may
* need to plow through anyway to get to the next object in the buffer,
* so bailing early helps no-one. However, it behooves the caller to
* check that sanity of the OID with a call to oid_sanity_check.
* 4. We DO bail if the OID is larger than we can accommodate, though.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
oid_t *oid_deserialize_full(unsigned char *buffer,
uint32_t size,
uint32_t *bytes_used)
{
uint32_t oid_size = 0;
oid_t *new_oid = NULL;
uint8_t val = 0;
unsigned char *cursor = NULL;
DTNMP_DEBUG_ENTRY("oid_deserialize_full","(%#llx,%d,%#llx)",
( unsigned long) buffer, size, (unsigned long) bytes_used);
/* Step 0: Sanity Checks. */
if((buffer == NULL) || (bytes_used == NULL) || (size == 0))
{
DTNMP_DEBUG_ERR("oid_deserialize_full", "Bad args.", NULL);
DTNMP_DEBUG_EXIT("oid_deserialize_full", "->NULL", NULL);
return NULL;
}
else
{
*bytes_used = 0;
cursor = buffer;
}
/*
* Step 1: Grab # SDNVs in the OID
* \todo: Check if this should be a byte or an SDNV.
*/
if((*bytes_used = utils_grab_byte(cursor, size, &val)) != 1)
{
DTNMP_DEBUG_ERR("oid_deserialize_full", "Can't grab size byte.", NULL);
DTNMP_DEBUG_EXIT("oid_deserialize_full", "-> NULL", NULL);
return NULL;
}
else
{
cursor += *bytes_used;
size -= *bytes_used;
oid_size = val;
}
/*
* Step 2: Make sure OID fits within our size. We add 1 to oid_size to
* account for the # bytes parameter.
* \todo Change the 1 here to something else if me move to SDNV for #bytes.
*/
if((oid_size + 1) >= MAX_OID_SIZE)
{
DTNMP_DEBUG_ERR("oid_deserialize_full","Size %d exceeds supported size %d",
oid_size + 1, MAX_OID_SIZE);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_full","-> NULL", NULL);
return NULL;
}
/* Step 3: Make sure we have oid_size bytes left in the buffer. */
if(oid_size > size)
{
DTNMP_DEBUG_ERR("oid_deserialize_full","Can't read %d bytes from %d sized buf.",
oid_size, size);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_full","-> NULL", NULL);
return NULL;
}
/* Step 4: Allocate the target OID object. */
if((new_oid = (oid_t*)MTAKE(sizeof(oid_t))) == NULL)
{
DTNMP_DEBUG_ERR("oid_deserialize_full","Cannot allocate new OID.", NULL);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_full","-> NULL", NULL);
return NULL;
}
/* Step 5: Copy in the data contents of the OID. We don't interpret the
* values here, just put them in.
*/
new_oid->value[0] = oid_size;
memcpy(&(new_oid->value[1]), cursor, oid_size);
new_oid->value_size = oid_size + 1;
*bytes_used += oid_size;
if((new_oid->params = lyst_create()) == NULL)
{
DTNMP_DEBUG_ERR("oid_deserialize_full","Cannot allocate param lyst.", NULL);
MRELEASE(new_oid);
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_full","-> NULL", NULL);
return NULL;
}
new_oid->type = OID_TYPE_FULL;
DTNMP_DEBUG_EXIT("oid_deserialize_full","-> new_oid: %#llx bytes_used %d",
(unsigned long)new_oid, *bytes_used);
return new_oid;
}
/******************************************************************************
*
* \par Function Name: oid_deserialize_param
*
* \par Purpose: Extracts a parameterized OID from a buffer.
*
* \retval NULL - Failure.
* !NULL - The extracted OID.
*
* \param[in] buffer The effective start of the buffer.
* \param[in] size The size of the buffer, in bytes. Don't go past this.
* \param[out] bytes_used The number of consumed buffer bytes.
*
* \par Notes:
* 1. The returned OID is dynamically allocated and must be freed when
* no longer needed.
* 2. The caller must increment the buffer pointed by the number of bytes
* used before trying to deserialize the next thing in the buffer.
* 3. We don't validate the oid here because if the oid is bad, we may
* need to plow through anyway to get to the next object in the buffer,
* so bailing early helps no-one. However, it behooves the caller to
* check that sanity of the OID with a call to oid_sanity_check.
* 4. We DO bail if the OID is larger than we can accommodate, though.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/27/12 E. Birrane Initial implementation,
*****************************************************************************/
oid_t *oid_deserialize_param(unsigned char *buffer,
uint32_t size,
uint32_t *bytes_used)
{
oid_t *new_oid;
uint32_t bytes = 0;
uvast value = 0;
uint32_t idx = 0;
unsigned char *cursor = NULL;
DTNMP_DEBUG_ENTRY("oid_deserialize_param","(%#llx,%d,%#llx)",
(unsigned long) buffer,
size,
(unsigned long) bytes_used);
/* Step 0: Sanity Checks... */
if((buffer == NULL) || (bytes_used == NULL))
{
DTNMP_DEBUG_ERR("oid_deserialize_param","NULL input values!",NULL);
DTNMP_DEBUG_EXIT("oid_deserialize_param","->NULL",NULL);
return NULL;
}
else
{
*bytes_used = 0;
cursor = buffer;
}
/* Step 1: Grab the regular OID and store it. */
new_oid = oid_deserialize_full(cursor, size, &bytes);
if((bytes == 0) || (new_oid == NULL))
{
DTNMP_DEBUG_ERR("oid_deserialize_param","Could not grab OID.", NULL);
oid_release(new_oid); /* release just in case bytes was 0. */
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_param","-> NULL", NULL);
return NULL;
}
else
{
cursor += bytes;
size -= bytes;
*bytes_used += bytes;
}
/* Step 2: Grab the data collection representing parameters. Only if there
* are parameters to grab.
*/
if(size > 0)
{
if((new_oid->params = utils_datacol_deserialize(cursor, size, &bytes)) == NULL)
{
DTNMP_DEBUG_ERR("oid_deserialize_param","Could not grab params.", NULL);
oid_release(new_oid); /* release just in case bytes was 0. */
*bytes_used = 0;
DTNMP_DEBUG_EXIT("oid_deserialize_param","-> NULL", NULL);
return NULL;
}
else
{
cursor += bytes;
size -= bytes;
*bytes_used += bytes;
}
}
/* Step 3: Fill in any other parts of the OID. */
new_oid->type = OID_TYPE_PARAM;
DTNMP_DEBUG_EXIT("oid_deserialize_param","-> %#llx.", (unsigned long) new_oid);
return new_oid;
}
/******************************************************************************
*
* \par Function Name: oid_pretty_print
*
* \par Purpose: Builds a string representation of the OID suitable for
* diagnostic viewing.
*
* \retval NULL: Failure to construct a string representation of the OID.
* !NULL: The string representation of the OID string.
*
* \param[in] oid The OID whose string representation is being created.
*
* \par Notes:
* 1. This is a slow, wasteful function and should not be called in
* embedded systems. For something to dump in a log, try the function
* oid_to_string instead.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 11/14/12 E. Birrane Initial implementation,
*****************************************************************************/
char *oid_pretty_print(oid_t *oid)
{
int size = 0;
char *str = NULL;
char *result = NULL;
char *cursor = NULL;
LystElt elt;
datacol_entry_t *entry = NULL;
DTNMP_DEBUG_ENTRY("oid_pretty_print","(%#llx)", (unsigned long) oid);
/* Step 0: Sanity Check. */
if(oid == NULL)
{
DTNMP_DEBUG_ERR("oid_pretty_print","NULL OID.",NULL);
DTNMP_DEBUG_EXIT("oid_pretty_print","->NULL.",NULL);
return NULL;
}
/* Step 1: Guestimate size. This is, at best, a dark art and prone to
* exaggeration. Numerics are assumed to take 5 bytes, unless I
* think they will take 3 instead (indices vs. values). Other
* values are based on constant strings in the function. One must
* update this calculation if one changes string constants, else
* one will be sorry.
*/
size = 33 + /* Header */
11 + /* OID type as text */
(lyst_length(oid->params) * 22) + /* Each parameter index */
(MAX_OID_PARM * (20 * MAX_OID_SIZE)) + /* Parameters */
12 + /* nn_id */
18 + /* value_size */
7 + (oid->value_size * 4) + /* OID values */
22; /* Footer. */
/* Step 2: Allocate the string. */
if((result = (char*)MTAKE(size)) == NULL)
{
DTNMP_DEBUG_ERR("oid_pretty_print", "Can't alloc %d bytes.", size);
DTNMP_DEBUG_EXIT("oid_pretty_print","->NULL",NULL);
return NULL;
}
/* Step 3: Populate the allocated string. Keep an eye on the size. */
cursor = result;
cursor += sprintf(cursor,"OID:\n---------------------\nType: ");
switch(oid->type)
{
case 0: cursor += sprintf(cursor,"FULL\n"); break;
case 1: cursor += sprintf(cursor,"PARAM\n"); break;
case 2: cursor += sprintf(cursor,"COMP_FULL\n"); break;
case 3: cursor += sprintf(cursor,"COMP_PARAM\n"); break;
default: cursor += sprintf(cursor,"UNKNOWN\n"); break;
}
cursor += sprintf(cursor,"num_parm: %ld\n", lyst_length(oid->params));
int i = 0;
for(elt = lyst_first(oid->params); elt; elt = lyst_next(elt))
{
entry = (datacol_entry_t*)lyst_data(elt);
str = utils_hex_to_string(entry->value, entry->length);
cursor += sprintf(cursor, "Parm %d:%s\n",i,str);
MRELEASE(str);
i++;
}
cursor += sprintf(cursor, "nn_id: %d\n", (uint32_t)oid->nn_id);
cursor += printf(cursor, "value_size: %d\n", oid->value_size);
str = oid_to_string(oid);
cursor += sprintf(cursor, "value: %s\n---------------------\n\n", str);
MRELEASE(str);
/* Step 4: Sanity check. */
if((cursor - result) > size)
{
DTNMP_DEBUG_ERR("oid_pretty_print", "OVERWROTE! Alloc %d, wrote %llu.",
size, (cursor-result));
MRELEASE(result);
DTNMP_DEBUG_EXIT("oid_pretty_print","->NULL",NULL);
return NULL;
}
DTNMP_DEBUG_INFO("oid_pretty_print","Wrote %llu into %d string.",
(cursor -result), size);
DTNMP_DEBUG_EXIT("oid_pretty_print","->%#llx",result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_release
*
* \par Purpose: Frees resources associated with an OID.
*
* \retval void
*
* \param[in,out] oid The OID being freed.
*
* \par Notes:
* 1. Very little to do here as an OID is statically loaded structure.
* Function remains for if/when we refactor OIDs to be more dynamic.
* 2. The OID pointer is garbage after this call and must not be
* referenced again.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
void oid_release(oid_t *oid)
{
DTNMP_DEBUG_ENTRY("oid_release", "(%#llx)", (unsigned long) oid);
if(oid != NULL)
{
oid_clear(oid);
MRELEASE(oid);
oid = NULL;
}
DTNMP_DEBUG_EXIT("oid_release", "-> NULL", NULL);
}
/******************************************************************************
*
* \par Function Name: oid_sanity_check
*
* \par Purpose: Checks an oid structure for proper values.
*
* \retval <=0 - Failure. The OID is not sane.
* >0 - Success. The OID is sane.
*
* \param[in] oid The OID whose sanity is in question.
*
* \par Notes:
* 1. We don't bail on the first failure so as to better populate the
* error log in instances where we have an OID with multiple problems.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
int oid_sanity_check(oid_t *oid)
{
int result = 1;
DTNMP_DEBUG_ENTRY("oid_sanity_check","(%#llx)", (unsigned long) oid);
/* NULL Checks. Perform all so that we get lots of logging in case
* there are multiple problems with the OID. */
if(oid == NULL)
{
DTNMP_DEBUG_ERR("oid_sanity_check","NULL oid.", NULL);
result = 0;
}
if(oid->type > 3)
{
DTNMP_DEBUG_ERR("oid_sanity_check","Bad type: %d.", oid->type);
result = 0;
}
if(lyst_length(oid->params) > MAX_OID_PARM)
{
DTNMP_DEBUG_ERR("oid_sanity_check","Too many params: %d.",
lyst_length(oid->params));
result = 0;
}
uint32_t size = oid_calc_size(oid);
if(size > MAX_OID_SIZE)
{
DTNMP_DEBUG_ERR("oid_sanity_check","Parm size %d bigger than max %d",
size, MAX_OID_SIZE);
result = 0;
}
if(oid->value_size > MAX_OID_SIZE)
{
DTNMP_DEBUG_ERR("oid_sanity_check","Val size %d bigger than max %d",
oid->value_size, MAX_OID_SIZE);
result = 0;
}
DTNMP_DEBUG_EXIT("oid_sanity_check","->%d", result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_serialize
*
* \par Purpose: Generate full, serialized version of the OID.
*
* \todo Add a mechanism to determine whether we should expand a nickname or
* not when serializing the OID.
*
* \todo Instead of counting exact size, it will be faster to simply allocate
* the MAX_OID_SIZE and populate up to that point. Not sure if we are
* going to be space or time constrained just yet. While the nn_id lookup
* stays expensive we may not want to call it 2x as part of serialization
* (once to calculate size and again to grab values).
*
*
* \retval NULL - Failure serializing
* !NULL - Serialized stream.
*
* \param[in] oid The OID to be serialized.
* \param[out] size The size of the resulting serialized OID.
*
* \par Notes:
* 1. The result is allocated on the memory pool and must be released when
* no longer needed.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
uint8_t *oid_serialize(oid_t *oid, uint32_t *size)
{
uint8_t *result = NULL;
uint8_t *cursor = NULL;
uint8_t *parms = NULL;
uint32_t parm_size = 0;
uint32_t idx = 0;
Sdnv nn_sdnv;
DTNMP_DEBUG_ENTRY("oid_serialize","(%#llx,%#llx)",
(unsigned long)oid, (unsigned long)size);
/* Step 0: Sanity Check */
if((oid == NULL) || (size == NULL))
{
DTNMP_DEBUG_ERR("oid_serialize","Bad args.",NULL);
DTNMP_DEBUG_EXIT("oid_serialize","->NULL",NULL);
return NULL;
}
/* Step 1: Count up the total size of the OID. */
if((*size = oid_calc_size(oid)) == 0)
{
DTNMP_DEBUG_ERR("oid_serialize","Bad size %d.",*size);
*size = 0;
DTNMP_DEBUG_EXIT("oid_serialize","->NULL",NULL);
return NULL;
}
/* Step 2: Sanity check the size. */
if(*size > MAX_OID_SIZE)
{
DTNMP_DEBUG_ERR("oid_serialize","Size %d bigger than max of %d.",
*size, MAX_OID_SIZE);
*size = 0;
DTNMP_DEBUG_EXIT("oid_serialize","->NULL",NULL);
return NULL;
}
/* Step 3: Allocate the serialized buffer.*/
if((result = (uint8_t *) MTAKE(*size)) == NULL)
{
DTNMP_DEBUG_ERR("oid_serialize","Couldn't allocate %d bytes.",
*size);
*size = 0;
DTNMP_DEBUG_EXIT("oid_serialize","->NULL",NULL);
return NULL;
}
else
{
cursor = result;
}
/* Step 4: Copy in the nickname portion. */
if((oid->type == OID_TYPE_COMP_FULL) || (oid->type == OID_TYPE_COMP_PARAM))
{
encodeSdnv(&nn_sdnv, oid->nn_id);
memcpy(cursor, nn_sdnv.text, nn_sdnv.length);
cursor += nn_sdnv.length;
}
/* Step 5: Copy in the value portion. */
memcpy(cursor,oid->value, oid->value_size);
cursor += oid->value_size;
/* Step 6: Copy in the parameters portion. */
if((oid->type == OID_TYPE_PARAM) || (oid->type == OID_TYPE_COMP_PARAM))
{
parms = utils_datacol_serialize(oid->params, &parm_size);
if((parms == NULL) || (parm_size == 0))
{
DTNMP_DEBUG_ERR("oid_serialize","Can't serialize parameters.",NULL);
*size = 0;
MRELEASE(result);
DTNMP_DEBUG_EXIT("oid_serialize","->NULL",NULL);
return NULL;
}
memcpy(cursor, parms, parm_size);
cursor += parm_size;
MRELEASE(parms);
}
/* Step 7: Final sanity check */
if((cursor-result) > *size)
{
DTNMP_DEBUG_ERR("oid_serialize","Serialized %d bytes but counted %d!",
(cursor-result), *size);
*size = 0;
MRELEASE(result);
DTNMP_DEBUG_EXIT("oid_serialize","->NULL",NULL);
return NULL;
}
DTNMP_DEBUG_EXIT("oid_serialize","->%#llx",(unsigned long)result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_to_string
*
* \par Purpose: Create a compact string representation of the OID.
*
* \retval NULL - Failure building string version of the OID.
* !NULL - The compact string representing the OID.
*
* \param[in] oid The OID whose string representation is being calculated.
*
* \par Notes:
* 1. This function allocates memory from the memory pool. The returned
* string MUST be released when no longer needed.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
char *oid_to_string(oid_t *oid)
{
char *result = NULL;
uint32_t size = 0;
DTNMP_DEBUG_ENTRY("oid_to_string","(%#llx)",(unsigned long) oid);
/* Step 0: Sanity Check. */
if(oid == NULL)
{
DTNMP_DEBUG_ERR("oid_to_string","NULL OID",NULL);
DTNMP_DEBUG_EXIT("oid_to_string","->NULL.",NULL);
return NULL;
}
/* Step 1: Walk through and collect size. */
/* For now, we just show the raw MID. */
result = utils_hex_to_string(oid->value, oid->value_size);
DTNMP_DEBUG_EXIT("oid_to_string","->%s.", result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_nn_add
*
* \par Purpose: Adds a nickname to the database.
*
* \retval 0 Failure.
* !0 Success.
*
* \param[in] oid The OID whose string representation is being calculated.
*
* \par Notes:
* 1. We will allocate our own entry on addition, the passed-in structure
* may be deleted or changed after this call.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
int oid_nn_add(oid_nn_t *nn)
{
oid_nn_t *new_nn = NULL;
DTNMP_DEBUG_ENTRY("oid_nn_add","(%#llx)",(unsigned long)nn);
/* Step 0: Sanity check. */
if(nn == NULL)
{
DTNMP_DEBUG_ERR("oid_nn_add","Bad args.",NULL);
DTNMP_DEBUG_EXIT("oid_nn_add","->0",NULL);
return 0;
}
/* Need to lock early so our uniqueness check (step 1) doesn't change by
* the time we go to insert in step 4. */
lockResource(&nn_db_mutex);
/* Step 1: Make sure entry doesn't already exist. */
if(oid_nn_exists(nn->id))
{
DTNMP_DEBUG_ERR("oid_nn_add","Id 0x%x already exists in db.",
nn->id);
unlockResource(&nn_db_mutex);
DTNMP_DEBUG_EXIT("oid_nn_add","->0",NULL);
return 0;
}
/* Step 2: Allocate new entry. */
if ((new_nn = (oid_nn_t*)MTAKE(sizeof(oid_nn_t))) == NULL)
{
DTNMP_DEBUG_ERR("oid_nn_add","Can't take %d bytes for new nn.",
sizeof(oid_nn_t));
unlockResource(&nn_db_mutex);
DTNMP_DEBUG_EXIT("oid_nn_add","->0",NULL);
return 0;
}
/* Step 3: Populate new entry with passed-in data. */
memcpy(new_nn, nn, sizeof(oid_nn_t));
/* Step 4: Add new entry to the db. */
lyst_insert_first(nn_db, new_nn);
unlockResource(&nn_db_mutex);
DTNMP_DEBUG_EXIT("oid_nn_add","->1",NULL);
return 1;
}
/******************************************************************************
*
* \par Function Name: oid_nn_cleanup
*
* \par Purpose: Breaks down the nickname database.
*
* \retval void
*
* \par Notes:
* 1. We assume there are no other people who will use the nn_db after
* this!
* 2. We also kill the mutex around the database.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
void oid_nn_cleanup()
{
LystElt elt;
oid_nn_t *entry = NULL;
DTNMP_DEBUG_ENTRY("oid_nn_cleanup","()",NULL);
/* Step 0: Sanity Check. */
if(nn_db == NULL)
{
DTNMP_DEBUG_WARN("oid_nn_cleanup","NN database already cleaned.",NULL);
return;
}
/* Step 1: Wait for folks to be done with the database. */
lockResource(&nn_db_mutex);
/* Step 2: Release each entry. */
for (elt = lyst_first(nn_db); elt; elt = lyst_next(elt))
{
entry = (oid_nn_t*) lyst_data(elt);
if (entry != NULL)
{
MRELEASE(entry);
}
else
{
DTNMP_DEBUG_WARN("oid_nn_cleanup","Found NULL entry in nickname db.",
NULL);
}
}
lyst_destroy(nn_db);
/* Step 3: Clean up locking mechanisms too. */
unlockResource(&nn_db_mutex);
killResourceLock(&nn_db_mutex);
}
/******************************************************************************
*
* \par Function Name: oid_nn_delete
*
* \par Purpose: Removes a nickname from the database.
*
* \retval 0 - Entry not found (or other error)
* !0 - Success.
*
* \param[in] nn_id The ID of the entry to remove.
*
* \par Notes:
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
int oid_nn_delete(uvast nn_id)
{
oid_nn_t *cur_nn = NULL;
LystElt tmp_elt;
int result = 0;
DTNMP_DEBUG_ENTRY("oid_nn_delete","(%#llx)",nn_id);
/* Step 1: Need to lock to preserve validity of the lookup result. . */
lockResource(&nn_db_mutex);
/* Step 2: If you find it, delete it. */
if((tmp_elt = oid_nn_exists(nn_id)) != NULL)
{
cur_nn = (oid_nn_t*) lyst_data(tmp_elt);
lyst_delete(tmp_elt);
MRELEASE(cur_nn);
result = 1;
}
unlockResource(&nn_db_mutex);
DTNMP_DEBUG_EXIT("oid_nn_delete","->%d",result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_nn_exists
*
* \par Purpose: Determines if an ID is in the nickname db.
*
* \retval NULL - Not found.
* !NULL - ELT pointing to the found element.
*
* \param[in] nn_id The ID of the nickname whose existence is in question.
*
* \todo There is probably a smarter way to do this with a lyst find function
* and a search callback.
*
* \par Notes:
* 1. LystElt is a pointer. Handle this handle with care.
* 2. We break abstraction here and return a Lyst structure because this
* lookup function is often called in the context of lyst maintenance,
* but if we were to change the underlying nickname database storage
* approach, this function would, clearly, need to change. Those who
* do not like this approach are referred to the much less deprecable
* function: oid_find.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
LystElt oid_nn_exists(uvast nn_id)
{
oid_nn_t *cur_nn = NULL;
LystElt tmp_elt = NULL;
LystElt result = NULL;
DTNMP_DEBUG_ENTRY("oid_nn_exists","(%#llx)",nn_id);
/* Step 0: Make sure no +/-'s while we search. */
lockResource(&nn_db_mutex);
for(tmp_elt = lyst_first(nn_db); tmp_elt; tmp_elt = lyst_next(tmp_elt))
{
cur_nn = (oid_nn_t*) lyst_data(tmp_elt);
if(cur_nn != NULL)
{
if(cur_nn->id == nn_id)
{
result = tmp_elt;
break;
}
}
else
{
DTNMP_DEBUG_WARN("oid_nn_exists","Encountered NULL nn?",NULL);
}
}
unlockResource(&nn_db_mutex);
DTNMP_DEBUG_EXIT("oid_nn_delete","->%x",result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_nn_find
*
* \par Purpose: Convenience function to grab a nickname entry from its ID.
*
* \retval NULL - Item not found.
* !NULL - Handle to the found item.
*
* \param[in] nn_id The ID of the nickname to find.
*
* \todo There is probably a smarter way to do this with a lyst find function
* and a search callback.
*
* \par Notes:
* 1. The returned pointer should NOT be released. It points directly into
* the nickname list.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
oid_nn_t* oid_nn_find(uvast nn_id)
{
LystElt tmpElt = NULL;
oid_nn_t *result = NULL;
DTNMP_DEBUG_ENTRY("oid_nn_find","(%#llx)",nn_id);
/* Step 0: Call exists function (exists should block mutex, so we don't. */
if((tmpElt = oid_nn_exists(nn_id)) != NULL)
{
result = (oid_nn_t*) lyst_data(tmpElt);
}
DTNMP_DEBUG_EXIT("oid_nn_find","->%#llx",result);
return result;
}
/******************************************************************************
*
* \par Function Name: oid_nn_init
*
* \par Purpose: Initialize the nickname database for OIDs.
*
* \retval <0 - Failure.
* 0 - Success.
*
* \param[in] nn_id The ID of the nickname to find.
*
* \todo Add functions here to read the nickname database from persistent
* storage, such as an SDR.
*
* \par Notes:
* 1. nn_db must only hold items that have been dynamically allocated
* from the memory pool.
*
* Modification History:
* MM/DD/YY AUTHOR DESCRIPTION
* -------- ------------ ---------------------------------------------
* 10/14/12 E. Birrane Initial implementation,
*****************************************************************************/
int oid_nn_init()
{
DTNMP_DEBUG_ENTRY("oid_init_nn_db","()",NULL);
/* Step 0: Sanity Check. */
if(nn_db != NULL)
{
DTNMP_DEBUG_WARN("oid_nn_init","Trying to init existing NN db.",NULL);
return 0;
}
if((nn_db = lyst_create()) == NULL)
{
DTNMP_DEBUG_ERR("oid_nn_init","Can't allocate NN DB!", NULL);
DTNMP_DEBUG_EXIT("oid_nn_init","->-1.",NULL);
return -1;
}
if(initResourceLock(&nn_db_mutex))
{
DTNMP_DEBUG_ERR("oid_init_nn_db","Unable to initialize mutex, errno = %s",
strerror(errno));
DTNMP_DEBUG_EXIT("oid_init_nn_db","->-1.",NULL);
return -1;
}
DTNMP_DEBUG_EXIT("oid_init_nn_db","->0.",NULL);
return 0;
}
|