summaryrefslogtreecommitdiff
path: root/transport.c
blob: a650beca0ed3ad00b574287f6d35c91307c78f95 (plain)
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
37
38
39
40
41
42
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
114
115
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* $Xorg: transport.c,v 1.4 2001/02/09 02:05:45 xorgcvs Exp $ */
/*

Copyright "1986-1997, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and the following permission notice
shall be included in all copies of the Software:

THE SOFTWARE IS PROVIDED "AS IS ", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NON-INFRINGEMENT. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER SIABILITIY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
CONNNECTION WITH THE SOFTWARE OR THE USE OF OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of The Open Group
shall not be used in advertising or otherwise to promote the use
or other dealings in this Software without prior written
authorization from The Open Group.

X Window System is a trademark of The Open Group.

*/
/* $XFree86: xc/programs/xfwp/transport.c,v 1.6 2001/07/29 21:23:20 tsi Exp $ */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <X11/Xos.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>

#include <X11/Xfuncs.h>		/* Need for bzero() */

#include <X11/ICE/ICElib.h>	/* Need typedef for Bool */

#include "xfwp.h"
#include "transport.h"

int
doSetupRemClientListen(
    char ** listen_port_string,
    struct clientDataStruct * program_data,
    char * server_address)
{
  int			this_server;
  int 			one = 1;
  struct sockaddr_in 	rem_sockaddr_in;
  int 			listen_port;
  char			hostname[MAX_HOSTNAME_LEN];
  struct timeval	time_val;
  struct timezone	time_zone;
  int			num_servers = program_data->config_info->num_servers;

  /*
   * ugh.  This really shouldn't be kept as a sparse list but no time...
   */
  for (this_server = 0;
       this_server < num_servers && server_array[this_server] != NULL;
       this_server++);
  if (this_server == num_servers)
  {
      (void) fprintf(stderr,
		 "Maximum number of server connections has been reached (%d)\n",
		 program_data->config_info->num_servers);
      return FAILURE;
  }

  /*
   * offset listen port into the X protocol range;
   * must be > X_SERVER_PORT_BASE < 65535
   */
  listen_port = this_server + X_SERVER_PORT_BASE + 1;

  /*
   * allocate the server_array struct and init the fd elements;
   * can't use the PM connection fd as an index into this array, since
   * there could be multiple servers per PM connection
   */
  if ((server_array[this_server] = malloc(sizeof(struct server_list))) == NULL)
  {
    (void) fprintf(stderr,"malloc - server_array\n");
    return FAILURE;
  }

  if ((server_array[this_server]->client_listen_fd =
	socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
      (void) fprintf(stderr,"socket call failed\n");
      free(server_array[this_server]);
      server_array[this_server] = NULL;
      return FAILURE;
  }

  /*
   * this is where we initialize the current time and timeout on this
   * client_listen object
   */
  gettimeofday(&time_val, &time_zone);
  server_array[this_server]->creation_time = time_val.tv_sec;
  server_array[this_server]->time_to_close =
      global_data.config_info->client_listen_timeout;

  /*
   * set up the rest of the remote client listener
   */
  bzero((char * ) &rem_sockaddr_in, sizeof(rem_sockaddr_in));
  rem_sockaddr_in.sin_family = AF_INET;
#ifdef BSD44SOCKETS
  rem_sockaddr_in.sin_len = sizeof rem_sockaddr_in;
#endif

#ifdef SO_REUSEADDR
  if (setsockopt(server_array[this_server]->client_listen_fd,
		 SOL_SOCKET, SO_REUSEADDR,
                 (char *) &one, sizeof(int)) < 0)
  {
      (void) fprintf(stderr, "setsockopt(SO_REUSEADDR) failed\n");
  returnFailure:
      close(server_array[this_server]->client_listen_fd);
      free(server_array[this_server]);
      server_array[this_server] = NULL;
      return FAILURE;
  }
#endif /* SO_REUSEADDR */

  while (True) {
      rem_sockaddr_in.sin_port = htons(listen_port);
      if (bind(server_array[this_server]->client_listen_fd,
	       (struct sockaddr *)&rem_sockaddr_in,
	       sizeof(rem_sockaddr_in)) == 0)
	  break;
      if (errno != EADDRINUSE)
      {
	  (void) fprintf(stderr,"bind call failed\n");
	  goto returnFailure;
      }
      listen_port++;

      /*
       * Can't keep going forever.
       *
       * Why 65535 - it's the same value used by the LBXProxy.
       */
      if (listen_port > 65535)
      {
	  (void) fprintf(stderr,"failed to find a valid port for bind\n");
	  goto returnFailure;
      }
  }

#ifdef DEBUG
  (void) fprintf (stderr, "Client connect port: %d\n", listen_port);
#endif

  if (listen(server_array[this_server]->client_listen_fd, SOMAXCONN) < 0)
  {
      (void) fprintf(stderr, "listen call failed\n");
      goto returnFailure;
  }

  /*
   *  update the nfds
   */
  *(program_data->nfds) = max(*(program_data->nfds),
	  server_array[this_server]->client_listen_fd + 1);

  /*
   * get fully qualified name of host on which FWP is running
   */
  if ((gethostname(hostname, MAX_HOSTNAME_LEN)) < 0)
  {
     (void) fprintf(stderr, "gethostname call failed\n");
     goto returnFailure;
  }

  /*
   * allocate and convert the listen_port string for return to PM;
   * string equals address of host on which FWP is running
   * plus ":<listen_port - X_SERVER_PORT_BASE> (up to xxx)"
   */
  if (((*listen_port_string) = malloc (strlen(hostname) + 10)) == NULL)
  {
    (void) fprintf(stderr, "malloc - proxy address\n");
    goto returnFailure;
  }
  (void) sprintf (*listen_port_string, "%s:%d", hostname,
		  listen_port - X_SERVER_PORT_BASE);

  /*
   * add the server name associated with the current PM request
   * to the list
   */
  if ((server_array[this_server]->x_server_hostport =
       strdup (server_address)) == NULL)
  {
    (void) fprintf(stderr, "malloc - server_array string\n");
    free(*listen_port_string);
    *listen_port_string = NULL;
    goto returnFailure;
  }

  /*
   * add the client listen port associated with the current PM connection
   * to the list
   */
  if ((server_array[this_server]->listen_port_string =
       strdup (*listen_port_string)) == NULL)
  {
    (void) fprintf(stderr, "malloc - server_array string\n");
    free(server_array[this_server]->x_server_hostport);
    free(*listen_port_string);
    *listen_port_string = NULL;
    goto returnFailure;
  }

  /*
   *  set the select() read mask for this descriptor
   */
  FD_SET(server_array[this_server]->client_listen_fd, program_data->rinit);

  return SUCCESS;
}

void
doSelect(struct
    config * config_info,
    int * nfds,
    int * nready,
    fd_set * readable,
    fd_set * writable)
{
  if ((*nready = select(*nfds,
			readable,
			writable,
			NULL,
			&config_info->select_timeout)) == -1)
  {
    if (errno == EINTR)
      return;
    (void) fprintf(stderr, "select call failed\n");
    perror("select");
    exit(1);
  }
}

int
doServerConnectSetup(
    char * x_server_hostport,
    int * server_connect_fd,
    struct sockaddr_in * server_sockaddr_in)
{
  struct hostent * 	hostptr;
  char *		server_name_base;
  char			server_port_base[10];
  int			server_port;
  int			i = 0;
  char *		tmp_str;
  char *		tmp_hostport_str;

  /*
   * need to copy the host port string because strtok() changes it
   */
  if ((tmp_hostport_str = strdup (x_server_hostport)) == NULL)
  {
    (void) fprintf(stderr, "malloc - hostport copy\n");
    return FAILURE;
  }

  tmp_str = x_server_hostport;
  while (tmp_str[i] != ':')
     tmp_str++;
  tmp_str++;
  strcpy(server_port_base, tmp_str);
  server_name_base = strtok(tmp_hostport_str,":");
  server_port = atoi(server_port_base) + X_SERVER_PORT_BASE;
  hostptr = gethostbyname(server_name_base);
  free(tmp_hostport_str);

  if (hostptr == NULL)
  {
    (void) fprintf(stderr, "gethostbyname call failed\n");
    return FAILURE;
  }

  if ((*server_connect_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    (void) fprintf(stderr, "socket call for server failed: %s\n",
		   strerror(errno));
    return FAILURE;
  }

  memset(server_sockaddr_in, 0, sizeof(*server_sockaddr_in));
  server_sockaddr_in->sin_family = hostptr->h_addrtype;
#ifdef BSD44SOCKETS
  server_sockaddr_in->sin_len = sizeof server_sockaddr_in;
#endif
  memcpy((char *) &server_sockaddr_in->sin_addr,
	 hostptr->h_addr,
	 hostptr->h_length);
  server_sockaddr_in->sin_port = htons(server_port);

  return SUCCESS;
}

int
doServerConnect(
    int * server_connect_fd,
    struct sockaddr_in * server_sockaddr_in)
{
  if(connect(*server_connect_fd, (struct sockaddr * )server_sockaddr_in,
	     sizeof(*server_sockaddr_in)) < 0)
  {
    (void) fprintf(stderr, "connect call to server failed: %s\n",
	    	   strerror(errno));
    return FAILURE;
  }
  return SUCCESS;
}