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
25 static void freeGenericFilterData(struct __jbase_stream
*stream
) {
26 g_free(stream
->filterData
);
29 /* BEGIN: filter HTTP func */
30 struct ___httpFilterData
{
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
;
39 if (direction
== fd
->direction
)
41 data
= pi
[fd
->protocol
].data
;
42 len
= pi
[fd
->protocol
].len
;
45 if (!strncmp(data
, "GET ", 4) || !strncmp(data
, "POST ", 5) || !strncmp(data
, "HEAD ", 5)) {
46 const gchar
*space1
, *space2
;
48 space1
= strchr(data
, ' ') + 1;
51 for (i
=0; i
<len
&& *space2
!= ' '; i
++, space2
++);
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
{
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
;
82 if (direction
== fd
->direction
)
84 data
= pi
[fd
->protocol
].data
;
85 len
= pi
[fd
->protocol
].len
;
88 if (!g_strncasecmp(data
, "MAIL FROM: ", 11)) {
89 const gchar
*space1
, *space2
;
94 for (i
=0; i
<len
&& *space2
!= '\r' && *space2
!= '\n'; i
++, space2
++);
96 int l
= space2
-space1
;
97 if (l
+1>sizeof(fd
->from
))
98 l
= sizeof(fd
->from
)-1;
99 memcpy(fd
->from
, space1
, l
);
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
;
108 len
-= space1
- data
;
110 for (i
=0; i
<len
&& *space2
!= '\r' && *space2
!= '\n'; i
++, space2
++);
112 int l
= space2
-space1
;
113 if (l
+1>sizeof(fd
->to
))
114 l
= sizeof(fd
->to
)-1;
115 memcpy(fd
->to
, space1
, l
);
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); \
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
);