aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/SocketManager.cxx
blob: 393e2191e15be4e557e07174fb7eacdcb52f8f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- SocketManager.cxx

#include <winsock2.h>
#include <list>
#include <rfb/LogWriter.h>
#include <rfb/Timer.h>
#include <rfb_win32/SocketManager.h>

using namespace rfb;
using namespace rfb::win32;

static LogWriter vlog("SocketManager");


// -=- SocketManager

SocketManager::SocketManager() {
}

SocketManager::~SocketManager() {
}


static void requestAddressChangeEvents(network::SocketListener* sock_) {
  DWORD dummy = 0;
  if (WSAIoctl(sock_->getFd(), SIO_ADDRESS_LIST_CHANGE, 0, 0, 0, 0, &dummy, 0, 0) == SOCKET_ERROR) {
    DWORD err = WSAGetLastError();
    if (err != WSAEWOULDBLOCK)
      vlog.error("Unable to track address changes: 0x%08x", (unsigned)err);
  }
}


void SocketManager::addListener(network::SocketListener* sock_,
                                network::SocketServer* srvr,
                                AddressChangeNotifier* acn) {
  WSAEVENT event = WSACreateEvent();
  long flags = FD_ACCEPT | FD_CLOSE;
  if (acn)
    flags |= FD_ADDRESS_LIST_CHANGE;
  try {
    if (event && (WSAEventSelect(sock_->getFd(), event, flags) == SOCKET_ERROR))
      throw rdr::SystemException("Unable to select on listener", WSAGetLastError());

    // requestAddressChangeEvents MUST happen after WSAEventSelect, so that the socket is non-blocking
    if (acn)
      requestAddressChangeEvents(sock_);

    // addEvent is the last thing we do, so that the event is NOT registered if previous steps fail
    if (!event || !addEvent(event, this))
      throw rdr::Exception("Unable to add listener");
  } catch (rdr::Exception& e) {
    if (event)
      WSACloseEvent(event);
    delete sock_;
    vlog.error("%s", e.str());
    throw;
  }

  ListenInfo li;
  li.sock = sock_;
  li.server = srvr;
  li.notifier = acn;
  li.disable = false;
  listeners[event] = li;
}

void SocketManager::remListener(network::SocketListener* sock) {
  std::map<HANDLE,ListenInfo>::iterator i;
  for (i=listeners.begin(); i!=listeners.end(); i++) {
    if (i->second.sock == sock) {
      removeEvent(i->first);
      WSACloseEvent(i->first);
      delete sock;
      listeners.erase(i);
      return;
    }
  }
  throw rdr::Exception("Listener not registered");
}


void SocketManager::addSocket(network::Socket* sock_, network::SocketServer* srvr, bool outgoing) {
  WSAEVENT event = WSACreateEvent();
  if (!event || !addEvent(event, this) ||
      (WSAEventSelect(sock_->getFd(), event, FD_READ | FD_CLOSE) == SOCKET_ERROR)) {
    if (event)
      WSACloseEvent(event);
    delete sock_;
    vlog.error("Unable to add connection");
    return;
  }
  ConnInfo ci;
  ci.sock = sock_;
  ci.server = srvr;
  connections[event] = ci;
  srvr->addSocket(sock_, outgoing);
}

void SocketManager::remSocket(network::Socket* sock_) {
  std::map<HANDLE,ConnInfo>::iterator i;
  for (i=connections.begin(); i!=connections.end(); i++) {
    if (i->second.sock == sock_) {
      i->second.server->removeSocket(sock_);
      removeEvent(i->first);
      WSACloseEvent(i->first);
      delete sock_;
      connections.erase(i);
      return;
    }
  }
  throw rdr::Exception("Socket not registered");
}

bool SocketManager::getDisable(network::SocketServer* srvr)
{
  std::map<HANDLE,ListenInfo>::iterator i;
  for (i=listeners.begin(); i!=listeners.end(); i++) {
    if (i->second.server == srvr) {
      return i->second.disable;
    }
  }
  throw rdr::Exception("Listener not registered");
}

void SocketManager::setDisable(network::SocketServer* srvr, bool disable)
{
  bool found = false;
  std::map<HANDLE,ListenInfo>::iterator i;
  for (i=listeners.begin(); i!=listeners.end(); i++) {
    if (i->second.server == srvr) {
      i->second.disable = disable;
      // There might be multiple sockets for the same server, so
      // continue iterating
      found = true;
    }
  }
  if (!found)
    throw rdr::Exception("Listener not registered");
}

int SocketManager::checkTimeouts() {
  int timeout = EventManager::checkTimeouts();

  std::map<HANDLE,ListenInfo>::iterator i;
  for (i=listeners.begin(); i!=listeners.end(); i++)
    soonestTimeout(&timeout, Timer::checkTimeouts());

  std::list<network::Socket*> shutdownSocks;
  std::map<HANDLE,ConnInfo>::iterator j, j_next;
  for (j=connections.begin(); j!=connections.end(); j=j_next) {
    j_next = j; j_next++;
    if (j->second.sock->isShutdown())
      shutdownSocks.push_back(j->second.sock);
    else {
      long eventMask = FD_READ | FD_CLOSE;
      if (j->second.sock->outStream().hasBufferedData())
        eventMask |= FD_WRITE;
      if (WSAEventSelect(j->second.sock->getFd(), j->first, eventMask) == SOCKET_ERROR)
        throw rdr::SystemException("unable to adjust WSAEventSelect:%u", WSAGetLastError());
    }
  }

  std::list<network::Socket*>::iterator k;
  for (k=shutdownSocks.begin(); k!=shutdownSocks.end(); k++)
    remSocket(*k);

  return timeout;
}


void SocketManager::processEvent(HANDLE event) {
  if (listeners.count(event)) {
    ListenInfo li = listeners[event];

    // Accept an incoming connection
    vlog.debug("accepting incoming connection");

    // What kind of event is this?
    WSANETWORKEVENTS network_events;
    WSAEnumNetworkEvents(li.sock->getFd(), event, &network_events);
    if (network_events.lNetworkEvents & FD_ACCEPT) {
      network::Socket* new_sock = li.sock->accept();
      if (new_sock && li.disable) {
        delete new_sock;
        new_sock = 0;
      }
      if (new_sock)
        addSocket(new_sock, li.server, false);
    } else if (network_events.lNetworkEvents & FD_CLOSE) {
      vlog.info("deleting listening socket");
      remListener(li.sock);
    } else if (network_events.lNetworkEvents & FD_ADDRESS_LIST_CHANGE) {
      li.notifier->processAddressChange();
      requestAddressChangeEvents(li.sock);
    } else {
      vlog.error("unknown listener event: %lx", network_events.lNetworkEvents);
    }
  } else if (connections.count(event)) {
    ConnInfo ci = connections[event];

    try {
      // Process data from an active connection

      WSANETWORKEVENTS events;
      long eventMask;

      // Fetch why this event notification triggered
      if (WSAEnumNetworkEvents(ci.sock->getFd(), event, &events) == SOCKET_ERROR)
        throw rdr::SystemException("unable to get WSAEnumNetworkEvents:%u", WSAGetLastError());

      // Cancel event notification for this socket
      if (WSAEventSelect(ci.sock->getFd(), event, 0) == SOCKET_ERROR)
        throw rdr::SystemException("unable to disable WSAEventSelect:%u", WSAGetLastError());

      // Reset the event object
      WSAResetEvent(event);


      // Call the socket server to process the event
      if (events.lNetworkEvents & FD_WRITE) {
        ci.server->processSocketWriteEvent(ci.sock);
        if (ci.sock->isShutdown()) {
          remSocket(ci.sock);
          return;
        }
      }
      if (events.lNetworkEvents & (FD_READ | FD_CLOSE)) {
        ci.server->processSocketReadEvent(ci.sock);
        if (ci.sock->isShutdown()) {
          remSocket(ci.sock);
          return;
        }
      }

      // Re-instate the required socket event
      // If the read event is still valid, the event object gets set here
      eventMask = FD_READ | FD_CLOSE;
      if (ci.sock->outStream().hasBufferedData())
        eventMask |= FD_WRITE;
      if (WSAEventSelect(ci.sock->getFd(), event, eventMask) == SOCKET_ERROR)
        throw rdr::SystemException("unable to re-enable WSAEventSelect:%u", WSAGetLastError());
    } catch (rdr::Exception& e) {
      vlog.error("%s", e.str());
      remSocket(ci.sock);
    }
  }
}