]> The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
34baf8a366a50ea8508151643f3b1d59fc8b46f7
[tcpdump] / print-cnfp.c
1 /* $OpenBSD: print-cnfp.c,v 1.2 1998/06/25 20:26:59 mickey Exp $ */
2
3 /*
4 * Copyright (c) 1998 Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Michael Shalayeff.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* Cisco NetFlow protocol */
34
35 #ifndef lint
36 static const char rcsid[] =
37 "@(#) $Header: /tcpdump/master/tcpdump/print-cnfp.c,v 1.3 2000-04-27 13:45:11 itojun Exp $";
38 #endif
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <sys/socket.h>
47 #include <netdb.h>
48
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
51
52 #include <stdio.h>
53 #include <string.h>
54
55 #include "interface.h"
56
57 struct nfhdr {
58 u_int32_t ver_cnt; /* version [15], and # of records */
59 u_int32_t msys_uptime;
60 u_int32_t utc_sec;
61 u_int32_t utc_nsec;
62 u_int32_t sequence; /* v5 flow sequence number */
63 u_int32_t reserved; /* v5 only */
64 };
65
66 struct nfrec {
67 struct in_addr src_ina;
68 struct in_addr dst_ina;
69 struct in_addr nhop_ina;
70 u_int32_t ifaces; /* src,dst ifaces */
71 u_int32_t packets;
72 u_int32_t octets;
73 u_int32_t start_time; /* sys_uptime value */
74 u_int32_t last_time; /* sys_uptime value */
75 u_int32_t ports; /* src,dst ports */
76 u_int32_t proto_tos; /* proto, tos, pad, flags(v5) */
77 u_int32_t asses; /* v1: flags; v5: src,dst AS */
78 u_int32_t masks; /* src,dst addr prefix */
79
80 };
81
82 void
83 cnfp_print(const u_char *cp, u_int len, const u_char *bp)
84 {
85 register const struct nfhdr *nh;
86 register const struct nfrec *nr;
87 register const struct ip *ip;
88 struct protoent *pent;
89 int nrecs, ver;
90 time_t t;
91 char *p;
92
93 ip = (struct ip *)bp;
94 nh = (struct nfhdr *)cp;
95
96 if ((u_char *)(nh + 1) > snapend)
97 return;
98
99 nrecs = ntohl(nh->ver_cnt) & 0xffff;
100 ver = (ntohl(nh->ver_cnt) & 0xffff0000) >> 16;
101 t = ntohl(nh->utc_sec);
102 /* (p = ctime(&t))[24] = '\0'; */
103
104 printf("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
105 ntohl(nh->msys_uptime)/1000, ntohl(nh->msys_uptime)%1000,
106 ntohl(nh->utc_sec), ntohl(nh->utc_nsec));
107
108 if (ver == 5) {
109 printf("#%u, ", htonl(nh->sequence));
110 nr = (struct nfrec *)&nh[1];
111 snaplen -= 24;
112 } else {
113 nr = (struct nfrec *)&nh->sequence;
114 snaplen -= 16;
115 }
116
117 printf("%2u recs", nrecs);
118
119 for (; nrecs-- && (u_char *)(nr + 1) <= snapend; nr++) {
120 char buf[5];
121 char asbuf[7];
122
123 printf("\n started %u.%03u, last %u.%03u",
124 ntohl(nr->start_time)/1000, ntohl(nr->start_time)%1000,
125 ntohl(nr->last_time)/1000, ntohl(nr->last_time)%1000);
126
127 asbuf[0] = buf[0] = '\0';
128 if (ver == 5) {
129 sprintf(buf, "/%d", (ntohl(nr->masks) >> 24) & 0xff);
130 sprintf(asbuf, "%d:", (ntohl(nr->asses) >> 16) & 0xffff);
131 }
132 printf("\n %s%s%s:%u ", inet_ntoa(nr->src_ina), buf, asbuf,
133 ntohl(nr->ports) >> 16);
134
135 if (ver == 5) {
136 sprintf(buf, "/%d", (ntohl(nr->masks) >> 16) & 0xff);
137 sprintf(asbuf, "%d:", ntohl(nr->asses) & 0xffff);
138 }
139 printf("> %s%s%s:%u ", inet_ntoa(nr->dst_ina), buf, asbuf,
140 ntohl(nr->ports) & 0xffff);
141
142 printf(">> %s\n ", inet_ntoa(nr->nhop_ina));
143
144 pent = getprotobynumber((ntohl(nr->proto_tos) >> 8) & 0xff);
145 if (!pent || nflag)
146 printf("%u ", (ntohl(nr->proto_tos) >> 8) & 0xff);
147 else
148 printf("%s ", pent->p_name);
149
150 /* tcp flags for tcp only */
151 if (pent && pent->p_proto == IPPROTO_TCP) {
152 int flags;
153 if (ver == 1)
154 flags = (ntohl(nr->asses) >> 24) & 0xff;
155 else
156 flags = (ntohl(nr->proto_tos) >> 16) & 0xff;
157 if (flags & TH_FIN) putchar('F');
158 if (flags & TH_SYN) putchar('S');
159 if (flags & TH_RST) putchar('R');
160 if (flags & TH_PUSH) putchar('P');
161 if (flags & TH_ACK) putchar('A');
162 if (flags & TH_URG) putchar('U');
163 if (flags)
164 putchar(' ');
165 }
166 printf("tos %u, %u (%u octets)", ntohl(nr->proto_tos) & 0xff,
167 ntohl(nr->packets), ntohl(nr->octets));
168
169
170 }
171
172 }
173