From Harald Welte:
[obnox/wireshark/wip.git] / wiretap / dbs-etherwatch.c
index d1540aae6a4293ee60725dcf90a79e4cd68291b3..366a118aba9fe7be67986006894161d9f16da84f 100644 (file)
@@ -100,8 +100,10 @@ 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 gint64 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;
@@ -115,7 +117,7 @@ static gint64 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;
@@ -128,9 +130,8 @@ static gint64 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;
 }
@@ -142,13 +143,16 @@ static gint64 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;
 
@@ -178,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;
                }
        }
@@ -187,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
@@ -217,7 +221,7 @@ static gboolean dbs_etherwatch_read(wtap *wth, int *err, gchar **err_info,
        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;
 
@@ -256,7 +260,7 @@ dbs_etherwatch_seek_read (wtap *wth, gint64 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);
                }
@@ -324,9 +328,9 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        int     eth_hdr_len, pkt_len, csec;
        int length_pos, length_from, length;
        struct tm tm;
-       char mon[4];
+       char mon[4] = "xxx";
        gchar *p;
-       static gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
+       static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
        int     count, line_count;
 
        eth_hdr_len = 0;
@@ -335,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;
                }
@@ -345,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;
        }
@@ -370,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;
        }
@@ -378,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;
                }
@@ -387,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,
                                &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;
        }
@@ -412,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;
                }
@@ -428,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;
                }
@@ -436,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;
                }
@@ -446,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;
                        }
@@ -460,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;
                        }
@@ -475,7 +479,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        if (wth) {
                p = strstr(months, mon);
                if (p)
-                       tm.tm_mon = (p - months) / 3;
+                       tm.tm_mon = (int)(p - months) / 3;
                tm.tm_year -= 1900;
 
                tm.tm_isdst = -1;
@@ -489,7 +493,7 @@ parse_dbs_etherwatch_packet(wtap *wth, FILE_T fh, guint8* buf, int *err,
        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;
                        }
@@ -497,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;
                }