~ubuntu-branches/ubuntu/raring/remmina/raring

« back to all changes in this revision

Viewing changes to libvncserver/libvncclient/listen.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2010-07-11 23:40:47 UTC
  • mfrom: (1.2.1 upstream) (8.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100711234047-id2bdu1gb59e34n4
* New upstream release.
* debian/control:
  - Bump Standards-Version to 3.9.0, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#define close closesocket
31
 
#include <winsock2.h>
32
 
#else
33
 
#include <sys/wait.h>
34
 
#include <sys/utsname.h>
35
 
#endif
36
 
#include <sys/time.h>
37
 
#include <rfb/rfbclient.h>
38
 
 
39
 
/*
40
 
 * listenForIncomingConnections() - listen for incoming connections from
41
 
 * servers, and fork a new process to deal with each connection.
42
 
 */
43
 
 
44
 
void
45
 
listenForIncomingConnections(rfbClient* client)
46
 
{
47
 
#ifdef __MINGW32__
48
 
  /* FIXME */
49
 
  rfbClientErr("listenForIncomingConnections on MinGW32 NOT IMPLEMENTED\n");
50
 
  return;
51
 
#else
52
 
  int listenSocket;
53
 
  fd_set fds;
54
 
 
55
 
  client->listenSpecified = TRUE;
56
 
 
57
 
  listenSocket = ListenAtTcpPort(client->listenPort);
58
 
 
59
 
  if ((listenSocket < 0))
60
 
    return;
61
 
 
62
 
  rfbClientLog("%s -listen: Listening on port %d\n",
63
 
          client->programName,client->listenPort);
64
 
  rfbClientLog("%s -listen: Command line errors are not reported until "
65
 
          "a connection comes in.\n", client->programName);
66
 
 
67
 
  while (TRUE) {
68
 
 
69
 
    /* reap any zombies */
70
 
    int status, pid;
71
 
    while ((pid= wait3(&status, WNOHANG, (struct rusage *)0))>0);
72
 
 
73
 
    /* TODO: callback for discard any events (like X11 events) */
74
 
 
75
 
    FD_ZERO(&fds); 
76
 
 
77
 
    FD_SET(listenSocket, &fds);
78
 
 
79
 
    select(listenSocket+1, &fds, NULL, NULL, NULL);
80
 
 
81
 
    if (FD_ISSET(listenSocket, &fds)) {
82
 
      client->sock = AcceptTcpConnection(listenSocket);
83
 
      if (client->sock < 0)
84
 
        return;
85
 
      if (!SetNonBlocking(client->sock))
86
 
        return;
87
 
 
88
 
      /* Now fork off a new process to deal with it... */
89
 
 
90
 
      switch (fork()) {
91
 
 
92
 
      case -1: 
93
 
        rfbClientErr("fork\n"); 
94
 
        return;
95
 
 
96
 
      case 0:
97
 
        /* child - return to caller */
98
 
        close(listenSocket);
99
 
        return;
100
 
 
101
 
      default:
102
 
        /* parent - go round and listen again */
103
 
        close(client->sock); 
104
 
        break;
105
 
      }
106
 
    }
107
 
  }
108
 
#endif
109
 
}
110
 
 
111
 
 
112
 
 
113
 
/*
114
 
 * listenForIncomingConnectionsNoFork() - listen for incoming connections
115
 
 * from servers, but DON'T fork, instead just wait timeout microseconds.
116
 
 * If timeout is negative, block indefinitly.
117
 
 * Returns 1 on success (there was an incoming connection on the listen socket
118
 
 * and we accepted it successfully), -1 on error, 0 on timeout.
119
 
 */
120
 
 
121
 
int
122
 
listenForIncomingConnectionsNoFork(rfbClient* client, int timeout)
123
 
{
124
 
  fd_set fds;
125
 
  struct timeval to;
126
 
  int r;
127
 
 
128
 
  to.tv_sec= timeout / 1000000;
129
 
  to.tv_usec= timeout % 1000000;
130
 
 
131
 
  client->listenSpecified = TRUE;
132
 
 
133
 
  if (client->listenSock < 0)
134
 
    {
135
 
      client->listenSock = ListenAtTcpPort(client->listenPort);
136
 
 
137
 
      if (client->listenSock < 0)
138
 
        return -1;
139
 
 
140
 
      rfbClientLog("%s -listennofork: Listening on port %d\n",
141
 
                   client->programName,client->listenPort);
142
 
      rfbClientLog("%s -listennofork: Command line errors are not reported until "
143
 
                   "a connection comes in.\n", client->programName);
144
 
    }
145
 
 
146
 
  FD_ZERO(&fds);
147
 
 
148
 
  FD_SET(client->listenSock, &fds);
149
 
 
150
 
  if (timeout < 0)
151
 
    r = select(client->listenSock+1, &fds, NULL, NULL, NULL);
152
 
  else
153
 
    r = select(client->listenSock+1, &fds, NULL, NULL, &to);
154
 
 
155
 
  if (r > 0)
156
 
    {
157
 
      client->sock = AcceptTcpConnection(client->listenSock);
158
 
      if (client->sock < 0)
159
 
        return -1;
160
 
      if (!SetNonBlocking(client->sock))
161
 
        return -1;
162
 
 
163
 
      close(client->listenSock);
164
 
      return r;
165
 
    }
166
 
 
167
 
  /* r is now either 0 (timeout) or -1 (error) */
168
 
  return r;
169
 
}
170
 
 
171