Skip to content

Commit

Permalink
""
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Lightfoot committed Aug 25, 2003
1 parent 968a9e3 commit 7e58633
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 360 deletions.
6 changes: 5 additions & 1 deletion CREDITS
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Credits and contributors for driftnet
$Id: CREDITS,v 1.7 2003/08/12 14:01:58 chris Exp $
$Id: CREDITS,v 1.8 2003/08/25 12:23:43 chris Exp $

Thanks to the authors of EtherPEG, for a cool idea, and to Eric Richardson,
whose website brought it to my attention.
Expand All @@ -12,6 +12,10 @@ Jamie Zawinski for helpful suggestions and webcollage integration.
Eric Dobbs for fixing some portability issues and getting driftnet running on
Solaris and BSD systems.

Drew Roedersheimer for contributing PNG support.

Joshua Wright for some 802.11 fixes.

Rob Timko and Joshua Wright for getting Driftnet working on wireless networks
and reading decrypted data from Kismet.

275 changes: 4 additions & 271 deletions Makefile

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions connection.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/*
* connection.c:
* Connection objects.
*
* Copyright (c) 2002 Chris Lightfoot. All rights reserved.
* Email: chris@ex-parrot.com; WWW: http://www.ex-parrot.com/~chris/
*
*/

static const char rcsid[] = "$Id: connection.c,v 1.4 2002/11/16 18:24:27 chris Exp $";
static const char rcsid[] = "$Id: connection.c,v 1.5 2003/08/25 12:23:43 chris Exp $";

#include <sys/types.h>

#include <assert.h>
#include <stdio.h>
Expand All @@ -19,13 +22,14 @@ static const char rcsid[] = "$Id: connection.c,v 1.4 2002/11/16 18:24:27 chris E
/* connection_new:
* Allocate a new connection structure between the given addresses. */
connection connection_new(const struct in_addr *src, const struct in_addr *dst, const short int sport, const short int dport) {
connection c = (connection)calloc(1, sizeof(struct _connection));
connection c;
alloc_struct(_connection, c);
c->src = *src;
c->dst = *dst;
c->sport = sport;
c->dport = dport;
c->alloc = 16384;
c->data = c->gif = c->jpeg = c->mpeg = malloc(c->alloc);
c->data = xmalloc(c->alloc);
c->last = time(NULL);
c->blocks = NULL;
return c;
Expand All @@ -48,27 +52,23 @@ void connection_delete(connection c) {
/* connection_push:
* Put some more data in a connection. */
void connection_push(connection c, const unsigned char *data, unsigned int off, unsigned int len) {
size_t goff = c->gif - c->data, joff = c->jpeg - c->data, moff = c->mpeg - c->data;
struct datablock *B, *b, *bl, BZ = {0};
int a;

assert(c->alloc > 0);
if (off + len > c->alloc) {
/* Allocate more memory. */
while (off + len > c->alloc) {
while (off + len > c->alloc)
c->alloc *= 2;
c->data = (unsigned char*)realloc(c->data, c->alloc);
}
c->data = (unsigned char*)xrealloc(c->data, c->alloc);
}
c->gif = c->data + goff;
c->jpeg = c->data + joff;
c->mpeg = c->data + moff;

memcpy(c->data + off, data, len);

if (off + len > c->len) c->len = off + len;
c->last = time(NULL);

B = malloc(sizeof *B);
B = xmalloc(sizeof *B);
*B = BZ;
B->off = off;
B->len = len;
Expand Down
32 changes: 18 additions & 14 deletions display.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@

#ifndef NO_DISPLAY_WINDOW

static const char rcsid[] = "$Id: display.c,v 1.17 2003/06/13 15:51:44 chris Exp $";
static const char rcsid[] = "$Id: display.c,v 1.18 2003/08/25 12:23:43 chris Exp $";

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include <sys/stat.h>

Expand Down Expand Up @@ -80,7 +84,7 @@ void make_backing_image() {
/* Possible it has scrolled off the window. */
if (ir->x > width || ir->y + ir->h < 0) {
unlink(ir->filename);
free(ir->filename);
xfree(ir->filename);
memset(ir, 0, sizeof *ir);
}
}
Expand Down Expand Up @@ -128,7 +132,7 @@ void scroll_backing_image(const int dy) {
/* scrolled off bottom, no longer in use. */
if ((ir->y + ir->h) < 0) {
unlink(ir->filename);
free(ir->filename);
xfree(ir->filename);
memset(ir, 0, sizeof *ir);
}
}
Expand All @@ -145,7 +149,7 @@ void add_image_rectangle(const char *filename, const int x, const int y, const i
break;
}
if (ir == imgrects + nimgrects) {
imgrects = realloc(imgrects, 2 * nimgrects * sizeof *imgrects);
imgrects = xrealloc(imgrects, 2 * nimgrects * sizeof *imgrects);
memset(imgrects + nimgrects, 0, nimgrects * sizeof *imgrects);
ir = imgrects + nimgrects;
nimgrects *= 2;
Expand Down Expand Up @@ -205,7 +209,7 @@ void save_image(struct imgrect *ir) {
struct stat st;

if (!name)
name = calloc(strlen(savedimgpfx) + 16, 1);
name = xcalloc(strlen(savedimgpfx) + 16, 1);

do
sprintf(name, "%s%d%s", savedimgpfx, num++, strrchr(ir->filename, '.'));
Expand Down Expand Up @@ -301,7 +305,7 @@ gboolean pipe_event(GIOChannel chan, GIOCondition cond, gpointer data) {
int nimgs = 0;

if (!path)
path = malloc(strlen(tmpdir) + 34);
path = xmalloc(strlen(tmpdir) + 34);

/* We are sent messages of size TMPNAMELEN containing a null-terminated
* file name. */
Expand All @@ -319,7 +323,7 @@ gboolean pipe_event(GIOChannel chan, GIOCondition cond, gpointer data) {
if (verbose)
fprintf(stderr, PROGNAME": received image %s of size %d\n", name, (int)st.st_size);
/* Check to see whether this looks like an image we're interested in. */
if (st.st_size > 256) {
if (st.st_size > 100) {
/* Small images are probably bollocks. */
img i = img_new();
if (!img_load_file(i, path, header, unknown))
Expand Down Expand Up @@ -386,7 +390,7 @@ int dodisplay(int argc, char *argv[]) {
fcntl(dpychld_fd, F_SETFL, O_NONBLOCK);

/* set up list of image rectangles. */
imgrects = calloc(nimgrects = 16, sizeof *imgrects);
imgrects = xcalloc(nimgrects = 16, sizeof *imgrects);

/* do some init thing */
gtk_init(&argc, &argv);
Expand Down
31 changes: 19 additions & 12 deletions driftnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

static const char rcsid[] = "$Id: driftnet.c,v 1.30 2003/08/12 14:01:58 chris Exp $";
static const char rcsid[] = "$Id: driftnet.c,v 1.31 2003/08/25 12:23:43 chris Exp $";

#undef NDEBUG

Expand Down Expand Up @@ -86,22 +86,22 @@ void clean_temporary_directory(void) {
char *buf;
size_t buflen;

buf = malloc(buflen = strlen(tmpdir) + 64);
buf = xmalloc(buflen = strlen(tmpdir) + 64);

while ((de = readdir(d))) {
char *p;
p = strrchr(de->d_name, '.');
if (!tmpdir_specified || (p && strncmp(de->d_name, "driftnet-", 9) == 0 && (strcmp(p, ".jpeg") == 0 || strcmp(p, ".gif") == 0 || strcmp(p, ".mp3") == 0))) {
if (buflen < strlen(tmpdir) + strlen(de->d_name) + 1)
buf = realloc(buf, buflen = strlen(tmpdir) + strlen(de->d_name) + 64);
buf = xrealloc(buf, buflen = strlen(tmpdir) + strlen(de->d_name) + 64);

sprintf(buf, "%s/%s", tmpdir, de->d_name);
unlink(buf);
}
}
closedir(d);

free(buf);
xfree(buf);
}


Expand All @@ -117,7 +117,7 @@ connection *alloc_connection(void) {
if (!*C) return C;
}
/* No connection slots left. */
slots = (connection*)realloc(slots, slotsalloc * 2 * sizeof(connection));
slots = (connection*)xrealloc(slots, slotsalloc * 2 * sizeof(connection));
memset(slots + slotsalloc, 0, slotsalloc * sizeof(connection));
C = slots + slotsalloc;
slotsalloc *= 2;
Expand Down Expand Up @@ -213,10 +213,17 @@ int get_link_level_hdr_length(int type)

case DLT_IEEE802:
return 22;


#ifdef DLT_ATM_RFC1483
case DLT_ATM_RFC1483:
return 8;
#endif

#ifdef DLT_PRISM_HEADER
case DLT_PRISM_HEADER:
return 32;
#endif

case DLT_RAW:
return 0;

Expand Down Expand Up @@ -466,7 +473,7 @@ void *packet_capture_thread(void *v) {
/* main:
* Entry point. Process command line options, start up pcap and enter capture
* loop. */
char optstring[] = "hi:psSMvam:d:x:bf:";
char optstring[] = "abd:f:hi:M:m:pSsvx:";

int main(int argc, char *argv[]) {
char *interface = NULL, *filterexpr;
Expand Down Expand Up @@ -618,10 +625,10 @@ int main(int argc, char *argv[]) {
} else {
/* need to make a temporary directory. */
for (;;) {
tmpdir = strdup(tmpnam(NULL));
tmpdir = strdup(tmpnam(NULL)); /* may generate a warning, but this is safe because we create a directory not a file */
if (mkdir(tmpdir, 0700) == 0)
break;
free(tmpdir);
xfree(tmpdir);
}
}

Expand Down Expand Up @@ -736,7 +743,7 @@ int main(int argc, char *argv[]) {

slotsused = 0;
slotsalloc = 64;
slots = (connection*)calloc(slotsalloc, sizeof(connection));
slots = (connection*)xcalloc(slotsalloc, sizeof(connection));

/* Actually start the capture stuff up. Unfortunately, on many platforms,
* libpcap doesn't have read timeouts, so we start the thing up in a
Expand Down Expand Up @@ -774,8 +781,8 @@ int main(int argc, char *argv[]) {
/* Easier for memory-leak debugging if we deallocate all this here.... */
for (C = slots; C < slots + slotsalloc; ++C)
if (*C) connection_delete(*C);
free(slots);
free(tmpdir);
xfree(slots);
xfree(tmpdir);

return 0;
}
40 changes: 33 additions & 7 deletions driftnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) 2001 Chris Lightfoot. All rights reserved.
* Email: chris@ex-parrot.com; WWW: http://www.ex-parrot.com/~chris/
*
* $Id: driftnet.h,v 1.9 2002/06/13 20:06:42 chris Exp $
* $Id: driftnet.h,v 1.10 2003/08/25 12:23:43 chris Exp $
*
*/

Expand All @@ -14,18 +14,24 @@

#define PROGNAME "driftnet"

#include <sys/types.h> /* added 20020604 edobbs for OpenBSD */
#include <sys/types.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <stdio.h>

/* alloc_struct S P
* Make P point to a new struct S, initialised as if in static storage (like
* = {0}). */
#define alloc_struct(S, p) do { struct S as__z = {0}; p = xmalloc(sizeof *p); *p = as__z; } while (0)

/* enum mediatype:
* Characterise types of media which we can extract. */
enum mediatype { m_image = 1, m_audio = 2 };
enum mediatype { m_image = 1, m_audio = 2, m_text = 3 };

#define NMEDIATYPES 3 /* keep up to date with media.c */
#define NMEDIATYPES 5 /* keep up to date with media.c */

/* struct datablock:
* Represents an extent in a captured stream. */
Expand All @@ -35,15 +41,27 @@ struct datablock {
};

/* connection:
* Object representing one half of a TCP stream connection. */
* Object representing one half of a TCP stream connection. Each connection
* maintains a record of the data which has been recovered from the network
* and a list of blocks of data which represent valid data in the buffer, so
* that if there is a gap in the received data, we don't search it for
* data. */
typedef struct _connection {
/* Source/destination address/port of this half-duplex connection. */
struct in_addr src, dst;
short int sport, dport;
/* The TCP initial-sequence-number of the connection. */
uint32_t isn;
unsigned int len, off, alloc;
unsigned char *data, *gif, *jpeg, *mpeg;
/* The highest offset and the buffer size allocated, and the buffer
* itself. */
unsigned int len, alloc;
unsigned char *data;
/* Flag indicating that we've seen a FIN-flagged segment for this stream,
* so that it is undergoing a shutdown. */
int fin;
/* The time at which we last received any data on this stream. */
time_t last;
/* A list of the extents in the buffer which contain valid data. */
struct datablock *blocks;
} *connection;

Expand All @@ -61,6 +79,14 @@ connection *find_connection(const struct in_addr *src, const struct in_addr *dst
/* media.c */
void connection_extract_media(connection c, const enum mediatype T);

/* util.c */
void *xmalloc(size_t n);
void *xcalloc(size_t n, size_t m);
void *xrealloc(void *w, size_t n);
void xfree(void *v);
char *xstrdup(const char *s);
unsigned char *memstr(const unsigned char *haystack, const size_t hlen, const unsigned char *needle, const size_t nlen);

#define TMPNAMELEN 64

#endif /* __DRIFTNET_H_ */
Loading

0 comments on commit 7e58633

Please sign in to comment.