📄 att_opr.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/att_opr.c,v 1.4 2003/01/15 14:04:54 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1991-1997 Epilogue Technology Corporation. * Copyright 1998-1999 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* Attache specific glue routines for snmptalk. *//* * $Log: att_opr.c,v $ * Revision 1.4 2003/01/15 14:04:54 josh * directory structure shifting * * Revision 1.3 2002/03/11 19:17:07 josh * snmptalk's destination address is now stored and passed around * as an ipaddr_t. This required changes to a lot of internal function * calls that used to expect an inaddr_t to be passed around. Also, * snmptalk is now capable of using IPv6 sockets * * Revision 1.2 2001/11/08 16:47:21 tneale * Updated for newset file layout * * Revision 1.1.1.1 2001/11/05 17:49:11 tneale * Tornado shuffle * * Revision 7.11 2001/01/19 22:24:42 paul * Update copyright. * * Revision 7.10 2000/03/17 00:14:32 meister * Update copyright message * * Revision 7.9 1999/11/16 18:07:50 josh * zero out the SNMPADDR_T, just to prevent problems * * Revision 7.8 1999/11/05 22:24:12 paul * Eliminate dependence on ancient backward-compatibility code. * * Revision 7.7 1998/11/25 03:38:19 sra * "timeout" => "snmp_timeout" to fix OpEN symbol conflict. * * Revision 7.6 1998/09/18 19:55:55 meister * timer call renaming tm_ --> etc_tm_ * * Revision 7.5 1998/06/23 20:56:42 sar * Moved parse.h to snark/h/parse.h * * Revision 7.4 1998/06/03 21:50:34 sar * Moved nvutils and nvviews to snark/lib so we can have one copy shared * amongst the demos * Moved the common string functions from stdf to common/lib and created * a series of macros for this code to use * Moved strdup to snark/lib and renamed it to etc_strdup to avoid * needing one from the system libraries * * Revision 7.3 1998/02/25 15:21:56 sra * Finish moving types.h, bug.h, and bugdef.h to common/h/. * * Revision 7.2 1998/02/25 04:57:52 sra * Update copyrights. * * Revision 7.1 1997/10/30 04:17:58 sar * Split attache.c into attache.c for utilities and att_opr.c for * the snmp send and receive functions. * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/install.h>#include <snmptalk.h>#include <stdio.h>#include <wrn/wm/attache/config.h>#include <wrn/wm/common/types.h>#include <wrn/wm/attache/mib.h>#include <wrn/wm/attache/timer.h>#include <wrn/wm/attache/packet.h>#include <wrn/wm/attache/net.h>#include <wrn/wm/attache/route.h>#include <wrn/wm/attache/ip.h>#include <wrn/wm/attache/arp.h>#include <wrn/wm/attache/udp.h>#include <wrn/wm/attache/dns.h>#include <wrn/wm/attache/glue.h>#include <wrn/wm/attache/tcp.h>#include <wrn/wm/demo/tasks.h>#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/snmpconf.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/attache/snmpvars.h>#include "md.h"#include "snmpint.h"/* * Code to implement snmp_send_rec(). *//* status kept here about operations in progress */struct operation { SNMP_PKT_T *pkt; struct timer timeout; int count; ipaddr_t dest; bits16_t port; struct sty *sty; void (*complete)(struct sty *, SNMP_PKT_T *); void (*error)(struct sty *, char *);};/* general cleanup routine */static void opr_cleanup(struct operation *opr){ etc_tm_cancel(&opr->timeout); udp_ignore(opr->port); SNMP_Free(opr->pkt); SNMP_memory_free(opr);}/* received a packet fom an operation */static void opr_recv(packet *rpkt, void *vopr){ struct operation *opr = vopr; SNMP_PKT_T *rcvd_pkt; SNMPADDR_T sa; void (*complete)(struct sty *, SNMP_PKT_T *); struct sty *sty; sty = opr->sty; MEMSET(&sa, 0, sizeof(SNMPADDR_T)); if (packet_dump) { OCTET_T *cp; unsigned int i; sty_printf(sty, "Received %d bytes", rpkt->pkt_datalen); for (cp = rpkt->pkt_data, i = 0; i < rpkt->pkt_datalen; cp++, i++) { if ((i % 16) == 0) sty_puts(sty, "\n"); sty_printf(sty, "%2x ", *cp & 0xff); } sty_puts(sty, "\n\n"); } rcvd_pkt = SNMP_Decode_Packet(rpkt->pkt_data, rpkt->pkt_datalen, &sa,&sa); if (rcvd_pkt == 0) { udp_free(rpkt); return; } udp_free(rpkt); /* Should put some checking here to make sure that this response * is really the response for me. */ complete = opr->complete; opr_cleanup(opr); (*complete)(sty, rcvd_pkt);}/* Got a destination unreachable back. */static void opr_du(packet *p, void *vopr){ struct operation *opr = vopr; void (*error)(struct sty *, char *); struct sty *sty; error = opr->error; sty = opr->sty; opr_cleanup(opr); (*error)(sty, "destination unreachable");}/* encodes the snmp request and sends out the packet with a timeout set */static void send_opr(struct operation *opr){ EBUFFER_T ebuff; packet *send_pkt; struct udp_conn udpc; EBufferInitialize(&ebuff); SNMP_Encode_Packet(opr->pkt, &ebuff); if (packet_dump) { OCTET_T *cp; int i; sty_printf(opr->sty, "Sending %d bytes\n", EBufferUsed(&ebuff)); for(cp = ebuff.start_bp, i = 1; cp < ebuff.next_bp; cp++, i++) { sty_printf(opr->sty, "%2x ", *cp & 0xff); if ((i % 16) == 0) sty_puts(opr->sty, "\n"); } sty_puts(opr->sty, "\n\n"); } /* This copies the encoded packet into a attache packet buffer. * I need to re-order this so we just encode the data into the * right buffer in the first place. */ if ((send_pkt = udp_alloc(EBufferUsed(&ebuff), 0)) == 0) { EBufferClean(&ebuff); (*opr->error)(opr->sty, "couldn't get packet buffer"); opr_cleanup(opr); } MEMCPY(send_pkt->pkt_data, EBufferStart(&ebuff), EBufferUsed(&ebuff)); etc_tm_set(&opr->timeout, (bits32_t)(snmp_timeout) * 1000); udp_conn_init(&udpc); udp_conn_set_src(&udpc, opr->port); udp_conn_set_dst(&udpc, udp_port); ip_conn_set_dst_ip(udp_conn_get_ip_conn(&udpc), &opr->dest); udp_send_conn(send_pkt, &udpc); EBufferClean(&ebuff);}/* handles the timeout. if opr->count is not yet 0 then just beep and * retransmit. otherwise, clean up and go away. */static void opr_timeout(struct timer *tmr, void *vopr){ struct operation *opr = vopr; if (opr->count-- <= 0) { (*opr->error)(opr->sty, "timeout"); opr_cleanup(opr); } else { send_opr(opr); sty_printf(opr->sty, "\007"); }}/* Send an SNMP packet and wait for the response. Retransmit a few times * if needed. */char * snmp_send_rec(struct sty *sty, SNMP_PKT_T *xmit_pkt, ipaddr_t *addr, void (*io_complete)(struct sty *sty, SNMP_PKT_T *rpkt), void (*io_error)(struct sty *sty, char *err_msg)){ struct operation *opr; if ((opr = SNMP_memory_alloc(sizeof(struct operation))) == 0) { SNMP_Free(xmit_pkt); return "Out of memory"; } opr->pkt = xmit_pkt; opr->complete = io_complete; opr->error = io_error; opr->count = retry_count; MEMCPY(&(opr->dest), addr, sizeof(ipaddr_t)); opr->sty = sty; if ((opr->port = udp_listen_port(0, opr_recv, opr_du, opr)) == 0) { SNMP_Free(xmit_pkt); SNMP_memory_free(opr); return "couldn't get a udp port"; } etc_tm_init(&opr->timeout); opr->timeout.handler = opr_timeout; opr->timeout.cookie = opr; send_opr(opr); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -