Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | #ifdef WIN32 |
| 20 | //#include <io.h> |
| 21 | #include <winsock2.h> |
| 22 | #define errorNumber WSAGetLastError() |
| 23 | #define snprintf _snprintf |
| 24 | #else |
| 25 | #define errorNumber errno |
| 26 | #define closesocket close |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/socket.h> |
| 29 | #include <arpa/inet.h> |
| 30 | #include <netinet/in.h> |
| 31 | #include <netinet/tcp.h> |
| 32 | #include <netdb.h> |
| 33 | #include <unistd.h> |
| 34 | #include <errno.h> |
| 35 | #include <string.h> |
| 36 | #include <signal.h> |
| 37 | #include <fcntl.h> |
| 38 | #endif |
| 39 | |
| 40 | #include <network/TcpSocket.h> |
| 41 | #include <rfb/util.h> |
| 42 | #include <rfb/LogWriter.h> |
| 43 | |
| 44 | #ifndef VNC_SOCKLEN_T |
| 45 | #define VNC_SOCKLEN_T int |
| 46 | #endif |
| 47 | |
| 48 | #ifndef INADDR_NONE |
| 49 | #define INADDR_NONE ((unsigned long)-1) |
| 50 | #endif |
| 51 | |
| 52 | using namespace network; |
| 53 | using namespace rdr; |
| 54 | |
| 55 | static rfb::LogWriter vlog("TcpSocket"); |
| 56 | |
| 57 | |
| 58 | void |
| 59 | TcpSocket::initTcpSockets() { |
| 60 | #ifdef WIN32 |
| 61 | WORD requiredVersion = MAKEWORD(2,0); |
| 62 | WSADATA initResult; |
| 63 | |
| 64 | if (WSAStartup(requiredVersion, &initResult) != 0) |
| 65 | throw SocketException("unable to initialise Winsock2", errorNumber); |
| 66 | #else |
| 67 | signal(SIGPIPE, SIG_IGN); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | // -=- TcpSocket |
| 72 | |
| 73 | TcpSocket::TcpSocket(int sock, bool close) |
| 74 | : Socket(new FdInStream(sock), new FdOutStream(sock), true), closeFd(close) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | TcpSocket::TcpSocket(const char *host, int port) |
| 79 | : closeFd(true) |
| 80 | { |
| 81 | int sock; |
| 82 | |
| 83 | // - Create a socket |
| 84 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| 85 | throw SocketException("unable to create socket", errorNumber); |
| 86 | |
| 87 | #ifndef WIN32 |
| 88 | // - By default, close the socket on exec() |
| 89 | fcntl(sock, F_SETFD, FD_CLOEXEC); |
| 90 | #endif |
| 91 | |
| 92 | // - Connect it to something |
| 93 | |
| 94 | // Try processing the host as an IP address |
| 95 | struct sockaddr_in addr; |
| 96 | memset(&addr, 0, sizeof(addr)); |
| 97 | addr.sin_family = AF_INET; |
| 98 | addr.sin_addr.s_addr = inet_addr(host); |
| 99 | addr.sin_port = htons(port); |
| 100 | if ((int)addr.sin_addr.s_addr == -1) { |
| 101 | // Host was not an IP address - try resolving as DNS name |
| 102 | struct hostent *hostinfo; |
| 103 | hostinfo = gethostbyname(host); |
| 104 | if (hostinfo && hostinfo->h_addr) { |
| 105 | addr.sin_addr.s_addr = ((struct in_addr *)hostinfo->h_addr)->s_addr; |
| 106 | } else { |
| 107 | int e = errorNumber; |
| 108 | closesocket(sock); |
| 109 | throw SocketException("unable to resolve host by name", e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Attempt to connect to the remote host |
| 114 | if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) { |
| 115 | int e = errorNumber; |
| 116 | closesocket(sock); |
| 117 | throw SocketException("unable to connect to host", e); |
| 118 | } |
| 119 | |
| 120 | int one = 1; |
| 121 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 122 | (char *)&one, sizeof(one)) < 0) { |
| 123 | int e = errorNumber; |
| 124 | closesocket(sock); |
| 125 | throw SocketException("unable to setsockopt TCP_NODELAY", e); |
| 126 | } |
| 127 | |
| 128 | // Create the input and output streams |
| 129 | instream = new FdInStream(sock); |
| 130 | outstream = new FdOutStream(sock); |
| 131 | own_streams = true; |
| 132 | } |
| 133 | |
| 134 | TcpSocket::~TcpSocket() { |
| 135 | if (closeFd) |
| 136 | closesocket(getFd()); |
| 137 | } |
| 138 | |
| 139 | char* TcpSocket::getMyAddress() { |
| 140 | struct sockaddr_in info; |
| 141 | struct in_addr addr; |
| 142 | VNC_SOCKLEN_T info_size = sizeof(info); |
| 143 | |
| 144 | getsockname(getFd(), (struct sockaddr *)&info, &info_size); |
| 145 | memcpy(&addr, &info.sin_addr, sizeof(addr)); |
| 146 | |
| 147 | char* name = inet_ntoa(addr); |
| 148 | if (name) { |
| 149 | return rfb::strDup(name); |
| 150 | } else { |
| 151 | return rfb::strDup(""); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int TcpSocket::getMyPort() { |
| 156 | return getSockPort(getFd()); |
| 157 | } |
| 158 | |
| 159 | char* TcpSocket::getMyEndpoint() { |
| 160 | rfb::CharArray address; address.buf = getMyAddress(); |
| 161 | int port = getMyPort(); |
| 162 | |
| 163 | int buflen = strlen(address.buf) + 32; |
| 164 | char* buffer = new char[buflen]; |
| 165 | sprintf(buffer, "%s::%d", address.buf, port); |
| 166 | return buffer; |
| 167 | } |
| 168 | |
| 169 | char* TcpSocket::getPeerAddress() { |
| 170 | struct sockaddr_in info; |
| 171 | struct in_addr addr; |
| 172 | VNC_SOCKLEN_T info_size = sizeof(info); |
| 173 | |
| 174 | getpeername(getFd(), (struct sockaddr *)&info, &info_size); |
| 175 | memcpy(&addr, &info.sin_addr, sizeof(addr)); |
| 176 | |
| 177 | char* name = inet_ntoa(addr); |
| 178 | if (name) { |
| 179 | return rfb::strDup(name); |
| 180 | } else { |
| 181 | return rfb::strDup(""); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | int TcpSocket::getPeerPort() { |
| 186 | struct sockaddr_in info; |
| 187 | VNC_SOCKLEN_T info_size = sizeof(info); |
| 188 | |
| 189 | getpeername(getFd(), (struct sockaddr *)&info, &info_size); |
| 190 | return ntohs(info.sin_port); |
| 191 | } |
| 192 | |
| 193 | char* TcpSocket::getPeerEndpoint() { |
| 194 | rfb::CharArray address; address.buf = getPeerAddress(); |
| 195 | int port = getPeerPort(); |
| 196 | |
| 197 | int buflen = strlen(address.buf) + 32; |
| 198 | char* buffer = new char[buflen]; |
| 199 | sprintf(buffer, "%s::%d", address.buf, port); |
| 200 | return buffer; |
| 201 | } |
| 202 | |
| 203 | bool TcpSocket::sameMachine() { |
| 204 | struct sockaddr_in peeraddr, myaddr; |
| 205 | VNC_SOCKLEN_T addrlen = sizeof(struct sockaddr_in); |
| 206 | |
| 207 | getpeername(getFd(), (struct sockaddr *)&peeraddr, &addrlen); |
| 208 | getsockname(getFd(), (struct sockaddr *)&myaddr, &addrlen); |
| 209 | |
| 210 | return (peeraddr.sin_addr.s_addr == myaddr.sin_addr.s_addr); |
| 211 | } |
| 212 | |
| 213 | void TcpSocket::shutdown() |
| 214 | { |
| 215 | ::shutdown(getFd(), 2); |
| 216 | } |
| 217 | |
| 218 | bool TcpSocket::isSocket(int sock) |
| 219 | { |
| 220 | struct sockaddr_in info; |
| 221 | VNC_SOCKLEN_T info_size = sizeof(info); |
| 222 | return getsockname(sock, (struct sockaddr *)&info, &info_size) >= 0; |
| 223 | } |
| 224 | |
| 225 | bool TcpSocket::isConnected(int sock) |
| 226 | { |
| 227 | struct sockaddr_in info; |
| 228 | VNC_SOCKLEN_T info_size = sizeof(info); |
| 229 | return getpeername(sock, (struct sockaddr *)&info, &info_size) >= 0; |
| 230 | } |
| 231 | |
| 232 | int TcpSocket::getSockPort(int sock) |
| 233 | { |
| 234 | struct sockaddr_in info; |
| 235 | VNC_SOCKLEN_T info_size = sizeof(info); |
| 236 | if (getsockname(sock, (struct sockaddr *)&info, &info_size) < 0) |
| 237 | return 0; |
| 238 | return ntohs(info.sin_port); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | TcpListener::TcpListener(int port, bool localhostOnly, int sock, bool close_) |
| 243 | : closeFd(close_) |
| 244 | { |
| 245 | if (sock != -1) { |
| 246 | fd = sock; |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
| 251 | throw SocketException("unable to create listening socket", errorNumber); |
| 252 | |
| 253 | #ifndef WIN32 |
| 254 | // - By default, close the socket on exec() |
| 255 | fcntl(sock, F_SETFD, FD_CLOEXEC); |
| 256 | |
| 257 | int one = 1; |
| 258 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 259 | (const char *)&one, sizeof(one)) < 0) { |
| 260 | int e = errorNumber; |
| 261 | closesocket(fd); |
| 262 | throw SocketException("unable to create listening socket", e); |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | // - Bind it to the desired port |
| 267 | struct sockaddr_in addr; |
| 268 | memset(&addr, 0, sizeof(addr)); |
| 269 | addr.sin_family = AF_INET; |
| 270 | addr.sin_port = htons(port); |
| 271 | if (localhostOnly) |
| 272 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 273 | else |
| 274 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 275 | if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 276 | int e = errorNumber; |
| 277 | closesocket(fd); |
| 278 | throw SocketException("unable to bind listening socket", e); |
| 279 | } |
| 280 | |
| 281 | // - Set it to be a listening socket |
| 282 | if (listen(fd, 5) < 0) { |
| 283 | int e = errorNumber; |
| 284 | closesocket(fd); |
| 285 | throw SocketException("unable to set socket to listening mode", e); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | TcpListener::~TcpListener() { |
| 290 | if (closeFd) closesocket(fd); |
| 291 | } |
| 292 | |
| 293 | void TcpListener::shutdown() |
| 294 | { |
| 295 | #ifdef WIN32 |
| 296 | closesocket(getFd()); |
| 297 | #else |
| 298 | ::shutdown(getFd(), 2); |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | |
| 303 | Socket* |
| 304 | TcpListener::accept() { |
| 305 | int new_sock = -1; |
| 306 | |
| 307 | // Accept an incoming connection |
| 308 | if ((new_sock = ::accept(fd, 0, 0)) < 0) |
| 309 | throw SocketException("unable to accept new connection", errorNumber); |
| 310 | |
| 311 | #ifndef WIN32 |
| 312 | // - By default, close the socket on exec() |
| 313 | fcntl(new_sock, F_SETFD, FD_CLOEXEC); |
| 314 | #endif |
| 315 | |
| 316 | // Disable Nagle's algorithm |
| 317 | int one = 1; |
| 318 | if (setsockopt(new_sock, IPPROTO_TCP, TCP_NODELAY, |
| 319 | (char *)&one, sizeof(one)) < 0) { |
| 320 | int e = errorNumber; |
| 321 | closesocket(new_sock); |
| 322 | throw SocketException("unable to setsockopt TCP_NODELAY", e); |
| 323 | } |
| 324 | |
| 325 | // Create the socket object & check connection is allowed |
| 326 | TcpSocket* s = new TcpSocket(new_sock); |
| 327 | if (filter && !filter->verifyConnection(s)) { |
| 328 | delete s; |
| 329 | return 0; |
| 330 | } |
| 331 | return s; |
| 332 | } |
| 333 | |
| 334 | void TcpListener::getMyAddresses(std::list<char*>* result) { |
| 335 | const hostent* addrs = gethostbyname(0); |
| 336 | if (addrs == 0) |
| 337 | throw rdr::SystemException("gethostbyname", errorNumber); |
| 338 | if (addrs->h_addrtype != AF_INET) |
| 339 | throw rdr::Exception("getMyAddresses: bad family"); |
| 340 | for (int i=0; addrs->h_addr_list[i] != 0; i++) { |
| 341 | const char* addrC = inet_ntoa(*((struct in_addr*)addrs->h_addr_list[i])); |
| 342 | char* addr = new char[strlen(addrC)+1]; |
| 343 | strcpy(addr, addrC); |
| 344 | result->push_back(addr); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | int TcpListener::getMyPort() { |
| 349 | return TcpSocket::getSockPort(getFd()); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | TcpFilter::TcpFilter(const char* spec) { |
| 354 | rfb::CharArray tmp; |
| 355 | tmp.buf = rfb::strDup(spec); |
| 356 | while (tmp.buf) { |
| 357 | rfb::CharArray first; |
| 358 | rfb::strSplit(tmp.buf, ',', &first.buf, &tmp.buf); |
| 359 | if (strlen(first.buf)) |
| 360 | filter.push_back(parsePattern(first.buf)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | TcpFilter::~TcpFilter() { |
| 365 | } |
| 366 | |
| 367 | |
| 368 | static bool |
| 369 | patternMatchIP(const TcpFilter::Pattern& pattern, const char* value) { |
| 370 | unsigned long address = inet_addr(value); |
| 371 | if (address == INADDR_NONE) return false; |
| 372 | return ((pattern.address & pattern.mask) == (address & pattern.mask)); |
| 373 | } |
| 374 | |
| 375 | bool |
| 376 | TcpFilter::verifyConnection(Socket* s) { |
| 377 | rfb::CharArray name; |
| 378 | |
| 379 | name.buf = s->getPeerAddress(); |
| 380 | std::list<TcpFilter::Pattern>::iterator i; |
| 381 | for (i=filter.begin(); i!=filter.end(); i++) { |
| 382 | if (patternMatchIP(*i, name.buf)) { |
| 383 | switch ((*i).action) { |
| 384 | case Accept: |
| 385 | vlog.debug("ACCEPT %s", name.buf); |
| 386 | return true; |
| 387 | case Query: |
| 388 | vlog.debug("QUERY %s", name.buf); |
| 389 | return queryUserAcceptConnection(s); |
| 390 | case Reject: |
| 391 | vlog.debug("REJECT %s", name.buf); |
| 392 | return false; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | vlog.debug("[REJECT] %s", name.buf); |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | TcpFilter::Pattern TcpFilter::parsePattern(const char* p) { |
| 403 | TcpFilter::Pattern pattern; |
| 404 | |
| 405 | bool expandMask = false; |
| 406 | rfb::CharArray addr, mask; |
| 407 | |
| 408 | if (rfb::strSplit(&p[1], '/', &addr.buf, &mask.buf)) { |
| 409 | if (rfb::strContains(mask.buf, '.')) { |
| 410 | pattern.mask = inet_addr(mask.buf); |
| 411 | } else { |
| 412 | pattern.mask = atoi(mask.buf); |
| 413 | expandMask = true; |
| 414 | } |
| 415 | } else { |
| 416 | pattern.mask = 32; |
| 417 | expandMask = true; |
| 418 | } |
| 419 | if (expandMask) { |
| 420 | unsigned long expanded = 0; |
| 421 | // *** check endianness! |
| 422 | for (int i=0; i<(int)pattern.mask; i++) |
| 423 | expanded |= 1<<(31-i); |
| 424 | pattern.mask = htonl(expanded); |
| 425 | } |
| 426 | |
| 427 | pattern.address = inet_addr(addr.buf) & pattern.mask; |
| 428 | if ((pattern.address == INADDR_NONE) || |
| 429 | (pattern.address == 0)) pattern.mask = 0; |
| 430 | |
| 431 | switch(p[0]) { |
| 432 | case '+': pattern.action = TcpFilter::Accept; break; |
| 433 | case '-': pattern.action = TcpFilter::Reject; break; |
| 434 | case '?': pattern.action = TcpFilter::Query; break; |
| 435 | }; |
| 436 | |
| 437 | return pattern; |
| 438 | } |
| 439 | |
| 440 | char* TcpFilter::patternToStr(const TcpFilter::Pattern& p) { |
| 441 | in_addr tmp; |
| 442 | rfb::CharArray addr, mask; |
| 443 | tmp.s_addr = p.address; |
| 444 | addr.buf = rfb::strDup(inet_ntoa(tmp)); |
| 445 | tmp.s_addr = p.mask; |
| 446 | mask.buf = rfb::strDup(inet_ntoa(tmp)); |
| 447 | char* result = new char[strlen(addr.buf)+1+strlen(mask.buf)+1+1]; |
| 448 | switch (p.action) { |
| 449 | case Accept: result[0] = '+'; break; |
| 450 | case Reject: result[0] = '-'; break; |
| 451 | case Query: result[0] = '?'; break; |
| 452 | }; |
| 453 | result[1] = 0; |
| 454 | strcat(result, addr.buf); |
| 455 | strcat(result, "/"); |
| 456 | strcat(result, mask.buf); |
| 457 | return result; |
| 458 | } |