git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4780
e03df62e-2008-0410-955e-
edbf42e46eb7
/**
* Error types which a socket may exhibit
*/
/**
* Error types which a socket may exhibit
*/
-enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE };
+enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE, I_ERR_NOMOREFDS };
/* Add a query with a predefined header, and allocate an ID for it. */
DNSRequest* DNS::AddQuery(DNSHeader *header, int &id)
{
/* Add a query with a predefined header, and allocate an ID for it. */
DNSRequest* DNS::AddQuery(DNSHeader *header, int &id)
{
+ /* Is the DNS connection down? */
+ if (MasterSocket == -1)
+ return NULL;
+
/* Are there already the max number of requests on the go? */
if (requests.size() == DNS::MAX_REQUEST_ID + 1)
return NULL;
/* Are there already the max number of requests on the go? */
if (requests.size() == DNS::MAX_REQUEST_ID + 1)
return NULL;
log(DEBUG,"Add master socket %d",MasterSocket);
/* Hook the descriptor into the socket engine */
if (ServerInstance && ServerInstance->SE)
log(DEBUG,"Add master socket %d",MasterSocket);
/* Hook the descriptor into the socket engine */
if (ServerInstance && ServerInstance->SE)
- ServerInstance->SE->AddFd(MasterSocket,true,X_ESTAB_DNS);
+ {
+ if (!ServerInstance->SE->AddFd(MasterSocket,true,X_ESTAB_DNS))
+ {
+ log(DEFAULT,"Internal error starting DNS - hostnames will NOT resolve.");
+ shutdown(MasterSocket,2);
+ close(MasterSocket);
+ MasterSocket = -1;
+ }
+ }
int InspIRCd::Run()
{
/* Until THIS point, ServerInstance == NULL */
int InspIRCd::Run()
{
/* Until THIS point, ServerInstance == NULL */
this->Res = new DNS();
LoadAllModules(this);
this->Res = new DNS();
LoadAllModules(this);
/* Just in case no modules were loaded - fix for bug #101 */
this->BuildISupport();
/* Just in case no modules were loaded - fix for bug #101 */
this->BuildISupport();
- printf("\nInspIRCd is now running!\n");
-
if (!stats->BoundPortCount)
{
printf("\nI couldn't bind any ports! Are you sure you didn't start InspIRCd twice?\n");
Exit(ERROR);
}
if (!stats->BoundPortCount)
{
printf("\nI couldn't bind any ports! Are you sure you didn't start InspIRCd twice?\n");
Exit(ERROR);
}
- if (!Config->nofork)
- {
- fclose(stdout);
- fclose(stderr);
- fclose(stdin);
- }
-
/* Add the listening sockets used for client inbound connections
* to the socket engine
*/
/* Add the listening sockets used for client inbound connections
* to the socket engine
*/
for (unsigned long count = 0; count < stats->BoundPortCount; count++)
{
log(DEBUG,"Add listener: %d",Config->openSockfd[count]);
for (unsigned long count = 0; count < stats->BoundPortCount; count++)
{
log(DEBUG,"Add listener: %d",Config->openSockfd[count]);
- SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
+ if (!SE->AddFd(Config->openSockfd[count],true,X_LISTEN))
+ {
+ printf("\nEH? Could not add listener to socketengine. You screwed up, aborting.\n");
+ Exit(ERROR);
+ }
+ if (!Config->nofork)
+ {
+ fclose(stdout);
+ fclose(stderr);
+ fclose(stdin);
+ }
+
+ printf("\nInspIRCd is now running!\n");
+
this->WritePID(Config->PID);
/* main loop, this never returns */
this->WritePID(Config->PID);
/* main loop, this never returns */
this->ClosePending = false;
if (this->fd > -1)
{
this->ClosePending = false;
if (this->fd > -1)
{
- ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+ this->ClosePending = (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE));
socket_ref[this->fd] = this;
}
}
socket_ref[this->fd] = this;
}
}
this->state = I_LISTENING;
if (this->fd > -1)
{
this->state = I_LISTENING;
if (this->fd > -1)
{
- ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+ if (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE))
+ {
+ this->Close();
+ this->state = I_ERROR;
+ this->OnError(I_ERR_NOMOREFDS);
+ this->ClosePending = true;
+ }
socket_ref[this->fd] = this;
}
log(DEBUG,"New socket now in I_LISTENING state");
socket_ref[this->fd] = this;
}
log(DEBUG,"New socket now in I_LISTENING state");
*/
this->WaitingForWriteEvent = true;
ServerInstance->SE->DelFd(this->fd);
*/
this->WaitingForWriteEvent = true;
ServerInstance->SE->DelFd(this->fd);
- ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
+ if (!ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE))
+ {
+ this->Close();
+ this->fd = -1;
+ this->state = I_ERROR;
+ this->OnError(I_ERR_NOMOREFDS);
+ this->ClosePending = true;
+ }
}
void InspSocket::SetQueues(int nfd)
}
void InspSocket::SetQueues(int nfd)
this->state = I_CONNECTING;
if (this->fd > -1)
{
this->state = I_CONNECTING;
if (this->fd > -1)
{
- ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
+ if (!ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE))
+ {
+ this->OnError(I_ERR_NOMOREFDS);
+ this->Close();
+ this->fd = -1;
+ this->state = I_ERROR;
+ this->ClosePending = true;
+ return false;
+ }
socket_ref[this->fd] = this;
this->SetQueues(this->fd);
}
socket_ref[this->fd] = this;
this->SetQueues(this->fd);
}
if (this->fd > -1)
{
ServerInstance->SE->DelFd(this->fd);
if (this->fd > -1)
{
ServerInstance->SE->DelFd(this->fd);
- ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+ if (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE))
+ return false;
}
return this->OnConnected();
break;
}
return this->OnConnected();
break;
{
/* Switch back to read events */
ServerInstance->SE->DelFd(this->fd);
{
/* Switch back to read events */
ServerInstance->SE->DelFd(this->fd);
- ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+ if (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE))
+ return false;
+
/* Trigger the write event */
n = this->OnWriteReady();
}
/* Trigger the write event */
n = this->OnWriteReady();
}
/* Associate the new open port with a slot in the socket engine */
if (Config->openSockfd[count] > -1)
{
/* Associate the new open port with a slot in the socket engine */
if (Config->openSockfd[count] > -1)
{
- ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
- BoundPortCount++;
+ if (!ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN))
+ {
+ log(DEFAULT,"ERK! Failed to add listening port to socket engine!");
+ shutdown(Config->openSockfd[count],2);
+ close(Config->openSockfd[count]);
+ }
+ else
+ BoundPortCount++;
- ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT);
+ if (!ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT))
+ {
+ kill_link(_new, "Internal error handling connection");
+ return;
+ }
- log(DEBUG,"Writing to client %d",_new->fd);
WriteServ(_new->fd,"NOTICE Auth :*** Looking up your hostname...");
}
WriteServ(_new->fd,"NOTICE Auth :*** Looking up your hostname...");
}