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