- std::string id;
- SQLConn* newconn;
-
- id = conf.ReadValue("database", "id", i);
- newconn = new SQLConn(Srv, conf.ReadValue("database", "hostname", i),
- conf.ReadInteger("database", "port", i, true),
- conf.ReadValue("database", "name", i),
- conf.ReadValue("database", "username", i),
- conf.ReadValue("database", "password", i),
- conf.ReadFlag("database", "ssl", i));
-
- connections.insert(std::make_pair(id, newconn));
- }
- }
-
+ SQLhost host;
+ int ipvalid;
+
+ host.id = conf.ReadValue("database", "id", i);
+ host.host = conf.ReadValue("database", "hostname", i);
+ host.port = conf.ReadInteger("database", "port", i, true);
+ host.name = conf.ReadValue("database", "name", i);
+ host.user = conf.ReadValue("database", "username", i);
+ host.pass = conf.ReadValue("database", "password", i);
+ host.ssl = conf.ReadFlag("database", "ssl", "0", i);
+
+ if (HasHost(host))
+ continue;
+
+#ifdef IPV6
+ if (strchr(host.host.c_str(),':'))
+ {
+ in6_addr blargle;
+ ipvalid = inet_pton(AF_INET6, host.host.c_str(), &blargle);
+ }
+ else
+#endif
+ {
+ in_addr blargle;
+ ipvalid = inet_aton(host.host.c_str(), &blargle);
+ }
+
+ if(ipvalid > 0)
+ {
+ /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
+ host.ip = host.host;
+ this->AddConn(host);
+ }
+ else if(ipvalid == 0)
+ {
+ /* Conversion failed, assume it's a host */
+ SQLresolver* resolver;
+
+ try
+ {
+ bool cached;
+ resolver = new SQLresolver(this, ServerInstance, host, cached);
+ ServerInstance->AddResolver(resolver, cached);
+ }
+ catch(...)
+ {
+ /* THE WORLD IS COMING TO AN END! */
+ }
+ }
+ else
+ {
+ /* Invalid address family, die horribly. */
+ ServerInstance->Log(DEBUG, "BUG: insp_aton failed returning -1, oh noes.");
+ }
+ }
+ }
+
+ void ClearOldConnections()
+ {
+ ConnMap::iterator iter,safei;
+ for (iter = connections.begin(); iter != connections.end(); iter++)
+ {
+ if (!HostInConf(iter->second->GetConfHost()))
+ {
+ DELETE(iter->second);
+ safei = iter;
+ --iter;
+ connections.erase(safei);
+ }
+ }
+ }
+
+ void ClearAllConnections()
+ {
+ ConnMap::iterator i;
+ while ((i = connections.begin()) != connections.end())
+ {
+ connections.erase(i);
+ DELETE(i->second);
+ }
+ }
+
+ void AddConn(const SQLhost& hi)
+ {
+ if (HasHost(hi))
+ {
+ ServerInstance->Log(DEFAULT, "WARNING: A pgsql connection with id: %s already exists, possibly due to DNS delay. Aborting connection attempt.", hi.id.c_str());
+ return;
+ }
+
+ SQLConn* newconn;
+
+ /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
+ newconn = new SQLConn(ServerInstance, this, hi);
+
+ connections.insert(std::make_pair(hi.id, newconn));
+ }
+
+ void ReconnectConn(SQLConn* conn)
+ {
+ for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
+ {
+ if (conn == iter->second)
+ {
+ DELETE(iter->second);
+ connections.erase(iter);
+ break;
+ }
+ }
+ retimer = new ReconnectTimer(ServerInstance, this);
+ ServerInstance->Timers->AddTimer(retimer);
+ }
+