snmp_session.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <sys/confnet.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <pro/snmp_auth.h>
  38. #include <pro/snmp_session.h>
  39. /*!
  40. * \addtogroup xgSNMP
  41. */
  42. /*@{*/
  43. static uint32_t snmp_reqid;
  44. static uint8_t temp_buffer[SNMP_MAX_LEN];
  45. /*!
  46. * \brief Open SNMP session.
  47. *
  48. * \param ip Remote IP address.
  49. * \param port Remote port number.
  50. * \param id Community name.
  51. * \param idlen Length of the community name.
  52. *
  53. * \return Pointer to a newly created session info.
  54. */
  55. SNMP_SESSION *SnmpSessionOpen(uint32_t ip, uint16_t port, uint8_t * id, size_t idlen)
  56. {
  57. SNMP_SESSION *session = calloc(1, sizeof(SNMP_SESSION));
  58. if (session) {
  59. /* Create UDP socket at random local port. */
  60. session->sess_sock = NutUdpCreateSocket(0);
  61. if (session->sess_sock == NULL) {
  62. /* Failed to create socket. */
  63. free(session);
  64. session = NULL;
  65. } else {
  66. /* Set remote address/port. */
  67. session->sess_rem_addr = ip;
  68. session->sess_rem_port = port;
  69. /* Set session identifier. */
  70. memcpy(session->sess_id, id, idlen);
  71. session->sess_id_len = idlen;
  72. }
  73. }
  74. return session;
  75. }
  76. /*!
  77. * \brief Close SNMP session.
  78. *
  79. * \param session Points to the session info, obtained by calling
  80. * SnmpSessionOpen().
  81. */
  82. void SnmpSessionClose(SNMP_SESSION * session)
  83. {
  84. if (session->sess_sock) {
  85. NutUdpDestroySocket(session->sess_sock);
  86. }
  87. free(session);
  88. }
  89. /*!
  90. * \brief Build SNMP packet.
  91. *
  92. * \param session Points to the session info, obtained by calling
  93. * SnmpSessionOpen().
  94. * \param pdu Points to the PDU info, obtained by calling
  95. * SnmpPduCreate().
  96. * \param packet Pointer to a packet buffer.
  97. * \param psize Points to a variable that contains the number of
  98. * available bytes in the packet buffer. On exit, it is
  99. * returned as the number of valid bytes in the packet buffer.
  100. *
  101. * \return 0 on success, -1 otherwise.
  102. */
  103. static int SnmpMsgBuild(SNMP_SESSION * session, SNMP_PDU * pdu, uint8_t * packet, size_t * psize)
  104. {
  105. uint8_t *cp;
  106. SNMP_VARLIST *vp;
  107. size_t length;
  108. int total;
  109. long lval;
  110. /*
  111. * Encode the list of variable bindings.
  112. */
  113. length = *psize;
  114. cp = packet;
  115. for (vp = pdu->pdu_variables; vp; vp = vp->var_next) {
  116. cp = SnmpVarBuild(cp, &length, vp->var_name, vp->var_nlen, vp->var_type, vp->var_vptr, vp->var_vlen);
  117. if (cp == NULL) {
  118. return -1;
  119. }
  120. }
  121. total = cp - packet;
  122. /*
  123. * Encode the sequence header of the variable bindings.
  124. */
  125. length = SNMP_MAX_LEN;
  126. cp = AsnHeaderBuild(temp_buffer, &length, ASN_SEQUENCE | ASN_CONSTRUCTOR, total);
  127. if (cp == NULL) {
  128. return -1;
  129. }
  130. /*
  131. * Append the variable bindings to its header.
  132. */
  133. memcpy(cp, packet, total);
  134. total += cp - temp_buffer;
  135. /*
  136. * Encode the PDU.
  137. *
  138. * Note the different format used for traps.
  139. */
  140. length = *psize;
  141. if (pdu->pdu_cmd == SNMP_MSG_TRAP) {
  142. /* Enterprise. */
  143. cp = AsnOidBuild(packet, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID, pdu->pdu_enterprise, pdu->pdu_enterprise_length);
  144. if (cp == NULL) {
  145. return -1;
  146. }
  147. /* Agent address. */
  148. cp = AsnOctetStringBuild(cp, &length, ASN_IPADDRESS, (u_char *) & confnet.cdn_ip_addr, sizeof(confnet.cdn_ip_addr));
  149. if (cp == NULL) {
  150. return -1;
  151. }
  152. /* Generic trap. */
  153. lval = (long)pdu->pdu_trap_type;
  154. cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, &lval);
  155. if (cp == NULL) {
  156. return -1;
  157. }
  158. /* Specific trap. */
  159. lval = (long)pdu->pdu_specific_type;
  160. cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, &lval);
  161. if (cp == NULL) {
  162. return -1;
  163. }
  164. /* Time stamp. */
  165. cp = AsnIntegerBuild(cp, &length, ASN_TIMETICKS, (long *) &pdu->pdu_time);
  166. if (cp == NULL) {
  167. return -1;
  168. }
  169. } else {
  170. /* Request ID. */
  171. cp = AsnIntegerBuild(packet, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_reqid);
  172. if (cp == NULL) {
  173. return -1;
  174. }
  175. /* Error status. */
  176. cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_errstat);
  177. if (cp == NULL) {
  178. return -1;
  179. }
  180. /* Error index. */
  181. cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_errindex);
  182. if (cp == NULL) {
  183. return -1;
  184. }
  185. }
  186. /*
  187. * Append the variable bindings.
  188. */
  189. if (length < total) {
  190. return -1;
  191. }
  192. memcpy(cp, temp_buffer, total);
  193. total += cp - packet;
  194. /*
  195. * Encode the PDU header containing its type.
  196. */
  197. length = SNMP_MAX_LEN;
  198. cp = AsnHeaderBuild(temp_buffer, &length, (u_char) pdu->pdu_cmd, total);
  199. if (cp == NULL || length < total) {
  200. return -1;
  201. }
  202. /*
  203. * Append the PDU body.
  204. */
  205. memcpy(cp, packet, total);
  206. total += cp - temp_buffer;
  207. /*
  208. * Encode the message header with version and community string.
  209. */
  210. length = *psize;
  211. cp = SnmpAuthBuild(session, packet, &length, total);
  212. if (cp == NULL) {
  213. return -1;
  214. }
  215. /*
  216. * Append the PDU.
  217. */
  218. if ((*psize - (cp - packet)) < total) {
  219. return -1;
  220. }
  221. memcpy(cp, temp_buffer, total);
  222. total += cp - packet;
  223. *psize = total;
  224. return 0;
  225. }
  226. /*!
  227. * \brief Send PDU.
  228. *
  229. * \param session Points to the session info, obtained by calling
  230. * SnmpSessionOpen().
  231. * \param pdu Points to the PDU info, obtained by calling
  232. * SnmpPduCreate().
  233. *
  234. * \return 0 on success, -1 otherwise.
  235. */
  236. int SnmpSessionSendPdu(SNMP_SESSION * session, SNMP_PDU * pdu)
  237. {
  238. int rc = -1;
  239. uint8_t *packet;
  240. size_t length;
  241. if (pdu->pdu_cmd == SNMP_MSG_TRAP) {
  242. /* Bogus request ID for traps. */
  243. pdu->pdu_reqid = 1;
  244. }
  245. else if (pdu->pdu_reqid == 0) {
  246. pdu->pdu_reqid = ++snmp_reqid;
  247. }
  248. length = SNMP_MAX_LEN;
  249. packet = malloc(length);
  250. if (packet) {
  251. if (SnmpMsgBuild(session, pdu, packet, &length) == 0) {
  252. rc = NutUdpSendTo(session->sess_sock, session->sess_rem_addr, session->sess_rem_port, packet, length);
  253. }
  254. free(packet);
  255. }
  256. return rc;
  257. }
  258. /*@}*/