#include "snmp_utils.h"
+inline void
+snmp_pdu_context(struct snmp_pdu *pdu, sock *sk)
+{
+ pdu->buffer = sk->tpos;
+ pdu->size = sk->tbuf + sk->tbsize - sk->tpos;
+ pdu->error = AGENTX_RES_NO_ERROR;
+ pdu->index = 0;
+}
+
+inline void
+snmp_session(const struct snmp_proto *p, struct agentx_header *h)
+{
+ h->session_id = p->session_id;
+ h->transaction_id = p->transaction_id;
+ h->packet_id = p->packet_id;
+}
+
+inline int
+snmp_has_context(const struct agentx_header *h)
+{
+ return h->flags & AGENTX_NON_DEFAULT_CONTEXT;
+}
+
+inline byte *
+snmp_add_context(struct snmp_proto *p, struct agentx_header *h, uint contid)
+{
+ h->flags = h->flags | AGENTX_NON_DEFAULT_CONTEXT;
+ // TODO append the context after the header
+ (void)p;
+ (void)contid;
+ return (void *)h + AGENTX_HEADER_SIZE;
+}
+
+inline void *
+snmp_varbind_data(const struct agentx_varbind *vb)
+{
+ uint name_size = snmp_oid_size(&vb->name);
+ return (void *)&vb->name + name_size;
+}
+
/**
* snmp_is_oid_empty - check if oid is null-valued
* @oid: object identifier to check
if (s >= 0)
return hdr_size + (uint) s;
- void *data = ((void *) vb) + hdr_size;
+ void *data = snmp_varbind_data(vb);
if (vb->type == AGENTX_OBJECT_ID)
return hdr_size + snmp_oid_size((struct oid *) data);
return NULL;
vb->type = type;
- u32 *data = SNMP_VB_DATA(vb);
+ u32 *data = snmp_varbind_data(vb);
*data = val;
return (byte *)(data + 1);
}
return NULL;
vb->type = AGENTX_IP_ADDRESS;
- return snmp_put_ip4(SNMP_VB_DATA(vb), addr);
+ return snmp_put_ip4(snmp_varbind_data(vb), addr);
}
inline byte *
return NULL;
vb->type = AGENTX_OCTET_STRING;
- //die("snmp_varbind_nstr() %p.data = %p", vb, SNMP_VB_DATA(vb));
- return snmp_put_nstr(SNMP_VB_DATA(vb), str, len);
+ return snmp_put_nstr(snmp_varbind_data(vb), str, len);
}
inline enum agentx_type
uint snmp_varbind_header_size(struct agentx_varbind *vb);
uint snmp_varbind_size(struct agentx_varbind *vb, int byte_ord);
int snmp_test_varbind(const struct agentx_varbind *vb);
+void snmp_session(const struct snmp_proto *p, struct agentx_header *h);
+int snmp_has_context(const struct agentx_header *h);
+void snmp_pdu_context(struct snmp_pdu *pdu, sock *sk);
void snmp_oid_copy(struct oid *dest, const struct oid *src);
struct oid *snmp_oid_duplicate(pool *pool, const struct oid *oid);
struct oid *snmp_oid_blank(struct snmp_proto *p);
+void *snmp_varbind_data(const struct agentx_varbind *vb);
struct agentx_varbind *snmp_create_varbind(byte* buf, struct oid *oid);
byte *snmp_fix_varbind(struct agentx_varbind *vb, struct oid *new);
snmp_simple_response(struct snmp_proto *p, enum agentx_response_errs error, u16 index)
{
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
if (c.size < sizeof(struct agentx_response))
snmp_manage_tbuf(p, &c);
const struct snmp_config *cf = SKIP_BACK(struct snmp_config, cf, p->p.cf);
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
#define TIMEOUT_SIZE 4 /* 1B timeout, 3B zero padding */
if (c.size < AGENTX_HEADER_SIZE + TIMEOUT_SIZE + snmp_oid_size(oid) +
{
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
#define UPTIME_SIZE \
(6 * sizeof(u32)) /* sizeof( { u32 vb_type, u32 oid_hdr, u32 ids[4] } )*/
for (uint i = 0; i < trap0.n_subid; i++)
STORE_U32(trap_vb->name.ids[i], trap0_ids[i]);
trap_vb->type = AGENTX_OBJECT_ID;
- snmp_put_oid(SNMP_VB_DATA(trap_vb), oid);
+ snmp_put_oid(snmp_varbind_data(trap_vb), oid);
ADVANCE(c.buffer, c.size, snmp_varbind_size(trap_vb, c.byte_ord));
memcpy(c.buffer, data, size);
byte *buf, *pkt;
buf = pkt = sk->tbuf;
uint size = sk->tbsize;
- struct snmp_pdu = SNMP_PDU_CONTEXT(p->sock);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, p->sock);
if (size > AGENTX_HEADER_SIZE + 0) // TODO additional size
static void
un_register_pdu(struct snmp_proto *p, struct oid *oid, uint bound, uint index, enum agentx_pdu_types type, u8 is_instance, uint UNUSED contid)
{
+ /* used for agentx-Register-PDU and agentx-Unregister-PDU */
const struct snmp_config *cf = SKIP_BACK(struct snmp_config, cf, p->p.cf);
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
/* conditional +4 for upper-bound (optinal field) */
uint sz = AGENTX_HEADER_SIZE + snmp_oid_size(oid) + ((bound > 1) ? 4 : 0);
close_pdu(struct snmp_proto *p, enum agentx_close_reasons reason)
{
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
#define REASON_SIZE 4
if (c.size < AGENTX_HEADER_SIZE + REASON_SIZE)
static uint
parse_test_set_pdu(struct snmp_proto *p, byte * const pkt_start, uint size)
{
+ TRACE(D_PACKETS, "SNMP received agentx-TestSet-PDU");
byte *pkt = pkt_start; /* pointer to agentx-TestSet-PDU in RX-buffer */
uint s; /* final packat size */
struct agentx_response *res; /* pointer to reponse in TX-buffer */
struct agentx_header *h = (void *) pkt;
ADVANCE(pkt, size, AGENTX_HEADER_SIZE);
- uint pkt_size = LOAD_U32(h->payload, h->flags & AGENTX_NETWORK_BYTE_ORDER);
+ uint pkt_size = LOAD_U32(h->payload);
if (pkt_size < size)
return 0;
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
- c.byte_ord = 0; /* use little-endian */
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
if (c.size < AGENTX_HEADER_SIZE)
snmp_manage_tbuf(p, &c);
// use varbind_list_size()??
}
- struct snmp_pdu c = SNMP_PDU_CONTEXT(p->sock);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, p->sock);
if (c.size < sizeof(struct agentx_response))
snmp_manage_tbuf(p, &c);
uint pkt_size = LOAD_U32(h->payload, h->flags & AGENTX_NETWORK_BYTE_ORDER);
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
// TODO better handling of endianness
c.byte_ord = 0; /* use little-endian */
snmp_ping(struct snmp_proto *p)
{
sock *sk = p->sock;
- struct snmp_pdu c = SNMP_PDU_CONTEXT(sk);
+ struct snmp_pdu c;
+ snmp_pdu_context(&c, sk);
if (c.size < AGENTX_HEADER_SIZE)
snmp_manage_tbuf(p, &c);
str[length] = '\0'; /* set term. char */ \
buf += 4 + snmp_str_size_from_len(length); })
-#define SNMP_HAS_CONTEXT(hdr) \
- hdr->flags |= AGENTX_NON_DEFAULT_CONTEXT
-
#define SNMP_PUT_OID(buf, size, oid, byte_ord) \
({ \
struct agentx_varbind *vb = (void *) buf; \
#define SNMP_FILL_VARBIND(vb, oid, byte_ord) \
snmp_oid_copy(&(vb)->name, (oid), (byte_ord)), snmp_oid_size((oid))
-#define SNMP_VB_DATA(varbind) \
- (((void *)(varbind)) + snmp_varbind_header_size(varbind))
-
-#define SNMP_PDU_CONTEXT(sk) { \
- .buffer = sk->tpos, \
- .size = sk->tbuf + sk->tbsize - sk->tpos, \
- .error = AGENTX_RES_NO_ERROR, \
- .index = 0, \
- }
-
struct agentx_header {
u8 version;
u8 type;
u16 pad;
/* oid part */
struct oid name;
- byte data[];
+ /* AgentX variable binding data optionaly here */
};
/* this does not work */