sockets.c 18.5 KB
Newer Older
Mark McLoughlin's avatar
Mark McLoughlin committed
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
/*
 * sockets.c - deal with TCP sockets.
 *
 * This code should be independent of any changes in the RFB protocol.  It just
 * deals with the X server scheduling stuff, calling rfbNewClientConnection and
 * rfbProcessClientMessage to actually deal with the protocol.  If a socket
 * needs to be closed for any reason then rfbCloseClient should be called, and
 * this in turn will call rfbClientConnectionGone.  To make an active
 * connection out, call rfbConnect - note that this does _not_ call
 * rfbNewClientConnection.
 *
 * This file is divided into two types of function.  Those beginning with
 * "rfb" are specific to sockets using the RFB protocol.  Those without the
 * "rfb" prefix are more general socket routines (which are used by the http
 * code).
 *
 * Thanks to Karl Hakimian for pointing out that some platforms return EAGAIN
 * not EWOULDBLOCK.
 */

/*
 *  OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
 *  Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.  
 *  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
37 38 39
 *  along with this software; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 *  USA.
Mark McLoughlin's avatar
Mark McLoughlin committed
40 41
 */

David King's avatar
David King committed
42
#include "rfb/rfb.h"
Mark McLoughlin's avatar
Mark McLoughlin committed
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

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef WIN32
#pragma warning (disable: 4018 4761)
#define close closesocket
#define read(sock,buf,len) recv(sock,buf,len,0)
#define EWOULDBLOCK WSAEWOULDBLOCK
#define ETIMEDOUT WSAETIMEDOUT
#define write(sock,buf,len) send(sock,buf,len,0)
#else
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#endif

73
#include <net/if.h>  // IFF_UP
74 75 76 77 78 79 80

#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#else
#include "ifaddr/ifaddrs.h"
#endif

81 82 83 84 85
#ifdef RFC2553
#define ADDR_FAMILY_MEMBER ss_family
#else
#define ADDR_FAMILY_MEMBER sa_family
#endif
86

Mark McLoughlin's avatar
Mark McLoughlin committed
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
#if defined(__linux__) && defined(NEED_TIMEVAL)
struct timeval 
{
   long int tv_sec,tv_usec;
}
;
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#include <errno.h>

/*#ifndef WIN32
int max(int i,int j) { return(i<j?j:i); }
#endif
*/

int rfbMaxClientWait = 20000;   /* time (ms) after which we decide client has
                                   gone away - needed to stop us hanging */

/*
 * rfbInitSockets sets up the TCP sockets to listen for RFB
 * connections.  It does nothing if called again.
 */

114 115
static void rfbInitListenSock(rfbScreenInfoPtr rfbScreen);

Mark McLoughlin's avatar
Mark McLoughlin committed
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
void
rfbInitSockets(rfbScreenInfoPtr rfbScreen)
{
    if (rfbScreen->socketInitDone)
	return;

    rfbScreen->socketInitDone = TRUE;

    if (rfbScreen->inetdSock != -1) {
	const int one = 1;

#ifndef WIN32
	if (fcntl(rfbScreen->inetdSock, F_SETFL, O_NONBLOCK) < 0) {
	    rfbLogPerror("fcntl");
	    return;
	}
#endif

	if (setsockopt(rfbScreen->inetdSock, IPPROTO_TCP, TCP_NODELAY,
		       (char *)&one, sizeof(one)) < 0) {
	    rfbLogPerror("setsockopt");
	    return;
	}

    	FD_ZERO(&(rfbScreen->allFds));
    	FD_SET(rfbScreen->inetdSock, &(rfbScreen->allFds));
    	rfbScreen->maxFd = rfbScreen->inetdSock;
	return;
    }

146 147 148 149 150 151
    rfbInitListenSock(rfbScreen);
}

static void
rfbInitListenSock(rfbScreenInfoPtr rfbScreen)
{
152 153 154 155 156 157 158 159 160
    char *netIface = (char*)rfbScreen->netIface;
    int i;

    if(netIface == NULL || if_nametoindex(netIface) == 0) {
      if(netIface != NULL)
        rfbLog("WARNING: This (%s) a invalid network interface, set to all\n", netIface);
      netIface = NULL;
    }

Mark McLoughlin's avatar
Mark McLoughlin committed
161
    if(rfbScreen->autoPort) {
162 163
      rfbLog("Autoprobing TCP port in (%s) network interface\n",
            netIface != NULL ? netIface : "all");
Mark McLoughlin's avatar
Mark McLoughlin committed
164

165 166 167 168
      for (i = 5900; i < 6000; i++) {
        if (ListenOnTCPPort(rfbScreen, i, netIface)) {
          rfbScreen->rfbPort = i;
          break;
Mark McLoughlin's avatar
Mark McLoughlin committed
169
        }
170
      }
Mark McLoughlin's avatar
Mark McLoughlin committed
171

172 173 174 175 176 177
      if (i >= 6000) {
        rfbLogPerror("Failure autoprobing");
        return;
      }

      rfbLog("Autoprobing selected port %d\n", rfbScreen->rfbPort);
Mark McLoughlin's avatar
Mark McLoughlin committed
178

179 180 181 182 183
      FD_ZERO(&rfbScreen->allFds);
      for(i=0; i < rfbScreen->rfbListenSockTotal; i++) {
        FD_SET(rfbScreen->rfbListenSock[i], &rfbScreen->allFds);
        rfbScreen->maxFd = rfbScreen->rfbListenSock[i];
      }
Mark McLoughlin's avatar
Mark McLoughlin committed
184
    }
185 186 187
    else if(rfbScreen->rfbPort > 0) {
      rfbLog("Listening for VNC connections on TCP port %d in (%s) network interface\n", 
            rfbScreen->rfbPort, netIface != NULL ? netIface : "all");
Mark McLoughlin's avatar
Mark McLoughlin committed
188

189 190 191
      if (!ListenOnTCPPort(rfbScreen, rfbScreen->rfbPort, netIface)) {
        rfbLogPerror("ListenOnTCPPort");
        return;
Mark McLoughlin's avatar
Mark McLoughlin committed
192 193
      }

194 195 196 197 198
      FD_ZERO(&rfbScreen->allFds);
      for(i=0; i < rfbScreen->rfbListenSockTotal; i++) {
        FD_SET(rfbScreen->rfbListenSock[i], &rfbScreen->allFds);
        rfbScreen->maxFd = rfbScreen->rfbListenSock[i];
      }
Mark McLoughlin's avatar
Mark McLoughlin committed
199 200 201
    }
}

202 203 204 205 206 207 208 209 210 211 212
void
rfbSetAutoPort(rfbScreenInfoPtr rfbScreen, rfbBool autoPort)
{
    if (rfbScreen->autoPort == autoPort)
        return;

    rfbScreen->autoPort = autoPort;

    if (!rfbScreen->socketInitDone)
	return;

213 214 215 216 217 218 219 220 221
    if (rfbScreen->rfbListenSockTotal > 0) {
        int i;

        for(i=0; i < rfbScreen->rfbListenSockTotal; i++) {
           FD_CLR(rfbScreen->rfbListenSock[i], &rfbScreen->allFds);
           close(rfbScreen->rfbListenSock[i]);
           rfbScreen->rfbListenSock[i] = -1;
        }
        rfbScreen->rfbListenSockTotal = 0;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    }

    rfbInitListenSock(rfbScreen);
}

void
rfbSetPort(rfbScreenInfoPtr rfbScreen, int port)
{
    if (rfbScreen->rfbPort == port)
        return;

    rfbScreen->rfbPort = port;

    if (!rfbScreen->socketInitDone || rfbScreen->autoPort)
	return;

238 239
    if (rfbScreen->rfbListenSockTotal > 0) {
        int i;
240

241 242 243 244 245 246
        for(i=0; i < rfbScreen->rfbListenSockTotal; i++) {
           FD_CLR(rfbScreen->rfbListenSock[i], &rfbScreen->allFds);
           close(rfbScreen->rfbListenSock[i]);
           rfbScreen->rfbListenSock[i] = -1;
        }
        rfbScreen->rfbListenSockTotal = 0;
247 248
    }

249
    rfbInitListenSock(rfbScreen);
250 251
}

Mark McLoughlin's avatar
Mark McLoughlin committed
252
void
253
rfbProcessNewConnection(rfbScreenInfoPtr rfbScreen, int insock)
Mark McLoughlin's avatar
Mark McLoughlin committed
254 255
{
    const int one = 1;
256 257 258 259 260 261
    int sock = -1;
    
    if((sock = accept(insock, NULL, NULL)) < 0) {
      rfbLogPerror("rfbCheckFds: accept");
      return;
    }
Mark McLoughlin's avatar
Mark McLoughlin committed
262

263 264 265
    if(sock < 0) {
      rfbLogPerror("rfbCheckFds: accept");
      return;
Mark McLoughlin's avatar
Mark McLoughlin committed
266 267 268 269 270 271 272 273 274 275
    }

#ifndef WIN32
    if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) {
	rfbLogPerror("rfbCheckFds: fcntl");
	close(sock);
	return;
    }
#endif

276
    if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) {
Mark McLoughlin's avatar
Mark McLoughlin committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	rfbLogPerror("rfbCheckFds: setsockopt");
	close(sock);
	return;
    }

    rfbNewClient(rfbScreen,sock);
}

/*
 * rfbCheckFds is called from ProcessInputEvents to check for input on the RFB
 * socket(s).  If there is input to process, the appropriate function in the
 * RFB server code will be called (rfbNewClientConnection,
 * rfbProcessClientMessage, etc).
 */

void
rfbCheckFds(rfbScreenInfoPtr rfbScreen,long usec)
{
    int nfds;
296
    int n;
Mark McLoughlin's avatar
Mark McLoughlin committed
297 298 299 300 301 302 303 304 305 306
    fd_set fds;
    struct timeval tv;
    rfbClientIteratorPtr i;
    rfbClientPtr cl;

    if (!rfbScreen->inetdInitDone && rfbScreen->inetdSock != -1) {
	rfbNewClientConnection(rfbScreen,rfbScreen->inetdSock); 
	rfbScreen->inetdInitDone = TRUE;
    }

307
    memcpy(&fds, &rfbScreen->allFds, sizeof(fd_set));
Mark McLoughlin's avatar
Mark McLoughlin committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    tv.tv_sec = 0;
    tv.tv_usec = usec;
    nfds = select(rfbScreen->maxFd + 1, &fds, NULL, NULL /* &fds */, &tv);
    if (nfds == 0) {
	return;
    }
    if (nfds < 0) {
#ifdef WIN32
		errno = WSAGetLastError();
#endif
	if (errno != EINTR)
		rfbLogPerror("rfbCheckFds: select");
	return;
    }
322 323 324 325 326 327 328 329
    printf("DUMP: nfds = %d\n", nfds);
    for(n=0; n < rfbScreen->rfbListenSockTotal; n++) {
        if (rfbScreen->rfbListenSock[n] != -1 && FD_ISSET(rfbScreen->rfbListenSock[n], &fds)) {
          rfbProcessNewConnection(rfbScreen, rfbScreen->rfbListenSock[n]);
          FD_CLR(rfbScreen->rfbListenSock[n], &fds);
          if (--nfds == 0)
            return;
        }
Mark McLoughlin's avatar
Mark McLoughlin committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    }

    i = rfbGetClientIterator(rfbScreen);
    while((cl = rfbClientIteratorNext(i))) {
      if (cl->onHold)
	continue;
      if (FD_ISSET(cl->sock, &fds) && FD_ISSET(cl->sock, &(rfbScreen->allFds)))
	rfbProcessClientMessage(cl);
    }
    rfbReleaseClientIterator(i);
}

void
rfbCloseClient(cl)
     rfbClientPtr cl;
{
    rfbAuthCleanupClient(cl);

    LOCK(cl->updateMutex);
#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
    if (cl->sock != -1)
#endif
      {
	FD_CLR(cl->sock,&(cl->screen->allFds));
	if(cl->sock==cl->screen->maxFd)
	  while(cl->screen->maxFd>0
		&& !FD_ISSET(cl->screen->maxFd,&(cl->screen->allFds)))
	    cl->screen->maxFd--;
	shutdown(cl->sock,SHUT_RDWR);
	close(cl->sock);
	cl->sock = -1;
      }
    UNLOCK(cl->updateMutex);
}


366
#ifdef VINO_HAVE_GNUTLS
Mark McLoughlin's avatar
Mark McLoughlin committed
367
static int
368
ReadExactOverTLS(rfbClientPtr cl, char* buf, int len, int timeout)
Mark McLoughlin's avatar
Mark McLoughlin committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
{
    while (len > 0) {
	int n;

	n = gnutls_record_recv(cl->tlsSession, buf, len);
	if (n == 0) {
	    UNLOCK(cl->outputMutex);
	    return 0;

	} else if (n < 0) {
	    if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
		continue;

	    UNLOCK(cl->outputMutex);
	    return -1;
	}

	buf += n;
	len -= n;
    }

    return 1;
}
392
#endif /* VINO_HAVE_GNUTLS */
Mark McLoughlin's avatar
Mark McLoughlin committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

/*
 * ReadExact reads an exact number of bytes from a client.  Returns 1 if
 * those bytes have been read, 0 if the other end has closed, or -1 if an error
 * occurred (errno is set to ETIMEDOUT if it timed out).
 */

int
ReadExactTimeout(rfbClientPtr cl, char* buf, int len, int timeout)
{
    int sock = cl->sock;
    int n;
    fd_set fds;
    struct timeval tv;

408
#ifdef VINO_HAVE_GNUTLS
409 410
    if (cl->useTLS)
	return ReadExactOverTLS(cl, buf, len, timeout);
411
#endif
Mark McLoughlin's avatar
Mark McLoughlin committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

    while (len > 0) {
        n = read(sock, buf, len);

        if (n > 0) {

            buf += n;
            len -= n;

        } else if (n == 0) {

            return 0;

        } else {
#ifdef WIN32
			errno = WSAGetLastError();
#endif
	    if (errno == EINTR)
		continue;

            if (errno != EWOULDBLOCK && errno != EAGAIN) {
                return n;
            }

            FD_ZERO(&fds);
            FD_SET(sock, &fds);
            tv.tv_sec = timeout / 1000;
            tv.tv_usec = (timeout % 1000) * 1000;
            n = select(sock+1, &fds, NULL, &fds, &tv);
            if (n < 0) {
                rfbLogPerror("ReadExact: select");
                return n;
            }
            if (n == 0) {
                errno = ETIMEDOUT;
                return -1;
            }
        }
    }
    return 1;
}

int ReadExact(rfbClientPtr cl,char* buf,int len)
{
  return(ReadExactTimeout(cl,buf,len,rfbMaxClientWait));
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
#ifdef VINO_HAVE_GNUTLS
extern int ReadPending(rfbClientPtr cl)
{
    if (cl->useTLS && cl->tlsSession)
        return gnutls_record_check_pending(cl->tlsSession);
    else
        return 0;
}
#else
extern int ReadPending(rfbClientPtr cl)
{
    return 0;
}
#endif

474
#ifdef VINO_HAVE_GNUTLS
Mark McLoughlin's avatar
Mark McLoughlin committed
475
static int
476
WriteExactOverTLS(rfbClientPtr cl, const char* buf, int len)
Mark McLoughlin's avatar
Mark McLoughlin committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
{
    LOCK(cl->outputMutex);

    while (len > 0) {
	int n;

	n = gnutls_record_send(cl->tlsSession, buf, len);
	if (n == 0) {
	    UNLOCK(cl->outputMutex);
	    return 0;

	} else if (n < 0) {
	    if (n == GNUTLS_E_INTERRUPTED || n == GNUTLS_E_AGAIN)
		continue;

	    UNLOCK(cl->outputMutex);
	    return -1;
	}

	buf += n;
	len -= n;
    }

    UNLOCK(cl->outputMutex);

    return 1;
}
504
#endif /* VINO_HAVE_GNUTLS */
Mark McLoughlin's avatar
Mark McLoughlin committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

/*
 * WriteExact writes an exact number of bytes to a client.  Returns 1 if
 * those bytes have been written, or -1 if an error occurred (errno is set to
 * ETIMEDOUT if it timed out).
 */

int
WriteExact(rfbClientPtr cl, const char* buf, int len)
{
    int sock = cl->sock;
    int n;
    fd_set fds;
    struct timeval tv;
    int totalTimeWaited = 0;

521
#ifdef VINO_HAVE_GNUTLS
522 523
    if (cl->useTLS)
	return WriteExactOverTLS(cl, buf, len);
524
#endif
Mark McLoughlin's avatar
Mark McLoughlin committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

    LOCK(cl->outputMutex);
    while (len > 0) {
        n = write(sock, buf, len);

        if (n > 0) {

            buf += n;
            len -= n;

        } else if (n == 0) {

            rfbErr("WriteExact: write returned 0?\n");
            return 0;

        } else {
#ifdef WIN32
			errno = WSAGetLastError();
#endif
	    if (errno == EINTR)
		continue;

            if (errno != EWOULDBLOCK && errno != EAGAIN) {
	        UNLOCK(cl->outputMutex);
                return n;
            }

            /* Retry every 5 seconds until we exceed rfbMaxClientWait.  We
               need to do this because select doesn't necessarily return
               immediately when the other end has gone away */

            FD_ZERO(&fds);
            FD_SET(sock, &fds);
            tv.tv_sec = 5;
            tv.tv_usec = 0;
            n = select(sock+1, NULL, &fds, NULL /* &fds */, &tv);
            if (n < 0) {
                rfbLogPerror("WriteExact: select");
                UNLOCK(cl->outputMutex);
                return n;
            }
            if (n == 0) {
                totalTimeWaited += 5000;
                if (totalTimeWaited >= rfbMaxClientWait) {
                    errno = ETIMEDOUT;
                    UNLOCK(cl->outputMutex);
                    return -1;
                }
            } else {
                totalTimeWaited = 0;
            }
        }
    }
    UNLOCK(cl->outputMutex);
    return 1;
}

582 583
rfbBool
ListenOnTCPPort(rfbScreenInfoPtr rfbScreen, int port, const char *netIface)
Mark McLoughlin's avatar
Mark McLoughlin committed
584
{
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  int sock   = -1;
  int *psock = NULL;
  int *ptot  = NULL;
  struct ifaddrs *myaddrs = NULL; 
  struct ifaddrs *ifa     = NULL;

  if(rfbScreen == NULL)
    return FALSE;
  
  psock = rfbScreen->rfbListenSock;
  ptot  = &rfbScreen->rfbListenSockTotal;
  *ptot = 0;

  if(netIface == NULL || strlen(netIface) == 0)
  {
David King's avatar
David King committed
600
#ifdef VINO_ENABLE_IPV6
601
    int sock6 = -1;
602
    struct sockaddr_in6 s6;
603

604 605 606 607
    memset(&s6, 0, sizeof(s6));
    s6.sin6_family = AF_INET6;
    s6.sin6_port   = htons(port);
    s6.sin6_addr   = in6addr_any;
608

609 610
    sock6 = NewSocketListenTCP ((struct sockaddr*)&s6, sizeof(s6));
    rfbLog("Listening IPv6://[::]:%d\n", port);
611
#endif
612

613
      struct sockaddr_in s4;
614

615 616 617 618
      memset(&s4, 0, sizeof(s4));
      s4.sin_family      = AF_INET;
      s4.sin_port        = htons(port);
      s4.sin_addr.s_addr = htonl(INADDR_ANY);
619

620 621
      sock = NewSocketListenTCP ((struct sockaddr*)&s4, sizeof(s4));
      rfbLog("Listening IPv4://0.0.0.0:%d\n", port);
622

623 624 625 626 627 628
#ifdef VINO_ENABLE_IPV6
    if(sock6 > 0) {
       psock[*ptot] = sock6;
      *ptot        += 1;
    }
#endif
629
    if(sock > 0) {
630 631
       psock[*ptot] = sock;
      *ptot        += 1;
Mark McLoughlin's avatar
Mark McLoughlin committed
632
    }
633

634 635 636 637 638
    if (*ptot)
      return TRUE;

    /* no need to log sock/sock6, both are -1 here */
    rfbLog("Problems in NewSocketListenTCP()\n");
639 640 641 642 643 644 645 646 647 648 649 650 651 652
    return FALSE;
  }

  if(getifaddrs(&myaddrs) != 0) {
    rfbLogPerror("getifaddrs\n");
    return FALSE;
  }

  for (ifa = myaddrs; ifa != NULL && *ptot < RFB_MAX_SOCKETLISTEN; ifa = ifa->ifa_next) {
    char buf[64] = { 0, };

    if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_UP) == 0) 
      continue;

653
    if (ifa->ifa_addr->ADDR_FAMILY_MEMBER == AF_INET) {
654 655 656 657 658 659 660 661 662 663 664
      struct sockaddr_in *s4 = (struct sockaddr_in*)ifa->ifa_addr;
      s4->sin_port           = htons(port);

      if (inet_ntop(s4->sin_family, (struct sockaddr*)&s4->sin_addr, buf, sizeof(buf)) == NULL) {
        rfbLog("%s: inet_ntop failed!\n", ifa->ifa_name);
        continue;
      }
      else if(!strcmp(ifa->ifa_name, netIface)) {
        rfbLog("Listening IPv4://%s:%d\n", buf, port);
        sock = NewSocketListenTCP((struct sockaddr*)s4, INET_ADDRSTRLEN);
      }
Mark McLoughlin's avatar
Mark McLoughlin committed
665
    }
David King's avatar
David King committed
666
#ifdef VINO_ENABLE_IPV6
667
    if (ifa->ifa_addr->ADDR_FAMILY_MEMBER == AF_INET6) {
668 669 670
      struct sockaddr_in6 *s6 = (struct sockaddr_in6*)ifa->ifa_addr;
      s6->sin6_port           = htons(port);

671
      if (inet_ntop(ifa->ifa_addr->ADDR_FAMILY_MEMBER, (struct sockaddr*)&s6->sin6_addr, buf, sizeof(buf)) == NULL) {
672 673 674 675 676 677 678
        rfbLog("%s: inet_ntop failed!\n", ifa->ifa_name);
        continue; 
      }
      else if(!strcmp(ifa->ifa_name, netIface)) {
        rfbLog("Listening IPv6://%s:%d\n", buf, port);
        sock = NewSocketListenTCP((struct sockaddr*)s6, INET6_ADDRSTRLEN);
      }
Mark McLoughlin's avatar
Mark McLoughlin committed
679
    }
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
#endif       

    if(sock > 0) {
       psock[*ptot] = sock;
      *ptot        += 1;
       sock         = -1;
    }
  }

  freeifaddrs(myaddrs);

  return TRUE;
}

int
NewSocketListenTCP(struct sockaddr *addr, socklen_t len)
{
    int sock = -1;
    int one  = 1;

    if ((sock = socket(addr->sa_family, SOCK_STREAM, 0)) < 0)
      return -1;

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)) < 0) {
      close(sock);
      return -1;
    }

708 709 710 711 712 713 714 715 716
#ifdef VINO_ENABLE_IPV6
    if (addr->sa_family == AF_INET6) {
#if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY)
      setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&one, sizeof(one));
      /* we cannot really check for errors here */
#endif
    }
#endif

717 718 719 720 721
    if (bind(sock, addr, len) < 0) {
      close(sock);
      return -1;
    }

Mark McLoughlin's avatar
Mark McLoughlin committed
722
    if (listen(sock, 5) < 0) {
723 724
      close(sock);
      return -1;
Mark McLoughlin's avatar
Mark McLoughlin committed
725 726 727 728
    }

    return sock;
}
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

rfbBool
rfbSetNetworkInterface(rfbScreenInfoPtr rfbScreen, const char *netIface)
{
  int i;

  if (!rfbScreen->socketInitDone || !rfbScreen->socketInitDone)
    return FALSE;

  if(rfbScreen->rfbListenSockTotal > 0) {
    for(i=0; i < rfbScreen->rfbListenSockTotal; i++) {
       FD_CLR(rfbScreen->rfbListenSock[i], &(rfbScreen->allFds));
       close(rfbScreen->rfbListenSock[i]);
       rfbScreen->rfbListenSock[i] = -1;
    }
    rfbScreen->rfbListenSockTotal = 0;
  }

  if(netIface != NULL && strlen(netIface) > 0 && if_nametoindex(netIface) > 0) {
     rfbScreen->netIface = netIface;
  }
  else {
     rfbScreen->netIface = NULL;
     if(netIface != NULL)
        rfbLog("WARNING: This (%s) a invalid network interface, set to all\n", netIface);
  }

  rfbLog("Re-binding socket to listen for VNC connections on TCP port %d in (%s) interface\n",
         rfbScreen->rfbPort, rfbScreen->netIface != NULL ? rfbScreen->netIface : "all");
 
  if (!ListenOnTCPPort(rfbScreen, rfbScreen->rfbPort, rfbScreen->netIface)) {
    rfbLogPerror("ListenOnTCPPort");
    return FALSE;
  }

  for(i=0; i < rfbScreen->rfbListenSockTotal; i++) {
    FD_SET(rfbScreen->rfbListenSock[i], &(rfbScreen->allFds));
    rfbScreen->maxFd = max(rfbScreen->rfbListenSock[i], rfbScreen->maxFd);
  }
 
  return TRUE;
}