2 * iostat 2003 Ronnie Sahlberg
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
36 #include <epan/packet_info.h>
37 #include <epan/packet.h>
38 #include <epan/addr_resolv.h>
40 #include <epan/conversation.h>
41 #include <epan/stat_cmd_args.h>
43 #include <epan/dissectors/packet-ip.h>
44 #include <epan/dissectors/packet-ipv6.h>
45 #include <epan/dissectors/packet-ipx.h>
46 #include <epan/dissectors/packet-tcp.h>
47 #include <epan/dissectors/packet-udp.h>
48 #include <epan/dissectors/packet-eth.h>
49 #include <epan/dissectors/packet-sctp.h>
50 #include <epan/dissectors/packet-tr.h>
51 #include <epan/dissectors/packet-scsi.h>
52 #include <epan/dissectors/packet-fc.h>
53 #include <epan/dissectors/packet-fddi.h>
55 typedef struct _io_users_t {
58 struct _io_users_item_t *items;
61 typedef struct _io_users_item_t {
62 struct _io_users_item_t *next;
75 iousers_udpip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vudph)
78 const e_udphdr *udph=vudph;
79 char name1[256],name2[256];
83 if(udph->uh_sport>udph->uh_dport){
85 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
86 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
87 } else if(udph->uh_sport<udph->uh_dport){
89 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
90 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
91 } else if(CMP_ADDRESS(&udph->ip_src, &udph->ip_dst)>0){
93 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
94 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
97 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
98 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
101 for(iui=iu->items;iui;iui=iui->next){
102 if((!strcmp(iui->name1, name1))
103 && (!strcmp(iui->name2, name2)) ){
109 iui=g_malloc(sizeof(io_users_item_t));
112 /* iui->addr1=NULL;*/
113 iui->name1=g_strdup(name1);
114 /* iui->addr2=NULL;*/
115 iui->name2=g_strdup(name2);
124 iui->bytes1+=pinfo->fd->pkt_len;
127 iui->bytes2+=pinfo->fd->pkt_len;
135 iousers_sctp_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vsctp)
138 const struct _sctp_info* sctph = vsctp;
139 char name1[256],name2[256], s_sport[10], s_dport[10];
140 io_users_item_t *iui;
143 g_snprintf(s_sport, sizeof s_sport, "%d",sctph->sport);
144 g_snprintf(s_dport, sizeof s_dport, "%d",sctph->dport);
146 if(sctph->sport > sctph->dport) {
148 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&sctph->ip_src),s_sport);
149 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&sctph->ip_dst),s_dport);
150 } else if(sctph->sport < sctph->dport) {
152 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&sctph->ip_src),s_sport);
153 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&sctph->ip_dst),s_dport);
156 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&sctph->ip_src),s_sport);
157 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&sctph->ip_dst),s_dport);
160 for(iui=iu->items;iui;iui=iui->next){
161 if((!strcmp(iui->name1, name1))
162 && (!strcmp(iui->name2, name2)) ){
168 iui=g_malloc(sizeof(io_users_item_t));
171 /* iui->addr1=NULL;*/
172 iui->name1=g_strdup(name1);
173 /* iui->addr2=NULL;*/
174 iui->name2=g_strdup(name2);
183 iui->bytes1+=pinfo->fd->pkt_len;
186 iui->bytes2+=pinfo->fd->pkt_len;
194 iousers_tcpip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vtcph)
197 const struct tcpheader *tcph=vtcph;
198 char name1[256],name2[256];
199 io_users_item_t *iui;
202 if(tcph->th_sport>tcph->th_dport){
204 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
205 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
206 } else if(tcph->th_sport<tcph->th_dport){
208 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
209 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
210 } else if(CMP_ADDRESS(&tcph->ip_src, &tcph->ip_dst)>0){
212 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
213 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
216 g_snprintf(name2,256,"%s:%s",ep_address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
217 g_snprintf(name1,256,"%s:%s",ep_address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
220 for(iui=iu->items;iui;iui=iui->next){
221 if((!strcmp(iui->name1, name1))
222 && (!strcmp(iui->name2, name2)) ){
228 iui=g_malloc(sizeof(io_users_item_t));
231 /* iui->addr1=NULL;*/
232 iui->name1=g_strdup(name1);
233 /* iui->addr2=NULL;*/
234 iui->name2=g_strdup(name2);
243 iui->bytes1+=pinfo->fd->pkt_len;
246 iui->bytes2+=pinfo->fd->pkt_len;
254 iousers_ip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip)
257 const ws_ip *iph=vip;
258 const address *addr1, *addr2;
259 io_users_item_t *iui;
261 if(CMP_ADDRESS(&iph->ip_src, &iph->ip_dst)>0){
269 for(iui=iu->items;iui;iui=iui->next){
270 if((!CMP_ADDRESS(&iui->addr1, addr1))
271 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
277 iui=g_malloc(sizeof(io_users_item_t));
280 COPY_ADDRESS(&iui->addr1, addr1);
281 iui->name1=g_strdup(ep_address_to_str(addr1));
282 COPY_ADDRESS(&iui->addr2, addr2);
283 iui->name2=g_strdup(ep_address_to_str(addr2));
290 if(!CMP_ADDRESS(&iph->ip_dst, &iui->addr1)){
292 iui->bytes1+=pinfo->fd->pkt_len;
295 iui->bytes2+=pinfo->fd->pkt_len;
302 iousers_ipv6_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip)
305 const struct ip6_hdr *ip6h=vip;
307 const address *addr1, *addr2;
308 io_users_item_t *iui;
310 /* Addresses aren't implemented as 'address' type in struct ip6_hdr */
311 src.type = dst.type = AT_IPv6;
312 src.len = dst.len = sizeof(struct e_in6_addr);
313 src.data = &ip6h->ip6_src;
314 dst.data = &ip6h->ip6_dst;
316 if(CMP_ADDRESS(&src, &dst)>0){
324 for(iui=iu->items;iui;iui=iui->next){
325 if((!CMP_ADDRESS(&iui->addr1, addr1))
326 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
332 iui=g_malloc(sizeof(io_users_item_t));
335 COPY_ADDRESS(&iui->addr1, addr1);
336 iui->name1=g_strdup(ep_address_to_str(addr1));
337 COPY_ADDRESS(&iui->addr2, addr2);
338 iui->name2=g_strdup(ep_address_to_str(addr2));
345 if(!CMP_ADDRESS(&dst, &iui->addr1)){
347 iui->bytes1+=pinfo->fd->pkt_len;
350 iui->bytes2+=pinfo->fd->pkt_len;
357 iousers_ipx_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vipx)
360 const ipxhdr_t *ipxh=vipx;
361 const address *addr1, *addr2;
362 io_users_item_t *iui;
364 if(CMP_ADDRESS(&ipxh->ipx_src, &ipxh->ipx_dst)>0){
365 addr1=&ipxh->ipx_src;
366 addr2=&ipxh->ipx_dst;
368 addr2=&ipxh->ipx_src;
369 addr1=&ipxh->ipx_dst;
372 for(iui=iu->items;iui;iui=iui->next){
373 if((!CMP_ADDRESS(&iui->addr1, addr1))
374 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
380 iui=g_malloc(sizeof(io_users_item_t));
383 COPY_ADDRESS(&iui->addr1, addr1);
384 iui->name1=g_strdup(ep_address_to_str(addr1));
385 COPY_ADDRESS(&iui->addr2, addr2);
386 iui->name2=g_strdup(ep_address_to_str(addr2));
393 if(!CMP_ADDRESS(&ipxh->ipx_dst, &iui->addr1)){
395 iui->bytes1+=pinfo->fd->pkt_len;
398 iui->bytes2+=pinfo->fd->pkt_len;
405 iousers_fc_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vfc)
408 const fc_hdr *fchdr=vfc;
409 const address *addr1, *addr2;
410 io_users_item_t *iui;
412 if(CMP_ADDRESS(&fchdr->s_id, &fchdr->d_id)<0){
420 for(iui=iu->items;iui;iui=iui->next){
421 if((!CMP_ADDRESS(&iui->addr1, addr1))
422 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
428 iui=g_malloc(sizeof(io_users_item_t));
431 COPY_ADDRESS(&iui->addr1, addr1);
432 iui->name1=g_strdup(ep_address_to_str(addr1));
433 COPY_ADDRESS(&iui->addr2, addr2);
434 iui->name2=g_strdup(ep_address_to_str(addr2));
441 if(!CMP_ADDRESS(&fchdr->d_id,&iui->addr1)){
443 iui->bytes1+=pinfo->fd->pkt_len;
446 iui->bytes2+=pinfo->fd->pkt_len;
453 iousers_eth_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *veth)
456 const eth_hdr *ehdr=veth;
457 const address *addr1, *addr2;
458 io_users_item_t *iui;
460 if(CMP_ADDRESS(&ehdr->src, &ehdr->dst)<0){
468 for(iui=iu->items;iui;iui=iui->next){
469 if((!CMP_ADDRESS(&iui->addr1, addr1))
470 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
476 iui=g_malloc(sizeof(io_users_item_t));
479 COPY_ADDRESS(&iui->addr1, addr1);
480 iui->name1=g_strdup(ep_address_to_str(addr1));
481 COPY_ADDRESS(&iui->addr2, addr2);
482 iui->name2=g_strdup(ep_address_to_str(addr2));
489 if(!CMP_ADDRESS(&ehdr->dst,&iui->addr1)){
491 iui->bytes1+=pinfo->fd->pkt_len;
494 iui->bytes2+=pinfo->fd->pkt_len;
501 iousers_fddi_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *veth)
504 const fddi_hdr *ehdr=veth;
505 const address *addr1, *addr2;
506 io_users_item_t *iui;
508 if(CMP_ADDRESS(&ehdr->src, &ehdr->dst)<0){
516 for(iui=iu->items;iui;iui=iui->next){
517 if((!CMP_ADDRESS(&iui->addr1, addr1))
518 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
524 iui=g_malloc(sizeof(io_users_item_t));
527 COPY_ADDRESS(&iui->addr1, addr1);
528 iui->name1=g_strdup(ep_address_to_str(addr1));
529 COPY_ADDRESS(&iui->addr2, addr2);
530 iui->name2=g_strdup(ep_address_to_str(addr2));
537 if(!CMP_ADDRESS(&ehdr->dst,&iui->addr1)){
539 iui->bytes1+=pinfo->fd->pkt_len;
542 iui->bytes2+=pinfo->fd->pkt_len;
549 iousers_tr_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vtr)
552 const tr_hdr *trhdr=vtr;
553 const address *addr1, *addr2;
554 io_users_item_t *iui;
556 if(CMP_ADDRESS(&trhdr->src, &trhdr->dst)<0){
564 for(iui=iu->items;iui;iui=iui->next){
565 if((!CMP_ADDRESS(&iui->addr1, addr1))
566 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
572 iui=g_malloc(sizeof(io_users_item_t));
575 COPY_ADDRESS(&iui->addr1, addr1);
576 iui->name1=g_strdup(ep_address_to_str(addr1));
577 COPY_ADDRESS(&iui->addr2, addr2);
578 iui->name2=g_strdup(ep_address_to_str(addr2));
585 if(!CMP_ADDRESS(&trhdr->dst,&iui->addr1)){
587 iui->bytes1+=pinfo->fd->pkt_len;
590 iui->bytes2+=pinfo->fd->pkt_len;
597 iousers_draw(void *arg)
599 io_users_t *iu = arg;
600 io_users_item_t *iui;
601 guint32 last_frames, max_frames;
603 printf("================================================================================\n");
604 printf("%s Conversations\n",iu->type);
605 printf("Filter:%s\n",iu->filter?iu->filter:"<No Filter>");
606 printf(" | <- | | -> | | Total |\n");
607 printf(" | Frames Bytes | | Frames Bytes | | Frames Bytes |\n");
608 max_frames=0xffffffff;
611 for(iui=iu->items;iui;iui=iui->next){
613 tot_frames=iui->frames1+iui->frames2;
615 if((tot_frames>last_frames)
616 &&(tot_frames<max_frames)){
617 last_frames=tot_frames;
620 for(iui=iu->items;iui;iui=iui->next){
622 tot_frames=iui->frames1+iui->frames2;
624 if(tot_frames==last_frames){
625 printf("%-20s <-> %-20s %6d %9" G_GINT64_MODIFIER "d %6d %9" G_GINT64_MODIFIER "d %6d %9" G_GINT64_MODIFIER "d\n",
626 iui->name1, iui->name2,
627 iui->frames1, iui->bytes1,
628 iui->frames2, iui->bytes2,
629 iui->frames1+iui->frames2,
630 iui->bytes1+iui->bytes2
634 max_frames=last_frames;
635 } while(last_frames);
636 printf("================================================================================\n");
640 iousers_init(const char *optarg, void* userdata _U_)
642 const char *filter=NULL;
643 const char *tap_type, *tap_type_name;
644 tap_packet_cb packet_func;
646 GString *error_string;
648 if(!strncmp(optarg,"conv,eth",8)){
655 tap_type_name="Ethernet";
656 packet_func=iousers_eth_packet;
657 } else if(!strncmp(optarg,"conv,fc",7)){
664 tap_type_name="Fibre Channel";
665 packet_func=iousers_fc_packet;
666 } else if(!strncmp(optarg,"conv,fddi",9)){
673 tap_type_name="FDDI";
674 packet_func=iousers_fddi_packet;
675 } else if(!strncmp(optarg,"conv,tcp",8)){
683 packet_func=iousers_tcpip_packet;
684 } else if(!strncmp(optarg,"conv,udp",8)){
692 packet_func=iousers_udpip_packet;
693 } else if(!strncmp(optarg,"conv,tr",7)){
700 tap_type_name="Token Ring";
701 packet_func=iousers_tr_packet;
702 } else if(!strncmp(optarg,"conv,ipx",8)){
710 packet_func=iousers_ipx_packet;
711 } else if(!strncmp(optarg,"conv,ip",7)){
718 tap_type_name="IPv4";
719 packet_func=iousers_ip_packet;
720 } else if(!strncmp(optarg,"conv,ipv6",7)){
727 tap_type_name="IPv6";
728 packet_func=iousers_ipv6_packet;
729 } else if(!strncmp(optarg,"conv,sctp",9)) {
736 tap_type_name="SCTP";
737 packet_func=iousers_sctp_packet;
739 fprintf(stderr, "tshark: invalid \"-z conv,<type>[,<filter>]\" argument\n");
740 fprintf(stderr," <type> must be one of\n");
741 fprintf(stderr," \"eth\"\n");
742 fprintf(stderr," \"fc\"\n");
743 fprintf(stderr," \"fddi\"\n");
744 fprintf(stderr," \"ip\"\n");
745 fprintf(stderr," \"ipx\"\n");
746 fprintf(stderr," \"sctp\"\n");
747 fprintf(stderr," \"tcp\"\n");
748 fprintf(stderr," \"tr\"\n");
749 fprintf(stderr," \"udp\"\n");
754 iu=g_malloc(sizeof(io_users_t));
756 iu->type=tap_type_name;
758 iu->filter=g_strdup(filter);
763 error_string=register_tap_listener(tap_type, iu, filter, 0, NULL, packet_func, iousers_draw);
769 fprintf(stderr, "tshark: Couldn't register conversations tap: %s\n",
771 g_string_free(error_string, TRUE);
778 register_tap_listener_iousers(void)
780 register_stat_cmd_arg("conv,", iousers_init, NULL);