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;
129 /** Holds the preallocated buffer passed to WSARecvFrom
130 * function. Yes, I know, it's a dirty hack.
132 udp_overlap * udp_ov;
134 /** Creates an IOCP Socket Engine
135 * @param Instance The creator of this object
139 /** Deletes an IOCP socket engine and all the attached sockets
143 /** Adds an event handler to the completion port, and sets up initial events.
144 * @param eh EventHandler to add
145 * @return True if success, false if no room
147 bool AddFd(EventHandler* eh, int event_mask);
149 /** Gets the maximum number of file descriptors that this engine can handle.
150 * @return The number of file descriptors
152 __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
154 /** Gets the number of free/remaining file descriptors under this engine.
155 * @return Remaining count
157 __inline int GetRemainingFds()
159 register int count = 0;
161 for(; i < MAX_DESCRIPTORS; ++i)
167 /** Removes a file descriptor from the set, preventing it from receiving any more events
168 * @return True if remove was successful, false otherwise
170 bool DelFd(EventHandler* eh, bool force = false);
172 /** Called every loop to handle input/output events for all sockets under this engine
173 * @return The number of "changed" sockets.
175 int DispatchEvents();
177 /** Gets the name of this socket engine as a string.
178 * @return string of socket engine name
180 std::string GetName();
182 void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
184 /** Posts a completion event on the specified socket.
185 * @param eh EventHandler for message
186 * @param type Event Type
187 * @param param Event Parameter
188 * @return True if added, false if not
190 bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
192 /** Posts a read event on the specified socket
193 * @param eh EventHandler (socket)
195 void PostReadEvent(EventHandler* eh);
197 /** Posts an accept event on the specified socket
198 * @param eh EventHandler (socket)
200 void PostAcceptEvent(EventHandler* eh);
202 /** Returns the EventHandler attached to a specific fd.
203 * If the fd isnt in the socketengine, returns NULL.
204 * @param fd The event handler to look for
205 * @return A pointer to the event handler, or NULL
207 EventHandler* GetRef(int fd);
209 /** Returns true if a file descriptor exists in
210 * the socket engine's list.
211 * @param fd The event handler to look for
212 * @return True if this fd has an event handler
216 /** Returns the EventHandler attached to a specific fd.
217 * If the fd isnt in the socketengine, returns NULL.
218 * @param fd The event handler to look for
219 * @return A pointer to the event handler, or NULL
221 EventHandler* GetIntRef(int fd);
223 bool BoundsCheckFd(EventHandler* eh);
225 virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
227 virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
229 virtual int Blocking(int fd);
231 virtual int NonBlocking(int fd);
233 virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen);
235 virtual int Close(int fd);
237 virtual int Close(EventHandler* fd);
242 #include "exitcodes.h"
245 IOCPEngine::IOCPEngine()
247 MAX_DESCRIPTORS = 10240;
249 /* Create completion port */
250 m_completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)0, 0);
252 if (!m_completionPort)
254 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
255 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
256 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
257 printf("ERROR: this is a fatal error, exiting now.\n");
258 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
261 /* Null variables out. */
263 MAX_DESCRIPTORS = 10240;
264 ref = new EventHandler* [10240];
265 memset(ref, 0, sizeof(EventHandler*) * MAX_DESCRIPTORS);
268 IOCPEngine::~IOCPEngine()
270 /* Clean up winsock and close completion port */
271 CloseHandle(m_completionPort);
276 bool IOCPEngine::AddFd(EventHandler* eh, int event_mask)
278 /* Does it at least look valid? */
282 int* fake_fd = new int(GenerateFd(eh->GetFd()));
284 int opt_len = sizeof(int);
287 if ((*fake_fd < 0) || (*fake_fd > MAX_DESCRIPTORS))
293 /* Already an entry here */
300 /* are we a listen socket? */
301 getsockopt(eh->GetFd(), SOL_SOCKET, SO_ACCEPTCONN, (char*)&is_accept, &opt_len);
303 /* set up the read event so the socket can actually receive data :P */
304 eh->Extend("internal_fd", fake_fd);
306 unsigned long completion_key = (ULONG_PTR)*fake_fd;
307 /* assign the socket to the completion port */
308 if (!CreateIoCompletionPort((HANDLE)eh->GetFd(), m_completionPort, completion_key, 0))
311 /* setup initial events */
318 ServerInstance->Logs->Log("SOCKET",DEBUG, "New fake fd: %u, real fd: %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
320 /* post a write event if there is data to be written */
321 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
324 /* we're all good =) */
327 m_binding.insert( std::map<int, EventHandler*>::value_type( eh->GetFd(), eh ) );
331 /* Ohshi-, map::insert failed :/ */
336 SocketEngine::SetEventMask(eh, event_mask);
342 bool IOCPEngine::DelFd(EventHandler* eh, bool force /* = false */)
349 if (!eh->GetExt("internal_fd", fake_fd))
352 int fd = eh->GetFd();
354 void* m_readEvent = NULL;
355 void* m_writeEvent = NULL;
356 void* m_acceptEvent = NULL;
358 ServerInstance->Logs->Log("SOCKET",DEBUG, "Removing fake fd %u, real fd %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
360 /* Cancel pending i/o operations. */
361 if (CancelIo((HANDLE)fd) == FALSE)
364 /* Free the buffer, and delete the event. */
365 if (eh->GetExt("windows_readevent", m_readEvent))
367 if(((Overlapped*)m_readEvent)->m_params != 0)
368 delete ((udp_overlap*)((Overlapped*)m_readEvent)->m_params);
370 delete ((Overlapped*)m_readEvent);
371 eh->Shrink("windows_readevent");
374 if(eh->GetExt("windows_writeevent", m_writeEvent))
376 delete ((Overlapped*)m_writeEvent);
377 eh->Shrink("windows_writeevent");
380 if(eh->GetExt("windows_acceptevent", m_acceptEvent))
382 delete ((accept_overlap*)((Overlapped*)m_acceptEvent)->m_params);
383 delete ((Overlapped*)m_acceptEvent);
384 eh->Shrink("windows_accepevent");
389 m_binding.erase(eh->GetFd());
392 eh->Shrink("internal_fd");
394 /* decrement set size */
401 void IOCPEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
406 void* m_writeEvent = NULL;
409 if (!eh->GetExt("internal_fd", fake_fd))
412 /* Post event - write begin */
413 if((new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !eh->GetExt("windows_writeevent", m_writeEvent))
415 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
416 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_WRITE_READY, 0);
417 eh->Shrink("windows_writeevent");
418 eh->Extend("windows_writeevent",ov);
419 PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
423 bool IOCPEngine::PostCompletionEvent(EventHandler * eh, SocketIOEvent type, int param)
429 if (!eh->GetExt("internal_fd", fake_fd))
432 Overlapped * ov = new Overlapped(type, param);
433 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
434 return PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
437 void IOCPEngine::PostReadEvent(EventHandler * eh)
442 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_READ_READY, 0);
447 /* by passing a null buffer pointer, we can have this working in the same way as epoll..
448 * its slower, but it saves modifying all network code.
453 /* determine socket type. */
455 int sock_len = sizeof(DWORD);
456 if(getsockopt(eh->GetFd(), SOL_SOCKET, SO_TYPE, (char*)&sock_type, &sock_len) == -1)
459 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
465 case SOCK_DGRAM: /* UDP Socket */
467 udp_overlap * uv = new udp_overlap;
468 uv->udp_sockaddr_len = sizeof(sockaddr);
469 buf.buf = (char*)uv->udp_buffer;
470 buf.len = sizeof(uv->udp_buffer);
471 ov->m_params = (unsigned long)uv;
472 if(WSARecvFrom(eh->GetFd(), &buf, 1, &uv->udp_len, &flags, uv->udp_sockaddr, (LPINT)&uv->udp_sockaddr_len, &ov->m_overlap, 0))
474 int err = WSAGetLastError();
475 if(err != WSA_IO_PENDING)
478 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
485 case SOCK_STREAM: /* TCP Socket */
487 if(WSARecv(eh->GetFd(), &buf, 1, &r_length, &flags, &ov->m_overlap, 0) == SOCKET_ERROR)
489 if(WSAGetLastError() != WSA_IO_PENDING)
492 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
501 printf("unknwon socket type: %u\n", sock_type);
506 eh->Extend("windows_readevent", ov);
509 int IOCPEngine::DispatchEvents()
512 LPOVERLAPPED overlap;
517 unsigned long bytes_recv;
519 while (GetQueuedCompletionStatus(m_completionPort, &len, &intfd, &overlap, 1000))
521 if (intfd > (unsigned long)MAX_DESCRIPTORS)
524 // woot, we got an event on a socket :P
526 ov = CONTAINING_RECORD(overlap, Overlapped, m_overlap);
531 void* m_readEvent = NULL;
532 void* m_writeEvent = NULL;
534 eh->GetExt("windows_readevent", m_readEvent);
535 eh->GetExt("windows_writeevent", m_writeEvent);
541 case SOCKET_IO_EVENT_WRITE_READY:
544 eh->Shrink("windows_writeevent");
545 SetEventMask(eh, eh->GetEventMask() & ~FD_WRITE_WILL_BLOCK);
546 eh->HandleEvent(EVENT_WRITE, 0);
550 case SOCKET_IO_EVENT_READ_READY:
553 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
556 // if we had params, it means we are a udp socket with a udp_overlap pointer in this long.
557 udp_overlap * uv = (udp_overlap*)ov->m_params;
560 eh->Shrink("windows_readevent");
561 eh->HandleEvent(EVENT_READ, 0);
568 ret = ioctlsocket(eh->GetFd(), FIONREAD, &bytes_recv);
569 eh->Shrink("windows_readevent");
570 if(ret != 0 || bytes_recv == 0)
573 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, EIO); /* Old macdonald had an error, EIEIO. */
577 eh->HandleEvent(EVENT_READ, 0);
584 case SOCKET_IO_EVENT_ACCEPT:
586 /* this is kinda messy.. :/ */
588 eh->HandleEvent(EVENT_READ, ov->m_params);
589 delete ((accept_overlap*)ov->m_params);
590 eh->Shrink("windows_acceptevent");
595 case SOCKET_IO_EVENT_ERROR:
598 eh->HandleEvent(EVENT_ERROR, ov->m_params);
609 void IOCPEngine::PostAcceptEvent(EventHandler * eh)
616 struct linger linger = { 0 };
618 int fd = WSASocket(AF_INET, SOCK_STREAM, 0, 0, 0, WSA_FLAG_OVERLAPPED);
620 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
621 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
624 setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
625 ioctlsocket(fd, FIONBIO, &arg);
627 int len = sizeof(sockaddr_in) + 16;
629 accept_overlap* ao = new accept_overlap;
630 memset(ao->buf, 0, 1024);
633 Overlapped* ov = new Overlapped(SOCKET_IO_EVENT_ACCEPT, (int)ao);
634 eh->Extend("windows_acceptevent", ov);
636 if(AcceptEx(eh->GetFd(), fd, ao->buf, 0, len, len, &dwBytes, &ov->m_overlap) == FALSE)
638 int err = WSAGetLastError();
639 if(err != WSA_IO_PENDING)
641 printf("PostAcceptEvent err: %d\n", err);
647 std::string IOCPEngine::GetName()
652 EventHandler * IOCPEngine::GetRef(int fd)
654 std::map<int, EventHandler*>::iterator itr = m_binding.find(fd);
655 return (itr == m_binding.end()) ? 0 : itr->second;
658 bool IOCPEngine::HasFd(int fd)
660 return (GetRef(fd) != 0);
663 bool IOCPEngine::BoundsCheckFd(EventHandler* eh)
666 if (!eh || eh->GetFd() < 0)
669 if(!eh->GetExt("internal_fd", internal_fd))
672 if(*internal_fd > MAX_DESCRIPTORS)
678 EventHandler * IOCPEngine::GetIntRef(int fd)
680 if(fd < 0 || fd > MAX_DESCRIPTORS)
685 int IOCPEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
687 //SOCKET s = fd->GetFd();
689 Overlapped* acceptevent = NULL;
690 if (!fd->GetExt("windows_acceptevent", acceptevent))
691 /* Shit, no accept event on this socket! :( */
694 Overlapped* ovl = acceptevent;
695 accept_overlap* ov = (accept_overlap*)ovl->m_params;
697 //sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
698 sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
700 memcpy(addr, client_address, sizeof(sockaddr_in));
701 *addrlen = sizeof(sockaddr_in);
706 int IOCPEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
708 Overlapped* ovl = NULL;
710 if (!fd->GetExt("windows_acceptevent", ovl))
713 accept_overlap* ov = (accept_overlap*)ovl->m_params;
715 sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
716 //sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
718 memcpy(name, server_address, sizeof(sockaddr_in));
719 *namelen = sizeof(sockaddr_in);
724 int IOCPEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
726 this->UpdateStats(len, 0);
727 udp_overlap * ov = NULL;
728 if (!fd->GetExt("windows_readevent", ov))
730 memcpy(buf, ov->udp_buffer, ov->udp_len);
731 memcpy(from, ov->udp_sockaddr, *fromlen);
735 int IOCPEngine::Blocking(int fd)
737 unsigned long opt = 0;
738 return ioctlsocket(fd, FIONBIO, &opt);
741 int IOCPEngine::NonBlocking(int fd)
743 unsigned long opt = 1;
744 return ioctlsocket(fd, FIONBIO, &opt);
747 int IOCPEngine::Close(int fd)
749 return closesocket(fd);
752 int IOCPEngine::Close(EventHandler* fd)
754 return this->Close(fd->GetFd());
757 SocketEngine* CreateSocketEngine()
759 return new IOCPEngine;