opa: Add dissectors for Intel’s Omni-Path Architecture (OPA)
[metze/wireshark/wip.git] / wiretap / cosine.c
index 8d6b905d8611af0dfcf58f5635ad2a556589ea7e..c7f5952294211a2c289b1f40531d3a06d452c620 100644 (file)
  */
 
 #include "config.h"
-#include "wftap-int.h"
 #include "wtap-int.h"
-#include "buffer.h"
 #include "cosine.h"
 #include "file_wrappers.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
 /*
 
 #define COSINE_REC_MAGIC_STR2  COSINE_HDR_MAGIC_STR2
 
 #define COSINE_HEADER_LINES_TO_CHECK   200
-#define COSINE_LINE_LENGTH             240
-
-#define COSINE_MAX_PACKET_LEN  65536
+#define COSINE_LINE_LENGTH             240
 
 static gboolean empty_line(const gchar *line);
-static gint64 cosine_seek_next_packet(wftap *wfth, int *err, gchar **err_info,
+static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
        char *hdr);
-static gboolean cosine_check_file_type(wftap *wfth, int *err, gchar **err_info);
-static gboolean cosine_read(wftap *wfth, int *err, gchar **err_info,
+static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info);
+static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
        gint64 *data_offset);
-static gboolean cosine_seek_read(wftap *wfth, gint64 seek_off,
-       void* header, Buffer *buf, int *err, gchar **err_info);
-static int parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
-       int *err, gchar **err_info);
-static gboolean parse_cosine_hex_dump(FILE_T fh, struct wtap_pkthdr *phdr,
-       int pkt_len, Buffer* buf, int *err, gchar **err_info);
+static gboolean cosine_seek_read(wtap *wth, gint64 seek_off,
+       struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
+static int parse_cosine_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer* buf,
+       char *line, int *err, gchar **err_info);
 static int parse_single_hex_dump_line(char* rec, guint8 *buf,
        guint byte_offset);
 
@@ -183,7 +175,7 @@ static int parse_single_hex_dump_line(char* rec, guint8 *buf,
 static gboolean empty_line(const gchar *line)
 {
        while (*line) {
-               if (isspace((guchar)*line)) {
+               if (g_ascii_isspace(*line)) {
                        line++;
                        continue;
                } else {
@@ -200,21 +192,21 @@ static gboolean empty_line(const gchar *line)
    byte offset. Copy the header line to hdr. Returns -1 on failure,
    and sets "*err" to the error and sets "*err_info" to null or an
    additional error string. */
-static gint64 cosine_seek_next_packet(wftap *wfth, int *err, gchar **err_info,
+static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
        char *hdr)
 {
        gint64 cur_off;
        char buf[COSINE_LINE_LENGTH];
 
        while (1) {
-               cur_off = file_tell(wfth->fh);
+               cur_off = file_tell(wth->fh);
                if (cur_off == -1) {
                        /* Error */
-                       *err = file_error(wfth->fh, err_info);
+                       *err = file_error(wth->fh, err_info);
                        return -1;
                }
-               if (file_gets(buf, sizeof(buf), wfth->fh) == NULL) {
-                       *err = file_error(wfth->fh, err_info);
+               if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
+                       *err = file_error(wth->fh, err_info);
                        return -1;
                }
                if (strstr(buf, COSINE_REC_MAGIC_STR1) ||
@@ -233,7 +225,7 @@ static gint64 cosine_seek_next_packet(wftap *wfth, int *err, gchar **err_info,
  * 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 additional error string.
  */
-static gboolean cosine_check_file_type(wftap *wfth, int *err, gchar **err_info)
+static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info)
 {
        char    buf[COSINE_LINE_LENGTH];
        gsize   reclen;
@@ -242,9 +234,9 @@ static gboolean cosine_check_file_type(wftap *wfth, int *err, gchar **err_info)
        buf[COSINE_LINE_LENGTH-1] = '\0';
 
        for (line = 0; line < COSINE_HEADER_LINES_TO_CHECK; line++) {
-               if (file_gets(buf, COSINE_LINE_LENGTH, wfth->fh) == NULL) {
+               if (file_gets(buf, COSINE_LINE_LENGTH, wth->fh) == NULL) {
                        /* EOF or error. */
-                       *err = file_error(wfth->fh, err_info);
+                       *err = file_error(wth->fh, err_info);
                        return FALSE;
                }
 
@@ -264,80 +256,66 @@ static gboolean cosine_check_file_type(wftap *wfth, int *err, gchar **err_info)
 }
 
 
-int cosine_open(wftap *wfth, int *err, gchar **err_info)
+wtap_open_return_val cosine_open(wtap *wth, int *err, gchar **err_info)
 {
        /* Look for CoSine header */
-       if (!cosine_check_file_type(wfth, err, err_info)) {
+       if (!cosine_check_file_type(wth, err, err_info)) {
                if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
 
-       if (file_seek(wfth->fh, 0L, SEEK_SET, err) == -1)       /* rewind */
-               return -1;
+       if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1)        /* rewind */
+               return WTAP_OPEN_ERROR;
 
-       wfth->file_encap = WTAP_ENCAP_COSINE;
-       wfth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COSINE;
-       wfth->snapshot_length = 0; /* not known */
-       wfth->subtype_read = cosine_read;
-       wfth->subtype_seek_read = cosine_seek_read;
-       wfth->tsprecision = WTAP_FILE_TSPREC_CSEC;
+       wth->file_encap = WTAP_ENCAP_COSINE;
+       wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COSINE;
+       wth->snapshot_length = 0; /* not known */
+       wth->subtype_read = cosine_read;
+       wth->subtype_seek_read = cosine_seek_read;
+       wth->file_tsprec = WTAP_TSPREC_CSEC;
 
-       return 1;
+       return WTAP_OPEN_MINE;
 }
 
 /* Find the next packet and parse it; called from wtap_read(). */
-static gboolean cosine_read(wftap *wfth, int *err, gchar **err_info,
+static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset)
 {
        gint64  offset;
-       int     pkt_len;
        char    line[COSINE_LINE_LENGTH];
-       wtap* wth = (wtap*)wfth->tap_specific_data;
 
        /* Find the next packet */
-       offset = cosine_seek_next_packet(wfth, err, err_info, line);
+       offset = cosine_seek_next_packet(wth, err, err_info, line);
        if (offset < 0)
                return FALSE;
        *data_offset = offset;
 
-       /* Parse the header */
-       pkt_len = parse_cosine_rec_hdr(&wth->phdr, line, err, err_info);
-       if (pkt_len == -1)
-               return FALSE;
-
-       /* Convert the ASCII hex dump to binary data */
-       return parse_cosine_hex_dump(wfth->fh, &wth->phdr, pkt_len,
-           wfth->frame_buffer, err, err_info);
+       /* Parse the header and convert the ASCII hex dump to binary data */
+       return parse_cosine_packet(wth->fh, &wth->phdr, wth->frame_buffer,
+           line, err, err_info);
 }
 
 /* Used to read packets in random-access fashion */
 static gboolean
-cosine_seek_read(wftap *wfth, gint64 seek_off, void* header,
+cosine_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
        Buffer *buf, int *err, gchar **err_info)
 {
-       int     pkt_len;
        char    line[COSINE_LINE_LENGTH];
-       struct wtap_pkthdr *phdr = (struct wtap_pkthdr*)header;
 
-       if (file_seek(wfth->random_fh, seek_off, SEEK_SET, err) == -1)
+       if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
                return FALSE;
 
-       if (file_gets(line, COSINE_LINE_LENGTH, wfth->random_fh) == NULL) {
-               *err = file_error(wfth->random_fh, err_info);
+       if (file_gets(line, COSINE_LINE_LENGTH, wth->random_fh) == NULL) {
+               *err = file_error(wth->random_fh, err_info);
                if (*err == 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
                return FALSE;
        }
 
-       /* Parse the header */
-       pkt_len = parse_cosine_rec_hdr(phdr, line, err, err_info);
-       if (pkt_len == -1)
-               return FALSE;
-
-       /* Convert the ASCII hex dump to binary data */
-       return parse_cosine_hex_dump(wfth->random_fh, phdr, pkt_len, buf, err,
+       /* Parse the header and convert the ASCII hex dump to binary data */
+       return parse_cosine_packet(wth->random_fh, phdr, buf, line, err,
            err_info);
 }
 
@@ -346,9 +324,9 @@ cosine_seek_read(wftap *wfth, gint64 seek_off, void* header,
         2002-5-10,20:1:31.4:  l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0]
     2) output to PE without date and time
         l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0] */
-static int
-parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
-     int *err, gchar **err_info)
+static gboolean
+parse_cosine_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
+    char *line, int *err, gchar **err_info)
 {
        union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
        int     num_items_scanned;
@@ -357,6 +335,8 @@ parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
        guint   code1, code2;
        char    if_name[COSINE_MAX_IF_NAME_LEN] = "", direction[6] = "";
        struct  tm tm;
+       guint8 *pd;
+       int     i, hex_lines, n, caplen = 0;
 
        if (sscanf(line, "%4d-%2d-%2d,%2d:%2d:%2d.%9d:",
                   &yy, &mm, &dd, &hr, &min, &sec, &csec) == 7) {
@@ -371,7 +351,7 @@ parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
                if (num_items_scanned != 17) {
                        *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("cosine: purported control blade line doesn't have code values");
-                       return -1;
+                       return FALSE;
                }
        } else {
                /* appears to be output to PE */
@@ -384,11 +364,27 @@ parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
                if (num_items_scanned != 10) {
                        *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("cosine: header line is neither control blade nor PE output");
-                       return -1;
+                       return FALSE;
                }
                yy = mm = dd = hr = min = sec = csec = 0;
        }
+       if (pkt_len < 0) {
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup("cosine: packet header has a negative packet length");
+               return FALSE;
+       }
+       if (pkt_len > WTAP_MAX_PACKET_SIZE) {
+               /*
+                * Probably a corrupt capture file; don't blow up trying
+                * to allocate space for an immensely-large packet.
+                */
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup_printf("cosine: File has %u-byte packet, bigger than maximum of %u",
+                   pkt_len, WTAP_MAX_PACKET_SIZE);
+               return FALSE;
+       }
 
+       phdr->rec_type = REC_TYPE_PACKET;
        phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
        tm.tm_year = yy - 1900;
        tm.tm_mon = mm - 1;
@@ -435,22 +431,9 @@ parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
        pseudo_header->cosine.rm = rm;
        pseudo_header->cosine.err = error;
 
-       return pkt_len;
-}
-
-/* Converts ASCII hex dump to binary data. Returns TRUE on success,
-   FALSE if any error is encountered. */
-static gboolean
-parse_cosine_hex_dump(FILE_T fh, struct wtap_pkthdr *phdr, int pkt_len,
-    Buffer* buf, int *err, gchar **err_info)
-{
-       guint8 *pd;
-       gchar   line[COSINE_LINE_LENGTH];
-       int     i, hex_lines, n, caplen = 0;
-
        /* Make sure we have enough room for the packet */
-       buffer_assure_space(buf, COSINE_MAX_PACKET_LEN);
-       pd = buffer_start_ptr(buf);
+       ws_buffer_assure_space(buf, pkt_len);
+       pd = ws_buffer_start_ptr(buf);
 
        /* Calculate the number of hex dump lines, each
         * containing 16 bytes of data */
@@ -478,7 +461,6 @@ parse_cosine_hex_dump(FILE_T fh, struct wtap_pkthdr *phdr, int pkt_len,
        return TRUE;
 }
 
-
 /* Take a string representing one line from a hex dump and converts
  * the text to binary data. We place the bytes in the buffer at the
  * specified offset.
@@ -507,3 +489,16 @@ parse_single_hex_dump_line(char* rec, guint8 *buf, guint byte_offset)
 
        return num_items_scanned;
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 noexpandtab:
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */