| 1 | #include <boost/asio.hpp>
|
|---|
| 2 | #include <boost/algorithm/string.hpp>
|
|---|
| 3 |
|
|---|
| 4 | #include <strEx.h>
|
|---|
| 5 | #include <socket/socket_helpers.hpp>
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | const int socket_helpers::connection_info::backlog_default = 0;
|
|---|
| 9 |
|
|---|
| 10 | namespace ip = boost::asio::ip;
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | std::wstring socket_helpers::allowed_hosts_manager::to_wstring() {
|
|---|
| 14 | std::wstring ret;
|
|---|
| 15 | BOOST_FOREACH(const host_record &r, entries) {
|
|---|
| 16 | ip::address_v4 a(r.in_addr);
|
|---|
| 17 | ip::address_v4 m(r.mask);
|
|---|
| 18 | std::wstring s = utf8::cvt<std::wstring>(a.to_string()) + _T("(") + utf8::cvt<std::wstring>(m.to_string()) + _T(")");
|
|---|
| 19 | strEx::append_list(ret, s);
|
|---|
| 20 | }
|
|---|
| 21 | return ret;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | unsigned int socket_helpers::allowed_hosts_manager::lookup_mask(std::string mask) {
|
|---|
| 25 | unsigned int masklen = 32;
|
|---|
| 26 | if (!mask.empty()) {
|
|---|
| 27 | std::string::size_type pos = mask.find_first_of("0123456789");
|
|---|
| 28 | if (pos != std::wstring::npos) {
|
|---|
| 29 | masklen = strEx::stoi(mask.substr(pos));
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 | if (masklen > 32)
|
|---|
| 33 | masklen = 32;
|
|---|
| 34 | return (0xffffffff << (32 - masklen )) & 0xffffffff;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void socket_helpers::allowed_hosts_manager::refresh(std::list<std::string> &errors) {
|
|---|
| 38 | boost::asio::io_service io_service;
|
|---|
| 39 | ip::tcp::resolver resolver(io_service);
|
|---|
| 40 | entries.clear();
|
|---|
| 41 | host_record tmp_record;
|
|---|
| 42 | BOOST_FOREACH(std::string &record, sources) {
|
|---|
| 43 | boost::trim(record);
|
|---|
| 44 | if (!record.empty()) {
|
|---|
| 45 | std::string::size_type pos = record.find('/');
|
|---|
| 46 | if (pos == std::string::npos) {
|
|---|
| 47 | tmp_record.host = record;
|
|---|
| 48 | tmp_record.mask = lookup_mask("");
|
|---|
| 49 | } else {
|
|---|
| 50 | tmp_record.host = record.substr(0, pos);
|
|---|
| 51 | tmp_record.mask = lookup_mask(record.substr(pos));
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | if (std::isdigit(tmp_record.host[0])) {
|
|---|
| 55 | ip::address_v4 a = ip::address_v4::from_string(tmp_record.host);
|
|---|
| 56 | tmp_record.in_addr = a.to_ulong();
|
|---|
| 57 | entries.push_back(tmp_record);
|
|---|
| 58 | } else {
|
|---|
| 59 | try {
|
|---|
| 60 | ip::tcp::resolver::query query(tmp_record.host, "");
|
|---|
| 61 | ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
|
|---|
| 62 | ip::tcp::resolver::iterator end;
|
|---|
| 63 | for (;endpoint_iterator != end; ++endpoint_iterator) {
|
|---|
| 64 | tmp_record.in_addr = endpoint_iterator->endpoint().address().to_v4().to_ulong();
|
|---|
| 65 | tmp_record.host = endpoint_iterator->endpoint().address().to_string();
|
|---|
| 66 | entries.push_back(tmp_record);
|
|---|
| 67 | }
|
|---|
| 68 | } catch (const std::exception &e) {
|
|---|
| 69 | errors.push_back("Failed to lookup allowed host " + record + ": " + e.what());
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | void socket_helpers::io::set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b) {
|
|---|
| 80 | a->reset(b);
|
|---|
| 81 | }
|
|---|