agentx.c
来自「wm PNE 3.3 source code, running at more 」· C语言 代码 · 共 1,633 行 · 第 1/3 页
C
1,633 行
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/agentx.c,v 1.5 2003/01/15 14:04:50 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 1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: agentx.c,v $ * Revision 1.5 2003/01/15 14:04:50 josh * directory structure shifting * * Revision 1.4 2002/04/04 21:24:35 josh * changes to make the agentx code build properly with ipaddr_ts * * Revision 1.3 2002/03/18 16:29:49 tneale * Allow setting of object ID in the "open" command * * Revision 1.2 2001/11/08 16:47:20 tneale * Updated for newset file layout * * Revision 1.1.1.1 2001/11/05 17:49:11 tneale * Tornado shuffle * * Revision 1.12 2001/01/19 22:24:42 paul * Update copyright. * * Revision 1.11 2000/03/17 00:14:31 meister * Update copyright message * * Revision 1.10 1998/11/25 03:38:19 sra * "timeout" => "snmp_timeout" to fix OpEN symbol conflict. * * Revision 1.9 1998/07/20 01:53:30 sar * removed the validate routine as it's in the library * added snmpstats to allow us to build in more configurations * * Revision 1.8 1998/06/03 21:50:27 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 1.7 1998/02/25 15:21:53 sra * Finish moving types.h, bug.h, and bugdef.h to common/h/. * * Revision 1.6 1998/02/25 04:57:51 sra * Update copyrights. * * Revision 1.5 1997/10/31 03:45:12 sar * Dont wait for a response from a notify * * Revision 1.4 1997/10/30 04:18:59 sar * Trying to get the types correct, change a bits16_t into a ALENGTH_T * * Revision 1.3 1997/10/22 03:03:32 sar * Updated makefile.cfg to use AGENTX_MASTER & AGENTX_SUB * * Added agentx_init() to snmptalk & agentx and envoy_init to sockets * * Revision 1.2 1997/10/21 02:49:17 sar * Allow print_vblist to be accessed for outside nprint so that * it can be used to print out allocated indexes in agentx * * Revision 1.1 1997/10/16 02:18:00 sar * AgentX test routines. * agentx - a sub agent using tcp to get to the master * ax_mast - master side code for use with snmptalk * ttmth - some test routines for use with agentx and tt.mib * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/install.h>#include <wrn/wm/common/config.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/strdup.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/agentx.h>#include <wrn/wm/snmp/engine/snmpstat.h>#include <wrn/wm/attache/snmpvars.h>#include "readmib.h"#include "mibutils.h"#include "md.h"#include "nprint.h"#include <wrn/wm/common/glue.h>extern void agentx_init(void);/* to keep the nprint routines happy */int batch_mode = 0;SNMP_STATS_T snmp_stats;static char *prompt = 0;static boolean_t quit_flag = 0;int SNMPTalk_SNMP_Version = 2;char *mib_filename = "mib.rt";unsigned udp_port = 0;int snmp_timeout = 0;int retry_count = 0;int packet_dump = 0;enum help_level { help_none, help_short, help_long };typedef struct cmd { char *name; boolean_t (*rtn) (struct sty *, enum help_level, int, char **); } cmd_t;extern cmd_t *command_lookup(char *);extern void command_next (struct sty *);extern cmd_t commands[];extern void open_session(struct sty *, struct tcb *, EBUFFER_T *);typedef struct ax_foo_s { struct sty *sty; bits16_t ref_count; ENVOY_AX_CHUNK_T chunk; char *dest_name; bits8_t op; EBUFFER_T packet_buf; } ax_foo_t;typedef struct ax_con_list_s { struct ax_con_list_s *next; bits32_t session_id; struct tcb *con; } ax_con_list_t;bits32_t ax_packet_id = 1;struct tcb *ax_con;ENVOY_AX_SA_SB_T ax_sasb;ax_con_list_t *ax_con_list;char *ax_destination = 0;char *ax_context = 0;bits8_t ax_ndc = 0;ipaddr_t ax_dest_addr = { IPNONE, { 0 } };bits32_t ax_session_id = 0;bits8_t ax_timeout = 1;bits8_t ax_priority = 128;bits8_t ax_byte_order = 0;#define AX_PACKET_DUMP_RAW 1#define AX_PACKET_DUMP_COOK 2int ax_packet_dump = 0;ax_con_list_t * find_con_list(ipaddr_t *dest_addr, bits32_t session_id, int check_flag){ax_con_list_t *con_list;for (con_list = ax_con_list; con_list; con_list = con_list->next) { if ((memcmp(&tcp_get_remote_address_ip(con_list->con), dest_addr, sizeof(ipaddr_t)) == 0) && ((check_flag == 0) || (con_list->session_id == session_id))) return(con_list); }return(0);}void add_con_list(struct tcb *con, bits32_t session_id){ax_con_list_t *con_list;con_list = (ax_con_list_t *)SNMP_memory_alloc(sizeof(ax_con_list_t));if (con_list == 0) return;con_list->con = con;con_list->session_id = session_id;con_list->next = ax_con_list;ax_con_list = con_list;return;}void remove_con_list(ax_con_list_t *con_list){ax_con_list_t **temp_con_list;for(temp_con_list = &ax_con_list; *temp_con_list; temp_con_list = &(*temp_con_list)->next) { if (*temp_con_list == con_list) { *temp_con_list = con_list->next; SNMP_memory_free(con_list); break; } }return;}static char *ax_strings[] = { "none", "open", "close", "register", "unregister", "get", "next", "bulk", "test", "commit", "undo", "cleanup", "notify", "ping", "allocate index", "deallocate index", "add ac", "remove ac", "response" };void agentx_admin(ptr_t cookie, ptr_t pkt){ENVOY_AX_PKT_T *ax_pkt = (ENVOY_AX_PKT_T *)pkt;ax_foo_t *myfoo;char *op_string;myfoo = (ax_foo_t *)(tcp_get_cookie((struct tcb *)cookie));if (ax_pkt->type == ENVOY_AX_RESPONSE) { op_string = ax_strings[myfoo->op]; if (ax_pkt->error_stat) { sty_printf(myfoo->sty, "Operation %s failed, error %d, index %d\n", op_string, ax_pkt->error_stat, ax_pkt->error_index); } else { sty_printf(myfoo->sty, "Operation %s succeeded\n", op_string); if (myfoo->op == ENVOY_AX_OPEN) { ax_session_id = ax_pkt->session_id; add_con_list((struct tcb *)cookie, ax_session_id); } if (myfoo->op == ENVOY_AX_INDEX_ALLOCATE) { print_vblist(myfoo->sty, &ax_pkt->data.proc_data.vbl_str); } } if (myfoo->op) command_next(myfoo->sty); myfoo->op = 0; }return;}void agentx_add(ptr_t cookie){((ax_foo_t *)(tcp_get_cookie((struct tcb *)cookie)))->ref_count++;}void agentx_free(ptr_t cookie, int foobar){((ax_foo_t *)(tcp_get_cookie((struct tcb *)cookie)))->ref_count--;}int agentx_send(ptr_t cookie, ptr_t ax_pkt, ptr_t vblist, ALENGTH_T need){packet *pkt;EBUFFER_T ebuff;EBufferInitialize(&ebuff);pkt = tcp_alloc(need);if (pkt == 0) return(1);EBufferSetup(BFL_IS_STATIC, &ebuff, pkt->pkt_data, pkt->pkt_datalen);if (envoy_ax_pkt_encode(ax_pkt, vblist, &ebuff, need)) { tcp_free(pkt); return(1); }(void) tcp_write((struct tcb *)cookie, pkt);return(0);}void agentx_open(struct tcb *con){ax_foo_t *myfoo;myfoo = (ax_foo_t *)tcp_get_cookie(con);open_session(myfoo->sty, con, &myfoo->packet_buf);return;}bits16_t agentx_rcv(struct tcb *con, packet *pkt){ax_foo_t *myfoo;myfoo = tcp_get_cookie(con);if (ax_packet_dump & AX_PACKET_DUMP_RAW) { bits8_t *cp; ALENGTH_T i; struct sty *sty; sty = myfoo->sty; sty_printf(sty, "Received %d bytes", pkt->pkt_datalen); for (cp = pkt->pkt_data, i = 0; i < pkt->pkt_datalen; cp++, i++) { if ((i % 16) == 0) sty_puts(sty, "\n"); sty_printf(sty, "%2x ", *cp & 0xff); } sty_puts(sty, "\n\n"); }envoy_ax_chunk_handler(pkt->pkt_data, pkt->pkt_datalen, envoy_ax_sa_handler, &myfoo->chunk, &ax_sasb, agentx_admin, agentx_send, agentx_free, agentx_add, con);tcp_free(pkt);return(1000);}void agentx_remote_close(struct tcb *con){tcp_close(con);return;}void agentx_closed(struct tcb *con, int why){ax_foo_t *myfoo;myfoo = (ax_foo_t *)tcp_get_cookie(con);if (myfoo) { envoy_ax_sa_session_loss(&ax_sasb, con, 1, 0); envoy_ax_chunk_clean(&myfoo->chunk); EBufferClean(&myfoo->packet_buf); SNMP_memory_free(myfoo); }return;}void agentx_init(){envoy_ax_sa_state_init(&ax_sasb);return;}boolean_t send_ax_queries(struct sty *sty, struct tcb *con, EBUFFER_T *ebufp){packet *pkt;pkt = tcp_alloc(EBufferUsed(ebufp));if (pkt == 0) return(1);MEMCPY(pkt->pkt_data, EBufferStart(ebufp), EBufferUsed(ebufp));pkt->pkt_datalen = EBufferUsed(ebufp);if (ax_packet_dump & AX_PACKET_DUMP_RAW) { bits8_t *cp; ALENGTH_T i; sty_printf(sty, "Sending %d bytes", pkt->pkt_datalen); for (cp = pkt->pkt_data, i = 0; i < pkt->pkt_datalen; cp++, i++) { if ((i % 16) == 0) sty_puts(sty, "\n"); sty_printf(sty, "%2x ", *cp & 0xff); } sty_puts(sty, "\n\n"); }tcp_write(con, pkt);return(0);}/* exit the program */boolean_t do_quit(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "quit - quit the agentx program\n"); break; case help_long: sty_puts(sty, "The 'quit' command exits from the agentx program.\n"); break; case help_none: quit_flag = 1; break; } return (1);}/* the short help command, a listing of the other commands */boolean_t do_short_help(struct sty *sty, enum help_level help, int argc, char *argv[]){cmd_t *cmd; switch (help) { case help_short: sty_puts(sty, "? - short listing of commands\n"); break; case help_long: sty_puts(sty, "\The '?' command gives a short listing of all the available commands.\n"); break; case help_none: for (cmd = &commands[0]; cmd->name; cmd++) cmd->rtn(sty, help_short, 0, 0); }return (1);}/* the help command */boolean_t do_help(struct sty *sty, enum help_level help, int argc, char *argv[]){static char help_word[] = "help";static char *help_argv[] = { help_word, help_word };cmd_t *cmd; switch (help) { case help_short: sty_puts(sty, "help <command> - give more information on <command>\n"); break; case help_long: sty_puts(sty, "help <command> - give more information on <command>\n"); break; case help_none: if (argc == 1) do_help(sty, help_none, 2, help_argv); else { cmd = command_lookup(argv[1]); if (cmd != 0) cmd->rtn(sty, help_long, 0, 0); else sty_printf(sty, "command `%s' not found\n", argv[1]); } }return (1);}/* print some information about the system */boolean_t do_status(struct sty *sty, enum help_level help, int argc, char *argv[]){ax_con_list_t *con_list;switch (help) { case help_short: sty_puts(sty, "status - print information about the system\n"); break; case help_long: sty_puts(sty, "\The 'status' command dumps information about the status of the system\n"); break; case help_none: sty_printf(sty, "timeout: %d, priority: %d, byte order: %d, context %s\n",
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?