Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef __STRICT_ANSI__
00026 #define _BSD_SOURCE
00027 #endif
00028 #include <unistd.h>
00029 #include <sys/types.h>
00030 #ifdef __MINGW32__
00031 #define close closesocket
00032 #include <winsock2.h>
00033 #undef max
00034 #else
00035 #include <sys/wait.h>
00036 #include <sys/utsname.h>
00037 #endif
00038 #include <sys/time.h>
00039 #include <rfb/rfbclient.h>
00040
00041
00042
00043
00044
00045
00046 void
00047 listenForIncomingConnections(rfbClient* client)
00048 {
00049 #ifdef __MINGW32__
00050
00051 rfbClientErr("listenForIncomingConnections on MinGW32 NOT IMPLEMENTED\n");
00052 return;
00053 #else
00054 int listenSocket, listen6Socket = -1;
00055 fd_set fds;
00056
00057 client->listenSpecified = TRUE;
00058
00059 listenSocket = ListenAtTcpPortAndAddress(client->listenPort, client->listenAddress);
00060
00061 if ((listenSocket < 0))
00062 return;
00063
00064 rfbClientLog("%s -listen: Listening on port %d\n",
00065 client->programName,client->listenPort);
00066 rfbClientLog("%s -listen: Command line errors are not reported until "
00067 "a connection comes in.\n", client->programName);
00068
00069 #ifdef LIBVNCSERVER_IPv6
00070
00071 if (client->listen6Port > 0)
00072 {
00073 listen6Socket = ListenAtTcpPortAndAddress(client->listen6Port, client->listen6Address);
00074
00075 if (listen6Socket < 0)
00076 return;
00077
00078 rfbClientLog("%s -listen: Listening on IPV6 port %d\n",
00079 client->programName,client->listenPort);
00080 rfbClientLog("%s -listen: Command line errors are not reported until "
00081 "a connection comes in.\n", client->programName);
00082 }
00083 #endif
00084
00085 while (TRUE) {
00086 int r;
00087
00088 int status, pid;
00089 while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
00090
00091
00092
00093 FD_ZERO(&fds);
00094
00095 if(listenSocket >= 0)
00096 FD_SET(listenSocket, &fds);
00097 if(listen6Socket >= 0)
00098 FD_SET(listen6Socket, &fds);
00099
00100 r = select(max(listenSocket, listen6Socket)+1, &fds, NULL, NULL, NULL);
00101
00102 if (r > 0) {
00103 if (FD_ISSET(listenSocket, &fds))
00104 client->sock = AcceptTcpConnection(client->listenSock);
00105 else if (FD_ISSET(listen6Socket, &fds))
00106 client->sock = AcceptTcpConnection(client->listen6Sock);
00107
00108 if (client->sock < 0)
00109 return;
00110 if (!SetNonBlocking(client->sock))
00111 return;
00112
00113
00114
00115 switch (fork()) {
00116
00117 case -1:
00118 rfbClientErr("fork\n");
00119 return;
00120
00121 case 0:
00122
00123 close(listenSocket);
00124 close(listen6Socket);
00125 return;
00126
00127 default:
00128
00129 close(client->sock);
00130 break;
00131 }
00132 }
00133 }
00134 #endif
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 int
00148 listenForIncomingConnectionsNoFork(rfbClient* client, int timeout)
00149 {
00150 fd_set fds;
00151 struct timeval to;
00152 int r;
00153
00154 to.tv_sec= timeout / 1000000;
00155 to.tv_usec= timeout % 1000000;
00156
00157 client->listenSpecified = TRUE;
00158
00159 if (client->listenSock < 0)
00160 {
00161 client->listenSock = ListenAtTcpPortAndAddress(client->listenPort, client->listenAddress);
00162
00163 if (client->listenSock < 0)
00164 return -1;
00165
00166 rfbClientLog("%s -listennofork: Listening on port %d\n",
00167 client->programName,client->listenPort);
00168 rfbClientLog("%s -listennofork: Command line errors are not reported until "
00169 "a connection comes in.\n", client->programName);
00170 }
00171
00172 #ifdef LIBVNCSERVER_IPv6
00173
00174 if (client->listen6Port > 0 && client->listen6Sock < 0)
00175 {
00176 client->listen6Sock = ListenAtTcpPortAndAddress(client->listen6Port, client->listen6Address);
00177
00178 if (client->listen6Sock < 0)
00179 return -1;
00180
00181 rfbClientLog("%s -listennofork: Listening on IPV6 port %d\n",
00182 client->programName,client->listenPort);
00183 rfbClientLog("%s -listennofork: Command line errors are not reported until "
00184 "a connection comes in.\n", client->programName);
00185 }
00186 #endif
00187
00188 FD_ZERO(&fds);
00189
00190 if(client->listenSock >= 0)
00191 FD_SET(client->listenSock, &fds);
00192 if(client->listen6Sock >= 0)
00193 FD_SET(client->listen6Sock, &fds);
00194
00195 if (timeout < 0)
00196 r = select(max(client->listenSock, client->listen6Sock) +1, &fds, NULL, NULL, NULL);
00197 else
00198 r = select(max(client->listenSock, client->listen6Sock) +1, &fds, NULL, NULL, &to);
00199
00200 if (r > 0)
00201 {
00202 if (FD_ISSET(client->listenSock, &fds))
00203 client->sock = AcceptTcpConnection(client->listenSock);
00204 else if (FD_ISSET(client->listen6Sock, &fds))
00205 client->sock = AcceptTcpConnection(client->listen6Sock);
00206
00207 if (client->sock < 0)
00208 return -1;
00209 if (!SetNonBlocking(client->sock))
00210 return -1;
00211
00212 if(client->listenSock >= 0) {
00213 close(client->listenSock);
00214 client->listenSock = -1;
00215 }
00216 if(client->listen6Sock >= 0) {
00217 close(client->listen6Sock);
00218 client->listen6Sock = -1;
00219 }
00220 return r;
00221 }
00222
00223
00224 return r;
00225 }
00226
00227