Contiki-NG
snmp.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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  * An implementation of the Simple Network Management Protocol (RFC 3411-3418)
36  * \author
37  * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
38  */
39 
40 #include "contiki.h"
41 
42 #include "snmp.h"
43 #include "snmp-mib.h"
44 #include "snmp-engine.h"
45 
46 #define LOG_MODULE "SNMP"
47 #define LOG_LEVEL LOG_LEVEL_SNMP
48 
49 /*---------------------------------------------------------------------------*/
50 #define SNMP_SERVER_PORT UIP_HTONS(SNMP_PORT)
51 PROCESS(snmp_process, "SNMP Process");
52 
53 static struct uip_udp_conn *snmp_udp_conn = NULL;
54 
55 /*---------------------------------------------------------------------------*/
56 static void
57 snmp_process_data(void)
58 {
59  static unsigned char packet[SNMP_MAX_PACKET_SIZE];
60  unsigned char *packet_end;
61  static uint32_t packet_len;
62 
63  packet_end = packet + sizeof(packet) - 1;
64  packet_len = 0;
65 
66  LOG_DBG("receiving UDP datagram from [");
67  LOG_DBG_6ADDR(&UIP_IP_BUF->srcipaddr);
68  LOG_DBG_("]:%u", uip_ntohs(UIP_UDP_BUF->srcport));
69  LOG_DBG_(" Length: %u\n", uip_datalen());
70 
71  /*
72  * Handle the request
73  */
74  if((packet_end = snmp_engine(uip_appdata, uip_datalen(), packet_end, &packet_len)) == NULL) {
75  LOG_DBG("Error while handling the request\n");
76  } else {
77  LOG_DBG("Sending response\n");
78  /*
79  * Send the response
80  */
81  uip_udp_packet_sendto(snmp_udp_conn, packet_end, packet_len, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport);
82  }
83 }
84 /*---------------------------------------------------------------------------*/
85 void
87 {
88  snmp_mib_init();
89  process_start(&snmp_process, NULL);
90 }
91 /*---------------------------------------------------------------------------*/
92 
93 /*---------------------------------------------------------------------------*/
94 PROCESS_THREAD(snmp_process, ev, data)
95 {
96  PROCESS_BEGIN();
97 
98  /* new connection with remote host */
99  snmp_udp_conn = udp_new(NULL, 0, NULL);
100  udp_bind(snmp_udp_conn, SNMP_SERVER_PORT);
101  LOG_DBG("Listening on port %u\n", uip_ntohs(snmp_udp_conn->lport));
102 
103  while(1) {
104  PROCESS_YIELD();
105 
106  if(ev == tcpip_event) {
107  if(uip_newdata()) {
108  snmp_process_data();
109  }
110  }
111  } /* while (1) */
112 
113  PROCESS_END();
114 }
115 /*---------------------------------------------------------------------------*/
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#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
uint16_t lport
The local port number in network byte order.
Definition: uip.h:1377
#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
void snmp_mib_init(void)
Initialize the MIB resources list.
Definition: snmp-mib.c:114
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
#define SNMP_MAX_PACKET_SIZE
Default maximum size of the packet in bytes.
Definition: snmp-conf.h:98
void snmp_init()
Initializes the SNMP engine.
Definition: snmp.c:86
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1107
unsigned char * snmp_engine(unsigned char *buff, uint32_t buff_len, unsigned char *out, uint32_t *out_len)
Process the SNMP packet and prepares the response.
Definition: snmp-engine.c:205
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
#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
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
#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
Representation of a uIP UDP connection.
Definition: uip.h:1375