using namespace std;
+
extern std::vector<Module*> modules;
extern std::vector<ircd_module*> factory;
extern int MODCOUNT;
-packet::packet()
-{
- srand(time(NULL));
- id = random();
-}
-
-packet::~packet()
-{
-}
connection::connection()
{
- key = GenKey();
fd = 0;
}
int on = 0;
struct linger linger = { 0 };
+ this->port = p;
+
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd <= 0)
{
return true;
}
+char* ircd_connector::GetServerIP()
+{
+ return this->host;
+}
+
+int ircd_connector::GetServerPort()
+{
+ return this->port;
+}
+
+bool ircd_connector::SetHostAndPort(char* host, int port)
+{
+ strncpy(this->host,host,160);
+ this->port = port;
+ return true;
+}
+
bool ircd_connector::SetHostAddress(char* host, int port)
{
+ strncpy(this->host,host,160);
+ this->port = port;
memset((void*)&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
inet_aton(host,&addr.sin_addr);
return true;
}
+void ircd_connector::SetServerPort(int p)
+{
+ this->port = p;
+}
+
bool ircd_connector::MakeOutboundConnection(char* host, int port)
{
+ log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
hostent* hoste = gethostbyname(host);
if (!hoste)
{
- WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
+ log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
this->SetHostAddress(host,port);
+ SetHostAndPort(host,port);
}
else
{
- WriteOpers("Found hostname for %s",host);
- this->SetHostAddress(hoste->h_addr,port);
+ struct in_addr* ia = (in_addr*)hoste->h_addr;
+ log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
+ this->SetHostAddress(inet_ntoa(*ia),port);
+ SetHostAndPort(inet_ntoa(*ia),port);
}
this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (this->fd >= 0)
{
- if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
+ if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
{
WriteOpers("connect() failed for %s",host);
+ RemoveServer(this->servername.c_str());
return false;
}
int flags = fcntl(this->fd, F_GETFL, 0);
else
{
WriteOpers("socket() failed!");
+ RemoveServer(this->servername.c_str());
}
return false;
}
-bool connection::BeginLink(char* targethost, int port, char* password, char* servername)
+bool connection::BeginLink(char* targethost, int port, char* password, char* servername, int myport)
{
char connect[MAXBUF];
ircd_connector connector;
+ ircd_connector *cn = this->FindHost(servername);
+
+
+ if (cn)
+ {
+ WriteOpers("CONNECT aborted: Server %s already exists",servername);
+ return false;
+ }
+
if (this->fd)
{
// targethost has been turned into an ip...
// we dont want this as the server name.
connector.SetServerName(servername);
- sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
+ sprintf(connect,"S %s %s %d %d :%s",getservername().c_str(),password,myport,GetRevision(),getserverdesc().c_str());
connector.SetState(STATE_NOAUTH_OUTBOUND);
+ connector.SetHostAndPort(targethost, port);
this->connectors.push_back(connector);
return this->SendPacket(connect, servername);
}
else
{
+ connector.SetState(STATE_DISCONNECTED);
WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
}
}
return false;
}
-bool connection::AddIncoming(int fd,char* targethost)
+bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
+{
+ char connect[MAXBUF];
+
+ ircd_connector connector;
+
+ WriteOpers("Establishing meshed link to %s:%d",servername,port);
+
+ if (this->fd)
+ {
+ if (connector.MakeOutboundConnection(targethost,port))
+ {
+ // targethost has been turned into an ip...
+ // we dont want this as the server name.
+ connector.SetServerName(servername);
+ sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
+ connector.SetState(STATE_NOAUTH_OUTBOUND);
+ connector.SetHostAndPort(targethost, port);
+ connector.SetState(STATE_CONNECTED);
+ this->connectors.push_back(connector);
+ return this->SendPacket(connect, servername);
+ }
+ else
+ {
+ connector.SetState(STATE_DISCONNECTED);
+ WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
+ }
+ }
+ return false;
+}
+
+bool connection::AddIncoming(int fd, char* targethost, int sourceport)
{
char connect[MAXBUF];
int recvbuf = 32768;
setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
+ connector.SetHostAndPort(targethost, sourceport);
+ connector.SetState(STATE_NOAUTH_INBOUND);
+ log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",targethost,sourceport);
this->connectors.push_back(connector);
return true;
}
return this->servername;
}
+std::string ircd_connector::GetDescription()
+{
+ return this->description;
+}
+
void ircd_connector::SetServerName(std::string serv)
{
this->servername = serv;
}
+void ircd_connector::SetDescription(std::string desc)
+{
+ this->description = desc;
+}
+
int ircd_connector::GetDescriptor()
{
void ircd_connector::SetState(int state)
{
this->state = state;
+ if (state == STATE_DISCONNECTED)
+ {
+ NetSendMyRoutingTable();
+ }
+}
+
+void ircd_connector::CloseConnection()
+{
+ int flags = fcntl(this->fd, F_GETFL, 0);
+ fcntl(this->fd, F_SETFL, flags ^ O_NONBLOCK);
+ close(this->fd);
+ flags = fcntl(this->fd, F_GETFL, 0);
+ fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
}
void ircd_connector::SetDescriptor(int fd)
bool connection::SendPacket(char *message, const char* host)
{
+ if ((!message) || (!host))
+ return true;
+
ircd_connector* cn = this->FindHost(host);
+ if (!strchr(message,'\n'))
+ {
+ strncat(message,"\n",MAXBUF);
+ }
+
if (cn)
{
log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
+
+ if (cn->GetState() == STATE_DISCONNECTED)
+ {
+ log(DEBUG,"Main route to %s is down, seeking alternative",host);
+ // fix: can only route one hop to avoid a loop
+ if (strncat(message,"R ",2))
+ {
+ // this route is down, we must re-route the packet through an available point in the mesh.
+ for (int k = 0; k < this->connectors.size(); k++)
+ {
+ // search for another point in the mesh which can 'reach' where we want to go
+ for (int m = 0; m < this->connectors[k].routes.size(); m++)
+ {
+ if (!strcasecmp(this->connectors[k].routes[m].c_str(),host))
+ {
+ log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"R %s %s",host,message);
+ this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
+ return true;
+ }
+ }
+ }
+ }
+ char buffer[MAXBUF];
+ snprintf(buffer,MAXBUF,"& %s",host);
+ NetSendToAllExcept(host,buffer);
+ log(DEBUG,"There are no routes to %s, we're gonna boot the server off!",host);
+ DoSplit(host);
+ return false;
+ }
- strncat(message,"\n",MAXBUF);
// returns false if the packet could not be sent (e.g. target host down)
if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
{
log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
- return false;
+ log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
+ cn->CloseConnection();
+ cn->SetState(STATE_DISCONNECTED);
+ // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
+ return this->SendPacket(message,host);
}
return true;
}
memset(data, 0, 32767);
for (int i = 0; i < this->connectors.size(); i++)
{
- // returns false if the packet could not be sent (e.g. target host down)
- int rcvsize = 0;
- if (rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0))
+ if (this->connectors[i].GetState() != STATE_DISCONNECTED)
{
+ // returns false if the packet could not be sent (e.g. target host down)
+ int rcvsize = 0;
+ rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0);
+ if (rcvsize == -1)
+ {
+ if (errno != EAGAIN)
+ {
+ log(DEBUG,"recv() failed for Connection::RecvPacket(): %s",strerror(errno));
+ log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
+ this->connectors[i].CloseConnection();
+ this->connectors[i].SetState(STATE_DISCONNECTED);
+ }
+ }
if (rcvsize > 0)
{
char* l = strtok(data,"\n");