master_loader.cc 11.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <dns/master_loader.h>
16
17
18
19
20
#include <dns/master_lexer.h>
#include <dns/name.h>
#include <dns/rrttl.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
21
#include <dns/rdata.h>
22

23
#include <string>
24
#include <memory>
25
#include <strings.h>
26

27
using std::string;
28
using std::auto_ptr;
29
30
31
32

namespace isc {
namespace dns {

33
34
35
36
37
38
39
40
41
42
// An internal exception, used to control the code flow in case of errors.
// It is thrown during the loading and caught later, not to be propagated
// outside of the file.
class InternalException : public isc::Exception {
public:
    InternalException(const char* filename, size_t line, const char* what) :
        Exception(filename, line, what)
    {}
};

43
44
45
46
class MasterLoader::MasterLoaderImpl {
public:
    MasterLoaderImpl(const char* master_file,
                     const Name& zone_origin,
47
48
                     const RRClass& zone_class,
                     const MasterLoaderCallbacks& callbacks,
49
                     const AddRRCallback& add_callback,
50
51
52
53
54
                     MasterLoader::Options options) :
        lexer_(),
        zone_origin_(zone_origin),
        zone_class_(zone_class),
        callbacks_(callbacks),
55
        add_callback_(add_callback),
56
57
58
        options_(options),
        master_file_(master_file),
        initialized_(false),
59
        ok_(true),
60
        many_errors_((options & MANY_ERRORS) != 0),
61
62
        complete_(false),
        seen_error_(false)
63
64
    {}

65
66
67
    void reportError(const std::string& filename, size_t line,
                     const std::string& reason)
    {
68
        seen_error_ = true;
69
        callbacks_.error(filename, line, reason);
70
        if (!many_errors_) {
71
72
73
74
75
76
77
78
            // In case we don't have the lenient mode, every error is fatal
            // and we throw
            ok_ = false;
            complete_ = true;
            isc_throw(MasterLoaderError, reason.c_str());
        }
    }

79
80
81
    void pushSource(const std::string& filename) {
        std::string error;
        if (!lexer_.pushSource(filename.c_str(), &error)) {
82
83
84
85
86
87
88
89
90
            if (initialized_) {
                // $INCLUDE file
                reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                            error);
            } else {
                // Top-level file
                reportError("", 0, error);
                ok_ = false;
            }
91
        }
92
93
94
95
96
97
        initialized_ = true;
    }

    void pushStreamSource(std::istream& stream) {
        lexer_.pushSource(stream);
        initialized_ = true;
98
99
    }

100
101
    // Get a string token. Handle it as error if it is not string.
    const string getString() {
102
        lexer_.getNextToken(MasterToken::STRING).getString(string_token_);
103
        return (string_token_);
104
105
    }

106
    bool loadIncremental(size_t count_limit);
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    void handleDirective(const char* directive, size_t length) {
        // We use strncasecmp, because there seems to be no reasonable
        // way to compare strings case-insensitive in C++

        // Warning: The order of compared strings does matter. The length
        // parameter applies to the first one only.
        if (strncasecmp(directive, "INCLUDE", length)) {

        } else if (strncasecmp(directive, "ORIGIN", length)) {
            // TODO: Implement
            isc_throw(isc::NotImplemented,
                      "Origin directive not implemented yet");
        } else if (strncasecmp(directive, "TTL", length)) {
            // TODO: Implement
            isc_throw(isc::NotImplemented,
                      "TTL directive not implemented yet");
        } else {
            isc_throw(InternalException, "Unknown directive '" <<
                      string(directive, directive + length) << "'");
        }
    }

130
131
private:
    MasterLexer lexer_;
132
    const Name zone_origin_;
133
134
    const RRClass zone_class_;
    MasterLoaderCallbacks callbacks_;
135
    AddRRCallback add_callback_;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
136
    const MasterLoader::Options options_;
137
    const std::string master_file_;
138
    std::string string_token_;
139
    bool initialized_;
140
141
142
    bool ok_;                   // Is it OK to continue loading?
    const bool many_errors_;    // Are many errors allowed (or should we abort
                                // on the first)
143
public:
144
145
146
    bool complete_;             // All work done.
    bool seen_error_;           // Was there at least one error during the
                                // load?
147
148
};

149
150
bool
MasterLoader::MasterLoaderImpl::loadIncremental(size_t count_limit) {
151
152
153
    if (count_limit == 0) {
        isc_throw(isc::InvalidParameter, "Count limit set to 0");
    }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    if (complete_) {
        isc_throw(isc::InvalidOperation,
                  "Trying to load when already loaded");
    }
    if (!initialized_) {
        pushSource(master_file_);
    }
    size_t count = 0;
    while (ok_ && count < count_limit) {
        try {
            // Skip all EOLNs (empty lines) and finish on EOF
            bool empty = true;
            do {
                const MasterToken& empty_token(lexer_.getNextToken());
                if (empty_token.getType() == MasterToken::END_OF_FILE) {
                    // TODO: Check if this is the last source, possibly pop
                    return (true);
                }
                empty = empty_token.getType() == MasterToken::END_OF_LINE;
            } while (empty);
            // Return the last token, as it was not empty
            lexer_.ungetToken();

177
            const MasterToken::StringRegion&
178
179
                name_string(lexer_.getNextToken(MasterToken::QSTRING).
                            getStringRegion());
180
181
182
183
184
185
186
187
188
189
190
191
192

            if (name_string.len > 0 && name_string.beg[0] == '$') {
                // This should have either thrown (and the error handler
                // will read up until the end of line) or read until the
                // end of line.

                // Exclude the $ from the string on this point.
                handleDirective(name_string.beg + 1, name_string.len - 1);
                // So, get to the next line, there's nothing more interesting
                // in this one.
                continue;
            }

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
            const Name name(name_string.beg, name_string.len,
                            &zone_origin_);
            // TODO: Some more flexibility. We don't allow omitting
            // anything yet

            // The parameters
            const RRTTL ttl(getString());
            const RRClass rrclass(getString());
            const RRType rrtype(getString());

            // TODO: Some more validation?
            if (rrclass != zone_class_) {
                // It doesn't really matter much what type of exception
                // we throw, we catch it just below.
                isc_throw(isc::BadValue, "Class mismatch: " << rrclass <<
                          "vs. " << zone_class_);
            }

            const rdata::RdataPtr data(rdata::createRdata(rrtype, rrclass,
                                                          lexer_,
                                                          &zone_origin_,
                                                          options_,
                                                          callbacks_));
            // In case we get NULL, it means there was error creating
            // the Rdata. The errors should have been reported by
            // callbacks_ already. We need to decide if we want to continue
            // or not.
220
            if (data) {
221
222
223
224
                add_callback_(name, rrclass, rrtype, ttl, data);

                // Good, we loaded another one
                ++count;
225
226
227
228
229
230
231
232
233
            } else {
                seen_error_ = true;
                if (!many_errors_) {
                    ok_ = false;
                    complete_ = true;
                    // We don't have the exact error here, but it was reported
                    // by the error callback.
                    isc_throw(MasterLoaderError, "Invalid RR data");
                }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
            }
        } catch (const MasterLoaderError&) {
            // This is a hack. We exclude the MasterLoaderError from the
            // below case. Once we restrict the below to some smaller
            // exception, we should remove this.
            throw;
        } catch (const isc::Exception& e) {
            // TODO: Once we do #2518, catch only the DNSTextError here,
            // not isc::Exception. The rest should be just simply
            // propagated.
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        e.what());
            // We want to continue. Try to read until the end of line
            bool end = false;
            do {
                const MasterToken& token(lexer_.getNextToken());
                switch (token.getType()) {
                    case MasterToken::END_OF_FILE:
252
253
254
                        callbacks_.warning(lexer_.getSourceName(),
                                           lexer_.getSourceLine(),
                                           "Unexpected end ond of file");
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
                        // TODO: Try pop in case this is not the only
                        // source
                        return (true);
                    case MasterToken::END_OF_LINE:
                        end = true;
                        break;
                    default:
                        // Do nothing. This is just to make compiler
                        // happy
                        break;
                }
            } while (!end);
        }
    }
    // When there was a fatal error and ok is false, we say we are done.
    return (!ok_);
}

273
274
MasterLoader::MasterLoader(const char* master_file,
                           const Name& zone_origin,
275
276
                           const RRClass& zone_class,
                           const MasterLoaderCallbacks& callbacks,
277
                           const AddRRCallback& add_callback,
278
279
                           Options options)
{
280
281
282
    if (add_callback.empty()) {
        isc_throw(isc::InvalidParameter, "Empty add RR callback");
    }
283
    impl_ = new MasterLoaderImpl(master_file, zone_origin,
284
                                 zone_class, callbacks, add_callback, options);
285
286
}

287
288
289
290
291
292
293
294
295
296
MasterLoader::MasterLoader(std::istream& stream,
                           const Name& zone_origin,
                           const RRClass& zone_class,
                           const MasterLoaderCallbacks& callbacks,
                           const AddRRCallback& add_callback,
                           Options options)
{
    if (add_callback.empty()) {
        isc_throw(isc::InvalidParameter, "Empty add RR callback");
    }
297
298
299
300
301
302
    auto_ptr<MasterLoaderImpl> impl(new MasterLoaderImpl("", zone_origin,
                                                         zone_class, callbacks,
                                                         add_callback,
                                                         options));
    impl->pushStreamSource(stream);
    impl_ = impl.release();
303
304
}

305
306
307
308
309
310
MasterLoader::~MasterLoader() {
    delete impl_;
}

bool
MasterLoader::loadIncremental(size_t count_limit) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
311
    const bool result = impl_->loadIncremental(count_limit);
312
313
    impl_->complete_ = result;
    return (result);
314
315
}

316
317
318
319
320
bool
MasterLoader::loadedSucessfully() const {
    return (impl_->complete_ && !impl_->seen_error_);
}

321
322
} // end namespace dns
} // end namespace isc