Nut/OS  4.10.3
API Reference
snmp_session.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009 by egnite GmbH
00003  * Copyright 1998-2007 by egnite Software GmbH
00004  * Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00035 #include <sys/confnet.h>
00036 
00037 #include <stdlib.h>
00038 #include <string.h>
00039 
00040 #include <pro/snmp_auth.h>
00041 #include <pro/snmp_session.h>
00042 
00047 
00048 static uint32_t snmp_reqid;
00049 static uint8_t temp_buffer[SNMP_MAX_LEN];
00050 
00061 SNMP_SESSION *SnmpSessionOpen(uint32_t ip, uint16_t port, uint8_t * id, size_t idlen)
00062 {
00063     SNMP_SESSION *session = calloc(1, sizeof(SNMP_SESSION));
00064 
00065     if (session) {
00066         /* Create UDP socket at random local port. */
00067         session->sess_sock = NutUdpCreateSocket(0);
00068         if (session->sess_sock == NULL) {
00069             /* Failed to create socket. */
00070             free(session);
00071             session = NULL;
00072         } else {
00073             /* Set remote address/port. */
00074             session->sess_rem_addr = ip;
00075             session->sess_rem_port = port;
00076             /* Set session identifier. */
00077             memcpy(session->sess_id, id, idlen);
00078             session->sess_id_len = idlen;
00079         }
00080     }
00081     return session;
00082 }
00083 
00090 void SnmpSessionClose(SNMP_SESSION * session)
00091 {
00092     if (session->sess_sock) {
00093         NutUdpDestroySocket(session->sess_sock);
00094     }
00095     free(session);
00096 }
00097 
00112 static int SnmpMsgBuild(SNMP_SESSION * session, SNMP_PDU * pdu, uint8_t * packet, size_t * psize)
00113 {
00114     uint8_t *cp;
00115     SNMP_VARLIST *vp;
00116     size_t length;
00117     int total;
00118     long lval;
00119 
00120     /*
00121      * Encode the list of variable bindings.
00122      */
00123     length = *psize;
00124     cp = packet;
00125     for (vp = pdu->pdu_variables; vp; vp = vp->var_next) {
00126         cp = SnmpVarBuild(cp, &length, vp->var_name, vp->var_nlen, vp->var_type, vp->var_vptr, vp->var_vlen);
00127         if (cp == NULL) {
00128             return -1;
00129         }
00130     }
00131     total = cp - packet;
00132 
00133     /*
00134      * Encode the sequence header of the variable bindings.
00135      */
00136     length = SNMP_MAX_LEN;
00137     cp = AsnHeaderBuild(temp_buffer, &length, ASN_SEQUENCE | ASN_CONSTRUCTOR, total);
00138     if (cp == NULL) {
00139         return -1;
00140     }
00141 
00142     /*
00143      * Append the variable bindings to its header.
00144      */
00145     memcpy(cp, packet, total);
00146     total += cp - temp_buffer;
00147 
00148     /*
00149      * Encode the PDU.
00150      *
00151      * Note the different format used for traps.
00152      */
00153     length = *psize;
00154     if (pdu->pdu_cmd == SNMP_MSG_TRAP) {
00155         /* Enterprise. */
00156         cp = AsnOidBuild(packet, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID, pdu->pdu_enterprise, pdu->pdu_enterprise_length);
00157         if (cp == NULL) {
00158             return -1;
00159         }
00160         /* Agent address. */
00161         cp = AsnOctetStringBuild(cp, &length, ASN_IPADDRESS, (u_char *) & confnet.cdn_ip_addr, sizeof(confnet.cdn_ip_addr));
00162         if (cp == NULL) {
00163             return -1;
00164         }
00165         /* Generic trap. */
00166         lval = (long)pdu->pdu_trap_type;
00167         cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, &lval);
00168         if (cp == NULL) {
00169             return -1;
00170         }
00171         /* Specific trap. */
00172         lval = (long)pdu->pdu_specific_type;
00173         cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, &lval);
00174         if (cp == NULL) {
00175             return -1;
00176         }
00177         /* Time stamp. */
00178         cp = AsnIntegerBuild(cp, &length, ASN_TIMETICKS, (long *) &pdu->pdu_time);
00179         if (cp == NULL) {
00180             return -1;
00181         }
00182     } else {
00183         /* Request ID. */
00184         cp = AsnIntegerBuild(packet, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_reqid);
00185         if (cp == NULL) {
00186             return -1;
00187         }
00188         /* Error status. */
00189         cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_errstat);
00190         if (cp == NULL) {
00191             return -1;
00192         }
00193         /* Error index. */
00194         cp = AsnIntegerBuild(cp, &length, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER, (long *) &pdu->pdu_errindex);
00195         if (cp == NULL) {
00196             return -1;
00197         }
00198     }
00199 
00200     /*
00201      * Append the variable bindings.
00202      */
00203     if (length < total) {
00204         return -1;
00205     }
00206     memcpy(cp, temp_buffer, total);
00207     total += cp - packet;
00208 
00209     /*
00210      * Encode the PDU header containing its type.
00211      */
00212     length = SNMP_MAX_LEN;
00213     cp = AsnHeaderBuild(temp_buffer, &length, (u_char) pdu->pdu_cmd, total);
00214     if (cp == NULL || length < total) {
00215         return -1;
00216     }
00217 
00218     /*
00219      * Append the PDU body.
00220      */
00221     memcpy(cp, packet, total);
00222     total += cp - temp_buffer;
00223 
00224     /*
00225      * Encode the message header with version and community string.
00226      */
00227     length = *psize;
00228     cp = SnmpAuthBuild(session, packet, &length, total);
00229     if (cp == NULL) {
00230         return -1;
00231     }
00232 
00233     /*
00234      * Append the PDU.
00235      */
00236     if ((*psize - (cp - packet)) < total) {
00237         return -1;
00238     }
00239     memcpy(cp, temp_buffer, total);
00240     total += cp - packet;
00241     *psize = total;
00242 
00243     return 0;
00244 }
00245 
00256 int SnmpSessionSendPdu(SNMP_SESSION * session, SNMP_PDU * pdu)
00257 {
00258     int rc = -1;
00259     uint8_t *packet;
00260     size_t length;
00261 
00262     if (pdu->pdu_cmd == SNMP_MSG_TRAP) {
00263         /* Bogus request ID for traps. */
00264         pdu->pdu_reqid = 1;
00265     }
00266     else if (pdu->pdu_reqid == 0) {
00267         pdu->pdu_reqid = ++snmp_reqid;
00268     }
00269 
00270     length = SNMP_MAX_LEN;
00271     packet = malloc(length);
00272     if (packet) {
00273         if (SnmpMsgBuild(session, pdu, packet, &length) == 0) {
00274             rc = NutUdpSendTo(session->sess_sock, session->sess_rem_addr, session->sess_rem_port, packet, length);
00275         }
00276         free(packet);
00277     }
00278     return rc;
00279 }
00280