]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
Create StreamSocket for IO hooking implementation
[user/henk/code/inspircd.git] / src / socketengine.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 /*********        DEFAULTS       **********/
17
18 /* $ExtraSources: socketengines/socketengine_select.cpp */
19 /* $ExtraObjects: socketengine_select.o */
20
21 /* $If: USE_POLL */
22 /* $ExtraSources: socketengines/socketengine_poll.cpp */
23 /* $ExtraObjects: socketengine_poll.o */
24 /* $EndIf */
25
26 /* $If: USE_KQUEUE */
27 /* $ExtraSources: socketengines/socketengine_kqueue.cpp */
28 /* $ExtraObjects: socketengine_kqueue.o */
29 /* $EndIf */
30
31 /* $If: USE_EPOLL */
32 /* $ExtraSources: socketengines/socketengine_epoll.cpp */
33 /* $ExtraObjects: socketengine_epoll.o */
34 /* $EndIf */
35
36 /* $If: USE_PORTS */
37 /* $ExtraSources: socketengines/socketengine_ports.cpp */
38 /* $ExtraObjects: socketengine_ports.o */
39 /* $EndIf */
40
41 #include "inspircd.h"
42 #include "socketengine.h"
43
44 EventHandler::EventHandler()
45 {
46         fd = -1;
47 }
48
49 void EventHandler::SetFd(int FD)
50 {
51         this->fd = FD;
52 }
53
54 SocketEngine::SocketEngine()
55 {
56         TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
57         lastempty = ServerInstance->Time();
58         indata = outdata = 0;
59 }
60
61 SocketEngine::~SocketEngine()
62 {
63 }
64
65 bool SocketEngine::HasFd(int fd)
66 {
67         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
68                 return false;
69         return ref[fd];
70 }
71
72 EventHandler* SocketEngine::GetRef(int fd)
73 {
74         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
75                 return 0;
76         return ref[fd];
77 }
78
79 int SocketEngine::GetMaxFds()
80 {
81         return 0;
82 }
83
84 int SocketEngine::GetRemainingFds()
85 {
86         return 0;
87 }
88
89 int SocketEngine::DispatchEvents()
90 {
91         return 0;
92 }
93
94 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
95 {
96         if (!eh)
97                 return false;
98         if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
99                 return false;
100         return true;
101 }
102
103
104 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
105 {
106         return accept(fd->GetFd(), addr, addrlen);
107 }
108
109 int SocketEngine::Close(EventHandler* fd)
110 {
111 #ifdef WINDOWS
112         return closesocket(fd->GetFd());
113 #else
114         return close(fd->GetFd());
115 #endif
116 }
117
118 int SocketEngine::Close(int fd)
119 {
120 #ifdef WINDOWS
121         return closesocket(fd);
122 #else
123         return close(fd);
124 #endif
125 }
126
127 int SocketEngine::Blocking(int fd)
128 {
129 #ifdef WINDOWS
130         unsigned long opt = 0;
131         return ioctlsocket(fd, FIONBIO, &opt);
132 #else
133         int flags = fcntl(fd, F_GETFL, 0);
134         return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
135 #endif
136 }
137
138 int SocketEngine::NonBlocking(int fd)
139 {
140 #ifdef WINDOWS
141         unsigned long opt = 1;
142         return ioctlsocket(fd, FIONBIO, &opt);
143 #else
144         int flags = fcntl(fd, F_GETFL, 0);
145         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
146 #endif
147 }
148
149 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
150 {
151         return getsockname(fd->GetFd(), name, namelen);
152 }
153
154 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
155 {
156         this->UpdateStats(len, 0);
157         return recvfrom(fd->GetFd(), buf, len, flags, from, fromlen);
158 }
159
160 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
161 {
162         this->UpdateStats(0, len);
163         return send(fd->GetFd(), buf, len, flags);
164 }
165
166 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
167 {
168         this->UpdateStats(len, 0);
169         return recv(fd->GetFd(), buf, len, flags);
170 }
171
172 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
173 {
174         this->UpdateStats(0, len);
175         return sendto(fd->GetFd(), buf, len, flags, to, tolen);
176 }
177
178 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
179 {
180         return connect(fd->GetFd(), serv_addr, addrlen);
181 }
182
183 int SocketEngine::Shutdown(EventHandler* fd, int how)
184 {
185         return shutdown(fd->GetFd(), how);
186 }
187
188 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
189 {
190         return bind(fd, my_addr, addrlen);
191 }
192
193 int SocketEngine::Listen(int sockfd, int backlog)
194 {
195         return listen(sockfd, backlog);
196 }
197
198 int SocketEngine::Shutdown(int fd, int how)
199 {
200         return shutdown(fd, how);
201 }
202
203 void SocketEngine::RecoverFromFork()
204 {
205 }
206
207 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
208 {
209         if (lastempty != ServerInstance->Time())
210         {
211                 lastempty = ServerInstance->Time();
212                 indata = outdata = 0;
213         }
214         indata += len_in;
215         outdata += len_out;
216 }
217
218 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
219 {
220         UpdateStats(0, 0); /* Forces emptying of the values if its been more than a second */
221         float in_kbit = indata * 8;
222         float out_kbit = outdata * 8;
223         kbitpersec_total = ((in_kbit + out_kbit) / 1024);
224         kbitpersec_in = in_kbit / 1024;
225         kbitpersec_out = out_kbit / 1024;
226 }