4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2001 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <ftypes-int.h>
30 #include <epan/addr_resolv.h>
31 #include <epan/strutil.h>
32 #include <epan/oids.h>
36 #define CMP_MATCHES cmp_matches
38 #define CMP_MATCHES NULL
45 bytes_fvalue_new(fvalue_t *fv)
47 fv->value.bytes = NULL;
51 bytes_fvalue_free(fvalue_t *fv)
53 if (fv->value.bytes) {
54 g_byte_array_free(fv->value.bytes, TRUE);
61 bytes_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
63 g_assert(already_copied);
65 /* Free up the old value, if we have one */
66 bytes_fvalue_free(fv);
68 fv->value.bytes = value;
72 bytes_repr_len(fvalue_t *fv, ftrepr_t rtype _U_)
74 if (fv->value.bytes->len == 0) {
75 /* Empty array of bytes, so the representation
76 * is an empty string. */
79 /* 3 bytes for each byte of the byte "NN:" minus 1 byte
80 * as there's no trailing ":". */
81 return fv->value.bytes->len * 3 - 1;
88 * 5 for the first byte ([0-2].[0-39].)
89 * for each extra byte if the sub-id is:
90 * 1 byte it can be at most "127." (4 bytes we give it 4)
91 * 2 bytes it can be at most "16383." (6 bytes we give it 8)
92 * 3 bytes it can be at most "2097151." (8 bytes we give it 12)
93 * 4 bytes it can be at most "268435456." (10 bytes we give it 16)
94 * 5 bytes it can be at most "34359738368." (12 bytes we give it 20)
96 * a 5 bytes encoded subid can already overflow the guint32 that holds a sub-id,
97 * making it a completely different issue!
99 #define OID_REPR_LEN(fv) (5 + (4 * ((fv)->value.bytes->len-1)))
102 oid_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
104 return OID_REPR_LEN(fv);
108 oid_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
110 const char* oid_str = oid_encoded2string(fv->value.bytes->data,fv->value.bytes->len);
113 * I'm assuming that oid_repr_len is going to be called before to set buf's size.
114 * or else we might have a BO.
115 * I guess that is why this callback is not passed a length.
118 strncpy(buf,oid_str,OID_REPR_LEN(fv));
122 bytes_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
128 c = fv->value.bytes->data;
131 for (i = 0; i < fv->value.bytes->len; i++) {
133 sprintf(write_cursor, "%02x", *c++);
137 sprintf(write_cursor, ":%02x", *c++);
144 common_fvalue_set(fvalue_t *fv, guint8* data, guint len)
146 /* Free up the old value, if we have one */
147 bytes_fvalue_free(fv);
149 fv->value.bytes = g_byte_array_new();
150 g_byte_array_append(fv->value.bytes, data, len);
154 ether_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
156 g_assert(!already_copied);
157 common_fvalue_set(fv, value, ETHER_LEN);
161 ipv6_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
163 g_assert(!already_copied);
164 common_fvalue_set(fv, value, IPv6_LEN);
168 oid_fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied)
170 g_assert(already_copied);
172 /* Free up the old value, if we have one */
173 bytes_fvalue_free(fv);
175 fv->value.bytes = value;
180 value_get(fvalue_t *fv)
182 return fv->value.bytes->data;
186 bytes_from_string(fvalue_t *fv, char *s, LogFunc logfunc _U_)
190 bytes = g_byte_array_new();
192 g_byte_array_append(bytes, (guint8 *)s, (guint)strlen(s));
194 /* Free up the old value, if we have one */
195 bytes_fvalue_free(fv);
196 fv->value.bytes = bytes;
202 bytes_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
207 bytes = g_byte_array_new();
209 res = hex_str_to_bytes(s, bytes, TRUE);
213 logfunc("\"%s\" is not a valid byte string.", s);
214 g_byte_array_free(bytes, TRUE);
218 /* Free up the old value, if we have one */
219 bytes_fvalue_free(fv);
221 fv->value.bytes = bytes;
227 ether_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value, LogFunc logfunc)
232 * Don't log a message if this fails; we'll try looking it
233 * up as an Ethernet host name if it does, and if that fails,
234 * we'll log a message.
236 if (bytes_from_unparsed(fv, s, TRUE, NULL)) {
237 if (fv->value.bytes->len > ETHER_LEN) {
238 logfunc("\"%s\" contains too many bytes to be a valid Ethernet address.",
242 else if (fv->value.bytes->len < ETHER_LEN && !allow_partial_value) {
243 logfunc("\"%s\" contains too few bytes to be a valid Ethernet address.",
251 mac = get_ether_addr(s);
253 logfunc("\"%s\" is not a valid hostname or Ethernet address.",
258 ether_fvalue_set(fv, mac, FALSE);
263 ipv6_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
267 if (!get_host_ipaddr6(s, (struct e_in6_addr*)buffer)) {
268 logfunc("\"%s\" is not a valid hostname or IPv6 address.", s);
272 ipv6_fvalue_set(fv, buffer, FALSE);
277 ipv6_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
280 * 39 characters for "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX".
286 ipv6_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
288 ip6_to_str_buf((struct e_in6_addr *)fv->value.bytes->data, buf);
292 oid_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
299 * Don't log a message if this fails; we'll try looking it
300 * up as an OID if it does, and if that fails,
301 * we'll log a message.
303 /* do not try it as '.' is handled as valid separator for hexbytes :(
304 if (bytes_from_unparsed(fv, s, TRUE, NULL)) {
309 bytes = g_byte_array_new();
310 res = oid_str_to_bytes(s, bytes);
313 logfunc("\"%s\" is not a valid OBJECT IDENTIFIER.", s);
314 g_byte_array_free(bytes, TRUE);
318 /* Free up the old value, if we have one */
319 bytes_fvalue_free(fv);
320 fv->value.bytes = bytes;
328 return fv->value.bytes->len;
332 slice(fvalue_t *fv, GByteArray *bytes, guint offset, guint length)
336 data = fv->value.bytes->data + offset;
338 g_byte_array_append(bytes, data, length);
343 cmp_eq(fvalue_t *fv_a, fvalue_t *fv_b)
345 GByteArray *a = fv_a->value.bytes;
346 GByteArray *b = fv_b->value.bytes;
348 if (a->len != b->len) {
352 return (memcmp(a->data, b->data, a->len) == 0);
357 cmp_ne(fvalue_t *fv_a, fvalue_t *fv_b)
359 GByteArray *a = fv_a->value.bytes;
360 GByteArray *b = fv_b->value.bytes;
362 if (a->len != b->len) {
366 return (memcmp(a->data, b->data, a->len) != 0);
371 cmp_gt(fvalue_t *fv_a, fvalue_t *fv_b)
373 GByteArray *a = fv_a->value.bytes;
374 GByteArray *b = fv_b->value.bytes;
376 if (a->len > b->len) {
380 if (a->len < b->len) {
384 return (memcmp(a->data, b->data, a->len) > 0);
388 cmp_ge(fvalue_t *fv_a, fvalue_t *fv_b)
390 GByteArray *a = fv_a->value.bytes;
391 GByteArray *b = fv_b->value.bytes;
393 if (a->len > b->len) {
397 if (a->len < b->len) {
401 return (memcmp(a->data, b->data, a->len) >= 0);
405 cmp_lt(fvalue_t *fv_a, fvalue_t *fv_b)
407 GByteArray *a = fv_a->value.bytes;
408 GByteArray *b = fv_b->value.bytes;
410 if (a->len < b->len) {
414 if (a->len > b->len) {
418 return (memcmp(a->data, b->data, a->len) < 0);
422 cmp_le(fvalue_t *fv_a, fvalue_t *fv_b)
424 GByteArray *a = fv_a->value.bytes;
425 GByteArray *b = fv_b->value.bytes;
427 if (a->len < b->len) {
431 if (a->len > b->len) {
435 return (memcmp(a->data, b->data, a->len) <= 0);
439 cmp_bitwise_and(fvalue_t *fv_a, fvalue_t *fv_b)
441 GByteArray *a = fv_a->value.bytes;
442 GByteArray *b = fv_b->value.bytes;
444 unsigned char *p_a, *p_b;
446 if (b->len != a->len) {
461 cmp_contains(fvalue_t *fv_a, fvalue_t *fv_b)
463 GByteArray *a = fv_a->value.bytes;
464 GByteArray *b = fv_b->value.bytes;
466 if (epan_memmem(a->data, a->len, b->data, b->len)) {
476 cmp_matches(fvalue_t *fv_a, fvalue_t *fv_b)
478 GString *a = fv_a->value.gstring;
479 pcre_tuple_t *pcre_t = fv_b->value.re;
483 /* fv_b is always a FT_PCRE, otherwise the dfilter semcheck() would have
484 * warned us. For the same reason (and because we're using g_malloc()),
485 * fv_b->value.re is not NULL.
487 if (strcmp(fv_b->ftype->name, "FT_PCRE") != 0) {
494 pcre_t->re, /* Compiled PCRE */
495 pcre_t->ex, /* PCRE extra from pcre_study() */
496 a->str, /* The data to check for the pattern... */
497 a->len, /* ... and its length */
498 0, /* Start offset within data */
499 options, /* PCRE options */
500 NULL, /* We are not interested in the matched string */
501 0 /* of the pattern; only in success or failure. */
503 /* NOTE - DO NOT g_free(data) */
512 ftype_register_bytes(void)
515 static ftype_t bytes_type = {
516 FT_BYTES, /* ftype */
517 "FT_BYTES", /* name */
518 "sequence of bytes", /* pretty_name */
520 bytes_fvalue_new, /* new_value */
521 bytes_fvalue_free, /* free_value */
522 bytes_from_unparsed, /* val_from_unparsed */
523 bytes_from_string, /* val_from_string */
524 bytes_to_repr, /* val_to_string_repr */
525 bytes_repr_len, /* len_string_repr */
527 bytes_fvalue_set, /* set_value */
528 NULL, /* set_value_uinteger */
529 NULL, /* set_value_sinteger */
530 NULL, /* set_value_integer64 */
531 NULL, /* set_value_floating */
533 value_get, /* get_value */
534 NULL, /* get_value_uinteger */
535 NULL, /* get_value_sinteger */
536 NULL, /* get_value_integer64 */
537 NULL, /* get_value_floating */
553 static ftype_t uint_bytes_type = {
554 FT_UINT_BYTES, /* ftype */
555 "FT_UINT_BYTES", /* name */
556 "sequence of bytes", /* pretty_name */
558 bytes_fvalue_new, /* new_value */
559 bytes_fvalue_free, /* free_value */
560 bytes_from_unparsed, /* val_from_unparsed */
561 NULL, /* val_from_string */
562 bytes_to_repr, /* val_to_string_repr */
563 bytes_repr_len, /* len_string_repr */
565 bytes_fvalue_set, /* set_value */
566 NULL, /* set_value_uinteger */
567 NULL, /* set_value_sinteger */
568 NULL, /* set_value_integer64 */
569 NULL, /* set_value_floating */
571 value_get, /* get_value */
572 NULL, /* get_value_uinteger */
573 NULL, /* get_value_sinteger */
574 NULL, /* get_value_integer64 */
575 NULL, /* get_value_floating */
585 NULL, /* cmp_matches */
591 static ftype_t ether_type = {
592 FT_ETHER, /* ftype */
593 "FT_ETHER", /* name */
594 "Ethernet or other MAC address",/* pretty_name */
595 ETHER_LEN, /* wire_size */
596 bytes_fvalue_new, /* new_value */
597 bytes_fvalue_free, /* free_value */
598 ether_from_unparsed, /* val_from_unparsed */
599 NULL, /* val_from_string */
600 bytes_to_repr, /* val_to_string_repr */
601 bytes_repr_len, /* len_string_repr */
603 ether_fvalue_set, /* set_value */
604 NULL, /* set_value_uinteger */
605 NULL, /* set_value_sinteger */
606 NULL, /* set_value_integer64 */
607 NULL, /* set_value_floating */
609 value_get, /* get_value */
610 NULL, /* get_value_uinteger */
611 NULL, /* get_value_sinteger */
612 NULL, /* get_value_integer64 */
613 NULL, /* get_value_floating */
629 static ftype_t ipv6_type = {
631 "FT_IPv6", /* name */
632 "IPv6 address", /* pretty_name */
633 IPv6_LEN, /* wire_size */
634 bytes_fvalue_new, /* new_value */
635 bytes_fvalue_free, /* free_value */
636 ipv6_from_unparsed, /* val_from_unparsed */
637 NULL, /* val_from_string */
638 ipv6_to_repr, /* val_to_string_repr */
639 ipv6_repr_len, /* len_string_repr */
641 ipv6_fvalue_set, /* set_value */
642 NULL, /* set_value_uinteger */
643 NULL, /* set_value_sinteger */
644 NULL, /* set_value_integer64 */
645 NULL, /* set_value_floating */
647 value_get, /* get_value */
648 NULL, /* get_value_uinteger */
649 NULL, /* get_value_sinteger */
650 NULL, /* get_value_integer64 */
651 NULL, /* get_value_floating */
661 NULL, /* cmp_matches */
667 static ftype_t oid_type = {
670 "OBJECT IDENTIFIER", /* pretty_name */
672 bytes_fvalue_new, /* new_value */
673 bytes_fvalue_free, /* free_value */
674 oid_from_unparsed, /* val_from_unparsed */
675 NULL, /* val_from_string */
676 oid_to_repr, /* val_to_string_repr */
677 oid_repr_len, /* len_string_repr */
679 oid_fvalue_set, /* set_value */
680 NULL, /* set_value_uinteger */
681 NULL, /* set_value_sinteger */
682 NULL, /* set_value_integer64 */
683 NULL, /* set_value_floating */
685 value_get, /* get_value */
686 NULL, /* get_value_uinteger */
687 NULL, /* get_value_sinteger */
688 NULL, /* get_value_integer64 */
689 NULL, /* get_value_floating */
699 NULL, /* cmp_matches */
705 ftype_register(FT_BYTES, &bytes_type);
706 ftype_register(FT_UINT_BYTES, &uint_bytes_type);
707 ftype_register(FT_ETHER, ðer_type);
708 ftype_register(FT_IPv6, &ipv6_type);
709 ftype_register(FT_OID, &oid_type);