static void* listenerRun(void *data) { rfbScreenInfoPtr rfbScreen=(rfbScreenInfoPtr)data; int client_fd; struct sockaddr_in peer; rfbClientPtr cl; socklen_t len; if (rfbScreen->inetdSock != -1) { cl = rfbNewClient(rfbScreen, rfbScreen->inetdSock); if (cl && !cl->onHold ) rfbStartOnHoldClient(cl); return 0; } len = sizeof(peer); /* TODO: this thread wont die by restarting the server */ while ((client_fd = accept(rfbScreen->rfbListenSock, (struct sockaddr*)&peer, &len)) >= 0) { cl = rfbNewClient(rfbScreen,client_fd); len = sizeof(peer); if (cl && !cl->onHold ) rfbStartOnHoldClient(cl); } return NULL; }
void rfbProcessNewConnection(rfbScreenInfoPtr rfbScreen, int insock) { const int one = 1; int sock = -1; if((sock = accept(insock, NULL, NULL)) < 0) { rfbLogPerror("rfbCheckFds: accept"); return; } if(sock < 0) { rfbLogPerror("rfbCheckFds: accept"); return; } #ifndef WIN32 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { rfbLogPerror("rfbCheckFds: fcntl"); close(sock); return; } #endif if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) { rfbLogPerror("rfbCheckFds: setsockopt"); close(sock); return; } rfbNewClient(rfbScreen,sock); }
rfbBool rfbProcessNewConnection(rfbScreenInfoPtr rfbScreen) { const int one = 1; int sock = -1; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); if ((sock = accept(rfbScreen->listenSock, (struct sockaddr *)&addr, &addrlen)) < 0) { rfbLogPerror("rfbCheckFds: accept"); return FALSE; } if(!rfbSetNonBlocking(sock)) { closesocket(sock); return FALSE; } if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) { rfbLogPerror("rfbCheckFds: setsockopt"); closesocket(sock); return FALSE; } #ifdef USE_LIBWRAP if(!hosts_ctl("vnc",STRING_UNKNOWN,inet_ntoa(addr.sin_addr), STRING_UNKNOWN)) { rfbLog("Rejected connection from client %s\n", inet_ntoa(addr.sin_addr)); closesocket(sock); return FALSE; } #endif rfbLog("Got connection from client %s\n", inet_ntoa(addr.sin_addr)); rfbNewClient(rfbScreen,sock); return TRUE; }
static void* listenerRun(void *data) { rfbScreenInfoPtr screen=(rfbScreenInfoPtr)data; int client_fd; struct sockaddr_storage peer; rfbClientPtr cl = NULL; socklen_t len; fd_set listen_fds; /* temp file descriptor list for select() */ /* TODO: this thread wont die by restarting the server */ /* TODO: HTTP is not handled */ while (1) { client_fd = -1; FD_ZERO(&listen_fds); if(screen->listenSock >= 0) FD_SET(screen->listenSock, &listen_fds); if(screen->listen6Sock >= 0) FD_SET(screen->listen6Sock, &listen_fds); if (select(screen->maxFd+1, &listen_fds, NULL, NULL, NULL) == -1) { rfbLogPerror("listenerRun: error in select"); return NULL; } /* there is something on the listening sockets, handle new connections */ len = sizeof (peer); if (FD_ISSET(screen->listenSock, &listen_fds)) client_fd = accept(screen->listenSock, (struct sockaddr*)&peer, &len); else if (FD_ISSET(screen->listen6Sock, &listen_fds)) client_fd = accept(screen->listen6Sock, (struct sockaddr*)&peer, &len); if(client_fd >= 0) cl = rfbNewClient(screen,client_fd); if (cl && !cl->onHold ) rfbStartOnHoldClient(cl); } return(NULL); }
static void* listenerRun(void *data) { rfbScreenInfoPtr screen=(rfbScreenInfoPtr)data; int client_fd; struct sockaddr_in peer; rfbClientPtr cl; socklen_t len; len = sizeof(peer); /* TODO: this thread wont die by restarting the server */ /* TODO: HTTP is not handled */ while ((client_fd = accept(screen->listenSock, (struct sockaddr*)&peer, &len)) >= 0) { cl = rfbNewClient(screen,client_fd); len = sizeof(peer); if (cl && !cl->onHold ) rfbStartOnHoldClient(cl); } return(NULL); }
int main(int argc,char** argv) { char *repeaterHost; int repeaterPort, sock; char id[250]; int idlen; rfbClientPtr cl; int i,j; uint16_t* f; /* Parse command-line arguments */ if (argc < 3) { fprintf(stderr, "Usage: %s <id> <repeater-host> [<repeater-port>]\n", argv[0]); exit(1); } idlen = snprintf(id, sizeof(id) - 1, "ID:%s", argv[1]); if(idlen < 0 || idlen >= (int)sizeof(id)) { fprintf(stderr, "Error, given ID is probably too long.\n"); return 1; } repeaterHost = argv[2]; repeaterPort = argc < 4 ? 5500 : atoi(argv[3]); /* The initialization is identical to simple15.c */ rfbScreenInfoPtr server=rfbGetScreen(&argc,argv,400,300,5,3,2); if(!server) return 0; server->frameBuffer=(char*)malloc(400*300*2); f=(uint16_t*)server->frameBuffer; for(j=0;j<300;j++) for(i=0;i<400;i++) f[j*400+i]=/* red */ ((j*32/300) << 10) | /* green */ (((j+400-i)*32/700) << 5) | /* blue */ ((i*32/400)); /* Now for the repeater-specific part: */ server->port = -1; /* do not listen on any port */ server->ipv6port = -1; /* do not listen on any port */ sock = rfbConnectToTcpAddr(repeaterHost, repeaterPort); if (sock < 0) { perror("connect to repeater"); return 1; } if (write(sock, id, idlen+1) != idlen+1) { perror("writing id"); return 1; } cl = rfbNewClient(server, sock); if (!cl) { perror("new client"); return 1; } cl->reverseConnection = 0; cl->clientGoneHook = clientGone; /* Run the server */ rfbInitServer(server); rfbRunEventLoop(server,-1,FALSE); return 0; }
int rfbCheckFds(rfbScreenInfoPtr rfbScreen,long usec) { int nfds; fd_set fds; struct timeval tv; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); char buf[6]; const int one = 1; int sock; rfbClientIteratorPtr i; rfbClientPtr cl; struct timeval tv_msg; fd_set fds_msg; int nfds_msg; int result = 0; if (!rfbScreen->inetdInitDone && rfbScreen->inetdSock != -1) { rfbNewClientConnection(rfbScreen,rfbScreen->inetdSock); rfbScreen->inetdInitDone = TRUE; } do { memcpy((char *)&fds, (char *)&(rfbScreen->allFds), sizeof(fd_set)); tv.tv_sec = 0; tv.tv_usec = usec; nfds = select(rfbScreen->maxFd + 1, &fds, NULL, NULL /* &fds */, &tv); if (nfds == 0) { /* timed out, check for async events */ i = rfbGetClientIterator(rfbScreen); while((cl = rfbClientIteratorNext(i))) { if (cl->onHold) continue; if (FD_ISSET(cl->sock, &(rfbScreen->allFds))) rfbSendFileTransferChunk(cl); } rfbReleaseClientIterator(i); return result; } if (nfds < 0) { #ifdef WIN32 errno = WSAGetLastError(); #endif if (errno != EINTR) rfbLogPerror("rfbCheckFds: select"); return -1; } result += nfds; if (rfbScreen->listenSock != -1 && FD_ISSET(rfbScreen->listenSock, &fds)) { if ((sock = accept(rfbScreen->listenSock, (struct sockaddr *)&addr, &addrlen)) < 0) { rfbLogPerror("rfbCheckFds: accept"); return -1; } #ifndef WIN32 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { rfbLogPerror("rfbCheckFds: fcntl"); closesocket(sock); return -1; } #endif if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) { rfbLogPerror("rfbCheckFds: setsockopt"); closesocket(sock); return -1; } #ifdef USE_LIBWRAP if(!hosts_ctl("vnc",STRING_UNKNOWN,inet_ntoa(addr.sin_addr), STRING_UNKNOWN)) { rfbLog("Rejected connection from client %s\n", inet_ntoa(addr.sin_addr)); closesocket(sock); return -1; } #endif rfbLog("Got connection from client %s\n", inet_ntoa(addr.sin_addr)); rfbNewClient(rfbScreen,sock); FD_CLR(rfbScreen->listenSock, &fds); if (--nfds == 0) return result; } if ((rfbScreen->udpSock != -1) && FD_ISSET(rfbScreen->udpSock, &fds)) { if(!rfbScreen->udpClient) rfbNewUDPClient(rfbScreen); if (recvfrom(rfbScreen->udpSock, buf, 1, MSG_PEEK, (struct sockaddr *)&addr, &addrlen) < 0) { rfbLogPerror("rfbCheckFds: UDP: recvfrom"); rfbDisconnectUDPSock(rfbScreen); rfbScreen->udpSockConnected = FALSE; } else { if (!rfbScreen->udpSockConnected || (memcmp(&addr, &rfbScreen->udpRemoteAddr, addrlen) != 0)) { /* new remote end */ rfbLog("rfbCheckFds: UDP: got connection\n"); memcpy(&rfbScreen->udpRemoteAddr, &addr, addrlen); rfbScreen->udpSockConnected = TRUE; if (connect(rfbScreen->udpSock, (struct sockaddr *)&addr, addrlen) < 0) { rfbLogPerror("rfbCheckFds: UDP: connect"); rfbDisconnectUDPSock(rfbScreen); return -1; } rfbNewUDPConnection(rfbScreen,rfbScreen->udpSock); } rfbProcessUDPInput(rfbScreen); } FD_CLR(rfbScreen->udpSock, &fds); if (--nfds == 0) return result; } i = rfbGetClientIterator(rfbScreen); while((cl = rfbClientIteratorNext(i))) { if (cl->onHold) continue; if (FD_ISSET(cl->sock, &(rfbScreen->allFds))) { if (FD_ISSET(cl->sock, &fds)) { tv_msg.tv_sec = 0; tv_msg.tv_usec = 0; FD_ZERO(&fds_msg); FD_SET(cl->sock, &fds_msg); do { /* drain all messages, speed up mouse move processing */ rfbProcessClientMessage(cl); nfds_msg = select(cl->sock + 1, &fds_msg, NULL, NULL, &tv_msg); } while (nfds_msg > 0); } else rfbSendFileTransferChunk(cl); } } rfbReleaseClientIterator(i); } while(rfbScreen->handleEventsEagerly); return result; }
rfbBool rfbProcessNewConnection(rfbScreenInfoPtr rfbScreen) { const int one = 1; int sock = -1; #ifdef LIBVNCSERVER_IPv6 struct sockaddr_storage addr; #else struct sockaddr_in addr; #endif socklen_t addrlen = sizeof(addr); fd_set listen_fds; int chosen_listen_sock = -1; /* Do another select() call to find out which listen socket has an incoming connection pending. We know that at least one of them has, so this should not block for too long! */ FD_ZERO(&listen_fds); if(rfbScreen->listenSock >= 0) FD_SET(rfbScreen->listenSock, &listen_fds); if(rfbScreen->listen6Sock >= 0) FD_SET(rfbScreen->listen6Sock, &listen_fds); if (select(rfbScreen->maxFd+1, &listen_fds, NULL, NULL, NULL) == -1) { rfbLogPerror("rfbProcessNewConnection: error in select"); return FALSE; } if (rfbScreen->listenSock >= 0 && FD_ISSET(rfbScreen->listenSock, &listen_fds)) chosen_listen_sock = rfbScreen->listenSock; if (rfbScreen->listen6Sock >= 0 && FD_ISSET(rfbScreen->listen6Sock, &listen_fds)) chosen_listen_sock = rfbScreen->listen6Sock; if ((sock = accept(chosen_listen_sock, (struct sockaddr *)&addr, &addrlen)) < 0) { rfbLogPerror("rfbCheckFds: accept"); return FALSE; } if(!rfbSetNonBlocking(sock)) { closesocket(sock); return FALSE; } if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)) < 0) { rfbLogPerror("rfbCheckFds: setsockopt"); closesocket(sock); return FALSE; } #ifdef USE_LIBWRAP if(!hosts_ctl("vnc",STRING_UNKNOWN,inet_ntoa(addr.sin_addr), STRING_UNKNOWN)) { rfbLog("Rejected connection from client %s\n", inet_ntoa(addr.sin_addr)); closesocket(sock); return FALSE; } #endif #ifdef LIBVNCSERVER_IPv6 { char host[1024]; if(getnameinfo((struct sockaddr*)&addr, addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST) != 0) { rfbLogPerror("rfbProcessNewConnection: error in getnameinfo"); } rfbLog("Got connection from client %s\n", host); } #else rfbLog("Got connection from client %s\n", inet_ntoa(addr.sin_addr)); #endif rfbNewClient(rfbScreen,sock); return TRUE; }