Contiki-NG
snmp.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*---------------------------------------------------------------------------*/
32
33/**
34 * \file
35 * SNMP Implementation of the process
36 * \author
37 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
38 */
39
40#include "contiki.h"
41#include "contiki-net.h"
42
43#include "snmp.h"
44#include "snmp-mib.h"
45#include "snmp-engine.h"
46
47#define LOG_MODULE "SNMP"
48#define LOG_LEVEL LOG_LEVEL_SNMP
49
50/*---------------------------------------------------------------------------*/
51#define SNMP_SERVER_PORT UIP_HTONS(SNMP_PORT)
52PROCESS(snmp_process, "SNMP Process");
53
54static struct uip_udp_conn *snmp_udp_conn;
55/*---------------------------------------------------------------------------*/
56void
58{
60 process_start(&snmp_process, NULL);
61}
62/*---------------------------------------------------------------------------*/
63
64/*---------------------------------------------------------------------------*/
65PROCESS_THREAD(snmp_process, ev, data)
66{
68
69 snmp_packet_t snmp_packet;
70
71 /* new connection with remote host */
72 snmp_udp_conn = udp_new(NULL, 0, NULL);
73 if(snmp_udp_conn == NULL) {
74 LOG_ERR("No UDP connection available, exiting the process!\n");
76 }
77
78 udp_bind(snmp_udp_conn, SNMP_SERVER_PORT);
79 LOG_DBG("Listening on port %u\n", uip_ntohs(snmp_udp_conn->lport));
80
81 while(1) {
83
84 if(ev != tcpip_event) {
85 continue;
86 }
87
88 if(!uip_newdata()) {
89 continue;
90 }
91
92 LOG_DBG("receiving UDP datagram from [");
93 LOG_DBG_6ADDR(&UIP_IP_BUF->srcipaddr);
94 LOG_DBG_("]:%u", uip_ntohs(UIP_UDP_BUF->srcport));
95 LOG_DBG_(" Length: %u\n", uip_datalen());
96
97 /* Setup SNMP packet */
98 snmp_packet.in = (uint8_t *)uip_appdata;
99 snmp_packet.used = uip_datalen();
100
101 snmp_packet.out = (uint8_t *)(uip_appdata + UIP_BUFSIZE - UIP_IPUDPH_LEN);
102 snmp_packet.max = UIP_BUFSIZE - UIP_IPUDPH_LEN;
103
104 /* Handle the request */
105 if(!snmp_engine(&snmp_packet)) {
106 LOG_DBG("Error while handling the request\n");
107 continue;
108 }
109
110 LOG_DBG("Sending response\n");
111 /* Send the response */
112 uip_udp_packet_sendto(snmp_udp_conn, snmp_packet.out, snmp_packet.used, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport);
113 } /* while (1) */
114
115 PROCESS_END();
116}
117/*---------------------------------------------------------------------------*/
void snmp_init()
Initializes the SNMP engine.
Definition: snmp.c:57
int snmp_engine(snmp_packet_t *snmp_packet)
Process the SNMP packet and prepares the response.
Definition: snmp-engine.c:220
void snmp_mib_init(void)
Initialize the MIB resources list.
Definition: snmp-mib.c:164
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1154
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:62
struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
Definition: tcpip.c:261
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
Definition: tcpip.h:261
void * uip_appdata
Pointer to the application data in the packet buffer.
Definition: uip6.c:148
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer.
Definition: uip.h:593
#define uip_newdata()
Is new incoming data available?
Definition: uip.h:680
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:93
SNMP Implementation of the protocol engine.
SNMP Implementation of the MIB.
SNMP Implementation of the process.
The packet struct.
Definition: snmp.h:206
uint8_t * in
The pointer used for the incoming packet.
Definition: snmp.h:221
uint8_t * out
The pointer used for the outgoing packet.
Definition: snmp.h:226
uint16_t max
The maximum number of bytes.
Definition: snmp.h:216
uint16_t used
The number of bytes used.
Definition: snmp.h:211
Representation of a uIP UDP connection.
Definition: uip.h:1309
uint16_t lport
The local port number in network byte order.
Definition: uip.h:1311