1195972f6Sopenharmony_ci/**
2195972f6Sopenharmony_ci * @file
3195972f6Sopenharmony_ci * Abstract Syntax Notation One (ISO 8824, 8825) codec.
4195972f6Sopenharmony_ci */
5195972f6Sopenharmony_ci
6195972f6Sopenharmony_ci/*
7195972f6Sopenharmony_ci * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
8195972f6Sopenharmony_ci * Copyright (c) 2016 Elias Oenal.
9195972f6Sopenharmony_ci * All rights reserved.
10195972f6Sopenharmony_ci *
11195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
12195972f6Sopenharmony_ci * are permitted provided that the following conditions are met:
13195972f6Sopenharmony_ci *
14195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice,
15195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer.
16195972f6Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice,
17195972f6Sopenharmony_ci *    this list of conditions and the following disclaimer in the documentation
18195972f6Sopenharmony_ci *    and/or other materials provided with the distribution.
19195972f6Sopenharmony_ci * 3. The name of the author may not be used to endorse or promote products
20195972f6Sopenharmony_ci *    derived from this software without specific prior written permission.
21195972f6Sopenharmony_ci *
22195972f6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23195972f6Sopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24195972f6Sopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25195972f6Sopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26195972f6Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27195972f6Sopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28195972f6Sopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29195972f6Sopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30195972f6Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31195972f6Sopenharmony_ci * OF SUCH DAMAGE.
32195972f6Sopenharmony_ci *
33195972f6Sopenharmony_ci * Author: Christiaan Simons <christiaan.simons@axon.tv>
34195972f6Sopenharmony_ci *         Martin Hentschel <info@cl-soft.de>
35195972f6Sopenharmony_ci *         Elias Oenal <lwip@eliasoenal.com>
36195972f6Sopenharmony_ci */
37195972f6Sopenharmony_ci
38195972f6Sopenharmony_ci#ifndef LWIP_HDR_APPS_SNMP_ASN1_H
39195972f6Sopenharmony_ci#define LWIP_HDR_APPS_SNMP_ASN1_H
40195972f6Sopenharmony_ci
41195972f6Sopenharmony_ci#include "lwip/apps/snmp_opts.h"
42195972f6Sopenharmony_ci
43195972f6Sopenharmony_ci#if LWIP_SNMP
44195972f6Sopenharmony_ci
45195972f6Sopenharmony_ci#include "lwip/err.h"
46195972f6Sopenharmony_ci#include "lwip/apps/snmp_core.h"
47195972f6Sopenharmony_ci#include "snmp_pbuf_stream.h"
48195972f6Sopenharmony_ci
49195972f6Sopenharmony_ci#ifdef __cplusplus
50195972f6Sopenharmony_ciextern "C" {
51195972f6Sopenharmony_ci#endif
52195972f6Sopenharmony_ci
53195972f6Sopenharmony_ci#define SNMP_ASN1_TLV_INDEFINITE_LENGTH 0x80
54195972f6Sopenharmony_ci
55195972f6Sopenharmony_ci#define SNMP_ASN1_CLASS_MASK        0xC0
56195972f6Sopenharmony_ci#define SNMP_ASN1_CONTENTTYPE_MASK  0x20
57195972f6Sopenharmony_ci#define SNMP_ASN1_DATATYPE_MASK     0x1F
58195972f6Sopenharmony_ci#define SNMP_ASN1_DATATYPE_EXTENDED 0x1F /* DataType indicating that datatype is encoded in following bytes */
59195972f6Sopenharmony_ci
60195972f6Sopenharmony_ci/* context specific (SNMP) tags (from SNMP spec. RFC1157 and RFC1905) */
61195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_GET_REQ      0
62195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_GET_NEXT_REQ 1
63195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_GET_RESP     2
64195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_SET_REQ      3
65195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_TRAP         4
66195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_GET_BULK_REQ 5
67195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_INFORM_REQ   6
68195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_V2_TRAP      7
69195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_PDU_REPORT       8
70195972f6Sopenharmony_ci
71195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_VARBIND_NO_SUCH_OBJECT      0
72195972f6Sopenharmony_ci#define SNMP_ASN1_CONTEXT_VARBIND_END_OF_MIB_VIEW     2
73195972f6Sopenharmony_ci
74195972f6Sopenharmony_cistruct snmp_asn1_tlv {
75195972f6Sopenharmony_ci  u8_t  type;       /* only U8 because extended types are not specified by SNMP */
76195972f6Sopenharmony_ci  u8_t  type_len;   /* encoded length of 'type' field (normally 1) */
77195972f6Sopenharmony_ci  u8_t  length_len; /* indicates how many bytes are required to encode the 'value_len' field */
78195972f6Sopenharmony_ci  u16_t value_len;  /* encoded length of the value */
79195972f6Sopenharmony_ci};
80195972f6Sopenharmony_ci#define SNMP_ASN1_TLV_HDR_LENGTH(tlv) ((tlv).type_len + (tlv).length_len)
81195972f6Sopenharmony_ci#define SNMP_ASN1_TLV_LENGTH(tlv) ((tlv).type_len + (tlv).length_len + (tlv).value_len)
82195972f6Sopenharmony_ci#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);
83195972f6Sopenharmony_ci
84195972f6Sopenharmony_cierr_t snmp_asn1_dec_tlv(struct snmp_pbuf_stream *pbuf_stream, struct snmp_asn1_tlv *tlv);
85195972f6Sopenharmony_cierr_t snmp_asn1_dec_u32t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u32_t *value);
86195972f6Sopenharmony_cierr_t snmp_asn1_dec_s32t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, s32_t *value);
87195972f6Sopenharmony_cierr_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);
88195972f6Sopenharmony_cierr_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);
89195972f6Sopenharmony_ci
90195972f6Sopenharmony_cierr_t snmp_ans1_enc_tlv(struct snmp_pbuf_stream *pbuf_stream, struct snmp_asn1_tlv *tlv);
91195972f6Sopenharmony_ci
92195972f6Sopenharmony_civoid snmp_asn1_enc_length_cnt(u16_t length, u8_t *octets_needed);
93195972f6Sopenharmony_civoid snmp_asn1_enc_u32t_cnt(u32_t value, u16_t *octets_needed);
94195972f6Sopenharmony_civoid snmp_asn1_enc_s32t_cnt(s32_t value, u16_t *octets_needed);
95195972f6Sopenharmony_civoid snmp_asn1_enc_oid_cnt(const u32_t *oid, u16_t oid_len, u16_t *octets_needed);
96195972f6Sopenharmony_cierr_t snmp_asn1_enc_oid(struct snmp_pbuf_stream *pbuf_stream, const u32_t *oid, u16_t oid_len);
97195972f6Sopenharmony_cierr_t snmp_asn1_enc_s32t(struct snmp_pbuf_stream *pbuf_stream, u16_t octets_needed, s32_t value);
98195972f6Sopenharmony_cierr_t snmp_asn1_enc_u32t(struct snmp_pbuf_stream *pbuf_stream, u16_t octets_needed, u32_t value);
99195972f6Sopenharmony_cierr_t snmp_asn1_enc_raw(struct snmp_pbuf_stream *pbuf_stream, const u8_t *raw, u16_t raw_len);
100195972f6Sopenharmony_ci
101195972f6Sopenharmony_ci#if LWIP_HAVE_INT64
102195972f6Sopenharmony_cierr_t snmp_asn1_dec_u64t(struct snmp_pbuf_stream *pbuf_stream, u16_t len, u64_t *value);
103195972f6Sopenharmony_civoid snmp_asn1_enc_u64t_cnt(u64_t value, u16_t *octets_needed);
104195972f6Sopenharmony_cierr_t snmp_asn1_enc_u64t(struct snmp_pbuf_stream *pbuf_stream, u16_t octets_needed, u64_t value);
105195972f6Sopenharmony_ci#endif
106195972f6Sopenharmony_ci
107195972f6Sopenharmony_ci#ifdef __cplusplus
108195972f6Sopenharmony_ci}
109195972f6Sopenharmony_ci#endif
110195972f6Sopenharmony_ci
111195972f6Sopenharmony_ci#endif /* LWIP_SNMP */
112195972f6Sopenharmony_ci
113195972f6Sopenharmony_ci#endif /* LWIP_HDR_APPS_SNMP_ASN1_H */
114