master_loader.cc 8.93 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
24
#include <string>

25
using std::string;
26
27
28
29
30
31
32
33

namespace isc {
namespace dns {

class MasterLoader::MasterLoaderImpl {
public:
    MasterLoaderImpl(const char* master_file,
                     const Name& zone_origin,
34
35
                     const RRClass& zone_class,
                     const MasterLoaderCallbacks& callbacks,
36
                     const AddRRCallback& add_callback,
37
38
39
40
41
                     MasterLoader::Options options) :
        lexer_(),
        zone_origin_(zone_origin),
        zone_class_(zone_class),
        callbacks_(callbacks),
42
        add_callback_(add_callback),
43
44
45
        options_(options),
        master_file_(master_file),
        initialized_(false),
46
47
        ok_(true),
        complete_(false)
48
49
    {}

50
51
52
53
54
55
56
57
58
59
60
61
62
    void reportError(const std::string& filename, size_t line,
                     const std::string& reason)
    {
        callbacks_.error(filename, line, reason);
        if (!(options_ & MANY_ERRORS)) {
            // 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());
        }
    }

63
64
65
    void pushSource(const std::string& filename) {
        std::string error;
        if (!lexer_.pushSource(filename.c_str(), &error)) {
66
            reportError("", 0, error);
67
            ok_ = false;
68
        }
69
70
71
72
73
74
        initialized_ = true;
    }

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

77
78
79
80
81
    // Get a string token. Handle it as error if it is not string.
    const string getString() {
        return (lexer_.getNextToken(MasterToken::QSTRING).getString());
    }

82
    bool loadIncremental(size_t count_limit) {
83
84
85
86
        if (complete_) {
            isc_throw(isc::InvalidOperation,
                      "Trying to load when already loaded");
        }
87
88
89
        if (!initialized_) {
            pushSource(master_file_);
        }
90
        size_t count = 0;
91
        while (ok_ && count < count_limit) {
92
93
94
95
96
97
98
99
100
101
102
103
104
            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();
105

106
107
108
109
110
111
112
113
                const MasterToken::StringRegion
                    name_string(lexer_.getNextToken(MasterToken::QSTRING).
                                getStringRegion());
                // TODO $ handling
                const Name name(name_string.beg, name_string.len,
                                &zone_origin_);
                // TODO: Some more flexibility. We don't allow omitting
                // anything yet
114

115
116
117
118
119
120
121
122
123
124
125
126
                // 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_);
                }
127

128
129
130
131
132
133
134
135
136
137
138
                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.
                if (data != rdata::RdataPtr()) {
                    add_callback_(name, rrclass, rrtype, ttl, data);
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
139

140
141
                    // Good, we loaded another one
                    ++count;
142
                } else if ((options_ & MANY_ERRORS) == 0) {
143
144
145
146
147
                    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");
148
                }
149
150
151
152
153
            } 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;
154
            } catch (const isc::Exception& e) {
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
                // 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:
                            // 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);
178
            }
179
        }
180
181
        // When there was a fatal error and ok is false, we say we are done.
        return (!ok_);
182
183
184
185
    }

private:
    MasterLexer lexer_;
186
    const Name zone_origin_;
187
188
    const RRClass zone_class_;
    MasterLoaderCallbacks callbacks_;
189
    AddRRCallback add_callback_;
190
    MasterLoader::Options options_;
191
192
193
    const std::string master_file_;
    bool initialized_;
    bool ok_;
194
195
public:
    bool complete_;
196
197
198
199
};

MasterLoader::MasterLoader(const char* master_file,
                           const Name& zone_origin,
200
201
                           const RRClass& zone_class,
                           const MasterLoaderCallbacks& callbacks,
202
                           const AddRRCallback& add_callback,
203
204
                           Options options)
{
205
206
207
    if (add_callback.empty()) {
        isc_throw(isc::InvalidParameter, "Empty add RR callback");
    }
208
    impl_ = new MasterLoaderImpl(master_file, zone_origin,
209
                                 zone_class, callbacks, add_callback, options);
210
211
}

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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");
    }
    impl_ = new MasterLoaderImpl("", zone_origin, zone_class, callbacks,
                                 add_callback, options);
    impl_->pushStreamSource(stream);
}

227
228
229
230
231
232
MasterLoader::~MasterLoader() {
    delete impl_;
}

bool
MasterLoader::loadIncremental(size_t count_limit) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
233
    const bool result = impl_->loadIncremental(count_limit);
234
235
    impl_->complete_ = result;
    return (result);
236
237
238
239
}

} // end namespace dns
} // end namespace isc