]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
cb019a3c2dbd1e255e4de882cb9fb828e360e18a
[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(std::string host, int port, bool listening, unsigned long maxtime)
52 {
53         if (listening) {
54         } else {
55                 char* ip;
56                 this->host = host;
57                 hostent* hoste = gethostbyname(host.c_str());
58                 if (!hoste) {
59                         ip = (char*)host.c_str();
60                 } else {
61                         struct in_addr* ia = (in_addr*)hoste->h_addr;
62                         ip = inet_ntoa(*ia);
63                 }
64
65                 timeout_end = time(NULL)+maxtime;
66                 timeout = false;
67                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
68                 {
69                         this->state = I_ERROR;
70                         this->OnError(I_ERR_SOCKET);
71                         return;
72                 }
73                 this->port = port;
74                 inet_aton(ip,&addy);
75                 addr.sin_family = AF_INET;
76                 addr.sin_addr = addy;
77                 addr.sin_port = htons(this->port);
78
79                 int flags;
80                 flags = fcntl(this->fd, F_GETFL, 0);
81                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
82
83                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
84                 {
85                         if (errno != EINPROGRESS)
86                         {
87                                 this->Close();
88                                 this->OnError(I_ERR_CONNECT);
89                                 this->state = I_ERROR;
90                                 return;
91                         }
92                 }
93                 this->state = I_CONNECTING;
94                 return;
95         }
96 }
97
98 void InspSocket::Close()
99 {
100         if (this->fd != -1)
101         {
102                 this->OnClose();
103                 shutdown(this->fd,2);
104                 close(this->fd);
105                 this->fd = -1;
106         }
107 }
108
109 char* InspSocket::Read()
110 {
111         int n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
112         if (n > 0)
113         {
114                 return ibuf;
115         }
116         else
117         {
118                 return NULL;
119         }
120 }
121
122 // There are two possible outcomes to this function.
123 // It will either write all of the data, or an undefined amount.
124 // If an undefined amount is written the connection has failed
125 // and should be aborted.
126 int InspSocket::Write(std::string data)
127 {
128         char* d = (char*)data.c_str();
129         unsigned int written = 0;
130         int n = 0;
131         int s = data.length();
132         while ((written < data.length()) && (n >= 0))
133         {
134                 n = send(this->fd,d,s,0);
135                 if (n > 0)
136                 {
137                         // If we didnt write everything, advance
138                         // the pointers so that when we retry
139                         // the next time around the loop, we try
140                         // to write what we failed to write before.
141                         written += n;
142                         s -= n;
143                         d += n;
144                 }
145         }
146         return written;
147 }
148
149 bool InspSocket::Poll()
150 {
151         if (time(NULL) > timeout_end)
152         {
153                 this->OnTimeout();
154                 this->Close();
155                 this->OnError(I_ERR_TIMEOUT);
156                 timeout = true;
157                 this->state = I_ERROR;
158                 return false;
159         }
160         polls.fd = this->fd;
161         state == I_CONNECTING ? polls.events = POLLOUT : polls.events = POLLIN;
162         int ret = poll(&polls,1,1);
163
164         if (ret > 0)
165         {
166                 switch (this->state)
167                 {
168                         case I_CONNECTING:
169                                 return this->OnConnected();
170                         break;
171                         case I_LISTENING:
172                                 this->OnIncomingConnection();
173                         break;
174                         case I_CONNECTED:
175                                 return this->OnDataReady();
176                         break;
177                         default:
178                         break;
179                 }
180         }
181
182         return true;
183 }
184
185 void InspSocket::SetState(InspSocketState s)
186 {
187         this->state = s;
188 }
189
190 bool InspSocket::OnConnected() { return true; }
191 void InspSocket::OnError(InspSocketError e) { return; }
192 int InspSocket::OnDisconnect() { return 0; }
193 int InspSocket::OnIncomingConnection() { return 0; }
194 bool InspSocket::OnDataReady() { return true; }
195 void InspSocket::OnTimeout() { return; }
196 void InspSocket::OnClose() { return; }
197
198 InspSocket::~InspSocket()
199 {
200         this->Close();
201 }
202
203 /*
204 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
205 int OpenTCPSocket (void)
206 */