1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
33 #include "inspstring.h"
34 #include "helperfuncs.h"
35 #include "connection.h"
40 std::deque<std::string> xsums;
42 serverrec::serverrec()
47 usercount_i = usercount = opercount = version = 0;
53 strlcpy(nickserv,"",NICKMAX);
58 serverrec::~serverrec()
62 serverrec::serverrec(char* n, long ver, bool jupe)
66 usercount_i = usercount = opercount = 0;
73 strlcpy(nickserv,"",NICKMAX);
77 bool serverrec::CreateListener(char* newhost, int p)
79 sockaddr_in host_address;
83 struct linger linger = { 0 };
87 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
93 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
96 setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
98 // attempt to increase socket sendq and recvq as high as its possible
99 // to get them on linux.
102 setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
103 setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
105 memset((void*)&host_address, 0, sizeof(host_address));
107 host_address.sin_family = AF_INET;
109 if (!strcmp(newhost,""))
111 host_address.sin_addr.s_addr = htonl(INADDR_ANY);
115 inet_aton(newhost,&addy);
116 host_address.sin_addr = addy;
119 host_address.sin_port = htons(p);
121 if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
126 // make the socket non-blocking
127 flags = fcntl(fd, F_GETFL, 0);
128 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
132 listen(this->fd, MaxConn);
138 bool serverrec::BeginLink(char* targethost, int newport, char* password, char* servername, int myport)
140 char connect[MAXBUF];
142 ircd_connector connector;
143 ircd_connector *cn = this->FindHost(servername);
148 WriteOpers("CONNECT aborted: Server %s already exists",servername);
155 if (connector.MakeOutboundConnection(targethost,newport))
157 // targethost has been turned into an ip...
158 // we dont want this as the server name.
159 connector.SetServerName(servername);
160 snprintf(connect,MAXBUF,"S %s %s %lu %s :%s",getservername().c_str(),password,(unsigned long)myport,GetRevision().c_str(),getserverdesc().c_str());
161 connector.SetState(STATE_NOAUTH_OUTBOUND);
162 connector.SetHostAndPort(targethost, newport);
163 this->connectors.push_back(connector);
164 return this->SendPacket(connect, servername);
168 connector.SetState(STATE_DISCONNECTED);
169 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
176 bool serverrec::MeshCookie(char* targethost, int newport, unsigned long cookie, char* servername)
178 char connect[MAXBUF];
180 ircd_connector connector;
182 WriteOpers("Establishing meshed link to %s:%d",servername,newport);
186 if (connector.MakeOutboundConnection(targethost,newport))
188 // targethost has been turned into an ip...
189 // we dont want this as the server name.
190 connector.SetServerName(servername);
191 snprintf(connect,MAXBUF,"- %lu %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
192 connector.SetState(STATE_NOAUTH_OUTBOUND);
193 connector.SetHostAndPort(targethost, newport);
194 connector.SetState(STATE_CONNECTED);
195 this->connectors.push_back(connector);
196 return this->SendPacket(connect, servername);
200 connector.SetState(STATE_DISCONNECTED);
201 WriteOpers("Could not create outbound connection to %s:%d",targethost,newport);
207 bool serverrec::AddIncoming(int newfd, char* targethost, int sourceport)
209 ircd_connector connector;
211 // targethost has been turned into an ip...
212 // we dont want this as the server name.
213 connector.SetServerName(targethost);
214 connector.SetDescriptor(newfd);
215 connector.SetState(STATE_NOAUTH_INBOUND);
216 int flags = fcntl(newfd, F_GETFL, 0);
217 fcntl(newfd, F_SETFL, flags | O_NONBLOCK);
220 setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf));
221 setsockopt(newfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
222 connector.SetHostAndPort(targethost, sourceport);
223 connector.SetState(STATE_NOAUTH_INBOUND);
224 log(DEBUG,"serverrec::AddIncoming() Added connection: %s:%d",targethost,sourceport);
225 this->connectors.push_back(connector);
229 void serverrec::TerminateLink(char* targethost)
231 // this locates the targethost in the serverrec::connectors vector of the class,
232 // and terminates it by sending it an SQUIT token and closing its descriptor.
233 // TerminateLink with a null string causes a terminate of ALL links
236 // Returns a pointer to the connector for 'host'
237 ircd_connector* serverrec::FindHost(std::string findhost)
239 for (int i = 0; i < this->connectors.size(); i++)
241 if (this->connectors[i].GetServerName() == findhost)
243 return &this->connectors[i];
249 void serverrec::FlushWriteBuffers()
251 for (int i = 0; i < this->connectors.size(); i++)
253 if (this->connectors[i].HasBufferedOutput())
255 if (!this->connectors[i].FlushWriteBuf())
257 // if we're here the write() caused an error, we cannot proceed
258 WriteOpers("*** Lost single connection to %s, link inactive and retrying: %s",this->connectors[i].GetServerName().c_str(),this->connectors[i].GetWriteError().c_str());
259 this->connectors[i].CloseConnection();
260 this->connectors[i].SetState(STATE_DISCONNECTED);
266 bool serverrec::SendPacket(char *message, const char* sendhost)
268 if ((!message) || (!sendhost))
271 ircd_connector* cn = this->FindHost(sendhost);
273 if (!strchr(message,'\n'))
275 strlcat(message,"\n",MAXBUF);
280 log(DEBUG,"main: serverrec::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());
282 if (cn->GetState() == STATE_DISCONNECTED)
284 // fix: can only route one hop to avoid a loop
285 if (strncmp(message,"R ",2))
287 log(DEBUG,"Not a double reroute");
288 // this route is down, we must re-route the packet through an available point in the mesh.
289 for (int k = 0; k < this->connectors.size(); k++)
291 log(DEBUG,"Check connector %d: %s",k,this->connectors[k].GetServerName().c_str());
292 // search for another point in the mesh which can 'reach' where we want to go
293 for (int m = 0; m < this->connectors[k].routes.size(); m++)
295 if (!strcasecmp(this->connectors[k].routes[m].c_str(),sendhost))
297 log(DEBUG,"Found alternative route for packet: %s",this->connectors[k].GetServerName().c_str());
299 snprintf(buffer,MAXBUF,"R %s %s",sendhost,message);
300 this->SendPacket(buffer,this->connectors[k].GetServerName().c_str());
307 snprintf(buffer,MAXBUF,"& %s",sendhost);
308 WriteOpers("*** All connections to %s lost.",sendhost);
309 NetSendToAllExcept(sendhost,buffer);
314 // returns false if the packet could not be sent (e.g. target host down)
315 if (!cn->AddWriteBuf(message))
317 // if we're here, there was an error pending, and the send cannot proceed
318 log(DEBUG,"cn->AddWriteBuf() failed for serverrec::SendPacket(): %s",cn->GetWriteError().c_str());
319 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
320 cn->CloseConnection();
321 cn->SetState(STATE_DISCONNECTED);
322 WriteOpers("*** Lost single connection to %s, link inactive and retrying: %s",cn->GetServerName().c_str(),cn->GetWriteError().c_str());
323 // retry the packet along a new route so either arrival OR failure are gauranteed (bugfix)
324 return this->SendPacket(message,sendhost);
326 if (!cn->FlushWriteBuf())
328 // if we're here the write() caused an error, we cannot proceed
329 log(DEBUG,"cn->FlushWriteBuf() failed for serverrec::SendPacket(): %s",cn->GetWriteError().c_str());
330 log(DEBUG,"Disabling connector: %s",cn->GetServerName().c_str());
331 cn->CloseConnection();
332 cn->SetState(STATE_DISCONNECTED);
333 WriteOpers("*** Lost single connection to %s, link inactive and retrying: %s",cn->GetServerName().c_str(),cn->GetWriteError().c_str());
334 // retry the packet along a new route so either arrival OR failure are gauranteed
335 return this->SendPacket(message,sendhost);
341 bool already_have_sum(std::string sum)
343 for (int i = 0; i < xsums.size(); i++)
350 if (xsums.size() >= 128)
354 xsums.push_back(sum);
358 // receives a packet from any where there is data waiting, first come, first served
359 // fills the message and host values with the host where the data came from.
361 bool serverrec::RecvPacket(std::deque<std::string> &messages, char* recvhost,std::deque<std::string> &sums)
364 memset(data, 0, 65536);
365 for (int i = 0; i < this->connectors.size(); i++)
367 if (this->connectors[i].GetState() != STATE_DISCONNECTED)
369 // returns false if the packet could not be sent (e.g. target host down)
372 // check if theres any data on this socket
373 // if not, continue onwards to the next.
375 polls.fd = this->connectors[i].GetDescriptor();
376 polls.events = POLLIN;
377 int ret = poll(&polls,1,1);
378 if (ret <= 0) continue;
380 rcvsize = recv(this->connectors[i].GetDescriptor(),data,65000,0);
381 data[rcvsize] = '\0';
386 log(DEBUG,"recv() failed for serverrec::RecvPacket(): %s",strerror(errno));
387 log(DEBUG,"Disabling connector: %s",this->connectors[i].GetServerName().c_str());
388 this->connectors[i].CloseConnection();
389 this->connectors[i].SetState(STATE_DISCONNECTED);
395 if (!this->connectors[i].AddBuffer(data))
397 WriteOpers("*** Read buffer for %s exceeds maximum, closing connection!",this->connectors[i].GetServerName().c_str());
398 this->connectors[i].CloseConnection();
399 this->connectors[i].SetState(STATE_DISCONNECTED);
401 if (this->connectors[i].BufferIsComplete())
403 while (this->connectors[i].BufferIsComplete())
405 std::string text = this->connectors[i].GetBuffer();
408 if ((text[0] == ':') && (text.find(" ") != std::string::npos))
410 std::string orig = text;
411 log(DEBUG,"Original: %s",text.c_str());
412 std::string sum = text.substr(1,text.find(" ")-1);
413 text = text.substr(text.find(" ")+1,text.length());
414 std::string possible_token = text.substr(1,text.find(" ")-1);
415 if (possible_token.length() > 1)
419 log(DEBUG,"Non-mesh, non-tokenized string passed up the chain");
423 log(DEBUG,"Packet sum: '%s'",sum.c_str());
424 if ((already_have_sum(sum)) && (sum != "*"))
426 // we don't accept dupes
429 sums.push_back(sum.c_str());
432 else sums.push_back("*");
433 messages.push_back(text.c_str());
434 strlcpy(recvhost,this->connectors[i].GetServerName().c_str(),160);
435 log(DEBUG,"serverrec::RecvPacket() %d:%s->%s",pushed++,recvhost,text.c_str());
443 // nothing new yet -- message and host will be undefined