From Jakub Zawadzki (bug 3334):
[metze/wireshark/wip.git] / epan / column-utils.c
1 /* column-utils.c
2  * Routines for column utilities.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30 #include <time.h>
31
32 #include "column-utils.h"
33 #include "timestamp.h"
34 #include "sna-utils.h"
35 #include "atalk-utils.h"
36 #include "to_str.h"
37 #include "packet_info.h"
38 #include "pint.h"
39 #include "addr_resolv.h"
40 #include "ipv6-utils.h"
41 #include "osi-utils.h"
42 #include "value_string.h"
43 #include "column_info.h"
44
45 #include <epan/strutil.h>
46 #include <epan/epan.h>
47
48 static column_info *ci;
49
50 /* Allocate all the data structures for constructing column data, given
51    the number of columns. */
52 void
53 col_setup(column_info *cinfo, gint num_cols)
54 {
55   int i;
56
57   cinfo->num_cols       = num_cols;
58   cinfo->col_fmt        = (gint *) g_malloc(sizeof(gint) * num_cols);
59   cinfo->fmt_matx       = (gboolean **) g_malloc(sizeof(gboolean *) * num_cols);
60   cinfo->col_first      = (int *) g_malloc(sizeof(int) * (NUM_COL_FMTS));
61   cinfo->col_last       = (int *) g_malloc(sizeof(int) * (NUM_COL_FMTS));
62   cinfo->col_title      = (gchar **) g_malloc(sizeof(gchar *) * num_cols);
63   cinfo->col_custom_field = (gchar **) g_malloc(sizeof(gchar *) * num_cols);
64   cinfo->col_data       = (const gchar **) g_malloc(sizeof(gchar *) * num_cols);
65   cinfo->col_buf        = (gchar **) g_malloc(sizeof(gchar *) * num_cols);
66   cinfo->col_fence      = (int *) g_malloc(sizeof(int) * num_cols);
67   cinfo->col_expr.col_expr = (gchar **) g_malloc(sizeof(gchar *) * (num_cols + 1));
68   cinfo->col_expr.col_expr_val = (gchar **) g_malloc(sizeof(gchar *) * (num_cols + 1));
69
70   for (i = 0; i < NUM_COL_FMTS; i++) {
71     cinfo->col_first[i] = -1;
72     cinfo->col_last[i] = -1;
73   }
74 }
75
76 /* Initialize the data structures for constructing column data. */
77 void
78 col_init(column_info *cinfo)
79 {
80   int i;
81
82   for (i = 0; i < cinfo->num_cols; i++) {
83     cinfo->col_buf[i][0] = '\0';
84     cinfo->col_data[i] = cinfo->col_buf[i];
85     cinfo->col_fence[i] = 0;
86     cinfo->col_expr.col_expr[i][0] = '\0';
87     cinfo->col_expr.col_expr_val[i][0] = '\0';
88   }
89   cinfo->writable = TRUE;
90 }
91
92 gboolean
93 col_get_writable(column_info *cinfo)
94 {
95         return (cinfo ? cinfo->writable : FALSE);
96 }
97
98 void
99 col_set_writable(column_info *cinfo, gboolean writable)
100 {
101         if (cinfo)
102                 cinfo->writable = writable;
103 }
104
105 /* Checks to see if a particular packet information element is needed for
106    the packet list */
107 gint
108 check_col(column_info *cinfo, gint el) {
109
110   if (cinfo && cinfo->writable) {
111     /* We are constructing columns, and they're writable */
112     if (cinfo->col_first[el] >= 0) {
113       /* There is at least one column in that format */
114       return TRUE;
115     }
116   }
117   return FALSE;
118 }
119
120 /* Sets the fence for a column to be at the end of the column. */
121 void
122 col_set_fence(column_info *cinfo, gint el)
123 {
124   int i;
125
126   if (cinfo && cinfo->writable) {
127     /* We are constructing columns, and they're writable */
128     if (cinfo->col_first[el] >= 0) {
129       /* There is at least one column in that format */
130       for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
131         if (cinfo->fmt_matx[i][el]) {
132           cinfo->col_fence[i] = strlen(cinfo->col_data[i]);
133         }
134       }
135     }
136   }
137 }
138
139 /* Use this to clear out a column, especially if you're going to be
140    appending to it later; at least on some platforms, it's more
141    efficient than using "col_add_str()" with a null string, and
142    more efficient than "col_set_str()" with a null string if you
143    later append to it, as the later append will cause a string
144    copy to be done. */
145 void
146 col_clear(column_info *cinfo, gint el)
147 {
148   int    i;
149   int    fence;
150
151   g_assert(cinfo->col_first[el] >= 0);
152   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
153     if (cinfo->fmt_matx[i][el]) {
154       /*
155        * At this point, either
156        *
157        *   1) col_data[i] is equal to col_buf[i], in which case we
158        *      don't have to worry about copying col_data[i] to
159        *      col_buf[i];
160        *
161        *   2) col_data[i] isn't equal to col_buf[i], in which case
162        *      the only thing that's been done to the column is
163        *      "col_set_str()" calls and possibly "col_set_fence()"
164        *      calls, in which case the fence is either unset and
165        *      at the beginning of the string or set and at the end
166        *      of the string - if it's at the beginning, we're just
167        *      going to clear the column, and if it's at the end,
168        *      we don't do anything.
169        */
170       fence = cinfo->col_fence[i];
171       if (fence == 0 || cinfo->col_buf[i] == cinfo->col_data[i]) {
172         /*
173          * The fence isn't at the end of the column, or the column wasn't
174          * last set with "col_set_str()", so clear the column out.
175          */
176         cinfo->col_buf[i][fence] = '\0';
177         cinfo->col_data[i] = cinfo->col_buf[i];
178       }
179       cinfo->col_expr.col_expr[i][0] = '\0';
180       cinfo->col_expr.col_expr_val[i][0] = '\0';
181     }
182   }
183 }
184
185 #define COL_CHECK_APPEND(cinfo, i, max_len) \
186   if (cinfo->col_data[i] != cinfo->col_buf[i]) {                \
187     /* This was set with "col_set_str()"; copy the string they  \
188        set it to into the buffer, so we can append to it. */    \
189     g_strlcpy(cinfo->col_buf[i], cinfo->col_data[i], max_len);  \
190     cinfo->col_data[i] = cinfo->col_buf[i];                     \
191   }
192
193 #define COL_CHECK_REF_TIME(fd, cinfo, col)      \
194   if(fd->flags.ref_time){                                       \
195     g_snprintf(cinfo->col_buf[col], COL_MAX_LEN, "*REF*");      \
196     cinfo->col_data[col] = cinfo->col_buf[col];                 \
197     return;                                                     \
198   }
199
200 /* Use this if "str" points to something that will stay around (and thus
201    needn't be copied). */
202 void
203 col_set_str(column_info *cinfo, gint el, const gchar* str)
204 {
205   int i;
206   int fence;
207   size_t max_len;
208
209   if (el == COL_INFO)
210         max_len = COL_MAX_INFO_LEN;
211   else
212         max_len = COL_MAX_LEN;
213
214   g_assert(cinfo->col_first[el] >= 0);
215   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
216     if (cinfo->fmt_matx[i][el]) {
217       fence = cinfo->col_fence[i];
218       if (fence != 0) {
219         /*
220          * We will append the string after the fence.
221          * First arrange that we can append, if necessary.
222          */
223         COL_CHECK_APPEND(cinfo, i, max_len);
224
225         g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence);
226       } else {
227         /*
228          * There's no fence, so we can just set the column to point
229          * to the string.
230          */
231         cinfo->col_data[i] = str;
232       }
233     }
234   }
235 }
236
237 /* Adds a vararg list to a packet info string. */
238 void
239 col_add_fstr(column_info *cinfo, gint el, const gchar *format, ...) {
240   va_list ap;
241   int     i;
242   int     fence;
243   size_t  max_len;
244
245   g_assert(cinfo->col_first[el] >= 0);
246   if (el == COL_INFO)
247         max_len = COL_MAX_INFO_LEN;
248   else
249         max_len = COL_MAX_LEN;
250
251   va_start(ap, format);
252   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
253     if (cinfo->fmt_matx[i][el]) {
254       fence = cinfo->col_fence[i];
255       if (fence != 0) {
256         /*
257          * We will append the string after the fence.
258          * First arrange that we can append, if necessary.
259          */
260         COL_CHECK_APPEND(cinfo, i, max_len);
261       } else {
262         /*
263          * There's no fence, so we can just write to the string.
264          */
265         cinfo->col_data[i] = cinfo->col_buf[i];
266       }
267       g_vsnprintf(&cinfo->col_buf[i][fence], max_len - fence, format, ap);
268       cinfo->col_buf[i][max_len - 1] = '\0';
269     }
270   }
271   va_end(ap);
272 }
273
274 void
275 col_custom_set_fstr(header_field_info *hfinfo, const gchar *format, ...)
276 {
277   va_list ap;
278   int     i;
279
280   if (!have_custom_cols(ci))
281     return;
282
283   va_start(ap, format);
284   for (i = ci->col_first[COL_CUSTOM];
285        i <= ci->col_last[COL_CUSTOM]; i++) {
286     if (ci->fmt_matx[i][COL_CUSTOM] &&
287         strcmp(ci->col_custom_field[i], hfinfo->abbrev) == 0) {
288       ci->col_data[i] = ci->col_buf[i];
289       g_vsnprintf(ci->col_buf[i], COL_MAX_LEN, format, ap);
290
291       g_strlcpy(ci->col_expr.col_expr[i], hfinfo->abbrev, COL_MAX_LEN);
292
293       switch(hfinfo->type) {
294       case FT_STRING:
295       case FT_STRINGZ:
296         g_snprintf(ci->col_expr.col_expr_val[i], COL_MAX_LEN, "\"%s\"",
297             ci->col_buf[i]);
298         break;
299
300       default:
301         g_strlcpy(ci->col_expr.col_expr_val[i], ci->col_buf[i], COL_MAX_LEN);
302         break;
303       }
304     }
305   }
306   va_end(ap);
307 }
308
309 void
310 col_custom_prime_edt(epan_dissect_t *edt, column_info *cinfo)
311 {
312   int i;
313   dfilter_t *dfilter_code;
314
315   ci = cinfo; /* Save this into the static variable ci for use by
316                * col_custom_set_fstr() later. */
317
318   if(!have_custom_cols(cinfo))
319       return;
320
321   for (i = cinfo->col_first[COL_CUSTOM];
322        i <= cinfo->col_last[COL_CUSTOM]; i++) {
323     if (cinfo->fmt_matx[i][COL_CUSTOM] &&
324         strlen(cinfo->col_custom_field[i]) > 0) {
325       if(dfilter_compile(cinfo->col_custom_field[i], &dfilter_code)) {
326         epan_dissect_prime_dfilter(edt, dfilter_code);
327         dfilter_free(dfilter_code);
328       }
329     }
330   }
331 }
332
333 gboolean
334 have_custom_cols(column_info *cinfo)
335 {
336   /* The same as check_col(), but without the check to see if the column
337    * is writable. */
338   if (cinfo && cinfo->col_first[COL_CUSTOM] >= 0)
339     return TRUE;
340   else
341     return FALSE;
342 }
343
344 gboolean
345 col_has_time_fmt(column_info *cinfo, gint col)
346 {
347   return ((cinfo->fmt_matx[col][COL_CLS_TIME]) ||
348           (cinfo->fmt_matx[col][COL_ABS_TIME]) ||
349           (cinfo->fmt_matx[col][COL_ABS_DATE_TIME]) ||
350           (cinfo->fmt_matx[col][COL_REL_TIME]) ||
351           (cinfo->fmt_matx[col][COL_DELTA_TIME]) ||
352           (cinfo->fmt_matx[col][COL_DELTA_TIME_DIS]));
353 }
354
355 static void
356 col_do_append_sep_va_fstr(column_info *cinfo, gint el, const gchar *separator,
357                           const gchar *format, va_list ap)
358 {
359   int     i;
360   size_t  len, max_len, sep_len;
361
362   g_assert(cinfo->col_first[el] >= 0);
363   if (el == COL_INFO)
364         max_len = COL_MAX_INFO_LEN;
365   else
366         max_len = COL_MAX_LEN;
367
368   if (separator == NULL)
369     sep_len = 0;
370   else
371     sep_len = strlen(separator);
372   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
373     if (cinfo->fmt_matx[i][el]) {
374       /*
375        * First arrange that we can append, if necessary.
376        */
377       COL_CHECK_APPEND(cinfo, i, max_len);
378
379       len = strlen(cinfo->col_buf[i]);
380
381       /*
382        * If we have a separator, append it if the column isn't empty.
383        */
384       if (separator != NULL) {
385         if (len != 0) {
386           g_strlcat(cinfo->col_buf[i], separator, max_len);
387           len += sep_len;
388         }
389       }
390       g_vsnprintf(&cinfo->col_buf[i][len], max_len - len, format, ap);
391       cinfo->col_buf[i][max_len-1] = 0;
392     }
393   }
394 }
395
396 /* Appends a vararg list to a packet info string. */
397 void
398 col_append_fstr(column_info *cinfo, gint el, const gchar *format, ...)
399 {
400   va_list ap;
401
402   va_start(ap, format);
403   col_do_append_sep_va_fstr(cinfo, el, NULL, format, ap);
404   va_end(ap);
405 }
406
407 /* Appends a vararg list to a packet info string.
408  * Prefixes it with the given separator if the column is not empty. */
409 void
410 col_append_sep_fstr(column_info *cinfo, gint el, const gchar *separator,
411                 const gchar *format, ...)
412 {
413   va_list ap;
414
415   if (separator == NULL)
416     separator = ", ";    /* default */
417   va_start(ap, format);
418   col_do_append_sep_va_fstr(cinfo, el, separator, format, ap);
419   va_end(ap);
420 }
421
422
423
424 /* Prepends a vararg list to a packet info string. */
425 #define COL_BUF_MAX_LEN (((COL_MAX_INFO_LEN) > (COL_MAX_LEN)) ? \
426         (COL_MAX_INFO_LEN) : (COL_MAX_LEN))
427 void
428 col_prepend_fstr(column_info *cinfo, gint el, const gchar *format, ...)
429 {
430   va_list     ap;
431   int         i;
432   char        orig_buf[COL_BUF_MAX_LEN];
433   const char *orig;
434   size_t      max_len;
435
436   g_assert(cinfo->col_first[el] >= 0);
437   if (el == COL_INFO)
438         max_len = COL_MAX_INFO_LEN;
439   else
440         max_len = COL_MAX_LEN;
441
442   va_start(ap, format);
443   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
444     if (cinfo->fmt_matx[i][el]) {
445       if (cinfo->col_data[i] != cinfo->col_buf[i]) {
446         /* This was set with "col_set_str()"; which is effectively const */
447         orig = cinfo->col_data[i];
448       } else {
449         g_strlcpy(orig_buf, cinfo->col_buf[i], max_len);
450         orig = orig_buf;
451       }
452       g_vsnprintf(cinfo->col_buf[i], max_len, format, ap);
453       cinfo->col_buf[i][max_len - 1] = '\0';
454
455       /*
456        * Move the fence, unless it's at the beginning of the string.
457        */
458       if (cinfo->col_fence[i] > 0)
459         cinfo->col_fence[i] += strlen(cinfo->col_buf[i]);
460
461       g_strlcat(cinfo->col_buf[i], orig, max_len);
462       cinfo->col_data[i] = cinfo->col_buf[i];
463     }
464   }
465   va_end(ap);
466 }
467 void
468 col_prepend_fence_fstr(column_info *cinfo, gint el, const gchar *format, ...)
469 {
470   va_list     ap;
471   int         i;
472   char        orig_buf[COL_BUF_MAX_LEN];
473   const char *orig;
474   size_t      max_len;
475
476   g_assert(cinfo->col_first[el] >= 0);
477   if (el == COL_INFO)
478         max_len = COL_MAX_INFO_LEN;
479   else
480         max_len = COL_MAX_LEN;
481
482   va_start(ap, format);
483   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
484     if (cinfo->fmt_matx[i][el]) {
485       if (cinfo->col_data[i] != cinfo->col_buf[i]) {
486         /* This was set with "col_set_str()"; which is effectively const */
487         orig = cinfo->col_data[i];
488       } else {
489         g_strlcpy(orig_buf, cinfo->col_buf[i], max_len);
490         orig = orig_buf;
491       }
492       g_vsnprintf(cinfo->col_buf[i], max_len, format, ap);
493       cinfo->col_buf[i][max_len - 1] = '\0';
494
495       /*
496        * Move the fence if it exists, else create a new fence at the
497        * end of the prepended data.
498        */
499       if (cinfo->col_fence[i] > 0) {
500         cinfo->col_fence[i] += strlen(cinfo->col_buf[i]);
501       } else {
502         cinfo->col_fence[i]  = strlen(cinfo->col_buf[i]);
503       }
504       g_strlcat(cinfo->col_buf[i], orig, max_len);
505       cinfo->col_data[i] = cinfo->col_buf[i];
506     }
507   }
508   va_end(ap);
509 }
510
511 /* Use this if "str" points to something that won't stay around (and
512    must thus be copied). */
513 void
514 col_add_str(column_info *cinfo, gint el, const gchar* str)
515 {
516   int    i;
517   int    fence;
518   size_t max_len;
519
520   g_assert(cinfo->col_first[el] >= 0);
521   if (el == COL_INFO)
522         max_len = COL_MAX_INFO_LEN;
523   else
524         max_len = COL_MAX_LEN;
525
526   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
527     if (cinfo->fmt_matx[i][el]) {
528       fence = cinfo->col_fence[i];
529       if (fence != 0) {
530         /*
531          * We will append the string after the fence.
532          * First arrange that we can append, if necessary.
533          */
534         COL_CHECK_APPEND(cinfo, i, max_len);
535       } else {
536         /*
537          * There's no fence, so we can just write to the string.
538          */
539         cinfo->col_data[i] = cinfo->col_buf[i];
540       }
541       g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence);
542     }
543   }
544 }
545
546 static void
547 col_do_append_str(column_info *cinfo, gint el, const gchar* separator,
548     const gchar* str)
549 {
550   int    i;
551   size_t len, max_len, sep_len;
552
553   g_assert(cinfo->col_first[el] >= 0);
554   if (el == COL_INFO)
555         max_len = COL_MAX_INFO_LEN;
556   else
557         max_len = COL_MAX_LEN;
558
559   if (separator == NULL)
560     sep_len = 0;
561   else
562     sep_len = strlen(separator);
563
564   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
565     if (cinfo->fmt_matx[i][el]) {
566       /*
567        * First arrange that we can append, if necessary.
568        */
569       COL_CHECK_APPEND(cinfo, i, max_len);
570
571       len = cinfo->col_buf[i][0];
572
573       /*
574        * If we have a separator, append it if the column isn't empty.
575        */
576       if (separator != NULL) {
577         if (len != 0) {
578           g_strlcat(cinfo->col_buf[i], separator, max_len);
579         }
580       }
581       g_strlcat(cinfo->col_buf[i], str, max_len);
582     }
583   }
584 }
585
586 void
587 col_append_str(column_info *cinfo, gint el, const gchar* str)
588 {
589   col_do_append_str(cinfo, el, NULL, str);
590 }
591
592 void
593 col_append_sep_str(column_info *cinfo, gint el, const gchar* separator,
594     const gchar* str)
595 {
596   if (separator == NULL)
597     separator = ", ";    /* default */
598   col_do_append_str(cinfo, el, separator, str);
599 }
600
601 static void
602 col_set_abs_date_time(frame_data *fd, column_info *cinfo, int col)
603 {
604   struct tm *tmp;
605   time_t then;
606
607   COL_CHECK_REF_TIME(fd, cinfo, col);
608
609   then = fd->abs_ts.secs;
610   tmp = localtime(&then);
611   if (tmp != NULL) {
612           switch(timestamp_get_precision()) {
613           case(TS_PREC_FIXED_SEC):
614           case(TS_PREC_AUTO_SEC):
615                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
616              "%04d-%02d-%02d %02d:%02d:%02d",
617              tmp->tm_year + 1900,
618              tmp->tm_mon + 1,
619              tmp->tm_mday,
620              tmp->tm_hour,
621              tmp->tm_min,
622              tmp->tm_sec);
623                   break;
624           case(TS_PREC_FIXED_DSEC):
625           case(TS_PREC_AUTO_DSEC):
626                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
627              "%04d-%02d-%02d %02d:%02d:%02d.%01ld",
628              tmp->tm_year + 1900,
629              tmp->tm_mon + 1,
630              tmp->tm_mday,
631              tmp->tm_hour,
632              tmp->tm_min,
633              tmp->tm_sec,
634              (long)fd->abs_ts.nsecs / 100000000);
635                   break;
636           case(TS_PREC_FIXED_CSEC):
637           case(TS_PREC_AUTO_CSEC):
638                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
639              "%04d-%02d-%02d %02d:%02d:%02d.%02ld",
640              tmp->tm_year + 1900,
641              tmp->tm_mon + 1,
642              tmp->tm_mday,
643              tmp->tm_hour,
644              tmp->tm_min,
645              tmp->tm_sec,
646              (long)fd->abs_ts.nsecs / 10000000);
647                   break;
648           case(TS_PREC_FIXED_MSEC):
649           case(TS_PREC_AUTO_MSEC):
650                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
651              "%04d-%02d-%02d %02d:%02d:%02d.%03ld",
652              tmp->tm_year + 1900,
653              tmp->tm_mon + 1,
654              tmp->tm_mday,
655              tmp->tm_hour,
656              tmp->tm_min,
657              tmp->tm_sec,
658              (long)fd->abs_ts.nsecs / 1000000);
659                   break;
660           case(TS_PREC_FIXED_USEC):
661           case(TS_PREC_AUTO_USEC):
662                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
663              "%04d-%02d-%02d %02d:%02d:%02d.%06ld",
664              tmp->tm_year + 1900,
665              tmp->tm_mon + 1,
666              tmp->tm_mday,
667              tmp->tm_hour,
668              tmp->tm_min,
669              tmp->tm_sec,
670              (long)fd->abs_ts.nsecs / 1000);
671                   break;
672           case(TS_PREC_FIXED_NSEC):
673           case(TS_PREC_AUTO_NSEC):
674                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
675              "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
676              tmp->tm_year + 1900,
677              tmp->tm_mon + 1,
678              tmp->tm_mday,
679              tmp->tm_hour,
680              tmp->tm_min,
681              tmp->tm_sec,
682              (long)fd->abs_ts.nsecs);
683                   break;
684           default:
685                   g_assert_not_reached();
686           }
687   } else {
688     cinfo->col_buf[col][0] = '\0';
689   }
690   cinfo->col_data[col] = cinfo->col_buf[col];
691   g_strlcpy(cinfo->col_expr.col_expr[col],"frame.time",COL_MAX_LEN);
692   g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
693 }
694
695 static void
696 col_set_rel_time(frame_data *fd, column_info *cinfo, int col)
697 {
698   COL_CHECK_REF_TIME(fd, cinfo, col);
699
700   switch(timestamp_get_precision()) {
701           case(TS_PREC_FIXED_SEC):
702           case(TS_PREC_AUTO_SEC):
703                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
704                         (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 1000000000, SECS);
705                   break;
706           case(TS_PREC_FIXED_DSEC):
707           case(TS_PREC_AUTO_DSEC):
708                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
709                         (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 100000000, DSECS);
710                   break;
711           case(TS_PREC_FIXED_CSEC):
712           case(TS_PREC_AUTO_CSEC):
713                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
714                         (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 10000000, CSECS);
715                   break;
716           case(TS_PREC_FIXED_MSEC):
717           case(TS_PREC_AUTO_MSEC):
718                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
719                         (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 1000000, MSECS);
720                   break;
721           case(TS_PREC_FIXED_USEC):
722           case(TS_PREC_AUTO_USEC):
723                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
724                         (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 1000, USECS);
725                   break;
726           case(TS_PREC_FIXED_NSEC):
727           case(TS_PREC_AUTO_NSEC):
728                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
729                         (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs, NSECS);
730                   break;
731           default:
732                   g_assert_not_reached();
733   }
734   cinfo->col_data[col] = cinfo->col_buf[col];
735   g_strlcpy(cinfo->col_expr.col_expr[col],"frame.time_relative", COL_MAX_LEN);
736   g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
737 }
738
739 static void
740 col_set_delta_time(frame_data *fd, column_info *cinfo, int col)
741 {
742   COL_CHECK_REF_TIME(fd, cinfo, col);
743
744   switch(timestamp_get_precision()) {
745           case(TS_PREC_FIXED_SEC):
746           case(TS_PREC_AUTO_SEC):
747                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
748                         (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 1000000000, SECS);
749                   break;
750           case(TS_PREC_FIXED_DSEC):
751           case(TS_PREC_AUTO_DSEC):
752                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
753                         (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 100000000, DSECS);
754                   break;
755           case(TS_PREC_FIXED_CSEC):
756           case(TS_PREC_AUTO_CSEC):
757                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
758                         (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 10000000, CSECS);
759                   break;
760           case(TS_PREC_FIXED_MSEC):
761           case(TS_PREC_AUTO_MSEC):
762                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
763                         (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 1000000, MSECS);
764                   break;
765           case(TS_PREC_FIXED_USEC):
766           case(TS_PREC_AUTO_USEC):
767                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
768                         (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 1000, USECS);
769                   break;
770           case(TS_PREC_FIXED_NSEC):
771           case(TS_PREC_AUTO_NSEC):
772                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
773                         (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs, NSECS);
774                   break;
775           default:
776                   g_assert_not_reached();
777   }
778   cinfo->col_data[col] = cinfo->col_buf[col];
779   g_strlcpy(cinfo->col_expr.col_expr[col],"frame.time_delta",COL_MAX_LEN);
780   g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
781 }
782
783 static void
784 col_set_delta_time_dis(frame_data *fd, column_info *cinfo, int col)
785 {
786   COL_CHECK_REF_TIME(fd, cinfo, col);
787
788   switch(timestamp_get_precision()) {
789           case(TS_PREC_FIXED_SEC):
790           case(TS_PREC_AUTO_SEC):
791                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
792                         (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 1000000000, SECS);
793                   break;
794           case(TS_PREC_FIXED_DSEC):
795           case(TS_PREC_AUTO_DSEC):
796                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
797                         (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 100000000, DSECS);
798                   break;
799           case(TS_PREC_FIXED_CSEC):
800           case(TS_PREC_AUTO_CSEC):
801                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
802                         (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 10000000, CSECS);
803                   break;
804           case(TS_PREC_FIXED_MSEC):
805           case(TS_PREC_AUTO_MSEC):
806                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
807                         (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 1000000, MSECS);
808                   break;
809           case(TS_PREC_FIXED_USEC):
810           case(TS_PREC_AUTO_USEC):
811                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
812                         (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 1000, USECS);
813                   break;
814           case(TS_PREC_FIXED_NSEC):
815           case(TS_PREC_AUTO_NSEC):
816                   display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
817                         (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs, NSECS);
818                   break;
819           default:
820                   g_assert_not_reached();
821   }
822   cinfo->col_data[col] = cinfo->col_buf[col];
823   g_strlcpy(cinfo->col_expr.col_expr[col],"frame.time_delta_displayed",
824             COL_MAX_LEN);
825   g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
826 }
827
828 /* To do: Add check_col checks to the col_add* routines */
829
830 static void
831 col_set_abs_time(frame_data *fd, column_info *cinfo, int col)
832 {
833   struct tm *tmp;
834   time_t then;
835
836   COL_CHECK_REF_TIME(fd, cinfo, col);
837
838   then = fd->abs_ts.secs;
839   tmp = localtime(&then);
840   if (tmp != NULL) {
841           switch(timestamp_get_precision()) {
842           case(TS_PREC_FIXED_SEC):
843           case(TS_PREC_AUTO_SEC):
844                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
845              "%02d:%02d:%02d",
846              tmp->tm_hour,
847              tmp->tm_min,
848              tmp->tm_sec);
849                   break;
850           case(TS_PREC_FIXED_DSEC):
851           case(TS_PREC_AUTO_DSEC):
852                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
853              "%02d:%02d:%02d.%01ld",
854              tmp->tm_hour,
855              tmp->tm_min,
856              tmp->tm_sec,
857              (long)fd->abs_ts.nsecs / 100000000);
858                   break;
859           case(TS_PREC_FIXED_CSEC):
860           case(TS_PREC_AUTO_CSEC):
861                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
862              "%02d:%02d:%02d.%02ld",
863              tmp->tm_hour,
864              tmp->tm_min,
865              tmp->tm_sec,
866              (long)fd->abs_ts.nsecs / 10000000);
867                   break;
868           case(TS_PREC_FIXED_MSEC):
869           case(TS_PREC_AUTO_MSEC):
870                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
871              "%02d:%02d:%02d.%03ld",
872              tmp->tm_hour,
873              tmp->tm_min,
874              tmp->tm_sec,
875              (long)fd->abs_ts.nsecs / 1000000);
876                   break;
877           case(TS_PREC_FIXED_USEC):
878           case(TS_PREC_AUTO_USEC):
879                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
880              "%02d:%02d:%02d.%06ld",
881              tmp->tm_hour,
882              tmp->tm_min,
883              tmp->tm_sec,
884              (long)fd->abs_ts.nsecs / 1000);
885                   break;
886           case(TS_PREC_FIXED_NSEC):
887           case(TS_PREC_AUTO_NSEC):
888                   g_snprintf(cinfo->col_buf[col], COL_MAX_LEN,
889              "%02d:%02d:%02d.%09ld",
890              tmp->tm_hour,
891              tmp->tm_min,
892              tmp->tm_sec,
893              (long)fd->abs_ts.nsecs);
894                   break;
895           default:
896                   g_assert_not_reached();
897           }
898   } else {
899     cinfo->col_buf[col][0] = '\0';
900   }
901   cinfo->col_data[col] = cinfo->col_buf[col];
902   g_strlcpy(cinfo->col_expr.col_expr[col],"frame.time",COL_MAX_LEN);
903   g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
904 }
905
906 static void
907 col_set_epoch_time(frame_data *fd, column_info *cinfo, int col)
908 {
909
910   COL_CHECK_REF_TIME(fd, cinfo, col);
911
912   switch(timestamp_get_precision()) {
913           case(TS_PREC_FIXED_SEC):
914           case(TS_PREC_AUTO_SEC):
915                   display_epoch_time(cinfo->col_buf[col], COL_MAX_LEN,
916                         fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000000, SECS);
917                   break;
918           case(TS_PREC_FIXED_DSEC):
919           case(TS_PREC_AUTO_DSEC):
920                   display_epoch_time(cinfo->col_buf[col], COL_MAX_LEN,
921                         fd->abs_ts.secs, fd->abs_ts.nsecs / 100000000, DSECS);
922                   break;
923           case(TS_PREC_FIXED_CSEC):
924           case(TS_PREC_AUTO_CSEC):
925                   display_epoch_time(cinfo->col_buf[col], COL_MAX_LEN,
926                         fd->abs_ts.secs, fd->abs_ts.nsecs / 10000000, CSECS);
927                   break;
928           case(TS_PREC_FIXED_MSEC):
929           case(TS_PREC_AUTO_MSEC):
930                   display_epoch_time(cinfo->col_buf[col], COL_MAX_LEN,
931                         fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000, MSECS);
932                   break;
933           case(TS_PREC_FIXED_USEC):
934           case(TS_PREC_AUTO_USEC):
935                   display_epoch_time(cinfo->col_buf[col], COL_MAX_LEN,
936                         fd->abs_ts.secs, fd->abs_ts.nsecs / 1000, USECS);
937                   break;
938           case(TS_PREC_FIXED_NSEC):
939           case(TS_PREC_AUTO_NSEC):
940                   display_epoch_time(cinfo->col_buf[col], COL_MAX_LEN,
941                         fd->abs_ts.secs, fd->abs_ts.nsecs, NSECS);
942                   break;
943           default:
944                   g_assert_not_reached();
945   }
946   cinfo->col_data[col] = cinfo->col_buf[col];
947   g_strlcpy(cinfo->col_expr.col_expr[col],"frame.time_delta",COL_MAX_LEN);
948   g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
949 }
950
951 static void
952 col_set_cls_time(frame_data *fd, column_info *cinfo, gint col)
953 {
954   switch (timestamp_get_type()) {
955     case TS_ABSOLUTE:
956       col_set_abs_time(fd, cinfo, col);
957       break;
958
959     case TS_ABSOLUTE_WITH_DATE:
960       col_set_abs_date_time(fd, cinfo, col);
961       break;
962
963     case TS_RELATIVE:
964       col_set_rel_time(fd, cinfo, col);
965       break;
966
967     case TS_DELTA:
968       col_set_delta_time(fd, cinfo, col);
969       break;
970
971     case TS_DELTA_DIS:
972       col_set_delta_time_dis(fd, cinfo, col);
973       break;
974
975     case TS_EPOCH:
976       col_set_epoch_time(fd, cinfo, col);
977       break;
978
979     case TS_NOT_SET:
980       /* code is missing for this case, but I don't know which [jmayer20051219] */
981       g_assert(FALSE);
982       break;
983   }
984 }
985
986 /* Set the format of the variable time format.
987    XXX - this is called from "file.c" when the user changes the time
988    format they want for "command-line-specified" time; it's a bit ugly
989    that we have to export it, but if we go to a CList-like widget that
990    invokes callbacks to get the text for the columns rather than
991    requiring us to stuff the text into the widget from outside, we
992    might be able to clean this up. */
993 void
994 col_set_fmt_time(frame_data *fd, column_info *cinfo, gint fmt, gint col)
995 {
996   switch (fmt) {
997     case COL_CLS_TIME:
998        col_set_cls_time(fd, cinfo, col);
999       break;
1000
1001     case COL_ABS_TIME:
1002       col_set_abs_time(fd, cinfo, col);
1003       break;
1004
1005     case COL_ABS_DATE_TIME:
1006       col_set_abs_date_time(fd, cinfo, col);
1007       break;
1008
1009     case COL_REL_TIME:
1010       col_set_rel_time(fd, cinfo, col);
1011       break;
1012
1013     case COL_DELTA_TIME:
1014       col_set_delta_time(fd, cinfo, col);
1015       break;
1016
1017     case COL_DELTA_TIME_DIS:
1018       col_set_delta_time_dis(fd, cinfo, col);
1019       break;
1020
1021     case COL_REL_CONV_TIME:
1022     case COL_DELTA_CONV_TIME:
1023       /* Will be set by various dissectors */
1024       break;
1025
1026     default:
1027       g_assert_not_reached();
1028       break;
1029   }
1030 }
1031
1032 void
1033 col_set_time(column_info *cinfo, gint el, nstime_t *ts, char *fieldname)
1034 {
1035   int   col;
1036
1037   g_assert(cinfo->col_first[el] >= 0);
1038
1039   for (col = cinfo->col_first[el]; col <= cinfo->col_last[el]; col++) {
1040     if (cinfo->fmt_matx[col][el]) {
1041       switch(timestamp_get_precision()) {
1042         case(TS_PREC_FIXED_SEC):
1043         case(TS_PREC_AUTO_SEC):
1044           display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1045             (gint32) ts->secs, ts->nsecs / 1000000000, SECS);
1046           break;
1047         case(TS_PREC_FIXED_DSEC):
1048         case(TS_PREC_AUTO_DSEC):
1049           display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1050             (gint32) ts->secs, ts->nsecs / 100000000, DSECS);
1051           break;
1052         case(TS_PREC_FIXED_CSEC):
1053         case(TS_PREC_AUTO_CSEC):
1054           display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1055             (gint32) ts->secs, ts->nsecs / 10000000, CSECS);
1056           break;
1057         case(TS_PREC_FIXED_MSEC):
1058         case(TS_PREC_AUTO_MSEC):
1059           display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1060             (gint32) ts->secs, ts->nsecs / 1000000, MSECS);
1061           break;
1062         case(TS_PREC_FIXED_USEC):
1063         case(TS_PREC_AUTO_USEC):
1064           display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1065             (gint32) ts->secs, ts->nsecs / 1000, USECS);
1066           break;
1067         case(TS_PREC_FIXED_NSEC):
1068         case(TS_PREC_AUTO_NSEC):
1069           display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1070             (gint32) ts->secs, ts->nsecs, NSECS);
1071           break;
1072         default:
1073           g_assert_not_reached();
1074       }
1075       cinfo->col_data[col] = cinfo->col_buf[col];
1076       g_strlcpy(cinfo->col_expr.col_expr[col],fieldname,COL_MAX_LEN);
1077       g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],
1078                 COL_MAX_LEN);
1079     }
1080   }
1081 }
1082
1083 static void
1084 col_set_addr(packet_info *pinfo, int col, address *addr, gboolean is_res,
1085              gboolean is_src)
1086 {
1087   struct e_in6_addr ipv6_addr;
1088
1089   pinfo->cinfo->col_expr.col_expr[col][0] = '\0';
1090   pinfo->cinfo->col_expr.col_expr_val[col][0] = '\0';
1091
1092   if (addr->type == AT_NONE)
1093     return;     /* no address, nothing to do */
1094
1095   if (is_res) {
1096     get_addr_name_buf(addr, pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1097   } else {
1098     address_to_str_buf(addr, pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1099   }
1100   pinfo->cinfo->col_data[col] = pinfo->cinfo->col_buf[col];
1101
1102   switch (addr->type) {
1103
1104   case AT_ETHER:
1105     if (is_src)
1106       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "eth.src",COL_MAX_LEN);
1107     else
1108       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "eth.dst",COL_MAX_LEN);
1109     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], ether_to_str(addr->data), COL_MAX_LEN);
1110     break;
1111
1112   case AT_IPv4:
1113     if (is_src)
1114       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ip.src", COL_MAX_LEN);
1115     else
1116       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ip.dst",COL_MAX_LEN);
1117     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], ip_to_str(addr->data), COL_MAX_LEN);
1118     break;
1119
1120   case AT_IPv6:
1121     if (is_src)
1122       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ipv6.src", COL_MAX_LEN);
1123     else
1124       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ipv6.dst", COL_MAX_LEN);
1125     memcpy(&ipv6_addr.bytes, addr->data, sizeof ipv6_addr.bytes);
1126     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], ip6_to_str(&ipv6_addr), COL_MAX_LEN);
1127     break;
1128
1129   case AT_ATALK:
1130     if (is_src)
1131       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ddp.src", COL_MAX_LEN);
1132     else
1133       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ddp.dst", COL_MAX_LEN);
1134     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1135     break;
1136
1137   case AT_ARCNET:
1138     if (is_src)
1139       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "arcnet.src",
1140          COL_MAX_LEN);
1141     else
1142       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "arcnet.dst", 
1143         COL_MAX_LEN);
1144     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1145     break;
1146
1147   case AT_URI:
1148     if (is_src)
1149       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "uri.src", COL_MAX_LEN);
1150     else
1151       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "uri.dst", COL_MAX_LEN);
1152     address_to_str_buf(addr, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1153     break;
1154
1155   default:
1156     break;
1157   }
1158 }
1159
1160 static void
1161 col_set_port(packet_info *pinfo, int col, gboolean is_res, gboolean is_src)
1162 {
1163   guint32 port;
1164
1165   if (is_src)
1166     port = pinfo->srcport;
1167   else
1168     port = pinfo->destport;
1169   pinfo->cinfo->col_expr.col_expr[col][0] = '\0';
1170   pinfo->cinfo->col_expr.col_expr_val[col][0] = '\0';
1171   switch (pinfo->ptype) {
1172
1173   case PT_SCTP:
1174     if (is_res)
1175       g_strlcpy(pinfo->cinfo->col_buf[col], get_sctp_port(port), COL_MAX_LEN);
1176     else
1177       g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
1178     break;
1179
1180   case PT_TCP:
1181     if (is_res)
1182       g_strlcpy(pinfo->cinfo->col_buf[col], get_tcp_port(port), COL_MAX_LEN);
1183     else
1184       g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
1185     if (is_src)
1186       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "tcp.srcport",
1187         COL_MAX_LEN);
1188     else
1189       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "tcp.dstport",
1190         COL_MAX_LEN);
1191     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "%u", port);
1192     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1193     break;
1194
1195   case PT_UDP:
1196     if (is_res)
1197       g_strlcpy(pinfo->cinfo->col_buf[col], get_udp_port(port), COL_MAX_LEN);
1198     else
1199       g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
1200     if (is_src)
1201       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "udp.srcport",
1202         COL_MAX_LEN);
1203     else
1204       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "udp.dstport",
1205         COL_MAX_LEN);
1206     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "%u", port);
1207     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1208     break;
1209
1210   case PT_DDP:
1211     if (is_src)
1212       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ddp.src_socket",
1213         COL_MAX_LEN);
1214     else
1215       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ddp.dst_socket",
1216         COL_MAX_LEN);
1217     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", port);
1218     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "%u", port);
1219     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1220     break;
1221
1222   case PT_IPX:
1223     /* XXX - resolve IPX socket numbers */
1224     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "0x%04x", port);
1225     if (is_src)
1226       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ipx.src.socket",
1227         COL_MAX_LEN);
1228     else
1229       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "ipx.dst.socket",
1230         COL_MAX_LEN);
1231     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "0x%04x", port);
1232     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1233     break;
1234
1235   case PT_IDP:
1236     /* XXX - resolve IDP socket numbers */
1237     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "0x%04x", port);
1238     if (is_src)
1239       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "idp.src.socket",
1240         COL_MAX_LEN);
1241     else
1242       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "idp.dst.socket",
1243         COL_MAX_LEN);
1244     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "0x%04x", port);
1245     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1246     break;
1247
1248   case PT_USB:
1249     /* XXX - resolve USB endpoint numbers */
1250     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "0x%08x", port);
1251     if (is_src)
1252       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "usb.src.endpoint",
1253         COL_MAX_LEN);
1254     else
1255       g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "usb.dst.endpoint",
1256         COL_MAX_LEN);
1257     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "0x%08x", port);
1258     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1259     break;
1260
1261   default:
1262     break;
1263   }
1264   pinfo->cinfo->col_buf[col][COL_MAX_LEN - 1] = '\0';
1265   pinfo->cinfo->col_data[col] = pinfo->cinfo->col_buf[col];
1266 }
1267
1268 /*
1269  * XXX - this should be in some common code in the epan directory, shared
1270  * by this code and packet-isdn.c.
1271  */
1272 static const value_string channel_vals[] = {
1273         { 0,    "D" },
1274         { 1,    "B1" },
1275         { 2,    "B2" },
1276         { 3,    "B3" },
1277         { 4,    "B4" },
1278         { 5,    "B5" },
1279         { 6,    "B6" },
1280         { 7,    "B7" },
1281         { 8,    "B8" },
1282         { 9,    "B9" },
1283         { 10,   "B10" },
1284         { 11,   "B11" },
1285         { 12,   "B12" },
1286         { 13,   "B13" },
1287         { 14,   "B14" },
1288         { 15,   "B15" },
1289         { 16,   "B16" },
1290         { 17,   "B17" },
1291         { 18,   "B19" },
1292         { 19,   "B19" },
1293         { 20,   "B20" },
1294         { 21,   "B21" },
1295         { 22,   "B22" },
1296         { 23,   "B23" },
1297         { 24,   "B24" },
1298         { 25,   "B25" },
1299         { 26,   "B26" },
1300         { 27,   "B27" },
1301         { 28,   "B29" },
1302         { 29,   "B29" },
1303         { 30,   "B30" },
1304         { 0,    NULL }
1305 };
1306
1307 static void
1308 col_set_circuit_id(packet_info *pinfo, int col)
1309 {
1310   pinfo->cinfo->col_expr.col_expr[col][0] = '\0';
1311   pinfo->cinfo->col_expr.col_expr_val[col][0] = '\0';
1312   switch (pinfo->ctype) {
1313
1314   case CT_DLCI:
1315     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", pinfo->circuit_id);
1316     g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "fr.dlci", COL_MAX_LEN);
1317     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "%u", pinfo->circuit_id);
1318     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1319     break;
1320
1321   case CT_ISDN:
1322     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%s",
1323              val_to_str(pinfo->circuit_id, channel_vals, "Unknown (%u)"));
1324     g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "isdn.channel",
1325         COL_MAX_LEN);
1326     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "%u", pinfo->circuit_id);
1327     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1328     break;
1329
1330   case CT_X25:
1331     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", pinfo->circuit_id);
1332     break;
1333
1334   case CT_ISUP:
1335     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "%u", pinfo->circuit_id);
1336     g_strlcpy(pinfo->cinfo->col_expr.col_expr[col], "isup.cic", COL_MAX_LEN);
1337     g_snprintf(pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN, "%u", pinfo->circuit_id);
1338     pinfo->cinfo->col_expr.col_expr_val[col][COL_MAX_LEN - 1] = '\0';
1339     break;
1340
1341   default:
1342     break;
1343   }
1344   pinfo->cinfo->col_buf[col][COL_MAX_LEN - 1] = '\0';
1345   pinfo->cinfo->col_data[col] = pinfo->cinfo->col_buf[col];
1346 }
1347
1348 void
1349 col_fill_in(packet_info *pinfo)
1350 {
1351   int i;
1352
1353   for (i = 0; i < pinfo->cinfo->num_cols; i++) {
1354     switch (pinfo->cinfo->col_fmt[i]) {
1355
1356     case COL_NUMBER:
1357       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "%u", pinfo->fd->num);
1358       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1359       g_strlcpy(pinfo->cinfo->col_expr.col_expr[i], "frame.number",
1360         COL_MAX_LEN);
1361       g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[i], pinfo->cinfo->col_buf[i], COL_MAX_LEN);
1362       break;
1363
1364     case COL_CLS_TIME:
1365        col_set_cls_time(pinfo->fd, pinfo->cinfo, i);
1366       break;
1367
1368     case COL_ABS_TIME:
1369       col_set_abs_time(pinfo->fd, pinfo->cinfo, i);
1370       break;
1371
1372     case COL_ABS_DATE_TIME:
1373       col_set_abs_date_time(pinfo->fd, pinfo->cinfo, i);
1374       break;
1375
1376     case COL_REL_TIME:
1377       col_set_rel_time(pinfo->fd, pinfo->cinfo, i);
1378       break;
1379
1380     case COL_DELTA_TIME:
1381       col_set_delta_time(pinfo->fd, pinfo->cinfo, i);
1382       break;
1383
1384     case COL_DELTA_TIME_DIS:
1385       col_set_delta_time_dis(pinfo->fd, pinfo->cinfo, i);
1386       break;
1387
1388     case COL_REL_CONV_TIME:
1389     case COL_DELTA_CONV_TIME:
1390       break;            /* Will be set by various dissectors */
1391
1392     case COL_DEF_SRC:
1393     case COL_RES_SRC:   /* COL_DEF_SRC is currently just like COL_RES_SRC */
1394       col_set_addr(pinfo, i, &pinfo->src, TRUE, TRUE);
1395       break;
1396
1397     case COL_UNRES_SRC:
1398       col_set_addr(pinfo, i, &pinfo->src, FALSE, TRUE);
1399       break;
1400
1401     case COL_DEF_DL_SRC:
1402     case COL_RES_DL_SRC:
1403       col_set_addr(pinfo, i, &pinfo->dl_src, TRUE, TRUE);
1404       break;
1405
1406     case COL_UNRES_DL_SRC:
1407       col_set_addr(pinfo, i, &pinfo->dl_src, FALSE, TRUE);
1408       break;
1409
1410     case COL_DEF_NET_SRC:
1411     case COL_RES_NET_SRC:
1412       col_set_addr(pinfo, i, &pinfo->net_src, TRUE, TRUE);
1413       break;
1414
1415     case COL_UNRES_NET_SRC:
1416       col_set_addr(pinfo, i, &pinfo->net_src, FALSE, TRUE);
1417       break;
1418
1419     case COL_DEF_DST:
1420     case COL_RES_DST:   /* COL_DEF_DST is currently just like COL_RES_DST */
1421       col_set_addr(pinfo, i, &pinfo->dst, TRUE, FALSE);
1422       break;
1423
1424     case COL_UNRES_DST:
1425       col_set_addr(pinfo, i, &pinfo->dst, FALSE, FALSE);
1426       break;
1427
1428     case COL_DEF_DL_DST:
1429     case COL_RES_DL_DST:
1430       col_set_addr(pinfo, i, &pinfo->dl_dst, TRUE, FALSE);
1431       break;
1432
1433     case COL_UNRES_DL_DST:
1434       col_set_addr(pinfo, i, &pinfo->dl_dst, FALSE, FALSE);
1435       break;
1436
1437     case COL_DEF_NET_DST:
1438     case COL_RES_NET_DST:
1439       col_set_addr(pinfo, i, &pinfo->net_dst, TRUE, FALSE);
1440       break;
1441
1442     case COL_UNRES_NET_DST:
1443       col_set_addr(pinfo, i, &pinfo->net_dst, FALSE, FALSE);
1444       break;
1445
1446     case COL_DEF_SRC_PORT:
1447     case COL_RES_SRC_PORT:      /* COL_DEF_SRC_PORT is currently just like COL_RES_SRC_PORT */
1448       col_set_port(pinfo, i, TRUE, TRUE);
1449       break;
1450
1451     case COL_UNRES_SRC_PORT:
1452       col_set_port(pinfo, i, FALSE, TRUE);
1453       break;
1454
1455     case COL_DEF_DST_PORT:
1456     case COL_RES_DST_PORT:      /* COL_DEF_DST_PORT is currently just like COL_RES_DST_PORT */
1457       col_set_port(pinfo, i, TRUE, FALSE);
1458       break;
1459
1460     case COL_UNRES_DST_PORT:
1461       col_set_port(pinfo, i, FALSE, FALSE);
1462       break;
1463
1464     case COL_PROTOCOL:  /* currently done by dissectors */
1465     case COL_INFO:      /* currently done by dissectors */
1466       break;
1467
1468     case COL_PACKET_LENGTH:
1469       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "%u", pinfo->fd->pkt_len);
1470       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1471       g_strlcpy(pinfo->cinfo->col_expr.col_expr[i], "frame.len",
1472         COL_MAX_LEN);
1473       g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[i], pinfo->cinfo->col_buf[i], COL_MAX_LEN);
1474       break;
1475
1476     case COL_CUMULATIVE_BYTES:
1477       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "%u", pinfo->fd->cum_bytes);
1478       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1479       break;
1480
1481     case COL_OXID:
1482       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "0x%x", pinfo->oxid);
1483       pinfo->cinfo->col_buf[i][COL_MAX_LEN - 1] = '\0';
1484       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1485       break;
1486
1487     case COL_RXID:
1488       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "0x%x", pinfo->rxid);
1489       pinfo->cinfo->col_buf[i][COL_MAX_LEN - 1] = '\0';
1490       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1491       break;
1492
1493     case COL_IF_DIR:    /* currently done by dissectors */
1494       break;
1495
1496     case COL_CIRCUIT_ID:
1497       col_set_circuit_id(pinfo, i);
1498       break;
1499
1500     case COL_SRCIDX:
1501       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "0x%x", pinfo->src_idx);
1502       pinfo->cinfo->col_buf[i][COL_MAX_LEN - 1] = '\0';
1503       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1504       break;
1505
1506     case COL_DSTIDX:
1507       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "0x%x", pinfo->dst_idx);
1508       pinfo->cinfo->col_buf[i][COL_MAX_LEN - 1] = '\0';
1509       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1510       break;
1511
1512     case COL_VSAN:
1513       g_snprintf(pinfo->cinfo->col_buf[i], COL_MAX_LEN, "%u", pinfo->vsan);
1514       pinfo->cinfo->col_buf[i][COL_MAX_LEN - 1] = '\0';
1515       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1516       break;
1517
1518     case COL_HPUX_SUBSYS: /* done by nettl disector */
1519     case COL_HPUX_DEVID:  /* done by nettl disector */
1520       break;
1521
1522     case COL_DCE_CALL:  /* done by dcerpc */
1523       break;
1524
1525     case COL_DCE_CTX:   /* done by dcerpc */
1526       break;
1527
1528     case COL_8021Q_VLAN_ID:
1529         break;
1530
1531     case COL_DSCP_VALUE:        /* done by packet-ip.c */
1532         break;
1533
1534     case COL_COS_VALUE:         /* done by packet-vlan.c */
1535         break;
1536
1537     case COL_FR_DLCI:   /* done by packet-fr.c */
1538     case COL_BSSGP_TLLI: /* done by packet-bssgp.c */
1539         break;
1540
1541     case COL_EXPERT:    /* done by expert.c */
1542         break;
1543
1544     case COL_FREQ_CHAN:    /* done by radio dissectors */
1545         break;
1546
1547     case COL_CUSTOM:     /* done by col_custom_set_fstr() called from proto.c */
1548         break;
1549
1550     case NUM_COL_FMTS:  /* keep compiler happy - shouldn't get here */
1551       g_assert_not_reached();
1552       break;
1553     }
1554   }
1555 }