]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Added most of the connection/authentication code, need to add CONNECT and syncs
[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                 ibuf[n] = 0;
147                 return ibuf;
148         }
149         else
150         {
151                 log(DEBUG,"EOF or error on socket");
152                 return NULL;
153         }
154 }
155
156 // There are two possible outcomes to this function.
157 // It will either write all of the data, or an undefined amount.
158 // If an undefined amount is written the connection has failed
159 // and should be aborted.
160 int InspSocket::Write(std::string data)
161 {
162         char* d = (char*)data.c_str();
163         unsigned int written = 0;
164         int n = 0;
165         int s = data.length();
166         while ((written < data.length()) && (n >= 0))
167         {
168                 n = send(this->fd,d,s,0);
169                 if (n > 0)
170                 {
171                         // If we didnt write everything, advance
172                         // the pointers so that when we retry
173                         // the next time around the loop, we try
174                         // to write what we failed to write before.
175                         written += n;
176                         s -= n;
177                         d += n;
178                 }
179         }
180         return written;
181 }
182
183 bool InspSocket::Poll()
184 {
185         if ((time(NULL) > timeout_end) && (this->state == I_CONNECTING))
186         {
187                 // for non-listening sockets, the timeout can occur
188                 // which causes termination of the connection after
189                 // the given number of seconds without a successful
190                 // connection.
191                 this->OnTimeout();
192                 this->OnError(I_ERR_TIMEOUT);
193                 timeout = true;
194                 this->state = I_ERROR;
195                 return false;
196         }
197         polls.fd = this->fd;
198         state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
199         int ret = poll(&polls,1,1);
200
201         if (ret > 0)
202         {
203                 int incoming = -1;
204                 
205                 switch (this->state)
206                 {
207                         case I_CONNECTING:
208                                 this->SetState(I_CONNECTED);
209                                 return this->OnConnected();
210                         break;
211                         case I_LISTENING:
212                                 length = sizeof (client);
213                                 incoming = accept (this->fd, (sockaddr*)&client,&length);
214                                 this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
215                                 return true;
216                         break;
217                         case I_CONNECTED:
218                                 return this->OnDataReady();
219                         break;
220                         default:
221                         break;
222                 }
223         }
224         return true;
225 }
226
227 void InspSocket::SetState(InspSocketState s)
228 {
229         log(DEBUG,"Socket state change");
230         this->state = s;
231 }
232
233 InspSocketState InspSocket::GetState()
234 {
235         return this->state;
236 }
237
238 bool InspSocket::OnConnected() { return true; }
239 void InspSocket::OnError(InspSocketError e) { return; }
240 int InspSocket::OnDisconnect() { return 0; }
241 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
242 bool InspSocket::OnDataReady() { return true; }
243 void InspSocket::OnTimeout() { return; }
244 void InspSocket::OnClose() { return; }
245
246 InspSocket::~InspSocket()
247 {
248         this->Close();
249 }
250
251 /*
252 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
253 int OpenTCPSocket (void)
254 */