7347a27b863060441d88d922f5cd0678c8ae493d
[presencevnc] / libvnc / libvncclient / listen.c
1 /*
2  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
3  *
4  *  This is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This software is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this software; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17  *  USA.
18  */
19
20 /*
21  * listen.c - listen for incoming connections
22  */
23
24 #ifdef __STRICT_ANSI__
25 #define _BSD_SOURCE
26 #endif
27 #include <unistd.h>
28 #include <sys/types.h>
29 #ifdef __MINGW32__
30 #include <winsock2.h>
31 #else
32 #include <sys/wait.h>
33 #include <sys/utsname.h>
34 #endif
35 #include <sys/time.h>
36 #include <rfb/rfbclient.h>
37
38 /*
39  * listenForIncomingConnections() - listen for incoming connections from
40  * servers, and fork a new process to deal with each connection.
41  */
42
43 void
44 listenForIncomingConnections(rfbClient* client)
45 {
46 #ifdef __MINGW32__
47   /* FIXME */
48   rfbClientErr("listenForIncomingConnections on MinGW32 NOT IMPLEMENTED\n");
49   return;
50 #else
51   int listenSocket;
52   fd_set fds;
53
54   client->listenSpecified = TRUE;
55
56   listenSocket = ListenAtTcpPort(client->listenPort);
57
58   if ((listenSocket < 0))
59     return;
60
61   rfbClientLog("%s -listen: Listening on port %d\n",
62           client->programName,client->listenPort);
63   rfbClientLog("%s -listen: Command line errors are not reported until "
64           "a connection comes in.\n", client->programName);
65
66   while (TRUE) {
67
68     /* reap any zombies */
69     int status, pid;
70     while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
71
72     /* TODO: callback for discard any events (like X11 events) */
73
74     FD_ZERO(&fds); 
75
76     FD_SET(listenSocket, &fds);
77
78     select(FD_SETSIZE, &fds, NULL, NULL, NULL);
79
80     if (FD_ISSET(listenSocket, &fds)) {
81       client->sock = AcceptTcpConnection(listenSocket);
82       if (client->sock < 0)
83         return;
84       if (!SetNonBlocking(client->sock))
85         return;
86
87       /* Now fork off a new process to deal with it... */
88
89       switch (fork()) {
90
91       case -1: 
92         rfbClientErr("fork\n"); 
93         return;
94
95       case 0:
96         /* child - return to caller */
97         close(listenSocket);
98         return;
99
100       default:
101         /* parent - go round and listen again */
102         close(client->sock); 
103         break;
104       }
105     }
106   }
107 #endif
108 }
109
110