Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Arne Øslebø
mapi
Commits
5b9032c7
Commit
5b9032c7
authored
Nov 03, 2007
by
Browse files
Minor fixes in packetloss application.
git-svn-id:
file:///home/svn/mapi/trunk@1352
8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent
9a442cd2
Changes
1
Hide whitespace changes
Inline
Side-by-side
applications/packetloss/packetloss.c
View file @
5b9032c7
...
...
@@ -187,6 +187,20 @@ void store_results(unsigned int sensor, unsigned int count, struct flow_data *fl
// exit(EXIT_FAILURE);
//}
inline
unsigned
int
hash_function
(
flow_data
record
)
{
ip_addr
saddr
=
record
.
saddr
,
daddr
=
record
.
daddr
;
if
(
(
record
.
ptcl
==
IPPROTO_TCP
)
||
(
record
.
ptcl
==
IPPROTO_UDP
)
)
{
return
(((
unsigned
int
)((
saddr
.
byte1
+
saddr
.
byte2
+
saddr
.
byte3
+
saddr
.
byte4
)
*
record
.
sport
+
(
daddr
.
byte1
+
daddr
.
byte2
+
daddr
.
byte3
+
daddr
.
byte4
+
1
)
*
record
.
dport
))
/
137
);
}
else
{
return
(((
unsigned
int
)((
saddr
.
byte1
+
saddr
.
byte2
+
saddr
.
byte3
+
saddr
.
byte4
)
+
(
daddr
.
byte1
+
daddr
.
byte2
+
daddr
.
byte3
+
daddr
.
byte4
+
1
)))
/
137
);
}
}
int
getDevByName
(
struct
thost
host
,
char
*
dev
,
int
tip
)
{
int
d
=
0
;
...
...
@@ -224,7 +238,7 @@ char* statstostr() {
toret
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
2048
);
toret
[
0
]
=
'\0'
;
for
(
h
=
0
;
h
<
2
;
h
++
)
{
// FIXME "2" => hosts
for
(
h
=
0
;
h
<
hosts
;
h
++
)
{
sprintf
(
toret
,
"<center> <br> Sensor %d interfaces stats:<br>"
,
h
+
1
);
for
(
d
=
0
;
d
<
host
[
h
].
devices
;
d
++
)
{
stats
=
host
[
h
].
mapi_stats_dev
[
d
];
...
...
@@ -284,7 +298,6 @@ int main(int argc, char *argv[])
char
error
[
512
];
int
err_no
=
0
;
int
malloc_size
=
0
;
unsigned
int
starttime
;
...
...
@@ -312,11 +325,10 @@ int main(int argc, char *argv[])
// }}}
// Initialize host {{{
if
((
host
=
(
struct
thost
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
thost
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
=
(
struct
thost
*
)
calloc
(
hosts
,
sizeof
(
struct
thost
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
,
0
,
malloc_size
);
// }}}
// Get host names, ents/IPs, # of devices {{{
...
...
@@ -358,66 +370,56 @@ int main(int argc, char *argv[])
// Initialize host arrays {{{
for
(
h
=
0
;
h
<
hosts
;
h
++
)
{
if
((
host
[
h
].
statsrelwith
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
statsrelwith
=
(
struct
tstats
*
)
calloc
(
hosts
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
statsrelwith
,
0
,
malloc_size
);
if
((
host
[
h
].
statsdirto
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
statsdirto
=
(
struct
tstats
*
)
calloc
(
hosts
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
statsdirto
,
0
,
malloc_size
);
if
((
host
[
h
].
statsdirfrom
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
statsdirfrom
=
(
struct
tstats
*
)
calloc
(
hosts
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
statsdirfrom
,
0
,
malloc_size
);
if
((
host
[
h
].
oldstatsrelwith
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
oldstatsrelwith
=
(
struct
tstats
*
)
calloc
(
hosts
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
oldstatsrelwith
,
0
,
malloc_size
);
if
((
host
[
h
].
oldstatsdirto
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
oldstatsdirto
=
(
struct
tstats
*
)
calloc
(
hosts
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
oldstatsdirto
,
0
,
malloc_size
);
if
((
host
[
h
].
oldstatsdirfrom
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
hosts
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
oldstatsdirfrom
=
(
struct
tstats
*
)
calloc
(
hosts
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
oldstatsdirfrom
,
0
,
malloc_size
);
if
((
host
[
h
].
mapi_stats_dev
=
(
struct
mapi_stat
*
)
malloc
(
malloc_size
=
(
host
[
h
].
devices
*
sizeof
(
struct
mapi_stat
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
mapi_stats_dev
=
(
struct
mapi_stat
*
)
calloc
(
host
[
h
].
devices
,
sizeof
(
struct
mapi_stat
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
mapi_stats_dev
,
0
,
malloc_size
);
if
((
host
[
h
].
mapi_stats_devptr
=
(
struct
mapi_stat
**
)
malloc
(
malloc_size
=
(
host
[
h
].
devices
*
sizeof
(
struct
mapi_stat
*
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
mapi_stats_devptr
=
(
struct
mapi_stat
**
)
calloc
(
host
[
h
].
devices
,
sizeof
(
struct
mapi_stat
*
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
mapi_stats_devptr
,
0
,
malloc_size
);
for
(
d
=
0
;
d
<
host
[
h
].
devices
;
d
++
)
host
[
h
].
mapi_stats_devptr
[
d
]
=
&
host
[
h
].
mapi_stats_dev
[
d
];
if
((
host
[
h
].
netload_stats_dev
=
(
struct
tstatistics
*
)
malloc
(
malloc_size
=
(
host
[
h
].
devices
*
sizeof
(
struct
tstatistics
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
netload_stats_dev
=
(
struct
tstatistics
*
)
calloc
(
host
[
h
].
devices
,
sizeof
(
struct
tstatistics
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
netload_stats_dev
,
0
,
malloc_size
);
if
((
host
[
h
].
stats_dev
=
(
struct
tstats
*
)
malloc
(
malloc_size
=
(
host
[
h
].
devices
*
sizeof
(
struct
tstats
)))
)
==
NULL
)
{
fprintf
(
stderr
,
"
m
alloc(): could not allocate memory"
);
if
((
host
[
h
].
stats_dev
=
(
struct
tstats
*
)
calloc
(
host
[
h
].
devices
,
sizeof
(
struct
tstats
)))
==
NULL
)
{
fprintf
(
stderr
,
"
c
alloc(): could not allocate memory"
);
return
-
1
;
}
memset
(
host
[
h
].
stats_dev
,
0
,
malloc_size
);
}
// }}}
...
...
@@ -621,7 +623,7 @@ int main(int argc, char *argv[])
for
(
count
=
*
results
;
count
>
0
;
count
--
)
{
if
(
LOGFILE
)
{
fprintf
(
fp_log
,
"[%d]
\t
%u
\t
"
,
h
,
(
unsigned
int
)
time
(
0
));
fprintf
(
fp_log
,
"
h[%d]i
[%d]
\t
%u
\t
"
,
h
,
d
,
(
unsigned
int
)
time
(
0
));
print_flow
(
fp_log
,
flowdata
,
0
);
fflush
(
fp_log
);
}
...
...
@@ -813,9 +815,11 @@ int main(int argc, char *argv[])
old_totalpk
.
loss
=
totalpk
.
loss
;
for
(
h1
=
0
;
h1
<
hosts
;
h1
++
)
{
for
(
h2
=
0
;
h2
<
hosts
;
h2
++
)
{
if
(
h2
!=
h1
)
host
[
h1
].
oldstatsrelwith
[
h2
].
totalpk
.
loss
=
host
[
h1
].
statsrelwith
[
h2
].
totalpk
.
loss
;
if
(
h2
!=
h1
)
host
[
h1
].
oldstatsdirto
[
h2
].
totalpk
.
loss
=
host
[
h1
].
statsdirto
[
h2
].
totalpk
.
loss
;
if
(
h2
!=
h1
)
host
[
h1
].
oldstatsdirfrom
[
h2
].
totalpk
.
loss
=
host
[
h1
].
statsdirfrom
[
h2
].
totalpk
.
loss
;
if
(
h2
!=
h1
)
{
host
[
h1
].
oldstatsrelwith
[
h2
].
totalpk
.
loss
=
host
[
h1
].
statsrelwith
[
h2
].
totalpk
.
loss
;
host
[
h1
].
oldstatsdirto
[
h2
].
totalpk
.
loss
=
host
[
h1
].
statsdirto
[
h2
].
totalpk
.
loss
;
host
[
h1
].
oldstatsdirfrom
[
h2
].
totalpk
.
loss
=
host
[
h1
].
statsdirfrom
[
h2
].
totalpk
.
loss
;
}
}
}
// }}}
...
...
@@ -1208,14 +1212,10 @@ unsigned long long cmp_results(struct flow_data *data, unsigned int sensor)
unsigned
int
sendersensor
=
0
;
unsigned
int
receiversensor
=
1
;
unsigned
int
hsvalue
=
0
,
pos
,
pkloss
=
0
;
ip_addr
saddr
=
data
->
saddr
,
daddr
=
data
->
daddr
;
u_short
sport
=
data
->
sport
,
dport
=
data
->
dport
;
unsigned
int
matchedsensor
;
if
((
data
->
ptcl
==
IPPROTO_UDP
)
||
(
data
->
ptcl
==
IPPROTO_TCP
))
hsvalue
=
((
unsigned
int
)
((
saddr
.
byte1
+
saddr
.
byte2
+
saddr
.
byte3
+
saddr
.
byte4
)
*
sport
+
(
daddr
.
byte1
+
daddr
.
byte2
+
daddr
.
byte3
+
daddr
.
byte4
)
*
dport
))
/
137
;
else
hsvalue
=
((
unsigned
int
)
((
saddr
.
byte1
+
saddr
.
byte2
+
saddr
.
byte3
+
saddr
.
byte4
)
+
(
daddr
.
byte1
+
daddr
.
byte2
+
daddr
.
byte3
+
daddr
.
byte4
)))
/
137
;
hsvalue
=
hash_function
(
*
data
);
pos
=
hsvalue
%
HASHTABLE_SIZE
;
// Flow doesnt exist? (it could be already removed after processing it at the other host)
...
...
@@ -1353,20 +1353,15 @@ void store_results(unsigned int sensor, unsigned int count, struct flow_data *fl
tp
=
(
struct
timeval
*
)
malloc
(
sizeof
(
struct
timeval
));
while
(
count
--
>
0
)
{
ip_addr
saddr
=
flowdata
->
saddr
,
daddr
=
flowdata
->
daddr
;
u_short
sport
=
flowdata
->
sport
,
dport
=
flowdata
->
dport
;
if
((
flowdata
->
ptcl
==
IPPROTO_UDP
)
||
(
flowdata
->
ptcl
==
IPPROTO_TCP
))
hsvalue
=
((
unsigned
int
)
((
saddr
.
byte1
+
saddr
.
byte2
+
saddr
.
byte3
+
saddr
.
byte4
)
*
sport
+
(
daddr
.
byte1
+
daddr
.
byte2
+
daddr
.
byte3
+
daddr
.
byte4
)
*
dport
))
/
137
;
else
hsvalue
=
((
unsigned
int
)
((
saddr
.
byte1
+
saddr
.
byte2
+
saddr
.
byte3
+
saddr
.
byte4
)
+
(
daddr
.
byte1
+
daddr
.
byte2
+
daddr
.
byte3
+
daddr
.
byte4
)))
/
137
;
hsvalue
=
hash_function
(
*
flowdata
);
pos
=
hsvalue
%
HASHTABLE_SIZE
;
// Flow already exist at this host?
if
((
flow
=
compare_bucket
(
hashtable
[
pos
],
flowdata
,
sensor
,
0
))
!=
NULL
)
{
flow
->
data
.
packets_count
+=
flowdata
->
packets_count
;
flow
->
data
.
bytes_count
+=
flowdata
->
bytes_count
;
if
(
gettimeofday
(
tp
,
NULL
)
==
0
)
{
// calculate the current timestamp
current_ts
=
(((
unsigned
long
long
)
tp
->
tv_sec
)
<<
32
)
+
((
tp
->
tv_usec
*
4295
)
&
0xffffffff
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment