Add 'dtds' and 'fix' directories to the checklicense whitelist, they don't
[metze/wireshark/wip.git] / wiretap / iptrace.c
index b9a382c8100877f0d2f3f7d5099cd4eedea867a3..6fc466afd4b304c4ca170f8dc72198f02f59dd21 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
 #include "atm.h"
 #include "iptrace.h"
 
+#define IPTRACE_IFT_HF 0x3d    /* Support for PERCS IP-HFI*/
+#define IPTRACE_IFT_IB  0xc7    /* IP over Infiniband. Number by IANA */
+
 static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean iptrace_seek_read_1_0(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guint8 *pd, int packet_size,
+    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
     int *err, gchar **err_info);
 
 static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean iptrace_seek_read_2_0(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guint8 *pd, int packet_size,
+    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
     int *err, gchar **err_info);
 
 static int iptrace_read_rec_header(FILE_T fh, guint8 *header, int header_len,
@@ -61,12 +62,11 @@ int iptrace_open(wtap *wth, int *err, gchar **err_info)
        bytes_read = file_read(name, 11, wth->fh);
        if (bytes_read != 11) {
                *err = file_error(wth->fh, err_info);
-               if (*err != 0)
+               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
                        return -1;
                return 0;
        }
-       wth->data_offset += 11;
-       name[11] = 0;
+       name[11] = '\0';
 
        if (strcmp(name, "iptrace 1.0") == 0) {
                wth->file_type = WTAP_FILE_IPTRACE_1_0;
@@ -134,14 +134,13 @@ static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
        guint8                  fddi_padding[3];
 
        /* Read the descriptor data */
-       *data_offset = wth->data_offset;
+       *data_offset = file_tell(wth->fh);
        ret = iptrace_read_rec_header(wth->fh, header, IPTRACE_1_0_PHDR_SIZE,
            err, err_info);
        if (ret <= 0) {
                /* Read error or EOF */
                return FALSE;
        }
-       wth->data_offset += IPTRACE_1_0_PHDR_SIZE;
 
        /*
         * Byte 28 of the frame header appears to be a BSD-style IFT_xxx
@@ -185,7 +184,6 @@ static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
                        return FALSE;
                }
                packet_size -= 3;
-               wth->data_offset += 3;
 
                /*
                 * Read the padding.
@@ -210,8 +208,8 @@ static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
        if (!iptrace_read_rec_data(wth->fh, data_ptr, packet_size, err,
            err_info))
                return FALSE;   /* Read error */
-       wth->data_offset += packet_size;
 
+       wth->phdr.presence_flags = WTAP_HAS_TS;
        wth->phdr.len = packet_size;
        wth->phdr.caplen = packet_size;
        wth->phdr.ts.secs = pntohl(&header[4]);
@@ -226,7 +224,7 @@ static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
 
        /* Fill in the pseudo-header. */
        fill_in_pseudo_header(wth->phdr.pkt_encap, data_ptr, wth->phdr.caplen,
-           &wth->pseudo_header, header);
+           &wth->phdr.pseudo_header, header);
 
        /* If the per-file encapsulation isn't known, set it to this
           packet's encapsulation.
@@ -245,9 +243,10 @@ static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
 }
 
 static gboolean iptrace_seek_read_1_0(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guint8 *pd, int packet_size,
+    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
     int *err, gchar **err_info)
 {
+       union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
        int                     ret;
        guint8                  header[IPTRACE_1_0_PHDR_SIZE];
        int                     pkt_encap;
@@ -348,14 +347,13 @@ static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
        guint8                  fddi_padding[3];
 
        /* Read the descriptor data */
-       *data_offset = wth->data_offset;
+       *data_offset = file_tell(wth->fh);
        ret = iptrace_read_rec_header(wth->fh, header, IPTRACE_2_0_PHDR_SIZE,
            err, err_info);
        if (ret <= 0) {
                /* Read error or EOF */
                return FALSE;
        }
-       wth->data_offset += IPTRACE_2_0_PHDR_SIZE;
 
        /*
         * Byte 28 of the frame header appears to be a BSD-style IFT_xxx
@@ -399,7 +397,6 @@ static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
                        return FALSE;
                }
                packet_size -= 3;
-               wth->data_offset += 3;
 
                /*
                 * Read the padding.
@@ -424,23 +421,32 @@ static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
        if (!iptrace_read_rec_data(wth->fh, data_ptr, packet_size, err,
            err_info))
                return FALSE;   /* Read error */
-       wth->data_offset += packet_size;
 
+       wth->phdr.presence_flags = WTAP_HAS_TS;
        wth->phdr.len = packet_size;
        wth->phdr.caplen = packet_size;
        wth->phdr.ts.secs = pntohl(&header[32]);
        wth->phdr.ts.nsecs = pntohl(&header[36]);
 
+/* We used to error out if the interface in iptrace was unknown/unhandled,
+ * but an iptrace may contain packets from a variety of interfaces, ome known,
+ * and others unknown. It is better to display the data even for unknown interface
+ * types, isntead of erroring out. In the future, it would be nice to be able to
+ * flag which frames are shown as data because their interface type is unknown,
+ * and also preset the interface type number to the user so that it can be
+ * reported easily back to the Wireshark develoer.
+
        if (wth->phdr.pkt_encap == WTAP_ENCAP_UNKNOWN) {
                *err = WTAP_ERR_UNSUPPORTED_ENCAP;
                *err_info = g_strdup_printf("iptrace: interface type IFT=0x%02x unknown or unsupported",
                    pkt_hdr.if_type);
                return FALSE;
        }
+*/
 
        /* Fill in the pseudo-header. */
        fill_in_pseudo_header(wth->phdr.pkt_encap, data_ptr, wth->phdr.caplen,
-           &wth->pseudo_header, header);
+           &wth->phdr.pseudo_header, header);
 
        /* If the per-file encapsulation isn't known, set it to this
           packet's encapsulation.
@@ -459,9 +465,10 @@ static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
 }
 
 static gboolean iptrace_seek_read_2_0(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guint8 *pd, int packet_size,
+    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
     int *err, gchar **err_info)
 {
+       union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
        int                     ret;
        guint8                  header[IPTRACE_2_0_PHDR_SIZE];
        int                     pkt_encap;
@@ -674,6 +681,28 @@ wtap_encap_ift(unsigned int  ift)
                return ift_encap[ift];
        }
        else {
-               return WTAP_ENCAP_UNKNOWN;
+               switch(ift) {
+                       /* Infiniband*/
+                       case IPTRACE_IFT_IB:
+                               return WTAP_ENCAP_INFINIBAND;
+                               break;
+
+                       /* Host Fabric Interface */
+                       case IPTRACE_IFT_HF:
+                               /* The HFI interface on AIX provides raw IP
+                               in the packet trace. It's unclear if the HFI
+                               can be configured for any other protocol, and if
+                               any field in the iptrace header indicates what
+                               that protocol is. For now, we are hard-coding
+                               this as RAW_IP, but if we find another iptrace file
+                               using HFI that provides another protocol, we will
+                               have to figure out which field in the iptrace file
+                               encodes it. */
+                               return WTAP_ENCAP_RAW_IP;
+                               break;
+
+                       default:
+                               return WTAP_ENCAP_UNKNOWN;
+               }
        }
 }