@@ -771,7 +771,7 @@ HAService::syncingStateHandler() {
771
771
// Perform synchronous leases update.
772
772
std::string status_message;
773
773
int sync_status = synchronize (status_message,
774
- config_->getFailoverPeerConfig ()-> getName () ,
774
+ config_->getFailoverPeerConfig (),
775
775
dhcp_disable_timeout);
776
776
777
777
// If the leases synchronization was successful, let's transition
@@ -1908,11 +1908,9 @@ HAService::startHeartbeat() {
1908
1908
1909
1909
void
1910
1910
HAService::asyncDisableDHCPService (HttpClient& http_client,
1911
- const std::string& server_name ,
1911
+ const HAConfig::PeerConfigPtr& remote_config ,
1912
1912
const unsigned int max_period,
1913
1913
PostRequestCallback post_request_action) {
1914
- HAConfig::PeerConfigPtr remote_config = config_->getPeerConfig (server_name);
1915
-
1916
1914
// Create HTTP/1.1 request including our command.
1917
1915
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
1918
1916
(HttpRequest::Method::HTTP_POST, " /" , HttpVersion::HTTP_11 (),
@@ -1991,10 +1989,8 @@ HAService::asyncDisableDHCPService(HttpClient& http_client,
1991
1989
1992
1990
void
1993
1991
HAService::asyncEnableDHCPService (HttpClient& http_client,
1994
- const std::string& server_name ,
1992
+ const HAConfig::PeerConfigPtr& remote_config ,
1995
1993
PostRequestCallback post_request_action) {
1996
- HAConfig::PeerConfigPtr remote_config = config_->getPeerConfig (server_name);
1997
-
1998
1994
// Create HTTP/1.1 request including our command.
1999
1995
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
2000
1996
(HttpRequest::Method::HTTP_POST, " /" , HttpVersion::HTTP_11 (),
@@ -2092,13 +2088,13 @@ HAService::asyncSyncLeases() {
2092
2088
}
2093
2089
2094
2090
lease_sync_filter_.apply ();
2095
- asyncSyncLeases (*client_, config_->getFailoverPeerConfig ()-> getName () ,
2091
+ asyncSyncLeases (*client_, config_->getFailoverPeerConfig (),
2096
2092
dhcp_disable_timeout, LeasePtr (), null_action);
2097
2093
}
2098
2094
2099
2095
void
2100
2096
HAService::asyncSyncLeases (http::HttpClient& http_client,
2101
- const std::string& server_name ,
2097
+ const HAConfig::PeerConfigPtr& remote_config ,
2102
2098
const unsigned int max_period,
2103
2099
const dhcp::LeasePtr& last_lease,
2104
2100
PostSyncCallback post_sync_action,
@@ -2108,8 +2104,8 @@ HAService::asyncSyncLeases(http::HttpClient& http_client,
2108
2104
// to allocate new leases while we fetch from it. The DHCP service will
2109
2105
// be disabled for a certain amount of time and will be automatically
2110
2106
// re-enabled if we die during the synchronization.
2111
- asyncDisableDHCPService (http_client, server_name , max_period,
2112
- [this , &http_client, server_name , max_period, last_lease,
2107
+ asyncDisableDHCPService (http_client, remote_config , max_period,
2108
+ [this , &http_client, remote_config , max_period, last_lease,
2113
2109
post_sync_action, dhcp_disabled]
2114
2110
(const bool success, const std::string& error_message, const int ) {
2115
2111
@@ -2118,7 +2114,7 @@ HAService::asyncSyncLeases(http::HttpClient& http_client,
2118
2114
if (success) {
2119
2115
// The last argument indicates that disabling the DHCP
2120
2116
// service on the partner server was successful.
2121
- asyncSyncLeasesInternal (http_client, server_name , max_period,
2117
+ asyncSyncLeasesInternal (http_client, remote_config , max_period,
2122
2118
last_lease, post_sync_action, true );
2123
2119
2124
2120
} else {
@@ -2129,19 +2125,16 @@ HAService::asyncSyncLeases(http::HttpClient& http_client,
2129
2125
2130
2126
void
2131
2127
HAService::asyncSyncLeasesInternal (http::HttpClient& http_client,
2132
- const std::string& server_name ,
2128
+ const HAConfig::PeerConfigPtr& remote_config ,
2133
2129
const unsigned int max_period,
2134
2130
const dhcp::LeasePtr& last_lease,
2135
2131
PostSyncCallback post_sync_action,
2136
2132
const bool dhcp_disabled) {
2137
-
2138
- HAConfig::PeerConfigPtr partner_config = config_->getPeerConfig (server_name);
2139
-
2140
2133
// Create HTTP/1.1 request including our command.
2141
2134
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
2142
2135
(HttpRequest::Method::HTTP_POST, " /" , HttpVersion::HTTP_11 (),
2143
- HostHttpHeader (partner_config ->getUrl ().getStrippedHostname ()));
2144
- partner_config ->addBasicAuthHttpHeader (request);
2136
+ HostHttpHeader (remote_config ->getUrl ().getStrippedHostname ()));
2137
+ remote_config ->addBasicAuthHttpHeader (request);
2145
2138
if (server_type_ == HAServerType::DHCPv4) {
2146
2139
request->setBodyAsJson (CommandCreator::createLease4GetPage (
2147
2140
boost::dynamic_pointer_cast<Lease4>(last_lease), config_->getSyncPageLimit ()));
@@ -2157,11 +2150,10 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2157
2150
HttpResponseJsonPtr response = boost::make_shared<HttpResponseJson>();
2158
2151
2159
2152
// Schedule asynchronous HTTP request.
2160
- http_client.asyncSendRequest (partner_config ->getUrl (),
2161
- partner_config ->getTlsContext (),
2153
+ http_client.asyncSendRequest (remote_config ->getUrl (),
2154
+ remote_config ->getTlsContext (),
2162
2155
request, response,
2163
- [this , partner_config, post_sync_action, &http_client, server_name,
2164
- max_period, dhcp_disabled]
2156
+ [this , remote_config, post_sync_action, &http_client, max_period, dhcp_disabled]
2165
2157
(const boost::system ::error_code& ec,
2166
2158
const HttpResponsePtr& response,
2167
2159
const std::string& error_str) {
@@ -2183,7 +2175,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2183
2175
error_message = (ec ? ec.message () : error_str);
2184
2176
LOG_ERROR (ha_logger, HA_LEASES_SYNC_COMMUNICATIONS_FAILED)
2185
2177
.arg (config_->getThisServerName ())
2186
- .arg (partner_config ->getLogLabel ())
2178
+ .arg (remote_config ->getLogLabel ())
2187
2179
.arg (error_message);
2188
2180
2189
2181
} else {
@@ -2211,7 +2203,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2211
2203
LOG_INFO (ha_logger, HA_LEASES_SYNC_LEASE_PAGE_RECEIVED)
2212
2204
.arg (config_->getThisServerName ())
2213
2205
.arg (leases_element.size ())
2214
- .arg (server_name );
2206
+ .arg (remote_config-> getLogLabel () );
2215
2207
2216
2208
// Count actually applied leases.
2217
2209
uint64_t applied_lease_count = 0 ;
@@ -2314,7 +2306,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2314
2306
error_message = ex.what ();
2315
2307
LOG_ERROR (ha_logger, HA_LEASES_SYNC_FAILED)
2316
2308
.arg (config_->getThisServerName ())
2317
- .arg (partner_config ->getLogLabel ())
2309
+ .arg (remote_config ->getLogLabel ())
2318
2310
.arg (error_message);
2319
2311
}
2320
2312
}
@@ -2327,7 +2319,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2327
2319
} else if (last_lease) {
2328
2320
// This indicates that there are more leases to be fetched.
2329
2321
// Therefore, we have to send another leaseX-get-page command.
2330
- asyncSyncLeases (http_client, server_name , max_period, last_lease,
2322
+ asyncSyncLeases (http_client, remote_config , max_period, last_lease,
2331
2323
post_sync_action, dhcp_disabled);
2332
2324
return ;
2333
2325
}
@@ -2350,20 +2342,32 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2350
2342
ConstElementPtr
2351
2343
HAService::processSynchronize (const std::string& server_name,
2352
2344
const unsigned int max_period) {
2345
+ HAConfig::PeerConfigPtr remote_config;
2346
+ try {
2347
+ remote_config = config_->getPeerConfig (server_name);
2348
+ } catch (const std::exception & ex) {
2349
+ return (createAnswer (CONTROL_RESULT_ERROR, ex.what ()));
2350
+ }
2351
+ // We must not synchronize with self.
2352
+ if (remote_config->getName () == config_->getThisServerName ()) {
2353
+ return (createAnswer (CONTROL_RESULT_ERROR, " '" + remote_config->getName ()
2354
+ + " ' points to local server but should point to a partner" ));
2355
+ }
2353
2356
std::string answer_message;
2354
- int sync_status = synchronize (answer_message, server_name , max_period);
2357
+ int sync_status = synchronize (answer_message, remote_config , max_period);
2355
2358
return (createAnswer (sync_status, answer_message));
2356
2359
}
2357
2360
2358
2361
int
2359
- HAService::synchronize (std::string& status_message, const std::string& server_name,
2362
+ HAService::synchronize (std::string& status_message,
2363
+ const HAConfig::PeerConfigPtr& remote_config,
2360
2364
const unsigned int max_period) {
2361
2365
lease_sync_filter_.apply ();
2362
2366
2363
2367
IOServicePtr io_service (new IOService ());
2364
2368
HttpClient client (io_service, false );
2365
2369
2366
- asyncSyncLeases (client, server_name , max_period, Lease4Ptr (),
2370
+ asyncSyncLeases (client, remote_config , max_period, Lease4Ptr (),
2367
2371
[&](const bool success, const std::string& error_message,
2368
2372
const bool dhcp_disabled) {
2369
2373
// If there was a fatal error while fetching the leases, let's
@@ -2381,15 +2385,15 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
2381
2385
// try to send the ha-sync-complete-notify command to the
2382
2386
// partner.
2383
2387
if (success) {
2384
- asyncSyncCompleteNotify (client, server_name ,
2388
+ asyncSyncCompleteNotify (client, remote_config ,
2385
2389
[&](const bool success,
2386
2390
const std::string& error_message,
2387
2391
const int rcode) {
2388
2392
// This command may not be supported by the partner when it
2389
2393
// runs an older Kea version. In that case, send the dhcp-enable
2390
2394
// command as in previous Kea version.
2391
2395
if (rcode == CONTROL_RESULT_COMMAND_UNSUPPORTED) {
2392
- asyncEnableDHCPService (client, server_name ,
2396
+ asyncEnableDHCPService (client, remote_config ,
2393
2397
[&](const bool success,
2394
2398
const std::string& error_message,
2395
2399
const int ) {
@@ -2422,7 +2426,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
2422
2426
// re-enable the DHCP service. Note, that we don't send the
2423
2427
// ha-sync-complete-notify command in this case. It is only sent in
2424
2428
// the case when synchronization ends successfully.
2425
- asyncEnableDHCPService (client, server_name ,
2429
+ asyncEnableDHCPService (client, remote_config ,
2426
2430
[&](const bool success,
2427
2431
const std::string& error_message,
2428
2432
const int ) {
@@ -2447,7 +2451,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
2447
2451
2448
2452
LOG_INFO (ha_logger, HA_SYNC_START)
2449
2453
.arg (config_->getThisServerName ())
2450
- .arg (server_name );
2454
+ .arg (remote_config-> getLogLabel () );
2451
2455
2452
2456
// Measure duration of the synchronization.
2453
2457
Stopwatch stopwatch;
@@ -2475,7 +2479,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
2475
2479
2476
2480
LOG_ERROR (ha_logger, HA_SYNC_FAILED)
2477
2481
.arg (config_->getThisServerName ())
2478
- .arg (server_name )
2482
+ .arg (remote_config-> getLogLabel () )
2479
2483
.arg (status_message);
2480
2484
2481
2485
return (CONTROL_RESULT_ERROR);
@@ -2488,7 +2492,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
2488
2492
2489
2493
LOG_INFO (ha_logger, HA_SYNC_SUCCESSFUL)
2490
2494
.arg (config_->getThisServerName ())
2491
- .arg (server_name )
2495
+ .arg (remote_config-> getLogLabel () )
2492
2496
.arg (stopwatch.logFormatLastDuration ());
2493
2497
2494
2498
return (CONTROL_RESULT_SUCCESS);
@@ -3015,10 +3019,8 @@ HAService::processMaintenanceCancel() {
3015
3019
3016
3020
void
3017
3021
HAService::asyncSyncCompleteNotify (HttpClient& http_client,
3018
- const std::string& server_name ,
3022
+ const HAConfig::PeerConfigPtr& remote_config ,
3019
3023
PostRequestCallback post_request_action) {
3020
- HAConfig::PeerConfigPtr remote_config = config_->getPeerConfig (server_name);
3021
-
3022
3024
// Create HTTP/1.1 request including our command.
3023
3025
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
3024
3026
(HttpRequest::Method::HTTP_POST, " /" , HttpVersion::HTTP_11 (),
0 commit comments