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

📄 snmp_client.c

📁 嵌入式操作系统ECOS的网络开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
//==========================================================================
//
//      ./lib/current/src/snmp_client.c
//
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//####UCDSNMPCOPYRIGHTBEGIN####
//
// -------------------------------------------
//
// Portions of this software may have been derived from the UCD-SNMP
// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
// California at Davis, which was originally based on the Carnegie Mellon
// University SNMP implementation.  Portions of this software are therefore
// covered by the appropriate copyright disclaimers included herein.
//
// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
// -------------------------------------------
//
//####UCDSNMPCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    hmt
// Contributors: hmt
// Date:         2000-05-30
// Purpose:      Port of UCD-SNMP distribution to eCos.
// Description:  
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================
/********************************************************************
       Copyright 1989, 1991, 1992 by Carnegie Mellon University

			  Derivative Work -
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California

			 All Rights Reserved

Permission to use, copy, modify and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appears in all copies and
that both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU and The Regents of
the University of California not be used in advertising or publicity
pertaining to distribution of the software without specific written
permission.

CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*********************************************************************/
/*
 * snmp_client.c - a toolkit of common functions for an SNMP client.
 *
 */
/**********************************************************************
	Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/

#include <config.h>

#include <stdio.h>
#include <errno.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#if TIME_WITH_SYS_TIME
# ifdef WIN32
#  include <sys/timeb.h>
# else
#  include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#if HAVE_DMALLOC_H
#include <dmalloc.h>
#endif

#if HAVE_WINSOCK_H
#include <winsock.h>
#endif

#include "asn1.h"
#include "snmp.h"
#include "snmp_api.h"
#include "snmp_impl.h"
#include "snmp_client.h"
#include "mib.h"


#ifndef BSD4_3
#define BSD4_2
#endif

#ifndef FD_SET

typedef long	fd_mask;
#define NFDBITS	(sizeof(fd_mask) * NBBY)	/* bits per mask */

#define	FD_SET(n, p)	((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
#define	FD_CLR(n, p)	((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
#define	FD_ISSET(n, p)	((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
#define FD_ZERO(p)	memset((p), 0, sizeof(*(p)))
#endif

#define PARTY_MIB_BASE	 ".1.3.6.1.6.3.3.1.3.127.0.0.1.1"
#define CONTEXT_MIB_BASE ".1.3.6.1.6.3.3.1.4.127.0.0.1.1"


struct snmp_pdu *
snmp_pdu_create(int command)
{
    struct snmp_pdu *pdu;
    struct sockaddr_in *pduIp;

    pdu = (struct snmp_pdu *)calloc(1,sizeof(struct snmp_pdu));
    if (pdu) {
    pduIp = (struct sockaddr_in *)&(pdu->address);
    pdu->version		 = SNMP_DEFAULT_VERSION;
    pdu->command		 = command;
    pdu->errstat		 = SNMP_DEFAULT_ERRSTAT;
    pdu->errindex		 = SNMP_DEFAULT_ERRINDEX;
    pduIp->sin_addr.s_addr       = SNMP_DEFAULT_ADDRESS;
    pdu->securityNameLen	 = 0;
    pdu->contextNameLen		 = 0;
    pdu->reqid                   = snmp_get_next_reqid();
    pdu->msgid                   = snmp_get_next_msgid();
    }
    return pdu;

}


/*
 * Add a null variable with the requested name to the end of the list of
 * variables for this pdu.
 */
struct variable_list* snmp_add_null_var(struct snmp_pdu * pdu, 
					oid *name, 
					size_t name_length)
{
    return snmp_pdu_add_variable(pdu, name, name_length, ASN_NULL, NULL, 0);
}



int
snmp_synch_input(int op,
		 struct snmp_session *session,
		 int reqid,
		 struct snmp_pdu *pdu,
		 void *magic)
{
    struct synch_state *state = (struct synch_state *)magic;
    int rpt_type;

    if (reqid != state->reqid && pdu->command != SNMP_MSG_REPORT)
	return 0;

    state->waiting = 0;
    if (op == RECEIVED_MESSAGE) {
      if (pdu->command == SNMP_MSG_REPORT) {
#ifdef CYGPKG_SNMPAGENT_V3_SUPPORT
	rpt_type = snmpv3_get_report_type(pdu);
	if (SNMPV3_IGNORE_UNAUTH_REPORTS || 
	    rpt_type == SNMPERR_NOT_IN_TIME_WINDOW) 
	  state->waiting = 1;
	session->s_snmp_errno = rpt_type;
#else
	session->s_snmp_errno = SNMPERR_UNSUPPORTED_SEC_LEVEL;
#endif
	state->pdu = NULL;
	state->status = STAT_ERROR;
        SET_SNMP_ERROR(rpt_type);
      } else if (pdu->command == SNMP_MSG_RESPONSE) {
	/* clone the pdu to return to snmp_synch_response */
	state->pdu = snmp_clone_pdu(pdu);
	state->status = STAT_SUCCESS;
	session->s_snmp_errno = SNMPERR_SUCCESS;
      }
    } else if (op == TIMED_OUT){
	state->pdu		 = NULL;
	state->status		 = STAT_TIMEOUT;
	session->s_snmp_errno	 = SNMPERR_TIMEOUT;
        SET_SNMP_ERROR(SNMPERR_TIMEOUT);
    }

    return 1;
}


/*
 * Clone an SNMP variable data structure.
 * Sets pointers to structure private storage, or
 * allocates larger object identifiers and values as needed.
 *
 * Caller must make list association for cloned variable.
 *
 * Returns 0 if successful.
 */
int
snmp_clone_var(struct variable_list *var, struct variable_list *newvar)
{
    if (!newvar || !var) return 1;

    memmove(newvar, var, sizeof(struct variable_list));
    newvar->next_variable = 0; newvar->name = 0; newvar->val.string = 0;

    /*
     * Clone the object identifier and the value.
     * Allocate memory iff original will not fit into local storage.
     */
    if (snmp_set_var_objid(newvar, var->name, var->name_length))
        return 1;

    /* need a pointer and a length to copy a string value. */
    if (var->val.string && var->val_len) {
      if (var->val.string != &var->buf[0]){
        if (var->val_len <= sizeof(var->buf))
            newvar->val.string = newvar->buf;
        else {
            newvar->val.string = (u_char *)malloc(var->val_len);
            if (!newvar->val.string) return 1;
        }
        memmove(newvar->val.string, var->val.string, var->val_len);
      }
      else { /* fix the pointer to new local store */
        newvar->val.string = newvar->buf;
      }
    }
    else {
        newvar->val.string = 0; newvar->val_len = 0;
    }

    return 0;
}


/*
 * Possibly make a copy of source memory buffer.
 * Will reset destination pointer if source pointer is NULL.
 * Returns 0 if successful, 1 if memory allocation fails.
 */
int
snmp_clone_mem(void ** dstPtr, void * srcPtr, unsigned len)
{
    *dstPtr = 0;
    if (srcPtr){
        *dstPtr = malloc(len + 1);
        if (! *dstPtr){
            return 1;
        }
        memmove(*dstPtr, srcPtr, len);
	/* this is for those routines that expect 0-terminated strings!!!
	   someone should rather have called strdup
	*/
	((char *)*dstPtr)[len] = 0;
    }
    return 0;
}


/*
 * Creates and allocates a clone of the input PDU,
 * but does NOT copy the variables.
 * This function should be used with another function,
 * such as _copy_pdu_vars.
 *
 * Returns a pointer to the cloned PDU if successful.
 * Returns 0 if failure.
 */
static
struct snmp_pdu *
_clone_pdu_header(struct snmp_pdu *pdu)
{
    struct snmp_pdu *newpdu;

    newpdu = (struct snmp_pdu *)malloc(sizeof(struct snmp_pdu));
    if (!newpdu) return 0;
    memmove(newpdu, pdu, sizeof(struct snmp_pdu));

    /* reset copied pointers if copy fails */
    newpdu->variables = 0; newpdu->enterprise = 0; newpdu->community = 0;
    newpdu->securityEngineID = 0; newpdu->securityName = 0;
    newpdu->contextEngineID  = 0; newpdu->contextName  = 0;

    /* copy buffers individually. If any copy fails, all are freed. */
    if ( snmp_clone_mem((void **)&newpdu->enterprise, pdu->enterprise,
                                    sizeof(oid)*pdu->enterprise_length)
     ||  snmp_clone_mem((void **)&newpdu->community, pdu->community,
                                    pdu->community_len)
#ifdef SNMPERR_UNSUPPORTED_SEC_LEVEL
     ||  snmp_clone_mem((void **)&newpdu->contextEngineID, pdu->contextEngineID,
                                    pdu->contextEngineIDLen)
     ||  snmp_clone_mem((void **)&newpdu->securityEngineID, pdu->securityEngineID,
                                    pdu->securityEngineIDLen)
     ||  snmp_clone_mem((void **)&newpdu->contextName, pdu->contextName,
                                    pdu->contextNameLen)
     ||  snmp_clone_mem((void **)&newpdu->securityName, pdu->securityName,
                                    pdu->securityNameLen)
#endif
       )
    {
        snmp_free_pdu(newpdu); return 0;
    }
    return newpdu;
}


/*
 * Copy some or all variables from source PDU to target PDU.
 * This function consolidates many of the needs of PDU variables:
 * Clone PDU : copy all the variables.
 * Split PDU : skip over some variables to copy other variables.
 * Fix PDU   : remove variable associated with error index.
 *
 * Designed to work with _clone_pdu_header.
 *
 * If drop_err is set, drop any variable associated with errindex.
 * If skip_count is set, skip the number of variable in pdu's list.
 * While copy_count is greater than zero, copy pdu variables to newpdu.
 *
 * If an error occurs, newpdu is freed and pointer is set to 0.
 *
 * Returns a pointer to the cloned PDU if successful.
 * Returns 0 if failure.
 */

⌨️ 快捷键说明

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