Add HP Switch Protocol SAP value
[obnox/wireshark/wip.git] / tap-iousers.c
index 2b334580123205d2a095a6c502fafddcdd1fbd5b..c030f469a7f0cc4741f4eb3d528d29b473b83da6 100644 (file)
@@ -3,8 +3,8 @@
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  * 
  * This program is free software; you can redistribute it and/or
 #include <string.h>
 #include <epan/packet_info.h>
 #include <epan/packet.h>
+#include <epan/emem.h>
 #include <epan/addr_resolv.h>
-#include "tap.h"
+#include <epan/tap.h>
+#include <epan/conversation.h>
+#include <epan/stat_cmd_args.h>
 #include "register.h"
 #include <epan/dissectors/packet-ip.h>
 #include <epan/dissectors/packet-ipx.h>
 #include <epan/dissectors/packet-tcp.h>
 #include <epan/dissectors/packet-udp.h>
 #include <epan/dissectors/packet-eth.h>
+#include <epan/dissectors/packet-sctp.h>
 #include <epan/dissectors/packet-tr.h>
+#include <epan/dissectors/packet-scsi.h>
 #include <epan/dissectors/packet-fc.h>
 #include <epan/dissectors/packet-fddi.h>
-#include <string.h>
 
 typedef struct _io_users_t {
-       char *type;
+       const char *type;
        char *filter;
        struct _io_users_item_t *items;
 } io_users_t;
@@ -68,29 +72,30 @@ typedef struct _io_users_item_t {
 
 
 static int
-iousers_udpip_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *vudph)
+iousers_udpip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vudph)
 {
-       e_udphdr *udph=vudph;
+       io_users_t *iu=arg;
+       const e_udphdr *udph=vudph;
        char name1[256],name2[256];
        io_users_item_t *iui;
        int direction=0;
 
        if(udph->uh_sport>udph->uh_dport){
                direction=0;
-               snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
-               snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
        } else if(udph->uh_sport<udph->uh_dport){
                direction=1;
-               snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
-               snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
        } else if(CMP_ADDRESS(&udph->ip_src, &udph->ip_dst)>0){
                direction=0;
-               snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
-               snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
        } else {
                direction=1;
-               snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
-               snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
        }
 
        for(iui=iu->items;iui;iui=iui->next){
@@ -127,29 +132,89 @@ iousers_udpip_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_
 
 
 static int
-iousers_tcpip_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *vtcph)
+iousers_sctp_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vsctp)
 {
-       struct tcpheader *tcph=vtcph;
+       io_users_t *iu=arg;
+       const struct _sctp_info* sctph = vsctp;
+       char name1[256],name2[256], s_sport[10], s_dport[10];
+       io_users_item_t *iui;
+       int direction=0;
+
+       g_snprintf(s_sport, sizeof s_sport, "%d",sctph->sport);
+       g_snprintf(s_dport, sizeof s_dport, "%d",sctph->dport);
+       
+       if(sctph->sport > sctph->dport) {
+               direction=0;
+               g_snprintf(name1,256,"%s:%s",address_to_str(&sctph->ip_src),s_sport);   
+               g_snprintf(name2,256,"%s:%s",address_to_str(&sctph->ip_dst),s_dport);   
+       } else if(sctph->sport < sctph->dport) {
+               direction=1;
+               g_snprintf(name1,256,"%s:%s",address_to_str(&sctph->ip_src),s_sport);   
+               g_snprintf(name2,256,"%s:%s",address_to_str(&sctph->ip_dst),s_dport);   
+       } else {
+               direction=0;
+               g_snprintf(name1,256,"%s:%s",address_to_str(&sctph->ip_src),s_sport);   
+               g_snprintf(name2,256,"%s:%s",address_to_str(&sctph->ip_dst),s_dport);   
+       }
+
+       for(iui=iu->items;iui;iui=iui->next){
+               if((!strcmp(iui->name1, name1))
+                && (!strcmp(iui->name2, name2)) ){
+                       break;
+               }
+       }
+
+       if(!iui){
+                iui=g_malloc(sizeof(io_users_item_t));
+                iui->next=iu->items;
+                iu->items=iui;
+/*              iui->addr1=NULL;*/
+                iui->name1=strdup(name1);
+/*              iui->addr2=NULL;*/
+                iui->name2=strdup(name2);
+                iui->frames1=0;
+                iui->frames2=0;
+                iui->bytes1=0;
+                iui->bytes2=0;
+        }
+
+       if(direction){
+                iui->frames1++;
+                iui->bytes1+=pinfo->fd->pkt_len;
+        } else {
+                iui->frames2++;
+                iui->bytes2+=pinfo->fd->pkt_len;
+        }
+
+        return 1;
+}
+
+
+static int
+iousers_tcpip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vtcph)
+{
+       io_users_t *iu=arg;
+       const struct tcpheader *tcph=vtcph;
        char name1[256],name2[256];
        io_users_item_t *iui;
        int direction=0;
 
        if(tcph->th_sport>tcph->th_dport){
                direction=0;
-               snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
-               snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
        } else if(tcph->th_sport<tcph->th_dport){
                direction=1;
-               snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
-               snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
        } else if(CMP_ADDRESS(&tcph->ip_src, &tcph->ip_dst)>0){
                direction=0;
-               snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
-               snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
        } else {
                direction=1;
-               snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
-               snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
+               g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
+               g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
        }
 
        for(iui=iu->items;iui;iui=iui->next){
@@ -186,10 +251,11 @@ iousers_tcpip_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_
 
 
 static int
-iousers_ip_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *vip)
+iousers_ip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip)
 {
-       e_ip *iph=vip;
-       address *addr1, *addr2;
+       io_users_t *iu=arg;
+       const e_ip *iph=vip;
+       const address *addr1, *addr2;
        io_users_item_t *iui;
 
        if(CMP_ADDRESS(&iph->ip_src, &iph->ip_dst)>0){
@@ -233,10 +299,11 @@ iousers_ip_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, v
 }
 
 static int
-iousers_ipx_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *vipx)
+iousers_ipx_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vipx)
 {
-       ipxhdr_t *ipxh=vipx;
-       address *addr1, *addr2;
+       io_users_t *iu=arg;
+       const ipxhdr_t *ipxh=vipx;
+       const address *addr1, *addr2;
        io_users_item_t *iui;
 
        if(CMP_ADDRESS(&ipxh->ipx_src, &ipxh->ipx_dst)>0){
@@ -280,10 +347,11 @@ iousers_ipx_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_,
 }
 
 static int
-iousers_fc_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *vfc)
+iousers_fc_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vfc)
 {
-       fc_hdr *fchdr=vfc;
-       address *addr1, *addr2;
+       io_users_t *iu=arg;
+       const fc_hdr *fchdr=vfc;
+       const address *addr1, *addr2;
        io_users_item_t *iui;
 
        if(CMP_ADDRESS(&fchdr->s_id, &fchdr->d_id)<0){
@@ -327,10 +395,11 @@ iousers_fc_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, v
 }
 
 static int
-iousers_eth_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *veth)
+iousers_eth_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *veth)
 {
-       eth_hdr *ehdr=veth;
-       address *addr1, *addr2;
+       io_users_t *iu=arg;
+       const eth_hdr *ehdr=veth;
+       const address *addr1, *addr2;
        io_users_item_t *iui;
 
        if(CMP_ADDRESS(&ehdr->src, &ehdr->dst)<0){
@@ -374,10 +443,11 @@ iousers_eth_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_,
 }
 
 static int
-iousers_fddi_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *veth)
+iousers_fddi_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *veth)
 {
-       fddi_hdr *ehdr=veth;
-       address *addr1, *addr2;
+       io_users_t *iu=arg;
+       const fddi_hdr *ehdr=veth;
+       const address *addr1, *addr2;
        io_users_item_t *iui;
 
        if(CMP_ADDRESS(&ehdr->src, &ehdr->dst)<0){
@@ -421,10 +491,11 @@ iousers_fddi_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_,
 }
 
 static int
-iousers_tr_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, void *vtr)
+iousers_tr_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vtr)
 {
-       tr_hdr *trhdr=vtr;
-       address *addr1, *addr2;
+       io_users_t *iu=arg;
+       const tr_hdr *trhdr=vtr;
+       const address *addr1, *addr2;
        io_users_item_t *iui;
 
        if(CMP_ADDRESS(&trhdr->src, &trhdr->dst)<0){
@@ -468,8 +539,9 @@ iousers_tr_packet(io_users_t *iu, packet_info *pinfo, epan_dissect_t *edt _U_, v
 }
 
 static void
-iousers_draw(io_users_t *iu)
+iousers_draw(void *arg)
 {
+       io_users_t *iu = arg;
        io_users_item_t *iui;
        guint32 last_frames, max_frames;
 
@@ -510,11 +582,11 @@ iousers_draw(io_users_t *iu)
 }
 
 void
-iousers_init(char *optarg)
+iousers_init(const char *optarg, void* userdata _U_)
 {
-       char *filter=NULL;
-       char *tap_type, *tap_type_name;
-       static int (*packet_func)(io_users_t *, packet_info *, epan_dissect_t *, void *);
+       const char *filter=NULL;
+       const char *tap_type, *tap_type_name;
+       tap_packet_cb packet_func;
        io_users_t *iu=NULL;
        GString *error_string;
 
@@ -590,14 +662,24 @@ iousers_init(char *optarg)
                tap_type="ip";
                tap_type_name="IPv4";
                packet_func=iousers_ip_packet;
+       } else if(!strncmp(optarg,"conv,sctp",9)) {
+               if(optarg[9]==','){
+                               filter=optarg+10;
+               } else {
+                        filter=NULL;
+                }
+               tap_type="sctp";
+               tap_type_name="SCTP";
+               packet_func=iousers_sctp_packet;
        } else {
-               fprintf(stderr, "tethereal: invalid \"-z conv,<type>[,<filter>]\" argument\n");
+               fprintf(stderr, "tshark: invalid \"-z conv,<type>[,<filter>]\" argument\n");
                fprintf(stderr,"   <type> must be one of\n");
                fprintf(stderr,"      \"eth\"\n");
                fprintf(stderr,"      \"fc\"\n");
                fprintf(stderr,"      \"fddi\"\n");
                fprintf(stderr,"      \"ip\"\n");
                fprintf(stderr,"      \"ipx\"\n");
+               fprintf(stderr,"      \"sctp\"\n");
                fprintf(stderr,"      \"tcp\"\n");
                fprintf(stderr,"      \"tr\"\n");
                fprintf(stderr,"      \"udp\"\n");
@@ -614,13 +696,13 @@ iousers_init(char *optarg)
                iu->filter=NULL;
        }
 
-       error_string=register_tap_listener(tap_type, iu, filter, NULL, (void*)packet_func, (void*)iousers_draw);
+       error_string=register_tap_listener(tap_type, iu, filter, NULL, packet_func, iousers_draw);
        if(error_string){
                if(iu->items){
                        g_free(iu->items);
                }
                g_free(iu);
-               fprintf(stderr, "tethereal: Couldn't register conversations tap: %s\n",
+               fprintf(stderr, "tshark: Couldn't register conversations tap: %s\n",
                    error_string->str);
                g_string_free(error_string, TRUE);
                exit(1);
@@ -631,6 +713,5 @@ iousers_init(char *optarg)
 void
 register_tap_listener_iousers(void)
 {
-       register_ethereal_tap("conv,", iousers_init);
+       register_stat_cmd_arg("conv,", iousers_init, NULL);
 }
-