File: conversations.c

package info (click to toggle)
etherape 0.9.12-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,612 kB
  • sloc: ansic: 11,042; sh: 10,656; xml: 1,290; makefile: 208; sed: 16
file content (198 lines) | stat: -rw-r--r-- 5,589 bytes parent folder | download | duplicates (2)
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
/* EtherApe
 * Copyright (C) 2001 Juan Toledo
 * $Id$
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <netinet/in.h>
#include "appdata.h"
#include "conversations.h"
#include "dns.h"
#include "util.h"

/* Some protocols add an item here to help identify further packets of the 
 * same protocol */
static GList *conversations = NULL;
static long n_conversations = 0;

long active_conversations(void)
{
  return n_conversations;
}

/* Returns the item ptr if there is any matching conversation in any of the
 * two directions */
/* A zero in any of the ports matches any port number */
static GList
  * find_conversation_ptr(address_t * src_address, address_t * dst_address,
		       guint16 src_port, guint16 dst_port)
{
  GList *item = conversations;
  conversation_t *conv = NULL;

  g_assert(src_address);
  g_assert(dst_address);
  while (item)
    {
      conv = item->data;
      if ((is_addr_eq(src_address, &conv->src_address)
	   && is_addr_eq(dst_address, &conv->dst_address)
	   && (!src_port || !conv->src_port || (src_port == conv->src_port))
	   && (!dst_port || !conv->dst_port || (dst_port == conv->dst_port))
          )
	  || (is_addr_eq(src_address, &conv->dst_address)
	      && is_addr_eq(dst_address, &conv->src_address)
	      && (!src_port || !conv->dst_port || (src_port == conv->dst_port)) 
              && (!dst_port || !conv->src_port || (dst_port == conv->src_port))
              )
          )
	{
          /* found */
	  break;
	}
      item = item->next;
    }

  return item;
}				/* find_conversation_ptr */


void
add_conversation (address_t * src_address, address_t * dst_address,
		  guint16 src_port, guint16 dst_port, const gchar * data)
{
  conversation_t *conv = NULL;
  const gchar *old_data = NULL;

  if (!src_address || !dst_address)
    {
      g_my_critical("NULL ptr in add_conversation");
      return;
    }

  /* Make sure there is not one such conversation */
  if ((old_data = find_conversation (src_address, dst_address,
				     src_port, dst_port)))
    {
      if (!strcmp (old_data, data))
	g_my_critical
	  ("Conflicting conversations %s:%d-%s:%d in add_conversation",
	   address_to_str (src_address), src_port,
	   address_to_str (dst_address), dst_port);
      else
	g_my_debug
	  ("Conversation %s:%d-%s:%d %s already exists in add_conversation",
	   address_to_str (src_address), src_port,
	   address_to_str (dst_address), dst_port, data);
      return;
    }

  g_my_debug ("Adding new conversation %s:%d-%s:%d %s",
	      address_to_str (src_address), src_port,
	      address_to_str (dst_address), dst_port, data);

  conv = g_malloc (sizeof (conversation_t));
  g_assert(conv);
  
  address_copy(&conv->src_address, src_address);
  address_copy(&conv->dst_address, dst_address);
  conv->src_port = src_port;
  conv->dst_port = dst_port;
  conv->data = g_strdup (data);

  conversations = g_list_prepend (conversations, conv);
  n_conversations++;
}				/* add_conversation */


/* Returns the data if there is any matching conversation in any of the
 * two directions */
/* A zero in any of the ports matches any port number */
const gchar* 
find_conversation (address_t * src_address, address_t * dst_address,
		      guint16 src_port, guint16 dst_port)
{
  GList *item;

  if (!src_address || !dst_address)
    {
      g_my_critical("NULL ptr in find_conversation");
      return NULL;
    }

  item = find_conversation_ptr(src_address, dst_address, src_port, dst_port);
  if (item)
    {
      /* found */
      conversation_t *conv = item->data;
      return conv->data;
    }
  else
    return NULL;
}

/* removes all conversations with the specified addresses */
void
delete_conversation_link(address_t * src_address, address_t * dst_address)
{
  GList *item;

  if (!src_address || !dst_address)
    {
      g_my_critical("NULL ptr in delete_conversation_link");
      return;
    }

  while ( (item = find_conversation_ptr(src_address, dst_address, 0, 0)) )
    {
      conversation_t *conv = NULL;
      conv = item->data;
      g_my_debug ("Removing conversation %s:%d-%s:%d %s",
		  address_to_str (&conv->src_address), conv->src_port,
		  address_to_str (&conv->dst_address), conv->dst_port, conv->data);
      g_free (conv->data);
      g_free (conv);
      conversations = g_list_delete_link(conversations, item);
      n_conversations--;
    }
}

void
delete_conversations (void)
{
  GList *item = conversations;
  conversation_t *conv = NULL;

  while (item)
    {
      conv = item->data;
      g_my_debug ("Removing conversation %s:%d-%s:%d %s",
		  address_to_str (&conv->src_address), conv->src_port,
		  address_to_str (&conv->dst_address), conv->dst_port, conv->data);
      g_free (conv->data);
      g_free (conv);
      item = item->next;
      n_conversations--;
    }

  g_list_free (conversations);
  conversations = NULL;
}				/* delete_conversations */