Exemplo n.º 1
0
int
smux_trap (oid *name, size_t namelen,
	   oid *iname, size_t inamelen,
	   struct trap_object *trapobj, size_t trapobjlen,
	   unsigned int tick, u_char sptrap)
{
  unsigned int i;
  u_char buf[BUFSIZ];
  u_char *ptr;
  size_t len, length;
  struct in_addr addr;
  unsigned long val;
  u_char *h1, *h1e;

  ptr = buf;
  len = BUFSIZ;
  length = len;

  /* When SMUX connection is not established. */
  if (smux_sock < 0)
    return 0;

  /* SMUX header. */
  ptr = asn_build_header (ptr, &len, (u_char) SMUX_TRAP, 0);

  /* Sub agent enterprise oid. */
  ptr = asn_build_objid (ptr, &len,
			 (u_char) 
			 (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
			 smux_oid, smux_oid_len);

  /* IP address. */
  addr.s_addr = 0;
  ptr = asn_build_string (ptr, &len, 
			  (u_char)
			  (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_IPADDRESS),
			  (u_char *)&addr, sizeof (addr));

  /* Generic trap integer. */
  val = SNMP_TRAP_ENTERPRISESPECIFIC;
  ptr = asn_build_int (ptr, &len, 
		       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       &val, sizeof (val));

  /* Specific trap integer. */
  val = sptrap;
  ptr = asn_build_int (ptr, &len, 
		       (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
		       &val, sizeof (val));

  /* Timeticks timestamp. */
  val = 0;
  ptr = asn_build_unsigned_int (ptr, &len, 
				(u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_TIMETICKS),
				&val, sizeof (val));
  
  /* Variables. */
  h1 = ptr;
  ptr = asn_build_sequence (ptr, &len, 
			    (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
			    0);


  /* Iteration for each objects. */
  h1e = ptr;
  for (i = 0; i < trapobjlen; i++)
    {
      int ret;
      oid oid[MAX_OID_LEN];
      size_t oid_len;
      void *val;
      size_t val_len;
      u_char val_type;

      /* Make OID. */
      if (trapobj[i].namelen > 0) 
        {
          oid_copy (oid, name, namelen);
          oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen);
          oid_copy (oid + namelen + trapobj[i].namelen, iname, inamelen);
          oid_len = namelen + trapobj[i].namelen + inamelen;
        }
      else 
        {
          oid_copy (oid, name, namelen);
          oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen * (-1));
          oid_len = namelen + trapobj[i].namelen * (-1) ;
        }

      if (debug_smux) 
        {
          smux_oid_dump ("Trap", name, namelen);
          if (trapobj[i].namelen < 0)
            smux_oid_dump ("Trap", 
                           trapobj[i].name, (- 1) * (trapobj[i].namelen));
          else 
            {
              smux_oid_dump ("Trap", trapobj[i].name, (trapobj[i].namelen));
              smux_oid_dump ("Trap", iname, inamelen);
            }
          smux_oid_dump ("Trap", oid, oid_len);
          zlog_info ("BUFSIZ: %d // oid_len: %d", BUFSIZ, oid_len);
      }

      ret = smux_get (oid, &oid_len, 1, &val_type, &val, &val_len);

      if (debug_smux)
	zlog_debug ("smux_get result %d", ret);

      if (ret == 0)
	ptr = snmp_build_var_op (ptr, oid, &oid_len,
				 val_type, val_len, val, &len);
    }

  /* Now variable size is known, fill in size */
  asn_build_sequence(h1, &length,
		     (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
		     ptr - h1e);

  /* Fill in size of whole sequence */
  len = BUFSIZ;
  asn_build_header (buf, &len, (u_char) SMUX_TRAP, (ptr - buf) - 2);

  return send (smux_sock, buf, (ptr - buf), 0);
}
Exemplo n.º 2
0
u_char         *
_docsis_snmp_build_var_op(u_char * data,
                          oid * var_name,
                          size_t * var_name_len,
                          u_char var_val_type,
                          size_t var_val_len,
                          u_char * var_val,
                          size_t * listlength)
{
    size_t          dummyLen, headerLen;
    u_char         *dataPtr;

    dummyLen = *listlength;
    dataPtr = data;
#if 0
    data = asn_build_sequence(data, &dummyLen,
                              (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
                              0);
    if (data == NULL) {
        return NULL;
    }
#endif
    if (dummyLen < 2)
        return NULL;
    data += 2;
    dummyLen -= 2;

    headerLen = data - dataPtr;
    *listlength -= headerLen;
    DEBUGDUMPHEADER("send", "Name");
    data = asn_build_objid(data, listlength,
                           (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE |
                                     ASN_OBJECT_ID), var_name,
                           *var_name_len);
    DEBUGINDENTLESS();
    if (data == NULL) {
        ERROR_MSG("Can't build OID for variable");
        return NULL;
    }
    DEBUGDUMPHEADER("send", "Value");
    switch (var_val_type) {
    case ASN_INTEGER:
        data = asn_build_int(data, listlength, var_val_type,
                             (long *) var_val, var_val_len);
        break;
    case ASN_GAUGE:
    case ASN_COUNTER:
    case ASN_TIMETICKS:
    case ASN_UINTEGER:
        data = asn_build_unsigned_int(data, listlength, var_val_type,
                                      (u_long *) var_val, var_val_len);
        break;
#ifdef OPAQUE_SPECIAL_TYPES
    case ASN_OPAQUE_COUNTER64:
    case ASN_OPAQUE_U64:
#endif
    case ASN_COUNTER64:
        data = asn_build_unsigned_int64(data, listlength, var_val_type,
                                        (struct counter64 *) var_val,
                                        var_val_len);
        break;
    case ASN_OCTET_STR:
    case ASN_IPADDRESS:
    case ASN_OPAQUE:
    case ASN_NSAP:
        data = asn_build_string(data, listlength, var_val_type,
                                var_val, var_val_len);
        break;
    case ASN_OBJECT_ID:
        data = asn_build_objid(data, listlength, var_val_type,
                               (oid *) var_val, var_val_len / sizeof(oid));
        break;
    case ASN_NULL:
        data = asn_build_null(data, listlength, var_val_type);
        break;
    case ASN_BIT_STR:
        data = asn_build_bitstring(data, listlength, var_val_type,
                                   var_val, var_val_len);
        break;
    case SNMP_NOSUCHOBJECT:
    case SNMP_NOSUCHINSTANCE:
    case SNMP_ENDOFMIBVIEW:
        data = asn_build_null(data, listlength, var_val_type);
        break;
#ifdef OPAQUE_SPECIAL_TYPES
    case ASN_OPAQUE_FLOAT:
        data = asn_build_float(data, listlength, var_val_type,
                               (float *) var_val, var_val_len);
        break;
    case ASN_OPAQUE_DOUBLE:
        data = asn_build_double(data, listlength, var_val_type,
                                (double *) var_val, var_val_len);
        break;
    case ASN_OPAQUE_I64:
        data = asn_build_signed_int64(data, listlength, var_val_type,
                                      (struct counter64 *) var_val,
                                      var_val_len);
        break;
#endif                          /* OPAQUE_SPECIAL_TYPES */
    default:
        ERROR_MSG("wrong type");
        return NULL;
    }
    DEBUGINDENTLESS();
    if (data == NULL) {
        ERROR_MSG("Can't build value");
        return NULL;
    }
    dummyLen = (data - dataPtr) - headerLen;

    _docsis_asn_build_sequence(dataPtr, &dummyLen,
                       (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
                       dummyLen);
    return data;
}
Exemplo n.º 3
0
/* Build a variable binding.
 *
 * RFC 1905: Protocol Operations for SNMPv2
 *
 * VarBind ::= 
 *   SEQUENCE {
 *     name ObjectName
 *     CHOICE {
 *       value ObjectSyntax
 *       unSpecified NULL
 *       noSuchObject[0] NULL
 *       noSuchInstance[1] NULL
 *       endOfMibView[2] NULL
 *     }
 *   }
 */
u_char *
snmp_var_EncodeVarBind(u_char * Buffer, int *BufLenP,
    variable_list * VarList,
    int Version)
{
    struct variable_list *Vars;
    u_char *bufp;
    u_char *HeaderStart;
    u_char *HeaderEnd;
    int FakeArg = *BufLenP;

    bufp = Buffer;

    for (Vars = VarList; Vars; Vars = Vars->next_variable) {

	/* Build the header for this variable
	 *
	 * Use Maximum size.
	 */
	HeaderStart = bufp;
	HeaderEnd = asn_build_header(HeaderStart, BufLenP,
	    (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
	    FakeArg);
	if (HeaderEnd == NULL)
	    return (NULL);

	/* Now, let's put the Object Identifier into the buffer */
	bufp = asn_build_objid(HeaderEnd, BufLenP,
	    (u_char) (ASN_UNIVERSAL |
		ASN_PRIMITIVE |
		ASN_OBJECT_ID),
	    Vars->name, Vars->name_length);
	if (bufp == NULL)
	    return (NULL);

	/* Now put the data in */
	switch (Vars->type) {

	case ASN_INTEGER:
	    bufp = asn_build_int(bufp,
		BufLenP, Vars->type,
		(int *) Vars->val.integer, Vars->val_len);
	    break;

	case SMI_COUNTER32:
	case SMI_GAUGE32:
	    /*  case SMI_UNSIGNED32: */
	case SMI_TIMETICKS:
	    bufp = asn_build_unsigned_int(bufp, BufLenP,
		Vars->type,
		(u_int *) Vars->val.integer, Vars->val_len);
	    break;

	case ASN_OCTET_STR:
	case SMI_IPADDRESS:
	case SMI_OPAQUE:
	    bufp = asn_build_string(bufp, BufLenP, Vars->type,
		Vars->val.string, Vars->val_len);
	    break;

	case ASN_OBJECT_ID:
	    bufp = asn_build_objid(bufp, BufLenP, Vars->type,
		(oid *) Vars->val.objid, Vars->val_len / sizeof(oid));
	    break;

	case SMI_NOSUCHINSTANCE:
	case SMI_NOSUCHOBJECT:
	case SMI_ENDOFMIBVIEW:
	    if (Version == SNMP_VERSION_1) {
		/* SNMP Version 1 does not support these error codes. */
		bufp = asn_build_null(bufp, BufLenP, SMI_NOSUCHOBJECT);
	    } else {
		bufp = asn_build_exception(bufp, BufLenP, Vars->type);
	    }
	    break;

	case ASN_NULL:
	    bufp = asn_build_null(bufp, BufLenP, Vars->type);
	    break;

	case SMI_COUNTER64:
	    snmplib_debug(2, "Unable to encode type SMI_COUNTER64!\n");
	    /* Fall through */

	default:
	    snmp_set_api_error(SNMPERR_UNSUPPORTED_TYPE);
	    return (NULL);
	}

	/* ASSERT:  bufp should now point to the next valid byte. */
	if (bufp == NULL)
	    return (NULL);

	/* Rebuild the header with the appropriate length */
	HeaderEnd = asn_build_header(HeaderStart, &FakeArg,
	    (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
	    (bufp - HeaderEnd));

	/* Returns NULL */
	if (HeaderEnd == NULL)
	    return (NULL);

    }

    /* or the end of the entire thing */
    return (bufp);
}
Exemplo n.º 4
0
/* Build a variable binding.
 *
 * RFC 1905: Protocol Operations for SNMPv2
 *
 * VarBind ::= 
 *   SEQUENCE {
 *     name ObjectName
 *     CHOICE {
 *       value ObjectSyntax
 *       unSpecified NULL
 *       noSuchObject[0] NULL
 *       noSuchInstance[1] NULL
 *       endOfMibView[2] NULL
 *     }
 *   }
 */
u_char *snmp_var_EncodeVarBind(u_char *Buffer, int *BufLenP,
			       struct variable_list *VarList,
			       int Version)
{
  struct variable_list *Vars;
  u_char *bufp;
  u_char *HeaderStart;
  u_char *HeaderEnd;
  int FakeArg = *BufLenP;
#ifdef DEBUG_VARS_ENCODE
  int StartLen = *BufLenP;
  int Counter = 1;
#endif

  bufp = Buffer;

#ifdef DEBUG_VARS_ENCODE
  printf("VARS: Encoding Variable list into buffer at 0x%x.\n", Buffer);
#endif

  for (Vars=VarList; Vars; Vars=Vars->next_variable) {

#ifdef DEBUG_VARS_ENCODE
    printf("VARS %d: Encoding Variable 0x%x.\n", Counter, Vars);
    printf("VARS %d: Starting at 0x%x (%d bytes left)\n", 
	   Counter, bufp, *BufLenP);
#endif

    /* Build the header for this variable
     *
     * Use Maximum size.
     */
    HeaderStart = bufp;
    HeaderEnd = asn_build_header(HeaderStart, BufLenP,
				 (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 
				 FakeArg);
    if (HeaderEnd == NULL)
      return(NULL);

#ifdef DEBUG_VARS_ENCODE
    printf("VARS %d: Encoding Object Identifier 0x%x (%d bytes) at 0x%x (%d bytes left)\n", 
	   Counter, Vars,
	   Vars->name_length, HeaderEnd, *BufLenP);
    print_oid(Vars->name, Vars->name_length),
#endif
    /* Now, let's put the Object Identifier into the buffer */
    bufp = asn_build_objid(HeaderEnd, BufLenP,
			   (u_char)(ASN_UNIVERSAL | 
				    ASN_PRIMITIVE | 
				    ASN_OBJECT_ID),
			   Vars->name, Vars->name_length);
    if (bufp == NULL)
      return(NULL);

    /* Now put the data in */
    switch(Vars->type) {

    case ASN_INTEGER:
#ifdef DEBUG_VARS_ENCODE
      printf("VARS %d: Encoding Integer %d at 0x%x\n", Counter,
	     *(Vars->val.integer), bufp);
#endif

      bufp = asn_build_int(bufp, 
			   BufLenP, Vars->type,
			   (int *)Vars->val.integer, Vars->val_len);
      break;

    case SMI_COUNTER32:
    case SMI_GAUGE32:
      /*  case SMI_UNSIGNED32: */
    case SMI_TIMETICKS:
#ifdef DEBUG_VARS_ENCODE
      printf("VARS %d: Encoding Timeticks %d at 0x%x\n", Counter,
	     *(Vars->val.integer), bufp);
#endif
      bufp = asn_build_unsigned_int(bufp, BufLenP, 
				    Vars->type,
				    (u_int *)Vars->val.integer, Vars->val_len);
      break;

    case ASN_OCTET_STR:
    case SMI_IPADDRESS:
    case SMI_OPAQUE:
#ifdef DEBUG_VARS_ENCODE
      printf("VARS %d: Encoding String %s (%d bytes) at 0x%x\n", Counter,
	     (Vars->val.string), Vars->val_len, bufp);
#endif
      bufp = asn_build_string(bufp, BufLenP, Vars->type,
			      Vars->val.string, Vars->val_len);
      break;

    case ASN_OBJECT_ID:
#ifdef DEBUG_VARS_ENCODE
      printf("VARS %d: Encoding Object Identifier (%d bytes) at 0x%x\n",
	     Counter,
	     Vars->val_len, bufp);
#endif
      bufp = asn_build_objid(bufp, BufLenP, Vars->type,
			     (oid *)Vars->val.objid, Vars->val_len / sizeof(oid));
      break;

    case SMI_NOSUCHINSTANCE:
    case SMI_NOSUCHOBJECT:
    case SMI_ENDOFMIBVIEW:

#ifdef DEBUG_VARS_ENCODE
      printf("VARS %d: Encoding NULL at 0x%x\n", Counter, bufp);
#endif
      if (Version == SNMP_VERSION_1) {
        /* SNMP Version 1 does not support these error codes. */
	bufp = asn_build_null(bufp, BufLenP, SMI_NOSUCHOBJECT);
      } else {
	bufp = asn_build_exception(bufp, BufLenP, Vars->type);
      }
      break;

    case ASN_NULL:
#ifdef DEBUG_VARS_ENCODE
      printf("VARS %d: Encoding NULL at 0x%x\n", Counter, bufp);
#endif
      bufp = asn_build_null(bufp, BufLenP, Vars->type);
      break;

    case SMI_COUNTER64:
#ifdef STDERR_OUTPUT
      fprintf(stderr, WIDE("Unable to encode type SMI_COUNTER64!\n"));
#endif
      /* Fall through */

    default:
      snmp_set_api_error(SNMPERR_UNSUPPORTED_TYPE);
      return(NULL);
    }

    /* ASSERT:  bufp should now point to the next valid byte. */
    if (bufp == NULL)
      return(NULL);

    /* Rebuild the header with the appropriate length */
#ifdef DEBUG_VARS_ENCODE
    printf("VARS %d: Resetting length to %d at 0x%x (%d bytes left)\n",
	   Counter,
	   (bufp - HeaderEnd), HeaderStart, *BufLenP);
#endif
    HeaderEnd = asn_build_header(HeaderStart, &FakeArg,
				 (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 
				 (bufp - HeaderEnd));

    /* Returns NULL */
    if (HeaderEnd == NULL)
      return(NULL);

#ifdef DEBUG_VARS_ENCODE
    Counter++;
#endif
  }

#ifdef DEBUG_VARS_ENCODE
  printf("VARS: Variable list of %d vars takes up %d bytes.\n",
	 --Counter, StartLen - *BufLenP);
#endif

  /* or the end of the entire thing */
  return(bufp);
}
Exemplo n.º 5
0
u_char *snmp_pdu_encode(u_char *DestBuf, int *DestBufLen,
			struct snmp_pdu *PDU)
{
  u_char *bufp;

#ifdef DEBUG_PDU_ENCODE
  printf("PDU: Encoding %d\n", PDU->command);
#endif

  /* ASN.1 Header */
  switch (PDU->command) {

    /**********************************************************************/

  case TRP_REQ_MSG:

    /* SNMPv1 Trap */

    /* enterprise */
    bufp = asn_build_objid(DestBuf, DestBufLen,
			   (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
			   (oid *)PDU->enterprise, PDU->enterprise_length);
    if (bufp == NULL)
      return(NULL);

    /* agent-addr */
    bufp = asn_build_string(bufp, DestBufLen,
			    (u_char)(SMI_IPADDRESS | ASN_PRIMITIVE),
			    (u_char *)&PDU->agent_addr.sin_addr.s_addr,
			    sizeof(PDU->agent_addr.sin_addr.s_addr));
    if (bufp == NULL)
      return(NULL);

    /* generic trap */
    bufp = asn_build_int(bufp, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 (int *)&PDU->trap_type, sizeof(PDU->trap_type));
    if (bufp == NULL)
      return(NULL);

    /* specific trap */
    bufp = asn_build_int(bufp, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 (int *)&PDU->specific_type, 
			 sizeof(PDU->specific_type));
    if (bufp == NULL)
      return(NULL);

    /* timestamp */
    bufp = asn_build_unsigned_int(bufp, DestBufLen,
				  (u_char)(SMI_TIMETICKS | ASN_PRIMITIVE),
				  &PDU->time, sizeof(PDU->time));
    if (bufp == NULL)
      return(NULL);
    break;

    /**********************************************************************/

  case SNMP_PDU_GETBULK:

    /* SNMPv2 Bulk Request */

    /* request id */
    bufp = asn_build_int(DestBuf, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 &PDU->reqid, sizeof(PDU->reqid));
    if (bufp == NULL)
      return(NULL);

    /* non-repeaters */
    bufp = asn_build_int(bufp, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 &PDU->non_repeaters, 
			 sizeof(PDU->non_repeaters));
    if (bufp == NULL)
      return(NULL);

    /* max-repetitions */
    bufp = asn_build_int(bufp, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 &PDU->max_repetitions,
			 sizeof(PDU->max_repetitions));
    if (bufp == NULL)
      return(NULL);
    break;

    /**********************************************************************/

  default:

    /* Normal PDU Encoding */

    /* request id */
#ifdef DEBUG_PDU_ENCODE
    printf("PDU: Request ID %d (0x%x)\n", PDU->reqid, DestBuf);
#endif
    bufp = asn_build_int(DestBuf, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 &PDU->reqid, sizeof(PDU->reqid));
    if (bufp == NULL)
      return(NULL);

    /* error status */
#ifdef DEBUG_PDU_ENCODE
    printf("PDU: Error Status %d (0x%x)\n", PDU->errstat, bufp);
#endif
    bufp = asn_build_int(bufp, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 &PDU->errstat, sizeof(PDU->errstat));
    if (bufp == NULL)
      return(NULL);

    /* error index */
#ifdef DEBUG_PDU_ENCODE
    printf("PDU: Error index %d (0x%x)\n", PDU->errindex, bufp);
#endif
    bufp = asn_build_int(bufp, DestBufLen,
			 (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER),
			 &PDU->errindex, sizeof(PDU->errindex));
    if (bufp == NULL)
      return(NULL);
    break;
  } /* End of encoding */

  return(bufp);
}