📄 tab2_sa.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/unxagent/tab2_sa.c,v 1.3 2003/01/15 14:04:58 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 1994-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//**********************************************************************//* *//* Simple SNMP sub agent, demoing a table *//* *//**********************************************************************//* * $Log: tab2_sa.c,v $ * Revision 1.3 2003/01/15 14:04:58 josh * directory structure shifting * * Revision 1.2 2001/11/09 21:48:59 josh * unxagent demo path adjustment, first pass * * Revision 1.1.1.1 2001/11/05 17:49:15 tneale * Tornado shuffle * * Revision 7.9 2001/01/19 22:24:58 paul * Update copyright. * * Revision 7.8 2000/03/17 00:14:55 meister * Update copyright message * * Revision 7.7 1998/06/28 21:57:54 sar * clean up some misspellings in the incude list * * Revision 7.6 1998/06/03 21:56:22 sar * Updated code to use common string macros * * Revision 7.5 1998/02/25 04:58:12 sra * Update copyrights. * * Revision 7.4 1997/10/22 02:59:10 sar * Removed the declaration of the snmp_coarselock as it is included in * envoy_init, which was added * * Revision 7.3 1997/03/20 06:54:20 sra * DFARS-safe copyright text. Zap! * * Revision 7.2 1997/02/25 10:58:16 sra * Update copyright notice, dust under the bed. * * Revision 7.1 1997/01/08 22:46:52 sar * Updated copyright and changed include files to use envoy/h where * appropriate * * Revision 7.0 1996/03/15 21:57:35 sar * Updated revision to 7.0 and copyright to 96 * * Revision 6.1 1995/11/01 01:01:02 sar * Added pp style argument lists and removed no_pp style stuff * * Revision 6.0 1995/05/31 21:49:33 sra * Release 6.0. * * Revision 5.4 1995/05/29 03:03:22 sar * Added stub routines for time functions * * Revision 5.3 1995/05/25 03:05:33 sar * Changed the method routines to use the async api * * Revision 5.2 1994/10/25 20:09:59 sar * Added an ifdef to allocate SNMP_CoarseLock if we are building a strcit * subagent. * Also modified some fprintf's to use %ld instead of %d * * Revision 5.1 1994/10/17 18:11:39 sar * Changed unused field in hdr_blk to be snmp_version * * Revision 5.0 1994/05/16 16:40:08 sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 1.1 1994/05/04 22:39:30 sar * Initial revision * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/config.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>#include <sys/time.h>#include <ctype.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/snmpdefs.h>#include <wrn/wm/snmp/engine/objectid.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/buildpkt.h>#include <wrn/wm/snmp/engine/subagent.h>#include <wrn/wm/snmp/engine/etimer.h>#include <wrn/wm/common/glue.h>typedef struct INDEXLIST_S { int index; int type; } INDEXLIST_T;#define BUFLEN 2000#define MASTER "/tmp/M_AGENT"#define SUBAGENT "/tmp/TABLE"extern int errno;#define LM_tableindex 1#define LM_tabledescr 2#define LM_tabletype 3INDEXLIST_T *indexlist;int ilen;char *subagent;int main(int argc, char **argv){int sock, fromlen, cc, i, j;struct sockaddr_un master_name, sa_name, from;char buf[BUFLEN], *vbuf;fd_set ready;struct timeval timeout;long tstamp;SA_HEADER_T hdr_blk;INT_32_T eret, groupid = 0, packets = 60, reg1 = 1;VBL_T vblist, rvblist;VB_T *vbp, *vbl;EBUFFER_T ebuf, rbuf;SA_DEMUX_T demuxer;static OIDC_T tableoidc[] = {1, 3, 6, 1, 4, 1, 12, 8, 1, 1, 0, 0};int tableoidclen = 12;/* Initialize envoy */envoy_init();/* Initialize some of the arguments */vblist.vbl_count = rvblist.vbl_count = 0;vblist.vblist = rvblist.vblist = 0;vblist.vblp = rvblist.vblp = 0;EBufferInitialize(&ebuf);EBufferInitialize(&rbuf);if (argc < 4) { printf("Usage: table_sa sock_name timeout indx1 <indx2 ...>\n"); exit(1); }subagent = argv[1];packets = atoi(argv[2]);argc -= 3;argv += 3;/* create the indexlist */ilen = argc;indexlist = (INDEXLIST_T *)SNMP_memory_alloc(argc * sizeof(INDEXLIST_T));if (indexlist == 0) { printf("unable to allocate index list\n"); exit(1); }for(i = 0; i < argc; i++) { indexlist[i].index = atoi(argv[i]); indexlist[i].type = 1; }/* create the vb list, we will build the whole list at once and use the appropriate pieces when required. There will be 3 * argc instances + 3 objects */vbl = (VB_T *)SNMP_memory_alloc(((3 * argc) + 3) * sizeof(VB_T));if (vbl == 0) { printf("unable to allocate vb list\n"); exit(1); }for(i = 1, vbp = vbl; i < 4; i++) { for(j = 0; j < argc + 1; j++, vbp++) { /* flags are always 0, and need to be set (and reset) by the sending code */ vbp->vb_flags = 0; vbp->value_u.v_uinteger32 = 0; vbuf = SNMP_memory_alloc(tableoidclen * sizeof(OIDC_T)); if (vbuf == 0) { printf("Couldn't allocate a vb objectid\n"); exit(1); } MEMCPY(vbuf, tableoidc, tableoidclen * sizeof(OIDC_T)); vbp->vb_obj_id.component_list = (OIDC_T *)vbuf; vbp->vb_obj_id.component_list[tableoidclen - 2] = i; /* install different values depending on which object we are dealing with*/ switch (i) { case 1: /* first group, the index, number and read-only access */ vbp->vb_data_flags_n_type = VT_NUMBER; vbp->vb_access = 0x01; break; case 2: /* second group, the descr, string and read-only access */ vbp->vb_data_flags_n_type = VT_STRING; vbp->vb_access = 0x01; break; case 3: /* third group, the type, number and read-write access */ vbp->vb_data_flags_n_type = VT_NUMBER; vbp->vb_access = 0x03; break; } /* The first one in each group (j == 0), is the object id the rest are instances */ if (j == 0) { vbp->vb_obj_id.num_components = tableoidclen - 1; vbp->sa_flags = SA_INSTANCE_TYPE; } else { vbp->vb_obj_id.num_components = tableoidclen; vbp->vb_obj_id.component_list[tableoidclen - 1] = (OIDC_T)indexlist[j - 1].index; vbp->sa_flags = 0; } } }/* set up the vblist */vblist.vbl_count = (argc * 3) + 3;vblist.vblist = vbl;/* create message */vbl[0].vb_flags = VFLAG_SA_SENDIT;vbl[argc + 1].vb_flags = VFLAG_SA_SENDIT;vbl[(2 * argc) + 2].vb_flags = VFLAG_SA_SENDIT;EBufferInitialize(&demuxer.string);init_object_id(&demuxer.object);hdr_blk.version = 1;hdr_blk.opcode1 = SA_REG_OBJ_REQUEST;hdr_blk.opcode2 = 0;hdr_blk.sa_error = 0;hdr_blk.encoding = SA_ENCODING_ASN1;hdr_blk.snmp_version = 0;hdr_blk.requestid = 0;hdr_blk.sysuptime = 0;hdr_blk.index = 0;hdr_blk.snmp_error = 0;eret = SA_Encode(&vblist, &hdr_blk, &demuxer, &ebuf);if (eret) { printf("Error creating message %ld\n", eret); exit(1); }/* build socket */sock = socket(AF_UNIX, SOCK_DGRAM, 0);if (sock < 0) { printf("Error opening datagram socket\n"); exit(1); }unlink(subagent);sa_name.sun_family = AF_UNIX;STRCPY(sa_name.sun_path, subagent);/* bind the socket to a name */if (bind(sock, (struct sockaddr *)&sa_name, sizeof(struct sockaddr_un))) { printf("Error binding socket name\n"); exit(1); }master_name.sun_family = AF_UNIX;STRCPY(master_name.sun_path, MASTER);/* send message */while(sendto(sock, EBufferStart(&ebuf), EBufferUsed(&ebuf), 0, (struct sockaddr *)&master_name, sizeof(struct sockaddr_un)) < 0) { printf("Registration request failed: %d, will try again\n", errno); sleep(10); }/* done with the buffer so clean it up */EBufferClean(&ebuf);fromlen = sizeof(struct sockaddr_un);cc = recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *)&from, &fromlen);eret = SA_Handler((OCTET_T *)buf, (ALENGTH_T)cc, &rbuf, &hdr_blk, &rvblist);if ((eret != 0) || (hdr_blk.sa_error)) { printf("Error with registration %d\n", hdr_blk.sa_error); exit(1); }else groupid = hdr_blk.index;/* clean up the receive buffer */EBufferClean(&rbuf);Clean_vb_list(&rvblist);/* set up the instance message, send everybody */for (i = 0; i < (argc * 3) + 3; i++) vbl[i].vb_flags = VFLAG_SA_SENDIT;hdr_blk.version = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -