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)
52 PROCESS(snmp_process, "SNMP Process");
53 
54 static struct uip_udp_conn *snmp_udp_conn;
55 /*---------------------------------------------------------------------------*/
56 void
58 {
59  snmp_mib_init();
60  process_start(&snmp_process, NULL);
61 }
62 /*---------------------------------------------------------------------------*/
63 
64 /*---------------------------------------------------------------------------*/
65 PROCESS_THREAD(snmp_process, ev, data)
66 {
67  PROCESS_BEGIN();
68 
69  snmp_packet_t snmp_packet;
70 
71  /* new connection with remote host */
72  snmp_udp_conn = udp_new(NULL, 0, NULL);
73  udp_bind(snmp_udp_conn, SNMP_SERVER_PORT);
74  LOG_DBG("Listening on port %u\n", uip_ntohs(snmp_udp_conn->lport));
75 
76  while(1) {
77  PROCESS_YIELD();
78 
79  if(ev != tcpip_event) {
80  continue;
81  }
82 
83  if(!uip_newdata()) {
84  continue;
85  }
86 
87  LOG_DBG("receiving UDP datagram from [");
88  LOG_DBG_6ADDR(&UIP_IP_BUF->srcipaddr);
89  LOG_DBG_("]:%u", uip_ntohs(UIP_UDP_BUF->srcport));
90  LOG_DBG_(" Length: %u\n", uip_datalen());
91 
92  /* Setup SNMP packet */
93  snmp_packet.in = (uint8_t *)uip_appdata;
94  snmp_packet.used = uip_datalen();
95 
96  snmp_packet.out = (uint8_t *)(uip_appdata + UIP_BUFSIZE - UIP_IPUDPH_LEN);
97  snmp_packet.max = UIP_BUFSIZE - UIP_IPUDPH_LEN;
98 
99  /* Handle the request */
100  if(!snmp_engine(&snmp_packet)) {
101  LOG_DBG("Error while handling the request\n");
102  continue;
103  }
104 
105  LOG_DBG("Sending response\n");
106  /* Send the response */
107  uip_udp_packet_sendto(snmp_udp_conn, snmp_packet.out, snmp_packet.used, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport);
108  } /* while (1) */
109 
110  PROCESS_END();
111 }
112 /*---------------------------------------------------------------------------*/
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
SNMP Implementation of the process
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
The packet struct.
Definition: snmp.h:206
#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
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:62
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:134
void snmp_init()
Initializes the SNMP engine.
Definition: snmp.c:57
uint16_t lport
The local port number in network byte order.
Definition: uip.h:1377
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
#define uip_newdata()
Is new incoming data available?
Definition: uip.h:726
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
uint16_t used
The number os bytes used.
Definition: snmp.h:211
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
void snmp_mib_init(void)
Initialize the MIB resources list.
Definition: snmp-mib.c:164
uint16_t max
The maximum number of bytes.
Definition: snmp.h:216
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
SNMP Implementation of the MIB
#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
SNMP Implementation of the protocol engine
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
Definition: uip.h:639
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int snmp_engine(snmp_packet_t *snmp_packet)
Process the SNMP packet and prepares the response.
Definition: snmp-engine.c:219
Representation of a uIP UDP connection.
Definition: uip.h:1375