]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/connection.cpp
Removed m_globops setting +g automatically on oper, m_opermodes.so can do this now
[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 bool 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         return (ircdbuffer.length() < 1048576);
132 }
133
134 bool ircd_connector::BufferIsComplete()
135 {
136         for (int i = 0; i < ircdbuffer.length(); i++)
137                 if (ircdbuffer[i] == '\n')
138                         return true;
139         return false;
140 }
141
142 void ircd_connector::ClearBuffer()
143 {
144         ircdbuffer = "";
145 }
146
147 std::string ircd_connector::GetBuffer()
148 {
149         // Fix by Brain 28th Apr 2005
150         // seems my stringstream code isnt liked by linux
151         // EVEN THOUGH IT IS CORRECT! Fixed by using a different
152         // (SLOWER) algorithm...
153         char* line = (char*)ircdbuffer.c_str();
154         std::string ret = "";
155         while ((*line != '\n') && (strlen(line)))
156         {
157                 ret = ret + *line;
158                 line++;
159         }
160         if ((*line == '\n') || (*line == '\r'))
161                 line++;
162         ircdbuffer = line;
163         return ret;
164 }
165
166 bool ircd_connector::AddWriteBuf(std::string data)
167 {
168         log(DEBUG,"connector::AddWriteBuf(%s)",data.c_str());
169         if (this->GetWriteError() != "")
170                 return false;
171         std::stringstream stream;
172         stream << sendq << data;
173         sendq = stream.str();
174         return (sendq.length() < 1048576);
175 }
176
177 bool ircd_connector::HasBufferedOutput()
178 {
179         return (sendq.length() > 0);
180 }
181
182 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
183 bool ircd_connector::FlushWriteBuf()
184 {
185         log(DEBUG,"connector::FlushWriteBuf()");
186         if (sendq.length())
187         {
188                 char* tb = (char*)this->sendq.c_str();
189                 int n_sent = write(this->fd,tb,this->sendq.length());
190                 if (n_sent == -1)
191                 {
192                         this->SetWriteError(strerror(errno));
193                         return false;
194                 }
195                 else
196                 {
197                         log(DEBUG,"Wrote %d chars to socket",n_sent);
198                         // advance the queue
199                         tb += n_sent;
200                         this->sendq = tb;
201                         return true;
202                 }
203         }
204         return true;
205 }
206
207 void ircd_connector::SetWriteError(std::string error)
208 {
209         if (this->WriteError == "")
210                 this->WriteError = error;
211 }
212
213 std::string ircd_connector::GetWriteError()
214 {
215         return this->WriteError;
216 }
217
218
219 bool ircd_connector::MakeOutboundConnection(char* newhost, int newport)
220 {
221         log(DEBUG,"MakeOutboundConnection: Original param: %s",newhost);
222         ClearBuffer();
223         hostent* hoste = gethostbyname(newhost);
224         if (!hoste)
225         {
226                 log(DEBUG,"MakeOutboundConnection: gethostbyname was NULL, setting %s",newhost);
227                 this->SetHostAddress(newhost,newport);
228                 SetHostAndPort(newhost,newport);
229         }
230         else
231         {
232                 struct in_addr* ia = (in_addr*)hoste->h_addr;
233                 log(DEBUG,"MakeOutboundConnection: gethostbyname was valid, setting %s",inet_ntoa(*ia));
234                 this->SetHostAddress(inet_ntoa(*ia),newport);
235                 SetHostAndPort(inet_ntoa(*ia),newport);
236         }
237
238         this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
239         if (this->fd >= 0)
240         {
241                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)))
242                 {
243                         WriteOpers("connect() failed for %s",host);
244                         RemoveServer(this->servername.c_str());
245                         return false;
246                 }
247                 int flags = fcntl(this->fd, F_GETFL, 0);
248                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
249                 int sendbuf = 32768;
250                 int recvbuf = 32768;
251                 setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
252                 setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
253                 return true;
254         }
255         else
256         {
257                 WriteOpers("socket() failed!");
258                 RemoveServer(this->servername.c_str());
259         }
260
261         return false;
262 }
263
264
265 void ircd_connector::SetVersionString(std::string newversion)
266 {
267         log(DEBUG,"Set version of %s to %s",this->servername.c_str(),newversion.c_str());
268         this->version = newversion;
269 }
270
271 std::string ircd_connector::GetVersionString()
272 {
273         return this->version;
274 }
275
276
277 std::string ircd_connector::GetServerName()
278 {
279         return this->servername;
280 }
281
282 std::string ircd_connector::GetDescription()
283 {
284         return this->description;
285 }
286
287 void ircd_connector::SetServerName(std::string serv)
288 {
289         this->servername = serv;
290 }
291
292 void ircd_connector::SetDescription(std::string desc)
293 {
294         this->description = desc;
295 }
296
297
298 int ircd_connector::GetDescriptor()
299 {
300         return this->fd;
301 }
302
303 int ircd_connector::GetState()
304 {
305         return this->state;
306 }
307
308
309 void ircd_connector::SetState(int newstate)
310 {
311         this->state = newstate;
312         if (state == STATE_DISCONNECTED)
313         {
314                 NetSendMyRoutingTable();
315         }
316 }
317
318 void ircd_connector::CloseConnection()
319 {
320         shutdown(this->fd,2);
321         close(this->fd);
322 }
323
324 void ircd_connector::SetDescriptor(int newfd)
325 {
326         this->fd = newfd;
327 }