Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-cnfp.c
blobe3f3970bf5af6668e8b8ce139b06dead31531592
1 /* $NetBSD: print-cnfp.c,v 1.5 2007/07/24 11:53:43 drochner Exp $ */
3 /* $OpenBSD: print-cnfp.c,v 1.2 1998/06/25 20:26:59 mickey Exp $ */
5 /*
6 * Copyright (c) 1998 Michael Shalayeff
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /* Cisco NetFlow protocol */
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static const char rcsid[] _U_ =
36 "@(#) Header: /tcpdump/master/tcpdump/print-cnfp.c,v 1.16.2.1 2005/04/20 20:53:39 guy Exp";
37 #else
38 __RCSID("$NetBSD: print-cnfp.c,v 1.5 2007/07/24 11:53:43 drochner Exp $");
39 #endif
40 #endif
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
46 #include <tcpdump-stdinc.h>
48 #include <stdio.h>
49 #include <string.h>
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h"
55 #include "tcp.h"
56 #include "ipproto.h"
58 struct nfhdr {
59 u_int32_t ver_cnt; /* version [15], and # of records */
60 u_int32_t msys_uptime;
61 u_int32_t utc_sec;
62 u_int32_t utc_nsec;
63 u_int32_t sequence; /* v5 flow sequence number */
64 u_int32_t reserved; /* v5 only */
67 struct nfrec {
68 struct in_addr src_ina;
69 struct in_addr dst_ina;
70 struct in_addr nhop_ina;
71 u_int32_t ifaces; /* src,dst ifaces */
72 u_int32_t packets;
73 u_int32_t octets;
74 u_int32_t start_time; /* sys_uptime value */
75 u_int32_t last_time; /* sys_uptime value */
76 u_int32_t ports; /* src,dst ports */
77 u_int32_t proto_tos; /* proto, tos, pad, flags(v5) */
78 u_int32_t asses; /* v1: flags; v5: src,dst AS */
79 u_int32_t masks; /* src,dst addr prefix; v6: encaps */
80 struct in_addr peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
83 void
84 cnfp_print(const u_char *cp, const u_char *bp _U_)
86 register const struct nfhdr *nh;
87 register const struct nfrec *nr;
88 struct protoent *pent;
89 int nrecs, ver;
90 #if 0
91 time_t t;
92 #endif
94 nh = (const struct nfhdr *)cp;
96 if ((const u_char *)(nh + 1) > snapend)
97 return;
99 nrecs = EXTRACT_32BITS(&nh->ver_cnt) & 0xffff;
100 ver = (EXTRACT_32BITS(&nh->ver_cnt) & 0xffff0000) >> 16;
101 #if 0
103 * This is seconds since the UN*X epoch, and is followed by
104 * nanoseconds. XXX - format it, rather than just dumping the
105 * raw seconds-since-the-Epoch.
107 t = EXTRACT_32BITS(&nh->utc_sec);
108 #endif
110 printf("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
111 EXTRACT_32BITS(&nh->msys_uptime)/1000,
112 EXTRACT_32BITS(&nh->msys_uptime)%1000,
113 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec));
115 if (ver == 5 || ver == 6) {
116 printf("#%u, ", EXTRACT_32BITS(&nh->sequence));
117 nr = (const struct nfrec *)&nh[1];
118 snaplen -= 24;
119 } else {
120 nr = (const struct nfrec *)&nh->sequence;
121 snaplen -= 16;
124 printf("%2u recs", nrecs);
126 for (; nrecs-- && (const u_char *)(nr + 1) <= snapend; nr++) {
127 char buf[20];
128 char asbuf[20];
130 printf("\n started %u.%03u, last %u.%03u",
131 EXTRACT_32BITS(&nr->start_time)/1000,
132 EXTRACT_32BITS(&nr->start_time)%1000,
133 EXTRACT_32BITS(&nr->last_time)/1000,
134 EXTRACT_32BITS(&nr->last_time)%1000);
136 asbuf[0] = buf[0] = '\0';
137 if (ver == 5 || ver == 6) {
138 snprintf(buf, sizeof(buf), "/%u",
139 (EXTRACT_32BITS(&nr->masks) >> 24) & 0xff);
140 snprintf(asbuf, sizeof(asbuf), ":%u",
141 (EXTRACT_32BITS(&nr->asses) >> 16) & 0xffff);
143 printf("\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
144 EXTRACT_32BITS(&nr->ports) >> 16);
146 if (ver == 5 || ver ==6) {
147 snprintf(buf, sizeof(buf), "/%d",
148 (EXTRACT_32BITS(&nr->masks) >> 16) & 0xff);
149 snprintf(asbuf, sizeof(asbuf), ":%u",
150 EXTRACT_32BITS(&nr->asses) & 0xffff);
152 printf("> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
153 EXTRACT_32BITS(&nr->ports) & 0xffff);
155 printf(">> %s\n ", intoa(nr->nhop_ina.s_addr));
157 pent = getprotobynumber((EXTRACT_32BITS(&nr->proto_tos) >> 8) & 0xff);
158 if (!pent || nflag)
159 printf("%u ",
160 (EXTRACT_32BITS(&nr->proto_tos) >> 8) & 0xff);
161 else
162 printf("%s ", pent->p_name);
164 /* tcp flags for tcp only */
165 if (pent && pent->p_proto == IPPROTO_TCP) {
166 int flags;
167 if (ver == 1)
168 flags = (EXTRACT_32BITS(&nr->asses) >> 24) & 0xff;
169 else
170 flags = (EXTRACT_32BITS(&nr->proto_tos) >> 16) & 0xff;
171 if (flags & TH_FIN) putchar('F');
172 if (flags & TH_SYN) putchar('S');
173 if (flags & TH_RST) putchar('R');
174 if (flags & TH_PUSH) putchar('P');
175 if (flags & TH_ACK) putchar('A');
176 if (flags & TH_URG) putchar('U');
177 if (flags)
178 putchar(' ');
181 buf[0]='\0';
182 if (ver == 6) {
183 snprintf(buf, sizeof(buf), "(%u<>%u encaps)",
184 (EXTRACT_32BITS(&nr->masks) >> 8) & 0xff,
185 (EXTRACT_32BITS(&nr->masks)) & 0xff);
187 printf("tos %u, %u (%u octets) %s",
188 EXTRACT_32BITS(&nr->proto_tos) & 0xff,
189 EXTRACT_32BITS(&nr->packets),
190 EXTRACT_32BITS(&nr->octets), buf);