File: tracker.c

package info (click to toggle)
tcpick 0.2.1-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,148 kB
  • sloc: ansic: 2,557; sh: 931; makefile: 16
file content (157 lines) | stat: -rw-r--r-- 3,891 bytes parent folder | download | duplicates (7)
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
/*
 * tracker.c -- connection status handler
 * Part of the tcpick project
 *
 * Author: Francesco Stablum <duskdruid @ despammed.com>
 *
 * Copyright (C) 2003, 2004  Francesco Stablum
 * Licensed under the GPL
 *
 */

/*
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "tcpick.h"
#include "extern.h"

struct CONN * first_conn = NULL; /*all connections, ends on a 0*/
struct CONN * last_conn  = NULL; 

int 
status_switch(struct CONN * prev, enum STATUS status) 
{
#define CURR (prev->next)
	display_status( stdout, CURR, status );
	if ( status == CLOSED || 
	     status == RESET  || 
	     status == EXPIRED )
		rmconn( prev );
	else
		CURR->status = status;
}

int 
newconn( struct CONN * prev_ring )
/* allocates a new connection */
{
	struct CONN * conn;

	if( flags.trackonly != -1 ) {
		if( flags.trackonly_first &&
		    ( count_conns >= flags.trackonly ) )
			return -1;
		else if( count_opened >= flags.trackonly )
			return -1;
		else
			count_opened++;
	}
	count_conns++;

	conn = ( struct CONN * )S_calloc( sizeof( struct CONN ), 1 );
	
	prev_ring->next = conn; /* and the chain becomes longer... :)
				   thank you mainman for this good idea! */
	last_conn = conn;

	conn->client.side = CLIENT;
	conn->server.side = SERVER;

	conn->client.ip.s_addr = ippacket->ip_src.s_addr;
	conn->server.ip.s_addr = ippacket->ip_dst.s_addr;
	conn->client.port      = tcppacket->source;
	conn->server.port      = tcppacket->dest;
	
	conn->client.sip  = ntohl(tcppacket->seq) + 1;
	
	conn->client.oth = & (conn->server);
	conn->server.oth = & (conn->client);
	
	conn->num = count_conns;

	conn->lasttime = time(NULL);
	
	status_switch( prev_ring, SYN_SENT );
}

int rmconn( struct CONN * prev_ring )
/* removes from the linked-list and deallocates a connection
 * prev_ring 
 */
{
	struct CONN * curr = prev_ring->next;
	struct CONN * conn;
	
	prev_ring->next = curr->next;
	if( curr->next == NULL )
		last_conn = prev_ring;
	
	free_desc( &(curr->client), 1);
	if (flags.writer.type == UNIQUE) free_desc( &(curr->server), 0);
	else free_desc( &(curr->server), 1);
	S_free( curr );

	conn = first_conn;
	
	if( flags.exitclosed ) {
		if( flags.exitclosed_first ) { /* -Ef */
			while( conn->num > flags.exitclosed ) {
				if(! (conn->next) )
					exit( EXIT_SUCCESS ); 
				conn = conn->next;
			}
		} else { /* -E */
			--flags.exitclosed;
			if( flags.exitclosed == 0)
				exit( EXIT_SUCCESS ); 
		}
	}

	if( flags.trackonly &&
	    (! flags.trackonly_first) )
		count_opened--;
}

int free_desc( struct HOST_DESC * desc, int freedescfilename )
/* frees the host descriptor and closes the file */
{
	struct FRAGMENT * tmp;

	if( desc->file ) {
		fclose( desc->file );
		if (flags.writer.type == UNIQUE)
			desc->oth->file = NULL;
	}
	if( desc->filename && freedescfilename ) {
		S_free( desc->filename );
		desc->filename = NULL;
	}

	if( desc->lockfilename ) {
		unlink( desc->lockfilename );
		S_free( desc->lockfilename );
		desc->lockfilename = NULL;
	}
	
	while( desc->unack ) { /* FIXME: desc->unack should be just NULL when closes! 
				* I should check it! */
		S_free( desc->unack->payload );
		desc->unack->payload = NULL;
		tmp = desc->unack;
		desc->unack = desc->unack->next;
		S_free( tmp );
	}
}