]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Fixed to not allow :Abc NICK Abc, where the case of the old and new nick are *identical*
[user/henk/code/inspircd.git] / src / socket.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 "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <string>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <sstream>
30 #include <iostream>
31 #include <fstream>
32 #include "socket.h"
33 #include "inspircd.h"
34 #include "inspircd_io.h"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
37 #include "socketengine.h"
38
39
40 extern InspIRCd* ServerInstance;
41 extern time_t TIME;
42
43 InspSocket* socket_ref[65535];
44
45 InspSocket::InspSocket()
46 {
47         this->state = I_DISCONNECTED;
48 }
49
50 InspSocket::InspSocket(int newfd, char* ip)
51 {
52         this->fd = newfd;
53         this->state = I_CONNECTED;
54         this->IP = ip;
55         ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
56         socket_ref[this->fd] = this;
57 }
58
59 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
60 {
61         if (listening) {
62                 if ((this->fd = OpenTCPSocket()) == ERROR)
63                 {
64                         this->fd = -1;
65                         this->state = I_ERROR;
66                         this->OnError(I_ERR_SOCKET);
67                         log(DEBUG,"OpenTCPSocket() error");
68                         return;
69                 }
70                 else
71                 {
72                         if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
73                         {
74                                 this->Close();
75                                 this->fd = -1;
76                                 this->state = I_ERROR;
77                                 this->OnError(I_ERR_BIND);
78                                 log(DEBUG,"BindSocket() error %s",strerror(errno));
79                                 return;
80                         }
81                         else
82                         {
83                                 this->state = I_LISTENING;
84                                 ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
85                                 socket_ref[this->fd] = this;
86                                 log(DEBUG,"New socket now in I_LISTENING state");
87                                 return;
88                         }
89                 }                       
90         } else {
91                 char* ip;
92                 this->host = host;
93                 hostent* hoste = gethostbyname(host.c_str());
94                 if (!hoste) {
95                         ip = (char*)host.c_str();
96                 } else {
97                         struct in_addr* ia = (in_addr*)hoste->h_addr;
98                         ip = inet_ntoa(*ia);
99                 }
100
101                 this->IP = ip;
102
103                 timeout_end = time(NULL)+maxtime;
104                 timeout = false;
105                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
106                 {
107                         this->state = I_ERROR;
108                         this->OnError(I_ERR_SOCKET);
109                         return;
110                 }
111                 this->port = port;
112                 inet_aton(ip,&addy);
113                 addr.sin_family = AF_INET;
114                 addr.sin_addr = addy;
115                 addr.sin_port = htons(this->port);
116
117                 int flags;
118                 flags = fcntl(this->fd, F_GETFL, 0);
119                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
120
121                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
122                 {
123                         if (errno != EINPROGRESS)
124                         {
125                                 this->Close();
126                                 this->OnError(I_ERR_CONNECT);
127                                 this->state = I_ERROR;
128                                 return;
129                         }
130                 }
131                 this->state = I_CONNECTING;
132                 ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
133                 socket_ref[this->fd] = this;
134                 return;
135         }
136 }
137
138 void InspSocket::Close()
139 {
140         if (this->fd != -1)
141         {
142                 this->OnClose();
143                 shutdown(this->fd,2);
144                 close(this->fd);
145                 socket_ref[this->fd] = NULL;
146                 this->fd = -1;
147         }
148 }
149
150 std::string InspSocket::GetIP()
151 {
152         return this->IP;
153 }
154
155 char* InspSocket::Read()
156 {
157         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
158         if (n > 0)
159         {
160                 ibuf[n] = 0;
161                 return ibuf;
162         }
163         else
164         {
165                 log(DEBUG,"EOF or error on socket");
166                 return NULL;
167         }
168 }
169
170 // There are two possible outcomes to this function.
171 // It will either write all of the data, or an undefined amount.
172 // If an undefined amount is written the connection has failed
173 // and should be aborted.
174 int InspSocket::Write(std::string data)
175 {
176         this->Buffer = this->Buffer + data;
177         this->FlushWriteBuffer();
178         return data.length();
179 }
180
181 void InspSocket::FlushWriteBuffer()
182 {
183         int result = 0;
184         if (this->Buffer.length())
185         {
186                 result = send(this->fd,this->Buffer.c_str(),this->Buffer.length(),0);
187                 if (result > 0)
188                 {
189                         /* If we wrote some, advance the buffer forwards */
190                         char* n = (char*)this->Buffer.c_str();
191                         n += result;
192                         this->Buffer = n;
193                 }
194         }
195 }
196
197 bool InspSocket::Timeout(time_t current)
198 {
199         if ((this->state == I_CONNECTING) && (current > timeout_end))
200         {
201                 // for non-listening sockets, the timeout can occur
202                 // which causes termination of the connection after
203                 // the given number of seconds without a successful
204                 // connection.
205                 this->OnTimeout();
206                 this->OnError(I_ERR_TIMEOUT);
207                 timeout = true;
208                 this->state = I_ERROR;
209                 return true;
210         }
211         if (this->Buffer.length())
212                 this->FlushWriteBuffer();
213         return false;
214 }
215
216 bool InspSocket::Poll()
217 {
218         int incoming = -1;
219         
220         switch (this->state)
221         {
222                 case I_CONNECTING:
223                         this->SetState(I_CONNECTED);
224                         /* Our socket was in write-state, so delete it and re-add it
225                          * in read-state.
226                          */
227                         ServerInstance->SE->DelFd(this->fd);
228                         ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
229                         return this->OnConnected();
230                 break;
231                 case I_LISTENING:
232                         length = sizeof (client);
233                         incoming = accept (this->fd, (sockaddr*)&client,&length);
234                         this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
235                         return true;
236                 break;
237                 case I_CONNECTED:
238                         return this->OnDataReady();
239                 break;
240                 default:
241                 break;
242         }
243
244         return true;
245 }
246
247 void InspSocket::SetState(InspSocketState s)
248 {
249         log(DEBUG,"Socket state change");
250         this->state = s;
251 }
252
253 InspSocketState InspSocket::GetState()
254 {
255         return this->state;
256 }
257
258 int InspSocket::GetFd()
259 {
260         return this->fd;
261 }
262
263 bool InspSocket::OnConnected() { return true; }
264 void InspSocket::OnError(InspSocketError e) { return; }
265 int InspSocket::OnDisconnect() { return 0; }
266 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
267 bool InspSocket::OnDataReady() { return true; }
268 void InspSocket::OnTimeout() { return; }
269 void InspSocket::OnClose() { return; }
270
271 InspSocket::~InspSocket()
272 {
273         this->Close();
274 }
275
276 /*
277 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
278 int OpenTCPSocket (void)
279 */