]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Added module sockets to new engine, MAY NOT WORK
[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 FILE *log_file;
43 extern int boundPortCount;
44 extern int openSockfd[MAXSOCKS];
45 extern time_t TIME;
46 extern bool unlimitcore;
47 extern int MaxConn;
48
49 InspSocket::InspSocket()
50 {
51         this->state = I_DISCONNECTED;
52 }
53
54 InspSocket::InspSocket(int newfd, char* ip)
55 {
56         this->fd = newfd;
57         this->state = I_CONNECTED;
58         this->IP = ip;
59         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
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                                 log(DEBUG,"New socket now in I_LISTENING state");
89                                 return;
90                         }
91                 }                       
92         } else {
93                 char* ip;
94                 this->host = host;
95                 hostent* hoste = gethostbyname(host.c_str());
96                 if (!hoste) {
97                         ip = (char*)host.c_str();
98                 } else {
99                         struct in_addr* ia = (in_addr*)hoste->h_addr;
100                         ip = inet_ntoa(*ia);
101                 }
102
103                 this->IP = ip;
104
105                 timeout_end = time(NULL)+maxtime;
106                 timeout = false;
107                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
108                 {
109                         this->state = I_ERROR;
110                         this->OnError(I_ERR_SOCKET);
111                         return;
112                 }
113                 this->port = port;
114                 inet_aton(ip,&addy);
115                 addr.sin_family = AF_INET;
116                 addr.sin_addr = addy;
117                 addr.sin_port = htons(this->port);
118
119                 int flags;
120                 flags = fcntl(this->fd, F_GETFL, 0);
121                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
122
123                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
124                 {
125                         if (errno != EINPROGRESS)
126                         {
127                                 this->Close();
128                                 this->OnError(I_ERR_CONNECT);
129                                 this->state = I_ERROR;
130                                 return;
131                         }
132                 }
133                 this->state = I_CONNECTING;
134                 SE->AddFd(this->fd,false,X_ESTAB_MODULE);
135                 return;
136         }
137 }
138
139 void InspSocket::Close()
140 {
141         if (this->fd != -1)
142         {
143                 this->OnClose();
144                 shutdown(this->fd,2);
145                 close(this->fd);
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         char* d = (char*)data.c_str();
177         unsigned int written = 0;
178         int n = 0;
179         int s = data.length();
180         while ((written < data.length()) && (n >= 0))
181         {
182                 n = send(this->fd,d,s,0);
183                 if (n > 0)
184                 {
185                         // If we didnt write everything, advance
186                         // the pointers so that when we retry
187                         // the next time around the loop, we try
188                         // to write what we failed to write before.
189                         written += n;
190                         s -= n;
191                         d += n;
192                 }
193         }
194         return written;
195 }
196
197 bool InspSocket::Poll()
198 {
199         if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
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 false;
210         }
211
212         int incoming = -1;
213         
214         switch (this->state)
215         {
216                 case I_CONNECTING:
217                         this->SetState(I_CONNECTED);
218                         return this->OnConnected();
219                         /* Our socket was in write-state, so delete it and re-add it
220                          * in read-state.
221                          */
222                         SE->DelFd(this->fd);
223                         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
224                 break;
225                 case I_LISTENING:
226                         length = sizeof (client);
227                         incoming = accept (this->fd, (sockaddr*)&client,&length);
228                         this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
229                         return true;
230                 break;
231                 case I_CONNECTED:
232                         return this->OnDataReady();
233                 break;
234                 default:
235                 break;
236         }
237
238         return true;
239 }
240
241 void InspSocket::SetState(InspSocketState s)
242 {
243         log(DEBUG,"Socket state change");
244         this->state = s;
245 }
246
247 InspSocketState InspSocket::GetState()
248 {
249         return this->state;
250 }
251
252 int InspSocket::GetFd()
253 {
254         return this->fd;
255 }
256
257 bool InspSocket::OnConnected() { return true; }
258 void InspSocket::OnError(InspSocketError e) { return; }
259 int InspSocket::OnDisconnect() { return 0; }
260 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
261 bool InspSocket::OnDataReady() { return true; }
262 void InspSocket::OnTimeout() { return; }
263 void InspSocket::OnClose() { return; }
264
265 InspSocket::~InspSocket()
266 {
267         this->Close();
268 }
269
270 /*
271 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
272 int OpenTCPSocket (void)
273 */