#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include "constants.h"
#include "field.h"
#include "log.h"
#include "util.h"
static char *field_debug_raw(u_int8_t *, size_t, struct constant_map **);
static char *field_debug_num(u_int8_t *, size_t, struct constant_map **);
static char *field_debug_mask(u_int8_t *, size_t, struct constant_map **);
static char *field_debug_ign(u_int8_t *, size_t, struct constant_map **);
static char *field_debug_cst(u_int8_t *, size_t, struct constant_map **);
static char *(*decode_field[]) (u_int8_t *, size_t,
struct constant_map **) = {
field_debug_raw,
field_debug_num,
field_debug_mask,
field_debug_ign,
field_debug_cst
};
static char *
field_debug_raw(u_int8_t *buf, size_t len, struct constant_map **maps)
{
return raw2hex(buf, len);
}
static int
extract_val(u_int8_t *buf, size_t len, u_int32_t *val)
{
switch (len) {
case 1:
*val = *buf;
break;
case 2:
*val = decode_16(buf);
break;
case 4:
*val = decode_32(buf);
break;
default:
return -1;
}
return 0;
}
static char *
field_debug_num(u_int8_t *buf, size_t len, struct constant_map **maps)
{
char *retval;
u_int32_t val;
if (extract_val(buf, len, &val))
return NULL;
if (asprintf(&retval, "%u", val) == -1)
return NULL;
return retval;
}
static char *
field_debug_mask(u_int8_t *buf, size_t len, struct constant_map **maps)
{
u_int32_t val;
u_int32_t bit;
char *retval, *new_buf, *name;
size_t buf_sz;
if (extract_val(buf, len, &val))
return NULL;
buf_sz = 4;
retval = malloc(buf_sz);
if (!retval)
return NULL;
strlcpy(retval, "[ ", buf_sz);
for (bit = 1; bit; bit <<= 1) {
if (val & bit) {
name = constant_name_maps(maps, bit);
buf_sz += strlen(name) + 1;
new_buf = realloc(retval, buf_sz);
if (!new_buf) {
free(retval);
return NULL;
}
retval = new_buf;
strlcat(retval, name, buf_sz);
strlcat(retval, " ", buf_sz);
}
}
strlcat(retval, "]", buf_sz);
return retval;
}
static char *
field_debug_ign(u_int8_t *buf, size_t len, struct constant_map **maps)
{
return NULL;
}
static char *
field_debug_cst(u_int8_t *buf, size_t len, struct constant_map **maps)
{
u_int32_t val;
if (extract_val(buf, len, &val))
return NULL;
return strdup(constant_name_maps(maps, val));
}
void
field_dump_field(struct field *f, u_int8_t *buf)
{
char *value;
value = decode_field[(int) f->type] (buf + f->offset, f->len, f->maps);
if (value) {
LOG_DBG((LOG_MESSAGE, 70, "%s: %s", f->name, value));
free(value);
}
}
void
field_dump_payload(struct field *fields, u_int8_t *buf)
{
struct field *field;
for (field = fields; field->name; field++)
field_dump_field(field, buf);
}
u_int32_t
field_get_num(struct field *f, u_int8_t *buf)
{
u_int32_t val;
if (extract_val(buf + f->offset, f->len, &val))
return 0;
return val;
}
void
field_set_num(struct field *f, u_int8_t *buf, u_int32_t val)
{
switch (f->len) {
case 1:
buf[f->offset] = val;
break;
case 2:
encode_16(buf + f->offset, val);
break;
case 4:
encode_32(buf + f->offset, val);
break;
}
}
void
field_get_raw(struct field *f, u_int8_t *buf, u_int8_t *val)
{
memcpy(val, buf + f->offset, f->len);
}
void
field_set_raw(struct field *f, u_int8_t *buf, u_int8_t *val)
{
memcpy(buf + f->offset, val, f->len);
}