snmp_pdu.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2009 by egnite GmbH
  3. * Copyright 1998-2007 by egnite Software GmbH
  4. * Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. #include <compiler.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <pro/snmp_pdu.h>
  38. /*!
  39. * \addtogroup xgSNMP
  40. */
  41. /*@{*/
  42. /*!
  43. * \brief Create PDU with a given type and object ID.
  44. *
  45. * \param cmd PDU type.
  46. * \param name Object identifier.
  47. * \param nlen Number of sub IDs of the object identifier.
  48. */
  49. SNMP_PDU *SnmpPduCreate(int cmd, const OID * name, size_t nlen)
  50. {
  51. SNMP_PDU *pdu = calloc(1, sizeof(SNMP_PDU));
  52. if (pdu) {
  53. /* Set the PDU type. */
  54. pdu->pdu_cmd = cmd;
  55. /* Set the device identifier. */
  56. pdu->pdu_enterprise = malloc(nlen * sizeof(OID));
  57. if (pdu->pdu_enterprise) {
  58. memcpy(pdu->pdu_enterprise, name, nlen * sizeof(OID));
  59. pdu->pdu_enterprise_length = nlen;
  60. } else {
  61. free(pdu);
  62. pdu = NULL;
  63. }
  64. }
  65. return pdu;
  66. }
  67. /*!
  68. * \brief Add variable to PDU.
  69. *
  70. * \param pdu Pointer to the PDU.
  71. * \param name Variable name.
  72. * \param nlen Number of sub IDs of the variable name.
  73. * \param type Variable type.
  74. * \param value Pointer to the variable's value.
  75. * \param vlen Length of the variable value in byts.
  76. */
  77. int SnmpPduAddVariable(SNMP_PDU * pdu, OID * name, size_t nlen, uint8_t type, uint8_t * value, size_t vlen)
  78. {
  79. SNMP_VARLIST *var = calloc(1, sizeof(SNMP_VARLIST));
  80. if (var == NULL) {
  81. return -1;
  82. }
  83. var->var_name = malloc(nlen * sizeof(OID));
  84. if (var->var_name == NULL) {
  85. free(var);
  86. return -1;
  87. }
  88. memcpy(var->var_name, name, nlen * sizeof(OID));
  89. var->var_nlen = nlen;
  90. var->var_type = type;
  91. switch (type) {
  92. case ASN_COUNTER:
  93. case ASN_GAUGE:
  94. case ASN_INTEGER:
  95. case ASN_TIMETICKS:
  96. case ASN_UINTEGER:
  97. if (vlen == 0) {
  98. vlen = sizeof(long);
  99. }
  100. var->var_vptr = (uint8_t *) & var->var_val;
  101. memcpy(var->var_vptr, value, vlen);
  102. var->var_vlen = sizeof(long);
  103. break;
  104. default:
  105. if (vlen) {
  106. var->var_vptr = malloc(vlen);
  107. if (var->var_vptr) {
  108. memcpy(var->var_vptr, value, vlen);
  109. var->var_vlen = vlen;
  110. }
  111. }
  112. break;
  113. }
  114. if (pdu->pdu_variables) {
  115. SNMP_VARLIST *vp;
  116. for (vp = pdu->pdu_variables; vp->var_next; vp = vp->var_next);
  117. vp->var_next = var;
  118. } else {
  119. pdu->pdu_variables = var;
  120. }
  121. return 0;
  122. }
  123. /*!
  124. * \brief Destroy PDU.
  125. *
  126. * \param pdu Pointer to the PDU info, obtained by a call to SnmpPduCreate().
  127. */
  128. void SnmpPduDestroy(SNMP_PDU * pdu)
  129. {
  130. SNMP_VARLIST *vp;
  131. SNMP_VARLIST *ovp;
  132. vp = pdu->pdu_variables;
  133. while (vp) {
  134. if (vp->var_name) {
  135. free(vp->var_name);
  136. }
  137. if (vp->var_vptr && vp->var_vptr != (uint8_t *) & vp->var_val) {
  138. free(vp->var_vptr);
  139. }
  140. ovp = vp;
  141. vp = vp->var_next;
  142. free(ovp);
  143. }
  144. if (pdu->pdu_enterprise) {
  145. free(pdu->pdu_enterprise);
  146. }
  147. free(pdu);
  148. }
  149. /*@}*/