adden new isakmpd
[anytun.git] / keyexchange / isakmpd-20041012 / field.c
blob0cc96d2748d519ac094aadd81e0d630530def118
1 /* $OpenBSD: field.c,v 1.16 2004/06/14 09:55:41 ho Exp $ */
2 /* $EOM: field.c,v 1.11 2000/02/20 19:58:37 niklas Exp $ */
4 /*
5 * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
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.
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.
29 * This code was written under funding by Ericsson Radio Systems.
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "sysdep.h"
38 #include "constants.h"
39 #include "field.h"
40 #include "log.h"
41 #include "util.h"
43 static char *field_debug_raw(u_int8_t *, size_t, struct constant_map **);
44 static char *field_debug_num(u_int8_t *, size_t, struct constant_map **);
45 static char *field_debug_mask(u_int8_t *, size_t, struct constant_map **);
46 static char *field_debug_ign(u_int8_t *, size_t, struct constant_map **);
47 static char *field_debug_cst(u_int8_t *, size_t, struct constant_map **);
49 /* Contents must match the enum in struct field. */
50 static char *(*decode_field[]) (u_int8_t *, size_t,
51 struct constant_map **) = {
52 field_debug_raw,
53 field_debug_num,
54 field_debug_mask,
55 field_debug_ign,
56 field_debug_cst
60 * Return a string showing the hexadecimal contents of the LEN-sized buffer
61 * BUF. MAPS should be zero and is only here because the API requires it.
63 static char *
64 field_debug_raw(u_int8_t *buf, size_t len, struct constant_map **maps)
66 char *retval, *p;
68 if (len == 0)
69 return 0;
70 retval = malloc(3 + len * 2);
71 if (!retval)
72 return 0;
73 strlcpy(retval, "0x", 3 + len * 2);
74 p = retval + 2;
75 for (; len > 0; len--) {
76 snprintf(p, 1 + len * 2, "%02x", *buf++);
77 p += 2;
79 return retval;
83 * Convert the unsigned LEN-sized number at BUF of network byteorder to a
84 * 32-bit unsigned integer of host byteorder pointed to by VAL.
86 static int
87 extract_val(u_int8_t *buf, size_t len, u_int32_t *val)
89 switch (len) {
90 case 1:
91 *val = *buf;
92 break;
93 case 2:
94 *val = decode_16(buf);
95 break;
96 case 4:
97 *val = decode_32(buf);
98 break;
99 default:
100 return -1;
102 return 0;
106 * Return a textual representation of the unsigned number pointed to by BUF
107 * which is LEN octets long. MAPS should be zero and is only here because
108 * the API requires it.
110 static char *
111 field_debug_num(u_int8_t *buf, size_t len, struct constant_map **maps)
113 char *retval;
114 u_int32_t val;
116 if (extract_val(buf, len, &val))
117 return 0;
118 /* 3 decimal digits are enough to represent each byte. */
119 retval = malloc(3 * len);
120 snprintf(retval, 3 * len, "%u", val);
121 return retval;
125 * Return the symbolic names of the flags pointed to by BUF which is LEN
126 * octets long, using the constant maps MAPS.
128 static char *
129 field_debug_mask(u_int8_t *buf, size_t len, struct constant_map **maps)
131 u_int32_t val;
132 u_int32_t bit;
133 char *retval, *new_buf, *name;
134 size_t buf_sz;
136 if (extract_val(buf, len, &val))
137 return 0;
139 /* Size for brackets, two spaces and a NUL terminator. */
140 buf_sz = 4;
141 retval = malloc(buf_sz);
142 if (!retval)
143 return 0;
145 strlcpy(retval, "[ ", buf_sz);
146 for (bit = 1; bit; bit <<= 1) {
147 if (val & bit) {
148 name = constant_name_maps(maps, bit);
149 buf_sz += strlen(name) + 1;
150 new_buf = realloc(retval, buf_sz);
151 if (!new_buf) {
152 free(retval);
153 return 0;
155 retval = new_buf;
156 strlcat(retval, name, buf_sz);
157 strlcat(retval, " ", buf_sz);
160 strlcat(retval, "]", buf_sz);
161 return retval;
165 * Just a dummy needed to skip the unused LEN sized space at BUF. MAPS
166 * should be zero and is only here because the API requires it.
168 static char *
169 field_debug_ign(u_int8_t *buf, size_t len, struct constant_map **maps)
171 return 0;
175 * Return the symbolic name of a constant pointed to by BUF which is LEN
176 * octets long, using the constant maps MAPS.
178 static char *
179 field_debug_cst(u_int8_t *buf, size_t len, struct constant_map **maps)
181 u_int32_t val;
183 if (extract_val(buf, len, &val))
184 return 0;
186 return strdup(constant_name_maps(maps, val));
189 /* Pretty-print a field from BUF as described by F. */
190 void
191 field_dump_field(struct field *f, u_int8_t *buf)
193 char *value;
195 value = decode_field[(int) f->type] (buf + f->offset, f->len, f->maps);
196 if (value) {
197 LOG_DBG((LOG_MESSAGE, 70, "%s: %s", f->name, value));
198 free(value);
202 /* Pretty-print all the fields of BUF as described in FIELDS. */
203 void
204 field_dump_payload(struct field *fields, u_int8_t *buf)
206 struct field *field;
208 for (field = fields; field->name; field++)
209 field_dump_field(field, buf);
212 /* Return the numeric value of the field F of BUF. */
213 u_int32_t
214 field_get_num(struct field *f, u_int8_t *buf)
216 u_int32_t val;
218 if (extract_val(buf + f->offset, f->len, &val))
219 return 0;
220 return val;
223 /* Stash the number VAL into BUF's field F. */
224 void
225 field_set_num(struct field *f, u_int8_t *buf, u_int32_t val)
227 switch (f->len) {
228 case 1:
229 buf[f->offset] = val;
230 break;
231 case 2:
232 encode_16(buf + f->offset, val);
233 break;
234 case 4:
235 encode_32(buf + f->offset, val);
236 break;
240 /* Stash BUF's raw field F into VAL. */
241 void
242 field_get_raw(struct field *f, u_int8_t *buf, u_int8_t *val)
244 memcpy(val, buf + f->offset, f->len);
247 /* Stash the buffer VAL into BUF's field F. */
248 void
249 field_set_raw(struct field *f, u_int8_t *buf, u_int8_t *val)
251 memcpy(buf + f->offset, val, f->len);