⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tst2_sa.c

📁 wm PNE 3.3 source code, running at more than vxworks6.x version.
💻 C
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/unxagent/tst2_sa.c,v 1.3 2003/01/15 14:04:59 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, using the toaster mib                      *//*                                                                    *//**********************************************************************//* * $Log: tst2_sa.c,v $ * Revision 1.3  2003/01/15 14:04:59  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:16  tneale * Tornado shuffle * * Revision 7.8  2001/01/19 22:24:59  paul * Update copyright. * * Revision 7.7  2000/03/17 00:14:56  meister * Update copyright message * * Revision 7.6  1998/06/03 21:56:23  sar * Updated code to use common string macros * * Revision 7.5  1998/02/25 04:58:14  sra * Update copyrights. * * Revision 7.4  1997/10/22 02:59:11  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:24  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:56  sra * Release 6.0. * * Revision 5.3  1995/05/29  03:03:22  sar * Added stub routines for time functions * * Revision 5.2  1995/05/25  03:05:33  sar * Changed the method routines to use the async api * * Revision 5.1  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.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>#define LM_toastManf	1#define LM_toastModel	2#define LM_toastControl 3#define LM_toastDone	4#define LM_toastType	5char toaster_manf[] = "Sunbeam";char cd_ModelNumber[] = "Radiant Automatic";INT_32_T toasterstate = 1;INT_32_T toasterdoneness = 1;INT_32_T toasttype = 1;extern int errno;#define BUFLEN 2000#define MASTER "/tmp/M_AGENT"#define SUBAGENT "/tmp/TOASTER"int  main(int    argc,       char **argv){int sock, fromlen, cc;struct sockaddr_un master_name, sa_name, from;char buf[BUFLEN], *subagent;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;EBUFFER_T ebuf, rbuf;SA_DEMUX_T demuxer;static OIDC_T toastoidc1[] = {1, 3, 6, 1, 4, 1, 12, 2, 1};static OIDC_T toastoidc2[] = {1, 3, 6, 1, 4, 1, 12, 2, 2};static OIDC_T toastoidc3[] = {1, 3, 6, 1, 4, 1, 12, 2, 3};static OIDC_T toastoidc4[] = {1, 3, 6, 1, 4, 1, 12, 2, 4};static OIDC_T toastoidc5[] = {1, 3, 6, 1, 4, 1, 12, 2, 5};VB_T  vb[5];/* Initialize envoy */envoy_init();if (argc > 1)    subagent = argv[1];else    subagent = SUBAGENT;if (argc > 2)    packets = atoi(argv[2]);/* 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);/* create message */(void)MEMSET(vb, 0, 5 * sizeof(VB_T));vb[0].vb_flags			= VFLAG_SA_SENDIT;vb[0].vb_obj_id.num_components  = 9;vb[0].vb_obj_id.component_list  = toastoidc1;vb[0].vb_data_flags_n_type	= VT_STRING;vb[0].vb_access			= 0x01;vb[1].vb_flags			= VFLAG_SA_SENDIT;vb[1].vb_obj_id.num_components	= 9;vb[1].vb_obj_id.component_list	= toastoidc2;vb[1].vb_data_flags_n_type	= VT_STRING;vb[1].vb_access			= 0x01;vb[2].vb_flags			= VFLAG_SA_SENDIT;vb[2].vb_obj_id.num_components	= 9;vb[2].vb_obj_id.component_list	= toastoidc3;vb[2].vb_data_flags_n_type	= VT_NUMBER;vb[2].vb_access			= 0x03;vb[3].vb_flags			= VFLAG_SA_SENDIT;vb[3].vb_obj_id.num_components	= 9;vb[3].vb_obj_id.component_list	= toastoidc4;vb[3].vb_data_flags_n_type	= VT_NUMBER;vb[3].vb_access			= 0x03;vb[4].vb_flags			= VFLAG_SA_SENDIT;vb[4].vb_obj_id.num_components	= 9;vb[4].vb_obj_id.component_list	= toastoidc5;vb[4].vb_data_flags_n_type	= VT_NUMBER;vb[4].vb_access			= 0x03;vblist.vbl_count = 5;vblist.vblist    = vb;EBufferInitialize(&demuxer.string);init_object_id(&demuxer.object);(void)MEMSET(&hdr_blk, 0, sizeof(SA_HEADER_T));hdr_blk.version    = 1;hdr_blk.opcode1    = SA_REG_OBJ_REQUEST;hdr_blk.encoding   = SA_ENCODING_ASN1;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);    }/* clean up the buffer */EBufferClean(&ebuf);/* wait for a reply to the registration request */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) {    printf("Error handling reply to registration request: %ld\n", eret);    exit(1);    }if (hdr_blk.opcode1 == SA_REG_OBJ_REPLY) {    if (hdr_blk.sa_error) {	printf("Error with registration request: %d\n", hdr_blk.sa_error);	exit(1);	}    else	groupid = hdr_blk.index;    }/* Loop waiting for messages.  Receive a message, call sa_handler to   process it, then send the reply if required */   tstamp = time(0) + packets;   while(1) {    /* set up the select call and then wait for a packet */    FD_ZERO(&ready);    FD_SET(sock, &ready);    timeout.tv_sec = 1;    timeout.tv_usec = 0;      if (select(sock + 1, &ready, 0, 0, &timeout) > 0) {       fromlen = sizeof(struct sockaddr_un);       cc = recvfrom(sock, buf, BUFLEN, 0, (struct sockaddr *)&from, &fromlen);            (void)MEMSET(&hdr_blk, 0, sizeof(SA_HEADER_T));       eret = SA_Handler((OCTET_T *)buf, (ALENGTH_T)cc, &rbuf, &hdr_blk,			 &rvblist);       if (eret != 0)	   continue;       switch(hdr_blk.opcode1) {           case SA_QUERY_REQUEST:	        /* send the reply, and clean the buffer */	        sendto(sock, EBufferStart(&rbuf), EBufferUsed(&rbuf), 0,		       (struct sockaddr *)&from, fromlen);		break;	    case SA_REG_OBJ_REPLY:		if (hdr_blk.sa_error) {		    printf("Error adding objects %d\n", hdr_blk.sa_error);		    exit(1);		    }		groupid = hdr_blk.index;		break;	    case SA_REM_OBJ_REPLY:		if (hdr_blk.sa_error) {		    printf("Error removing objects %d\n", hdr_blk.sa_error);		    exit(1);		    }		break;	    }        }    EBufferClean(&rbuf);    Clean_vb_list(&rvblist);      if (tstamp > time(0))         continue;    tstamp = time(0) + packets;      /* so we timed out, we either register or deregister the node set */    (void)MEMSET(&hdr_blk, 0, sizeof(SA_HEADER_T));    hdr_blk.version  = 1;    hdr_blk.encoding = SA_ENCODING_ASN1;        if (reg1) {        hdr_blk.opcode1  = SA_REM_OBJ_REQUEST;	hdr_blk.index    = groupid;	reg1 = 0;        }    else {        hdr_blk.opcode1  = SA_REG_OBJ_REQUEST;	hdr_blk.index    = 0;	reg1 = 1;        }    eret = SA_Encode(&vblist, &hdr_blk, &demuxer, &ebuf);    if (eret) {	printf("Error creating reg or rem message: %ld\n", eret);	exit(1);	}    sendto(sock, EBufferStart(&ebuf), EBufferUsed(&ebuf), 0,	   (struct sockaddr *)&master_name, sizeof(struct sockaddr_un));    EBufferClean(&ebuf);    }}void  toaster_free(PTR_T vbptr){return;}/****************************************************************************NAME:  toaster_testPURPOSE:  Find the appropriate entry in the sub agent table and attach	  information from it to the vbp using the getproc_got_* functions.	  If we can't find an entry indicate that by calling getproc_nosuchins.PARAMETERS:	OIDC_T		Last component of the object id leading to			the leaf node in the MIB.  This is usually			the identifier for the particular attribute			in the table.	int		Number of components in the unused part of the			object identifier	OIDC_T *	Unused part of the object identifier	SNMP_PKT_T *	SNMP packet currently being processed.	VB_T *		Variable being processed.RETURNS: void****************************************************************************//*ARGSUSED*/void  toaster_test(OIDC_T		 last_match,	       int		 tcount,	       OIDC_T		*tlist,	       SNMP_PKT_T	*pktp,	       VB_T		*vbp){INT_32_T value;value = VB_GET_INT32(vbp);if (value < 1) {    testproc_error(pktp, vbp, WRONG_VALUE);    return;    }switch (last_match) {   case LM_toastControl:	if (value > 2) {	    testproc_error(pktp, vbp, WRONG_VALUE);	    return;	    }	break;    case LM_toastDone:	if (value > 10) {	    testproc_error(pktp, vbp, WRONG_VALUE);	    return;	    }	break;    case LM_toastType:	if (value > 7) {	    testproc_error(pktp, vbp, WRONG_VALUE);	    return;	    }	break;    }if ((tcount != 1) || (*tlist != 0)) {    testproc_error(pktp, vbp, NO_CREATION);    return;    }testproc_good(pktp, vbp);vbp->vb_free_priv = toaster_free;vbp->vb_priv = 0;return;}/****************************************************************************NAME:  toaster_setPURPOSE:  Find the appropriate entry in the sub agent table and attach	  information from it to the vbp using the getproc_got_* functions.	  If we can't find an entry indicate that by calling getproc_nosuchins.PARAMETERS:	OIDC_T		Last component of the object id leading to			the leaf node in the MIB.  This is usually			the identifier for the particular attribute			in the table.	int		Number of components in the unused part of the			object identifier	OIDC_T *	Unused part of the object identifier	SNMP_PKT_T *	SNMP packet currently being processed.	VB_T *		Variable being processed.RETURNS: void****************************************************************************//*ARGSUSED*/void  toaster_set(OIDC_T		 last_match,	      int		 tcount,	      OIDC_T		*tlist,	      SNMP_PKT_T	*pktp,	      VB_T		*vbp){switch (last_match) {    case LM_toastControl:	toasterstate = VB_GET_INT32(vbp);	break;    case LM_toastDone:	toasterdoneness = VB_GET_INT32(vbp);	break;    case LM_toastType:	toasttype = VB_GET_INT32(vbp);	break;    }setproc_good(pktp, vbp);vbp->vb_free_priv = 0;vbp->vb_priv = 0;return;}/* Stub routines to keep from using the versions in sun/agent.c */bits32_t  envoy_now(void){return(0);}void  envoy_call_timer (bits32_t when,		    void (*what)(void)){return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -