setting svn:exports
[jnettop.git] / jnettop / jfilter.c
blob08c9eb69639afa764cd4995388cd691cbdc5ea8c
1 /*
2 * jnettop, network online traffic visualiser
3 * Copyright (C) 2002-2005 Jakub Skopal
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * $Header$
23 #include "jbase.h"
25 static void freeGenericFilterData(struct __jbase_stream *stream) {
26 g_free(stream->filterData);
29 /* BEGIN: filter HTTP func */
30 struct ___httpFilterData {
31 guint protocol;
32 gboolean direction;
35 static void filterHTTPFunc(struct __jbase_stream *stream, const struct __jbase_packet *packet, gboolean direction, const struct __jbase_payload_info *pi) {
36 struct ___httpFilterData *fd = (struct ___httpFilterData *)stream->filterData;
37 const gchar *data;
38 guint len;
39 if (direction == fd->direction)
40 return;
41 data = pi[fd->protocol].data;
42 len = pi[fd->protocol].len;
43 if (!data || len<0)
44 return;
45 if (!strncmp(data, "GET ", 4) || !strncmp(data, "POST ", 5) || !strncmp(data, "HEAD ", 5)) {
46 const gchar *space1, *space2;
47 int i;
48 space1 = strchr(data, ' ') + 1;
49 len -= space1 - data;
50 space2 = space1;
51 for (i=0; i<len && *space2 != ' '; i++, space2++);
52 if (i<len) {
53 gchar url[BUFSIZ];
54 memcpy(url, data, space2-data);
55 url[space2-data] = '\0';
56 SET_FILTER_DATA_STRING(stream, url);
61 static void assignHTTPFilter(jbase_stream *stream, gboolean direction) {
62 struct ___httpFilterData *fd;
63 stream->filterDataFunc = filterHTTPFunc;
64 stream->filterData = (guchar*)(fd = g_new0(struct ___httpFilterData, 1));
65 fd->direction = direction;
66 fd->protocol = stream->proto;
67 stream->filterDataFreeFunc = freeGenericFilterData;
69 /* END: filter HTTP func */
71 /* BEGIN: filter SMTP func */
72 struct ___smtpFilterData {
73 guint protocol;
74 gboolean direction;
75 gchar from[512], to[512];
78 static void filterSMTPFunc(struct __jbase_stream *stream, const struct __jbase_packet *packet, gboolean direction, const struct __jbase_payload_info *pi) {
79 struct ___smtpFilterData *fd = (struct ___smtpFilterData *)stream->filterData;
80 const gchar *data;
81 guint len;
82 if (direction == fd->direction)
83 return;
84 data = pi[fd->protocol].data;
85 len = pi[fd->protocol].len;
86 if (!data || len<0)
87 return;
88 if (!g_strncasecmp(data, "MAIL FROM: ", 11)) {
89 const gchar *space1, *space2;
90 int i;
91 space1 = data + 11;
92 len -= space1 - data;
93 space2 = space1;
94 for (i=0; i<len && *space2 != '\r' && *space2 != '\n'; i++, space2++);
95 if (i<len) {
96 int l = space2-space1;
97 if (l+1>sizeof(fd->from))
98 l = sizeof(fd->from)-1;
99 memcpy(fd->from, space1, l);
100 fd->from[l] = '\0';
101 fd->to[0] = '\0';
102 SET_FILTER_DATA_STRING(stream, fd->from);
104 } else if (!fd->to[0] && !g_strncasecmp(data, "RCPT TO: ", 9)) {
105 const gchar *space1, *space2;
106 int i;
107 space1 = data + 9;
108 len -= space1 - data;
109 space2 = space1;
110 for (i=0; i<len && *space2 != '\r' && *space2 != '\n'; i++, space2++);
111 if (i<len) {
112 int l = space2-space1;
113 if (l+1>sizeof(fd->to))
114 l = sizeof(fd->to)-1;
115 memcpy(fd->to, space1, l);
116 fd->to[l] = '\0';
117 SET_FILTER_DATA_STRING_2(stream, "%s -> %s", fd->from, fd->to);
122 static void assignSMTPFilter(jbase_stream *stream, gboolean direction) {
123 struct ___smtpFilterData *fd;
124 stream->filterDataFunc = filterSMTPFunc;
125 stream->filterData = (guchar*) (fd = g_new0(struct ___smtpFilterData, 1));
126 fd->direction = direction;
127 fd->protocol = stream->proto;
128 stream->filterDataFreeFunc = freeGenericFilterData;
131 #define IF_TCP_PORT_THEN_ASSIGN(port, assignFunc) \
132 if ((stream->proto == JBASE_PROTO_TCP || stream->proto == JBASE_PROTO_TCP6) \
133 && (stream->srcport == port || stream->dstport == port)) { \
134 assignFunc(stream, stream->dstport == port); \
135 return; \
138 void jfilter_AssignDataFilter(jbase_stream *stream) {
139 IF_TCP_PORT_THEN_ASSIGN(80, assignHTTPFilter);
140 IF_TCP_PORT_THEN_ASSIGN(8080, assignHTTPFilter);
141 IF_TCP_PORT_THEN_ASSIGN(3128, assignHTTPFilter);
142 IF_TCP_PORT_THEN_ASSIGN(25, assignSMTPFilter);