]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_win32.cpp
Fix the DNS socket not being closed when core_dns is unloaded.
[user/henk/code/inspircd.git] / src / threadengines / threadengine_win32.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
6  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
7  *   Copyright (C) 2009, 2011 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9  *   Copyright (C) 2009 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2008-2009 Craig Edwards <brain@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "threadengines/threadengine_win32.h"
28
29 void ThreadEngine::Start(Thread* thread)
30 {
31         thread->state.handle = CreateThread(NULL, 0, ThreadEngine::Entry, thread, 0, NULL);
32
33         if (thread->state.handle == NULL)
34         {
35                 DWORD lasterr = GetLastError();
36                 std::string err = "Unable to create new thread: " + ConvToStr(lasterr);
37                 SetLastError(ERROR_SUCCESS);
38                 throw CoreException(err);
39         }
40 }
41
42 DWORD WINAPI ThreadEngine::Entry(void* parameter)
43 {
44         Thread* pt = static_cast<Thread*>(parameter);
45         pt->Run();
46         return 0;
47 }
48
49 void ThreadEngine::Stop(Thread* thread)
50 {
51         thread->SetExitFlag();
52         HANDLE handle = thread->state.handle;
53         WaitForSingleObject(handle,INFINITE);
54         CloseHandle(handle);
55 }
56
57 class ThreadSignalSocket : public BufferedSocket
58 {
59         SocketThread* parent;
60  public:
61         ThreadSignalSocket(SocketThread* t, int newfd)
62                 : BufferedSocket(newfd), parent(t)
63         {
64         }
65
66         void OnDataReady()
67         {
68                 recvq.clear();
69                 parent->OnNotify();
70         }
71
72         void OnError(BufferedSocketError)
73         {
74                 ServerInstance->GlobalCulls.AddItem(this);
75         }
76 };
77
78 static bool BindAndListen(int sockfd, int port, const char* addr)
79 {
80         irc::sockets::sockaddrs servaddr;
81         if (!irc::sockets::aptosa(addr, port, servaddr))
82                 return false;
83
84         if (SocketEngine::Bind(sockfd, servaddr) != 0)
85                 return false;
86
87         if (SocketEngine::Listen(sockfd, ServerInstance->Config->MaxConn) != 0)
88         {
89                 ServerInstance->Logs->Log("SOCKET", LOG_DEFAULT, "ERROR in listen(): %s", strerror(errno));
90                 return false;
91         }
92
93         return true;
94 }
95
96 SocketThread::SocketThread()
97 {
98         int listenFD = socket(AF_INET, SOCK_STREAM, 0);
99         if (listenFD == -1)
100                 throw CoreException("Could not create ITC pipe");
101         int connFD = socket(AF_INET, SOCK_STREAM, 0);
102         if (connFD == -1)
103                 throw CoreException("Could not create ITC pipe");
104
105         if (!BindAndListen(listenFD, 0, "127.0.0.1"))
106                 throw CoreException("Could not create ITC pipe");
107         SocketEngine::NonBlocking(connFD);
108
109         struct sockaddr_in addr;
110         socklen_t sz = sizeof(addr);
111         getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
112         connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
113         SocketEngine::Blocking(listenFD);
114         int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
115         if (nfd < 0)
116                 throw CoreException("Could not create ITC pipe");
117         new ThreadSignalSocket(this, nfd);
118         closesocket(listenFD);
119
120         SocketEngine::Blocking(connFD);
121         this->signal.connFD = connFD;
122 }
123
124 void SocketThread::NotifyParent()
125 {
126         char dummy = '*';
127         send(signal.connFD, &dummy, 1, 0);
128 }
129
130 SocketThread::~SocketThread()
131 {
132         if (signal.connFD >= 0)
133         {
134                 shutdown(signal.connFD, 2);
135                 closesocket(signal.connFD);
136         }
137 }