00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include "config.h"
00025
00026 #include <netdb.h>
00027 #include <sys/socket.h>
00028 #include <sys/types.h>
00029
00030 #include <scalestack/kernel/module.h>
00031 #include <scalestack/network/ip/address_service.h>
00032
00033 using namespace std;
00034
00035 namespace scalestack
00036 {
00037 namespace network
00038 {
00039 namespace ip
00040 {
00041
00042
00043
00044
00045
00046 address_service::address_service(kernel::module& module):
00047 _module(module),
00048 _addresses()
00049 {
00050 }
00051
00052 address_service::~address_service()
00053 {
00054 if (_addresses != NULL)
00055 freeaddrinfo(_addresses);
00056 }
00057
00058 void address_service::parse_addresses(const std::string& addresses,
00059 bool default_value_is_host,
00060 const std::string& default_value,
00061 const struct addrinfo& address_options)
00062 {
00063 string::size_type current = 0;
00064
00065 do
00066 {
00067 string::size_type next = addresses.find_first_of(",", current);
00068 string address = addresses.substr(current, next - current);
00069 string::size_type port = address.find_last_of(":");
00070
00071 if (port == string::npos)
00072 {
00073 if (default_value_is_host)
00074 lookup_addresses(default_value, address, address_options);
00075 else
00076 lookup_addresses(address, default_value, address_options);
00077 }
00078 else
00079 {
00080 lookup_addresses(address.substr(0, port), address.substr(port + 1),
00081 address_options);
00082 }
00083
00084 current = addresses.find_first_not_of(",", next);
00085 }
00086 while (current != string::npos);
00087 }
00088
00089 void address_service::lookup_addresses(const std::string& host,
00090 const std::string& port,
00091 const struct addrinfo& address_options)
00092 {
00093 int return_code = ::getaddrinfo(host == "" ? NULL : host.c_str(),
00094 port == "" ? NULL : port.c_str(),
00095 &address_options, &_addresses);
00096 if (return_code != 0)
00097 _module.log_fatal(_("Failed getaddrinfo: %s"), gai_strerror(return_code));
00098
00099 if (add_addresses(_addresses))
00100 freeaddrinfo(_addresses);
00101
00102 _addresses = NULL;
00103 }
00104
00105 }
00106 }
00107 }