source: tools/tracereport/port_report.c @ d74ca03

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since d74ca03 was ee6e802, checked in by Shane Alcock <salcock@…>, 5 years ago

Updated copyright blurb on all source files

In some cases, this meant adding copyright blurbs to files that
had never had them before.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26
27
28#include <netdb.h>
29#include <inttypes.h>
30#include <lt_inttypes.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include "libtrace.h"
35#include "tracereport.h"
36#include "contain.h"
37#include "report.h"
38
39stat_t *ports[3][256] = {{NULL}};
40char protn[256]={0};
41static bool suppress[3] = {true,true,true};
42
43void port_per_packet(struct libtrace_packet_t *packet)
44{
45        uint8_t proto;
46        int port;
47        libtrace_direction_t dir = trace_get_direction(packet);
48
49        if(trace_get_transport(packet,&proto,NULL)==NULL) 
50                return;
51
52        if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING)
53                dir = TRACE_DIR_OTHER;
54       
55        port = trace_get_server_port(proto,
56                        trace_get_source_port(packet),
57                        trace_get_destination_port(packet))==USE_SOURCE
58                ? trace_get_source_port(packet)
59                : trace_get_destination_port(packet);
60
61        if (!ports[dir][proto])
62                ports[dir][proto]=calloc(65536,sizeof(stat_t));
63        ports[dir][proto][port].bytes+=trace_get_wire_length(packet);
64        ports[dir][proto][port].count++;
65        protn[proto]=1;
66        suppress[dir] = false;
67}
68
69
70static void port_port(int i,char *prot, int j, FILE *out)
71{
72        struct servent *ent = getservbyport(htons(j),prot);
73        int k;
74       
75        if(ent){
76                fprintf(out,"%16s:",ent->s_name);
77        }
78        else{
79                fprintf(out,"%16i:",j);
80        }
81
82        for (k = 0; k < 3; k++) {
83                if (!ports[k][i])
84                        continue;
85                if (k != 0) {
86                        fprintf(out, "%16s", " ");
87                }
88                switch (k) {
89                        case 0:
90                                fprintf(out, "\t%10s", "Outbound");
91                                break;
92                        case 1:
93                                fprintf(out, "\t%10s", "Inbound");
94                                break;
95                        case 2:
96                                fprintf(out, "\t%10s", "Unknown");
97                                break;
98                }
99                fprintf(out, "\t%16" PRIu64 " %16" PRIu64 "\n",
100                        ports[k][i][j].bytes,
101                        ports[k][i][j].count);
102        }
103}
104
105static void port_protocol(int i, FILE *out)
106{
107        int j,k;
108        struct protoent *ent = getprotobynumber(i);
109        fprintf(out, "Protocol: %i %s%s%s\n",i,
110                        ent?"(":"",ent?ent->p_name:"",ent?")":"");
111        for(j=0;j<65536;++j) {
112                for(k=0;k<3;k++){
113                        if (ports[k][i] && ports[k][i][j].count) {
114                                port_port(i,ent?ent->p_name:"",j, out);
115                                break;
116                        }
117                }
118        }
119}
120
121void port_report(void)
122{
123        int i;
124        FILE *out = fopen("ports.rpt", "w");
125        if (!out) {
126                perror("fopen");
127                return;
128        }
129        fprintf(out, "%-16s\t%10s\t%16s %16s\n",
130                        "PORT",
131                        "DIRECTION",
132                        "BYTES",
133                        "PACKETS");     
134
135        setservent(1);
136        setprotoent(1);
137        for(i=0;i<256;++i) {
138                if (protn[i]) {
139                        port_protocol(i, out);
140                        free(ports[0][i]);
141                        free(ports[1][i]);
142                        free(ports[2][i]);
143                }
144        }
145        endprotoent();
146        endservent();
147        fclose(out);
148}
Note: See TracBrowser for help on using the repository browser.