Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/dsslib/netflow/flow-fixed.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2002-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* netflow dump type
23
*
24
* Glenn Fowler
25
* AT&T Research
26
*/
27
28
#include "flowlib.h"
29
30
#include <magicid.h>
31
#include <swap.h>
32
33
#define MAGIC_NAME "netflow"
34
#define MAGIC_TYPE "router"
35
#define MAGIC_VERSION 20080611L
36
37
typedef union Fixedheader_u
38
{
39
Magicid_t magic;
40
Netflow_t netflow;
41
} Fixedheader_t;
42
43
typedef struct State_s
44
{
45
int swap;
46
} State_t;
47
48
/*
49
* identf
50
*/
51
52
static int
53
fixedident(Dssfile_t* file, void* buf, size_t n, Dssdisc_t* disc)
54
{
55
Magicid_t* mp = (Magicid_t*)buf;
56
Magicid_data_t magic;
57
int swap;
58
59
magic = MAGICID;
60
if (n >= sizeof(Netflow_t) &&
61
(swap = swapop(&magic, &mp->magic, sizeof(magic))) >= 0 &&
62
streq(mp->name, MAGIC_NAME) &&
63
swapget(swap ^ int_swap, &mp->size, sizeof(mp->size)) == sizeof(Netflow_t))
64
{
65
file->skip = sizeof(Netflow_t);
66
file->ident = swap;
67
return 1;
68
}
69
return 0;
70
}
71
72
/*
73
* openf
74
*/
75
76
static int
77
fixedopen(Dssfile_t* file, Dssdisc_t* disc)
78
{
79
if (file->flags & DSS_FILE_READ)
80
{
81
if (!sfreserve(file->io, file->skip, 0))
82
{
83
if (disc->errorf)
84
(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "header read error");
85
return -1;
86
}
87
if (!(file->data = (void*)vmnewof(file->dss->vm, 0, State_t, 1, 0)))
88
{
89
if (disc->errorf)
90
(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
91
return -1;
92
}
93
((State_t*)file->data)->swap = file->ident;
94
}
95
else if (!(file->flags & DSS_FILE_APPEND))
96
{
97
Fixedheader_t hdr;
98
99
memset(&hdr, 0, sizeof(hdr));
100
hdr.magic.magic = MAGICID;
101
strcpy(hdr.magic.name, MAGIC_NAME);
102
strcpy(hdr.magic.type, MAGIC_TYPE);
103
hdr.magic.version = MAGIC_VERSION;
104
hdr.magic.size = sizeof(Netflow_t);
105
sfwrite(file->io, &hdr, sizeof(hdr));
106
}
107
return 0;
108
}
109
110
/*
111
* readf
112
*/
113
114
static int
115
fixedread(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
116
{
117
register State_t* state = (State_t*)file->data;
118
register Netflow_t* rp;
119
120
if (!(rp = (Netflow_t*)sfreserve(file->io, sizeof(*rp), 0)))
121
{
122
if (sfvalue(file->io))
123
{
124
if (disc->errorf)
125
(*disc->errorf)(NiL, disc, 2, "%slast record incomplete", cxlocation(file->dss->cx, record));
126
return -1;
127
}
128
return 0;
129
}
130
if (state->swap)
131
{
132
}
133
record->data = rp;
134
record->size = sizeof(*rp);
135
return 1;
136
}
137
138
/*
139
* writef
140
*/
141
142
static int
143
fixedwrite(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
144
{
145
if (sfwrite(file->io, record->data, sizeof(Netflow_t)) != sizeof(Netflow_t))
146
{
147
if (disc->errorf)
148
(*disc->errorf)(NiL, disc, 2, "%swrite error", cxlocation(file->dss->cx, record));
149
return -1;
150
}
151
return 0;
152
}
153
154
/*
155
* closef
156
*/
157
158
static int
159
fixedclose(Dssfile_t* file, Dssdisc_t* disc)
160
{
161
if (file->data)
162
vmfree(file->dss->vm, file->data);
163
return 0;
164
}
165
166
Dssformat_t netflow_fixed_format =
167
{
168
"fixed",
169
"Cisco netflow fixed format (2008-06-20) compresses well with pzip(1)",
170
CXH,
171
fixedident,
172
fixedopen,
173
fixedread,
174
fixedwrite,
175
0,
176
fixedclose,
177
0,
178
0,
179
netflow_fixed_next
180
};
181
182