📄 flow-export.c
字号:
/* * Copyright (c) 2001 Mark Fullmer and The Ohio State University * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. * * $Id: flow-export.c,v 1.24 2003/04/02 18:03:01 maf Exp $ */#include "ftconfig.h"#include <ftlib.h>#include <sys/time.h>#include <sys/types.h>#include <sys/uio.h>#include <ctype.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <fcntl.h>#if HAVE_STRINGS_H #include <strings.h>#endif#if HAVE_STRING_H #include <string.h>#endif#ifdef HAVE_MYSQL#include <mysql.h>#define DB_DEFAULT_DBHOST "localhost"#define DB_DEFAULT_DBNAME "netflow"#define DB_DEFAULT_DBPORT 3306#define DB_DEFAULT_DBTABLE "raw"#define DB_DEFAULT_DBUSER "netflow"#define DB_DEFAULT_DBPWD "netflow"#endif /* MYSQL */#if HAVE_LL_STRTOUL #define strtoull strtoul#endif /* HAVE_LL_STRTOULL */#include "ftbuild.h"#include "pcap.h"#include "cflowd.h"#define PRCOMMA\ if (comma)\ printf(",");\struct options { char dbaseURI[256]; u_int32 cflowd_mask; u_int64 ft_mask; u_long records;};struct jump { int (*where)(struct ftio *ftio, struct options *opt);};int format0(struct ftio *ftio, struct options *opt);int format1(struct ftio *ftio, struct options *opt);int format2(struct ftio *ftio, struct options *opt);int format3(struct ftio *ftio, struct options *opt);int format4(struct ftio *ftio, struct options *opt);int ftxfield_tocflow(u_int64 xfields, u_int32 *cfmask);int fmt_xfields_val(char *fmt_buf, char *rec, struct fts3rec_offsets *fo, u_int64 xfields, int quote);int fmt_xfields_type(char *buf, u_int64 xfield);void usage(void);#define NFORMATS 5 /* nformats - 1 */struct jump format[] = {{format0}, {format1}, {format2}, {format3}, {format4}};int main(int argc, char **argv){ int i, format_index, ret, ascii_mask; struct ftio ftio; struct ftprof ftp; struct options opt; int debug; /* init fterr */ fterr_setid(argv[0]); debug = 0; format_index = 0; bzero(&opt, sizeof opt); ascii_mask = 0; opt.cflowd_mask = 0xFFFFFFFFL; opt.ft_mask = 0xFFFFFFFFFFFFFFFFLL; /* profile */ ftprof_start (&ftp); while ((i = getopt(argc, argv, "h?d:f:m:u:")) != -1) switch (i) { case 'd': /* debug */ debug = atoi(optarg); break; case 'f': /* format */ format_index = atoi(optarg); break; case 'h': /* help */ case '?': usage(); exit (0); break; case 'm': /* cflowd mask */ if (isalpha((int)optarg[0])) { ascii_mask = 1; if (ftxfield_parse(optarg, &opt.ft_mask) < 0) fterr_errx(1, "ftxfield_parse(): failed"); } else { opt.cflowd_mask = strtoul(optarg, (char **)0L, 0); opt.ft_mask = strtoull(optarg, (char **)0L, 0); } break; case 'u': /* db URI */ if (strlen(optarg) >= sizeof (opt.dbaseURI)) fterr_errx(1, "dbaseURI string too long."); strcpy(opt.dbaseURI, optarg); break; default: usage(); exit (1); break; } /* switch */ if (argc - optind) fterr_errx(1, "Extra arguments starting with %s.", argv[optind]); if (format_index >= NFORMATS) fterr_errx(1, "No such format, %d", format_index); if ((format_index == 0) && ascii_mask) { opt.cflowd_mask = 0; if (ftxfield_tocflow(opt.ft_mask, &opt.cflowd_mask) < 0) { fterr_errx(1, "ftxfield_tocflow(): failed"); } } if (ftio_init(&ftio, 0, FT_IO_FLAG_READ) < 0) fterr_errx(1, "ftio_init(): failed"); ret = format[format_index].where(&ftio, &opt); if ((!ret) && (debug > 0)) { ftprof_end(&ftp, ftio_get_rec_total(&ftio)); ftprof_print(&ftp, argv[0], stderr); } fprintf(stderr, "%s: Exported %lu records\n", argv[0], opt.records); return ret;} /* main *//* * function: format0 * * export flows in cflowd format*/int format0(struct ftio *ftio, struct options *opt){ struct fts3rec_offsets fo; struct ftver ftv; struct fttime ftt; char *rec; u_int32 ui32, index, sysUpTime, unix_secs, unix_nsecs, First, Last; u_int16 ui16; u_int8 ui8; ftio_get_ver(ftio, &ftv); fts3rec_compute_offsets(&fo, &ftv); switch (ftv.d_version) { case 1: opt->cflowd_mask &= CF_INDEX_V1_MASK; break; case 5: opt->cflowd_mask &= CF_INDEX_V5_MASK; break; case 6: opt->cflowd_mask &= CF_INDEX_V6_MASK; break; case 7: opt->cflowd_mask &= CF_INDEX_V7_MASK; break; case 1005: opt->cflowd_mask &= CF_INDEX_V5_MASK; break; case 8: switch (ftv.agg_method) { case 1: opt->cflowd_mask &= CF_INDEX_V8_1_MASK; break; case 2: opt->cflowd_mask &= CF_INDEX_V8_2_MASK; break; case 3: opt->cflowd_mask &= CF_INDEX_V8_3_MASK; break; case 4: opt->cflowd_mask &= CF_INDEX_V8_4_MASK; break; case 5: opt->cflowd_mask &= CF_INDEX_V8_5_MASK; break; case 6: opt->cflowd_mask &= CF_INDEX_V8_6_MASK; break; case 7: opt->cflowd_mask &= CF_INDEX_V8_7_MASK; break; case 8: opt->cflowd_mask &= CF_INDEX_V8_8_MASK; break; case 9: opt->cflowd_mask &= CF_INDEX_V8_9_MASK; break; case 10: opt->cflowd_mask &= CF_INDEX_V8_10_MASK; break; case 11: opt->cflowd_mask &= CF_INDEX_V8_11_MASK; break; case 12: opt->cflowd_mask &= CF_INDEX_V8_12_MASK; break; case 13: opt->cflowd_mask &= CF_INDEX_V8_13_MASK; break; case 14: opt->cflowd_mask &= CF_INDEX_V8_14_MASK; break; default: fterr_warnx("Unsupported export version"); return -1; } /* switch */ break; default: fterr_warnx("Unsupported export version"); return -1; } /* switch */ /* index */ index = opt->cflowd_mask; index = htonl(index); while ((rec = ftio_read(ftio))) { fwrite(&index, sizeof (index), 1, stdout); if (opt->cflowd_mask & CF_ROUTERMASK) { ui32 = *((u_int32*)(rec+fo.exaddr)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_SRCIPADDRMASK) { ui32 = *((u_int32*)(rec+fo.srcaddr)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_DSTIPADDRMASK) { ui32 = *((u_int32*)(rec+fo.dstaddr)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_INPUTIFINDEXMASK) { ui16 = *((u_int16*)(rec+fo.input)); ui16 = htons(ui16); fwrite(&ui16, sizeof (ui16), 1, stdout); } if (opt->cflowd_mask & CF_OUTPUTIFINDEXMASK) { ui16 = *((u_int16*)(rec+fo.output)); ui16 = htons(ui16); fwrite(&ui16, sizeof (ui16), 1, stdout); } if (opt->cflowd_mask & CF_SRCPORTMASK) { ui16 = *((u_int16*)(rec+fo.srcport)); ui16 = htons(ui16); fwrite(&ui16, sizeof (ui16), 1, stdout); } if (opt->cflowd_mask & CF_DSTPORTMASK) { ui16 = *((u_int16*)(rec+fo.dstport)); ui16 = htons(ui16); fwrite(&ui16, sizeof (ui16), 1, stdout); } if (opt->cflowd_mask & CF_PKTSMASK) { ui32 = *((u_int32*)(rec+fo.dPkts)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_BYTESMASK) { ui32 = *((u_int32*)(rec+fo.dOctets)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_IPNEXTHOPMASK) { ui32 = *((u_int32*)(rec+fo.nexthop)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_STARTTIMEMASK) { sysUpTime = *((u_int32*)(rec+fo.sysUpTime)); unix_secs = *((u_int32*)(rec+fo.unix_secs)); unix_nsecs = *((u_int32*)(rec+fo.unix_nsecs)); First = *((u_int32*)(rec+fo.First)); ftt = ftltime(sysUpTime, unix_secs, unix_nsecs, First); ui32 = htonl(ftt.secs); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_ENDTIMEMASK) { sysUpTime = *((u_int32*)(rec+fo.sysUpTime)); unix_secs = *((u_int32*)(rec+fo.unix_secs)); unix_nsecs = *((u_int32*)(rec+fo.unix_nsecs)); Last = *((u_int32*)(rec+fo.Last)); ftt = ftltime(sysUpTime, unix_secs, unix_nsecs, Last); ui32 = htonl(ftt.secs); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_PROTOCOLMASK) { ui8 = *((u_int8*)(rec+fo.prot)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_TOSMASK) { ui8 = *((u_int8*)(rec+fo.tos)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_SRCASMASK) { ui16 = *((u_int16*)(rec+fo.src_as)); ui16 = htons(ui16); fwrite(&ui16, sizeof (ui16), 1, stdout); } if (opt->cflowd_mask & CF_DSTASMASK) { ui16 = *((u_int16*)(rec+fo.dst_as)); ui16 = htons(ui16); fwrite(&ui16, sizeof (ui16), 1, stdout); } if (opt->cflowd_mask & CF_SRCMASKLENMASK) { ui8 = *((u_int8*)(rec+fo.src_mask)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_DSTMASKLENMASK) { ui8 = *((u_int8*)(rec+fo.dst_mask)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_TCPFLAGSMASK) { ui8 = *((u_int8*)(rec+fo.tcp_flags)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_INPUTENCAPMASK) { ui8 = *((u_int8*)(rec+fo.in_encaps)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_OUTPUTENCAPMASK) { ui8 = *((u_int8*)(rec+fo.out_encaps)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_PEERNEXTHOPMASK) { ui32 = *((u_int32*)(rec+fo.peer_nexthop)); ui32 = htonl(ui32); fwrite(&ui32, sizeof (ui32), 1, stdout); } if (opt->cflowd_mask & CF_ENGINETYPEMASK) { ui8 = *((u_int8*)(rec+fo.engine_type)); fwrite(&ui8, sizeof (ui8), 1, stdout); } if (opt->cflowd_mask & CF_ENGINEIDMASK) { ui8 = *((u_int8*)(rec+fo.engine_id)); fwrite(&ui8, sizeof (ui8), 1, stdout); } ++opt->records; } /* while */ return 0;} /* format 0 *//* * function: format1 * * export flows in pcap format. Hack to use tcpdump's packet matcher*/int format1(struct ftio *ftio, struct options *opt){ struct timeval now; struct timezone tz; struct fts3rec_all cur; struct fts3rec_offsets fo; struct ftver ftv; struct pcap_file_header pfh; struct pcap_packet_header pph; struct pcap_data1 pd1; struct pcap_data2 pd2; struct pcap_data3 pd3; struct pcap_data4 pd4; int bsize, bsize2, good;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -