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 #include "inspircd_config.h"
15 #ifdef CONFIG_USE_IOCP
17 #ifndef __SOCKETENGINE_IOCP__
18 #define __SOCKETENGINE_IOCP__
20 #include "inspircd_win32wrapper.h"
22 #include "socketengine.h"
24 #define READ_BUFFER_SIZE 600
27 /** Socket overlapped event types
32 SOCKET_IO_EVENT_READ_READY = 0,
34 SOCKET_IO_EVENT_WRITE_READY = 1,
36 SOCKET_IO_EVENT_ACCEPT = 2,
38 SOCKET_IO_EVENT_ERROR = 3,
39 /** Number of events */
40 NUM_SOCKET_IO_EVENTS = 4,
43 /** Represents a windows overlapped IO event
51 SocketIOEvent m_event;
54 unsigned __int64 m_params;
57 unsigned long m_params;
59 /** Create an overlapped event
61 Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
63 memset(&m_overlap, 0, sizeof(OVERLAPPED));
67 /** Specific to UDP sockets with overlapped IO
71 unsigned char udp_buffer[600];
72 unsigned long udp_len;
73 sockaddr udp_sockaddr[2];
74 unsigned long udp_sockaddr_len;
77 /** Specific to accepting sockets with overlapped IO
85 /** Implementation of SocketEngine that implements windows IO Completion Ports
87 class IOCPEngine : public SocketEngine
89 /** Creates a "fake" file descriptor for use with an IOCP socket.
90 * This is a little slow, but it isnt called too much. We'll fix it
91 * in a future release.
92 * @return -1 if there are no free slots, and an integer if it finds one.
94 __inline int GenerateFd(int RealFd)
96 int index_hash = RealFd % MAX_DESCRIPTORS;
97 if(ref[index_hash] == 0)
102 for(; i < MAX_DESCRIPTORS; ++i)
109 /** Global I/O completion port that sockets attach to.
111 HANDLE m_completionPort;
113 /** This is kinda shitty... :/ for getting an address from a real fd.
115 std::map<int, EventHandler*> m_binding;
119 LocalIntExt writeExt;
120 LocalIntExt acceptExt;
123 /** Holds the preallocated buffer passed to WSARecvFrom
124 * function. Yes, I know, it's a dirty hack.
126 udp_overlap * udp_ov;
128 /** Creates an IOCP Socket Engine
129 * @param Instance The creator of this object
133 /** Deletes an IOCP socket engine and all the attached sockets
137 /** Adds an event handler to the completion port, and sets up initial events.
138 * @param eh EventHandler to add
139 * @return True if success, false if no room
141 bool AddFd(EventHandler* eh, int event_mask);
143 /** Gets the maximum number of file descriptors that this engine can handle.
144 * @return The number of file descriptors
146 __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
148 /** Gets the number of free/remaining file descriptors under this engine.
149 * @return Remaining count
151 __inline int GetRemainingFds()
153 register int count = 0;
155 for(; i < MAX_DESCRIPTORS; ++i)
161 /** Removes a file descriptor from the set, preventing it from receiving any more events
162 * @return True if remove was successful, false otherwise
164 bool DelFd(EventHandler* eh, bool force = false);
166 /** Called every loop to handle input/output events for all sockets under this engine
167 * @return The number of "changed" sockets.
169 int DispatchEvents();
171 /** Gets the name of this socket engine as a string.
172 * @return string of socket engine name
174 std::string GetName();
176 void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
178 /** Posts a completion event on the specified socket.
179 * @param eh EventHandler for message
180 * @param type Event Type
181 * @param param Event Parameter
182 * @return True if added, false if not
184 bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
186 /** Posts a read event on the specified socket
187 * @param eh EventHandler (socket)
189 void PostReadEvent(EventHandler* eh);
191 /** Posts an accept event on the specified socket
192 * @param eh EventHandler (socket)
194 void PostAcceptEvent(EventHandler* eh);
196 /** Returns the EventHandler attached to a specific fd.
197 * If the fd isnt in the socketengine, returns NULL.
198 * @param fd The event handler to look for
199 * @return A pointer to the event handler, or NULL
201 EventHandler* GetRef(int fd);
203 /** Returns true if a file descriptor exists in
204 * the socket engine's list.
205 * @param fd The event handler to look for
206 * @return True if this fd has an event handler
210 /** Returns the EventHandler attached to a specific fd.
211 * If the fd isnt in the socketengine, returns NULL.
212 * @param fd The event handler to look for
213 * @return A pointer to the event handler, or NULL
215 EventHandler* GetIntRef(int fd);
217 bool BoundsCheckFd(EventHandler* eh);
219 virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
221 virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
223 virtual int Blocking(int fd);
225 virtual int NonBlocking(int fd);
227 virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen);
229 virtual int Close(int fd);
231 virtual int Close(EventHandler* fd);
236 #include "exitcodes.h"
239 IOCPEngine::IOCPEngine()
240 : fdExt("internal_fd", NULL),
241 readExt("windows_readevent", NULL),
242 writeExt("windows_writeevent", NULL),
243 acceptExt("windows_acceptevent", NULL)
245 MAX_DESCRIPTORS = 10240;
247 /* Create completion port */
248 m_completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)0, 0);
250 if (!m_completionPort)
252 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
253 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
254 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
255 printf("ERROR: this is a fatal error, exiting now.\n");
256 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
259 /* Null variables out. */
261 MAX_DESCRIPTORS = 10240;
262 ref = new EventHandler* [10240];
263 memset(ref, 0, sizeof(EventHandler*) * MAX_DESCRIPTORS);
266 IOCPEngine::~IOCPEngine()
268 /* Clean up winsock and close completion port */
269 CloseHandle(m_completionPort);
274 bool IOCPEngine::AddFd(EventHandler* eh, int event_mask)
276 /* Does it at least look valid? */
280 int* fake_fd = new int(GenerateFd(eh->GetFd()));
282 int opt_len = sizeof(int);
285 if ((*fake_fd < 0) || (*fake_fd > MAX_DESCRIPTORS))
291 /* Already an entry here */
298 /* are we a listen socket? */
299 getsockopt(eh->GetFd(), SOL_SOCKET, SO_ACCEPTCONN, (char*)&is_accept, &opt_len);
301 /* set up the read event so the socket can actually receive data :P */
302 fdExt.set(eh, *fake_fd);
304 unsigned long completion_key = (ULONG_PTR)*fake_fd;
305 /* assign the socket to the completion port */
306 if (!CreateIoCompletionPort((HANDLE)eh->GetFd(), m_completionPort, completion_key, 0))
309 /* setup initial events */
316 ServerInstance->Logs->Log("SOCKET",DEBUG, "New fake fd: %u, real fd: %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
318 /* post a write event if there is data to be written */
319 if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
320 OnSetEvent(eh, event_mask, event_mask);
322 /* we're all good =) */
325 m_binding.insert( std::map<int, EventHandler*>::value_type( eh->GetFd(), eh ) );
329 /* Ohshi-, map::insert failed :/ */
334 SocketEngine::SetEventMask(eh, event_mask);
340 bool IOCPEngine::DelFd(EventHandler* eh, bool force /* = false */)
345 int* fake_fd = (int*)fdExt.get(eh);
350 int fd = eh->GetFd();
352 void* m_readEvent = (void*)readExt.get(eh);
353 void* m_writeEvent = (void*)writeExt.get(eh);
354 void* m_acceptEvent = (void*)acceptExt.get(eh);
356 ServerInstance->Logs->Log("SOCKET",DEBUG, "Removing fake fd %u, real fd %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
358 /* Cancel pending i/o operations. */
359 if (CancelIo((HANDLE)fd) == FALSE)
362 /* Free the buffer, and delete the event. */
365 if(((Overlapped*)m_readEvent)->m_params != 0)
366 delete ((udp_overlap*)((Overlapped*)m_readEvent)->m_params);
368 delete ((Overlapped*)m_readEvent);
374 delete ((Overlapped*)m_writeEvent);
380 delete ((accept_overlap*)((Overlapped*)m_acceptEvent)->m_params);
381 delete ((Overlapped*)m_acceptEvent);
387 m_binding.erase(eh->GetFd());
392 /* decrement set size */
399 void IOCPEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
404 void* m_writeEvent = (void*)writeExt.get(eh);
406 int* fake_fd = (int*)fdExt.get(eh);
410 /* Post event - write begin */
411 if((new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !m_writeEvent)
413 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
414 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_WRITE_READY, 0);
416 writeExt.set(eh, (intptr_t)ov);
417 PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
421 bool IOCPEngine::PostCompletionEvent(EventHandler * eh, SocketIOEvent type, int param)
426 int* fake_fd = (int*)fdExt.get(eh);
430 Overlapped * ov = new Overlapped(type, param);
431 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
432 return PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
435 void IOCPEngine::PostReadEvent(EventHandler * eh)
440 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_READ_READY, 0);
445 /* by passing a null buffer pointer, we can have this working in the same way as epoll..
446 * its slower, but it saves modifying all network code.
451 /* determine socket type. */
453 int sock_len = sizeof(DWORD);
454 if(getsockopt(eh->GetFd(), SOL_SOCKET, SO_TYPE, (char*)&sock_type, &sock_len) == -1)
457 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
463 case SOCK_DGRAM: /* UDP Socket */
465 udp_overlap * uv = new udp_overlap;
466 uv->udp_sockaddr_len = sizeof(sockaddr);
467 buf.buf = (char*)uv->udp_buffer;
468 buf.len = sizeof(uv->udp_buffer);
469 ov->m_params = (unsigned long)uv;
470 if(WSARecvFrom(eh->GetFd(), &buf, 1, &uv->udp_len, &flags, uv->udp_sockaddr, (LPINT)&uv->udp_sockaddr_len, &ov->m_overlap, 0))
472 int err = WSAGetLastError();
473 if(err != WSA_IO_PENDING)
476 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
483 case SOCK_STREAM: /* TCP Socket */
485 if(WSARecv(eh->GetFd(), &buf, 1, &r_length, &flags, &ov->m_overlap, 0) == SOCKET_ERROR)
487 if(WSAGetLastError() != WSA_IO_PENDING)
490 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
499 printf("unknwon socket type: %u\n", sock_type);
504 readExt.set(eh, (intptr_t)ov);
507 int IOCPEngine::DispatchEvents()
510 LPOVERLAPPED overlap;
515 unsigned long bytes_recv;
517 while (GetQueuedCompletionStatus(m_completionPort, &len, &intfd, &overlap, 1000))
519 if (intfd > (unsigned long)MAX_DESCRIPTORS)
522 // woot, we got an event on a socket :P
524 ov = CONTAINING_RECORD(overlap, Overlapped, m_overlap);
533 case SOCKET_IO_EVENT_WRITE_READY:
537 SetEventMask(eh, eh->GetEventMask() & ~FD_WRITE_WILL_BLOCK);
538 eh->HandleEvent(EVENT_WRITE, 0);
542 case SOCKET_IO_EVENT_READ_READY:
545 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
548 // if we had params, it means we are a udp socket with a udp_overlap pointer in this long.
549 udp_overlap * uv = (udp_overlap*)ov->m_params;
553 eh->HandleEvent(EVENT_READ, 0);
560 ret = ioctlsocket(eh->GetFd(), FIONREAD, &bytes_recv);
562 if(ret != 0 || bytes_recv == 0)
565 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, EIO); /* Old macdonald had an error, EIEIO. */
569 eh->HandleEvent(EVENT_READ, 0);
576 case SOCKET_IO_EVENT_ACCEPT:
578 /* this is kinda messy.. :/ */
580 eh->HandleEvent(EVENT_READ, ov->m_params);
581 delete ((accept_overlap*)ov->m_params);
587 case SOCKET_IO_EVENT_ERROR:
590 eh->HandleEvent(EVENT_ERROR, ov->m_params);
601 void IOCPEngine::PostAcceptEvent(EventHandler * eh)
608 struct linger linger = { 0 };
610 int fd = WSASocket(AF_INET, SOCK_STREAM, 0, 0, 0, WSA_FLAG_OVERLAPPED);
612 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
613 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
616 setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
617 ioctlsocket(fd, FIONBIO, &arg);
619 int len = sizeof(sockaddr_in) + 16;
621 accept_overlap* ao = new accept_overlap;
622 memset(ao->buf, 0, 1024);
625 Overlapped* ov = new Overlapped(SOCKET_IO_EVENT_ACCEPT, (int)ao);
626 acceptExt.set(eh, (intptr_t)ov);
628 if(AcceptEx(eh->GetFd(), fd, ao->buf, 0, len, len, &dwBytes, &ov->m_overlap) == FALSE)
630 int err = WSAGetLastError();
631 if(err != WSA_IO_PENDING)
633 printf("PostAcceptEvent err: %d\n", err);
639 std::string IOCPEngine::GetName()
644 EventHandler * IOCPEngine::GetRef(int fd)
646 std::map<int, EventHandler*>::iterator itr = m_binding.find(fd);
647 return (itr == m_binding.end()) ? 0 : itr->second;
650 bool IOCPEngine::HasFd(int fd)
652 return (GetRef(fd) != 0);
655 bool IOCPEngine::BoundsCheckFd(EventHandler* eh)
657 int* internal_fd = (int*)fdExt.get(eh);
658 if (!eh || eh->GetFd() < 0)
664 if(*internal_fd > MAX_DESCRIPTORS)
670 EventHandler * IOCPEngine::GetIntRef(int fd)
672 if(fd < 0 || fd > MAX_DESCRIPTORS)
677 int IOCPEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
679 //SOCKET s = fd->GetFd();
681 Overlapped* acceptevent = (Overlapped*)acceptExt.get(fd);
683 /* Shit, no accept event on this socket! :( */
686 Overlapped* ovl = acceptevent;
687 accept_overlap* ov = (accept_overlap*)ovl->m_params;
689 //sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
690 sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
692 memcpy(addr, client_address, sizeof(sockaddr_in));
693 *addrlen = sizeof(sockaddr_in);
698 int IOCPEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
700 Overlapped* ovl = (Overlapped*)acceptExt.get(fd);
705 accept_overlap* ov = (accept_overlap*)ovl->m_params;
707 sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
708 //sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
710 memcpy(name, server_address, sizeof(sockaddr_in));
711 *namelen = sizeof(sockaddr_in);
716 int IOCPEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
718 this->UpdateStats(len, 0);
719 udp_overlap* ov = (udp_overlap*)readExt.get(fd);
722 memcpy(buf, ov->udp_buffer, ov->udp_len);
723 memcpy(from, ov->udp_sockaddr, *fromlen);
727 int IOCPEngine::Blocking(int fd)
729 unsigned long opt = 0;
730 return ioctlsocket(fd, FIONBIO, &opt);
733 int IOCPEngine::NonBlocking(int fd)
735 unsigned long opt = 1;
736 return ioctlsocket(fd, FIONBIO, &opt);
739 int IOCPEngine::Close(int fd)
741 return closesocket(fd);
744 int IOCPEngine::Close(EventHandler* fd)
746 return this->Close(fd->GetFd());
749 SocketEngine* CreateSocketEngine()
751 return new IOCPEngine;