]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
e3d8483fbfd74150107190e9b032cdeb1ce01158
[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 extern SocketEngine* SE;
40
41 extern int boundPortCount;
42 extern int openSockfd[MAXSOCKS];
43 extern time_t TIME;
44
45 InspSocket* socket_ref[65535];
46
47 InspSocket::InspSocket()
48 {
49         this->state = I_DISCONNECTED;
50 }
51
52 InspSocket::InspSocket(int newfd, char* ip)
53 {
54         this->fd = newfd;
55         this->state = I_CONNECTED;
56         this->IP = ip;
57         SE->AddFd(this->fd,true,X_ESTAB_MODULE);
58         socket_ref[this->fd] = this;
59 }
60
61 InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
62 {
63         if (listening) {
64                 if ((this->fd = OpenTCPSocket()) == ERROR)
65                 {
66                         this->fd = -1;
67                         this->state = I_ERROR;
68                         this->OnError(I_ERR_SOCKET);
69                         log(DEBUG,"OpenTCPSocket() error");
70                         return;
71                 }
72                 else
73                 {
74                         if (BindSocket(this->fd,this->client,this->server,port,(char*)host.c_str()) == ERROR)
75                         {
76                                 this->Close();
77                                 this->fd = -1;
78                                 this->state = I_ERROR;
79                                 this->OnError(I_ERR_BIND);
80                                 log(DEBUG,"BindSocket() error %s",strerror(errno));
81                                 return;
82                         }
83                         else
84                         {
85                                 this->state = I_LISTENING;
86                                 SE->AddFd(this->fd,true,X_ESTAB_MODULE);
87                                 socket_ref[this->fd] = this;
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                 socket_ref[this->fd] = this;
136                 return;
137         }
138 }
139
140 void InspSocket::Close()
141 {
142         if (this->fd != -1)
143         {
144                 this->OnClose();
145                 shutdown(this->fd,2);
146                 close(this->fd);
147                 socket_ref[this->fd] = NULL;
148                 this->fd = -1;
149         }
150 }
151
152 std::string InspSocket::GetIP()
153 {
154         return this->IP;
155 }
156
157 char* InspSocket::Read()
158 {
159         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
160         if (n > 0)
161         {
162                 ibuf[n] = 0;
163                 return ibuf;
164         }
165         else
166         {
167                 log(DEBUG,"EOF or error on socket");
168                 return NULL;
169         }
170 }
171
172 // There are two possible outcomes to this function.
173 // It will either write all of the data, or an undefined amount.
174 // If an undefined amount is written the connection has failed
175 // and should be aborted.
176 int InspSocket::Write(std::string data)
177 {
178         char* d = (char*)data.c_str();
179         unsigned int written = 0;
180         int n = 0;
181         int s = data.length();
182         while ((written < data.length()) && (n >= 0))
183         {
184                 n = send(this->fd,d,s,0);
185                 if (n > 0)
186                 {
187                         // If we didnt write everything, advance
188                         // the pointers so that when we retry
189                         // the next time around the loop, we try
190                         // to write what we failed to write before.
191                         written += n;
192                         s -= n;
193                         d += n;
194                 }
195         }
196         return written;
197 }
198
199 bool InspSocket::Timeout(time_t current)
200 {
201         if ((this->state == I_CONNECTING) && (current > timeout_end))
202         {
203                 // for non-listening sockets, the timeout can occur
204                 // which causes termination of the connection after
205                 // the given number of seconds without a successful
206                 // connection.
207                 this->OnTimeout();
208                 this->OnError(I_ERR_TIMEOUT);
209                 timeout = true;
210                 this->state = I_ERROR;
211                 return true;
212         }
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                         SE->DelFd(this->fd);
228                         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 */