agentx.c
来自「wm PNE 3.3 source code, running at more 」· C语言 代码 · 共 1,633 行 · 第 1/3 页
C
1,633 行
ax_timeout, ax_priority, ax_byte_order, ax_context); sty_printf(sty, "\ session id destination\n\current %10lu %s\n", ax_session_id, ax_destination); for(con_list = ax_con_list; con_list; con_list = con_list->next) { sty_printf(sty, " %10lu %s\n", con_list->session_id, ((ax_foo_t *)tcp_get_cookie(con_list->con))->dest_name); } break; } return (1);}/* set the packet dump options */boolean_t do_packet_dump(struct sty *sty, enum help_level help, int argc, char *argv[]){int temp_pd;switch (help) { case help_short: sty_puts(sty, "packet_dump <number> - set the packet dump flag\n"); break; case help_long: sty_puts(sty,"\The 'packet_dump <number>' command sets the packet dump flag.\n\The options are 0 - none, 1 - raw and 2 - cooked.\n"); break; case help_none: if (argc < 2) sty_puts(sty, "`packet_dump' command needs `number' as argument\n"); else { temp_pd = atoi(argv[1]); if ((temp_pd < 0) || (temp_pd > (AX_PACKET_DUMP_RAW | AX_PACKET_DUMP_COOK))) sty_puts(sty, "Illegal value for `packet_dump'\n"); else ax_packet_dump = temp_pd; } break; }return(1);}/* set the session id to use */boolean_t do_session(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "session <number> - set the session id\n"); break; case help_long: sty_puts(sty,"\The 'session <number>' command sets the id of the session to use.\n"); break; case help_none: if (argc < 2) sty_puts(sty, "`session' command needs `number' as argument\n"); else ax_session_id = atoi(argv[1]); break; }return(1);}/* set the timeout period to use */boolean_t do_timeout(struct sty *sty, enum help_level help, int argc, char *argv[]){int tout;switch (help) { case help_short: sty_puts(sty, "timeout <number> - set the timeout period\n"); break; case help_long: sty_puts(sty,"\The 'timeout <number>' command sets the period to send to the master.\n"); break; case help_none: if (argc < 2) sty_puts(sty, "`timeout' command needs `number' as argument\n"); else { tout = atoi(argv[1]); if (tout > 255) sty_puts(sty, "`timeout' period must be less than 256\n"); else ax_timeout = (bits8_t)(tout & 0xFF); } break; }return(1);}/* set priority level to use */boolean_t do_priority(struct sty *sty, enum help_level help, int argc, char *argv[]){int priority;switch (help) { case help_short: sty_puts(sty, "priority <number> - set the priority level\n"); break; case help_long: sty_puts(sty,"\The 'priority <number>' command sets the priority level to use.\n"); break; case help_none: if (argc < 2) sty_puts(sty, "`priority' command needs `number' as argument\n"); else { priority = atoi(argv[1]); if (priority > 255) sty_puts(sty, "`priority' level must be less than 256\n"); else ax_priority = (bits8_t)(priority & 0xFF); } break; }return(1);}/* set the byte ordering to use */boolean_t do_byte_order(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "byte_order <number> - set the byte order flag\n"); break; case help_long: sty_puts(sty,"\The 'byte_order <number>' command sets the byte order flag.\n"); break; case help_none: if (argc < 2) sty_puts(sty, "`byte_order' command needs `number' as argument\n"); else if (atoi(argv[1])) ax_byte_order = 1; else ax_byte_order = 0; break; }return(1);}/* set the context string to use */boolean_t do_context(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "context <string> - set the context name\n"); break; case help_long: sty_puts(sty,"\The 'context <string>' command sets the name of the context to use.\n"); break; case help_none: if (ax_context) etc_strdup_free(ax_context); if (argc >= 2) ax_context = etc_strdup(argv[1]); if (ax_context) ax_ndc = ENVOY_AX_BIT_NDC; else ax_ndc = 0; break; }return(1);}/* find a destination */static void do_dest_got_answer(struct sty *sty, char *name, ipaddr_t *addr){char *p;if (addr == 0) { sty_printf(sty, "Can not locate host `%s'\n", name); command_next(sty); return; }memcpy(&ax_dest_addr, addr, sizeof(ipaddr_t));if (ax_destination != name) { if (ax_destination) etc_strdup_free(ax_destination); ax_destination = etc_strdup(name); }p = SNMP_memory_alloc(STRLEN(ax_destination) + sizeof("agentx@> "));if (p) { STRCPY(p, "agentx@"); strcat(p, ax_destination); strcat(p, "> "); if (prompt) free(prompt); prompt = p; }command_next(sty);}boolean_t do_dest(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "dest <host> - set the agentx master\n"); break; case help_long: sty_puts(sty, "\The 'dest <host>' command sets the IP hostname or address of\n\the agentx master agent to use.\n"); break; case help_none: if (argc < 2) { sty_puts(sty, "`dest' command needs `hostname' as argument\n"); return (1); } else { md_hostname_to_addr(sty, argv[1], do_dest_got_answer); return (0); } }return(1);}/* open a session */void open_session(struct sty *sty, struct tcb *con, EBUFFER_T *ebuff){ax_foo_t *myfoo;myfoo = (ax_foo_t *)tcp_get_cookie(con);myfoo->op = ENVOY_AX_OPEN;if (send_ax_queries(sty, con, ebuff)) { myfoo->op = 0; command_next(sty); }EBufferClean(ebuff);return;}void open_connection(struct sty *sty, EBUFFER_T *ebuff){ax_foo_t *myfoo;struct tcb *con;enum tcp_start_errors err_ret;con = tcp_create();if (con == 0) { EBufferClean(ebuff); sty_puts(sty, "Couldn't allocate tcb struct\n"); command_next(sty); return; }myfoo = (ax_foo_t *)SNMP_memory_alloc(sizeof(ax_foo_t));if (myfoo == 0) { EBufferClean(ebuff); tcp_close(con); sty_puts(sty, "Couldn't allocate cookie struct for tcb\n"); command_next(sty); return; }envoy_ax_chunk_init(&myfoo->chunk);myfoo->ref_count = 0;myfoo->sty = sty;myfoo->dest_name = etc_strdup(ax_destination);MEMCPY(&myfoo->packet_buf, ebuff, sizeof(EBUFFER_T));EBufferInitialize(ebuff);tcp_set_remote_port(con, ENVOY_AX_MASTER_PORT);tcp_set_remote_ipaddr(con, &ax_dest_addr);tcp_set_open_upcall(con, agentx_open); tcp_set_receive_window(con, 1000);tcp_set_receive_upcall(con, agentx_rcv);tcp_set_remote_close_upcall(con, agentx_remote_close);tcp_set_closed_upcall(con, agentx_closed);tcp_set_cookie(con, (void *)myfoo);err_ret = tcp_start(con, TCP_ACTIVE);if (err_ret != TCP_START_OK) { tcp_close(con); EBufferClean(&myfoo->packet_buf); free(myfoo->dest_name); SNMP_memory_free(myfoo); sty_printf(sty, "Couldn't start tcp connect %d\n", err_ret); command_next(sty); return; } return;}boolean_t do_open(struct sty *sty, enum help_level help, int argc, char *argv[]){ax_con_list_t *con_list;EBUFFER_T ebuff;OIDC_T oidc[40];OBJ_ID_T obj;char *descr;bits32_t descrlen;switch (help) { case help_short: sty_puts(sty, "open - open a session to a master agent\n"); break; case help_long: sty_puts(sty, "\The 'open' <obj id> <description> command opens a session to a master agent.\<obj id> and <description> are optional.\n"); break; case help_none: /* try to open a session to the requested master agent, if we don't have a dest_addr we complain, else if we don't have a connection we try to open one else we try to open a session */ if (ax_dest_addr.type == IPNONE) { sty_puts(sty, "Need to specify destination first\n"); return(1); } EBufferInitialize(&ebuff); /* set up the object id for the system oid */ if (argc >= 2) { obj.num_components = string2oid(argv[1], oidc, sizeof(oidc)/sizeof(OIDC_T)); if (obj.num_components == 0) { sty_printf(sty, "No mib entry `%s'\n", argv[1]); return(1); } obj.component_list = oidc; } else { obj.num_components = 0; obj.component_list = 0; } /* set up the description string */ if (argc == 3) { descr = argv[2]; descrlen = STRLEN(descr); } else { descr = 0; descrlen = 0; } if (envoy_ax_pkt_create_open(&ebuff, 1, ax_byte_order, 0, ax_timeout, descrlen, (bits8_t *)descr, &obj)) { sty_printf(sty, "Unable to create open packet\n"); return(1); } con_list = find_con_list(&ax_dest_addr, ax_session_id, 0); if (con_list == 0) open_connection(sty, &ebuff); else open_session(sty, con_list->con, &ebuff); return(0); }return(1);}/* close a session */boolean_t do_close(struct sty *sty, enum help_level help, int argc, char *argv[]){EBUFFER_T ebuff;ax_con_list_t *con_list;int reason;switch (help) { case help_short: sty_puts(sty, "close - close a session to a master agent\n"); break; case help_long: sty_puts(sty, "The 'close' command closes a session to a master agent.\n"); break; case help_none: /* build close request */ con_list = find_con_list(&ax_dest_addr, ax_session_id, 1); if (con_list == 0) { sty_puts(sty, "Need to specify an open session\n"); break; } if ((argc < 2) || ((reason = atoi(argv[1])) > 6)) { sty_puts(sty, "Need to specify a legal reason for closing\n"); break; } EBufferInitialize(&ebuff); if (envoy_ax_pkt_create_close(&ebuff, 1, ax_byte_order, ax_session_id, 0, reason) == 0) { (void) send_ax_queries(sty, con_list->con, &ebuff); EBufferClean(&ebuff); /*sar ref count? */ remove_con_list(con_list); ax_session_id = 0; } break; }return(1);}/* register an object */boolean_t process_register(struct sty *sty, int argc, char *argv[], bits8_t op_req){EBUFFER_T ebuff;ax_con_list_t *con_list;int range = 0;OBJ_ID_T obj;OIDC_T oidc1[40], bound = 0;ax_foo_t *myfoo;con_list = find_con_list(&ax_dest_addr, ax_session_id, 1);if (con_list == 0) { sty_puts(sty, "Need to specify an open session\n"); return(1); }if ((argc != 2) && (argc != 4)) { sty_puts(sty, "Wrong arguments 'command <objectid> <range> <bound>\n"); return(1); }obj.num_components = string2oid(argv[1], oidc1, sizeof(oidc1)/sizeof(OIDC_T));obj.component_list = oidc1;if (obj.num_components == 0) { sty_printf(sty, "No mib entry `%s'\n", argv[1]); return(1); }if (argc == 4) { range = atoi(argv[2]); if (range > obj.num_components) { sty_printf(sty, "<range> exceeds length of <objectid>\n"); return(1); } bound = (OIDC_T)atol(argv[3]); }EBufferInitialize(&ebuff);if (envoy_ax_pkt_create_registrations(&ebuff, 1, op_req, ax_byte_order | ax_ndc, ax_packet_id++, ax_session_id, ax_timeout, ax_priority, (bits8_t)(range & 0xff), &obj, bound, sizeof(ax_context), (bits8_t *)ax_context) == 0) { myfoo = (ax_foo_t *)tcp_get_cookie(con_list->con); myfoo->op = op_req; if (send_ax_queries(sty, con_list->con, &ebuff)) { myfoo->op = 0; EBufferClean(&ebuff); return(1); } EBufferClean(&ebuff); return(0); }return(1);}boolean_t
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?