* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- * E-mail:
- * <brain@chatspike.net>
- * <Craig@chatspike.net>
- *
- * Written by Craig Edwards, Craig McLure, and others.
+ * InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
* This program is free but copyrighted software; see
* the file COPYING for details.
*
this->IsIOHooked = false;
if (listening)
{
- if ((this->fd = OpenTCPSocket()) == ERROR)
+ if ((this->fd = OpenTCPSocket(host)) == ERROR)
{
this->fd = -1;
this->state = I_ERROR;
this->OnError(I_ERR_SOCKET);
- this->Instance->Log(DEBUG,"OpenTCPSocket() error");
return;
}
else
{
- if (!SI->BindSocket(this->fd,this->client,this->server,aport,(char*)ipaddr.c_str()))
+ if (!SI->BindSocket(this->fd,aport,(char*)ipaddr.c_str()))
{
- this->Instance->Log(DEBUG,"BindSocket() error %s",strerror(errno));
this->Close();
this->fd = -1;
this->state = I_ERROR;
this->OnError(I_ERR_NOMOREFDS);
}
}
- this->Instance->Log(DEBUG,"New socket now in I_LISTENING state");
return;
}
- }
+ }
}
else
{
if (insp_aton(host,&addy) < 1)
{
- this->Instance->Log(DEBUG,"You cannot pass hostnames to InspSocket, resolve them first with Resolver!");
+ this->Instance->Log(DEBUG,"BUG: Hostname passed to InspSocket, rather than an IP address!");
this->Close();
this->fd = -1;
this->state = I_ERROR;
}
else
{
- this->Instance->Log(DEBUG,"No need to resolve %s",this->host);
strlcpy(this->IP,host,MAXBUF);
timeout_val = maxtime;
this->DoConnect();
{
insp_inaddr n;
ConfigReader Conf(this->Instance);
-
- this->Instance->Log(DEBUG,"In InspSocket::BindAddr()");
for (int j =0; j < Conf.Enumerate("bind"); j++)
{
std::string Type = Conf.ReadValue("bind","type",j);
std::string IP = Conf.ReadValue("bind","address",j);
if (Type == "servers")
{
- if ((IP != "*") && (IP != "127.0.0.1") && (IP != ""))
+ if ((IP != "*") && (IP != "127.0.0.1") && (IP != "") && (IP != "::1"))
{
insp_sockaddr s;
if (insp_aton(IP.c_str(),&n) > 0)
{
- this->Instance->Log(DEBUG,"Found an IP to bind to: %s",IP.c_str());
#ifdef IPV6
- s.sin6_addr = n;
+ memcpy(&s.sin6_addr, &n, sizeof(n));
s.sin6_family = AF_FAMILY;
#else
s.sin_addr = n;
#endif
if (bind(this->fd,(struct sockaddr*)&s,sizeof(s)) < 0)
{
- this->Instance->Log(DEBUG,"Cant bind()");
this->state = I_ERROR;
this->OnError(I_ERR_BIND);
this->fd = -1;
return false;
}
- this->Instance->Log(DEBUG,"bind() reports outbound fd bound to ip %s",IP.c_str());
return true;
}
- else
- {
- this->Instance->Log(DEBUG,"Address '%s' was not an IP address",IP.c_str());
- }
}
}
}
- this->Instance->Log(DEBUG,"Found no suitable IPs to bind, binding INADDR_ANY");
return true;
}
bool InspSocket::DoConnect()
{
- this->Instance->Log(DEBUG,"In DoConnect()");
if ((this->fd = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
{
- this->Instance->Log(DEBUG,"Cant socket()");
this->state = I_ERROR;
this->OnError(I_ERR_SOCKET);
return false;
return false;
}
- this->Instance->Log(DEBUG,"Part 2 DoConnect() %s",this->IP);
insp_aton(this->IP,&addy);
#ifdef IPV6
addr.sin6_family = AF_FAMILY;
- memcpy(&addr.sin6_addr, &addy, sizeof(insp_inaddr));
+ memcpy(&addr.sin6_addr, &addy, sizeof(addy));
addr.sin6_port = htons(this->port);
#else
addr.sin_family = AF_FAMILY;
addr.sin_port = htons(this->port);
#endif
- int flags;
- flags = fcntl(this->fd, F_GETFL, 0);
+ int flags = fcntl(this->fd, F_GETFL, 0);
fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
if (connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
{
if (errno != EINPROGRESS)
{
- this->Instance->Log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
this->OnError(I_ERR_CONNECT);
this->Close();
this->state = I_ERROR;
}
this->SetQueues(this->fd);
}
- this->Instance->Log(DEBUG,"Returning true from InspSocket::DoConnect");
return true;
}
void InspSocket::Close()
{
+ /* Save this, so we dont lose it,
+ * otherise on failure, error messages
+ * might be inaccurate.
+ */
+ int save = errno;
if (this->fd > -1)
{
if (this->IsIOHooked && Instance->Config->GetIOHook(this))
{
Instance->Config->GetIOHook(this)->OnRawSocketClose(this->fd);
}
- catch (ModuleException& modexcept)
+ catch (CoreException& modexcept)
{
- Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+ Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
}
}
this->OnClose();
shutdown(this->fd,2);
close(this->fd);
}
+ errno = save;
}
std::string InspSocket::GetIP()
{
MOD_RESULT = Instance->Config->GetIOHook(this)->OnRawSocketRead(this->fd,this->ibuf,sizeof(this->ibuf),result2);
}
- catch (ModuleException& modexcept)
+ catch (CoreException& modexcept)
{
- Instance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+ Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
}
if (MOD_RESULT < 0)
{
{
int err = errno;
if (err == EAGAIN)
- {
return "";
- }
else
- {
- if (!n)
- this->Instance->Log(DEBUG,"EOF or error on socket: EOF");
- else
- this->Instance->Log(DEBUG,"EOF or error on socket: %s",strerror(err));
return NULL;
- }
}
}
void InspSocket::MarkAsClosed()
{
- this->Instance->Log(DEBUG,"Marked as closed");
}
// There are two possible outcomes to this function.
errno = EAGAIN;
}
}
- else if ((result == -1) && (errno != EAGAIN))
+ else if (((result == -1) && (errno != EAGAIN)) || (result == 0))
{
- this->Instance->Log(DEBUG,"Write error on socket: %s",strerror(errno));
this->OnError(I_ERR_WRITE);
this->state = I_ERROR;
this->Instance->SE->DelFd(this);
return true;
}
}
- catch (ModuleException& modexcept)
+ catch (CoreException& modexcept)
{
- Instance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+ Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
return true;
}
}
}
else if ((result == -1) && (errno != EAGAIN))
{
- this->Instance->Log(DEBUG,"Write error on socket: %s",strerror(errno));
this->OnError(I_ERR_WRITE);
this->state = I_ERROR;
this->Instance->SE->DelFd(this);
void SocketTimeout::Tick(time_t now)
{
if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
- {
- ServerInstance->Log(DEBUG,"Our socket has been deleted before the timeout was reached.");
return;
- }
if (this->sock->state == I_CONNECTING)
{
- ServerInstance->Log(DEBUG,"Timed out, current=%lu",now);
// for non-listening sockets, the timeout can occur
// which causes termination of the connection after
// the given number of seconds without a successful
switch (this->state)
{
case I_CONNECTING:
- this->Instance->Log(DEBUG,"State = I_CONNECTING");
/* Our socket was in write-state, so delete it and re-add it
* in read-state.
*/
{
Instance->Config->GetIOHook(this)->OnRawSocketConnect(this->fd);
}
- catch (ModuleException& modexcept)
+ catch (CoreException& modexcept)
{
- Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+ Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
}
}
return this->OnConnected();
Instance->Config->GetIOHook(this)->OnRawSocketAccept(incoming, insp_ntoa(client.sin_addr), this->port);
#endif
}
- catch (ModuleException& modexcept)
+ catch (CoreException& modexcept)
{
- Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+ Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
}
}
void InspSocket::SetState(InspSocketState s)
{
- this->Instance->Log(DEBUG,"Socket state change");
this->state = s;
}
}
else
{
- Instance->Log(DEBUG,"State=%d CONNECTED=%d", this->state, I_CONNECTED);
if (this->FlushWriteBuffer())
{
this->Instance->SE->DelFd(this);