Commit 08545b16 authored by Micha Mueller's avatar Micha Mueller
Browse files

First implementation draft of HTTPS network logic for PDU plugin (incomplete)

Missing:
-Certificate validation and so on required?
-just send request.xml? or use GET or something?
-undefined symbol error when trying to load plugin
parent 667fd482
......@@ -12,7 +12,7 @@ SensorTemplate {
pdus {
pdu rack1 {
; networkAddress ?
; host testHorst ;port 443 for https will automatically be added
cooldown 500 ;Time interval at which the energy.xml is refreshed. If empty use smallest sensor-interval
; total {
......
......@@ -77,6 +77,8 @@ std::vector<Sensor*>& PDUConfigurator::readConfig(std::string cfgPath) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, pdu.second) {
if (boost::iequals(val.first, "cooldown")) {
pduUnit.setCooldown(stoull(val.second.data()));
} else if (boost::iequals(val.first, "host")) {
pduUnit.setHost(val.second.data() + ":443");
} else if (boost::iequals(val.first, "sensors")) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &sensor, val.second) {
cout << "sensor \"" << sensor.second.data() << "\"" << endl;
......
......@@ -7,12 +7,21 @@
#include "PDUUnit.h"
#include "timestamp.h"
#include <iostream>
#include <sstream>
#include <boost/foreach.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
PDUUnit::PDUUnit() {
_cooldown = 1000;
_lastRefresh = 0;
_host = "";
_strand = NULL;
}
......@@ -28,7 +37,6 @@ uint64_t PDUUnit::findValue(unsigned clustsafeID, unsigned outletID) {
#ifdef DEBUG
std::cout << "[" << prettyPrintTimestamp(now) << "] " << "Refreshed XML-file" << std::endl;
#endif
_lastRefresh = now;
}
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, _ptree.get_child("clustsafeResponse.energy")) {
if (v.first == "clustsafe") {
......@@ -55,8 +63,74 @@ void PDUUnit::initalizeStrand(boost::asio::io_service& io) {
}
void PDUUnit::refresh() {
//TODO
boost::property_tree::read_xml("/home/micha/LRZ/energy.xml", _ptree);
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
SSL_CTX* ctx;
BIO* bio;
ctx = SSL_CTX_new(SSLv23_method());
if (!ctx) {
std::cerr << "OpenSSL: " << ERR_reason_error_string(ERR_get_error()) << std::endl;
return;
}
bio = BIO_new_ssl_connect(ctx);
BIO_set_conn_hostname(bio, "httpbin.org/xml");//_host.c_str());
if(BIO_do_connect(bio) <= 0)
{
std::cerr << "OpenSSL: " << ERR_reason_error_string(ERR_get_error()) << std::endl;
BIO_free_all(bio);
SSL_CTX_free(ctx);
return;
}
//TODO hardcode request? make path to file with request configurable?
char buf[2048];
size_t len;
std::ifstream in("sensors/pdu/clustsafe_req.xml");
std::stringstream sstr;
sstr << in.rdbuf();
std::string str = sstr.str();
strncpy(buf, str.c_str(), sizeof(buf));
len = str.length();
// do not bother too long if a write/read failed. Sensor intervals are usually small, just try again when next sensor wants to read.
// Current sensor has to use old value though.
// send request
/*if (BIO_write(bio, buf, len) <= 0) {
std::cerr << "OpenSSL: " << ERR_reason_error_string(ERR_get_error()) << std::endl;
BIO_free_all(bio);
SSL_CTX_free(ctx);
return;
}*/
memset(buf, 0, 2048);
// read reply
if (BIO_read(bio, buf, sizeof(buf)) <= 0) {
std::cerr << "OpenSSL: " << ERR_reason_error_string(ERR_get_error()) << std::endl;
BIO_free_all(bio);
SSL_CTX_free(ctx);
return;
}
BIO_free_all(bio);
SSL_CTX_free(ctx);
std::ofstream ofs;
//TODO store in same location as program?
ofs.open("test.xml");
ofs.write(buf, sizeof(buf));
ofs.close();
boost::property_tree::read_xml("energy.xml", _ptree);
_lastRefresh = getTimestamp();
return;
}
......@@ -24,6 +24,10 @@ public:
return _cooldown;
}
void setHost(const std::string& host) {
_host = host;
}
void initalizeStrand(boost::asio::io_service& io);
boost::asio::io_service::strand* getStrand() const {
......@@ -49,6 +53,7 @@ private:
uint64_t _lastRefresh;
unsigned int _cooldown;
std::string _host;
boost::property_tree::ptree _ptree;
boost::asio::io_service::strand* _strand;
};
......
content-length: 183
<?xml version="1.0" encoding="UTF-8"?>
<clustsafeRequest>
<authorization>
<username>admin</username>
<password>admin</password>
</authorization>
<energy/>
</clustsafeRequest>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment