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