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
|
/*
* record.c
*
* Copyright (c) 2000 Dug Song <dugsong@monkey.org>
*
* $Id: record.c,v 1.10 2001/03/15 08:33:04 dugsong Exp $
*/
#include "config.h"
#include <sys/types.h>
#include <netinet/in.h>
#include <rpc/rpc.h>
#include <stdio.h>
#include <time.h>
#include <md5.h>
#ifdef HAVE_DB_185_H
#define DB_LIBRARY_COMPATIBILITY_API
#include <db_185.h>
#elif HAVE_DB_H
#include <db.h>
#endif
#include <libnet.h>
#include "options.h"
#include "record.h"
struct rec {
time_t time;
in_addr_t src;
in_addr_t dst;
u_int proto;
u_short sport;
u_short dport;
struct netobj name;
struct netobj data;
};
static DB *db;
static int
xdr_rec(XDR *xdrs, struct rec *rec)
{
if (xdr_u_long(xdrs, (u_long *)&rec->time) &&
xdr_u_long(xdrs, (u_long *)&rec->src) &&
xdr_u_long(xdrs, (u_long *)&rec->dst) &&
xdr_u_int(xdrs, &rec->proto) &&
xdr_u_short(xdrs, &rec->sport) &&
xdr_u_short(xdrs, &rec->dport) &&
xdr_netobj(xdrs, &rec->name) &&
xdr_netobj(xdrs, &rec->data)) {
return (1);
}
return (0);
}
static void
record_print(struct rec *rec)
{
struct tm *tm;
char *srcp, *dstp, *protop, tstr[24], spstr[8], dpstr[8];
struct protoent *pr;
tm = localtime(&rec->time);
strftime(tstr, sizeof(tstr), "%x %X", tm);
srcp = libnet_addr2name4(rec->src, Opt_dns);
dstp = libnet_addr2name4(rec->dst, Opt_dns);
if ((pr = getprotobynumber(rec->proto)) == NULL)
protop = "unknown";
else
protop = pr->p_name;
snprintf(spstr, sizeof(spstr), "%d", rec->sport);
snprintf(dpstr, sizeof(dpstr), "%d", rec->dport);
printf("-----------------\n");
printf("%s %s %s%s%s -> %s%s%s (%.*s)\n",
tstr, protop,
srcp, rec->sport ? "." : "", rec->sport ? spstr : "",
dstp, rec->dport ? "." : "", rec->dport ? dpstr : "",
(int) rec->name.n_len, rec->name.n_bytes);
fwrite(rec->data.n_bytes, 1, rec->data.n_len, stdout);
printf("\n");
fflush(stdout);
}
static DBT *
record_hash(struct rec *rec)
{
static DBT key;
static u_char hash[16];
MD5_CTX ctx;
/* Unique key: src/dst IPs, decode type, decode data. */
MD5Init(&ctx);
MD5Update(&ctx, (u_char *) &rec->src, sizeof(rec->src));
MD5Update(&ctx, (u_char *) &rec->dst, sizeof(rec->dst));
MD5Update(&ctx, rec->name.n_bytes, rec->name.n_len);
MD5Update(&ctx, rec->data.n_bytes, rec->data.n_len);
MD5Final(hash, &ctx);
key.data = hash;
key.size = sizeof(hash);
return (&key);
}
static int
record_save(struct rec *rec)
{
DBT *key, data;
XDR xdrs;
u_char buf[2048];
xdrmem_create(&xdrs, buf, sizeof(buf), XDR_ENCODE);
if (!xdr_rec(&xdrs, rec))
return (0);
data.data = buf;
data.size = xdr_getpos(&xdrs);
xdr_destroy(&xdrs);
key = record_hash(rec);
if (db->put(db, key, &data, R_NOOVERWRITE) == 0)
db->sync(db, 0);
return (1);
}
void
record_dump(void)
{
DBT key, data;
XDR xdrs;
struct rec rec;
while (db->seq(db, &key, &data, R_NEXT) == 0) {
memset(&rec, 0, sizeof(rec));
xdrmem_create(&xdrs, data.data, data.size, XDR_DECODE);
if (xdr_rec(&xdrs, &rec)) {
record_print(&rec);
}
xdr_destroy(&xdrs);
}
}
int
record_init(char *file)
{
int flags, mode;
if (Opt_read) {
flags = O_RDONLY;
mode = 0;
}
else {
flags = O_RDWR|O_CREAT;
mode = S_IRUSR|S_IWUSR;
}
if ((db = dbopen(file, flags, mode, DB_BTREE, NULL)) == NULL)
return (0);
return (1);
}
int
record(in_addr_t src, in_addr_t dst, int proto, u_short sport, u_short dport,
char *name, u_char *buf, int len)
{
struct rec rec;
rec.time = time(NULL);
rec.src = src;
rec.dst = dst;
rec.proto = proto;
rec.sport = sport;
rec.dport = dport;
rec.name.n_bytes = name;
rec.name.n_len = strlen(name);
rec.data.n_bytes = buf;
rec.data.n_len = len;
if (!Opt_read && !Opt_write)
record_print(&rec);
record_save(&rec);
return (1);
}
void
record_close(void)
{
db->close(db);
}
|