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