source: nscp/include/socket/socket_helpers.cpp @ 58f0e80

0.4.00.4.10.4.2
Last change on this file since 58f0e80 was 58f0e80, checked in by Michael Medin <michael@…>, 20 months ago

2011-10-21 MickeM

  • Some more threading issues in Python (works perfectly(?) now)
  • Refactored the tests a bit making test suites runable from a central hub
  • Fixed soo everything builds and works on Linux

2011-10-19 MickeM

  • Fixed some threadding issues in PythonScript
  • Still working on refactoring the unittest helpers a bit

2011-10-16 MickeM

  • Fixed issue with loading zero-byte UTF-8 config files
  • Fixed some issues with settings subsystem (debug log as well as createing instances for --generate)
  • Added ability to set event log to real-time
  • Added support for multiple eventlogs in real-time
  • Property mode set to 100644
File size: 2.6 KB
Line 
1#include <boost/asio.hpp>
2#include <boost/algorithm/string.hpp>
3
4#include <strEx.h>
5#include <socket/socket_helpers.hpp>
6
7
8const int socket_helpers::connection_info::backlog_default = 0;
9
10namespace ip = boost::asio::ip;
11
12
13std::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
24unsigned 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
37void 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
79void socket_helpers::io::set_result(boost::optional<boost::system::error_code>* a, boost::system::error_code b) {
80        if (!b) {
81                a->reset(b);
82//      } else {
83//              std::cout << "timer aborted incorrectly: " << b.message() << std::endl;
84        }
85}
Note: See TracBrowser for help on using the repository browser.