Put into the "Capture Preferences" dialog box a check box to control
[obnox/wireshark/wip.git] / packet.c
1 /* packet.c
2  * Routines for packet disassembly
3  *
4  * $Id: packet.c,v 1.63 2000/01/10 17:32:52 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37
38 #ifdef HAVE_WINSOCK_H
39 #include <winsock.h>
40 #endif
41
42 #include <glib.h>
43
44 #include <stdio.h>
45 #include <stdarg.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <time.h>
49
50 #ifdef NEED_SNPRINTF_H
51 # include "snprintf.h"
52 #endif
53
54 #ifdef HAVE_NETINET_IN_H
55 # include <netinet/in.h>
56 #endif
57
58 #ifdef HAVE_ARPA_INET_H
59 #include <arpa/inet.h>
60 #endif
61
62 #ifdef NEED_INET_V6DEFS_H
63 # include "inet_v6defs.h"
64 #endif
65
66 #include "packet.h"
67 #include "print.h"
68 #include "timestamp.h"
69 #include "file.h"
70
71 #include "packet-atalk.h"
72
73 #include "packet-ipv6.h"
74
75 #include "packet-sna.h"
76
77 #include "packet-vines.h"
78
79 #ifndef __RESOLV_H__
80 #include "resolv.h"
81 #endif
82
83 extern capture_file  cf;
84
85 static int proto_frame = -1;
86 static int hf_frame_arrival_time = -1;
87 static int hf_frame_time_delta = -1;
88 static int hf_frame_number = -1;
89 static int hf_frame_packet_len = -1;
90 static int hf_frame_capture_len = -1;
91
92 static gint ett_frame = -1;
93
94 /* Wrapper for the most common case of asking
95  * for a string using a colon as the hex-digit separator.
96  */
97 gchar *
98 ether_to_str(const guint8 *ad)
99 {
100         return ether_to_str_punct(ad, ':');
101 }
102
103 /* Places char punct in the string as the hex-digit separator.
104  * If punct is '\0', no punctuation is applied (and thus
105  * the resulting string is 5 bytes shorter)
106  */
107 gchar *
108 ether_to_str_punct(const guint8 *ad, char punct) {
109   static gchar  str[3][18];
110   static gchar *cur;
111   gchar        *p;
112   int          i;
113   guint32      octet;
114   static const gchar hex_digits[16] = "0123456789abcdef";
115
116   if (cur == &str[0][0]) {
117     cur = &str[1][0];
118   } else if (cur == &str[1][0]) {  
119     cur = &str[2][0];
120   } else {  
121     cur = &str[0][0];
122   }
123   p = &cur[18];
124   *--p = '\0';
125   i = 5;
126   for (;;) {
127     octet = ad[i];
128     *--p = hex_digits[octet&0xF];
129     octet >>= 4;
130     *--p = hex_digits[octet&0xF];
131     if (i == 0)
132       break;
133     if (punct)
134       *--p = punct;
135     i--;
136   }
137   return p;
138 }
139
140 gchar *
141 ip_to_str(const guint8 *ad) {
142   static gchar  str[3][16];
143   static gchar *cur;
144   gchar        *p;
145   int           i;
146   guint32       octet;
147   guint32       digit;
148
149   if (cur == &str[0][0]) {
150     cur = &str[1][0];
151   } else if (cur == &str[1][0]) {  
152     cur = &str[2][0];
153   } else {  
154     cur = &str[0][0];
155   }
156   p = &cur[16];
157   *--p = '\0';
158   i = 3;
159   for (;;) {
160     octet = ad[i];
161     *--p = (octet%10) + '0';
162     octet /= 10;
163     digit = octet%10;
164     octet /= 10;
165     if (digit != 0 || octet != 0)
166       *--p = digit + '0';
167     if (octet != 0)
168       *--p = octet + '0';
169     if (i == 0)
170       break;
171     *--p = '.';
172     i--;
173   }
174   return p;
175 }
176
177 gchar *
178 ip6_to_str(struct e_in6_addr *ad) {
179 #ifndef INET6_ADDRSTRLEN
180 #define INET6_ADDRSTRLEN 46
181 #endif
182   static gchar buf[INET6_ADDRSTRLEN];
183
184   inet_ntop(AF_INET6, (u_char*)ad, (gchar*)buf, sizeof(buf));
185   return buf;
186 }
187
188
189 #define PLURALIZE(n)    (((n) > 1) ? "s" : "")
190 #define COMMA(do_it)    ((do_it) ? ", " : "")
191
192 gchar *
193 time_secs_to_str(guint32 time)
194 {
195   static gchar  str[3][8+1+4+2+2+5+2+2+7+2+2+7+1];
196   static gchar *cur, *p;
197   int hours, mins, secs;
198   int do_comma;
199
200   if (cur == &str[0][0]) {
201     cur = &str[1][0];
202   } else if (cur == &str[1][0]) {  
203     cur = &str[2][0];
204   } else {  
205     cur = &str[0][0];
206   }
207
208   if (time == 0) {
209     sprintf(cur, "0 time");
210     return cur;
211   }
212
213   secs = time % 60;
214   time /= 60;
215   mins = time % 60;
216   time /= 60;
217   hours = time % 24;
218   time /= 24;
219
220   p = cur;
221   if (time != 0) {
222     sprintf(p, "%u day%s", time, PLURALIZE(time));
223     p += strlen(p);
224     do_comma = 1;
225   } else
226     do_comma = 0;
227   if (hours != 0) {
228     sprintf(p, "%s%u hour%s", COMMA(do_comma), hours, PLURALIZE(hours));
229     p += strlen(p);
230     do_comma = 1;
231   } else
232     do_comma = 0;
233   if (mins != 0) {
234     sprintf(p, "%s%u minute%s", COMMA(do_comma), mins, PLURALIZE(mins));
235     p += strlen(p);
236     do_comma = 1;
237   } else
238     do_comma = 0;
239   if (secs != 0)
240     sprintf(p, "%s%u second%s", COMMA(do_comma), secs, PLURALIZE(secs));
241   return cur;
242 }
243
244 /* Max string length for displaying byte string.  */
245 #define MAX_BYTE_STR_LEN        20
246
247 /* Turn an array of bytes into a string showing the bytes in hex. */
248 #define N_BYTES_TO_STR_STRINGS  6
249 gchar *
250 bytes_to_str(const guint8 *bd, int bd_len) {
251   static gchar  str[N_BYTES_TO_STR_STRINGS][MAX_BYTE_STR_LEN+3+1];
252   static int    cur_idx;
253   gchar        *cur;
254   gchar        *p;
255   int           len;
256   static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
257                                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
258
259   cur_idx++;
260   if (cur_idx >= N_BYTES_TO_STR_STRINGS)
261     cur_idx = 0;
262   cur = &str[cur_idx][0];
263   p = cur;
264   len = MAX_BYTE_STR_LEN;
265   while (bd_len > 0 && len > 0) {
266     *p++ = hex[(*bd) >> 4];
267     *p++ = hex[(*bd) & 0xF];
268     len -= 2;
269     bd++;
270     bd_len--;
271   }
272   if (bd_len != 0) {
273     /* Note that we're not showing the full string.  */
274     *p++ = '.';
275     *p++ = '.';
276     *p++ = '.';
277   }
278   *p = '\0';
279   return cur;
280 }
281
282 static const char *mon_names[12] = {
283         "Jan",
284         "Feb",
285         "Mar",
286         "Apr",
287         "May",
288         "Jun",
289         "Jul",
290         "Aug",
291         "Sep",
292         "Oct",
293         "Nov",
294         "Dec"
295 };
296
297 gchar *
298 abs_time_to_str(struct timeval *abs_time)
299 {
300         struct tm *tmp;
301         static gchar *cur;
302         static char str[3][3+1+2+2+4+1+2+1+2+1+2+1+4+1 + 5 /* extra */];
303
304         if (cur == &str[0][0]) {
305                 cur = &str[1][0];
306         } else if (cur == &str[1][0]) {
307                 cur = &str[2][0];
308         } else {
309                 cur = &str[0][0];
310         }
311
312         tmp = localtime(&abs_time->tv_sec);
313         sprintf(cur, "%s %2d, %d %02d:%02d:%02d.%04ld",
314             mon_names[tmp->tm_mon],
315             tmp->tm_mday,
316             tmp->tm_year + 1900,
317             tmp->tm_hour,
318             tmp->tm_min,
319             tmp->tm_sec,
320             (long)abs_time->tv_usec/100);
321
322         return cur;
323 }
324
325 gchar *
326 rel_time_to_str(struct timeval *rel_time)
327 {
328         static gchar *cur;
329         static char str[3][10+1+6+1];
330
331         if (cur == &str[0][0]) {
332                 cur = &str[1][0];
333         } else if (cur == &str[1][0]) {
334                 cur = &str[2][0];
335         } else {
336                 cur = &str[0][0];
337         }
338
339         sprintf(cur, "%ld.%06ld", (long)rel_time->tv_sec,
340                 (long)rel_time->tv_usec);
341
342         return cur;
343 }
344
345 /*
346  * Given a pointer into a data buffer, and to the end of the buffer,
347  * find the end of the (putative) line at that position in the data
348  * buffer.
349  * Return a pointer to the EOL character(s) in "*eol".
350  */
351 const u_char *
352 find_line_end(const u_char *data, const u_char *dataend, const u_char **eol)
353 {
354   const u_char *lineend;
355
356   lineend = memchr(data, '\n', dataend - data);
357   if (lineend == NULL) {
358     /*
359      * No LF - line is probably continued in next TCP segment.
360      */
361     lineend = dataend;
362     *eol = dataend;
363   } else {
364     /*
365      * Is the LF at the beginning of the line?
366      */
367     if (lineend > data) {
368       /*
369        * No - is it preceded by a carriage return?
370        * (Perhaps it's supposed to be, but that's not guaranteed....)
371        */
372       if (*(lineend - 1) == '\r') {
373         /*
374          * Yes.  The EOL starts with the CR.
375          */
376         *eol = lineend - 1;
377       } else {
378         /*
379          * No.  The EOL starts with the LF.
380          */
381         *eol = lineend;
382
383         /*
384          * I seem to remember that we once saw lines ending with LF-CR
385          * in an HTTP request or response, so check if it's *followed*
386          * by a carriage return.
387          */
388         if (lineend < (dataend - 1) && *(lineend + 1) == '\r') {
389           /*
390            * It's <non-LF><LF><CR>; say it ends with the CR.
391            */
392           lineend++;
393         }
394       }
395     }
396
397     /*
398      * Point to the character after the last character.
399      */
400     lineend++;
401   }
402   return lineend;
403 }
404
405 #define MAX_COLUMNS_LINE_DETAIL 62
406
407 /*
408  * Get the length of the next token in a line, and the beginning of the
409  * next token after that (if any).
410  * Return 0 if there is no next token.
411  */
412 int
413 get_token_len(const u_char *linep, const u_char *lineend,
414               const u_char **next_token)
415 {
416   const u_char *tokenp;
417   int token_len;
418
419   tokenp = linep;
420   
421   /*
422    * Search for a blank, a CR or an LF, or the end of the buffer.
423    */
424   while (linep < lineend && *linep != ' ' && *linep != '\r' && *linep != '\n')
425       linep++;
426   token_len = linep - tokenp;
427
428   /*
429    * Skip trailing blanks.
430    */
431   while (linep < lineend && *linep == ' ')
432     linep++;
433
434   *next_token = linep;
435
436   return token_len;
437 }
438
439 /*
440  * Given a string, generate a string from it that shows non-printable
441  * characters as C-style escapes, and return a pointer to it.
442  */
443 gchar *
444 format_text(const u_char *string, int len)
445 {
446   static gchar fmtbuf[MAX_COLUMNS_LINE_DETAIL + 3 + 4 + 1];
447   gchar *fmtbufp;
448   int column;
449   const u_char *stringend = string + len;
450   u_char c;
451   int i;
452
453   column = 0;
454   fmtbufp = &fmtbuf[0];
455   while (string < stringend) {
456     if (column >= MAX_COLUMNS_LINE_DETAIL) {
457       /*
458        * Put "..." and quit.
459        */
460       strcpy(fmtbufp, " ...");
461       fmtbufp += 4;
462       break;
463     }
464     c = *string++;
465     if (isprint(c)) {
466       *fmtbufp++ = c;
467       column++;
468     } else {
469       *fmtbufp++ =  '\\';
470       column++;
471       switch (c) {
472
473       case '\\':
474         *fmtbufp++ = '\\';
475         column++;
476         break;
477
478       case '\a':
479         *fmtbufp++ = 'a';
480         column++;
481         break;
482
483       case '\b':
484         *fmtbufp++ = 'b';
485         column++;
486         break;
487
488       case '\f':
489         *fmtbufp++ = 'f';
490         column++;
491         break;
492
493       case '\n':
494         *fmtbufp++ = 'n';
495         column++;
496         break;
497
498       case '\r':
499         *fmtbufp++ = 'r';
500         column++;
501         break;
502
503       case '\t':
504         *fmtbufp++ = 't';
505         column++;
506         break;
507
508       case '\v':
509         *fmtbufp++ = 'v';
510         column++;
511         break;
512
513       default:
514         i = (c>>6)&03;
515         *fmtbufp++ = i + '0';
516         column++;
517         i = (c>>3)&07;
518         *fmtbufp++ = i + '0';
519         column++;
520         i = (c>>0)&07;
521         *fmtbufp++ = i + '0';
522         column++;
523         break;
524       }
525     }
526   }
527   *fmtbufp = '\0';
528   return fmtbuf;
529 }
530
531
532 /* Tries to match val against each element in the value_string array vs.
533    Returns the associated string ptr on a match.
534    Formats val with fmt, and returns the resulting string, on failure. */
535 gchar*
536 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
537   gchar *ret;
538   static gchar  str[3][64];
539   static gchar *cur;
540
541   ret = match_strval(val, vs);
542   if (ret != NULL)
543     return ret;
544   if (cur == &str[0][0]) {
545     cur = &str[1][0];
546   } else if (cur == &str[1][0]) {  
547     cur = &str[2][0];
548   } else {  
549     cur = &str[0][0];
550   }
551   snprintf(cur, 64, fmt, val);
552   return cur;
553 }
554
555 /* Tries to match val against each element in the value_string array vs.
556    Returns the associated string ptr on a match, or NULL on failure. */
557 gchar*
558 match_strval(guint32 val, const value_string *vs) {
559   gint i = 0;
560   
561   while (vs[i].strptr) {
562     if (vs[i].value == val)
563       return(vs[i].strptr);
564     i++;
565   }
566
567   return(NULL);
568 }
569
570 /* Generate, into "buf", a string showing the bits of a bitfield.
571    Return a pointer to the character after that string. */
572 char *
573 decode_bitfield_value(char *buf, guint32 val, guint32 mask, int width)
574 {
575   int i;
576   guint32 bit;
577   char *p;
578
579   i = 0;
580   p = buf;
581   bit = 1 << (width - 1);
582   for (;;) {
583     if (mask & bit) {
584       /* This bit is part of the field.  Show its value. */
585       if (val & bit)
586         *p++ = '1';
587       else
588         *p++ = '0';
589     } else {
590       /* This bit is not part of the field. */
591       *p++ = '.';
592     }
593     bit >>= 1;
594     i++;
595     if (i >= width)
596       break;
597     if (i % 4 == 0)
598       *p++ = ' ';
599   }
600   strcpy(p, " = ");
601   p += 3;
602   return p;
603 }
604
605 /* Generate a string describing a Boolean bitfield (a one-bit field that
606    says something is either true of false). */
607 const char *
608 decode_boolean_bitfield(guint32 val, guint32 mask, int width,
609     const char *truedesc, const char *falsedesc)
610 {
611   static char buf[1025];
612   char *p;
613
614   p = decode_bitfield_value(buf, val, mask, width);
615   if (val & mask)
616     strcpy(p, truedesc);
617   else
618     strcpy(p, falsedesc);
619   return buf;
620 }
621
622 /* Generate a string describing an enumerated bitfield (an N-bit field
623    with various specific values having particular names). */
624 const char *
625 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
626     const value_string *tab, const char *fmt)
627 {
628   static char buf[1025];
629   char *p;
630
631   p = decode_bitfield_value(buf, val, mask, width);
632   sprintf(p, fmt, val_to_str(val & mask, tab, "Unknown"));
633   return buf;
634 }
635
636 /* Generate a string describing a numeric bitfield (an N-bit field whose
637    value is just a number). */
638 const char *
639 decode_numeric_bitfield(guint32 val, guint32 mask, int width,
640     const char *fmt)
641 {
642   static char buf[1025];
643   char *p;
644   int shift = 0;
645
646   /* Compute the number of bits we have to shift the bitfield right
647      to extract its value. */
648   while ((mask & (1<<shift)) == 0)
649     shift++;
650
651   p = decode_bitfield_value(buf, val, mask, width);
652   sprintf(p, fmt, (val & mask) >> shift);
653   return buf;
654 }
655
656 /* Checks to see if a particular packet information element is needed for
657    the packet list */
658 gint
659 check_col(frame_data *fd, gint el) {
660   int i;
661
662   if (fd->cinfo) {
663     for (i = 0; i < fd->cinfo->num_cols; i++) {
664       if (fd->cinfo->fmt_matx[i][el])
665         return TRUE;
666     }
667   }
668   return FALSE;
669 }
670
671 /* Adds a vararg list to a packet info string. */
672 void
673 col_add_fstr(frame_data *fd, gint el, gchar *format, ...) {
674   va_list ap;
675   int     i;
676   size_t  max_len;
677   
678   va_start(ap, format);
679   for (i = 0; i < fd->cinfo->num_cols; i++) {
680     if (fd->cinfo->fmt_matx[i][el]) {
681       if (el == COL_INFO)
682         max_len = COL_MAX_INFO_LEN;
683       else
684         max_len = COL_MAX_LEN;
685       vsnprintf(fd->cinfo->col_data[i], max_len, format, ap);
686     }
687   }
688 }
689
690 void
691 col_add_str(frame_data *fd, gint el, const gchar* str) {
692   int    i;
693   size_t max_len;
694
695   for (i = 0; i < fd->cinfo->num_cols; i++) {
696     if (fd->cinfo->fmt_matx[i][el]) {
697       if (el == COL_INFO)
698         max_len = COL_MAX_INFO_LEN;
699       else
700         max_len = COL_MAX_LEN;
701       strncpy(fd->cinfo->col_data[i], str, max_len);
702       fd->cinfo->col_data[i][max_len - 1] = 0;
703     }
704   }
705 }
706
707 /* Appends a vararg list to a packet info string. */
708 void
709 col_append_fstr(frame_data *fd, gint el, gchar *format, ...) {
710   va_list ap;
711   int     i;
712   size_t  len, max_len;
713   
714   va_start(ap, format);
715   for (i = 0; i < fd->cinfo->num_cols; i++) {
716     if (fd->cinfo->fmt_matx[i][el]) {
717       len = strlen(fd->cinfo->col_data[i]);
718       if (el == COL_INFO)
719         max_len = COL_MAX_INFO_LEN;
720       else
721         max_len = COL_MAX_LEN;
722       vsnprintf(&fd->cinfo->col_data[i][len], max_len - len, format, ap);
723     }
724   }
725 }
726
727 void
728 col_append_str(frame_data *fd, gint el, gchar* str) {
729   int    i;
730   size_t len, max_len;
731
732   for (i = 0; i < fd->cinfo->num_cols; i++) {
733     if (fd->cinfo->fmt_matx[i][el]) {
734       len = strlen(fd->cinfo->col_data[i]);
735       if (el == COL_INFO)
736         max_len = COL_MAX_LEN;
737       else
738         max_len = COL_MAX_INFO_LEN;
739       strncat(fd->cinfo->col_data[i], str, max_len - len);
740       fd->cinfo->col_data[i][max_len - 1] = 0;
741     }
742   }
743 }
744
745 /* To do: Add check_col checks to the col_add* routines */
746
747 static void
748 col_set_abs_time(frame_data *fd, int col)
749 {
750   struct tm *tmp;
751   time_t then;
752
753   then = fd->abs_secs;
754   tmp = localtime(&then);
755   snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%02d:%02d:%02d.%04ld",
756     tmp->tm_hour,
757     tmp->tm_min,
758     tmp->tm_sec,
759     (long)fd->abs_usecs/100);
760 }
761
762 static void
763 col_set_rel_time(frame_data *fd, int col)
764 {
765   snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%d.%06d", fd->rel_secs,
766     fd->rel_usecs);
767 }
768
769 static void
770 col_set_delta_time(frame_data *fd, int col)
771 {
772   snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%d.%06d", fd->del_secs,
773     fd->del_usecs);
774 }
775
776 /* Add "command-line-specified" time.
777    XXX - this is called from "file.c" when the user changes the time
778    format they want for "command-line-specified" time; it's a bit ugly
779    that we have to export it, but if we go to a CList-like widget that
780    invokes callbacks to get the text for the columns rather than
781    requiring us to stuff the text into the widget from outside, we
782    might be able to clean this up. */
783 void
784 col_set_cls_time(frame_data *fd, int col)
785 {
786   switch (timestamp_type) {
787     case ABSOLUTE:
788       col_set_abs_time(fd, col);
789       break;
790
791     case RELATIVE:
792       col_set_rel_time(fd, col);
793       break;
794
795     case DELTA:
796       col_set_delta_time(fd, col);
797       break;
798   }
799 }
800
801 static void
802 col_set_addr(frame_data *fd, int col, address *addr, gboolean is_res)
803 {
804   u_int ipv4_addr;
805   struct e_in6_addr ipv6_addr;
806   struct atalk_ddp_addr ddp_addr;
807   struct sna_fid_type_4_addr sna_fid_type_4_addr;
808
809   switch (addr->type) {
810
811   case AT_ETHER:
812     if (is_res)
813       strncpy(fd->cinfo->col_data[col], get_ether_name(addr->data), COL_MAX_LEN);
814     else
815       strncpy(fd->cinfo->col_data[col], ether_to_str(addr->data), COL_MAX_LEN);
816     break;
817
818   case AT_IPv4:
819     memcpy(&ipv4_addr, addr->data, sizeof ipv4_addr);
820     if (is_res)
821       strncpy(fd->cinfo->col_data[col], get_hostname(ipv4_addr), COL_MAX_LEN);
822     else
823       strncpy(fd->cinfo->col_data[col], ip_to_str(addr->data), COL_MAX_LEN);
824     break;
825
826   case AT_IPv6:
827     memcpy(&ipv6_addr.s6_addr, addr->data, sizeof ipv6_addr.s6_addr);
828     if (is_res)
829       strncpy(fd->cinfo->col_data[col], get_hostname6(&ipv6_addr), COL_MAX_LEN);
830     else
831       strncpy(fd->cinfo->col_data[col], ip6_to_str(&ipv6_addr), COL_MAX_LEN);
832     break;
833
834   case AT_IPX:
835     strncpy(fd->cinfo->col_data[col],
836       ipx_addr_to_str(pntohl(&addr->data[0]), &addr->data[4]), COL_MAX_LEN);
837     break;
838
839   case AT_SNA:
840     switch (addr->len) {
841
842     case 1:
843       snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%04X", addr->data[0]);
844       break;
845
846     case 2:
847       snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%04X",
848         pntohs(&addr->data[0]));
849       break;
850
851     case SNA_FID_TYPE_4_ADDR_LEN:
852       memcpy(&sna_fid_type_4_addr, addr->data, SNA_FID_TYPE_4_ADDR_LEN);
853       strncpy(fd->cinfo->col_data[col],
854         sna_fid_type_4_addr_to_str(&sna_fid_type_4_addr), COL_MAX_LEN);
855       break;
856     }
857     break;
858
859   case AT_ATALK:
860     memcpy(&ddp_addr, addr->data, sizeof ddp_addr);
861     strncpy(fd->cinfo->col_data[col], atalk_addr_to_str(&ddp_addr),
862       COL_MAX_LEN);
863     break;
864
865   case AT_VINES:
866     strncpy(fd->cinfo->col_data[col], vines_addr_to_str(&addr->data[0]),
867       COL_MAX_LEN);
868     break;
869
870   default:
871     break;
872   }
873   fd->cinfo->col_data[col][COL_MAX_LEN - 1] = '\0';
874 }
875
876 static void
877 col_set_port(frame_data *fd, int col, port_type ptype, guint32 port,
878                 gboolean is_res)
879 {
880   switch (ptype) {
881
882   case PT_TCP:
883     if (is_res)
884       strncpy(fd->cinfo->col_data[col], get_tcp_port(port), COL_MAX_LEN);
885     else
886       snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%u", port);
887     break;
888
889   case PT_UDP:
890     if (is_res)
891       strncpy(fd->cinfo->col_data[col], get_udp_port(port), COL_MAX_LEN);
892     else
893       snprintf(fd->cinfo->col_data[col], COL_MAX_LEN, "%u", port);
894     break;
895
896   default:
897     break;
898   }
899   fd->cinfo->col_data[col][COL_MAX_LEN - 1] = '\0';
900 }
901
902 void
903 fill_in_columns(frame_data *fd)
904 {
905   int i;
906
907   for (i = 0; i < fd->cinfo->num_cols; i++) {
908     switch (fd->cinfo->col_fmt[i]) {
909
910     case COL_NUMBER:
911       snprintf(fd->cinfo->col_data[i], COL_MAX_LEN, "%u", fd->num);
912       break;
913
914     case COL_CLS_TIME:
915       col_set_cls_time(fd, i);
916       break;
917
918     case COL_ABS_TIME:
919       col_set_abs_time(fd, i);
920       break;
921
922     case COL_REL_TIME:
923       col_set_rel_time(fd, i);
924       break;
925
926     case COL_DELTA_TIME:
927       col_set_delta_time(fd, i);
928       break;
929
930     case COL_DEF_SRC:
931     case COL_RES_SRC:   /* COL_DEF_SRC is currently just like COL_RES_SRC */
932       col_set_addr(fd, i, &pi.src, TRUE);
933       break;
934
935     case COL_UNRES_SRC:
936       col_set_addr(fd, i, &pi.src, FALSE);
937       break;
938
939     case COL_DEF_DL_SRC:
940     case COL_RES_DL_SRC:
941       col_set_addr(fd, i, &pi.dl_src, TRUE);
942       break;
943
944     case COL_UNRES_DL_SRC:
945       col_set_addr(fd, i, &pi.dl_src, FALSE);
946       break;
947
948     case COL_DEF_NET_SRC:
949     case COL_RES_NET_SRC:
950       col_set_addr(fd, i, &pi.net_src, TRUE);
951       break;
952
953     case COL_UNRES_NET_SRC:
954       col_set_addr(fd, i, &pi.net_src, FALSE);
955       break;
956
957     case COL_DEF_DST:
958     case COL_RES_DST:   /* COL_DEF_DST is currently just like COL_RES_DST */
959       col_set_addr(fd, i, &pi.dst, TRUE);
960       break;
961
962     case COL_UNRES_DST:
963       col_set_addr(fd, i, &pi.dst, FALSE);
964       break;
965
966     case COL_DEF_DL_DST:
967     case COL_RES_DL_DST:
968       col_set_addr(fd, i, &pi.dl_dst, TRUE);
969       break;
970
971     case COL_UNRES_DL_DST:
972       col_set_addr(fd, i, &pi.dl_dst, FALSE);
973       break;
974
975     case COL_DEF_NET_DST:
976     case COL_RES_NET_DST:
977       col_set_addr(fd, i, &pi.net_dst, TRUE);
978       break;
979
980     case COL_UNRES_NET_DST:
981       col_set_addr(fd, i, &pi.net_dst, FALSE);
982       break;
983
984     case COL_DEF_SRC_PORT:
985     case COL_RES_SRC_PORT:      /* COL_DEF_SRC_PORT is currently just like COL_RES_SRC_PORT */
986       col_set_port(fd, i, pi.ptype, pi.srcport, TRUE);
987       break;
988
989     case COL_UNRES_SRC_PORT:
990       col_set_port(fd, i, pi.ptype, pi.srcport, FALSE);
991       break;
992
993     case COL_DEF_DST_PORT:
994     case COL_RES_DST_PORT:      /* COL_DEF_DST_PORT is currently just like COL_RES_DST_PORT */
995       col_set_port(fd, i, pi.ptype, pi.destport, TRUE);
996       break;
997
998     case COL_UNRES_DST_PORT:
999       col_set_port(fd, i, pi.ptype, pi.destport, FALSE);
1000       break;
1001
1002     case COL_PROTOCOL:  /* currently done by dissectors */
1003     case COL_INFO:      /* currently done by dissectors */
1004       break;
1005
1006     case COL_PACKET_LENGTH:
1007       snprintf(fd->cinfo->col_data[i], COL_MAX_LEN, "%d", fd->pkt_len);
1008       break;
1009
1010     case NUM_COL_FMTS:  /* keep compiler happy - shouldn't get here */
1011       break;
1012     }
1013   }
1014 }
1015         
1016 void blank_packetinfo(void)
1017 {
1018   pi.dl_src.type = AT_NONE;
1019   pi.dl_dst.type = AT_NONE;
1020   pi.net_src.type = AT_NONE;
1021   pi.net_dst.type = AT_NONE;
1022   pi.src.type = AT_NONE;
1023   pi.dst.type = AT_NONE;
1024   pi.ipproto  = 0;
1025   pi.ptype = PT_NONE;
1026   pi.srcport  = 0;
1027   pi.destport = 0;
1028 }
1029
1030 /* Allow protocols to register "init" routines, which are called before
1031    we make a pass through a capture file and dissect all its packets
1032    (e.g., when we read in a new capture file, or run a "filter packets"
1033    or "colorize packets" pass over the current capture file). */
1034 static GSList *init_routines;
1035
1036 void
1037 register_init_routine(void (*func)(void))
1038 {
1039         init_routines = g_slist_append(init_routines, func);
1040 }
1041
1042 /* Call all the registered "init" routines. */
1043 static void
1044 call_init_routine(gpointer routine, gpointer dummy)
1045 {
1046         void (*func)(void) = routine;
1047
1048         (*func)();
1049 }
1050
1051 void
1052 init_all_protocols(void)
1053 {
1054         g_slist_foreach(init_routines, &call_init_routine, NULL);
1055 }
1056
1057 /* this routine checks the frame type from the cf structure */
1058 void
1059 dissect_packet(const u_char *pd, frame_data *fd, proto_tree *tree)
1060 {
1061         proto_tree *fh_tree;
1062         proto_item *ti;
1063         struct timeval tv;
1064
1065         /* Put in frame header information. */
1066         if (tree) {
1067           ti = proto_tree_add_item_format(tree, proto_frame, 0, fd->cap_len,
1068             NULL, "Frame %u (%u on wire, %u captured)", fd->num,
1069             fd->pkt_len, fd->cap_len);
1070
1071           fh_tree = proto_item_add_subtree(ti, ett_frame);
1072
1073           tv.tv_sec = fd->abs_secs;
1074           tv.tv_usec = fd->abs_usecs;
1075
1076           proto_tree_add_item(fh_tree, hf_frame_arrival_time,
1077                 0, 0, &tv);
1078
1079           tv.tv_sec = fd->del_secs;
1080           tv.tv_usec = fd->del_usecs;
1081
1082           proto_tree_add_item(fh_tree, hf_frame_time_delta,
1083                 0, 0, &tv);
1084
1085           proto_tree_add_item(fh_tree, hf_frame_number,
1086                 0, 0, fd->num);
1087
1088           proto_tree_add_item_format(fh_tree, hf_frame_packet_len,
1089                 0, 0, fd->pkt_len, "Packet Length: %d byte%s", fd->pkt_len,
1090                 plurality(fd->pkt_len, "", "s"));
1091                 
1092           proto_tree_add_item_format(fh_tree, hf_frame_capture_len,
1093                 0, 0, fd->cap_len, "Capture Length: %d byte%s", fd->cap_len,
1094                 plurality(fd->cap_len, "", "s"));
1095         }
1096
1097         blank_packetinfo();
1098
1099         /* Set the initial payload to the packet length, and the initial
1100            captured payload to the capture length (other protocols may
1101            reduce them if their headers say they're less). */
1102         pi.len = fd->pkt_len;
1103         pi.captured_len = fd->cap_len;
1104
1105         switch (fd->lnk_t) {
1106                 case WTAP_ENCAP_ETHERNET :
1107                         dissect_eth(pd, 0, fd, tree);
1108                         break;
1109                 case WTAP_ENCAP_FDDI :
1110                         dissect_fddi(pd, fd, tree, FALSE);
1111                         break;
1112                 case WTAP_ENCAP_FDDI_BITSWAPPED :
1113                         dissect_fddi(pd, fd, tree, TRUE);
1114                         break;
1115                 case WTAP_ENCAP_TR :
1116                         dissect_tr(pd, 0, fd, tree);
1117                         break;
1118                 case WTAP_ENCAP_NULL :
1119                         dissect_null(pd, fd, tree);
1120                         break;
1121                 case WTAP_ENCAP_PPP :
1122                         dissect_ppp(pd, fd, tree);
1123                         break;
1124                 case WTAP_ENCAP_LAPB :
1125                         dissect_lapb(pd, fd, tree);
1126                         break;
1127                 case WTAP_ENCAP_RAW_IP :
1128                         dissect_raw(pd, fd, tree);
1129                         break;
1130                 case WTAP_ENCAP_LINUX_ATM_CLIP :
1131                         dissect_clip(pd, fd, tree);
1132                         break;
1133                 case WTAP_ENCAP_ATM_SNIFFER :
1134                         dissect_atm(pd, fd, tree);
1135                         break;
1136                 case WTAP_ENCAP_ASCEND :
1137                         dissect_ascend(pd, fd, tree);
1138                         break;
1139                 case WTAP_ENCAP_LAPD :
1140                         dissect_lapd(pd, fd, tree);
1141                         break;
1142                 case WTAP_ENCAP_V120 :
1143                         dissect_v120(pd, fd, tree);
1144                         break;
1145         }
1146 }
1147
1148 void
1149 proto_register_frame(void)
1150 {
1151         static hf_register_info hf[] = {
1152                 { &hf_frame_arrival_time,
1153                 { "Arrival Time",               "frame.time", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
1154                         ""}},
1155
1156                 { &hf_frame_time_delta,
1157                 { "Time delta from previous packet",    "frame.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL,
1158                         0x0,
1159                         "" }},
1160
1161                 { &hf_frame_number,
1162                 { "Frame Number",               "frame.number", FT_UINT32, BASE_DEC, NULL, 0x0,
1163                         "" }},
1164
1165                 { &hf_frame_packet_len,
1166                 { "Total Frame Length",         "frame.pkt_len", FT_UINT32, BASE_DEC, NULL, 0x0,
1167                         "" }},
1168
1169                 { &hf_frame_capture_len,
1170                 { "Capture Frame Length",       "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
1171                         "" }},
1172         };
1173         static gint *ett[] = {
1174                 &ett_frame,
1175         };
1176
1177         proto_frame = proto_register_protocol("Frame", "frame");
1178         proto_register_field_array(proto_frame, hf, array_length(hf));
1179         proto_register_subtree_array(ett, array_length(ett));
1180 }