Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/dsslib/bgp/bgp-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
* bgp fixed method
23
*
24
* Glenn Fowler
25
* AT&T Research
26
*/
27
28
#include "bgplib.h"
29
30
#include <magicid.h>
31
32
#define MAGIC_NAME "bgp"
33
#define MAGIC_TYPE "router"
34
#define MAGIC_VERSION 20020311L
35
36
typedef union Fixedheader_u
37
{
38
Magicid_t magic;
39
Bgproute_t route;
40
} Fixedheader_t;
41
42
typedef struct Fixedstate_s
43
{
44
int swap;
45
} Fixedstate_t;
46
47
/*
48
* fixed identf
49
*/
50
51
static int
52
fixedident(Dssfile_t* file, void* buf, size_t n, Dssdisc_t* disc)
53
{
54
Magicid_t* mp = (Magicid_t*)buf;
55
Magicid_data_t magic;
56
int swap;
57
58
magic = MAGICID;
59
if (n >= sizeof(Bgproute_t) &&
60
(swap = swapop(&magic, &mp->magic, sizeof(magic))) >= 0 &&
61
streq(mp->name, MAGIC_NAME) &&
62
swapget(swap ^ int_swap, &mp->size, sizeof(mp->size)) == sizeof(Bgproute_t))
63
{
64
file->skip = sizeof(Bgproute_t);
65
file->ident = swap;
66
return 1;
67
}
68
return 0;
69
}
70
71
/*
72
* fixed file openf
73
*/
74
75
static int
76
fixedopen(Dssfile_t* file, Dssdisc_t* disc)
77
{
78
if (file->flags & DSS_FILE_READ)
79
{
80
if (!sfreserve(file->io, file->skip, 0))
81
{
82
if (disc->errorf)
83
(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "header read error");
84
return -1;
85
}
86
if (!(file->data = (void*)vmnewof(file->dss->vm, 0, Fixedstate_t, 1, 0)))
87
{
88
if (disc->errorf)
89
(*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space");
90
return -1;
91
}
92
((Fixedstate_t*)file->data)->swap = file->ident;
93
}
94
else if (!(file->flags & DSS_FILE_APPEND))
95
{
96
Fixedheader_t hdr;
97
98
memset(&hdr, 0, sizeof(hdr));
99
hdr.magic.magic = MAGICID;
100
strcpy(hdr.magic.name, MAGIC_NAME);
101
strcpy(hdr.magic.type, MAGIC_TYPE);
102
hdr.magic.version = MAGIC_VERSION;
103
hdr.magic.size = sizeof(Bgproute_t);
104
sfwrite(file->io, &hdr, sizeof(hdr));
105
}
106
return 0;
107
}
108
109
/*
110
* fixed readf
111
*/
112
113
static int
114
fixedread(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
115
{
116
register Fixedstate_t* state = (Fixedstate_t*)file->data;
117
register Bgproute_t* rp;
118
119
if (!(rp = (Bgproute_t*)sfreserve(file->io, sizeof(*rp), 0)))
120
{
121
if (sfvalue(file->io))
122
{
123
if (disc->errorf)
124
(*disc->errorf)(NiL, disc, 2, "%s: last record incomplete", file->format->name);
125
return -1;
126
}
127
return 0;
128
}
129
if (state->swap)
130
{
131
swapmem(state->swap, rp, rp, (char*)&rp->path - (char*)rp);
132
if (rp->cluster.size)
133
swapmem(state->swap, rp->data + rp->cluster.offset, rp->data + rp->cluster.offset, rp->cluster.size * sizeof(Bgpnum_t));
134
if (state->swap & 1)
135
{
136
swapmem(state->swap & 1, (char*)&rp->path, (char*)&rp->path, (char*)&rp->bits - (char*)&rp->path);
137
if (rp->path.size)
138
swapmem(state->swap & 1, rp->data + rp->path.offset, rp->data + rp->path.offset, rp->path.size * sizeof(Bgpasn_t));
139
if (rp->community.size)
140
swapmem(state->swap & 1, rp->data + rp->community.offset, rp->data + rp->community.offset, rp->community.size * sizeof(Bgpasn_t));
141
}
142
}
143
record->data = rp;
144
record->size = rp->size;
145
return 1;
146
}
147
148
/*
149
* fixed writef
150
*/
151
152
static int
153
fixedwrite(Dssfile_t* file, Dssrecord_t* record, Dssdisc_t* disc)
154
{
155
if (sfwrite(file->io, record->data, sizeof(Bgproute_t)) != sizeof(Bgproute_t))
156
{
157
if (disc->errorf)
158
(*disc->errorf)(NiL, disc, 2, "%s: write error", file->format->name);
159
return -1;
160
}
161
return 0;
162
}
163
164
/*
165
* fixed closef
166
*/
167
168
static int
169
fixedclose(Dssfile_t* file, Dssdisc_t* disc)
170
{
171
if (!file->data)
172
return -1;
173
vmfree(file->dss->vm, file->data);
174
return 0;
175
}
176
177
Dssformat_t bgp_fixed_format =
178
{
179
"fixed",
180
"bgp fixed format (2007-09-27) compresses well with pzip(1)",
181
CXH,
182
fixedident,
183
fixedopen,
184
fixedread,
185
fixedwrite,
186
0,
187
fixedclose,
188
0,
189
0,
190
bgp_fixed_next
191
};
192
193