aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2002-06-16 23:11:33 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2002-06-16 23:11:33 +0000
commit8153335ec82690e28974fd0bd0f1726d2a1baa3f (patch)
tree939cb8338a9ce35f9b03ff7e0b6168707f897e0f
parent9afb28078732bae4930adae93ed81ac7d899d191 (diff)
downloadipacct-8153335ec82690e28974fd0bd0f1726d2a1baa3f.tar.gz
ipacct-8153335ec82690e28974fd0bd0f1726d2a1baa3f.tar.bz2
Lots of fixes
-rw-r--r--src/account.c52
-rw-r--r--src/config.y153
-rw-r--r--src/ipacct.h13
-rw-r--r--src/main.c28
4 files changed, 152 insertions, 94 deletions
diff --git a/src/account.c b/src/account.c
index 1ee3f08..511d356 100644
--- a/src/account.c
+++ b/src/account.c
@@ -15,7 +15,7 @@
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
-/* $Id: account.c,v 1.2 2001/06/15 04:46:37 gray Exp $ */
+/* $Id: account.c,v 1.3 2002/06/16 23:11:33 gray Exp $ */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
@@ -49,8 +49,10 @@ _stream_free(streamp, unused)
void *unused;
{
stream_t *stream = (stream_t *)streamp;
- list_free(&stream->netlist);
- list_free(&stream->except);
+ list_free(&stream->direct.src);
+ list_free(&stream->direct.dst);
+ list_free(&stream->except.src);
+ list_free(&stream->except.dst);
}
void
@@ -99,20 +101,23 @@ _network_match(networkp, ipdatap)
struct ipdata *ipd = (struct ipdata*)ipdatap;
if (np->addr == (ipd->addr[ipd->dir] & np->netmask)) {
- ipd->match = 1;
+ ipd->match++;
return 1;
}
return 0;
}
int
-lookup_address(list, ipd)
- list_t list;
+lookup_address(flow, ipd)
+ flow_t *flow;
struct ipdata *ipd;
{
ipd->match = 0;
- list_iterate(list, _network_match, ipd);
- return ipd->match;
+ ipd->dir = DIR_SRC;
+ list_iterate(flow->src, _network_match, ipd);
+ ipd->dir = DIR_DST;
+ list_iterate(flow->dst, _network_match, ipd);
+ return ipd->match == 2;
}
int
@@ -128,22 +133,17 @@ _account_check_stream(streamp, ipp)
ipd->addr[DIR_SRC] = ipd->ip->ip_src.s_addr;
ipd->addr[DIR_DST] = ipd->ip->ip_dst.s_addr;
- if (lookup_address(stream->netlist, ipd)
- && !lookup_address(stream->except, ipd)) {
- ipd->stat->bytes.out += ipd->length;
+ if (lookup_address(&stream->direct, ipd)
+ && !lookup_address(&stream->except, ipd)) {
+ /*FIXME: bytes shoud be ctr[2] */
+ if (ipd->dir == DIR_SRC)
+ ipd->stat->bytes.out += ipd->length;
+ else
+ ipd->stat->bytes.in += ipd->length;
match++;
+ if (foreground && verbose)
+ verbose_report(ipd->stat, ipd->ip);
}
-
- ipd->addr[DIR_SRC] = ipd->ip->ip_dst.s_addr;
- ipd->addr[DIR_DST] = ipd->ip->ip_src.s_addr;
- if (lookup_address(stream->netlist, ipd)
- && !lookup_address(stream->except, ipd)) {
- ipd->stat->bytes.in += ipd->length;
- match++;
- }
-
- if (match && foreground && verbose)
- verbose_report(ipd->stat, ipd->ip);
return match;
}
@@ -177,13 +177,14 @@ verbose_report(stat, ip)
{
char buf1[16], buf2[16];
- printf("%s: (%x) %s > (%x) %s %d\n",
+ printf("%s: (%x) %s > (%x) %s %d: %u %u\n",
stat->name,
ip->ip_src.s_addr,
ipaddr2str(buf1, ip->ip_src.s_addr),
ip->ip_dst.s_addr,
ipaddr2str(buf2, ip->ip_dst.s_addr),
- ntohs(ip->ip_len));
+ ntohs(ip->ip_len),
+ stat->bytes.in, stat->bytes.out);
}
int
@@ -325,9 +326,10 @@ test_shell()
nextkn();
ip.ip_dst.s_addr = ipstr2long(tok);
if (opttkn())
- ip.ip_len = htonl(atoi(tok));
+ ip.ip_len = atoi(tok);
else
ip.ip_len = sizeof(ip);
+ ip.ip_len = htons(ip.ip_len);
account(&ip);
break;
case 'q':
diff --git a/src/config.y b/src/config.y
index c4859c8..4575d23 100644
--- a/src/config.y
+++ b/src/config.y
@@ -1,3 +1,4 @@
+%{
/* This file is part of IPACCT
Copyright (c) Sergey Poznyakoff
@@ -14,45 +15,43 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
-
-%{
- #if defined(HAVE_CONFIG_H)
- # include <config.h>
- #endif
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <varargs.h>
- #include <netdb.h>
- #include <syslog.h>
- #include <string.h>
- #include <ipacct.h>
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <varargs.h>
+#include <netdb.h>
+#include <syslog.h>
+#include <string.h>
+#include <ipacct.h>
- #define YYDEBUG 1
- #define PARSER_BUFSIZE 128
+#define YYDEBUG 1
+#define PARSER_BUFSIZE 128
/* Note: currently these should be the same: */
- #define MAX_STR_LEN PARSER_BUFSIZE
-
- #define OPT_EXACT 1
-
- struct keyword {
- char *name;
- int val;
- };
-
- struct keyword options[] = {
- "exact", OPT_EXACT,
- NULL, 0
- };
+#define MAX_STR_LEN PARSER_BUFSIZE
- int get_mask_by_length(int len, IPADDR *);
- int read_address(char **, IPADDR *);
+#define OPT_EXACT 1
+
+struct keyword {
+ char *name;
+ int val;
+};
- static int xlat_keyword(struct keyword *kw, char *str);
- static void yyerrsync();
- static void register_address(char *name, IPADDR addr, UINT4 netmask);
+struct keyword options[] = {
+ "exact", OPT_EXACT,
+ NULL, 0
+};
+
+int get_mask_by_length(int len, IPADDR *);
+int read_address(char **, IPADDR *);
+
+static int xlat_keyword(struct keyword *kw, char *str);
+static void yyerrsync();
+static void register_address(char *name, IPADDR addr, UINT4 netmask);
%}
%union {
@@ -62,13 +61,14 @@
network_t network;
list_t list;
stream_t stream;
+ flow_t flow;
}
%token <number> NUMBER
%token <string> STRING
%token <ipaddr> IPADDRESS
%token STAT HOST NET NETMASK
-%token STREAM IS FROM TO ALL EXCEPT ITSELF
+%token STREAM IS FROM TO ALL EXCEPT ITSELF IN OUT
%token DEFINE
%token SLICE OPTION
%token CHANNEL K_FILE K_SYSLOG UDP
@@ -89,6 +89,7 @@
%type <network> address
%type <list> addr_list
%type <number> flow_dir
+%type <flow> flow_decl
%type <stream> stream_decl
%type <list> stream_decl_list
%%
@@ -272,29 +273,46 @@ stream_decl_list: stream_decl
}
;
-stream_decl: flow_dir addr_list
- {
- $$.dir = $1;
- $$.netlist = $2;
- $$.except = NULL;
- }
- | flow_dir addr_list EXCEPT addr_list
- {
- $$.dir = $1;
- $$.netlist = $2;
- $$.except = $4;
- }
- ;
-
-flow_dir : FROM
- {
- $$ = DIR_SRC;
- }
- | TO
- {
- $$ = DIR_DST;
- }
- ;
+stream_decl: flow_dir flow_decl
+ {
+ $$.dir = $1;
+ $$.direct = $2;
+ $$.except.src = NULL;
+ $$.except.dst = NULL;
+ }
+ | flow_dir flow_decl EXCEPT flow_decl
+ {
+ $$.dir = $1;
+ $$.direct = $2;
+ $$.except = $4;
+ }
+ ;
+
+flow_decl : FROM addr_list TO addr_list
+ {
+ $$.src = $2;
+ $$.dst = $4;
+ }
+ | TO addr_list FROM addr_list
+ {
+ $$.src = $4;
+ $$.dst = $2;
+ }
+ ;
+
+flow_dir : /* empty */
+ {
+ $$ = DIR_SRC;
+ }
+ | IN
+ {
+ $$ = DIR_SRC;
+ }
+ | OUT
+ {
+ $$ = DIR_DST;
+ }
+ ;
addr_list: address
{
@@ -446,6 +464,8 @@ struct keyword keywords[] = {
"but", EXCEPT,
"itself", ITSELF,
"define", DEFINE,
+ "in", IN,
+ "out", OUT,
NULL, 0
};
@@ -610,18 +630,25 @@ register_address(name, addr, netmask)
list_t streamlist = NULL;
stream_t stream;
network_t network;
+ network_t all;
network.addr = addr;
network.netmask = netmask;
+ all.addr = 0;
+ all.netmask = 0;
stream.dir = DIR_SRC;
- stream.netlist = NULL;
- list_alloc(&stream.netlist, &network, sizeof(network));
+ stream.direct.src = NULL;
+ stream.direct.dst = NULL;
+ stream.except.src = NULL;
+ stream.except.dst = NULL;
+ list_alloc(&stream.direct.src, &network, sizeof(network));
list_alloc(&streamlist, &stream, sizeof(stream));
stream.dir = DIR_DST;
- stream.netlist = NULL;
- list_alloc(&stream.netlist, &network, sizeof(network));
+ stream.direct.src = NULL;
+ stream.direct.dst = NULL;
+ list_alloc(&stream.direct.dst, &network, sizeof(network));
list_alloc(&streamlist, &stream, sizeof(stream));
register_stream_list(name, streamlist);
}
diff --git a/src/ipacct.h b/src/ipacct.h
index 646b37f..0a16c93 100644
--- a/src/ipacct.h
+++ b/src/ipacct.h
@@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
-/* $Id: ipacct.h,v 1.3 2001/06/15 04:46:37 gray Exp $ */
+/* $Id: ipacct.h,v 1.4 2002/06/16 23:11:33 gray Exp $ */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -60,10 +60,15 @@ typedef struct network_rec {
#define DIR_SRC 0
#define DIR_DST 1
+typedef struct {
+ list_t src;
+ list_t dst;
+} flow_t;
+
typedef struct stream_rec {
- int dir; /* stream direction */
- list_t netlist;
- list_t except;
+ int dir; /* stream direction */
+ flow_t direct;
+ flow_t except;
} stream_t;
struct counter {
diff --git a/src/main.c b/src/main.c
index 2053e4c..ad71cc9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
-/* $Id: main.c,v 1.3 2001/06/15 04:46:37 gray Exp $ */
+/* $Id: main.c,v 1.4 2002/06/16 23:11:33 gray Exp $ */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
@@ -32,6 +32,7 @@
#include <signal.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
+#include <getopt.h>
#include <pcap.h>
#include <pcap-int.h>
#include <ipacct.h>
@@ -67,6 +68,27 @@ time_t slice_interval = TIME_TO_WAIT;
static pcap_t *pcap;
static char pidfile[MAX_PATH_LENGTH];
+static char short_options[] = "bc:fFhi:l:OpsSt:Tvx";
+static struct option long_options[] = {
+ { "dump-bytecode", no_argument, 0, 'b' },
+ { "config", required_argument, 0, 'c' },
+ { "foreground", no_argument, 0, 'f' },
+ { "file", required_argument, 0, 'F' },
+ { "help", no_argument, 0, 'h' },
+ { "interface", required_argument, 0, 'i' },
+ { "log-file", required_argument, 0, 'l' },
+ { "optimize", no_argument, 0, 'O' },
+ { "no-promiscuous-mode", no_argument, 0, 'p' },
+ { "single-process", no_argument, 0, 's' },
+ { "syntax-check", no_argument, 0, 'S' },
+ { "timeslice", required_argument, 0, 't' },
+ { "test-shell", no_argument, 0, 'T' },
+ { "verbose", no_argument, 0, 'v' },
+ { "fixed-clocks", no_argument, 0, 'x' },
+ { NULL, 0, 0, 0 },
+};
+
+
int
main(argc, argv)
int argc;
@@ -83,7 +105,8 @@ main(argc, argv)
progname = argv[0];
- while ((c = getopt(argc, argv, "bc:fFhi:l:OpsSt:Tvx")) != EOF) {
+ while ((c = getopt_long(argc, argv, short_options, long_options,
+ &type)) != EOF) {
switch (c) {
case 'b':
dump_bytecode++;
@@ -125,6 +148,7 @@ main(argc, argv)
break;
case 'T':
test_mode++;
+ foreground++;
break;
case 'v':
verbose++;

Return to:

Send suggestions and report system problems to the System administrator.