1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/* $OpenBSD: field.c,v 1.16 2004/06/14 09:55:41 ho Exp $ */
/* $EOM: field.c,v 1.11 2000/02/20 19:58:37 niklas Exp $ */
/*
* Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sysdep.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 **);
/* Contents must match the enum in struct field. */
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
};
/*
* Return a string showing the hexadecimal contents of the LEN-sized buffer
* BUF. MAPS should be zero and is only here because the API requires it.
*/
static char *
field_debug_raw(u_int8_t *buf, size_t len, struct constant_map **maps)
{
char *retval, *p;
if (len == 0)
return 0;
retval = malloc(3 + len * 2);
if (!retval)
return 0;
strlcpy(retval, "0x", 3 + len * 2);
p = retval + 2;
for (; len > 0; len--) {
snprintf(p, 1 + len * 2, "%02x", *buf++);
p += 2;
}
return retval;
}
/*
* Convert the unsigned LEN-sized number at BUF of network byteorder to a
* 32-bit unsigned integer of host byteorder pointed to by VAL.
*/
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;
}
/*
* Return a textual representation of the unsigned number pointed to by BUF
* which is LEN octets long. MAPS should be zero and is only here because
* the API requires it.
*/
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 0;
/* 3 decimal digits are enough to represent each byte. */
retval = malloc(3 * len);
snprintf(retval, 3 * len, "%u", val);
return retval;
}
/*
* Return the symbolic names of the flags pointed to by BUF which is LEN
* octets long, using the constant maps MAPS.
*/
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 0;
/* Size for brackets, two spaces and a NUL terminator. */
buf_sz = 4;
retval = malloc(buf_sz);
if (!retval)
return 0;
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 0;
}
retval = new_buf;
strlcat(retval, name, buf_sz);
strlcat(retval, " ", buf_sz);
}
}
strlcat(retval, "]", buf_sz);
return retval;
}
/*
* Just a dummy needed to skip the unused LEN sized space at BUF. MAPS
* should be zero and is only here because the API requires it.
*/
static char *
field_debug_ign(u_int8_t *buf, size_t len, struct constant_map **maps)
{
return 0;
}
/*
* Return the symbolic name of a constant pointed to by BUF which is LEN
* octets long, using the constant maps MAPS.
*/
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 0;
return strdup(constant_name_maps(maps, val));
}
/* Pretty-print a field from BUF as described by F. */
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);
}
}
/* Pretty-print all the fields of BUF as described in FIELDS. */
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);
}
/* Return the numeric value of the field F of 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;
}
/* Stash the number VAL into BUF's field F. */
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;
}
}
/* Stash BUF's raw field F into VAL. */
void
field_get_raw(struct field *f, u_int8_t *buf, u_int8_t *val)
{
memcpy(val, buf + f->offset, f->len);
}
/* Stash the buffer VAL into BUF's field F. */
void
field_set_raw(struct field *f, u_int8_t *buf, u_int8_t *val)
{
memcpy(buf + f->offset, val, f->len);
}
|