/*
* Copyright 2002 Damien Miller <djm@mindrot.org> 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 ``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 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: softflowd.c,v 1.79 2004/09/30 04:12:36 djm Exp $ */
/*
* This is software implementation of Cisco's NetFlow(tm) traffic
* reporting system. It operates by listening (via libpcap) on a
* promiscuous interface and tracking traffic flows.
*
* Traffic flows are recorded by source/destination/protocol IP address or, in the
* case of TCP and UDP, by src_addr:src_port/dest_addr:dest_port/protocol
*
* Flows expire automatically after a period of inactivity (default: 1 hour)
* They may also be evicted (in order of age) in situations where there are
* more flows than slots available.
*
* Netflow version 1 compatible packets are sent to a specified target
* host upon flow expiry.
*
* As this implementation watches traffic promiscuously, it is likely to
* place significant load on hosts or gateways on which it is installed.
*/
#include "common.h"
#include "sys-tree.h"
#include "convtime.h"
#include "softflowd.h"
#include "treetype.h"
#include "log.h"
#include <pcap.h>
RCSID("$Id: softflowd.c,v 1.79 2004/09/30 04:12:36 djm Exp $");
/* Global variables */
static int verbose_flag = 0; /* Debugging flag */
/* Signal handler flags */
static int graceful_shutdown_request = 0;
/* Context for libpcap callback functions */
struct CB_CTXT {
struct FLOWTRACK *ft;
int linktype;
int fatal;
int want_v6;
};
/* Describes a datalink header and how to extract v4/v6 frames from it */
struct DATALINK {
int dlt; /* BPF datalink type */
int skiplen; /* Number of bytes to skip datalink header */
int ft_off; /* Datalink frametype offset */
int ft_len; /* Datalink frametype length */
int ft_is_be; /* Set if frametype is big-endian */
u_int32_t ft_mask; /* Mask applied to frametype */
u_int32_t ft_v4; /* IPv4 frametype */
u_int32_t ft_v6; /* IPv6 frametype */
};
/* Datalink types that we know about */
static const struct DATALINK lt[] = {
{ DLT_EN10MB, 14, 12, 2, 1, 0xffffffff, 0x0800, 0x86dd },
{ DLT_PPP, 5, 3, 2, 1, 0xffffffff, 0x0021, 0x0057 },
{ DLT_RAW, 0, 0, 1, 1, 0x000000f0, 0x0004, 0x0006 },
{ DLT_NULL, 4, 0, 4, 0, 0xffffffff, AF_INET, AF_INET6 },
#ifdef DLT_LOOP
{ DLT_LOOP, 4, 0, 4, 1, 0xffffffff, AF_INET, AF_INET6 },
#endif
{ -1, -1, -1, -1, -1, 0x00000000, 0xffff, 0xffff },
};
/* Netflow send functions */
typedef int (netflow_send_func_t)(struct FLOW **, int, int, u_int64_t,
struct timeval *, int);
struct NETFLOW_SENDER {
int version;
netflow_send_func_t *func;
int v6_capable;
};
/* Array of NetFlow export function that we know of. NB. nf[0] is default */
static const struct NETFLOW_SENDER nf[] = {
{ 5, send_netflow_v5, 0 },
{ 1, send_netflow_v1, 0 },
{ 9, send_netflow_v9, 1 },
{ -1, NULL, 0 },
};
/* Describes a location where we send NetFlow packets to */
struct NETFLOW_TARGET {
int fd;
const struct NETFLOW_SENDER *dialect;
};
/* Signal handlers */
static void sighand_graceful_shutdown(int signum)
{
graceful_shutdown_request = signum;
}
static void sighand_other(int signum)
{
/* XXX: this may not be completely safe */
logit(LOG_WARNING, "Exiting immediately on unexpected signal %d", signum);
_exit(0);
}
/*
* This is the flow comparison function.
*/
static int
flow_compare(struct FLOW *a, struct FLOW *b)
{
/* Be careful to avoid signed vs unsigned issues here */
int r;
if (a->af != b->af)
return (a->af > b->af ? 1 : -1);
if ((r = memcmp(&a->addr[0], &b->addr[0], sizeof(a->addr[0]))) != 0)
return (r > 0 ? 1 : -1);
if ((r = memcmp(&a->addr[1], &b->addr[1], sizeof(a->addr[1]))) != 0)
return (r > 0 ? 1 : -1);
#ifdef notyet
if (a->ip6_flowlabel[0] != 0 && b->ip6_flowlabel[0] != 0 &&
a->ip6_flowlabel[0] != b->ip6_flowlabel[0])
return (a->ip6_flowlabel[0] > b->ip6_flowlabel[0] ? 1 : -1);
if (a->ip6_flowlabel[1] != 0 && b->ip6_flowlabel[1] != 0 &&
a->ip6_flowlabel[1] != b->ip6_flowlabel[1])
return (a->ip6_flowlabel[1] > b->ip6_flowlabel[1] ? 1 : -1);
#endif
if (a->protocol != b->protocol)
return (a->protocol > b->protocol ? 1 : -1);
if (a->port[0] != b->port[0])
return (ntohs(a->port[0]) > ntohs(b->port[0]) ? 1 : -1);
if (a->port[1] != b->port[1])
return (ntohs(a->port[1]) > ntohs(b->port[1]) ? 1 : -1);
return (0);
}
/* Generate functions for flow tree */
FLOW_PROTOTYPE(FLOWS, FLOW, trp, flow_compare);
FLOW_GENERATE(FLOWS, FLOW, trp, flow_compare);
/*
* This is the expiry comparison function.
*/
static int
expiry_compare(struct EXPIRY *a, struct EXPIRY *b)
{
if (a->expires_at != b->expires_at)
return (a->expires_at > b->expires_at ? 1 : -1);
/* Make expiry entries unique by comparing flow sequence */
if (a->flow->flow_seq != b->flow->flow_seq)
return (a->flow->flow_seq > b->flow->flow_seq ? 1 : -1);
return (0);
}
/* Generate functions for flow tree */
EXPIRY_PROTOTYPE(EXPIRIES, EXPIRY, trp, expiry_compare);
EXPIRY_GENERATE(EXPIRIES, EXPIRY, trp, expiry_compare);
#if 0
/* Dump a packet */
static void
dump_packet(const u_int8_t *p, int len)
{
char buf[1024], tmp[3];
int i;
for (*buf = '\0', i = 0; i < len; i++) {
snprintf(tmp, sizeof(tmp), "%02x%s", p[i], i % 2 ? " " : "");
if (strlcat(buf, tmp, sizeof(buf) - 4) >= sizeof(buf) - 4) {
strlcat(buf, "...", sizeof(buf));
break;
}
}
logit(LOG_INFO, "packet len %d: %s", len, buf);
}
#endif
/* Format a time in an ISOish format */
static const char *
format_time(time_t t)
{
struct tm *tm;
static char buf[20];
tm = localtime(&t);
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", tm);
return (buf);
}
/* Format a flow in a verbose and ugly way */
static const char *
format_flow(struct FLOW *flow)
{
char addr1[64], addr2[64], stime[20], ftime[20];
static char buf[1024];
inet_ntop(flow->af, &flow->addr[0], addr1, sizeof(addr1));
inet_ntop(flow->af, &flow->addr[1], addr2, sizeof(addr2));
snprintf(stime, sizeof(ftime), "%s",
format_time(flow->flow_start.tv_sec));
snprintf(ftime, sizeof(ftime), "%s",
format_time(flow->flow_last.tv_sec));
snprintf(buf, sizeof(buf), "seq:%llu [%s]:%hu <> [%s]:%hu proto:%u "
"octets>:%u packets>:%u octets<:%u packets<:%u "
"start:%s.%03ld finish:%s.%03ld tcp>:%02x tcp<:%02x "
"flowlabel>:%08x flowlabel<:%08x ",
flow->flow_seq,
addr1, ntohs(flow->port[0]), addr2, ntohs(flow->port[1]),
(int)flow->protocol,
flow->octets[0], flow->packets[0],
flow->octets[1], flow->packets[1],
stime, (flow->flow_start.tv_usec + 500) / 1000,
ftime, (flow->flow_start.tv_usec + 500) / 1000,
flow->tcp_flags[0], flow->tcp_flags[1],
flow->ip6_flowlabel[0], flow->ip6_flowlabel[1]);
return (buf);
}
/* Format a flow in a brief way */
static const char *
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共30个文件
c:11个
h:6个
in:2个
make/make install安装后,即可以使用。 可以指定监听某个接口/pcap文件,生成v5/v9版本的netflow,并且可以将它转发给指定的netflow采集器。 用法示例: ./softflowd -i em1 -v 5 -n 1.1.1.1:9999 ./softflowd -r /root/xxx.pcap -v 5 1.1.1.1:9999
资源推荐
资源详情
资源评论
收起资源包目录
softflowd-0.9.6.tar.gz (30个子文件)
softflowd-0.9.6
daemon.c 2KB
convtime.c 2KB
collector.pl 7KB
convtime.h 2KB
strlcat.c 2KB
sys-tree.h 22KB
netflow1.c 5KB
softflowctl.c 3KB
common.h 4KB
netflow9.c 12KB
config.h.in 4KB
aclocal.m4 17B
TODO 4KB
softflowd.c 49KB
strlcpy.c 2KB
README 2KB
Makefile.in 1KB
softflowctl.8 3KB
.cvsignore 22B
install-sh 5KB
log.c 2KB
netflow5.c 5KB
closefrom.c 3KB
configure 216KB
softflowd.h 7KB
log.h 1KB
ChangeLog 7KB
softflowd.8 10KB
configure.ac 6KB
treetype.h 3KB
共 30 条
- 1
rongyongfeikai2
- 粉丝: 558
- 资源: 68
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页