예제 #1
0
파일: snmp.c 프로젝트: AsherBond/collectd
static int csnmp_strvbcopy (char *dst, /* {{{ */
    const struct variable_list *vb, size_t dst_size)
{
  char *src;
  size_t num_chars;
  size_t i;

  if (vb->type == ASN_OCTET_STR)
    src = (char *) vb->val.string;
  else if (vb->type == ASN_BIT_STR)
    src = (char *) vb->val.bitstring;
  else
  {
    dst[0] = 0;
    return (EINVAL);
  }

  num_chars = dst_size - 1;
  if (num_chars > vb->val_len)
    num_chars = vb->val_len;

  for (i = 0; i < num_chars; i++)
  {
    /* Check for control characters. */
    if ((unsigned char)src[i] < 32)
      return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
    dst[i] = src[i];
  }
  dst[num_chars] = 0;

  return ((int) vb->val_len);
} /* }}} int csnmp_strvbcopy */
예제 #2
0
파일: snmp.c 프로젝트: Mindera/collectd
/* csnmp_strvbcopy copies the octet string or bit string contained in vb to
 * dst. If non-printable characters are detected, it will switch to a hex
 * representation of the string. Returns zero on success, EINVAL if vb does not
 * contain a string and ENOMEM if dst is not large enough to contain the
 * string. */
static int csnmp_strvbcopy (char *dst, /* {{{ */
    const struct variable_list *vb, size_t dst_size)
{
  char *src;
  size_t num_chars;
  size_t i;

  if (vb->type == ASN_OCTET_STR)
    src = (char *) vb->val.string;
  else if (vb->type == ASN_BIT_STR)
    src = (char *) vb->val.bitstring;
  else if (vb->type == ASN_IPADDRESS)
  {
    return ssnprintf (dst, dst_size, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"",
          (uint8_t) vb->val.string[0],
          (uint8_t) vb->val.string[1],
          (uint8_t) vb->val.string[2],
          (uint8_t) vb->val.string[3]);
  }
  else
  {
    dst[0] = 0;
    return (EINVAL);
  }

  num_chars = dst_size - 1;
  if (num_chars > vb->val_len)
    num_chars = vb->val_len;

  for (i = 0; i < num_chars; i++)
  {
    /* Check for control characters. */
    if ((unsigned char)src[i] < 32)
      return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
    dst[i] = src[i];
  }
  dst[num_chars] = 0;
  dst[dst_size - 1] = 0;

  if (dst_size <= vb->val_len)
    return ENOMEM;

  return 0;
} /* }}} int csnmp_strvbcopy */