From Harald Welte:
[obnox/wireshark/wip.git] / wiretap / dbs-etherwatch.c
index 4baff3a33913b0017e2250196576eed4af123df1..366a118aba9fe7be67986006894161d9f16da84f 100644 (file)
  */
 
 /*
-   Example 'ETHERWATCH' output data:
+   Example 'ETHERWATCH' output data (with "printable" characters in the
+   "printable characters" section of the output replaced by "." if they have
+   the 8th bit set, so as not to upset compilers that are expecting text
+   in comments to be in a particular character encoding that can't handle
+   those values):
 ETHERWATCH  X5-008
 42 names and addresses were loaded
 Reading recorded data from PERSISTENCE
 ------------------------------------------------------------------------------
 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
 Protocol 08-00 00 00-00-00-00-00,   60 byte buffer at 10-OCT-2001 10:20:45.16
-  [E..<8.....Ò.....]-    0-[45 00 00 3C 38 93 00 00 1D 06 D2 12 80 93 11 1A]
-  [...Ö.Ò...(¤.....]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 90 00 00 00 00]
-  [.....½.....´....]-   32-[A0 02 FF FF 95 BD 00 00 02 04 05 B4 03 03 04 01]
-  [......å.....    ]-   48-[01 01 08 0A 90 90 E5 14 00 00 00 00]
+  [E..<8...........]-    0-[45 00 00 3C 38 93 00 00 1D 06 D2 12 80 93 11 1A]
+  [.........(......]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 90 00 00 00 00]
+  [................]-   32-[A0 02 FF FF 95 BD 00 00 02 04 05 B4 03 03 04 01]
+  [............    ]-   48-[01 01 08 0A 90 90 E5 14 00 00 00 00]
 ------------------------------------------------------------------------------
 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
-  [E..(8.....Ò%....]-    0-[45 00 00 28 38 94 00 00 1D 06 D2 25 80 93 11 1A]
-  [...Ö.Ò...(¤.Z.4w]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 91 5A 1C 34 77]
-  [P.#(Ás.....´....]-   32-[50 10 23 28 C1 73 00 00 02 04 05 B4 03 03 00 00]
+  [E..(8......%....]-    0-[45 00 00 28 38 94 00 00 1D 06 D2 25 80 93 11 1A]
+  [.........(..Z.4w]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 91 5A 1C 34 77]
+  [P.#(.s..........]-   32-[50 10 23 28 C1 73 00 00 02 04 05 B4 03 03 00 00]
   [..              ]-   48-[02 04]
 
 
-Alternative HEX only output, slightly more efficient and all ethereal needs:
+Alternative HEX only output, slightly more efficient and all wireshark needs:
 ------------------------------------------------------------------------------
 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
@@ -85,8 +89,8 @@ static const char dbs_etherwatch_rec_magic[]  =
 #define DBS_ETHERWATCH_MAX_PACKET_LEN  16384
 
 static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
-       long *data_offset);
-static gboolean dbs_etherwatch_seek_read(wtap *wth, long seek_off,
+       gint64 *data_offset);
+static gboolean dbs_etherwatch_seek_read(wtap *wth, gint64 seek_off,
        union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
        int *err, gchar **err_info);
 static int parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf,
@@ -96,12 +100,14 @@ static guint parse_single_hex_dump_line(char* rec, guint8 *buf,
 static guint parse_hex_dump(char* dump, guint8 *buf, char seperator, char end);
 
 /* Seeks to the beginning of the next packet, and returns the
-   byte offset.  Returns -1 on failure, and sets "*err" to the error. */
-static long dbs_etherwatch_seek_next_packet(wtap *wth, int *err)
+   byte offset.  Returns -1 on failure, and sets "*err" to the error
+   and "*err_info" to null or an additional error string. */
+static gint64 dbs_etherwatch_seek_next_packet(wtap *wth, int *err,
+    gchar **err_info)
 {
   int byte;
   unsigned int level = 0;
-  long cur_off;
+  gint64 cur_off;
 
   while ((byte = file_getc(wth->fh)) != EOF) {
     if (byte == dbs_etherwatch_rec_magic[level]) {
@@ -111,7 +117,7 @@ static long dbs_etherwatch_seek_next_packet(wtap *wth, int *err)
         cur_off = file_tell(wth->fh);
         if (cur_off == -1) {
           /* Error. */
-          *err = file_error(wth->fh);
+          *err = file_error(wth->fh, err_info);
           return -1;
         }
         return cur_off + 1;
@@ -124,9 +130,8 @@ static long dbs_etherwatch_seek_next_packet(wtap *wth, int *err)
     /* We got an EOF. */
     *err = 0;
   } else {
-    /* We (presumably) got an error (there's no equivalent to "ferror()"
-       in zlib, alas, so we don't have a wrapper to check for an error). */
-    *err = file_error(wth->fh);
+    /* We got an error. */
+    *err = file_error(wth->fh, err_info);
   }
   return -1;
 }
@@ -138,13 +143,16 @@ static long dbs_etherwatch_seek_next_packet(wtap *wth, int *err)
  * a DBS Ethertrace text trace file.
  *
  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
- * if we get an I/O error, "*err" will be set to a non-zero value.
+ * if we get an I/O error, "*err" will be set to a non-zero value and
+ * "*err_info" will be set to null or an error string.
  */
-static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err)
+static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err,
+    gchar **err_info)
 {
        char    buf[DBS_ETHERWATCH_LINE_LENGTH];
        int     line, byte;
-       unsigned int reclen, i, level;
+       gsize   reclen;
+       unsigned int i, level;
 
        buf[DBS_ETHERWATCH_LINE_LENGTH-1] = 0;
 
@@ -174,7 +182,7 @@ static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err)
                        if (file_eof(wth->fh))
                                *err = 0;
                        else
-                               *err = file_error(wth->fh);
+                               *err = file_error(wth->fh, err_info);
                        return FALSE;
                }
        }
@@ -183,10 +191,10 @@ static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err)
 }
 
 
-int dbs_etherwatch_open(wtap *wth, int *err, gchar **err_info _U_)
+int dbs_etherwatch_open(wtap *wth, int *err, gchar **err_info)
 {
        /* Look for DBS ETHERWATCH header */
-       if (!dbs_etherwatch_check_file_type(wth, err)) {
+       if (!dbs_etherwatch_check_file_type(wth, err, err_info)) {
                if (*err == 0)
                        return 0;
                else
@@ -199,20 +207,21 @@ int dbs_etherwatch_open(wtap *wth, int *err, gchar **err_info _U_)
        wth->snapshot_length = 0;       /* not known */
        wth->subtype_read = dbs_etherwatch_read;
        wth->subtype_seek_read = dbs_etherwatch_seek_read;
+       wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
 
        return 1;
 }
 
-/* Find the next packet and parse it; called from wtap_loop(). */
+/* Find the next packet and parse it; called from wtap_read(). */
 static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
-    long *data_offset)
+    gint64 *data_offset)
 {
-       long    offset;
+       gint64  offset;
        guint8  *buf;
        int     pkt_len;
 
        /* Find the next packet */
-       offset = dbs_etherwatch_seek_next_packet(wth, err);
+       offset = dbs_etherwatch_seek_next_packet(wth, err, err_info);
        if (offset < 1)
                return FALSE;
 
@@ -237,7 +246,7 @@ static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
 
 /* Used to read packets in random-access fashion */
 static gboolean
-dbs_etherwatch_seek_read (wtap *wth, long seek_off,
+dbs_etherwatch_seek_read (wtap *wth, gint64 seek_off,
        union wtap_pseudo_header *pseudo_header _U_,
        guint8 *pd, int len, int *err, gchar **err_info)
 {
@@ -251,7 +260,7 @@ dbs_etherwatch_seek_read (wtap *wth, long seek_off,
 
        if (pkt_len != len) {
                if (pkt_len != -1) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup_printf("dbs_etherwatch: packet length %d doesn't match requested length %d",
                            pkt_len, len);
                }
@@ -318,10 +327,10 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        int     num_items_scanned;
        int     eth_hdr_len, pkt_len, csec;
        int length_pos, length_from, length;
-       struct tm time;
-       char mon[4];
+       struct tm tm;
+       char mon[4] = "xxx";
        gchar *p;
-       static gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
+       static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
        int     count, line_count;
 
        eth_hdr_len = 0;
@@ -330,7 +339,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
         * extract the useful information
         */
        if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
-               *err = file_error(fh);
+               *err = file_error(fh, err_info);
                if (*err == 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
@@ -340,14 +349,14 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        /* Get the destination address */
        p = strstr(line, DEST_MAC_PREFIX);
        if(!p) {
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("dbs_etherwatch: destination address not found");
                return -1;
        }
        p += strlen(DEST_MAC_PREFIX);
        if(parse_hex_dump(p, &buf[eth_hdr_len], HEX_HDR_SPR, HEX_HDR_END)
                                != MAC_ADDR_LENGTH) {
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("dbs_etherwatch: destination address not valid");
                return -1;
        }
@@ -365,7 +374,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        }
        if(parse_hex_dump(p, &buf[eth_hdr_len], HEX_HDR_SPR,
                HEX_HDR_END) != MAC_ADDR_LENGTH) {
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("dbs_etherwatch: source address not valid");
                return -1;
        }
@@ -373,7 +382,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
 
        /* Read the next line of the record header */
        if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
-               *err = file_error(fh);
+               *err = file_error(fh, err_info);
                if (*err == 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
@@ -382,20 +391,20 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
 
        /* Check the lines is as least as long as the length position */
        if(strlen(line) < LENGTH_POS) {
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("dbs_etherwatch: line too short");
                return -1;
        }
 
        num_items_scanned = sscanf(line + LENGTH_POS,
-                               "%d byte buffer at %d-%3s-%d %d:%d:%d.%d",
+                               "%9d byte buffer at %2d-%3s-%4d %2d:%2d:%2d.%9d",
                                &pkt_len,
-                               &time.tm_mday, mon,
-                               &time.tm_year, &time.tm_hour, &time.tm_min,
-                               &time.tm_sec, &csec);
+                               &tm.tm_mday, mon,
+                               &tm.tm_year, &tm.tm_hour, &tm.tm_min,
+                               &tm.tm_sec, &csec);
 
        if (num_items_scanned != 8) {
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("dbs_etherwatch: header line not valid");
                return -1;
        }
@@ -407,7 +416,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
                /* Get the Protocol */
                if(parse_hex_dump(&line[PROTOCOL_POS], &buf[eth_hdr_len], HEX_HDR_SPR,
                                        HEX_HDR_END) != PROTOCOL_LENGTH) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("dbs_etherwatch: Ethernet II protocol value not valid");
                        return -1;
                }
@@ -423,7 +432,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
                /* Get the DSAP + SSAP */
                if(parse_hex_dump(&line[SAP_POS], &buf[eth_hdr_len], HEX_HDR_SPR,
                                        HEX_HDR_END) != SAP_LENGTH) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("dbs_etherwatch: 802.2 DSAP+SSAP value not valid");
                        return -1;
                }
@@ -431,7 +440,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
                /* Get the (first part of the) control field */
                if(parse_hex_dump(&line[CTL_POS], &buf[eth_hdr_len], HEX_HDR_SPR,
                                        HEX_HDR_END) != CTL_UNNUMB_LENGTH) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("dbs_etherwatch: 802.2 control field first part not valid");
                        return -1;
                }
@@ -441,7 +450,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
                        if(parse_hex_dump(&line[PID_POS],
                                                &buf[eth_hdr_len + CTL_UNNUMB_LENGTH], HEX_HDR_END,
                                                HEX_HDR_SPR) != CTL_NUMB_LENGTH - CTL_UNNUMB_LENGTH) {
-                               *err = WTAP_ERR_BAD_RECORD;
+                               *err = WTAP_ERR_BAD_FILE;
                                *err_info = g_strdup("dbs_etherwatch: 802.2 control field second part value not valid");
                                return -1;
                        }
@@ -455,7 +464,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
                        /* Get the PID */
                        if(parse_hex_dump(&line[PID_POS], &buf[eth_hdr_len], HEX_HDR_SPR,
                                                HEX_PID_END) != PID_LENGTH) {
-                               *err = WTAP_ERR_BAD_RECORD;
+                               *err = WTAP_ERR_BAD_FILE;
                                *err_info = g_strdup("dbs_etherwatch: 802.2 PID value not valid");
                                return -1;
                        }
@@ -470,22 +479,21 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        if (wth) {
                p = strstr(months, mon);
                if (p)
-                       time.tm_mon = (p - months) / 3;
-               time.tm_year -= 1900;
+                       tm.tm_mon = (int)(p - months) / 3;
+               tm.tm_year -= 1900;
 
-               time.tm_isdst = -1;
-               wth->phdr.ts.tv_sec = mktime(&time);
-
-               wth->phdr.ts.tv_usec = csec * 10000;
+               tm.tm_isdst = -1;
+               wth->phdr.ts.secs = mktime(&tm);
+               wth->phdr.ts.nsecs = csec * 10000000;
                wth->phdr.caplen = eth_hdr_len + pkt_len;
                wth->phdr.len = eth_hdr_len + pkt_len;
        }
 
        /* Parse the hex dump */
-    count = 0;
+       count = 0;
        while (count < pkt_len) {
                if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
-                       *err = file_error(fh);
+                       *err = file_error(fh, err_info);
                        if (*err == 0) {
                                *err = WTAP_ERR_SHORT_READ;
                        }
@@ -493,13 +501,13 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
                }
                if (!(line_count = parse_single_hex_dump_line(line,
                                &buf[eth_hdr_len + count], count))) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("dbs_etherwatch: packet data value not valid");
                        return -1;
                }
                count += line_count;
                if (count > pkt_len) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("dbs_etherwatch: packet data value has too many bytes");
                        return -1;
                }
@@ -513,9 +521,9 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
 
           1         2         3         4
 0123456789012345678901234567890123456789012345
-  [E..(8.....Ò.....]-    0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A]
-  [...Ö.Ò...(¤¿Z.4y]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 BF 5A 1C 34 79]
-  [P.#(ÁC...00000..]-   32-[50 10 23 28 C1 43 00 00 03 30 30 30 30 30 00 00]
+  [E..(8...........]-    0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A]
+  [.........(..Z.4y]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 BF 5A 1C 34 79]
+  [P.#(.C...00000..]-   32-[50 10 23 28 C1 43 00 00 03 30 30 30 30 30 00 00]
   [.0              ]-   48-[03 30]
 
 /DISPLAY=HEXADECIMAL output:
@@ -551,24 +559,24 @@ parse_single_hex_dump_line(char* rec, guint8 *buf, int byte_offset) {
        int             value;
 
 
-    /* Check that the record is as least as long as the check offset */
+       /* Check that the record is as least as long as the check offset */
        for(i = 0; i < TYPE_CHECK_POS; i++)
        {
                if(rec[i] == '\0') {
                        return 0;
                }
        }
-    /* determine the format and thus the counter offset and hex dump length */
-    if(rec[TYPE_CHECK_POS] == TYPE_CHECK_BOTH)
-    {
-        pos = COUNT_POS_BOTH;
-    }
-    else
-    {
-        pos = COUNT_POS_HEX;
-    }    
+       /* determine the format and thus the counter offset and hex dump length */
+       if(rec[TYPE_CHECK_POS] == TYPE_CHECK_BOTH)
+       {
+               pos = COUNT_POS_BOTH;
+       }
+       else
+       {
+               pos = COUNT_POS_HEX;
+       }       
 
-    /* Check that the record is as least as long as the start position */
+       /* Check that the record is as least as long as the start position */
        while(i < pos)
        {
                if(rec[i] == '\0') {
@@ -634,7 +642,7 @@ parse_hex_dump(char* dump, guint8 *buf, char seperator, char end) {
                }
                pos++;
                count++;
-           /* Skip the seperator characters */
+               /* Skip the seperator characters */
                while(dump[pos] == seperator) {
                        pos++;
                }