Nut/OS  4.10.3
API Reference
snmp_pdu.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 <compiler.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038 
00039 #include <pro/snmp_pdu.h>
00040 
00045 
00053 SNMP_PDU *SnmpPduCreate(int cmd, CONST OID * name, size_t nlen)
00054 {
00055     SNMP_PDU *pdu = calloc(1, sizeof(SNMP_PDU));
00056 
00057     if (pdu) {
00058         /* Set the PDU type. */
00059         pdu->pdu_cmd = cmd;
00060         /* Set the device identifier. */
00061         pdu->pdu_enterprise = malloc(nlen * sizeof(OID));
00062         if (pdu->pdu_enterprise) {
00063             memcpy(pdu->pdu_enterprise, name, nlen * sizeof(OID));
00064             pdu->pdu_enterprise_length = nlen;
00065         } else {
00066             free(pdu);
00067             pdu = NULL;
00068         }
00069     }
00070     return pdu;
00071 }
00072 
00083 int SnmpPduAddVariable(SNMP_PDU * pdu, OID * name, size_t nlen, uint8_t type, uint8_t * value, size_t vlen)
00084 {
00085     SNMP_VARLIST *var = calloc(1, sizeof(SNMP_VARLIST));
00086 
00087     if (var == NULL) {
00088         return -1;
00089     }
00090     var->var_name = malloc(nlen * sizeof(OID));
00091     if (var->var_name == NULL) {
00092         free(var);
00093         return -1;
00094     }
00095     memcpy(var->var_name, name, nlen * sizeof(OID));
00096     var->var_nlen = nlen;
00097     var->var_type = type;
00098     switch (type) {
00099     case ASN_COUNTER:
00100     case ASN_GAUGE:
00101     case ASN_INTEGER:
00102     case ASN_TIMETICKS:
00103     case ASN_UINTEGER:
00104         if (vlen == 0) {
00105             vlen = sizeof(long);
00106         }
00107         var->var_vptr = (uint8_t *) & var->var_val;
00108         memcpy(var->var_vptr, value, vlen);
00109         var->var_vlen = sizeof(long);
00110         break;
00111     default:
00112         if (vlen) {
00113             var->var_vptr = malloc(vlen);
00114             if (var->var_vptr) {
00115                 memcpy(var->var_vptr, value, vlen);
00116                 var->var_vlen = vlen;
00117             }
00118         }
00119         break;
00120     }
00121     if (pdu->pdu_variables) {
00122         SNMP_VARLIST *vp;
00123 
00124         for (vp = pdu->pdu_variables; vp->var_next; vp = vp->var_next);
00125         vp->var_next = var;
00126     } else {
00127         pdu->pdu_variables = var;
00128     }
00129     return 0;
00130 }
00131 
00137 void SnmpPduDestroy(SNMP_PDU * pdu)
00138 {
00139     SNMP_VARLIST *vp;
00140     SNMP_VARLIST *ovp;
00141 
00142     vp = pdu->pdu_variables;
00143     while (vp) {
00144         if (vp->var_name) {
00145             free(vp->var_name);
00146         }
00147         if (vp->var_vptr && vp->var_vptr != (uint8_t *) & vp->var_val) {
00148             free(vp->var_vptr);
00149         }
00150         ovp = vp;
00151         vp = vp->var_next;
00152         free(ovp);
00153     }
00154     if (pdu->pdu_enterprise) {
00155         free(pdu->pdu_enterprise);
00156     }
00157     free(pdu);
00158 }
00159