1 /**
2  * @file
3  * Abstract Syntax Notation One (ISO 8824, 8825) codec.
4  */
5 
6 /*
7  * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
8  * Copyright (c) 2016 Elias Oenal.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * Author: Christiaan Simons <christiaan.simons@axon.tv>
34  *         Martin Hentschel <info@cl-soft.de>
35  *         Elias Oenal <lwip@eliasoenal.com>
36  */
37 
38 #ifndef LWIP_HDR_APPS_SNMP_ASN1_H
39 #define LWIP_HDR_APPS_SNMP_ASN1_H
40 
41 #include "lwip/apps/snmp_opts.h"
42 
43 #if LWIP_SNMP
44 
45 #include "lwip/err.h"
46 #include "lwip/apps/snmp_core.h"
47 #include "snmp_pbuf_stream.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #define SNMP_ASN1_TLV_INDEFINITE_LENGTH 0x80
54 
55 #define SNMP_ASN1_CLASS_MASK        0xC0
56 #define SNMP_ASN1_CONTENTTYPE_MASK  0x20
57 #define SNMP_ASN1_DATATYPE_MASK     0x1F
58 #define SNMP_ASN1_DATATYPE_EXTENDED 0x1F /* DataType indicating that datatype is encoded in following bytes */
59 
60 /* context specific (SNMP) tags (from SNMP spec. RFC1157) */
61 #define SNMP_ASN1_CONTEXT_PDU_GET_REQ      0
62 #define SNMP_ASN1_CONTEXT_PDU_GET_NEXT_REQ 1
63 #define SNMP_ASN1_CONTEXT_PDU_GET_RESP     2
64 #define SNMP_ASN1_CONTEXT_PDU_SET_REQ      3
65 #define SNMP_ASN1_CONTEXT_PDU_TRAP         4
66 #define SNMP_ASN1_CONTEXT_PDU_GET_BULK_REQ 5
67 
68 #define SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_OBJECT      0
69 #define SNMP_ASN1_CONTEXT_VARBIND_END_OF_MIB_VIEW     2
70 
71 struct snmp_asn1_tlv
72 {
73   u8_t  type;       /* only U8 because extended types are not specified by SNMP */
74   u8_t  type_len;   /* encoded length of 'type' field (normally 1) */
75   u8_t  length_len; /* indicates how many bytes are required to encode the 'value_len' field */
76   u16_t value_len;  /* encoded length of the value */
77 };
78 #define SNMP_ASN1_TLV_HDR_LENGTH(tlv) ((tlv).type_len + (tlv).length_len)
79 #define SNMP_ASN1_TLV_LENGTH(tlv) ((tlv).type_len + (tlv).length_len + (tlv).value_len)
80 #define SNMP_ASN1_SET_TLV_PARAMS(tlv, type_, length_len_, value_len_) do { (tlv).type = (type_); (tlv).type_len = 0; (tlv).length_len = (length_len_); (tlv).value_len = (value_len_); } while (0);
81 
82 err_t snmp_asn1_dec_tlv(struct snmp_pbuf_stream* pbuf_stream, struct snmp_asn1_tlv* tlv);
83 err_t snmp_asn1_dec_u32t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t *value);
84 err_t snmp_asn1_dec_u64t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t *value);
85 err_t snmp_asn1_dec_s32t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, s32_t *value);
86 err_t snmp_asn1_dec_oid(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t* oid, u8_t* oid_len, u8_t oid_max_len);
87 err_t snmp_asn1_dec_raw(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u8_t *buf, u16_t* buf_len, u16_t buf_max_len);
88 
89 err_t snmp_ans1_enc_tlv(struct snmp_pbuf_stream* pbuf_stream, struct snmp_asn1_tlv* tlv);
90 
91 void snmp_asn1_enc_length_cnt(u16_t length, u8_t *octets_needed);
92 void snmp_asn1_enc_u32t_cnt(u32_t value, u16_t *octets_needed);
93 void snmp_asn1_enc_u64t_cnt(const u32_t *value, u16_t *octets_needed);
94 void snmp_asn1_enc_s32t_cnt(s32_t value, u16_t *octets_needed);
95 void snmp_asn1_enc_oid_cnt(const u32_t *oid, u16_t oid_len, u16_t *octets_needed);
96 err_t snmp_asn1_enc_oid(struct snmp_pbuf_stream* pbuf_stream, const u32_t *oid, u16_t oid_len);
97 err_t snmp_asn1_enc_s32t(struct snmp_pbuf_stream* pbuf_stream, u16_t octets_needed, s32_t value);
98 err_t snmp_asn1_enc_u32t(struct snmp_pbuf_stream* pbuf_stream, u16_t octets_needed, u32_t value);
99 err_t snmp_asn1_enc_u64t(struct snmp_pbuf_stream* pbuf_stream, u16_t octets_needed, const u32_t* value);
100 err_t snmp_asn1_enc_raw(struct snmp_pbuf_stream* pbuf_stream, const u8_t *raw, u16_t raw_len);
101 
102 #ifdef __cplusplus
103 }
104 #endif
105 
106 #endif /* LWIP_SNMP */
107 
108 #endif /* LWIP_HDR_APPS_SNMP_ASN1_H */
109