portconfig.cc 4.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2011  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.

15
#include <server_common/portconfig.h>
16
17

#include <asiolink/io_address.h>
18
19
20
21
22
#include <asiolink/dns_service.h>
#include <log/dummylog.h>

#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
23
24
25
26

using namespace std;
using namespace isc::data;
using namespace asiolink;
27
using isc::log::dlog;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

namespace isc {
namespace server_common {
namespace portconfig {

AddressList
parseAddresses(isc::data::ConstElementPtr addresses,
               const std::string& elemName)
{
    AddressList result;
    if (addresses) {
        if (addresses->getType() == Element::list) {
            for (size_t i(0); i < addresses->size(); ++ i) {
                ConstElementPtr addrPair(addresses->get(i));
                ConstElementPtr addr(addrPair->get("address"));
                ConstElementPtr port(addrPair->get("port"));
                if (!addr || ! port) {
                    isc_throw(BadValue, "Address must contain both the IP"
                        "address and port");
                }
                try {
                    IOAddress(addr->stringValue());
                    if (port->intValue() < 0 ||
                        port->intValue() > 0xffff) {
                        isc_throw(BadValue, "Bad port value (" <<
                            port->intValue() << ")");
                    }
                    result.push_back(AddressPair(addr->stringValue(),
                        port->intValue()));
                }
                catch (const TypeError &e) { // Better error message
                    isc_throw(TypeError,
                        "Address must be a string and port an integer");
                }
            }
        } else if (addresses->getType() != Element::null) {
            isc_throw(TypeError, elemName + " config element must be a list");
        }
    }
    return (result);
}

70
71
72
73
74
75
76
77
78
79
80
81
namespace {

void
setAddresses(DNSService& service, const AddressList& addresses) {
    service.clearServers();
    BOOST_FOREACH(const AddressPair &address, addresses) {
        service.addServer(address.second, address.first);
    }
}

}

82
void
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
installListenAddresses(const AddressList& newAddresses,
                       AddressList& addressStore,
                       asiolink::DNSService& service)
{
    try {
        dlog("Setting listen addresses:");
        BOOST_FOREACH(const AddressPair& addr, newAddresses) {
            dlog(" " + addr.first + ":" +
                        boost::lexical_cast<string>(addr.second));
        }
        setAddresses(service, newAddresses);
        addressStore = newAddresses;
    }
    catch (const exception& e) {
        /*
98
99
100
101
102
103
104
         * If one of the addresses isn't set successfully, we will restore
         * the old addresses, the behavior is that either all address are
         * set successuflly or none of them will be used. whether this
         * behavior is user desired, maybe we need revisited it later. And
         * if address setting is more smarter, it should check whether some
         * part of the new address already in used to avoid interuption the
         * service.
105
         *
106
107
108
         * If the address setting still failed, we can live with it, since
         * user will get error info, command control can be used to set new
         * address. So we just catch the exception without propagating outside
109
110
111
112
113
114
115
116
117
         */
        dlog(string("Unable to set new address: ") + e.what(), true);
        try {
            setAddresses(service, addressStore);
        }
        catch (const exception& e2) {
            dlog("Unable to recover from error;", true);
            dlog(string("Rollback failed with: ") + e2.what(), true);
        }
118
119
120
        //Anyway the new configure has problem, we need to notify configure
        //manager the new configure doesn't work
        throw;
121
122
    }
}
123

124
125
126
}
}
}