From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28063 invoked by alias); 3 Mar 2013 17:41:20 -0000 Received: (qmail 28053 invoked by uid 22791); 3 Mar 2013 17:41:19 -0000 X-SWARE-Spam-Status: No, hits=-0.8 required=5.0 tests=BAYES_00,KHOP_SPAMHAUS_DROP,RCVD_IN_HOSTKARMA_YE,RDNS_NONE,TW_CP X-Spam-Check-By: sourceware.org Received: from Unknown (HELO p02c12o143.mxlogic.net) (208.65.145.76) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 03 Mar 2013 17:40:13 +0000 Received: from unknown [12.218.215.72] (EHLO smtpauth1.linear.com) by p02c12o143.mxlogic.net(mxl_mta-7.0.0-1) with ESMTP id 7fa83315.0.74574.00-353.145883.p02c12o143.mxlogic.net (envelope-from ); Sun, 03 Mar 2013 10:40:13 -0700 (MST) X-MXL-Hash: 51338afd71c0eae5-b902885fc4712c7a9c569e343188daa2e84290da Received: from smtpauth1.linear.com (localhost [127.0.0.1]) by smtpauth1.linear.com (Postfix) with ESMTP id 06385740A8 for ; Sun, 3 Mar 2013 09:40:06 -0800 (PST) Received: from [192.168.0.24] (71-219-193-67.clsp.qwest.net [71.219.193.67]) by smtpauth1.linear.com (Postfix) with ESMTPSA id C2119740A3 for ; Sun, 3 Mar 2013 09:40:05 -0800 (PST) From: Michael Jones Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Message-Id: Date: Sun, 03 Mar 2013 17:41:00 -0000 To: ecos discuss Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) X-AnalysisOut: [v=2.0 cv=OcQv+GvY c=1 sm=1 a=glloKNylpeYNumXQcclYyA==:17 a] X-AnalysisOut: [=D2_GN2MmYMYA:10 a=BLceEmwcHowA:10 a=kj9zAlcOel0A:10 a=MqD] X-AnalysisOut: [INYqSAAAA:8 a=46PJdJM5dksA:10 a=j6A6G630TVdv1nptgTsA:9 a=C] X-AnalysisOut: [juIK1q_8ugA:10 a=Tq9B4dRkXIG2hEn5:21 a=IMFJ_N_rgrjkEjT2:21] X-MAIL-FROM: X-IsSubscribed: yes Mailing-List: contact ecos-discuss-help@ecos.sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: ecos-discuss-owner@ecos.sourceware.org Subject: [ECOS] How to make lwip SNMP work X-SW-Source: 2013-03/txt/msg00003.txt.bz2 It took some effort to figure out how to make SNMP work with LWIP and priva= te MIB. I left a little code in bug 1001789, but here is working code that = does not have memory leaks and seems to work consistently. I don't know for= sure if the callback is required, but it does ensure the trap runs on the = tcpip thread, and from what I have read, this needs to be the case. This works on kinetis K60 with the lwip sequential interface enbled. #ifndef SNMP_H_ #define SNMP_H_ #include "lwip/snmp_structs.h" #include "lwip/tcpip.h" #include #include "pmbus.h" #define SNMP_SEND_TRAP 1 typedef struct snmp_message { uint32_t COMMAND; struct ip_addr ADDRESS; unsigned char TRAP; char *message; } SNMP_MESSAGE, *SNMP_MESSAGE_PTR; extern void snmp_thread_entry(void *arg); extern void send_trap(struct ip_addr addr, unsigned char trap, char *messag= e); #endif /* SNMP_H_ */ #include "private_mib.h" #include "lwip/snmp.h" #include "lwip/snmp_msg.h" #include "lwip/snmp_asn1.h" #include "lwip/snmp_structs.h" #include "lwip/mem.h" #include "snmp.h" extern struct snmp_msg_trap trap_msg; // Private MIB code from python script here void lwip_privmib_init(void) { } void send_trap_callback( void * parameters ) { struct snmp_obj_id eoid; // There should be a way to do this directly. eoid.id[0] =3D enterprises_ids[1]; eoid.len =3D 1; snmp_send_trap(SNMP_GENTRAP_ENTERPRISESPC, &eoid, 1); snmp_varbind_list_free(&trap_msg.outvb); } void snmp_thread_entry(void *arg) { SNMP_MESSAGE_PTR snmp_message; struct snmp_obj_id objid; // There should be a better way to initialize this. objid.len =3D 1; objid.id[0] =3D private_ids[0]; snmp_init(); cyg_mbox_create(&snmp_mailbox_handle, &snmp_mailbox); while (1) { snmp_message =3D (SNMP_MESSAGE_PTR) cyg_mbox_get(snmp_mailbox_handle); if (snmp_message->COMMAND =3D=3D SNMP_SEND_TRAP) { struct ip_addr addr; // Reverse the bytes because snmp_trap_dst_ip_set reverses them again. addr.addr =3D htonl(snmp_message->ADDRESS.addr); snmp_trap_dst_ip_set(0,&addr); snmp_trap_dst_enable(0,snmp_message->TRAP); struct snmp_varbind *vb =3D snmp_varbind_alloc(&objid, SNMP_ASN1_OPAQUE,= strlen(snmp_message->message)+1); if (vb =3D=3D NULL) { diag_printf("SNMP failure to allocate memory\n"); continue; } strcpy (vb->value, snmp_message->message); snmp_varbind_tail_add(&trap_msg.outvb,vb); tcpip_callback(send_trap_callback, NULL); } free(snmp_message->message); free(snmp_message); } } void send_trap(struct ip_addr addr, unsigned char trap, char *message) { SNMP_MESSAGE_PTR snmp_message =3D (SNMP_MESSAGE_PTR) malloc(sizeof(SNMP_ME= SSAGE)); snmp_message->COMMAND =3D SNMP_SEND_TRAP; snmp_message->TRAP =3D trap; snmp_message->ADDRESS =3D addr; snmp_message->message =3D (char*) malloc(strlen(message)+1); strcpy(snmp_message->message, message); cyg_mbox_put(snmp_mailbox_handle, (void *) snmp_message); } Code to start the snmp thread: void start_network(cyg_addrword_t p) { struct ip_addr ipaddr, netmask, gw; struct netif *netif; /* Set network address variables */ IP_GATEWAY(&gw); IP_ADDRESS(&ipaddr); IP_MASK(&netmask); //tcpip_init(NULL, NULL); // Check return for null //netif_result =3D netif_add(&netif, &ipaddr, &netmask, &gw, NULL, init_ip= , tcpip_input); //netif_set_up(&netif); cyg_lwip_sequential_init(); netif =3D netif_find("et0"); netif_set_ipaddr(netif, &ipaddr); netif_set_netmask(netif, &netmask); netif_set_gw(netif, &gw); netif_set_up(netif); cyg_lwip_thread_new("snmp", snmp_thread_entry, NULL, snmp_stack, STACK_SIZE, 7); cyg_thread_exit(); } -- Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss