1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* +------------------------------------+
15 * | Inspire Internet Relay Chat Daemon |
16 * +------------------------------------+
18 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
19 * See: http://wiki.inspircd.org/Credits
21 * This program is free but copyrighted software; see
22 * the file COPYING for details.
24 * ---------------------------------------------------
27 #ifndef __SOCKETENGINE_IOCP__
28 #define __SOCKETENGINE_IOCP__
30 #define READ_BUFFER_SIZE 600
33 #include "inspircd_config.h"
34 #include "inspircd_win32wrapper.h"
36 #include "socketengine.h"
38 /** Socket overlapped event types
43 SOCKET_IO_EVENT_READ_READY = 0,
45 SOCKET_IO_EVENT_WRITE_READY = 1,
47 SOCKET_IO_EVENT_ACCEPT = 2,
49 SOCKET_IO_EVENT_ERROR = 3,
50 /** Number of events */
51 NUM_SOCKET_IO_EVENTS = 4,
54 /** Represents a windows overlapped IO event
62 SocketIOEvent m_event;
65 unsigned __int64 m_params;
68 unsigned long m_params;
70 /** Create an overlapped event
72 Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
74 memset(&m_overlap, 0, sizeof(OVERLAPPED));
78 /** Specific to UDP sockets with overlapped IO
82 unsigned char udp_buffer[600];
83 unsigned long udp_len;
84 sockaddr udp_sockaddr[2];
85 unsigned long udp_sockaddr_len;
88 /** Specific to accepting sockets with overlapped IO
96 /** Implementation of SocketEngine that implements windows IO Completion Ports
98 class IOCPEngine : public SocketEngine
100 /** Creates a "fake" file descriptor for use with an IOCP socket.
101 * This is a little slow, but it isnt called too much. We'll fix it
102 * in a future release.
103 * @return -1 if there are no free slots, and an integer if it finds one.
105 __inline int GenerateFd(int RealFd)
107 int index_hash = RealFd % MAX_DESCRIPTORS;
108 if(ref[index_hash] == 0)
113 for(; i < MAX_DESCRIPTORS; ++i)
120 /** Global I/O completion port that sockets attach to.
122 HANDLE m_completionPort;
124 /** This is kinda shitty... :/ for getting an address from a real fd.
126 std::map<int, EventHandler*> m_binding;
130 LocalIntExt writeExt;
131 LocalIntExt acceptExt;
134 /** Holds the preallocated buffer passed to WSARecvFrom
135 * function. Yes, I know, it's a dirty hack.
137 udp_overlap * udp_ov;
139 /** Creates an IOCP Socket Engine
140 * @param Instance The creator of this object
144 /** Deletes an IOCP socket engine and all the attached sockets
148 /** Adds an event handler to the completion port, and sets up initial events.
149 * @param eh EventHandler to add
150 * @return True if success, false if no room
152 bool AddFd(EventHandler* eh, int event_mask);
154 /** Gets the maximum number of file descriptors that this engine can handle.
155 * @return The number of file descriptors
157 __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
159 /** Gets the number of free/remaining file descriptors under this engine.
160 * @return Remaining count
162 __inline int GetRemainingFds()
164 register int count = 0;
166 for(; i < MAX_DESCRIPTORS; ++i)
172 /** Removes a file descriptor from the set, preventing it from receiving any more events
173 * @return True if remove was successful, false otherwise
175 bool DelFd(EventHandler* eh, bool force = false);
177 /** Called every loop to handle input/output events for all sockets under this engine
178 * @return The number of "changed" sockets.
180 int DispatchEvents();
182 /** Gets the name of this socket engine as a string.
183 * @return string of socket engine name
185 std::string GetName();
187 void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
189 /** Posts a completion event on the specified socket.
190 * @param eh EventHandler for message
191 * @param type Event Type
192 * @param param Event Parameter
193 * @return True if added, false if not
195 bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
197 /** Posts a read event on the specified socket
198 * @param eh EventHandler (socket)
200 void PostReadEvent(EventHandler* eh);
202 /** Posts an accept event on the specified socket
203 * @param eh EventHandler (socket)
205 void PostAcceptEvent(EventHandler* eh);
207 /** Returns the EventHandler attached to a specific fd.
208 * If the fd isnt in the socketengine, returns NULL.
209 * @param fd The event handler to look for
210 * @return A pointer to the event handler, or NULL
212 EventHandler* GetRef(int fd);
214 /** Returns true if a file descriptor exists in
215 * the socket engine's list.
216 * @param fd The event handler to look for
217 * @return True if this fd has an event handler
221 /** Returns the EventHandler attached to a specific fd.
222 * If the fd isnt in the socketengine, returns NULL.
223 * @param fd The event handler to look for
224 * @return A pointer to the event handler, or NULL
226 EventHandler* GetIntRef(int fd);
228 bool BoundsCheckFd(EventHandler* eh);
230 virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
232 virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
234 virtual int Blocking(int fd);
236 virtual int NonBlocking(int fd);
238 virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen);
240 virtual int Close(int fd);
242 virtual int Close(EventHandler* fd);
247 #include "exitcodes.h"
250 IOCPEngine::IOCPEngine()
251 : fdExt("internal_fd", NULL),
252 readExt("windows_readevent", NULL),
253 writeExt("windows_writeevent", NULL),
254 acceptExt("windows_acceptevent", NULL)
256 MAX_DESCRIPTORS = 10240;
258 /* Create completion port */
259 m_completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)0, 0);
261 if (!m_completionPort)
263 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
264 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
265 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
266 printf("ERROR: this is a fatal error, exiting now.\n");
267 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
270 /* Null variables out. */
272 MAX_DESCRIPTORS = 10240;
273 ref = new EventHandler* [10240];
274 memset(ref, 0, sizeof(EventHandler*) * MAX_DESCRIPTORS);
277 IOCPEngine::~IOCPEngine()
279 /* Clean up winsock and close completion port */
280 CloseHandle(m_completionPort);
285 bool IOCPEngine::AddFd(EventHandler* eh, int event_mask)
287 /* Does it at least look valid? */
291 int* fake_fd = new int(GenerateFd(eh->GetFd()));
293 int opt_len = sizeof(int);
296 if ((*fake_fd < 0) || (*fake_fd > MAX_DESCRIPTORS))
302 /* Already an entry here */
309 /* are we a listen socket? */
310 getsockopt(eh->GetFd(), SOL_SOCKET, SO_ACCEPTCONN, (char*)&is_accept, &opt_len);
312 /* set up the read event so the socket can actually receive data :P */
313 fdExt.set(eh, *fake_fd);
315 unsigned long completion_key = (ULONG_PTR)*fake_fd;
316 /* assign the socket to the completion port */
317 if (!CreateIoCompletionPort((HANDLE)eh->GetFd(), m_completionPort, completion_key, 0))
320 /* setup initial events */
327 ServerInstance->Logs->Log("SOCKET",DEBUG, "New fake fd: %u, real fd: %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
329 /* post a write event if there is data to be written */
330 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
331 OnSetEvent(eh, event_mask, event_mask);
333 /* we're all good =) */
336 m_binding.insert( std::map<int, EventHandler*>::value_type( eh->GetFd(), eh ) );
340 /* Ohshi-, map::insert failed :/ */
345 SocketEngine::SetEventMask(eh, event_mask);
351 bool IOCPEngine::DelFd(EventHandler* eh, bool force /* = false */)
356 int* fake_fd = (int*)fdExt.get(eh);
361 int fd = eh->GetFd();
363 void* m_readEvent = (void*)readExt.get(eh);
364 void* m_writeEvent = (void*)writeExt.get(eh);
365 void* m_acceptEvent = (void*)acceptExt.get(eh);
367 ServerInstance->Logs->Log("SOCKET",DEBUG, "Removing fake fd %u, real fd %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
369 /* Cancel pending i/o operations. */
370 if (CancelIo((HANDLE)fd) == FALSE)
373 /* Free the buffer, and delete the event. */
376 if(((Overlapped*)m_readEvent)->m_params != 0)
377 delete ((udp_overlap*)((Overlapped*)m_readEvent)->m_params);
379 delete ((Overlapped*)m_readEvent);
385 delete ((Overlapped*)m_writeEvent);
391 delete ((accept_overlap*)((Overlapped*)m_acceptEvent)->m_params);
392 delete ((Overlapped*)m_acceptEvent);
398 m_binding.erase(eh->GetFd());
403 /* decrement set size */
410 void IOCPEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
415 void* m_writeEvent = (void*)writeExt.get(eh);
417 int* fake_fd = (int*)fdExt.get(eh);
421 /* Post event - write begin */
422 if((new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !m_writeEvent)
424 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
425 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_WRITE_READY, 0);
427 writeExt.set(eh, (intptr_t)ov);
428 PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
432 bool IOCPEngine::PostCompletionEvent(EventHandler * eh, SocketIOEvent type, int param)
437 int* fake_fd = (int*)fdExt.get(eh);
441 Overlapped * ov = new Overlapped(type, param);
442 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
443 return PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
446 void IOCPEngine::PostReadEvent(EventHandler * eh)
451 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_READ_READY, 0);
456 /* by passing a null buffer pointer, we can have this working in the same way as epoll..
457 * its slower, but it saves modifying all network code.
462 /* determine socket type. */
464 int sock_len = sizeof(DWORD);
465 if(getsockopt(eh->GetFd(), SOL_SOCKET, SO_TYPE, (char*)&sock_type, &sock_len) == -1)
468 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
474 case SOCK_DGRAM: /* UDP Socket */
476 udp_overlap * uv = new udp_overlap;
477 uv->udp_sockaddr_len = sizeof(sockaddr);
478 buf.buf = (char*)uv->udp_buffer;
479 buf.len = sizeof(uv->udp_buffer);
480 ov->m_params = (unsigned long)uv;
481 if(WSARecvFrom(eh->GetFd(), &buf, 1, &uv->udp_len, &flags, uv->udp_sockaddr, (LPINT)&uv->udp_sockaddr_len, &ov->m_overlap, 0))
483 int err = WSAGetLastError();
484 if(err != WSA_IO_PENDING)
487 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
494 case SOCK_STREAM: /* TCP Socket */
496 if(WSARecv(eh->GetFd(), &buf, 1, &r_length, &flags, &ov->m_overlap, 0) == SOCKET_ERROR)
498 if(WSAGetLastError() != WSA_IO_PENDING)
501 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
510 printf("unknwon socket type: %u\n", sock_type);
515 readExt.set(eh, (intptr_t)ov);
518 int IOCPEngine::DispatchEvents()
521 LPOVERLAPPED overlap;
526 unsigned long bytes_recv;
528 while (GetQueuedCompletionStatus(m_completionPort, &len, &intfd, &overlap, 1000))
530 if (intfd > (unsigned long)MAX_DESCRIPTORS)
533 // woot, we got an event on a socket :P
535 ov = CONTAINING_RECORD(overlap, Overlapped, m_overlap);
544 case SOCKET_IO_EVENT_WRITE_READY:
548 SetEventMask(eh, eh->GetEventMask() & ~FD_WRITE_WILL_BLOCK);
549 eh->HandleEvent(EVENT_WRITE, 0);
553 case SOCKET_IO_EVENT_READ_READY:
556 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
559 // if we had params, it means we are a udp socket with a udp_overlap pointer in this long.
560 udp_overlap * uv = (udp_overlap*)ov->m_params;
564 eh->HandleEvent(EVENT_READ, 0);
571 ret = ioctlsocket(eh->GetFd(), FIONREAD, &bytes_recv);
573 if(ret != 0 || bytes_recv == 0)
576 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, EIO); /* Old macdonald had an error, EIEIO. */
580 eh->HandleEvent(EVENT_READ, 0);
587 case SOCKET_IO_EVENT_ACCEPT:
589 /* this is kinda messy.. :/ */
591 eh->HandleEvent(EVENT_READ, ov->m_params);
592 delete ((accept_overlap*)ov->m_params);
598 case SOCKET_IO_EVENT_ERROR:
601 eh->HandleEvent(EVENT_ERROR, ov->m_params);
612 void IOCPEngine::PostAcceptEvent(EventHandler * eh)
619 struct linger linger = { 0 };
621 int fd = WSASocket(AF_INET, SOCK_STREAM, 0, 0, 0, WSA_FLAG_OVERLAPPED);
623 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
624 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
627 setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
628 ioctlsocket(fd, FIONBIO, &arg);
630 int len = sizeof(sockaddr_in) + 16;
632 accept_overlap* ao = new accept_overlap;
633 memset(ao->buf, 0, 1024);
636 Overlapped* ov = new Overlapped(SOCKET_IO_EVENT_ACCEPT, (int)ao);
637 acceptExt.set(eh, (intptr_t)ov);
639 if(AcceptEx(eh->GetFd(), fd, ao->buf, 0, len, len, &dwBytes, &ov->m_overlap) == FALSE)
641 int err = WSAGetLastError();
642 if(err != WSA_IO_PENDING)
644 printf("PostAcceptEvent err: %d\n", err);
650 std::string IOCPEngine::GetName()
655 EventHandler * IOCPEngine::GetRef(int fd)
657 std::map<int, EventHandler*>::iterator itr = m_binding.find(fd);
658 return (itr == m_binding.end()) ? 0 : itr->second;
661 bool IOCPEngine::HasFd(int fd)
663 return (GetRef(fd) != 0);
666 bool IOCPEngine::BoundsCheckFd(EventHandler* eh)
668 int* internal_fd = (int*)fdExt.get(eh);
669 if (!eh || eh->GetFd() < 0)
675 if(*internal_fd > MAX_DESCRIPTORS)
681 EventHandler * IOCPEngine::GetIntRef(int fd)
683 if(fd < 0 || fd > MAX_DESCRIPTORS)
688 int IOCPEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
690 //SOCKET s = fd->GetFd();
692 Overlapped* acceptevent = (Overlapped*)acceptExt.get(fd);
694 /* Shit, no accept event on this socket! :( */
697 Overlapped* ovl = acceptevent;
698 accept_overlap* ov = (accept_overlap*)ovl->m_params;
700 //sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
701 sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
703 memcpy(addr, client_address, sizeof(sockaddr_in));
704 *addrlen = sizeof(sockaddr_in);
709 int IOCPEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
711 Overlapped* ovl = (Overlapped*)acceptExt.get(fd);
716 accept_overlap* ov = (accept_overlap*)ovl->m_params;
718 sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
719 //sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
721 memcpy(name, server_address, sizeof(sockaddr_in));
722 *namelen = sizeof(sockaddr_in);
727 int IOCPEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
729 this->UpdateStats(len, 0);
730 udp_overlap* ov = (udp_overlap*)readExt.get(fd);
733 memcpy(buf, ov->udp_buffer, ov->udp_len);
734 memcpy(from, ov->udp_sockaddr, *fromlen);
738 int IOCPEngine::Blocking(int fd)
740 unsigned long opt = 0;
741 return ioctlsocket(fd, FIONBIO, &opt);
744 int IOCPEngine::NonBlocking(int fd)
746 unsigned long opt = 1;
747 return ioctlsocket(fd, FIONBIO, &opt);
750 int IOCPEngine::Close(int fd)
752 return closesocket(fd);
755 int IOCPEngine::Close(EventHandler* fd)
757 return this->Close(fd->GetFd());
760 SocketEngine* CreateSocketEngine()
762 return new IOCPEngine;