Codebase list nfdump / bullseye-backports/main bin / output_util.c
bullseye-backports/main

Tree @bullseye-backports/main (Download .tar.gz)

output_util.c @bullseye-backports/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 *  Copyright (c) 2021, Peter Haag
 *  All rights reserved.
 *  
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions are met:
 *  
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation 
 *     and/or other materials provided with the distribution.
 *   * Neither the name of the author nor the names of its contributors may be 
 *     used to endorse or promote products derived from this software without 
 *     specific prior written permission.
 *  
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 *  POSSIBILITY OF SUCH DAMAGE.
 *  
 */

#include "config.h"

#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
#include <string.h>

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include "nfdump.h"
#include "nffile.h"
#include "output_util.h"

#define NumProtos	138
static char *protoList[NumProtos] = {
	"0",	  // 0 	masked out - no protocol info - set to '0'
	"ICMP",	  // 1 	Internet Control Message
	"IGMP",	  // 2	Internet Group Management
	"GGP",	  // 3	Gateway-to-Gateway
	"IPIP",	  // 4	IP in IP (encapsulation)
	"ST",	  // 5	Stream
	"TCP",	  // 6	Transmission Control
	"CBT",	  // 7	CBT
	"EGP",    // 8	Exterior Gateway Protocol
	"IGP",    // 9	any private interior gateway (used by Cisco for their IGRP)
	"BBN",	  // 10	BBN RCC Monitoring
	"NVPII",  // 11	Network Voice Protocol
	"PUP",	  // 12	PUP
	"ARGUS",  // 13	ARGUS
	"ENCOM",  // 14	EMCON
	"XNET",   // 15	Cross Net Debugger
	"CHAOS",  // 16	Chaos
	"UDP",	  // 17	User Datagram 
	"MUX",	  // 18	Multiplexing
	"DCN",	  // 19	DCN Measurement Subsystems
	"HMP",	  // 20	Host Monitoring
	"PRM",	  // 21	Packet Radio Measurement
	"XNS",	  // 22	XEROX NS IDP 
	"Trnk1",  // 23	Trunk-1
	"Trnk2",  // 24	Trunk-2
	"Leaf1",  // 25	Leaf-1
	"Leaf2",  // 26	Leaf-2
	"RDP",	  // 27	Reliable Data Protocol
	"IRTP",	  // 28	Internet Reliable Transaction
	"ISO-4",  // 29	ISO Transport Protocol Class 4
	"NETBK",  // 30	Bulk Data Transfer Protocol
	"MFESP",  // 31	MFE Network Services Protocol
	"MEINP",  // 32	MERIT Internodal Protocol
	"DCCP",	  // 33	Datagram Congestion Control Protocol
	"3PC",	  // 34	Third Party Connect Protocol
	"IDPR",	  // 35	Inter-Domain Policy Routing Protocol 
	"XTP",	  // 36	XTP
	"DDP",	  // 37	Datagram Delivery Protocol
	"IDPR",	  // 38	IDPR Control Message Transport Proto
	"TP++",	  // 39	TP++ Transport Protocol
	"IL",	  // 40	IL Transport Protocol
	"IPv6",	  // 41	IPv6
	"SDRP",	  // 42	Source Demand Routing Protocol
	"Rte6",   // 43	Routing Header for IPv6
	"Frag6",  // 44	Fragment Header for IPv6
	"IDRP",	  // 45	Inter-Domain Routing Protocol
	"RSVP",	  // 46	Reservation Protocol 
	"GRE",	  // 47	General Routing Encapsulation
	"MHRP",	  // 48	Mobile Host Routing Protocol
	"BNA",	  // 49	BNA
	"ESP",    // 50	Encap Security Payload 
	"AH",     // 51	Authentication Header
	"INLSP",  // 52	Integrated Net Layer Security  TUBA 
	"SWIPE",  // 53	IP with Encryption 
	"NARP",   // 54	NBMA Address Resolution Protocol
	"MOBIL",  // 55	IP Mobility
	"TLSP",   // 56	Transport Layer Security Protocol
	"SKIP",   // 57	SKIP
	"ICMP6",  // 58	ICMP for IPv6
	"NOHE6",  // 59	No Next Header for IPv6
	"OPTS6",  // 60	Destination Options for IPv6
	"HOST",   // 61	any host internal protocol
	"CFTP",   // 62	CFTP
	"NET",    // 63	any local network
	"SATNT",  // 64	SATNET and Backroom EXPAK
	"KLAN",   // 65	Kryptolan
	"RVD",    // 66	MIT Remote Virtual Disk Protocol
	"IPPC",   // 67	Internet Pluribus Packet Core
	"FS",     // 68	any distributed file system
	"SATM",   // 69	SATNET Monitoring 
	"VISA",   // 70	VISA Protocol
	"IPCV",   // 71	Internet Packet Core Utility
	"CPNX",   // 72	Computer Protocol Network Executive
	"CPHB",   // 73	Computer Protocol Heart Beat
	"WSN",    // 74	Wang Span Network
	"PVP",    // 75	Packet Video Protocol 
	"BSATM",  // 76	Backroom SATNET Monitoring
	"SUNND",  // 77	SUN ND PROTOCOL-Temporary
	"WBMON",  // 78	WIDEBAND Monitoring
	"WBEXP",  // 79	WIDEBAND EXPAK
	"ISOIP",  // 80	ISO Internet Protocol
	"VMTP",   // 81	VMTP
	"SVMTP",  // 82	SECURE-VMTP
	"VINES",  // 83	VINES
	"TTP",    // 84	TTP
	"NSIGP",  // 85	NSFNET-IGP
	"DGP",    // 86	Dissimilar Gateway Protocol
	"TCF",    // 87	TCF
	"EIGRP",  // 88	EIGRP
	"OSPF",   // 89	OSPFIGP
	"S-RPC",  // 90	Sprite RPC Protocol
	"LARP",   // 91	Locus Address Resolution Protocol
	"MTP",    // 92	Multicast Transport Protocol
	"AX.25",  // 93	AX.25 Frames
	"IPIP",	  // 94	IP-within-IP Encapsulation Protocol
	"MICP",   // 95	Mobile Internetworking Control Protocol
	"SCCSP",  // 96	Semaphore Communications Sec. Protocol
	"ETHIP",  // 97	Ethernet-within-IP Encapsulation
	"ENCAP",  // 98	Encapsulation Header
	"99",     // 99	any private encryption scheme
	"GMTP",   // 100	GMTP
	"IFMP",   // 101	Ipsilon Flow Management Protocol
	"PNNI",   // 102	PNNI over IP 
	"PIM",	  // 103	Protocol Independent Multicast
	"ARIS",   // 104	ARIS
	"SCPS",   // 105	SCPS
	"QNX",    // 106	QNX
	"A/N",    // 107	Active Networks
	"IPcmp",  // 108	IP Payload Compression Protocol
	"SNP",    // 109	Sitara Networks Protocol
	"CpqPP",  // 110	Compaq Peer Protocol
	"IPXIP",  // 111	IPX in IP
	"VRRP",   // 112	Virtual Router Redundancy Protocol
	"PGM",    // 113	PGM Reliable Transport Protocol
	"0hop",   // 114	any 0-hop protocol
	"L2TP",   // 115	Layer Two Tunneling Protocol
	"DDX",    // 116	D-II Data Exchange (DDX)
	"IATP",   // 117	Interactive Agent Transfer Protocol
	"STP",    // 118	Schedule Transfer Protocol
	"SRP",    // 119	SpectraLink Radio Protocol
	"UTI",    // 120	UTI
	"SMP",    // 121	Simple Message Protocol
	"SM",     // 122	SM
	"PTP",    // 123	Performance Transparency Protocol
	"ISIS4",  // 124	ISIS over IPv4
	"FIRE",   // 125	FIRE
	"CRTP",   // 126	Combat Radio Transport Protocol
	"CRUDP",  // 127	Combat Radio User Datagram
	"128",    // 128	SSCOPMCE
	"IPLT",   // 129	IPLP
	"SPS",    // 130	Secure Packet Shield 
	"PIPE",   // 131	Private IP Encapsulation within IP
	"SCTP",   // 132	Stream Control Transmission Protocol
	"FC",     // 133	Fibre Channel
	"134",    // 134	RSVP-E2E-IGNORE
	"MHEAD",  // 135	Mobility Header
	"UDP-L",  // 136	UDPLite
	"MPLS"    // 137	MPLS-in-IP 
};

char *ProtoString(uint8_t protoNum, uint32_t printPlain) {
static char s[16];

	if ( protoNum >= NumProtos || printPlain) {
		snprintf(s,15,"%-5i", protoNum );
		s[15] = '\0';
		return s;
	} else {
		return protoList[protoNum];
	}

} // End of ProtoString

int ProtoNum(char *protoString) {
int len;

	if ( (len = strlen(protoString)) >= 6 )
		return -1;

	for ( int i=0; i<NumProtos; i++ ) {
		if ( strncasecmp(protoString,protoList[i], len) == 0 && 
			( strlen(protoList[i]) == len) )
			return i;
	}

	return -1;

} // End of ProtoNum

char *FlagsString(uint16_t flags) {
static char string[16];

	string[0] = flags & 128 ? 'C' : '.';	// Congestion window reduced -  CWR
	string[1] = flags &  64 ? 'E' : '.';	// ECN-Echo
	string[2] = flags &  32 ? 'U' : '.';	// Urgent
	string[3] = flags &  16 ? 'A' : '.';	// Ack
	string[4] = flags &   8 ? 'P' : '.';	// Push
	string[5] = flags &   4 ? 'R' : '.';	// Reset
	string[6] = flags &   2 ? 'S' : '.';	// Syn
	string[7] = flags &   1 ? 'F' : '.';	// Fin
	string[8] = '\0';
	
	return string;
} // End of FlagsString

char *biFlowString(uint8_t biFlow) {

	switch (biFlow) {
		case 0:
			return "arbitrary";
			break;
		case 1:
			return "initiator";
			break;
		case 2:
			return "reverseInitiator";
			break;
		case 3:
			return "perimeter";
			break;
	}

	return "undef";

} // End of biFlowString

char *FlowEndString(uint8_t endReason) {

	switch (endReason) {
		case 0:
			return "";
			break;
		case 1:
			return "idle timeout";
			break;
		case 2:
			return "active timeout";
			break;
		case 3:
			return "end of Flow detected";
			break;
		case 4:
			return "forced end";
			break;
		case 5:
			return "lack of resources";
			break;
	}

	return "undef";

} // End of FlowEndString

void CondenseV6(char *s) {
size_t len = strlen(s);
char	*p, *q;

	if ( len <= 16 )
		return;

	// orig:      2001:620:1000:cafe:20e:35ff:fec0:fed5 len = 37
	// condensed: 2001:62..e0:fed5
	p = s + 7;
	*p++ = '.';
	*p++ = '.';
	q = s + len - 7;
	while ( *q ) { 
		*p++ = *q++; 
	}
	*p = 0;

} // End of CondenseV6

char *FwEventString(int event) {

	switch(event) {
#ifdef JUNOS
		case 0:
			return "IGNORE";
			break;
		case 1:
		case 4:
		case 6:
		case 8:
		case 12:
			return "CREATE";
			break;
		case 2:
		case 5:
		case 7:
		case 9:
		case 13:
			return "DELETE";
			break;
		case 3:
		case 10:
			return "EXHAUSTED";
			break;
		case 11:
			return "QUOTA EXCEED";
			break;
		case 14:
			return "NAT PORT ALLOC";
			break;
		case 15:
			return "NAT PORT RELEASE";
			break;
		case 16:
			return "NAT PORT ACTIVE";
			break;
#else
		case 0:
			return "IGNORE";
			break;
		case 1:
			return "CREATE";
			break;
		case 2:
			return "DELETE";
			break;
		case 3:
			return "DENIED";
			break;
		case 4:
			return "ALERT";
			break;
		case 5:
			return "UPDATE";
			break;
#endif
		default:
			return "UNKNOW";
	}			

} // End of FwEventString

char *EventString(int event) {

	switch(event) {
		case 0:
			return "INVALID";
			break;
		case 1:
			return "ADD";
			break;
		case 2:
			return "DELETE";
			break;
		default:
			return "UNKNOW";
	}			

} // End of EventString

char *EventXString(int xevent) {
static char s[16];

	switch( xevent) {
		case 0:
			return "Ignore";
			break;
		case 1001:
			return "I-ACL";
			break;
		case 1002:
			 return "E-ACL";
			break;
		case 1003:
			 return "Adap";
			break;
		case 1004:
			return "No Syn";
			break;
		default:
			snprintf(s,15,"%u",xevent);
			s[15] = '\0';
			return s;
	}

	// not reached
} // End of EventXString