Revert SVN #32360 until Windows compilation errors corrected.
[obnox/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 /* Allocate all the data structures for constructing column data, given
49    the number of columns. */
50 void
51 col_setup(column_info *cinfo, gint num_cols)
52 {
53   int i;
54
55   cinfo->num_cols   = num_cols;
56   cinfo->col_fmt    = g_new(gint, num_cols);
57   cinfo->fmt_matx   = g_new(gboolean*, num_cols);
58   cinfo->col_first  = g_new(int, NUM_COL_FMTS);
59   cinfo->col_last   = g_new(int, NUM_COL_FMTS);
60   cinfo->col_title  = g_new(gchar*, num_cols);
61   cinfo->col_custom_field = g_new(gchar*, num_cols);
62   cinfo->col_custom_field_id = g_new(int, num_cols);
63   cinfo->col_custom_dfilter = g_new(dfilter_t*, num_cols);
64   cinfo->col_data   = (const gchar **)g_new(gchar*, num_cols);
65   cinfo->col_buf    = g_new(gchar*, num_cols);
66   cinfo->col_fence  = g_new(int, num_cols);
67   cinfo->col_expr.col_expr = (const gchar **) g_new(gchar*, num_cols + 1);
68   cinfo->col_expr.col_expr_val = g_new(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   if (!cinfo)
83     return;
84
85   for (i = 0; i < cinfo->num_cols; i++) {
86     cinfo->col_buf[i][0] = '\0';
87     cinfo->col_data[i] = cinfo->col_buf[i];
88     cinfo->col_fence[i] = 0;
89     cinfo->col_expr.col_expr[i] = "";
90     cinfo->col_expr.col_expr_val[i][0] = '\0';
91   }
92   cinfo->writable = TRUE;
93 }
94
95 #define COL_GET_WRITABLE(cinfo) (cinfo ? cinfo->writable : FALSE)
96
97 gboolean
98 col_get_writable(column_info *cinfo)
99 {
100     return COL_GET_WRITABLE(cinfo);
101 }
102
103 void
104 col_set_writable(column_info *cinfo, gboolean writable)
105 {
106     if (cinfo)
107         cinfo->writable = writable;
108 }
109
110 /* Checks to see if a particular packet information element is needed for the packet list */
111 #define CHECK_COL(cinfo, el) \
112     /* We are constructing columns, and they're writable */ \
113     (COL_GET_WRITABLE(cinfo) && \
114       /* There is at least one column in that format */ \
115     ((cinfo)->col_first[el] >= 0))
116
117 gint
118 check_col(column_info *cinfo, gint el)
119 {
120   return CHECK_COL(cinfo, el);
121 }
122
123 /* Sets the fence for a column to be at the end of the column. */
124 void
125 col_set_fence(column_info *cinfo, gint el)
126 {
127   int i;
128
129   if (!CHECK_COL(cinfo, el))
130     return;
131
132   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
133     if (cinfo->fmt_matx[i][el]) {
134       cinfo->col_fence[i] = (int)strlen(cinfo->col_data[i]);
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   if (!CHECK_COL(cinfo, el))
152     return;
153
154   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
155     if (cinfo->fmt_matx[i][el]) {
156       /*
157        * At this point, either
158        *
159        *   1) col_data[i] is equal to col_buf[i], in which case we
160        *      don't have to worry about copying col_data[i] to
161        *      col_buf[i];
162        *
163        *   2) col_data[i] isn't equal to col_buf[i], in which case
164        *      the only thing that's been done to the column is
165        *      "col_set_str()" calls and possibly "col_set_fence()"
166        *      calls, in which case the fence is either unset and
167        *      at the beginning of the string or set and at the end
168        *      of the string - if it's at the beginning, we're just
169        *      going to clear the column, and if it's at the end,
170        *      we don't do anything.
171        */
172       fence = cinfo->col_fence[i];
173       if (cinfo->col_buf[i] == cinfo->col_data[i] || fence == 0) {
174         /*
175          * The fence isn't at the end of the column, or the column wasn't
176          * last set with "col_set_str()", so clear the column out.
177          */
178         cinfo->col_buf[i][fence] = '\0';
179         cinfo->col_data[i] = cinfo->col_buf[i];
180       }
181       cinfo->col_expr.col_expr[i] = "";
182       cinfo->col_expr.col_expr_val[i][0] = '\0';
183     }
184   }
185 }
186
187 #define COL_CHECK_APPEND(cinfo, i, max_len) \
188   if (cinfo->col_data[i] != cinfo->col_buf[i]) {        \
189     /* This was set with "col_set_str()"; copy the string they  \
190        set it to into the buffer, so we can append to it. */    \
191     g_strlcpy(cinfo->col_buf[i], cinfo->col_data[i], max_len);  \
192     cinfo->col_data[i] = cinfo->col_buf[i];         \
193   }
194
195 #define COL_CHECK_REF_TIME(fd, buf)         \
196   if(fd->flags.ref_time){                   \
197     g_strlcpy(buf, "*REF*", COL_MAX_LEN );  \
198     return;                                 \
199   }
200
201 /* The same as CHECK_COL(), but without the check to see if the column is writable. */
202 #define HAVE_CUSTOM_COLS(cinfo) ((cinfo) && (cinfo)->col_first[COL_CUSTOM] >= 0)
203
204 gboolean
205 have_custom_cols(column_info *cinfo)
206 {
207   return HAVE_CUSTOM_COLS(cinfo);
208 }
209
210 /* search in edt tree custom fields */
211 void col_custom_set_edt(epan_dissect_t *edt, column_info *cinfo)
212 {
213   int i;
214
215   if(!HAVE_CUSTOM_COLS(cinfo))
216       return;
217
218   for (i = cinfo->col_first[COL_CUSTOM];
219        i <= cinfo->col_last[COL_CUSTOM]; i++) {
220     if (cinfo->fmt_matx[i][COL_CUSTOM] &&
221         cinfo->col_custom_field[i] &&
222         cinfo->col_custom_field_id[i] != -1) {
223        cinfo->col_data[i] = cinfo->col_buf[i];
224        cinfo->col_expr.col_expr[i] = epan_custom_set(edt, cinfo->col_custom_field_id[i],
225                                      cinfo->col_buf[i],
226                                      cinfo->col_expr.col_expr_val[i],
227                                      COL_MAX_LEN);
228     }
229   }
230 }
231
232 void
233 col_custom_prime_edt(epan_dissect_t *edt, column_info *cinfo)
234 {
235   int i;
236
237   if(!HAVE_CUSTOM_COLS(cinfo))
238       return;
239
240   for (i = cinfo->col_first[COL_CUSTOM];
241        i <= cinfo->col_last[COL_CUSTOM]; i++) {
242
243     cinfo->col_custom_field_id[i] = -1;
244     if (cinfo->fmt_matx[i][COL_CUSTOM] &&
245         cinfo->col_custom_dfilter[i]){
246         epan_dissect_prime_dfilter(edt, cinfo->col_custom_dfilter[i]);
247         if (cinfo->col_custom_field) {
248             header_field_info* hfinfo = proto_registrar_get_byname(cinfo->col_custom_field[i]);
249             g_assert(hfinfo);
250             cinfo->col_custom_field_id[i] = hfinfo->id;
251         }
252     }
253   }
254 }
255
256 static void
257 col_do_append_sep_va_fstr(column_info *cinfo, gint el, const gchar *separator,
258               const gchar *format, va_list ap)
259 {
260   int  i;
261   int  len, max_len, sep_len;
262
263   if (el == COL_INFO)
264     max_len = COL_MAX_INFO_LEN;
265   else
266     max_len = COL_MAX_LEN;
267
268   if (separator == NULL)
269     sep_len = 0;
270   else
271     sep_len = (int) strlen(separator);
272   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
273     if (cinfo->fmt_matx[i][el]) {
274       /*
275        * First arrange that we can append, if necessary.
276        */
277       COL_CHECK_APPEND(cinfo, i, max_len);
278
279       len = (int) strlen(cinfo->col_buf[i]);
280
281       /*
282        * If we have a separator, append it if the column isn't empty.
283        */
284       if (separator != NULL) {
285         if (len != 0) {
286           g_strlcat(cinfo->col_buf[i], separator, max_len);
287           len += sep_len;
288         }
289       }
290       g_vsnprintf(&cinfo->col_buf[i][len], max_len - len, format, ap);
291     }
292   }
293 }
294
295 /* Appends a vararg list to a packet info string. */
296 void
297 col_append_fstr(column_info *cinfo, gint el, const gchar *format, ...)
298 {
299   va_list ap;
300
301   if (!CHECK_COL(cinfo, el))
302     return;
303
304   va_start(ap, format);
305   col_do_append_sep_va_fstr(cinfo, el, NULL, format, ap);
306   va_end(ap);
307 }
308
309 /* Appends a vararg list to a packet info string.
310  * Prefixes it with the given separator if the column is not empty. */
311 void
312 col_append_sep_fstr(column_info *cinfo, gint el, const gchar *separator,
313         const gchar *format, ...)
314 {
315   va_list ap;
316
317   if (!CHECK_COL(cinfo, el))
318     return;
319
320   if (separator == NULL)
321     separator = ", ";    /* default */
322   va_start(ap, format);
323   col_do_append_sep_va_fstr(cinfo, el, separator, format, ap);
324   va_end(ap);
325 }
326
327
328
329 /* Prepends a vararg list to a packet info string. */
330 #define COL_BUF_MAX_LEN (((COL_MAX_INFO_LEN) > (COL_MAX_LEN)) ? \
331     (COL_MAX_INFO_LEN) : (COL_MAX_LEN))
332 void
333 col_prepend_fstr(column_info *cinfo, gint el, const gchar *format, ...)
334 {
335   va_list     ap;
336   int         i;
337   char        orig_buf[COL_BUF_MAX_LEN];
338   const char *orig;
339   int         max_len;
340
341   if (!CHECK_COL(cinfo, el))
342     return;
343
344   if (el == COL_INFO)
345     max_len = COL_MAX_INFO_LEN;
346   else
347     max_len = COL_MAX_LEN;
348
349   va_start(ap, format);
350   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
351     if (cinfo->fmt_matx[i][el]) {
352       if (cinfo->col_data[i] != cinfo->col_buf[i]) {
353         /* This was set with "col_set_str()"; which is effectively const */
354         orig = cinfo->col_data[i];
355       } else {
356         g_strlcpy(orig_buf, cinfo->col_buf[i], max_len);
357         orig = orig_buf;
358       }
359       g_vsnprintf(cinfo->col_buf[i], max_len, format, ap);
360
361       /*
362        * Move the fence, unless it's at the beginning of the string.
363        */
364       if (cinfo->col_fence[i] > 0)
365         cinfo->col_fence[i] += (int) strlen(cinfo->col_buf[i]);
366
367       g_strlcat(cinfo->col_buf[i], orig, max_len);
368       cinfo->col_data[i] = cinfo->col_buf[i];
369     }
370   }
371   va_end(ap);
372 }
373 void
374 col_prepend_fence_fstr(column_info *cinfo, gint el, const gchar *format, ...)
375 {
376   va_list     ap;
377   int         i;
378   char        orig_buf[COL_BUF_MAX_LEN];
379   const char *orig;
380   int         max_len;
381
382   if (!CHECK_COL(cinfo, el))
383     return;
384
385   if (el == COL_INFO)
386     max_len = COL_MAX_INFO_LEN;
387   else
388     max_len = COL_MAX_LEN;
389
390   va_start(ap, format);
391   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
392     if (cinfo->fmt_matx[i][el]) {
393       if (cinfo->col_data[i] != cinfo->col_buf[i]) {
394         /* This was set with "col_set_str()"; which is effectively const */
395         orig = cinfo->col_data[i];
396       } else {
397         g_strlcpy(orig_buf, cinfo->col_buf[i], max_len);
398         orig = orig_buf;
399       }
400       g_vsnprintf(cinfo->col_buf[i], max_len, format, ap);
401
402       /*
403        * Move the fence if it exists, else create a new fence at the
404        * end of the prepended data.
405        */
406       if (cinfo->col_fence[i] > 0) {
407         cinfo->col_fence[i] += (int) strlen(cinfo->col_buf[i]);
408       } else {
409         cinfo->col_fence[i]  = (int) strlen(cinfo->col_buf[i]);
410       }
411       g_strlcat(cinfo->col_buf[i], orig, max_len);
412       cinfo->col_data[i] = cinfo->col_buf[i];
413     }
414   }
415   va_end(ap);
416 }
417
418 /* Use this if "str" points to something that won't stay around (and
419    must thus be copied). */
420 void
421 col_add_str(column_info *cinfo, gint el, const gchar* str)
422 {
423   int    i;
424   int    fence;
425   size_t max_len;
426
427   if (!CHECK_COL(cinfo, el))
428     return;
429
430   if (el == COL_INFO)
431     max_len = COL_MAX_INFO_LEN;
432   else
433     max_len = COL_MAX_LEN;
434
435   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
436     if (cinfo->fmt_matx[i][el]) {
437       fence = cinfo->col_fence[i];
438       if (fence != 0) {
439         /*
440          * We will append the string after the fence.
441          * First arrange that we can append, if necessary.
442          */
443         COL_CHECK_APPEND(cinfo, i, max_len);
444       } else {
445         /*
446          * There's no fence, so we can just write to the string.
447          */
448         cinfo->col_data[i] = cinfo->col_buf[i];
449       }
450       g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence);
451     }
452   }
453 }
454
455 /* Use this if "str" points to something that will stay around (and thus
456    needn't be copied). */
457 void
458 col_set_str(column_info *cinfo, gint el, const gchar* str)
459 {
460   int i;
461   int fence;
462   size_t max_len;
463
464   DISSECTOR_ASSERT(str);
465
466   /* The caller is expected to pass in something that 'will stay around' and
467    * something from the ephemeral pool certainly doesn't fit the bill. */
468   DISSECTOR_ASSERT(!ep_verify_pointer(str));
469
470   if (!CHECK_COL(cinfo, el))
471     return;
472
473   if (el == COL_INFO)
474     max_len = COL_MAX_INFO_LEN;
475   else
476     max_len = COL_MAX_LEN;
477
478   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
479     if (cinfo->fmt_matx[i][el]) {
480       fence = cinfo->col_fence[i];
481       if (fence != 0) {
482         /*
483          * We will append the string after the fence.
484          * First arrange that we can append, if necessary.
485          */
486         COL_CHECK_APPEND(cinfo, i, max_len);
487
488         g_strlcpy(&cinfo->col_buf[i][fence], str, max_len - fence);
489       } else {
490         /*
491          * There's no fence, so we can just set the column to point
492          * to the string.
493          */
494         cinfo->col_data[i] = str;
495       }
496     }
497   }
498 }
499
500 /* Adds a vararg list to a packet info string. */
501 void
502 col_add_fstr(column_info *cinfo, gint el, const gchar *format, ...) {
503   va_list ap;
504   int     i;
505   int     fence;
506   int     max_len;
507
508   if (!CHECK_COL(cinfo, el))
509     return;
510
511   if (el == COL_INFO)
512     max_len = COL_MAX_INFO_LEN;
513   else
514     max_len = COL_MAX_LEN;
515
516   va_start(ap, format);
517   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
518     if (cinfo->fmt_matx[i][el]) {
519       fence = cinfo->col_fence[i];
520       if (fence != 0) {
521         /*
522          * We will append the string after the fence.
523          * First arrange that we can append, if necessary.
524          */
525         COL_CHECK_APPEND(cinfo, i, max_len);
526       } else {
527         /*
528          * There's no fence, so we can just write to the string.
529          */
530         cinfo->col_data[i] = cinfo->col_buf[i];
531       }
532       g_vsnprintf(&cinfo->col_buf[i][fence], max_len - fence, format, ap);
533     }
534   }
535   va_end(ap);
536 }
537
538 static void
539 col_do_append_str(column_info *cinfo, gint el, const gchar* separator,
540     const gchar* str)
541 {
542   int    i;
543   size_t len, max_len;
544
545   if (el == COL_INFO)
546     max_len = COL_MAX_INFO_LEN;
547   else
548     max_len = COL_MAX_LEN;
549
550   for (i = cinfo->col_first[el]; i <= cinfo->col_last[el]; i++) {
551     if (cinfo->fmt_matx[i][el]) {
552       /*
553        * First arrange that we can append, if necessary.
554        */
555       COL_CHECK_APPEND(cinfo, i, max_len);
556
557       len = cinfo->col_buf[i][0];
558
559       /*
560        * If we have a separator, append it if the column isn't empty.
561        */
562       if (separator != NULL) {
563         if (len != 0) {
564           g_strlcat(cinfo->col_buf[i], separator, max_len);
565         }
566       }
567       g_strlcat(cinfo->col_buf[i], str, max_len);
568     }
569   }
570 }
571
572 void
573 col_append_str(column_info *cinfo, gint el, const gchar* str)
574 {
575   if (!CHECK_COL(cinfo, el))
576     return;
577
578   col_do_append_str(cinfo, el, NULL, str);
579 }
580
581 void
582 col_append_sep_str(column_info *cinfo, gint el, const gchar* separator,
583     const gchar* str)
584 {
585   if (!CHECK_COL(cinfo, el))
586     return;
587
588   if (separator == NULL)
589     separator = ", ";    /* default */
590
591   col_do_append_str(cinfo, el, separator, str);
592 }
593
594 /* --------------------------------- */
595 gboolean
596 col_has_time_fmt(column_info *cinfo, gint col)
597 {
598   return ((cinfo->fmt_matx[col][COL_CLS_TIME]) ||
599           (cinfo->fmt_matx[col][COL_ABS_TIME]) ||
600           (cinfo->fmt_matx[col][COL_ABS_DATE_TIME]) ||
601           (cinfo->fmt_matx[col][COL_REL_TIME]) ||
602           (cinfo->fmt_matx[col][COL_DELTA_TIME]) ||
603           (cinfo->fmt_matx[col][COL_DELTA_TIME_DIS]));
604 }
605
606 static gint
607 set_abs_date_time(frame_data *fd, gchar *buf)
608 {
609   struct tm *tmp;
610   time_t then;
611
612   then = fd->abs_ts.secs;
613   tmp = localtime(&then);
614   if (tmp != NULL) {
615       switch(timestamp_get_precision()) {
616       case(TS_PREC_FIXED_SEC):
617       case(TS_PREC_AUTO_SEC):
618           g_snprintf(buf, COL_MAX_LEN,"%04d-%02d-%02d %02d:%02d:%02d",
619              tmp->tm_year + 1900,
620              tmp->tm_mon + 1,
621              tmp->tm_mday,
622              tmp->tm_hour,
623              tmp->tm_min,
624              tmp->tm_sec);
625           break;
626       case(TS_PREC_FIXED_DSEC):
627       case(TS_PREC_AUTO_DSEC):
628           g_snprintf(buf, COL_MAX_LEN,"%04d-%02d-%02d %02d:%02d:%02d.%01ld",
629              tmp->tm_year + 1900,
630              tmp->tm_mon + 1,
631              tmp->tm_mday,
632              tmp->tm_hour,
633              tmp->tm_min,
634              tmp->tm_sec,
635              (long)fd->abs_ts.nsecs / 100000000);
636           break;
637       case(TS_PREC_FIXED_CSEC):
638       case(TS_PREC_AUTO_CSEC):
639           g_snprintf(buf, COL_MAX_LEN,"%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(buf, COL_MAX_LEN, "%04d-%02d-%02d %02d:%02d:%02d.%03ld",
651              tmp->tm_year + 1900,
652              tmp->tm_mon + 1,
653              tmp->tm_mday,
654              tmp->tm_hour,
655              tmp->tm_min,
656              tmp->tm_sec,
657              (long)fd->abs_ts.nsecs / 1000000);
658           break;
659       case(TS_PREC_FIXED_USEC):
660       case(TS_PREC_AUTO_USEC):
661           g_snprintf(buf, COL_MAX_LEN, "%04d-%02d-%02d %02d:%02d:%02d.%06ld",
662              tmp->tm_year + 1900,
663              tmp->tm_mon + 1,
664              tmp->tm_mday,
665              tmp->tm_hour,
666              tmp->tm_min,
667              tmp->tm_sec,
668              (long)fd->abs_ts.nsecs / 1000);
669           break;
670       case(TS_PREC_FIXED_NSEC):
671       case(TS_PREC_AUTO_NSEC):
672           g_snprintf(buf, COL_MAX_LEN, "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
673              tmp->tm_year + 1900,
674              tmp->tm_mon + 1,
675              tmp->tm_mday,
676              tmp->tm_hour,
677              tmp->tm_min,
678              tmp->tm_sec,
679              (long)fd->abs_ts.nsecs);
680           break;
681       default:
682           g_assert_not_reached();
683       }
684   } else {
685     buf[0] = '\0';
686   }
687   return 1;
688 }
689
690 static void
691 col_set_abs_date_time(frame_data *fd, column_info *cinfo, int col)
692 {
693   if (set_abs_date_time(fd, cinfo->col_buf[col])) {
694       cinfo->col_expr.col_expr[col] = "frame.time";
695       g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
696   }
697   cinfo->col_data[col] = cinfo->col_buf[col];
698 }
699
700 static gint
701 set_rel_time(frame_data *fd, gchar *buf)
702 {
703   switch(timestamp_get_precision()) {
704       case(TS_PREC_FIXED_SEC):
705       case(TS_PREC_AUTO_SEC):
706           display_signed_time(buf, COL_MAX_LEN,
707             (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
708           break;
709       case(TS_PREC_FIXED_DSEC):
710       case(TS_PREC_AUTO_DSEC):
711           display_signed_time(buf, COL_MAX_LEN,
712             (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
713           break;
714       case(TS_PREC_FIXED_CSEC):
715       case(TS_PREC_AUTO_CSEC):
716           display_signed_time(buf, COL_MAX_LEN,
717             (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
718           break;
719       case(TS_PREC_FIXED_MSEC):
720       case(TS_PREC_AUTO_MSEC):
721           display_signed_time(buf, COL_MAX_LEN,
722             (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
723           break;
724       case(TS_PREC_FIXED_USEC):
725       case(TS_PREC_AUTO_USEC):
726           display_signed_time(buf, COL_MAX_LEN,
727             (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs / 1000, TO_STR_TIME_RES_T_USECS);
728           break;
729       case(TS_PREC_FIXED_NSEC):
730       case(TS_PREC_AUTO_NSEC):
731           display_signed_time(buf, COL_MAX_LEN,
732             (gint32) fd->rel_ts.secs, fd->rel_ts.nsecs, TO_STR_TIME_RES_T_NSECS);
733           break;
734       default:
735           g_assert_not_reached();
736   }
737   return 1;
738 }
739
740 static void
741 col_set_rel_time(frame_data *fd, column_info *cinfo, int col)
742 {
743   if (set_rel_time(fd, cinfo->col_buf[col])) {
744       cinfo->col_expr.col_expr[col] = "frame.time_relative";
745       g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
746   }
747   cinfo->col_data[col] = cinfo->col_buf[col];
748 }
749
750 static gint
751 set_delta_time(frame_data *fd, gchar *buf)
752 {
753   switch(timestamp_get_precision()) {
754       case(TS_PREC_FIXED_SEC):
755       case(TS_PREC_AUTO_SEC):
756           display_signed_time(buf, COL_MAX_LEN,
757             (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
758           break;
759       case(TS_PREC_FIXED_DSEC):
760       case(TS_PREC_AUTO_DSEC):
761           display_signed_time(buf, COL_MAX_LEN,
762             (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
763           break;
764       case(TS_PREC_FIXED_CSEC):
765       case(TS_PREC_AUTO_CSEC):
766           display_signed_time(buf, COL_MAX_LEN,
767             (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
768           break;
769       case(TS_PREC_FIXED_MSEC):
770       case(TS_PREC_AUTO_MSEC):
771           display_signed_time(buf, COL_MAX_LEN,
772             (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
773           break;
774       case(TS_PREC_FIXED_USEC):
775       case(TS_PREC_AUTO_USEC):
776           display_signed_time(buf, COL_MAX_LEN,
777             (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs / 1000, TO_STR_TIME_RES_T_USECS);
778           break;
779       case(TS_PREC_FIXED_NSEC):
780       case(TS_PREC_AUTO_NSEC):
781           display_signed_time(buf, COL_MAX_LEN,
782             (gint32) fd->del_cap_ts.secs, fd->del_cap_ts.nsecs, TO_STR_TIME_RES_T_NSECS);
783           break;
784       default:
785           g_assert_not_reached();
786   }
787   return 1;
788 }
789
790 static void
791 col_set_delta_time(frame_data *fd, column_info *cinfo, int col)
792 {
793   if (set_delta_time(fd, cinfo->col_buf[col])) {
794       cinfo->col_expr.col_expr[col] = "frame.time_delta";
795       g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
796   }
797   cinfo->col_data[col] = cinfo->col_buf[col];
798 }
799
800 static gint
801 set_delta_time_dis(frame_data *fd, gchar *buf)
802 {
803   switch(timestamp_get_precision()) {
804       case(TS_PREC_FIXED_SEC):
805       case(TS_PREC_AUTO_SEC):
806           display_signed_time(buf, COL_MAX_LEN,
807             (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
808           break;
809       case(TS_PREC_FIXED_DSEC):
810       case(TS_PREC_AUTO_DSEC):
811           display_signed_time(buf, COL_MAX_LEN,
812             (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
813           break;
814       case(TS_PREC_FIXED_CSEC):
815       case(TS_PREC_AUTO_CSEC):
816           display_signed_time(buf, COL_MAX_LEN,
817             (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
818           break;
819       case(TS_PREC_FIXED_MSEC):
820       case(TS_PREC_AUTO_MSEC):
821           display_signed_time(buf, COL_MAX_LEN,
822             (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
823           break;
824       case(TS_PREC_FIXED_USEC):
825       case(TS_PREC_AUTO_USEC):
826           display_signed_time(buf, COL_MAX_LEN,
827             (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs / 1000, TO_STR_TIME_RES_T_USECS);
828           break;
829       case(TS_PREC_FIXED_NSEC):
830       case(TS_PREC_AUTO_NSEC):
831           display_signed_time(buf, COL_MAX_LEN,
832             (gint32) fd->del_dis_ts.secs, fd->del_dis_ts.nsecs, TO_STR_TIME_RES_T_NSECS);
833           break;
834       default:
835           g_assert_not_reached();
836   }
837   return 1;
838 }
839
840 static void
841 col_set_delta_time_dis(frame_data *fd, column_info *cinfo, int col)
842 {
843   if (set_delta_time_dis(fd, cinfo->col_buf[col])) {
844     cinfo->col_expr.col_expr[col] = "frame.time_delta_displayed";
845     g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
846   }
847   cinfo->col_data[col] = cinfo->col_buf[col];
848 }
849
850 static gint
851 set_abs_time(frame_data *fd, gchar *buf)
852 {
853   struct tm *tmp;
854   time_t then;
855
856   then = fd->abs_ts.secs;
857   tmp = localtime(&then);
858   if (tmp != NULL) {
859       switch(timestamp_get_precision()) {
860       case(TS_PREC_FIXED_SEC):
861       case(TS_PREC_AUTO_SEC):
862           g_snprintf(buf, COL_MAX_LEN,"%02d:%02d:%02d",
863              tmp->tm_hour,
864              tmp->tm_min,
865              tmp->tm_sec);
866           break;
867       case(TS_PREC_FIXED_DSEC):
868       case(TS_PREC_AUTO_DSEC):
869           g_snprintf(buf, COL_MAX_LEN,"%02d:%02d:%02d.%01ld",
870              tmp->tm_hour,
871              tmp->tm_min,
872              tmp->tm_sec,
873              (long)fd->abs_ts.nsecs / 100000000);
874           break;
875       case(TS_PREC_FIXED_CSEC):
876       case(TS_PREC_AUTO_CSEC):
877           g_snprintf(buf, COL_MAX_LEN,"%02d:%02d:%02d.%02ld",
878              tmp->tm_hour,
879              tmp->tm_min,
880              tmp->tm_sec,
881              (long)fd->abs_ts.nsecs / 10000000);
882           break;
883       case(TS_PREC_FIXED_MSEC):
884       case(TS_PREC_AUTO_MSEC):
885           g_snprintf(buf, COL_MAX_LEN,"%02d:%02d:%02d.%03ld",
886              tmp->tm_hour,
887              tmp->tm_min,
888              tmp->tm_sec,
889              (long)fd->abs_ts.nsecs / 1000000);
890           break;
891       case(TS_PREC_FIXED_USEC):
892       case(TS_PREC_AUTO_USEC):
893           g_snprintf(buf, COL_MAX_LEN,"%02d:%02d:%02d.%06ld",
894              tmp->tm_hour,
895              tmp->tm_min,
896              tmp->tm_sec,
897              (long)fd->abs_ts.nsecs / 1000);
898           break;
899       case(TS_PREC_FIXED_NSEC):
900       case(TS_PREC_AUTO_NSEC):
901           g_snprintf(buf, COL_MAX_LEN, "%02d:%02d:%02d.%09ld",
902              tmp->tm_hour,
903              tmp->tm_min,
904              tmp->tm_sec,
905              (long)fd->abs_ts.nsecs);
906           break;
907       default:
908           g_assert_not_reached();
909       }
910
911   } else {
912     *buf = '\0';
913   }
914   return 1;
915 }
916
917 static void
918 col_set_abs_time(frame_data *fd, column_info *cinfo, int col)
919 {
920   if (set_abs_time(fd, cinfo->col_buf[col])) {
921       cinfo->col_expr.col_expr[col] = "frame.time";
922       g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
923   }
924   cinfo->col_data[col] = cinfo->col_buf[col];
925 }
926
927 static gint
928 set_epoch_time(frame_data *fd, gchar *buf)
929 {
930   switch(timestamp_get_precision()) {
931       case(TS_PREC_FIXED_SEC):
932       case(TS_PREC_AUTO_SEC):
933           display_epoch_time(buf, COL_MAX_LEN,
934             fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
935           break;
936       case(TS_PREC_FIXED_DSEC):
937       case(TS_PREC_AUTO_DSEC):
938           display_epoch_time(buf, COL_MAX_LEN,
939             fd->abs_ts.secs, fd->abs_ts.nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
940           break;
941       case(TS_PREC_FIXED_CSEC):
942       case(TS_PREC_AUTO_CSEC):
943           display_epoch_time(buf, COL_MAX_LEN,
944             fd->abs_ts.secs, fd->abs_ts.nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
945           break;
946       case(TS_PREC_FIXED_MSEC):
947       case(TS_PREC_AUTO_MSEC):
948           display_epoch_time(buf, COL_MAX_LEN,
949             fd->abs_ts.secs, fd->abs_ts.nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
950           break;
951       case(TS_PREC_FIXED_USEC):
952       case(TS_PREC_AUTO_USEC):
953           display_epoch_time(buf, COL_MAX_LEN,
954             fd->abs_ts.secs, fd->abs_ts.nsecs / 1000, TO_STR_TIME_RES_T_USECS);
955           break;
956       case(TS_PREC_FIXED_NSEC):
957       case(TS_PREC_AUTO_NSEC):
958           display_epoch_time(buf, COL_MAX_LEN,
959             fd->abs_ts.secs, fd->abs_ts.nsecs, TO_STR_TIME_RES_T_NSECS);
960           break;
961       default:
962           g_assert_not_reached();
963   }
964   return 1;
965 }
966
967 static void
968 col_set_epoch_time(frame_data *fd, column_info *cinfo, int col)
969 {
970   if (set_epoch_time(fd, cinfo->col_buf[col])) {
971     cinfo->col_expr.col_expr[col] = "frame.time_delta";
972     g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
973   }
974   cinfo->col_data[col] = cinfo->col_buf[col];
975 }
976
977 #if 0
978 /* Set the format of the variable time format.
979    XXX - this is called from "file.c" when the user changes the time
980    format they want for "command-line-specified" time; it's a bit ugly
981    that we have to export it, but if we go to a CList-like widget that
982    invokes callbacks to get the text for the columns rather than
983    requiring us to stuff the text into the widget from outside, we
984    might be able to clean this up. */
985 void
986 set_cls_time(frame_data *fd, gchar *buf)
987 {
988   COL_CHECK_REF_TIME(fd, cinfo->col_buf[col]);
989
990   switch (timestamp_get_type()) {
991     case TS_ABSOLUTE:
992       set_abs_time(fd, buf);
993       break;
994
995     case TS_ABSOLUTE_WITH_DATE:
996       set_abs_date_time(fd, buf);
997       break;
998
999     case TS_RELATIVE:
1000       set_rel_time(fd, buf);
1001       break;
1002
1003     case TS_DELTA:
1004       set_delta_time(fd, buf);
1005       break;
1006
1007     case TS_DELTA_DIS:
1008       set_delta_time_dis(fd, buf);
1009       break;
1010
1011     case TS_EPOCH:
1012       set_epoch_time(fd, buf);
1013       break;
1014
1015     case TS_NOT_SET:
1016     /* code is missing for this case, but I don't know which [jmayer20051219] */
1017     g_assert(FALSE);
1018         break;
1019   }
1020 }
1021 #endif
1022
1023 static void
1024 col_set_cls_time(frame_data *fd, column_info *cinfo, gint col)
1025 {
1026   switch (timestamp_get_type()) {
1027     case TS_ABSOLUTE:
1028       col_set_abs_time(fd, cinfo, col);
1029       break;
1030
1031     case TS_ABSOLUTE_WITH_DATE:
1032       col_set_abs_date_time(fd, cinfo, col);
1033       break;
1034
1035     case TS_RELATIVE:
1036       col_set_rel_time(fd, cinfo, col);
1037       break;
1038
1039     case TS_DELTA:
1040       col_set_delta_time(fd, cinfo, col);
1041       break;
1042
1043     case TS_DELTA_DIS:
1044       col_set_delta_time_dis(fd, cinfo, col);
1045       break;
1046
1047     case TS_EPOCH:
1048       col_set_epoch_time(fd, cinfo, col);
1049       break;
1050
1051     case TS_NOT_SET:
1052       /* code is missing for this case, but I don't know which [jmayer20051219] */
1053       g_assert_not_reached();
1054       break;
1055   }
1056 }
1057
1058 /* Set the format of the variable time format.
1059    XXX - this is called from "file.c" when the user changes the time
1060    format they want for "command-line-specified" time; it's a bit ugly
1061    that we have to export it, but if we go to a CList-like widget that
1062    invokes callbacks to get the text for the columns rather than
1063    requiring us to stuff the text into the widget from outside, we
1064    might be able to clean this up. */
1065 void
1066 col_set_fmt_time(frame_data *fd, column_info *cinfo, gint fmt, gint col)
1067 {
1068   COL_CHECK_REF_TIME(fd, cinfo->col_buf[col]);
1069
1070   switch (fmt) {
1071     case COL_CLS_TIME:
1072       col_set_cls_time(fd, cinfo, col);
1073       break;
1074
1075     case COL_ABS_TIME:
1076       col_set_abs_time(fd, cinfo, col);
1077       break;
1078
1079     case COL_ABS_DATE_TIME:
1080       col_set_abs_date_time(fd, cinfo, col);
1081       break;
1082
1083     case COL_REL_TIME:
1084       col_set_rel_time(fd, cinfo, col);
1085       break;
1086
1087     case COL_DELTA_TIME:
1088       col_set_delta_time(fd, cinfo, col);
1089       break;
1090
1091     case COL_DELTA_TIME_DIS:
1092       col_set_delta_time_dis(fd, cinfo, col);
1093       break;
1094
1095     default:
1096       g_assert_not_reached();
1097       break;
1098   }
1099 }
1100
1101 /* --------------------------- */
1102 void
1103 col_set_time(column_info *cinfo, gint el, nstime_t *ts, char *fieldname)
1104 {
1105   int col;
1106
1107   if (!CHECK_COL(cinfo, el))
1108     return;
1109
1110   /* TODO: We don't respect fd->flags.ref_time (no way to access 'fd')
1111   COL_CHECK_REF_TIME(fd, buf);
1112   */
1113
1114   for (col = cinfo->col_first[el]; col <= cinfo->col_last[el]; col++) {
1115     if (cinfo->fmt_matx[col][el]) {
1116       switch(timestamp_get_precision()) {
1117     case(TS_PREC_FIXED_SEC):
1118     case(TS_PREC_AUTO_SEC):
1119       display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1120         (gint32) ts->secs, ts->nsecs / 1000000000, TO_STR_TIME_RES_T_SECS);
1121       break;
1122     case(TS_PREC_FIXED_DSEC):
1123     case(TS_PREC_AUTO_DSEC):
1124       display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1125         (gint32) ts->secs, ts->nsecs / 100000000, TO_STR_TIME_RES_T_DSECS);
1126       break;
1127     case(TS_PREC_FIXED_CSEC):
1128     case(TS_PREC_AUTO_CSEC):
1129       display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1130         (gint32) ts->secs, ts->nsecs / 10000000, TO_STR_TIME_RES_T_CSECS);
1131       break;
1132     case(TS_PREC_FIXED_MSEC):
1133     case(TS_PREC_AUTO_MSEC):
1134       display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1135         (gint32) ts->secs, ts->nsecs / 1000000, TO_STR_TIME_RES_T_MSECS);
1136       break;
1137     case(TS_PREC_FIXED_USEC):
1138     case(TS_PREC_AUTO_USEC):
1139       display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1140         (gint32) ts->secs, ts->nsecs / 1000, TO_STR_TIME_RES_T_USECS);
1141       break;
1142     case(TS_PREC_FIXED_NSEC):
1143     case(TS_PREC_AUTO_NSEC):
1144       display_signed_time(cinfo->col_buf[col], COL_MAX_LEN,
1145         (gint32) ts->secs, ts->nsecs, TO_STR_TIME_RES_T_NSECS);
1146       break;
1147     default:
1148       g_assert_not_reached();
1149       }
1150       cinfo->col_data[col] = cinfo->col_buf[col];
1151       cinfo->col_expr.col_expr[col] = fieldname;
1152       g_strlcpy(cinfo->col_expr.col_expr_val[col],cinfo->col_buf[col],COL_MAX_LEN);
1153     }
1154   }
1155 }
1156
1157 static void
1158 col_set_addr(packet_info *pinfo, int col, address *addr, gboolean is_src, gboolean fill_col_exprs)
1159 {
1160   if (addr->type == AT_NONE) {
1161     pinfo->cinfo->col_data[col] = "";
1162     /* No address, nothing to do */
1163     return;
1164   }
1165
1166 #ifdef NEW_PACKET_LIST
1167   pinfo->cinfo->col_data[col] = se_get_addr_name(addr);
1168 #else
1169   get_addr_name_buf(addr, pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1170   pinfo->cinfo->col_data[col] = pinfo->cinfo->col_buf[col];
1171 #endif
1172
1173   if (!fill_col_exprs)
1174     return;
1175
1176   switch (addr->type) {
1177
1178   case AT_ETHER:
1179     if (is_src)
1180       pinfo->cinfo->col_expr.col_expr[col] = "eth.src";
1181     else
1182       pinfo->cinfo->col_expr.col_expr[col] = "eth.dst";
1183     address_to_str_buf(addr, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1184     break;
1185
1186   case AT_IPv4:
1187     if (is_src)
1188       pinfo->cinfo->col_expr.col_expr[col] = "ip.src";
1189     else
1190       pinfo->cinfo->col_expr.col_expr[col] = "ip.dst";
1191     ip_to_str_buf(addr->data, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1192     break;
1193
1194   case AT_IPv6:
1195     if (is_src)
1196       pinfo->cinfo->col_expr.col_expr[col] = "ipv6.src";
1197     else
1198       pinfo->cinfo->col_expr.col_expr[col] = "ipv6.dst";
1199     address_to_str_buf(addr, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1200     break;
1201
1202   case AT_ATALK:
1203     if (is_src)
1204       pinfo->cinfo->col_expr.col_expr[col] = "ddp.src";
1205     else
1206       pinfo->cinfo->col_expr.col_expr[col] = "ddp.dst";
1207     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1208     break;
1209
1210   case AT_ARCNET:
1211     if (is_src)
1212       pinfo->cinfo->col_expr.col_expr[col] = "arcnet.src";
1213     else
1214       pinfo->cinfo->col_expr.col_expr[col] = "arcnet.dst";
1215     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1216     break;
1217
1218   case AT_URI:
1219     if (is_src)
1220       pinfo->cinfo->col_expr.col_expr[col] = "uri.src";
1221     else
1222       pinfo->cinfo->col_expr.col_expr[col] = "uri.dst";
1223     address_to_str_buf(addr, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1224     break;
1225
1226   default:
1227     break;
1228   }
1229 }
1230
1231 /* ------------------------ */
1232 static void
1233 col_set_port(packet_info *pinfo, int col, gboolean is_res, gboolean is_src, gboolean fill_col_exprs _U_)
1234 {
1235   guint32 port;
1236
1237   if (is_src)
1238     port = pinfo->srcport;
1239   else
1240     port = pinfo->destport;
1241
1242   /* TODO: Use fill_col_exprs */
1243
1244   switch (pinfo->ptype) {
1245   case PT_SCTP:
1246     if (is_res)
1247       g_strlcpy(pinfo->cinfo->col_buf[col], get_sctp_port(port), COL_MAX_LEN);
1248     else
1249       guint32_to_str_buf(port, pinfo->cinfo->col_buf[col], COL_MAX_LEN);
1250     break;
1251
1252   case PT_TCP:
1253     guint32_to_str_buf(port, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1254     if (is_res)
1255       g_strlcpy(pinfo->cinfo->col_buf[col], get_tcp_port(port), COL_MAX_LEN);
1256     else
1257       g_strlcpy(pinfo->cinfo->col_buf[col], pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1258     if (is_src)
1259       pinfo->cinfo->col_expr.col_expr[col] = "tcp.srcport";
1260     else
1261       pinfo->cinfo->col_expr.col_expr[col] = "tcp.dstport";
1262     break;
1263
1264   case PT_UDP:
1265     guint32_to_str_buf(port, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1266     if (is_res)
1267       g_strlcpy(pinfo->cinfo->col_buf[col], get_udp_port(port), COL_MAX_LEN);
1268     else
1269       g_strlcpy(pinfo->cinfo->col_buf[col], pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1270     if (is_src)
1271       pinfo->cinfo->col_expr.col_expr[col] = "udp.srcport";
1272     else
1273       pinfo->cinfo->col_expr.col_expr[col] = "udp.dstport";
1274     break;
1275
1276   case PT_DDP:
1277     if (is_src)
1278       pinfo->cinfo->col_expr.col_expr[col] = "ddp.src_socket";
1279     else
1280       pinfo->cinfo->col_expr.col_expr[col] = "ddp.dst_socket";
1281     guint32_to_str_buf(port, pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1282     g_strlcpy(pinfo->cinfo->col_buf[col], pinfo->cinfo->col_expr.col_expr_val[col], COL_MAX_LEN);
1283     break;
1284
1285   case PT_IPX:
1286     /* XXX - resolve IPX socket numbers */
1287     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "0x%04x", port);
1288     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col],COL_MAX_LEN);
1289     if (is_src)
1290       pinfo->cinfo->col_expr.col_expr[col] = "ipx.src.socket";
1291     else
1292       pinfo->cinfo->col_expr.col_expr[col] = "ipx.dst.socket";
1293     break;
1294
1295   case PT_IDP:
1296     /* XXX - resolve IDP socket numbers */
1297     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "0x%04x", port);
1298     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col],COL_MAX_LEN);
1299     if (is_src)
1300       pinfo->cinfo->col_expr.col_expr[col] = "idp.src.socket";
1301     else
1302       pinfo->cinfo->col_expr.col_expr[col] = "idp.dst.socket";
1303     break;
1304
1305   case PT_USB:
1306     /* XXX - resolve USB endpoint numbers */
1307     g_snprintf(pinfo->cinfo->col_buf[col], COL_MAX_LEN, "0x%08x", port);
1308     g_strlcpy(pinfo->cinfo->col_expr.col_expr_val[col], pinfo->cinfo->col_buf[col],COL_MAX_LEN);
1309     if (is_src)
1310       pinfo->cinfo->col_expr.col_expr[col] = "usb.src.endpoint";
1311     else
1312       pinfo->cinfo->col_expr.col_expr[col] = "usb.dst.endpoint";
1313     break;
1314
1315   default:
1316     break;
1317   }
1318   pinfo->cinfo->col_data[col] = pinfo->cinfo->col_buf[col];
1319 }
1320
1321 gboolean
1322 col_based_on_frame_data(column_info *cinfo, gint col)
1323 {
1324     g_assert(cinfo);
1325     g_assert(col < cinfo->num_cols);
1326
1327     switch (cinfo->col_fmt[col]) {
1328
1329     case COL_NUMBER:
1330     case COL_CLS_TIME:
1331     case COL_ABS_TIME:
1332     case COL_ABS_DATE_TIME:
1333     case COL_REL_TIME:
1334     case COL_DELTA_TIME:
1335     case COL_DELTA_TIME_DIS:
1336     case COL_PACKET_LENGTH:
1337     case COL_CUMULATIVE_BYTES:
1338       return TRUE;
1339
1340     default:
1341         return FALSE;
1342     }
1343 }
1344
1345 void
1346 col_fill_in_frame_data(frame_data *fd, column_info *cinfo, gint col, gboolean fill_col_exprs)
1347 {
1348     switch (cinfo->col_fmt[col]) {
1349
1350     case COL_NUMBER:
1351       guint32_to_str_buf(fd->num, cinfo->col_buf[col], COL_MAX_LEN);
1352       cinfo->col_data[col] = cinfo->col_buf[col];
1353       break;
1354
1355     case COL_CLS_TIME:
1356     case COL_ABS_TIME:
1357     case COL_ABS_DATE_TIME:
1358     case COL_REL_TIME:
1359     case COL_DELTA_TIME:
1360     case COL_DELTA_TIME_DIS:
1361        /* TODO: Pass on fill_col_exprs */
1362       col_set_fmt_time(fd, cinfo, cinfo->col_fmt[col], col);
1363       break;
1364
1365     case COL_PACKET_LENGTH:
1366       guint32_to_str_buf(fd->pkt_len, cinfo->col_buf[col], COL_MAX_LEN);
1367       cinfo->col_data[col] = cinfo->col_buf[col];
1368       break;
1369
1370     case COL_CUMULATIVE_BYTES:
1371       guint32_to_str_buf(fd->cum_bytes, cinfo->col_buf[col], COL_MAX_LEN);
1372       cinfo->col_data[col] = cinfo->col_buf[col];
1373       break;
1374
1375     default:
1376       break;
1377     }
1378
1379     if (!fill_col_exprs)
1380         return;
1381
1382     switch (cinfo->col_fmt[col]) {
1383
1384     case COL_NUMBER:
1385       cinfo->col_expr.col_expr[col] = "frame.number";
1386       g_strlcpy(cinfo->col_expr.col_expr_val[col], cinfo->col_buf[col], COL_MAX_LEN);
1387       break;
1388
1389     case COL_CLS_TIME:
1390     case COL_ABS_TIME:
1391     case COL_ABS_DATE_TIME:
1392     case COL_REL_TIME:
1393     case COL_DELTA_TIME:
1394     case COL_DELTA_TIME_DIS:
1395       /* Already handled above */
1396       break;
1397
1398     case COL_PACKET_LENGTH:
1399       cinfo->col_expr.col_expr[col] = "frame.len";
1400       g_strlcpy(cinfo->col_expr.col_expr_val[col], cinfo->col_buf[col], COL_MAX_LEN);
1401       break;
1402
1403     case COL_CUMULATIVE_BYTES:
1404       break;
1405
1406     default:
1407       break;
1408     }
1409 }
1410
1411 void
1412 col_fill_in(packet_info *pinfo, gboolean fill_col_exprs, gboolean fill_fd_colums)
1413 {
1414   int i;
1415
1416   if (!pinfo->cinfo)
1417     return;
1418
1419   for (i = 0; i < pinfo->cinfo->num_cols; i++) {
1420     switch (pinfo->cinfo->col_fmt[i]) {
1421
1422     case COL_NUMBER:
1423     case COL_CLS_TIME:
1424     case COL_ABS_TIME:
1425     case COL_ABS_DATE_TIME:
1426     case COL_REL_TIME:
1427     case COL_DELTA_TIME:
1428     case COL_DELTA_TIME_DIS:
1429     case COL_PACKET_LENGTH:
1430     case COL_CUMULATIVE_BYTES:
1431       if (fill_fd_colums)
1432         col_fill_in_frame_data(pinfo->fd, pinfo->cinfo, i, fill_col_exprs);
1433       break;
1434
1435     case COL_DEF_SRC:
1436     case COL_RES_SRC:   /* COL_DEF_SRC is currently just like COL_RES_SRC */
1437       col_set_addr(pinfo, i, &pinfo->src, TRUE, fill_col_exprs);
1438       break;
1439
1440     case COL_UNRES_SRC:
1441       col_set_addr(pinfo, i, &pinfo->src, TRUE, fill_col_exprs);
1442       break;
1443
1444     case COL_DEF_DL_SRC:
1445     case COL_RES_DL_SRC:
1446       col_set_addr(pinfo, i, &pinfo->dl_src, TRUE, fill_col_exprs);
1447       break;
1448
1449     case COL_UNRES_DL_SRC:
1450       col_set_addr(pinfo, i, &pinfo->dl_src, TRUE, fill_col_exprs);
1451       break;
1452
1453     case COL_DEF_NET_SRC:
1454     case COL_RES_NET_SRC:
1455       col_set_addr(pinfo, i, &pinfo->net_src, TRUE, fill_col_exprs);
1456       break;
1457
1458     case COL_UNRES_NET_SRC:
1459       col_set_addr(pinfo, i, &pinfo->net_src, TRUE, fill_col_exprs);
1460       break;
1461
1462     case COL_DEF_DST:
1463     case COL_RES_DST:   /* COL_DEF_DST is currently just like COL_RES_DST */
1464       col_set_addr(pinfo, i, &pinfo->dst, FALSE, fill_col_exprs);
1465       break;
1466
1467     case COL_UNRES_DST:
1468       col_set_addr(pinfo, i, &pinfo->dst, FALSE, fill_col_exprs);
1469       break;
1470
1471     case COL_DEF_DL_DST:
1472     case COL_RES_DL_DST:
1473       col_set_addr(pinfo, i, &pinfo->dl_dst, FALSE, fill_col_exprs);
1474       break;
1475
1476     case COL_UNRES_DL_DST:
1477       col_set_addr(pinfo, i, &pinfo->dl_dst, FALSE, fill_col_exprs);
1478       break;
1479
1480     case COL_DEF_NET_DST:
1481     case COL_RES_NET_DST:
1482       col_set_addr(pinfo, i, &pinfo->net_dst, FALSE, fill_col_exprs);
1483       break;
1484
1485     case COL_UNRES_NET_DST:
1486       col_set_addr(pinfo, i, &pinfo->net_dst, FALSE, fill_col_exprs);
1487       break;
1488
1489     case COL_DEF_SRC_PORT:
1490     case COL_RES_SRC_PORT:  /* COL_DEF_SRC_PORT is currently just like COL_RES_SRC_PORT */
1491       col_set_port(pinfo, i, TRUE, TRUE, fill_col_exprs);
1492       break;
1493
1494     case COL_UNRES_SRC_PORT:
1495       col_set_port(pinfo, i, FALSE, TRUE, fill_col_exprs);
1496       break;
1497
1498     case COL_DEF_DST_PORT:
1499     case COL_RES_DST_PORT:  /* COL_DEF_DST_PORT is currently just like COL_RES_DST_PORT */
1500       col_set_port(pinfo, i, TRUE, FALSE, fill_col_exprs);
1501       break;
1502
1503     case COL_UNRES_DST_PORT:
1504       col_set_port(pinfo, i, FALSE, FALSE, fill_col_exprs);
1505       break;
1506
1507     case COL_VSAN:
1508       guint32_to_str_buf(pinfo->vsan, pinfo->cinfo->col_buf[i], COL_MAX_LEN);
1509       pinfo->cinfo->col_data[i] = pinfo->cinfo->col_buf[i];
1510       break;
1511
1512     case COL_CUSTOM:    /* done by col_custom_set_edt() */
1513       break;
1514
1515     case COL_PROTOCOL:  /* currently done by dissectors */
1516     case COL_INFO:      /* currently done by dissectors */
1517       break;
1518
1519     case COL_IF_DIR:    /* currently done by dissectors */
1520       break;
1521
1522     case COL_DCE_CALL:  /* done by dcerpc */
1523       break;
1524
1525     case COL_8021Q_VLAN_ID: /* done by packet-nstrace.c and packet-vlan.c */
1526       break;
1527
1528     case COL_EXPERT:    /* done by expert.c */
1529       break;
1530
1531     case COL_FREQ_CHAN: /* done by radio dissectors */
1532       break;
1533
1534     case COL_TX_RATE:   /* done by packet-radiotap.c */
1535       break;
1536
1537     case NUM_COL_FMTS:  /* keep compiler happy - shouldn't get here */
1538     default:
1539       g_assert_not_reached();
1540       break;
1541     }
1542   }
1543 }
1544
1545 #if 0
1546 XXX this needs more rework?
1547 /* --------------------------- */
1548
1549 static  gchar *
1550 set_addr(address *addr, gboolean is_res)
1551 {
1552   if (addr->type == AT_NONE)
1553     return "";  /* no address, nothing to do */
1554
1555   if (is_res) {
1556     return se_get_addr_name(addr /*, COL_MAX_LEN*/);
1557   }
1558   return se_address_to_str(addr);
1559 }
1560
1561 /* Fills col_text in the frame data structure */
1562 void
1563 col_fill_fdata(packet_info *pinfo)
1564 {
1565   int i;
1566   frame_data *fdata;
1567   gboolean res;
1568
1569   if (!pinfo->cinfo)
1570     return;
1571
1572   fdata = pinfo->fd;
1573
1574   res =FALSE;
1575
1576   for (i = 0; i < pinfo->cinfo->num_cols; i++) {
1577
1578     switch (pinfo->cinfo->col_fmt[i]) {
1579     case COL_NUMBER:           /* frame number */
1580     case COL_PACKET_LENGTH:    /* fd->pkt_len */
1581     case COL_CUMULATIVE_BYTES: /* fd->cum_bytes */
1582     case COL_CLS_TIME:
1583     case COL_ABS_TIME:
1584     case COL_ABS_DATE_TIME:    /* from fd structures */
1585     case COL_REL_TIME:
1586     case COL_DELTA_TIME:
1587     case COL_DELTA_TIME_DIS:
1588       break;
1589
1590     case COL_DEF_SRC:
1591     case COL_RES_SRC:   /* COL_DEF_SRC is currently just like COL_RES_SRC */
1592       res = TRUE;
1593     case COL_UNRES_SRC:
1594       fdata->col_text[i] = set_addr(&pinfo->src, res);
1595       break;
1596
1597     case COL_DEF_DL_SRC:
1598     case COL_RES_DL_SRC:
1599       res = TRUE;
1600     case COL_UNRES_DL_SRC:
1601       fdata->col_text[i] = set_addr (&pinfo->dl_src, res);
1602       break;
1603
1604     case COL_DEF_NET_SRC:
1605     case COL_RES_NET_SRC:
1606       res = TRUE;
1607     case COL_UNRES_NET_SRC:
1608       fdata->col_text[i] = set_addr (&pinfo->net_src, res);
1609       break;
1610
1611     case COL_DEF_DST:
1612     case COL_RES_DST:   /* COL_DEF_DST is currently just like COL_RES_DST */
1613       res = TRUE;
1614     case COL_UNRES_DST:
1615       fdata->col_text[i] = set_addr (&pinfo->dst, res);
1616       break;
1617
1618     case COL_DEF_DL_DST:
1619     case COL_RES_DL_DST:
1620       res = TRUE;
1621     case COL_UNRES_DL_DST:
1622       fdata->col_text[i] = set_addr (&pinfo->dl_dst, res);
1623       break;
1624
1625     case COL_DEF_NET_DST:
1626     case COL_RES_NET_DST:
1627       res = TRUE;
1628     case COL_UNRES_NET_DST:
1629       fdata->col_text[i] = set_addr (&pinfo->net_dst, res);
1630       break;
1631
1632     case COL_DEF_SRC_PORT:
1633     case COL_RES_SRC_PORT:  /* COL_DEF_SRC_PORT is currently just like COL_RES_SRC_PORT */
1634       fdata->col_text[i] = set_port(pinfo, TRUE, pinfo->srcport);
1635       break;
1636     case COL_UNRES_SRC_PORT:
1637       fdata->col_text[i] = set_port(pinfo, FALSE, pinfo->srcport);
1638       break;
1639
1640     case COL_DEF_DST_PORT:
1641     case COL_RES_DST_PORT:  /* COL_DEF_DST_PORT is currently just like COL_RES_DST_PORT */
1642       fdata->col_text[i] = set_port(pinfo, TRUE, pinfo->destport);
1643       break;
1644
1645     case COL_UNRES_DST_PORT:
1646       fdata->col_text[i] = set_port(pinfo, FALSE, pinfo->destport);
1647       break;
1648
1649     case COL_IF_DIR:    /* currently done by dissectors */
1650     case COL_PROTOCOL:
1651     case COL_INFO:
1652     case COL_HPUX_SUBSYS:
1653     case COL_HPUX_DEVID:
1654     case COL_DCE_CALL:
1655     case COL_8021Q_VLAN_ID:
1656     case COL_DSCP_VALUE:
1657     case COL_COS_VALUE:
1658     case COL_FR_DLCI:
1659     case COL_BSSGP_TLLI:
1660     case COL_EXPERT:
1661     case COL_CUSTOM:
1662     case COL_FREQ_CHAN:
1663       if (pinfo->cinfo->col_data[i] != pinfo->cinfo->col_buf[i]) {
1664          /* XXX assume it's a constant */
1665          fdata->col_text[i] = (gchar *)pinfo->cinfo->col_data[i];
1666       }
1667       else {
1668          /* copy */
1669          fdata->col_text[i] = se_strdup(pinfo->cinfo->col_data[i]);
1670       }
1671       break;
1672     case COL_OXID:
1673       fdata->col_text[i] = (gchar *)(GUINT_TO_POINTER((guint)pinfo->oxid));
1674       break;
1675     case COL_RXID:
1676       fdata->col_text[i] = (gchar *)(GUINT_TO_POINTER((guint)pinfo->rxid));
1677       break;
1678     case COL_CIRCUIT_ID:
1679       set_circuit_id(pinfo);
1680       break;
1681     case COL_SRCIDX:
1682       fdata->col_text[i] = (gchar *)(GUINT_TO_POINTER((guint)pinfo->src_idx));
1683       break;
1684     case COL_DSTIDX:
1685       fdata->col_text[i] = (gchar *)(GUINT_TO_POINTER((guint)pinfo->dst_idx));
1686       break;
1687     case COL_VSAN:
1688       fdata->col_text[i] = (gchar *)(GUINT_TO_POINTER((guint)pinfo->vsan));
1689       break;
1690
1691     case NUM_COL_FMTS:  /* keep compiler happy - shouldn't get here */
1692       g_assert_not_reached();
1693       break;
1694     }
1695   }
1696 }
1697
1698 /* XXX Gets/creates the text from col_text in frame data */
1699 /* --------------------- */
1700 gchar *
1701 col_get_text(frame_data *fd, column_info *cinfo, gint col)
1702 {
1703 static gchar fmtbuf[3][COL_MAX_LEN];
1704 static int idx;
1705 gchar  *buf;
1706 gchar  *ptr;
1707
1708     idx = (idx + 1) % 3;
1709     buf = fmtbuf[idx];
1710     *buf = 0;
1711     ptr = buf;
1712
1713     switch (cinfo->col_fmt[col]) {
1714     case COL_NUMBER: /* frame number */
1715       guint32_to_str_buf(fd->num, buf, COL_MAX_LEN);
1716       break;
1717
1718     case COL_CLS_TIME:
1719       set_cls_time(fd, buf);
1720       break;
1721     case COL_ABS_TIME:
1722       set_abs_time(fd, buf);
1723       break;
1724     case COL_ABS_DATE_TIME:
1725       set_abs_date_time(fd, buf);
1726       break;
1727     case COL_REL_TIME:
1728       set_rel_time(fd, buf);
1729       break;
1730     case COL_DELTA_TIME:
1731       set_delta_time(fd, buf);
1732       break;
1733     case COL_DELTA_TIME_DIS:
1734       set_delta_time_dis(fd, buf);
1735       break;
1736
1737     case COL_PACKET_LENGTH: /* fd->pkt_len */
1738       guint32_to_str_buf(fd->pkt_len, buf, COL_MAX_LEN);
1739       break;
1740
1741     case COL_CUMULATIVE_BYTES: /* fd->cum_bytes */
1742       guint32_to_str_buf(fd->cum_bytes, buf, COL_MAX_LEN);
1743       break;
1744
1745     case COL_DEF_SRC:
1746     case COL_RES_SRC:   /* network address */
1747     case COL_UNRES_SRC:
1748     case COL_DEF_DL_SRC:
1749     case COL_RES_DL_SRC:
1750     case COL_UNRES_DL_SRC:
1751     case COL_DEF_NET_SRC:
1752     case COL_RES_NET_SRC:
1753     case COL_UNRES_NET_SRC:
1754     case COL_DEF_DST:
1755     case COL_RES_DST:
1756     case COL_UNRES_DST:
1757     case COL_DEF_DL_DST:
1758     case COL_RES_DL_DST:
1759     case COL_UNRES_DL_DST:
1760     case COL_DEF_NET_DST:
1761     case COL_RES_NET_DST:
1762     case COL_UNRES_NET_DST:
1763
1764     case COL_IF_DIR:
1765     case COL_CIRCUIT_ID:
1766     case COL_PROTOCOL:
1767     case COL_INFO:
1768     case COL_HPUX_SUBSYS:
1769     case COL_HPUX_DEVID:
1770     case COL_DCE_CALL:
1771     case COL_8021Q_VLAN_ID:
1772     case COL_DSCP_VALUE:
1773     case COL_COS_VALUE:
1774     case COL_FR_DLCI:
1775     case COL_BSSGP_TLLI:
1776     case COL_EXPERT:
1777     case COL_CUSTOM:
1778     case COL_FREQ_CHAN:
1779       ptr = fd->col_text[col];
1780       break;
1781
1782     case COL_DEF_SRC_PORT:
1783     case COL_RES_SRC_PORT:
1784     case COL_UNRES_SRC_PORT:
1785     case COL_DEF_DST_PORT:
1786     case COL_RES_DST_PORT:
1787     case COL_UNRES_DST_PORT:
1788       /* hack */
1789       if (GPOINTER_TO_UINT(fd->col_text[col]) <= 65536)
1790           guint32_to_str_buf(GPOINTER_TO_UINT(fd->col_text[col], buf, COL_MAX_LEN));
1791       else
1792           ptr = fd->col_text[col];
1793       break;
1794
1795     case COL_OXID:
1796     case COL_RXID:
1797     case COL_SRCIDX:
1798     case COL_DSTIDX:
1799       g_snprintf(buf, COL_MAX_LEN, "0x%x", GPOINTER_TO_UINT(fd->col_text[col]));
1800       break;
1801
1802     case COL_VSAN:
1803       guint32_to_str_buf(GPOINTER_TO_UINT(fd->col_text[col]), buf, COL_MAX_LEN);
1804       break;
1805
1806     case NUM_COL_FMTS:  /* keep compiler happy - shouldn't get here */
1807       g_assert_not_reached();
1808       break;
1809     }
1810     return ptr;
1811 }
1812 #endif