abw_common.c 6.82 KB
Newer Older
's avatar
committed
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
/*
 * Copyright (c) 2006, CESNET
 * All rights reserved.
 *
 * LICENSE TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the company nor the names of its contributors 
 *       may be used to endorse or promote products derived from this 
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COMPANY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 *
 */

's avatar
committed
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
's avatar
committed
37
#include <unistd.h>
's avatar
committed
38
39
#include <netdb.h>
extern int h_errno;		/* man gethostbyname */
's avatar
committed
40

41
#include "abw_common.h"
's avatar
committed
42
43
#include "abw_conf.h"

44
45
extern int debug;

46
47
48
49
50
51
52
53
tracked_protocol_t tracked_protocols[] = {
	{ "ftp", "TRACK_FTP" },
	{ "gnutella", "TRACK_GNUTELLA" },
	{ "edonkey", "TRACK_EDONKEY" },
	{ "torrent", "TRACK_TORRENT" },
	{ "dc", "TRACK_DC" },
	{ NULL, NULL },
};
's avatar
committed
54

55
56
subject_t *new_subject(void) {
	subject_t *p;
's avatar
committed
57

58
	if ((p=malloc(sizeof(subject_t)))==NULL) {
's avatar
committed
59
60
61
62
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return NULL;
	}

63
	memset(p, 0, sizeof(subject_t));
's avatar
committed
64

65
	p->port=-1;
66
67
	p->mpls=0;
	p->link_mbps=DEFAULT_LINK_MBPS;
68
	p->out=0;
's avatar
committed
69
70

	return p;
71
} /* new_subject() */
's avatar
committed
72

73
74
scope_t *new_scope(void) {
	scope_t *p;
's avatar
committed
75

76
	if ((p=malloc(sizeof(scope_t)))==NULL) {
's avatar
committed
77
78
79
80
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return NULL;
	}

81
	memset(p, 0, sizeof(scope_t));
's avatar
committed
82

's avatar
committed
83
	return p;
84
} /* new_scope() */
's avatar
committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

parameters_t *new_parameters(void) {
	parameters_t *p;

	if ((p=malloc(sizeof(parameters_t)))==NULL) {
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return NULL;
	}

	memset(p, 0, sizeof(parameters_t));

	p->sau_mode='d';
	p->sau_threshold=1;
	p->interval.tv_sec=1;
	p->interval.tv_usec=0;

	return p;
} /* new_parameters() */

104
105
measurement_t *new_measurement(void) {
	measurement_t *p;
's avatar
committed
106

107
108
109
	if ((p=malloc(sizeof(measurement_t)))==NULL) {
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return NULL;
's avatar
committed
110
111
	}

112
	memset(p, 0, sizeof(measurement_t));
's avatar
committed
113

114
115
	return p;
} /* new_measurement() */
's avatar
committed
116

117
118
flow_t *new_flow(void) {
   flow_t *p;
's avatar
committed
119

120
121
122
123
   if ((p=malloc(sizeof(flow_t)))==NULL) {
      fprintf(stderr, "%s: malloc() failed\n", __func__);
      return NULL;
   }
's avatar
committed
124

125
   memset(p, 0, sizeof(flow_t));
's avatar
committed
126

127
128
	return p;
} /* new_flow() */
's avatar
committed
129

's avatar
committed
130
int protocol_filter(char *header_filter, char *protocol, int mpls,
's avatar
committed
131
132
133
134
		char **new_header_filter) {

	int new_header_filter_length;
	char *new_protocol;
's avatar
committed
135
136
	int tracked_protocol;
	int i;
's avatar
committed
137
138

	new_header_filter_length=0;
's avatar
committed
139
	tracked_protocol=0;
's avatar
committed
140
141
142

	new_protocol=NULL;
	if (protocol && protocol[0] && strcmp(protocol, "all")) {
's avatar
committed
143
144
145
146
147
148
149
150
		if (!strcmp(protocol, "ip") || !strcmp(protocol, "ip6") ||
			 !strcmp(protocol, "tcp") || !strcmp(protocol, "udp") ||
			 !strcmp(protocol, "multicast"))
			new_protocol=protocol;
		else if (!strcmp(protocol, "http"))
			new_protocol="port 80";
		else if (!strcmp(protocol, "https"))
			new_protocol="port 443";
's avatar
committed
151
		else {
's avatar
committed
152
153
154
155
156
157
158
159
160
161
162
			i=0;
			while (tracked_protocols[i].protocol) {
				if (!strcmp(tracked_protocols[i].protocol, protocol)) {
					tracked_protocol=i+1;
					break;
				}
				i++;
			}
		}

		if (new_protocol==NULL && tracked_protocol==0) {
's avatar
committed
163
164
165
			fprintf(stderr, "%s: unknown protocol %s\n", __func__, protocol);
			return -1;
		}	
's avatar
committed
166
167
168
169
170
171
	}

	/* Find length of new_header_filter */

	if (mpls)
		new_header_filter_length+=strlen("mpls");
's avatar
committed
172

's avatar
committed
173
174
175
	if (new_protocol) {
		if (new_header_filter_length)
			new_header_filter_length+=strlen(" and ");
's avatar
committed
176
177
178
179
		new_header_filter_length+=strlen(new_protocol);
	}

	if (header_filter) {
's avatar
committed
180
		if (new_header_filter_length)
's avatar
committed
181
182
183
184
			new_header_filter_length+=strlen(" and ");
		new_header_filter_length+=strlen(header_filter);
	}

's avatar
committed
185
186
187
	/* Allocate memory for new_header_filter and fill it */

	if (new_header_filter_length) {
's avatar
committed
188
189
190
191
		if ((*new_header_filter=malloc(new_header_filter_length+1))==NULL) {
			fprintf(stderr, "%s: malloc() failed\n", __func__);
			return -1;
		}
's avatar
committed
192
		memset(*new_header_filter, 0, new_header_filter_length+1);
's avatar
committed
193
194
195
196
197
198
199
200
201

		if (mpls)
			strcpy(*new_header_filter, "mpls");

		if (new_protocol) {
			if (strlen(*new_header_filter))
				strcat(*new_header_filter, " and ");
			strcat(*new_header_filter, new_protocol);
		}
's avatar
committed
202
203

		if (header_filter) {
's avatar
committed
204
			if (strlen(*new_header_filter))
's avatar
committed
205
206
207
208
209
210
211
				strcat(*new_header_filter, " and ");
			strcat(*new_header_filter, header_filter);
		}
	}
	else
		*new_header_filter=NULL;

's avatar
committed
212
	return tracked_protocol;
's avatar
committed
213
} /* protocol_filter() */
's avatar
committed
214

's avatar
committed
215
int get_local_hostname(char **hostname) {
's avatar
committed
216
217

	char buffer[MAX_HOSTNAME+1];
's avatar
committed
218
	struct hostent *hostent;
's avatar
committed
219
220
221
222
223
224
225

	if (gethostname(buffer, MAX_HOSTNAME+1)<0) {
		fprintf(stderr, "%s: gethostname() failed\n", __func__);
		return -1;
	}
	buffer[MAX_HOSTNAME]='\0';

's avatar
committed
226
227
228
229
	if ((hostent=gethostbyname(buffer))==NULL) {
		fprintf(stderr, "%s: gethostbyname() failed\n", __func__);
		return -1;
	}
's avatar
committed
230

's avatar
committed
231
	if ((*hostname=malloc(strlen(hostent->h_name)+1))==NULL) {
's avatar
committed
232
233
234
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return -1;
	}
's avatar
committed
235
	strcpy(*hostname, hostent->h_name);
's avatar
committed
236

's avatar
committed
237
238
	return 0;
} /* get_local_hostname() */
's avatar
   
committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

int timestamp_diff(struct timeval *tv_start, struct timeval *tv_stop) {
	int sec, usec;

	if (tv_start==NULL || tv_stop==NULL) {
		fprintf(stderr, "%s: tv_start or tv_stop is NULL\n", __func__);
		return -1;
	}

	usec=0;

	/* printf("tv_start->tv_sec: %u, tv_start->tv_usec: %u\n",
		(unsigned int)(tv_start->tv_sec), (unsigned int)(tv_start->tv_usec));
	printf("tv_stop->tv_sec: %u, tv_stop->tv_usec: %u\n",
		(unsigned int)(tv_stop->tv_sec), (unsigned int)(tv_stop->tv_usec)); */

	sec=tv_stop->tv_sec - tv_start->tv_sec;

	if (sec<0) {
		fprintf(stderr, "%s: stop is less than start\n", __func__);
		return -1;
	}

	if (sec>1)
		usec = (tv_stop->tv_sec - tv_start->tv_sec - 1)*1000000;
	
	if (sec)
		usec += 1000000-tv_start->tv_usec + tv_stop->tv_usec;
	else 
		usec = tv_stop->tv_usec - tv_start->tv_usec;

	if (usec<0) {
	  fprintf(stderr, "%s: stop is less than start\n", __func__);
     return -1;
   }

	return usec;
} /* timestamp_diff() */