2 * iostat 2003 Ronnie Sahlberg
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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/stat_cmd_args.h>
42 #include <epan/dissectors/packet-ip.h>
43 #include <epan/dissectors/packet-ipx.h>
44 #include <epan/dissectors/packet-tcp.h>
45 #include <epan/dissectors/packet-udp.h>
46 #include <epan/dissectors/packet-eth.h>
47 #include <epan/dissectors/packet-sctp.h>
48 #include <epan/dissectors/packet-tr.h>
49 #include <epan/dissectors/packet-fc.h>
50 #include <epan/dissectors/packet-fddi.h>
52 typedef struct _io_users_t {
55 struct _io_users_item_t *items;
58 typedef struct _io_users_item_t {
59 struct _io_users_item_t *next;
72 iousers_udpip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vudph)
75 const e_udphdr *udph=vudph;
76 char name1[256],name2[256];
80 if(udph->uh_sport>udph->uh_dport){
82 g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
83 g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
84 } else if(udph->uh_sport<udph->uh_dport){
86 g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
87 g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
88 } else if(CMP_ADDRESS(&udph->ip_src, &udph->ip_dst)>0){
90 g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
91 g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
94 g_snprintf(name2,256,"%s:%s",address_to_str(&udph->ip_src),get_udp_port(udph->uh_sport));
95 g_snprintf(name1,256,"%s:%s",address_to_str(&udph->ip_dst),get_udp_port(udph->uh_dport));
98 for(iui=iu->items;iui;iui=iui->next){
99 if((!strcmp(iui->name1, name1))
100 && (!strcmp(iui->name2, name2)) ){
106 iui=g_malloc(sizeof(io_users_item_t));
109 /* iui->addr1=NULL;*/
110 iui->name1=strdup(name1);
111 /* iui->addr2=NULL;*/
112 iui->name2=strdup(name2);
121 iui->bytes1+=pinfo->fd->pkt_len;
124 iui->bytes2+=pinfo->fd->pkt_len;
132 iousers_sctp_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vsctp)
135 const struct _sctp_info* sctph = vsctp;
136 char name1[256],name2[256], s_sport[10], s_dport[10];
137 io_users_item_t *iui;
140 g_snprintf(s_sport, sizeof s_sport, "%d",sctph->sport);
141 g_snprintf(s_dport, sizeof s_dport, "%d",sctph->dport);
143 if(sctph->sport > sctph->dport) {
145 g_snprintf(name1,256,"%s:%s",address_to_str(&sctph->ip_src),s_sport);
146 g_snprintf(name2,256,"%s:%s",address_to_str(&sctph->ip_dst),s_dport);
147 } else if(sctph->sport < sctph->dport) {
149 g_snprintf(name1,256,"%s:%s",address_to_str(&sctph->ip_src),s_sport);
150 g_snprintf(name2,256,"%s:%s",address_to_str(&sctph->ip_dst),s_dport);
153 g_snprintf(name1,256,"%s:%s",address_to_str(&sctph->ip_src),s_sport);
154 g_snprintf(name2,256,"%s:%s",address_to_str(&sctph->ip_dst),s_dport);
157 for(iui=iu->items;iui;iui=iui->next){
158 if((!strcmp(iui->name1, name1))
159 && (!strcmp(iui->name2, name2)) ){
165 iui=g_malloc(sizeof(io_users_item_t));
168 /* iui->addr1=NULL;*/
169 iui->name1=strdup(name1);
170 /* iui->addr2=NULL;*/
171 iui->name2=strdup(name2);
180 iui->bytes1+=pinfo->fd->pkt_len;
183 iui->bytes2+=pinfo->fd->pkt_len;
191 iousers_tcpip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vtcph)
194 const struct tcpheader *tcph=vtcph;
195 char name1[256],name2[256];
196 io_users_item_t *iui;
199 if(tcph->th_sport>tcph->th_dport){
201 g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
202 g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
203 } else if(tcph->th_sport<tcph->th_dport){
205 g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
206 g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
207 } else if(CMP_ADDRESS(&tcph->ip_src, &tcph->ip_dst)>0){
209 g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
210 g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
213 g_snprintf(name2,256,"%s:%s",address_to_str(&tcph->ip_src),get_tcp_port(tcph->th_sport));
214 g_snprintf(name1,256,"%s:%s",address_to_str(&tcph->ip_dst),get_tcp_port(tcph->th_dport));
217 for(iui=iu->items;iui;iui=iui->next){
218 if((!strcmp(iui->name1, name1))
219 && (!strcmp(iui->name2, name2)) ){
225 iui=g_malloc(sizeof(io_users_item_t));
228 /* iui->addr1=NULL;*/
229 iui->name1=strdup(name1);
230 /* iui->addr2=NULL;*/
231 iui->name2=strdup(name2);
240 iui->bytes1+=pinfo->fd->pkt_len;
243 iui->bytes2+=pinfo->fd->pkt_len;
251 iousers_ip_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip)
255 const address *addr1, *addr2;
256 io_users_item_t *iui;
258 if(CMP_ADDRESS(&iph->ip_src, &iph->ip_dst)>0){
266 for(iui=iu->items;iui;iui=iui->next){
267 if((!CMP_ADDRESS(&iui->addr1, addr1))
268 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
274 iui=g_malloc(sizeof(io_users_item_t));
277 COPY_ADDRESS(&iui->addr1, addr1);
278 iui->name1=strdup(address_to_str(addr1));
279 COPY_ADDRESS(&iui->addr2, addr2);
280 iui->name2=strdup(address_to_str(addr2));
287 if(!CMP_ADDRESS(&iph->ip_dst, &iui->addr1)){
289 iui->bytes1+=pinfo->fd->pkt_len;
292 iui->bytes2+=pinfo->fd->pkt_len;
299 iousers_ipx_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vipx)
302 const ipxhdr_t *ipxh=vipx;
303 const address *addr1, *addr2;
304 io_users_item_t *iui;
306 if(CMP_ADDRESS(&ipxh->ipx_src, &ipxh->ipx_dst)>0){
307 addr1=&ipxh->ipx_src;
308 addr2=&ipxh->ipx_dst;
310 addr2=&ipxh->ipx_src;
311 addr1=&ipxh->ipx_dst;
314 for(iui=iu->items;iui;iui=iui->next){
315 if((!CMP_ADDRESS(&iui->addr1, addr1))
316 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
322 iui=g_malloc(sizeof(io_users_item_t));
325 COPY_ADDRESS(&iui->addr1, addr1);
326 iui->name1=strdup(address_to_str(addr1));
327 COPY_ADDRESS(&iui->addr2, addr2);
328 iui->name2=strdup(address_to_str(addr2));
335 if(!CMP_ADDRESS(&ipxh->ipx_dst, &iui->addr1)){
337 iui->bytes1+=pinfo->fd->pkt_len;
340 iui->bytes2+=pinfo->fd->pkt_len;
347 iousers_fc_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vfc)
350 const fc_hdr *fchdr=vfc;
351 const address *addr1, *addr2;
352 io_users_item_t *iui;
354 if(CMP_ADDRESS(&fchdr->s_id, &fchdr->d_id)<0){
362 for(iui=iu->items;iui;iui=iui->next){
363 if((!CMP_ADDRESS(&iui->addr1, addr1))
364 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
370 iui=g_malloc(sizeof(io_users_item_t));
373 COPY_ADDRESS(&iui->addr1, addr1);
374 iui->name1=strdup(address_to_str(addr1));
375 COPY_ADDRESS(&iui->addr2, addr2);
376 iui->name2=strdup(address_to_str(addr2));
383 if(!CMP_ADDRESS(&fchdr->d_id,&iui->addr1)){
385 iui->bytes1+=pinfo->fd->pkt_len;
388 iui->bytes2+=pinfo->fd->pkt_len;
395 iousers_eth_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *veth)
398 const eth_hdr *ehdr=veth;
399 const address *addr1, *addr2;
400 io_users_item_t *iui;
402 if(CMP_ADDRESS(&ehdr->src, &ehdr->dst)<0){
410 for(iui=iu->items;iui;iui=iui->next){
411 if((!CMP_ADDRESS(&iui->addr1, addr1))
412 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
418 iui=g_malloc(sizeof(io_users_item_t));
421 COPY_ADDRESS(&iui->addr1, addr1);
422 iui->name1=strdup(address_to_str(addr1));
423 COPY_ADDRESS(&iui->addr2, addr2);
424 iui->name2=strdup(address_to_str(addr2));
431 if(!CMP_ADDRESS(&ehdr->dst,&iui->addr1)){
433 iui->bytes1+=pinfo->fd->pkt_len;
436 iui->bytes2+=pinfo->fd->pkt_len;
443 iousers_fddi_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *veth)
446 const fddi_hdr *ehdr=veth;
447 const address *addr1, *addr2;
448 io_users_item_t *iui;
450 if(CMP_ADDRESS(&ehdr->src, &ehdr->dst)<0){
458 for(iui=iu->items;iui;iui=iui->next){
459 if((!CMP_ADDRESS(&iui->addr1, addr1))
460 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
466 iui=g_malloc(sizeof(io_users_item_t));
469 COPY_ADDRESS(&iui->addr1, addr1);
470 iui->name1=strdup(address_to_str(addr1));
471 COPY_ADDRESS(&iui->addr2, addr2);
472 iui->name2=strdup(address_to_str(addr2));
479 if(!CMP_ADDRESS(&ehdr->dst,&iui->addr1)){
481 iui->bytes1+=pinfo->fd->pkt_len;
484 iui->bytes2+=pinfo->fd->pkt_len;
491 iousers_tr_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vtr)
494 const tr_hdr *trhdr=vtr;
495 const address *addr1, *addr2;
496 io_users_item_t *iui;
498 if(CMP_ADDRESS(&trhdr->src, &trhdr->dst)<0){
506 for(iui=iu->items;iui;iui=iui->next){
507 if((!CMP_ADDRESS(&iui->addr1, addr1))
508 &&(!CMP_ADDRESS(&iui->addr2, addr2)) ){
514 iui=g_malloc(sizeof(io_users_item_t));
517 COPY_ADDRESS(&iui->addr1, addr1);
518 iui->name1=strdup(address_to_str(addr1));
519 COPY_ADDRESS(&iui->addr2, addr2);
520 iui->name2=strdup(address_to_str(addr2));
527 if(!CMP_ADDRESS(&trhdr->dst,&iui->addr1)){
529 iui->bytes1+=pinfo->fd->pkt_len;
532 iui->bytes2+=pinfo->fd->pkt_len;
539 iousers_draw(void *arg)
541 io_users_t *iu = arg;
542 io_users_item_t *iui;
543 guint32 last_frames, max_frames;
545 printf("================================================================================\n");
546 printf("%s Conversations\n",iu->type);
547 printf("Filter:%s\n",iu->filter?iu->filter:"<No Filter>");
548 printf(" | <- | | -> | | Total |\n");
549 printf(" | Frames Bytes | | Frames Bytes | | Frames Bytes |\n");
550 max_frames=0xffffffff;
553 for(iui=iu->items;iui;iui=iui->next){
555 tot_frames=iui->frames1+iui->frames2;
557 if((tot_frames>last_frames)
558 &&(tot_frames<max_frames)){
559 last_frames=tot_frames;
562 for(iui=iu->items;iui;iui=iui->next){
564 tot_frames=iui->frames1+iui->frames2;
566 if(tot_frames==last_frames){
567 printf("%-20s <-> %-20s %6d %9d %6d %9d %6d %9d\n",
568 iui->name1, iui->name2,
569 iui->frames1, iui->bytes1,
570 iui->frames2, iui->bytes2,
571 iui->frames1+iui->frames2,
572 iui->bytes1+iui->bytes2
576 max_frames=last_frames;
577 } while(last_frames);
578 printf("================================================================================\n");
582 iousers_init(const char *optarg)
584 const char *filter=NULL;
585 const char *tap_type, *tap_type_name;
586 tap_packet_cb packet_func;
588 GString *error_string;
590 if(!strncmp(optarg,"conv,eth",8)){
597 tap_type_name="Ethernet";
598 packet_func=iousers_eth_packet;
599 } else if(!strncmp(optarg,"conv,fc",7)){
606 tap_type_name="Fibre Channel";
607 packet_func=iousers_fc_packet;
608 } else if(!strncmp(optarg,"conv,fddi",9)){
615 tap_type_name="FDDI";
616 packet_func=iousers_fddi_packet;
617 } else if(!strncmp(optarg,"conv,tcp",8)){
625 packet_func=iousers_tcpip_packet;
626 } else if(!strncmp(optarg,"conv,udp",8)){
634 packet_func=iousers_udpip_packet;
635 } else if(!strncmp(optarg,"conv,tr",7)){
642 tap_type_name="Token Ring";
643 packet_func=iousers_tr_packet;
644 } else if(!strncmp(optarg,"conv,ipx",8)){
652 packet_func=iousers_ipx_packet;
653 } else if(!strncmp(optarg,"conv,ip",7)){
660 tap_type_name="IPv4";
661 packet_func=iousers_ip_packet;
662 } else if(!strncmp(optarg,"conv,sctp",9)) {
669 tap_type_name="SCTP";
670 packet_func=iousers_sctp_packet;
672 fprintf(stderr, "tethereal: invalid \"-z conv,<type>[,<filter>]\" argument\n");
673 fprintf(stderr," <type> must be one of\n");
674 fprintf(stderr," \"eth\"\n");
675 fprintf(stderr," \"fc\"\n");
676 fprintf(stderr," \"fddi\"\n");
677 fprintf(stderr," \"ip\"\n");
678 fprintf(stderr," \"ipx\"\n");
679 fprintf(stderr," \"sctp\"\n");
680 fprintf(stderr," \"tcp\"\n");
681 fprintf(stderr," \"tr\"\n");
682 fprintf(stderr," \"udp\"\n");
687 iu=g_malloc(sizeof(io_users_t));
689 iu->type=tap_type_name;
691 iu->filter=strdup(filter);
696 error_string=register_tap_listener(tap_type, iu, filter, NULL, packet_func, iousers_draw);
702 fprintf(stderr, "tethereal: Couldn't register conversations tap: %s\n",
704 g_string_free(error_string, TRUE);
711 register_tap_listener_iousers(void)
713 register_stat_cmd_arg("conv,", iousers_init);