]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
7d5df66f95c4784be929c2d9faeef5732bad1891
[user/henk/code/inspircd.git] / src / connection.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <connection.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <vector>
27 #include <string>
28 #include <deque>
29 #include <sstream>
30 #include "inspircd.h"
31 #include "modules.h"
32 #include "inspstring.h"
33 #include "helperfuncs.h"
34
35
36 extern std::vector<Module*> modules;
37 extern std::vector<ircd_module*> factory;
38
39 extern int MODCOUNT;
40
41 extern time_t TIME;
42
43
44 /**
45  * The InspIRCd mesh network is maintained by a tree of objects which reference *themselves*.
46  * Every local server has an array of 32 *serverrecs, known as me[]. Each of these represents
47  * a local listening port, and is not null if the user has opened a listening port on the server.
48  * It is assumed nobody will ever want to open more than 32 listening server ports at any one
49  * time (i mean come on, why would you want more, the ircd works fine with ONE).
50  * Each me[] entry has multiple classes within it of type ircd_connector. These are stored in a vector
51  * and each represents a server linked via this socket. If the connection was created outbound,
52  * the connection is bound to the default ip address by using me[defaultRoute] (defaultRoute being
53  * a global variable which indicates the default server to make connections on). If the connection
54  * was created inbound, it is attached to the port the connection came in on. There may be as many
55  * ircd_connector objects as needed in each me[] entry. Each ircd_connector implements the specifics
56  * of an ircd connection in the mesh, however each ircd may have multiple ircd_connector connections
57  * to it, to maintain the mesh link.
58  */
59
60 char* xsumtable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
61
62 // creates a random id for a line for detection of duplicate messages
63 std::string CreateSum()
64 {
65         char sum[9];
66         sum[0] = ':';
67         sum[8] = '\0';
68         for(int q = 1; q < 8; q++)
69                 sum[q] = xsumtable[rand()%52];
70         return sum;
71 }
72
73 connection::connection()
74 {
75         fd = -1;
76 }
77
78
79 ircd_connector::ircd_connector()
80 {
81         fd = -1;
82         port = 0;
83         sendq = "";
84         WriteError = "";
85 }
86
87 char* ircd_connector::GetServerIP()
88 {
89         return this->host;
90 }
91
92 int ircd_connector::GetServerPort()
93 {
94         return this->port;
95 }
96
97 bool ircd_connector::SetHostAndPort(char* newhost, int newport)
98 {
99         strncpy(this->host,newhost,160);
100         this->port = newport;
101         return true;
102 }
103
104 bool ircd_connector::SetHostAddress(char* newhost, int newport)
105 {
106         strncpy(this->host,newhost,160);
107         this->port = newport;
108         memset((void*)&addr, 0, sizeof(addr));
109         addr.sin_family = AF_INET;
110         inet_aton(host,&addr.sin_addr);
111         addr.sin_port = htons(port);
112         return true;
113 }
114
115 void ircd_connector::SetServerPort(int p)
116 {
117         this->port = p;
118 }
119
120 void ircd_connector::AddBuffer(std::string a)
121 {
122         std::string b = "";
123         for (int i = 0; i < a.length(); i++)
124                 if (a[i] != '\r')
125                         b = b + a[i];
126
127         std::stringstream stream(ircdbuffer);
128         stream << b;
129         log(DEBUG,"AddBuffer: %s",b.c_str());
130         ircdbuffer = stream.str();
131 }
132
133 bool ircd_connector::BufferIsComplete()
134 {
135         for (int i = 0; i < ircdbuffer.length(); i++)
136                 if (ircdbuffer[i] == '\n')
137                         return true;
138         return false;
139 }
140
141 void ircd_connector::ClearBuffer()
142 {
143         ircdbuffer = "";
144 }
145
146 std::string ircd_connector::GetBuffer()
147 {
148         // Fix by Brain 28th Apr 2005
149         // seems my stringstream code isnt liked by linux
150         // EVEN THOUGH IT IS CORRECT! Fixed by using a different
151         // (SLOWER) algorithm...
152         char* line = (char*)ircdbuffer.c_str();
153         std::string ret = "";
154         while ((*line != '\n') && (strlen(line)))
155         {
156                 ret = ret + *line;
157                 line++;
158         }
159         if ((*line == '\n') || (*line == '\r'))
160                 line++;
161         ircdbuffer = line;
162         return ret;
163 }
164
165 bool ircd_connector::AddWriteBuf(std::string data)
166 {
167         log(DEBUG,"connector::AddWriteBuf(%s)",data.c_str());
168         if (this->GetWriteError() != "")
169                 return false;
170         std::stringstream stream;
171         stream << sendq << data;
172         sendq = stream.str();
173         return true;
174 }
175
176 bool ircd_connector::HasBufferedOutput()
177 {
178         return (sendq.length() > 0);
179 }
180
181 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
182 bool ircd_connector::FlushWriteBuf()
183 {
184         log(DEBUG,"connector::FlushWriteBuf()");
185         if (sendq.length())
186         {
187                 char* tb = (char*)this->sendq.c_str();
188                 int n_sent = write(this->fd,tb,this->sendq.length());
189                 if (n_sent == -1)
190                 {
191                         this->SetWriteError(strerror(errno));
192                         return false;
193                 }
194                 else
195                 {
196                         log(DEBUG,"Wrote %d chars to socket",n_sent);
197                         // advance the queue
198                         tb += n_sent;
199                         this->sendq = tb;
200                         return true;
201                 }
202         }
203         return true;
204 }
205
206 void ircd_connector::SetWriteError(std::string error)
207 {
208         if (this->WriteError == "")
209                 this->WriteError = error;
210 }
211
212 std::string ircd_connector::GetWriteError()
213 {
214         return this->WriteError;
215 }
216
217
218 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
219 {
220         log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
221         ClearBuffer();
222         hostent* hoste = gethostbyname(newhost);
223         if (!hoste)
224         {
225                 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
226                 this->SetHostAddress(newhost,newport);
227                 SetHostAndPort(newhost,newport);
228         }
229         else
230         {
231                 struct in_addr* ia = (in_addr*)hoste->h_addr;
232                 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
233                 this->SetHostAddress(inet_ntoa(*ia),newport);
234                 SetHostAndPort(inet_ntoa(*ia),newport);
235         }
236
237         this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
238         if (this->fd >= 0)
239         {
240                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
241                 {
242                         WriteOpers("connect() failed for %s",host);
243                         RemoveServer(this->servername.c_str());
244                         return false;
245                 }
246                 int flags = fcntl(this->fd, F_GETFL, 0);
247                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
248                 int sendbuf = 32768;
249                 int recvbuf = 32768;
250                 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
251                 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
252                 return true;
253         }
254         else
255         {
256                 WriteOpers("socket() failed!");
257                 RemoveServer(this->servername.c_str());
258         }
259
260         return false;
261 }
262
263
264 void ircd_connector::SetVersionString(std::string newversion)
265 {
266         log(DEBUG,"Set version of %s to %s",this->servername.c_str(),newversion.c_str());
267         this->version = newversion;
268 }
269
270 std::string ircd_connector::GetVersionString()
271 {
272         return this->version;
273 }
274
275
276 std::string ircd_connector::GetServerName()
277 {
278         return this->servername;
279 }
280
281 std::string ircd_connector::GetDescription()
282 {
283         return this->description;
284 }
285
286 void ircd_connector::SetServerName(std::string serv)
287 {
288         this->servername = serv;
289 }
290
291 void ircd_connector::SetDescription(std::string desc)
292 {
293         this->description = desc;
294 }
295
296
297 int ircd_connector::GetDescriptor()
298 {
299         return this->fd;
300 }
301
302 int ircd_connector::GetState()
303 {
304         return this->state;
305 }
306
307
308 void ircd_connector::SetState(int newstate)
309 {
310         this->state = newstate;
311         if (state == STATE_DISCONNECTED)
312         {
313                 NetSendMyRoutingTable();
314         }
315 }
316
317 void ircd_connector::CloseConnection()
318 {
319         shutdown(this->fd,2);
320         close(this->fd);
321 }
322
323 void ircd_connector::SetDescriptor(int newfd)
324 {
325         this->fd = newfd;
326 }