#include "inspircd.h"
using irc::sockets::OpenTCPSocket;
-using irc::sockets::insp_inaddr;
-using irc::sockets::insp_sockaddr;
bool InspSocket::Readable()
{
InspSocket::InspSocket(InspIRCd* SI)
{
+ this->Timeout = NULL;
this->state = I_DISCONNECTED;
this->fd = -1;
this->WaitingForWriteEvent = false;
InspSocket::InspSocket(InspIRCd* SI, int newfd, const char* ip)
{
+ this->Timeout = NULL;
this->fd = newfd;
this->state = I_CONNECTED;
strlcpy(this->IP,ip,MAXBUF);
this->Instance->SE->AddFd(this);
}
-InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool listening, unsigned long maxtime)
+InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool listening, unsigned long maxtime, const std::string &connectbindip)
{
+ this->cbindip = connectbindip;
this->fd = -1;
this->Instance = SI;
strlcpy(host,ipaddr.c_str(),MAXBUF);
this->WaitingForWriteEvent = false;
this->IsIOHooked = false;
+ this->Timeout = NULL;
if (listening)
{
if ((this->fd = OpenTCPSocket(host)) == ERROR)
{
strlcpy(this->IP,host,MAXBUF);
timeout_val = maxtime;
- this->DoConnect();
+ if (!this->DoConnect())
+ {
+ this->Close();
+ this->fd = -1;
+ this->state = I_ERROR;
+ this->OnError(I_ERR_CONNECT);
+ return;
+ }
}
}
}
* This is easier to configure when you have a lot of links and a lot
* of servers to configure.
*/
-bool InspSocket::BindAddr()
+bool InspSocket::BindAddr(const std::string &ip)
{
+ Instance->Log(DEBUG,"BindAddr(%s)", ip.c_str());
ConfigReader Conf(this->Instance);
bool bindfail = false;
socklen_t size = sizeof(sockaddr_in);
if ((!*this->host) || strchr(this->host, ':'))
v6 = true;
#endif
- for (int j =0; j < Conf.Enumerate("bind"); j++)
+ int j = 0;
+ while (j < Conf.Enumerate("bind") || (!ip.empty()))
{
- std::string Type = Conf.ReadValue("bind","type",j);
- std::string IP = Conf.ReadValue("bind","address",j);
- if (Type == "servers")
+ std::string IP = ip.empty() ? Conf.ReadValue("bind","address",j) : ip;
+ if (!ip.empty() || Conf.ReadValue("bind","type",j) == "servers")
{
- if ((IP != "*") && (IP != "127.0.0.1") && (IP != "") && (IP != "::1"))
+ if (!ip.empty() || ((IP != "*") && (IP != "127.0.0.1") && (IP != "") && (IP != "::1")))
{
sockaddr* s = new sockaddr[2];
#ifdef IPV6
return true;
}
}
+ j++;
}
return true;
}
this->fd = socket(AF_INET6, SOCK_STREAM, 0);
if ((this->fd > -1) && ((strstr(this->IP,"::ffff:") != (char*)&this->IP) && (strstr(this->IP,"::FFFF:") != (char*)&this->IP)))
{
- if (!this->BindAddr())
+ if (!this->BindAddr(this->cbindip))
{
delete[] addr;
return false;
else
{
this->fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (this->fd > -1)
+ {
+ if (!this->BindAddr(this->cbindip))
+ {
+ delete[] addr;
+ return false;
+ }
+ }
}
#else
this->fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (this->fd > -1)
+ {
+ if (!this->BindAddr(this->cbindip))
+ {
+ delete[] addr;
+ return false;
+ }
+ }
#endif
if (this->fd == -1)
delete this->sock;
return;
}
+ else
+ {
+ this->sock->Timeout = NULL;
+ }
}
bool InspSocket::Poll()
length = sizeof(sockaddr_in6);
#endif
incoming = accept (this->fd, client, &length);
-
#ifdef IPV6
if ((!*this->host) || strchr(this->host, ':'))
{