#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
+#include <vector>
+#include <string>
+#include <deque>
#include "inspircd.h"
#include "modules.h"
-extern vector<Module*> modules;
-extern vector<ircd_module*> factory;
+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 };
- fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ this->port = p;
+
+ fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd <= 0)
{
return false;
this->port = p;
- setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
- linger.l_onoff = 1;
- linger.l_linger = 0;
- setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
+ setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
+ linger.l_onoff = 1;
+ linger.l_linger = 0;
+ setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
+
+ // attempt to increase socket sendq and recvq as high as its possible
+ // to get them on linux.
+ int sendbuf = 32768;
+ 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));
+
+ listen(this->fd,5);
return true;
}
-bool connection::BeginLink(char* targethost, int port, char* password)
+char* ircd_connector::GetServerIP()
{
- char connect[MAXBUF];
-
- if (this->fd)
- {
- sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
- this->haspassed = false;
- this->SendPacket(connect, targethost, port);
- return true;
- }
- return false;
+ return this->host;
}
-// targethost: in dot notation a.b.c.d
-void connection::TerminateLink(char* targethost)
+int ircd_connector::GetServerPort()
{
+ return this->port;
}
-// host: in dot notation a.b.c.d
-// port: host byte order
-bool connection::SendPacket(char *message, char* host, int port)
+bool ircd_connector::SetHostAndPort(char* host, int port)
{
- sockaddr_in host_address;
- in_addr addy;
- packet p;
+ strncpy(this->host,host,160);
+ this->port = port;
+ return true;
+}
- memset((void*)&host_address, 0, sizeof(host_address));
+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);
+ addr.sin_port = htons(port);
+ return true;
+}
- host_address.sin_family = AF_INET;
- inet_aton(host,&addy);
- host_address.sin_addr = addy;
+void ircd_connector::SetServerPort(int p)
+{
+ this->port = p;
+}
- host_address.sin_port = htons(port);
+bool ircd_connector::MakeOutboundConnection(char* host, int port)
+{
+ log(DEBUG,"MakeOutboundConnection: Original param: %s",host);
+ hostent* hoste = gethostbyname(host);
+ if (!hoste)
+ {
+ log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",host);
+ this->SetHostAddress(host,port);
+ SetHostAndPort(host,port);
+ }
+ else
+ {
+ 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*)&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);
+ fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
+ int sendbuf = 32768;
+ int recvbuf = 32768;
+ setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
+ setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
+ return true;
+ }
+ else
+ {
+ WriteOpers("socket() failed!");
+ RemoveServer(this->servername.c_str());
+ }
+
+ return false;
+}
- strcpy(p.data,message);
- p.type = PT_SYN_WITH_DATA;
- p.key = key;
+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);
- FOREACH_MOD OnPacketTransmit(p.data);
- // returns false if the packet could not be sent (e.g. target host down)
- if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
+ if (cn)
{
+ WriteOpers("CONNECT aborted: Server %s already exists",servername);
return false;
}
- return true;
+
+ 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,"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::SendSYN(char* host, int port)
+bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
{
- sockaddr_in host_address;
- in_addr addy;
- packet p;
+ char connect[MAXBUF];
+
+ ircd_connector connector;
+
+ WriteOpers("Establishing meshed link to %s:%d",servername,port);
- memset((void*)&host_address, 0, sizeof(host_address));
+ 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;
+}
- host_address.sin_family = AF_INET;
- inet_aton(host,&addy);
- host_address.sin_addr = addy;
+bool connection::AddIncoming(int fd, char* targethost, int sourceport)
+{
+ char connect[MAXBUF];
+
+ ircd_connector connector;
+
+ // targethost has been turned into an ip...
+ // we dont want this as the server name.
+ connector.SetServerName(targethost);
+ connector.SetDescriptor(fd);
+ connector.SetState(STATE_NOAUTH_INBOUND);
+ int flags = fcntl(fd, F_GETFL, 0);
+ fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+ int sendbuf = 32768;
+ 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;
+}
- host_address.sin_port = htons(port);
+void connection::TerminateLink(char* targethost)
+{
+ // this locates the targethost in the connection::connectors vector of the class,
+ // and terminates it by sending it an SQUIT token and closing its descriptor.
+ // TerminateLink with a null string causes a terminate of ALL links
+}
- p.type = PT_SYN_ONLY;
- p.key = key;
- strcpy(p.data,"");
- if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
+// Returns a pointer to the connector for 'host'
+ircd_connector* connection::FindHost(std::string host)
+{
+ for (int i = 0; i < this->connectors.size(); i++)
{
- return false;
+ if (this->connectors[i].GetServerName() == host)
+ {
+ return &this->connectors[i];
+ }
}
- return true;
-
+ return NULL;
}
-bool connection::SendACK(char* host, int port, int reply_id)
+std::string ircd_connector::GetServerName()
{
- sockaddr_in host_address;
- in_addr addy;
- packet p;
+ return this->servername;
+}
- memset((void*)&host_address, 0, sizeof(host_address));
+std::string ircd_connector::GetDescription()
+{
+ return this->description;
+}
- host_address.sin_family = AF_INET;
- inet_aton(host,&addy);
- host_address.sin_addr = addy;
+void ircd_connector::SetServerName(std::string serv)
+{
+ this->servername = serv;
+}
- host_address.sin_port = htons(port);
+void ircd_connector::SetDescription(std::string desc)
+{
+ this->description = desc;
+}
- p.type = PT_ACK_ONLY;
- p.key = key;
- p.id = reply_id;
- strcpy(p.data,"");
- if (sendto(fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
- {
- return false;
- }
- return true;
+int ircd_connector::GetDescriptor()
+{
+ return this->fd;
+}
+int ircd_connector::GetState()
+{
+ return this->state;
}
-// Generates a server key. This is pseudo-random.
-// the server always uses the same server-key in all communications
-// across the network. All other servers must remember the server key
-// of servers in the network, e.g.:
-//
-// ServerA: key=5555555555
-// ServerB: key=6666666666
-// I am ServerC: key=77777777777
-//
-// If ServerC sees a packet from ServerA, and the key stored for ServerA
-// is 0, then cache the key as the servers key.
-// after this point, any packet from ServerA which does not contain its key,
-// 555555555, will be silently dropped.
-// This should prevent blind spoofing, as to fake a server you must know its
-// assigned key, and to do that you must receive messages that are origintated
-// from it or hack the running executable.
-//
-// During the AUTH phase (when server passwords are checked, the key in any
-// packet MUST be 0). Only the initial SERVER/PASS packets may have a key
-// of 0 (and any ACK responses to them).
-//
+void ircd_connector::SetState(int state)
+{
+ this->state = state;
+ if (state == STATE_DISCONNECTED)
+ {
+ NetSendMyRoutingTable();
+ }
+}
-long connection::GenKey()
+void ircd_connector::CloseConnection()
{
- srand(time(NULL));
- return (random()*time(NULL));
+ 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);
}
-// host: in dot notation a.b.c.d
-// port: host byte order
-bool connection::RecvPacket(char *message, char* host, int &prt)
+void ircd_connector::SetDescriptor(int fd)
{
- // returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
- sockaddr_in host_address;
- socklen_t host_address_size;
- packet p;
-
- memset((void*)&host_address, 0, sizeof(host_address));
+ this->fd = fd;
+}
- host_address.sin_family=AF_INET;
- host_address_size=sizeof(host_address);
+bool connection::SendPacket(char *message, const char* host)
+{
+ if ((!message) || (!host))
+ return true;
- if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
+ ircd_connector* cn = this->FindHost(host);
+
+ if (!strchr(message,'\n'))
{
- return false;
+ strncat(message,"\n",MAXBUF);
}
- if (p.type == PT_SYN_ONLY)
+ if (cn)
{
- strcpy(message,p.data);
- strcpy(host,inet_ntoa(host_address.sin_addr));
- prt = ntohs(host_address.sin_port);
- SendACK(host,this->port,p.id);
- return false;
+ 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;
+ }
+
+ // 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));
+ 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;
}
+}
- if (p.type == PT_ACK_ONLY)
- {
- strcpy(message,p.data);
- strcpy(host,inet_ntoa(host_address.sin_addr));
- prt = ntohs(host_address.sin_port);
- return false;
- }
+// receives a packet from any where there is data waiting, first come, first served
+// fills the message and host values with the host where the data came from.
- if (p.type == PT_SYN_WITH_DATA)
+bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
+{
+ char data[32767];
+ memset(data, 0, 32767);
+ for (int i = 0; i < this->connectors.size(); i++)
{
- strcpy(message,p.data);
- strcpy(host,inet_ntoa(host_address.sin_addr));
- prt = ntohs(host_address.sin_port);
- SendACK(host,this->port,p.id);
+ 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");
+ while (l)
+ {
+ char sanitized[32767];
+ memset(sanitized, 0, 32767);
+ int ptt = 0;
+ for (int pt = 0; pt < strlen(l); pt++)
+ {
+ if (l[pt] != '\r')
+ {
+ sanitized[ptt++] = l[pt];
+ }
+ }
+ sanitized[ptt] = '\0';
+ if (strlen(sanitized))
+ {
+ messages.push_back(sanitized);
+ strncpy(host,this->connectors[i].GetServerName().c_str(),160);
+ log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
+
+ }
+ l = strtok(NULL,"\n");
+ }
+ return true;
+ }
+ }
}
+ // nothing new yet -- message and host will be undefined
+ return false;
+}
- return true;
+long connection::GenKey()
+{
+ srand(time(NULL));
+ return (random()*time(NULL));
}