Use time_t instead of int.
[bpdyndnsd] / src / service_ods.cpp
CommitLineData
089a7152
BS
1/** @file
2 * @brief ODS Service class implementation. This class represents the ODS service.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
10#include "service_ods.h"
6650af14 11#include "net_helper.h"
ca5d6889 12#include "util.h"
089a7152
BS
13
14#include <time.h>
15
16using namespace std;
17
18
19/**
20 * Default Constructor, needed for object serialization.
21 */
629d8110 22ServiceOds::ServiceOds()
089a7152
BS
23{
24}
25
26
27/**
28 * Constructor.
29 * @param _hostname The hostname to update
30 * @param _login The login name.
31 * @param _password The corresponding password.
32 */
629d8110 33ServiceOds::ServiceOds(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl)
6650af14
BS
34 : UpdateServer("update.ods.org")
35 , Port("7071")
089a7152
BS
36{
37 if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
38 set_update_interval(15); // use default protocol value
39 else
40 set_update_interval(_update_interval);
41
42 if ( _max_updates_within_interval == -1 )
43 set_max_updates_within_interval(3);
44 else
45 set_max_updates_within_interval(_max_updates_within_interval);
46
47 if ( _dns_cache_ttl == -1 )
48 set_dns_cache_ttl(180);
49 else
50 set_dns_cache_ttl(_dns_cache_ttl);
51
52 set_protocol(_protocol);
53 set_hostname(_hostname);
54 set_login(_login);
55 set_password(_password);
56 set_logger(_logger);
57}
58
59
60/**
61 * Default destructor.
62 */
629d8110 63ServiceOds::~ServiceOds()
089a7152
BS
64{
65}
66
67
68/**
69 * Performs the Service update.
70 * @param ip IP Address to set.
71 * @return 0 if all is fine, -1 otherwise.
72 */
629d8110 73int ServiceOds::perform_update(const std::string& ip)
089a7152 74{
6650af14
BS
75 // First of all create a boost shared pointer to the NetHelper.
76 NetHelper::Ptr connection(new NetHelper(get_logger()));
77
78 // Then open the connection to the ODS update server.
79 if (connection->open_connection(UpdateServer,Port) != 0)
80 return -1;
81
82 // Receive the server greeting.
83 string server_greeting = connection->receive_data();
84 if ( server_greeting.empty() )
85 return -1;
86
87 // Send login command
88 ostringstream login;
89 login << "LOGIN " << get_login() << " " << get_password() << endl;
90 if (connection->send_data(login.str()))
91 return -1;
92
93 // Receive login reply
94 string login_reply = connection->receive_data();
95 if ( login_reply.empty() )
96 return -1;
97 else
98 {
99 // Get the return code out of the received data
100 string status_code = Util::parse_status_code(login_reply);
101 if ( status_code == "520" )
102 {
103 // Login failed
104 get_logger()->print_service_not_authorized(UpdateServer,get_login(),get_password());
105 connection->close_connection();
106 return -1;
107 }
108 else if ( status_code != "225" )
109 {
110 // Undefined protocol error, Log login_reply
111 get_logger()->print_undefined_protocol_error("ODS",login_reply);
112 connection->close_connection();
113 return -1;
114 }
115 }
116
117 // Successfully loged in, status_code should be 225
118
c1247537
BS
119 // Perform delete operation until there is no RR or we have performed it 10 times.
120 int count = 0;
121 string delete_reply, reply_code;
122 ostringstream delete_request;
123 delete_request << "DELRR " << get_hostname() << " A " << endl;
124 do
125 {
126 // Send delete request
127 if (connection->send_data(delete_request.str()))
128 return -1;
129 // Get delete reply
130 delete_reply = connection->receive_data();
131 if ( delete_reply.empty() )
132 return -1;
133 count++;
134 reply_code = Util::parse_status_code(delete_reply);
135 }while ( (count < 10) && (reply_code != "300") );
136
137
6650af14
BS
138 // Send update request
139 ostringstream update_request;
c1247537 140 update_request << "ADDRR " << get_hostname() << " A " << ip << endl;
6650af14
BS
141 if (connection->send_data(update_request.str()))
142 return -1;
143
144 // Receive update request server reply
145 string update_reply = connection->receive_data();
c1247537 146 if ( update_reply.empty() )
6650af14
BS
147 return -1;
148 else
149 {
150 // Get the return code out of the received data
c1247537 151 string status_code = Util::parse_status_code(update_reply);
6650af14
BS
152 if ( status_code != "795" )
153 {
154 get_logger()->print_undefined_protocol_error("ODS",update_reply);
155 connection->close_connection();
156 return -1;
157 }
158 }
159
160 // Successfully updated host
161 connection->close_connection();
089a7152
BS
162 return 0;
163}