| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */ |
| 4 |
/* Copyright (c) 2008-2017 Oleksandr Shneyder <[email protected]> */ |
| 5 |
/* Copyright (c) 2011-2022 Mike Gabriel <[email protected]>*/ |
| 6 |
/* Copyright (c) 2014-2019 Mihai Moldovan <[email protected]> */ |
| 7 |
/* Copyright (c) 2014-2022 Ulrich Sibiller <[email protected]> */ |
| 8 |
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */ |
| 9 |
/* */ |
| 10 |
/* nx-X11, NX protocol compression and NX extensions to this software */ |
| 11 |
/* are copyright of the aforementioned persons and companies. */ |
| 12 |
/* */ |
| 13 |
/* Redistribution and use of the present software is allowed according */ |
| 14 |
/* to terms specified in the file LICENSE which comes in the source */ |
| 15 |
/* distribution. */ |
| 16 |
/* */ |
| 17 |
/* All rights reserved. */ |
| 18 |
/* */ |
| 19 |
/* NOTE: This software has received contributions from various other */ |
| 20 |
/* contributors, only the core maintainers and supporters are listed as */ |
| 21 |
/* copyright holders. Please contact us, if you feel you should be listed */ |
| 22 |
/* as copyright holder, as well. */ |
| 23 |
/* */ |
| 24 |
/**************************************************************************/ |
| 25 |
|
| 26 |
/* |
| 27 |
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
| 28 |
* |
| 29 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 30 |
* copy of this software and associated documentation files (the "Software"), |
| 31 |
* to deal in the Software without restriction, including without limitation |
| 32 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 33 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 34 |
* Software is furnished to do so, subject to the following conditions: |
| 35 |
* |
| 36 |
* The above copyright notice and this permission notice (including the next |
| 37 |
* paragraph) shall be included in all copies or substantial portions of the |
| 38 |
* Software. |
| 39 |
* |
| 40 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 41 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 42 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 43 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 44 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 45 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 46 |
* DEALINGS IN THE SOFTWARE. |
| 47 |
*/ |
| 48 |
/* |
| 49 |
|
| 50 |
Copyright 1993, 1994, 1998 The Open Group |
| 51 |
|
| 52 |
Permission to use, copy, modify, distribute, and sell this software and its |
| 53 |
documentation for any purpose is hereby granted without fee, provided that |
| 54 |
the above copyright notice appear in all copies and that both that |
| 55 |
copyright notice and this permission notice appear in supporting |
| 56 |
documentation. |
| 57 |
|
| 58 |
The above copyright notice and this permission notice shall be included |
| 59 |
in all copies or substantial portions of the Software. |
| 60 |
|
| 61 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 62 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 63 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 64 |
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 65 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 66 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 67 |
OTHER DEALINGS IN THE SOFTWARE. |
| 68 |
|
| 69 |
Except as contained in this notice, the name of the copyright holders shall |
| 70 |
not be used in advertising or otherwise to promote the sale, use or |
| 71 |
other dealings in this Software without prior written authorization |
| 72 |
from the copyright holders. |
| 73 |
|
| 74 |
* Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA |
| 75 |
* |
| 76 |
* All Rights Reserved |
| 77 |
* |
| 78 |
* Permission to use, copy, modify, and distribute this software and its |
| 79 |
* documentation for any purpose and without fee is hereby granted, provided |
| 80 |
* that the above copyright notice appear in all copies and that both that |
| 81 |
* copyright notice and this permission notice appear in supporting |
| 82 |
* documentation, and that the name NCR not be used in advertising |
| 83 |
* or publicity pertaining to distribution of the software without specific, |
| 84 |
* written prior permission. NCR makes no representations about the |
| 85 |
* suitability of this software for any purpose. It is provided "as is" |
| 86 |
* without express or implied warranty. |
| 87 |
* |
| 88 |
* NCR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 89 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 90 |
* NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 91 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 92 |
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 93 |
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 94 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 95 |
*/ |
| 96 |
|
| 97 |
#ifdef NX_TRANS_SOCKET |
| 98 |
|
| 99 |
#ifdef NX_TRANS_DEBUG |
| 100 |
#define XTRANSDEBUG 5 |
| 101 |
#endif |
| 102 |
|
| 103 |
#ifndef PF_LOCAL |
| 104 |
#define PF_LOCAL PF_UNIX |
| 105 |
#endif |
| 106 |
|
| 107 |
#endif |
| 108 |
|
| 109 |
#include <ctype.h> |
| 110 |
#ifdef XTHREADS |
| 111 |
#include <nx-X11/Xthreads.h> |
| 112 |
#endif |
| 113 |
|
| 114 |
#ifndef WIN32 |
| 115 |
|
| 116 |
#if defined(TCPCONN) || defined(UNIXCONN) |
| 117 |
#include <sys/socket.h> |
| 118 |
#include <netinet/in.h> |
| 119 |
#include <arpa/inet.h> |
| 120 |
#endif |
| 121 |
|
| 122 |
#if defined(TCPCONN) || defined(UNIXCONN) |
| 123 |
#define X_INCLUDE_NETDB_H |
| 124 |
#define XOS_USE_NO_LOCKING |
| 125 |
#include <nx-X11/Xos_r.h> |
| 126 |
#endif |
| 127 |
|
| 128 |
#ifdef UNIXCONN |
| 129 |
#ifndef X_NO_SYS_UN |
| 130 |
#include <sys/un.h> |
| 131 |
#endif |
| 132 |
#include <sys/stat.h> |
| 133 |
#endif |
| 134 |
|
| 135 |
|
| 136 |
#ifndef NO_TCP_H |
| 137 |
#if defined(linux) || defined(__GLIBC__) |
| 138 |
#include <sys/param.h> |
| 139 |
#endif /* linux || __GLIBC__ */ |
| 140 |
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) |
| 141 |
#include <sys/param.h> |
| 142 |
#include <machine/endian.h> |
| 143 |
#endif /* __NetBSD__ || __OpenBSD__ || __FreeBSD__ || __DragonFly__ */ |
| 144 |
#include <netinet/tcp.h> |
| 145 |
#endif /* !NO_TCP_H */ |
| 146 |
|
| 147 |
#include <sys/ioctl.h> |
| 148 |
#if defined(SVR4) || defined(__SVR4) |
| 149 |
#include <sys/filio.h> |
| 150 |
#endif |
| 151 |
|
| 152 |
#include <unistd.h> |
| 153 |
|
| 154 |
#else /* !WIN32 */ |
| 155 |
|
| 156 |
#include <nx-X11/Xwinsock.h> |
| 157 |
#include <nx-X11/Xwindows.h> |
| 158 |
#include <nx-X11/Xw32defs.h> |
| 159 |
#undef close |
| 160 |
#define close closesocket |
| 161 |
#define ECONNREFUSED WSAECONNREFUSED |
| 162 |
#define EADDRINUSE WSAEADDRINUSE |
| 163 |
#define EPROTOTYPE WSAEPROTOTYPE |
| 164 |
#undef EWOULDBLOCK |
| 165 |
#define EWOULDBLOCK WSAEWOULDBLOCK |
| 166 |
#define EINPROGRESS WSAEINPROGRESS |
| 167 |
#undef EINTR |
| 168 |
#define EINTR WSAEINTR |
| 169 |
#define X_INCLUDE_NETDB_H |
| 170 |
#define XOS_USE_MTSAFE_NETDBAPI |
| 171 |
#include <nx-X11/Xos_r.h> |
| 172 |
#endif /* WIN32 */ |
| 173 |
|
| 174 |
#if defined(SO_DONTLINGER) && defined(SO_LINGER) |
| 175 |
#undef SO_DONTLINGER |
| 176 |
#endif |
| 177 |
|
| 178 |
/* others don't need this */ |
| 179 |
#define SocketInitOnce() /**/ |
| 180 |
|
| 181 |
#ifdef linux |
| 182 |
#define HAVE_ABSTRACT_SOCKETS |
| 183 |
#endif |
| 184 |
|
| 185 |
#define MIN_BACKLOG 128 |
| 186 |
#ifdef SOMAXCONN |
| 187 |
#if SOMAXCONN > MIN_BACKLOG |
| 188 |
#define BACKLOG SOMAXCONN |
| 189 |
#endif |
| 190 |
#endif |
| 191 |
#ifndef BACKLOG |
| 192 |
#define BACKLOG MIN_BACKLOG |
| 193 |
#endif |
| 194 |
|
| 195 |
/* |
| 196 |
* This is the Socket implementation of the X Transport service layer |
| 197 |
* |
| 198 |
* This file contains the implementation for both the UNIX and INET domains, |
| 199 |
* and can be built for either one, or both. |
| 200 |
* |
| 201 |
*/ |
| 202 |
|
| 203 |
typedef struct _Sockettrans2dev { |
| 204 |
const char *transname; |
| 205 |
int family; |
| 206 |
int devcotsname; |
| 207 |
int devcltsname; |
| 208 |
int protocol; |
| 209 |
} Sockettrans2dev; |
| 210 |
|
| 211 |
static Sockettrans2dev Sockettrans2devtab[] = { |
| 212 |
#ifdef TCPCONN |
| 213 |
{"inet",AF_INET,SOCK_STREAM,SOCK_DGRAM,0}, |
| 214 |
#if !defined(IPv6) || !defined(AF_INET6) |
| 215 |
{"tcp",AF_INET,SOCK_STREAM,SOCK_DGRAM,0}, |
| 216 |
#else /* IPv6 */ |
| 217 |
{"tcp",AF_INET6,SOCK_STREAM,SOCK_DGRAM,0}, |
| 218 |
{"tcp",AF_INET,SOCK_STREAM,SOCK_DGRAM,0}, /* fallback */ |
| 219 |
{"inet6",AF_INET6,SOCK_STREAM,SOCK_DGRAM,0}, |
| 220 |
#endif |
| 221 |
#endif /* TCPCONN */ |
| 222 |
#ifdef UNIXCONN |
| 223 |
{"unix",AF_UNIX,SOCK_STREAM,SOCK_DGRAM,0}, |
| 224 |
#if !defined(LOCALCONN) |
| 225 |
{"local",AF_UNIX,SOCK_STREAM,SOCK_DGRAM,0}, |
| 226 |
#endif /* !LOCALCONN */ |
| 227 |
#endif /* UNIXCONN */ |
| 228 |
}; |
| 229 |
|
| 230 |
#define NUMSOCKETFAMILIES (sizeof(Sockettrans2devtab)/sizeof(Sockettrans2dev)) |
| 231 |
|
| 232 |
#ifdef TCPCONN |
| 233 |
static int TRANS(SocketINETClose) (XtransConnInfo ciptr); |
| 234 |
#endif |
| 235 |
|
| 236 |
#ifdef UNIXCONN |
| 237 |
|
| 238 |
|
| 239 |
#if defined(X11_t) |
| 240 |
#define UNIX_PATH "/tmp/.X11-unix/X" |
| 241 |
#define UNIX_DIR "/tmp/.X11-unix" |
| 242 |
#endif /* X11_t */ |
| 243 |
#if defined(XIM_t) |
| 244 |
#define UNIX_PATH "/tmp/.XIM-unix/XIM" |
| 245 |
#define UNIX_DIR "/tmp/.XIM-unix" |
| 246 |
#endif /* XIM_t */ |
| 247 |
#if defined(FS_t) || defined(FONT_t) |
| 248 |
#define UNIX_PATH "/tmp/.font-unix/fs" |
| 249 |
#define UNIX_DIR "/tmp/.font-unix" |
| 250 |
#endif /* FS_t || FONT_t */ |
| 251 |
#if defined(ICE_t) |
| 252 |
#define UNIX_PATH "/tmp/.ICE-unix/" |
| 253 |
#define UNIX_DIR "/tmp/.ICE-unix" |
| 254 |
#endif /* ICE_t */ |
| 255 |
|
| 256 |
|
| 257 |
#endif /* UNIXCONN */ |
| 258 |
|
| 259 |
#define PORTBUFSIZE 32 |
| 260 |
|
| 261 |
#ifndef MAXHOSTNAMELEN |
| 262 |
#define MAXHOSTNAMELEN 255 |
| 263 |
#endif |
| 264 |
|
| 265 |
#if defined HAVE_SOCKLEN_T || (defined(IPv6) && defined(AF_INET6)) |
| 266 |
# define SOCKLEN_T socklen_t |
| 267 |
#elif defined(SVR4) || defined(__SVR4) || defined(__SCO__) |
| 268 |
# define SOCKLEN_T size_t |
| 269 |
#else |
| 270 |
# define SOCKLEN_T int |
| 271 |
#endif |
| 272 |
|
| 273 |
#ifndef X11_t |
| 274 |
|
| 275 |
/* |
| 276 |
* No NX changes if this is not |
| 277 |
* compiled as a X11 transport. |
| 278 |
*/ |
| 279 |
|
| 280 |
#undef NX_TRANS_SOCKET |
| 281 |
|
| 282 |
#endif |
| 283 |
|
| 284 |
#ifdef NX_TRANS_SOCKET |
| 285 |
|
| 286 |
#if XTRANS_SEND_FDS |
| 287 |
#error XTRANS_SEND_FDS is unsupported for NX! |
| 288 |
#endif |
| 289 |
|
| 290 |
#ifdef TRANS_CLIENT |
| 291 |
|
| 292 |
#include <nx/NX.h> |
| 293 |
|
| 294 |
typedef struct |
| 295 |
{ |
| 296 |
XtransConnInfo info; |
| 297 |
int local; |
| 298 |
int remote; |
| 299 |
int congestion; |
| 300 |
|
| 301 |
} _NXProxyConnInfo; |
| 302 |
|
| 303 |
#define NX_PROXY_CONN_LIMIT 256 |
| 304 |
|
| 305 |
static _NXProxyConnInfo *_NXProxyConnInfoTab[NX_PROXY_CONN_LIMIT]; |
| 306 |
|
| 307 |
#endif /* #ifdef TRANS_CLIENT */ |
| 308 |
|
| 309 |
/* |
| 310 |
* Forcibly close any connection attempt on the |
| 311 |
* listening socket. Need this to avoid loopback |
| 312 |
* connections to the agent server. |
| 313 |
*/ |
| 314 |
|
| 315 |
#ifdef TRANS_SERVER |
| 316 |
|
| 317 |
void TRANS(SocketRejectConnection) (XtransConnInfo ciptr) |
| 318 |
{ |
| 319 |
socklen_t sa_l = sizeof(struct sockaddr); |
| 320 |
struct sockaddr sa; |
| 321 |
fd_set fs; |
| 322 |
struct timeval t; |
| 323 |
int f; |
| 324 |
|
| 325 |
prmsg (3, "SocketRejectConnection(%p)\n", (void *) ciptr); |
| 326 |
|
| 327 |
FD_ZERO(&fs); |
| 328 |
FD_SET(ciptr -> fd, &fs); |
| 329 |
|
| 330 |
t.tv_sec = 0; |
| 331 |
t.tv_usec = 0; |
| 332 |
|
| 333 |
/* |
| 334 |
* Check if there is an awaiting connection. |
| 335 |
*/ |
| 336 |
|
| 337 |
if (select(ciptr -> fd + 1, &fs, NULL, NULL, &t) == 1) |
| 338 |
{ |
| 339 |
#ifdef NX_TRANS_TEST |
| 340 |
fprintf(stderr, "SocketRejectConnection: Accepting connection attempt on fd [%d].\n", |
| 341 |
ciptr -> fd); |
| 342 |
#endif |
| 343 |
/* |
| 344 |
* If there is one, close it. |
| 345 |
*/ |
| 346 |
|
| 347 |
if ((f = accept(ciptr -> fd, &sa, &sa_l)) >= 0) |
| 348 |
{ |
| 349 |
#ifdef NX_TRANS_TEST |
| 350 |
fprintf(stderr, "SocketRejectConnection: Closing connection attempt on fd [%d].\n", |
| 351 |
ciptr -> fd); |
| 352 |
#endif |
| 353 |
close(f); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
#endif /* #ifdef TRANS_SERVER */ |
| 359 |
|
| 360 |
#ifdef TRANS_CLIENT |
| 361 |
|
| 362 |
void *TRANS(SocketProxyConnInfo) (XtransConnInfo ciptr) |
| 363 |
{ |
| 364 |
if (_NXProxyConnInfoTab[ciptr->fd] != NULL) |
| 365 |
{ |
| 366 |
return ciptr->priv; |
| 367 |
} |
| 368 |
|
| 369 |
return NULL; |
| 370 |
} |
| 371 |
|
| 372 |
static XtransConnInfo TRANS(SocketCreateConnInfo) () |
| 373 |
{ |
| 374 |
XtransConnInfo ciptr; |
| 375 |
|
| 376 |
int fds[2]; |
| 377 |
|
| 378 |
#ifdef NX_TRANS_TEST |
| 379 |
fprintf(stderr, "SocketCreateConnInfo: Going to create the NX connection info.\n"); |
| 380 |
#endif |
| 381 |
|
| 382 |
if ((ciptr = (XtransConnInfo) calloc (1, sizeof(struct _XtransConnInfo))) == NULL) |
| 383 |
{ |
| 384 |
prmsg (1, "SocketCreateConnInfo: malloc failed\n"); |
| 385 |
return NULL; |
| 386 |
} |
| 387 |
|
| 388 |
/* |
| 389 |
* Create a pair of sockets. We'll communicate with |
| 390 |
* the NX proxy by reading and writing to our end. |
| 391 |
*/ |
| 392 |
|
| 393 |
#ifdef NX_TRANS_TEST |
| 394 |
fprintf(stderr, "SocketCreateConnInfo: Going to create the NX socketpair.\n"); |
| 395 |
#endif |
| 396 |
|
| 397 |
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) < 0) |
| 398 |
{ |
| 399 |
prmsg (1, "SocketCreateConnInfo: socketpair() failed.\n"); |
| 400 |
free ((char *) ciptr); |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
#ifdef NX_TRANS_TEST |
| 405 |
fprintf(stderr, "SocketCreateConnInfo: X socket end is [%d] NX proxy end is [%d].\n", |
| 406 |
fds[0], fds[1]); |
| 407 |
#endif |
| 408 |
|
| 409 |
/* |
| 410 |
* Save in _NXProxyConnInfoTab the local and remote end of |
| 411 |
* the socketpair. The remote end will be used by the proxy. |
| 412 |
* When the memory-to-memory transport is activated, the |
| 413 |
* agent and the proxy don't read or write to the real des- |
| 414 |
* criptors but the communication takes place by reading |
| 415 |
* and writing to the proxy's buffers. |
| 416 |
*/ |
| 417 |
|
| 418 |
ciptr->fd = fds[0]; |
| 419 |
|
| 420 |
if (ciptr->fd >= NX_PROXY_CONN_LIMIT) |
| 421 |
{ |
| 422 |
prmsg (1, "SocketCreateConnInfo: No space for a new _NXProxyConnInfo for [%d].\n", |
| 423 |
ciptr->fd); |
| 424 |
free ((char *) ciptr); |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
else if (_NXProxyConnInfoTab[ciptr->fd] != NULL) |
| 428 |
{ |
| 429 |
prmsg (1, "SocketCreateConnInfo: _NXProxyConnInfo for [%d] is not NULL. Exiting.\n", |
| 430 |
ciptr->fd); |
| 431 |
exit(1); |
| 432 |
} |
| 433 |
|
| 434 |
_NXProxyConnInfoTab[ciptr->fd] = (_NXProxyConnInfo *) calloc(1, sizeof(_NXProxyConnInfo)); |
| 435 |
|
| 436 |
if (_NXProxyConnInfoTab[ciptr->fd] == NULL) |
| 437 |
{ |
| 438 |
prmsg (1, "SocketCreateConnInfo: Alloc of _NXProxyConnInfo failed.\n"); |
| 439 |
free ((char *) ciptr); |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
|
| 443 |
#ifdef NX_TRANS_TEST |
| 444 |
fprintf(stderr, "SocketCreateConnInfo: Allocated new _NXProxyConnInfo for [%d].\n", |
| 445 |
ciptr->fd); |
| 446 |
#endif |
| 447 |
|
| 448 |
_NXProxyConnInfoTab[ciptr->fd]->info = ciptr; |
| 449 |
_NXProxyConnInfoTab[ciptr->fd]->local = fds[0]; |
| 450 |
_NXProxyConnInfoTab[ciptr->fd]->remote = fds[1]; |
| 451 |
_NXProxyConnInfoTab[ciptr->fd]->congestion = 0; |
| 452 |
|
| 453 |
ciptr->priv = (char *) _NXProxyConnInfoTab[ciptr->fd]; |
| 454 |
|
| 455 |
return ciptr; |
| 456 |
} |
| 457 |
|
| 458 |
static int TRANS(SocketConnectConnInfo) (XtransConnInfo ciptr, const char *host, const char *port) |
| 459 |
{ |
| 460 |
int fds[2]; |
| 461 |
char display[1024]; |
| 462 |
|
| 463 |
_NXProxyConnInfo *proxy_conn; |
| 464 |
|
| 465 |
#ifdef NX_TRANS_TEST |
| 466 |
fprintf(stderr, "SocketConnectConnInfo: Going to connect NX fd [%d] to host [%s] port [%s].\n", |
| 467 |
ciptr->fd, host, port); |
| 468 |
#endif |
| 469 |
|
| 470 |
/* |
| 471 |
* We should have already created the socket pair. |
| 472 |
*/ |
| 473 |
|
| 474 |
proxy_conn = (_NXProxyConnInfo *) ciptr->priv; |
| 475 |
|
| 476 |
if (proxy_conn == NULL) |
| 477 |
{ |
| 478 |
prmsg (1, "SocketConnectConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n"); |
| 479 |
|
| 480 |
exit(1); |
| 481 |
} |
| 482 |
else if (_NXProxyConnInfoTab[ciptr->fd] != (_NXProxyConnInfo *) ciptr->priv) |
| 483 |
{ |
| 484 |
prmsg (1, "SocketConnectConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n"); |
| 485 |
|
| 486 |
exit(1); |
| 487 |
} |
| 488 |
|
| 489 |
if (strlen(host) + strlen(port) + 1 >= 1023) |
| 490 |
{ |
| 491 |
prmsg (1, "SocketConnectConnInfo: Length of NX display string '%s:%s' would exceed %d characters.\n", |
| 492 |
host, port, 1023); |
| 493 |
|
| 494 |
return TRANS_CONNECT_FAILED; |
| 495 |
} |
| 496 |
|
| 497 |
sprintf(display, "%s:%s", host, port); |
| 498 |
|
| 499 |
#ifdef NX_TRANS_TEST |
| 500 |
fprintf(stderr, "SocketConnectConnInfo: Setting close-on-exec flag on local NX descriptor [%d].\n", |
| 501 |
proxy_conn -> local); |
| 502 |
#endif |
| 503 |
|
| 504 |
#ifdef F_SETFD |
| 505 |
#ifdef FD_CLOEXEC |
| 506 |
if (fcntl(proxy_conn -> local, F_SETFD, FD_CLOEXEC) != 0) |
| 507 |
#else |
| 508 |
if (fcntl(proxy_conn -> local, F_SETFD, 1) != 0) |
| 509 |
#endif |
| 510 |
#endif |
| 511 |
{ |
| 512 |
prmsg (1, "SocketConnectConnInfo: Cannot set close-on-exec on local NX descriptor [%d].\n", |
| 513 |
proxy_conn -> local); |
| 514 |
|
| 515 |
return TRANS_CONNECT_FAILED; |
| 516 |
} |
| 517 |
|
| 518 |
#ifdef NX_TRANS_TEST |
| 519 |
fprintf(stderr, "SocketConnectConnInfo: Creating the NX transport with display [%s].\n", |
| 520 |
display); |
| 521 |
#endif |
| 522 |
|
| 523 |
if (NXTransCreate(NX_FD_ANY, NX_MODE_CLIENT, display) < 0) |
| 524 |
{ |
| 525 |
prmsg (1, "SocketConnectConnInfo: Cannot create the NX transport.\n"); |
| 526 |
|
| 527 |
return TRANS_CONNECT_FAILED; |
| 528 |
} |
| 529 |
|
| 530 |
#ifdef NX_TRANS_TEST |
| 531 |
fprintf(stderr, "SocketConnectConnInfo: Starting the NX agent with descriptor [%d].\n", |
| 532 |
proxy_conn -> remote); |
| 533 |
#endif |
| 534 |
|
| 535 |
fds[0] = proxy_conn -> local; |
| 536 |
fds[1] = proxy_conn -> remote; |
| 537 |
|
| 538 |
NXTransAgent(fds); |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
|
| 543 |
static void TRANS(SocketCloseConnInfo) (XtransConnInfo ciptr) |
| 544 |
{ |
| 545 |
_NXProxyConnInfo *proxy_conn; |
| 546 |
|
| 547 |
#ifdef NX_TRANS_TEST |
| 548 |
fprintf(stderr, "SocketCloseConnInfo: Going to close the NX fd [%d].\n", ciptr->fd); |
| 549 |
#endif |
| 550 |
|
| 551 |
proxy_conn = (_NXProxyConnInfo *) ciptr->priv; |
| 552 |
|
| 553 |
if (proxy_conn == NULL) |
| 554 |
{ |
| 555 |
prmsg (1, "SocketCloseConnInfo: Pointer to _NXProxyConnInfo is NULL. Exiting.\n"); |
| 556 |
|
| 557 |
exit(1); |
| 558 |
} |
| 559 |
else if (ciptr->fd >= NX_PROXY_CONN_LIMIT || |
| 560 |
_NXProxyConnInfoTab[ciptr->fd] != (_NXProxyConnInfo *) ciptr->priv) |
| 561 |
{ |
| 562 |
prmsg (1, "SocketCloseConnInfo: Can't find _NXProxyConnInfo in table. Exiting.\n"); |
| 563 |
exit(1); |
| 564 |
} |
| 565 |
else if (_NXProxyConnInfoTab[ciptr->fd] -> info != ciptr || |
| 566 |
_NXProxyConnInfoTab[ciptr->fd] -> local != ciptr->fd) |
| 567 |
{ |
| 568 |
prmsg (1, "SocketCloseConnInfo: Invalid _NXProxyConnInfo structure for [%d]. Exiting.\n", |
| 569 |
ciptr->fd); |
| 570 |
exit(1); |
| 571 |
} |
| 572 |
else if (proxy_conn->local < 0 || proxy_conn->remote < 0) |
| 573 |
{ |
| 574 |
prmsg (1, "SocketCloseConnInfo: Invalid socket pair in NX connection for [%d]. Exiting.\n", |
| 575 |
ciptr->fd); |
| 576 |
exit(1); |
| 577 |
} |
| 578 |
|
| 579 |
NXTransClose(ciptr->fd); |
| 580 |
|
| 581 |
/* |
| 582 |
* Get rid of the _NXProxyConnInfo structure. |
| 583 |
*/ |
| 584 |
|
| 585 |
#ifdef NX_TRANS_TEST |
| 586 |
fprintf(stderr, "SocketCloseConnInfo: Freeing _NXProxyConnInfo structure for [%d].\n", |
| 587 |
ciptr->fd); |
| 588 |
#endif |
| 589 |
|
| 590 |
free((char *) _NXProxyConnInfoTab[ciptr->fd]); |
| 591 |
|
| 592 |
_NXProxyConnInfoTab[ciptr->fd] = NULL; |
| 593 |
|
| 594 |
ciptr->priv = NULL; |
| 595 |
|
| 596 |
#ifdef NX_TRANS_TEST |
| 597 |
fprintf(stderr, "SocketCloseConnInfo: Should now close the local descriptor [%d].\n", |
| 598 |
ciptr->fd); |
| 599 |
#endif |
| 600 |
} |
| 601 |
|
| 602 |
#endif /* #ifdef TRANS_CLIENT */ |
| 603 |
|
| 604 |
#if defined(TRANS_CLIENT) && defined(NX_TRANS_CHANGE) |
| 605 |
|
| 606 |
/* |
| 607 |
* Check the congestion state of the NX transport |
| 608 |
* and return 1 if there has been a change. This |
| 609 |
* can be extended by adding a few counters track- |
| 610 |
* ing the bandwidth usage of the X11 connection. |
| 611 |
*/ |
| 612 |
|
| 613 |
int TRANS(SocketCongestionChange) (XtransConnInfo ciptr, int *state) |
| 614 |
{ |
| 615 |
int congestion; |
| 616 |
|
| 617 |
_NXProxyConnInfo *proxy_conn; |
| 618 |
|
| 619 |
prmsg (3, "SocketCongestionChange(%p)\n", (void *) ciptr); |
| 620 |
|
| 621 |
proxy_conn = (_NXProxyConnInfo *) ciptr->priv; |
| 622 |
|
| 623 |
if (proxy_conn == NULL) |
| 624 |
{ |
| 625 |
#ifdef NX_TRANS_DEBUG |
| 626 |
fprintf(stderr, "SocketCongestionChange: Descriptor [%d] doesn't appear to be a NX connection.\n", |
| 627 |
ciptr->fd); |
| 628 |
#endif |
| 629 |
return 0; |
| 630 |
} |
| 631 |
|
| 632 |
#ifdef NX_TRANS_DEBUG |
| 633 |
fprintf(stderr, "SocketCongestionChange: Checking congestion on fd [%d] with old state [%d].\n", |
| 634 |
ciptr->fd, proxy_conn->congestion); |
| 635 |
#endif |
| 636 |
|
| 637 |
congestion = NXTransCongestion(ciptr->fd); |
| 638 |
|
| 639 |
if (congestion != proxy_conn->congestion) |
| 640 |
{ |
| 641 |
proxy_conn->congestion = congestion; |
| 642 |
|
| 643 |
#ifdef NX_TRANS_TEST |
| 644 |
fprintf(stderr, "SocketCongestionChange: Change detected on fd [%d] with new state [%d].\n", |
| 645 |
ciptr->fd, proxy_conn->congestion); |
| 646 |
#endif |
| 647 |
return 1; |
| 648 |
} |
| 649 |
|
| 650 |
#ifdef NX_TRANS_TEST |
| 651 |
fprintf(stderr, "SocketCongestionChange: No change on fd [%d] with current state [%d].\n", |
| 652 |
ciptr->fd, congestion); |
| 653 |
#endif |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
#endif /* #if defined(TRANS_CLIENT) && defined(NX_TRANS_CHANGE) */ |
| 658 |
|
| 659 |
#endif /* #ifdef NX_TRANS_SOCKET */ |
| 660 |
|
| 661 |
/* |
| 662 |
* These are some utility function used by the real interface function below. |
| 663 |
*/ |
| 664 |
|
| 665 |
static int |
| 666 |
TRANS(SocketSelectFamily) (int first, const char *family) |
| 667 |
|
| 668 |
{ |
| 669 |
int i; |
| 670 |
|
| 671 |
prmsg (3,"SocketSelectFamily(%s)\n", family); |
| 672 |
|
| 673 |
for (i = first + 1; i < NUMSOCKETFAMILIES;i++) |
| 674 |
{ |
| 675 |
if (!strcmp (family, Sockettrans2devtab[i].transname)) |
| 676 |
return i; |
| 677 |
} |
| 678 |
|
| 679 |
return (first == -1 ? -2 : -1); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
/* |
| 684 |
* This function gets the local address of the socket and stores it in the |
| 685 |
* XtransConnInfo structure for the connection. |
| 686 |
*/ |
| 687 |
|
| 688 |
static int |
| 689 |
TRANS(SocketINETGetAddr) (XtransConnInfo ciptr) |
| 690 |
|
| 691 |
{ |
| 692 |
#if defined(IPv6) && defined(AF_INET6) |
| 693 |
struct sockaddr_storage socknamev6; |
| 694 |
#else |
| 695 |
struct sockaddr_in socknamev4; |
| 696 |
#endif |
| 697 |
void *socknamePtr; |
| 698 |
SOCKLEN_T namelen; |
| 699 |
|
| 700 |
prmsg (3,"SocketINETGetAddr(%p)\n", (void *) ciptr); |
| 701 |
|
| 702 |
#if defined(IPv6) && defined(AF_INET6) |
| 703 |
namelen = sizeof(socknamev6); |
| 704 |
socknamePtr = &socknamev6; |
| 705 |
#else |
| 706 |
namelen = sizeof(socknamev4); |
| 707 |
socknamePtr = &socknamev4; |
| 708 |
#endif |
| 709 |
|
| 710 |
bzero(socknamePtr, namelen); |
| 711 |
|
| 712 |
if (getsockname (ciptr->fd,(struct sockaddr *) socknamePtr, |
| 713 |
(void *)&namelen) < 0) |
| 714 |
{ |
| 715 |
#ifdef WIN32 |
| 716 |
errno = WSAGetLastError(); |
| 717 |
#endif |
| 718 |
prmsg (1,"SocketINETGetAddr: getsockname() failed: %d\n", |
| 719 |
EGET()); |
| 720 |
return -1; |
| 721 |
} |
| 722 |
|
| 723 |
/* |
| 724 |
* Everything looks good: fill in the XtransConnInfo structure. |
| 725 |
*/ |
| 726 |
|
| 727 |
if ((ciptr->addr = malloc (namelen)) == NULL) |
| 728 |
{ |
| 729 |
prmsg (1, |
| 730 |
"SocketINETGetAddr: Can't allocate space for the addr\n"); |
| 731 |
return -1; |
| 732 |
} |
| 733 |
|
| 734 |
#if defined(IPv6) && defined(AF_INET6) |
| 735 |
ciptr->family = ((struct sockaddr *)socknamePtr)->sa_family; |
| 736 |
#else |
| 737 |
ciptr->family = socknamev4.sin_family; |
| 738 |
#endif |
| 739 |
ciptr->addrlen = namelen; |
| 740 |
memcpy (ciptr->addr, socknamePtr, ciptr->addrlen); |
| 741 |
|
| 742 |
return 0; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
/* |
| 747 |
* This function gets the remote address of the socket and stores it in the |
| 748 |
* XtransConnInfo structure for the connection. |
| 749 |
*/ |
| 750 |
|
| 751 |
static int |
| 752 |
TRANS(SocketINETGetPeerAddr) (XtransConnInfo ciptr) |
| 753 |
|
| 754 |
{ |
| 755 |
#if defined(IPv6) && defined(AF_INET6) |
| 756 |
struct sockaddr_storage socknamev6; |
| 757 |
#endif |
| 758 |
struct sockaddr_in socknamev4; |
| 759 |
void *socknamePtr; |
| 760 |
SOCKLEN_T namelen; |
| 761 |
|
| 762 |
#if defined(IPv6) && defined(AF_INET6) |
| 763 |
if (ciptr->family == AF_INET6) |
| 764 |
{ |
| 765 |
namelen = sizeof(socknamev6); |
| 766 |
socknamePtr = &socknamev6; |
| 767 |
} |
| 768 |
else |
| 769 |
#endif |
| 770 |
{ |
| 771 |
namelen = sizeof(socknamev4); |
| 772 |
socknamePtr = &socknamev4; |
| 773 |
} |
| 774 |
|
| 775 |
bzero(socknamePtr, namelen); |
| 776 |
|
| 777 |
prmsg (3,"SocketINETGetPeerAddr(%p)\n", (void *) ciptr); |
| 778 |
|
| 779 |
if (getpeername (ciptr->fd, (struct sockaddr *) socknamePtr, |
| 780 |
(void *)&namelen) < 0) |
| 781 |
{ |
| 782 |
#ifdef WIN32 |
| 783 |
errno = WSAGetLastError(); |
| 784 |
#endif |
| 785 |
prmsg (1,"SocketINETGetPeerAddr: getpeername() failed: %d\n", |
| 786 |
EGET()); |
| 787 |
return -1; |
| 788 |
} |
| 789 |
|
| 790 |
/* |
| 791 |
* Everything looks good: fill in the XtransConnInfo structure. |
| 792 |
*/ |
| 793 |
|
| 794 |
if ((ciptr->peeraddr = malloc (namelen)) == NULL) |
| 795 |
{ |
| 796 |
prmsg (1, |
| 797 |
"SocketINETGetPeerAddr: Can't allocate space for the addr\n"); |
| 798 |
return -1; |
| 799 |
} |
| 800 |
|
| 801 |
ciptr->peeraddrlen = namelen; |
| 802 |
memcpy (ciptr->peeraddr, socknamePtr, ciptr->peeraddrlen); |
| 803 |
|
| 804 |
return 0; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
static XtransConnInfo |
| 809 |
TRANS(SocketOpen) (int i, int type) |
| 810 |
|
| 811 |
{ |
| 812 |
XtransConnInfo ciptr; |
| 813 |
|
| 814 |
prmsg (3,"SocketOpen(%d,%d)\n", i, type); |
| 815 |
|
| 816 |
if ((ciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) |
| 817 |
{ |
| 818 |
prmsg (1, "SocketOpen: malloc failed\n"); |
| 819 |
return NULL; |
| 820 |
} |
| 821 |
|
| 822 |
if ((ciptr->fd = socket(Sockettrans2devtab[i].family, type, |
| 823 |
Sockettrans2devtab[i].protocol)) < 0 |
| 824 |
#ifndef WIN32 |
| 825 |
#if (defined(X11_t) && !defined(USE_POLL)) || defined(FS_t) || defined(FONT_t) |
| 826 |
|| ciptr->fd >= sysconf(_SC_OPEN_MAX) |
| 827 |
#endif |
| 828 |
#endif |
| 829 |
) { |
| 830 |
#ifdef WIN32 |
| 831 |
errno = WSAGetLastError(); |
| 832 |
#endif |
| 833 |
prmsg (2, "SocketOpen: socket() failed for %s\n", |
| 834 |
Sockettrans2devtab[i].transname); |
| 835 |
|
| 836 |
free (ciptr); |
| 837 |
return NULL; |
| 838 |
} |
| 839 |
|
| 840 |
#ifdef TCP_NODELAY |
| 841 |
if (Sockettrans2devtab[i].family == AF_INET |
| 842 |
#if defined(IPv6) && defined(AF_INET6) |
| 843 |
|| Sockettrans2devtab[i].family == AF_INET6 |
| 844 |
#endif |
| 845 |
) |
| 846 |
{ |
| 847 |
/* |
| 848 |
* turn off TCP coalescence for INET sockets |
| 849 |
*/ |
| 850 |
|
| 851 |
int tmp = 1; |
| 852 |
setsockopt (ciptr->fd, IPPROTO_TCP, TCP_NODELAY, |
| 853 |
(char *) &tmp, sizeof (int)); |
| 854 |
} |
| 855 |
#endif |
| 856 |
|
| 857 |
/* |
| 858 |
* Some systems provide a really small default buffer size for |
| 859 |
* UNIX sockets. Bump it up a bit such that large transfers don't |
| 860 |
* proceed at glacial speed. |
| 861 |
*/ |
| 862 |
#ifdef SO_SNDBUF |
| 863 |
if (Sockettrans2devtab[i].family == AF_UNIX) |
| 864 |
{ |
| 865 |
SOCKLEN_T len = sizeof (int); |
| 866 |
int val; |
| 867 |
|
| 868 |
if (getsockopt (ciptr->fd, SOL_SOCKET, SO_SNDBUF, |
| 869 |
(char *) &val, &len) == 0 && val < 64 * 1024) |
| 870 |
{ |
| 871 |
val = 64 * 1024; |
| 872 |
setsockopt (ciptr->fd, SOL_SOCKET, SO_SNDBUF, |
| 873 |
(char *) &val, sizeof (int)); |
| 874 |
} |
| 875 |
} |
| 876 |
#endif |
| 877 |
|
| 878 |
return ciptr; |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
#ifdef TRANS_REOPEN |
| 883 |
|
| 884 |
static XtransConnInfo |
| 885 |
TRANS(SocketReopen) (int i _X_UNUSED, int type, int fd, const char *port) |
| 886 |
|
| 887 |
{ |
| 888 |
XtransConnInfo ciptr; |
| 889 |
int portlen; |
| 890 |
struct sockaddr *addr; |
| 891 |
size_t addrlen; |
| 892 |
|
| 893 |
prmsg (3,"SocketReopen(%d,%d,%s)\n", type, fd, port); |
| 894 |
|
| 895 |
if (port == NULL) { |
| 896 |
prmsg (1, "SocketReopen: port was null!\n"); |
| 897 |
return NULL; |
| 898 |
} |
| 899 |
|
| 900 |
portlen = strlen(port) + 1; // include space for trailing null |
| 901 |
#ifdef SOCK_MAXADDRLEN |
| 902 |
if (portlen < 0 || portlen > (SOCK_MAXADDRLEN + 2)) { |
| 903 |
prmsg (1, "SocketReopen: invalid portlen %d\n", portlen); |
| 904 |
return NULL; |
| 905 |
} |
| 906 |
if (portlen < 14) portlen = 14; |
| 907 |
#else |
| 908 |
if (portlen < 0 || portlen > 14) { |
| 909 |
prmsg (1, "SocketReopen: invalid portlen %d\n", portlen); |
| 910 |
return NULL; |
| 911 |
} |
| 912 |
#endif /*SOCK_MAXADDRLEN*/ |
| 913 |
|
| 914 |
if ((ciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) |
| 915 |
{ |
| 916 |
prmsg (1, "SocketReopen: malloc(ciptr) failed\n"); |
| 917 |
return NULL; |
| 918 |
} |
| 919 |
|
| 920 |
ciptr->fd = fd; |
| 921 |
|
| 922 |
addrlen = portlen + offsetof(struct sockaddr, sa_data); |
| 923 |
if ((addr = calloc (1, addrlen)) == NULL) { |
| 924 |
prmsg (1, "SocketReopen: malloc(addr) failed\n"); |
| 925 |
free (ciptr); |
| 926 |
return NULL; |
| 927 |
} |
| 928 |
ciptr->addr = (char *) addr; |
| 929 |
ciptr->addrlen = addrlen; |
| 930 |
|
| 931 |
if ((ciptr->peeraddr = calloc (1, addrlen)) == NULL) { |
| 932 |
prmsg (1, "SocketReopen: malloc(portaddr) failed\n"); |
| 933 |
free (addr); |
| 934 |
free (ciptr); |
| 935 |
return NULL; |
| 936 |
} |
| 937 |
ciptr->peeraddrlen = addrlen; |
| 938 |
|
| 939 |
/* Initialize ciptr structure as if it were a normally-opened unix socket */ |
| 940 |
ciptr->flags = TRANS_LOCAL | TRANS_NOUNLINK; |
| 941 |
#ifdef BSD44SOCKETS |
| 942 |
addr->sa_len = addrlen; |
| 943 |
#endif |
| 944 |
addr->sa_family = AF_UNIX; |
| 945 |
#if defined(HAVE_STRLCPY) || defined(HAS_STRLCPY) |
| 946 |
strlcpy(addr->sa_data, port, portlen); |
| 947 |
#else |
| 948 |
strncpy(addr->sa_data, port, portlen); |
| 949 |
#endif |
| 950 |
ciptr->family = AF_UNIX; |
| 951 |
memcpy(ciptr->peeraddr, ciptr->addr, addrlen); |
| 952 |
ciptr->port = rindex(addr->sa_data, ':'); |
| 953 |
if (ciptr->port == NULL) { |
| 954 |
if (is_numeric(addr->sa_data)) { |
| 955 |
ciptr->port = addr->sa_data; |
| 956 |
} |
| 957 |
} else if (ciptr->port[0] == ':') { |
| 958 |
ciptr->port++; |
| 959 |
} |
| 960 |
/* port should now point to portnum or NULL */ |
| 961 |
return ciptr; |
| 962 |
} |
| 963 |
|
| 964 |
#endif /* TRANS_REOPEN */ |
| 965 |
|
| 966 |
|
| 967 |
/* |
| 968 |
* These functions are the interface supplied in the Xtransport structure |
| 969 |
*/ |
| 970 |
|
| 971 |
#ifdef TRANS_CLIENT |
| 972 |
|
| 973 |
static XtransConnInfo |
| 974 |
TRANS(SocketOpenCOTSClientBase) (const char *transname, const char *protocol, |
| 975 |
const char *host, const char *port, int previndex) |
| 976 |
{ |
| 977 |
XtransConnInfo ciptr; |
| 978 |
int i = previndex; |
| 979 |
|
| 980 |
prmsg (2, "SocketOpenCOTSClient(%s,%s,%s)\n", |
| 981 |
protocol, host, port); |
| 982 |
|
| 983 |
SocketInitOnce(); |
| 984 |
|
| 985 |
while ((i = TRANS(SocketSelectFamily) (i, transname)) >= 0) { |
| 986 |
|
| 987 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 988 |
|
| 989 |
if ((!strcmp(protocol, "local") || !strcmp(protocol, "nx")) && |
| 990 |
(!strcasecmp(host, "nx") || !strncasecmp(host, "nx,", 3))) |
| 991 |
{ |
| 992 |
ciptr = TRANS(SocketCreateConnInfo) (); |
| 993 |
|
| 994 |
if (ciptr == NULL) |
| 995 |
{ |
| 996 |
prmsg (1, "SocketOpenCOTSClient: Unable to create the NX connection info for %s.\n", |
| 997 |
transname); |
| 998 |
|
| 999 |
return NULL; |
| 1000 |
} |
| 1001 |
|
| 1002 |
ciptr->index = i; |
| 1003 |
|
| 1004 |
return ciptr; |
| 1005 |
} |
| 1006 |
|
| 1007 |
#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 1008 |
|
| 1009 |
if ((ciptr = TRANS(SocketOpen) ( |
| 1010 |
i, Sockettrans2devtab[i].devcotsname)) != NULL) { |
| 1011 |
/* Save the index for later use */ |
| 1012 |
|
| 1013 |
ciptr->index = i; |
| 1014 |
break; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
if (i < 0) { |
| 1018 |
if (i == -1) |
| 1019 |
prmsg (1,"SocketOpenCOTSClient: Unable to open socket for %s\n", |
| 1020 |
transname); |
| 1021 |
else |
| 1022 |
prmsg (1,"SocketOpenCOTSClient: Unable to determine socket type for %s\n", |
| 1023 |
transname); |
| 1024 |
return NULL; |
| 1025 |
} |
| 1026 |
|
| 1027 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 1028 |
|
| 1029 |
ciptr->priv = NULL; |
| 1030 |
|
| 1031 |
#endif |
| 1032 |
|
| 1033 |
return ciptr; |
| 1034 |
} |
| 1035 |
|
| 1036 |
static XtransConnInfo |
| 1037 |
TRANS(SocketOpenCOTSClient) (Xtransport *thistrans, const char *protocol, |
| 1038 |
const char *host, const char *port) |
| 1039 |
{ |
| 1040 |
return TRANS(SocketOpenCOTSClientBase)( |
| 1041 |
thistrans->TransName, protocol, host, port, -1); |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
#endif /* TRANS_CLIENT */ |
| 1046 |
|
| 1047 |
|
| 1048 |
#ifdef TRANS_SERVER |
| 1049 |
|
| 1050 |
static XtransConnInfo |
| 1051 |
TRANS(SocketOpenCOTSServer) (Xtransport *thistrans, const char *protocol, |
| 1052 |
const char *host, const char *port) |
| 1053 |
|
| 1054 |
{ |
| 1055 |
XtransConnInfo ciptr; |
| 1056 |
int i = -1; |
| 1057 |
|
| 1058 |
prmsg (2,"SocketOpenCOTSServer(%s,%s,%s)\n", protocol, host, port); |
| 1059 |
|
| 1060 |
SocketInitOnce(); |
| 1061 |
|
| 1062 |
while ((i = TRANS(SocketSelectFamily) (i, thistrans->TransName)) >= 0) { |
| 1063 |
if ((ciptr = TRANS(SocketOpen) ( |
| 1064 |
i, Sockettrans2devtab[i].devcotsname)) != NULL) |
| 1065 |
break; |
| 1066 |
} |
| 1067 |
if (i < 0) { |
| 1068 |
if (i == -1) |
| 1069 |
prmsg (1,"SocketOpenCOTSServer: Unable to open socket for %s\n", |
| 1070 |
thistrans->TransName); |
| 1071 |
else |
| 1072 |
prmsg (1,"SocketOpenCOTSServer: Unable to determine socket type for %s\n", |
| 1073 |
thistrans->TransName); |
| 1074 |
return NULL; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* |
| 1078 |
* Using this prevents the bind() check for an existing server listening |
| 1079 |
* on the same port, but it is required for other reasons. |
| 1080 |
*/ |
| 1081 |
#ifdef SO_REUSEADDR |
| 1082 |
|
| 1083 |
/* |
| 1084 |
* SO_REUSEADDR only applied to AF_INET && AF_INET6 |
| 1085 |
*/ |
| 1086 |
|
| 1087 |
if (Sockettrans2devtab[i].family == AF_INET |
| 1088 |
#if defined(IPv6) && defined(AF_INET6) |
| 1089 |
|| Sockettrans2devtab[i].family == AF_INET6 |
| 1090 |
#endif |
| 1091 |
) |
| 1092 |
{ |
| 1093 |
int one = 1; |
| 1094 |
setsockopt (ciptr->fd, SOL_SOCKET, SO_REUSEADDR, |
| 1095 |
(char *) &one, sizeof (int)); |
| 1096 |
} |
| 1097 |
#endif |
| 1098 |
#ifdef IPV6_V6ONLY |
| 1099 |
if (Sockettrans2devtab[i].family == AF_INET6) |
| 1100 |
{ |
| 1101 |
int one = 1; |
| 1102 |
setsockopt(ciptr->fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(int)); |
| 1103 |
} |
| 1104 |
#endif |
| 1105 |
/* Save the index for later use */ |
| 1106 |
|
| 1107 |
ciptr->index = i; |
| 1108 |
|
| 1109 |
return ciptr; |
| 1110 |
} |
| 1111 |
|
| 1112 |
#endif /* TRANS_SERVER */ |
| 1113 |
|
| 1114 |
|
| 1115 |
#ifdef TRANS_REOPEN |
| 1116 |
|
| 1117 |
static XtransConnInfo |
| 1118 |
TRANS(SocketReopenCOTSServer) (Xtransport *thistrans, int fd, const char *port) |
| 1119 |
|
| 1120 |
{ |
| 1121 |
XtransConnInfo ciptr; |
| 1122 |
int i = -1; |
| 1123 |
|
| 1124 |
prmsg (2, |
| 1125 |
"SocketReopenCOTSServer(%d, %s)\n", fd, port); |
| 1126 |
|
| 1127 |
SocketInitOnce(); |
| 1128 |
|
| 1129 |
while ((i = TRANS(SocketSelectFamily) (i, thistrans->TransName)) >= 0) { |
| 1130 |
if ((ciptr = TRANS(SocketReopen) ( |
| 1131 |
i, Sockettrans2devtab[i].devcotsname, fd, port)) != NULL) |
| 1132 |
break; |
| 1133 |
} |
| 1134 |
if (i < 0) { |
| 1135 |
if (i == -1) |
| 1136 |
prmsg (1,"SocketReopenCOTSServer: Unable to open socket for %s\n", |
| 1137 |
thistrans->TransName); |
| 1138 |
else |
| 1139 |
prmsg (1,"SocketReopenCOTSServer: Unable to determine socket type for %s\n", |
| 1140 |
thistrans->TransName); |
| 1141 |
return NULL; |
| 1142 |
} |
| 1143 |
|
| 1144 |
/* Save the index for later use */ |
| 1145 |
|
| 1146 |
ciptr->index = i; |
| 1147 |
|
| 1148 |
return ciptr; |
| 1149 |
} |
| 1150 |
|
| 1151 |
#endif /* TRANS_REOPEN */ |
| 1152 |
|
| 1153 |
|
| 1154 |
static int |
| 1155 |
TRANS(SocketSetOption) (XtransConnInfo ciptr, int option, int arg) |
| 1156 |
|
| 1157 |
{ |
| 1158 |
prmsg (2,"SocketSetOption(%d,%d,%d)\n", ciptr->fd, option, arg); |
| 1159 |
|
| 1160 |
#ifdef NX_TRANS_TEST |
| 1161 |
fprintf(stderr, "SocketSetOption: WARNING! Not setting option [%d] with value [%d] on descriptor [%d].\n", |
| 1162 |
option, arg, ciptr -> fd); |
| 1163 |
#endif |
| 1164 |
|
| 1165 |
return -1; |
| 1166 |
} |
| 1167 |
|
| 1168 |
#ifdef UNIXCONN |
| 1169 |
static int |
| 1170 |
set_sun_path(const char *port, const char *upath, char *path, int abstract) |
| 1171 |
{ |
| 1172 |
struct sockaddr_un s; |
| 1173 |
int maxlen = sizeof(s.sun_path) - 1; |
| 1174 |
const char *at = ""; |
| 1175 |
|
| 1176 |
if (!port || !*port || !path) |
| 1177 |
return -1; |
| 1178 |
|
| 1179 |
#ifdef HAVE_ABSTRACT_SOCKETS |
| 1180 |
if (port[0] == '@') |
| 1181 |
upath = ""; |
| 1182 |
else if (abstract) |
| 1183 |
at = "@"; |
| 1184 |
#endif |
| 1185 |
|
| 1186 |
if (*port == '/') /* a full pathname */ |
| 1187 |
upath = ""; |
| 1188 |
|
| 1189 |
if (strlen(port) + strlen(upath) > maxlen) |
| 1190 |
return -1; |
| 1191 |
snprintf(path, sizeof(s.sun_path), "%s%s%s", at, upath, port); |
| 1192 |
return 0; |
| 1193 |
} |
| 1194 |
#endif |
| 1195 |
|
| 1196 |
#ifdef TRANS_SERVER |
| 1197 |
|
| 1198 |
static int |
| 1199 |
TRANS(SocketCreateListener) (XtransConnInfo ciptr, |
| 1200 |
struct sockaddr *sockname, |
| 1201 |
int socknamelen, unsigned int flags) |
| 1202 |
|
| 1203 |
{ |
| 1204 |
SOCKLEN_T namelen = socknamelen; |
| 1205 |
int fd = ciptr->fd; |
| 1206 |
int retry; |
| 1207 |
|
| 1208 |
prmsg (3, "SocketCreateListener(%p,%d)\n", (void *) ciptr, fd); |
| 1209 |
|
| 1210 |
if (Sockettrans2devtab[ciptr->index].family == AF_INET |
| 1211 |
#if defined(IPv6) && defined(AF_INET6) |
| 1212 |
|| Sockettrans2devtab[ciptr->index].family == AF_INET6 |
| 1213 |
#endif |
| 1214 |
) |
| 1215 |
retry = 20; |
| 1216 |
else |
| 1217 |
retry = 0; |
| 1218 |
|
| 1219 |
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) |
| 1220 |
fprintf(stderr, "SocketCreateListener: Creating listener for ciptr at [%p] on path [%s].\n", |
| 1221 |
(void *) ciptr, ((struct sockaddr_un *) sockname)->sun_family == AF_UNIX ? |
| 1222 |
((struct sockaddr_un *) sockname)->sun_path : "TCP"); |
| 1223 |
#endif |
| 1224 |
while (bind (fd, (struct sockaddr *) sockname, namelen) < 0) |
| 1225 |
{ |
| 1226 |
if (errno == EADDRINUSE) { |
| 1227 |
if (flags & ADDR_IN_USE_ALLOWED) |
| 1228 |
break; |
| 1229 |
else |
| 1230 |
return TRANS_ADDR_IN_USE; |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (retry-- == 0) { |
| 1234 |
prmsg (1, "SocketCreateListener: failed to bind listener\n"); |
| 1235 |
close (fd); |
| 1236 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1237 |
} |
| 1238 |
#ifdef SO_REUSEADDR |
| 1239 |
sleep (1); |
| 1240 |
#else |
| 1241 |
sleep (10); |
| 1242 |
#endif /* SO_REUSEDADDR */ |
| 1243 |
} |
| 1244 |
|
| 1245 |
if (Sockettrans2devtab[ciptr->index].family == AF_INET |
| 1246 |
#if defined(IPv6) && defined(AF_INET6) |
| 1247 |
|| Sockettrans2devtab[ciptr->index].family == AF_INET6 |
| 1248 |
#endif |
| 1249 |
) { |
| 1250 |
#ifdef SO_DONTLINGER |
| 1251 |
setsockopt (fd, SOL_SOCKET, SO_DONTLINGER, (char *) NULL, 0); |
| 1252 |
#else |
| 1253 |
#ifdef SO_LINGER |
| 1254 |
{ |
| 1255 |
static int linger[2] = { 0, 0 }; |
| 1256 |
setsockopt (fd, SOL_SOCKET, SO_LINGER, |
| 1257 |
(char *) linger, sizeof (linger)); |
| 1258 |
} |
| 1259 |
#endif |
| 1260 |
#endif |
| 1261 |
} |
| 1262 |
|
| 1263 |
if (listen (fd, BACKLOG) < 0) |
| 1264 |
{ |
| 1265 |
prmsg (1, "SocketCreateListener: listen() failed\n"); |
| 1266 |
close (fd); |
| 1267 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1268 |
} |
| 1269 |
|
| 1270 |
/* Set a flag to indicate that this connection is a listener */ |
| 1271 |
|
| 1272 |
ciptr->flags = 1 | (ciptr->flags & TRANS_KEEPFLAGS); |
| 1273 |
|
| 1274 |
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) |
| 1275 |
fprintf(stderr, "SocketCreateListener: Set flags to [%d] for ciptr [%p].\n", |
| 1276 |
ciptr->flags, (void *) ciptr); |
| 1277 |
#endif |
| 1278 |
|
| 1279 |
return 0; |
| 1280 |
} |
| 1281 |
|
| 1282 |
#ifdef TCPCONN |
| 1283 |
static int |
| 1284 |
TRANS(SocketINETCreateListener) (XtransConnInfo ciptr, const char *port, |
| 1285 |
unsigned int flags) |
| 1286 |
|
| 1287 |
{ |
| 1288 |
#if defined(IPv6) && defined(AF_INET6) |
| 1289 |
struct sockaddr_storage sockname; |
| 1290 |
#else |
| 1291 |
struct sockaddr_in sockname; |
| 1292 |
#endif |
| 1293 |
unsigned short sport; |
| 1294 |
SOCKLEN_T namelen = sizeof(sockname); |
| 1295 |
int status; |
| 1296 |
long tmpport; |
| 1297 |
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS |
| 1298 |
_Xgetservbynameparams sparams; |
| 1299 |
#endif |
| 1300 |
struct servent *servp; |
| 1301 |
|
| 1302 |
#ifdef X11_t |
| 1303 |
char portbuf[PORTBUFSIZE]; |
| 1304 |
#endif |
| 1305 |
|
| 1306 |
prmsg (2, "SocketINETCreateListener(%s)\n", port); |
| 1307 |
|
| 1308 |
#ifdef X11_t |
| 1309 |
/* |
| 1310 |
* X has a well known port, that is transport dependent. It is easier |
| 1311 |
* to handle it here, than try and come up with a transport independent |
| 1312 |
* representation that can be passed in and resolved the usual way. |
| 1313 |
* |
| 1314 |
* The port that is passed here is really a string containing the idisplay |
| 1315 |
* from ConnectDisplay(). |
| 1316 |
*/ |
| 1317 |
|
| 1318 |
if (is_numeric (port)) |
| 1319 |
{ |
| 1320 |
/* fixup the server port address */ |
| 1321 |
tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); |
| 1322 |
snprintf (portbuf, sizeof(portbuf), "%lu", tmpport); |
| 1323 |
port = portbuf; |
| 1324 |
} |
| 1325 |
#endif |
| 1326 |
|
| 1327 |
if (port && *port) |
| 1328 |
{ |
| 1329 |
/* Check to see if the port string is just a number (handles X11) */ |
| 1330 |
|
| 1331 |
if (!is_numeric (port)) |
| 1332 |
{ |
| 1333 |
if ((servp = _XGetservbyname (port,"tcp",sparams)) == NULL) |
| 1334 |
{ |
| 1335 |
prmsg (1, |
| 1336 |
"SocketINETCreateListener: Unable to get service for %s\n", |
| 1337 |
port); |
| 1338 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1339 |
} |
| 1340 |
/* we trust getservbyname to return a valid number */ |
| 1341 |
sport = servp->s_port; |
| 1342 |
} |
| 1343 |
else |
| 1344 |
{ |
| 1345 |
tmpport = strtol (port, (char**)NULL, 10); |
| 1346 |
/* |
| 1347 |
* check that somehow the port address isn't negative or in |
| 1348 |
* the range of reserved port addresses. This can happen and |
| 1349 |
* be very bad if the server is suid-root and the user does |
| 1350 |
* something (dumb) like `X :60049`. |
| 1351 |
*/ |
| 1352 |
if (tmpport < 1024 || tmpport > USHRT_MAX) |
| 1353 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1354 |
|
| 1355 |
sport = (unsigned short) tmpport; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
else |
| 1359 |
sport = 0; |
| 1360 |
|
| 1361 |
bzero(&sockname, sizeof(sockname)); |
| 1362 |
#if defined(IPv6) && defined(AF_INET6) |
| 1363 |
if (Sockettrans2devtab[ciptr->index].family == AF_INET) { |
| 1364 |
namelen = sizeof (struct sockaddr_in); |
| 1365 |
#ifdef BSD44SOCKETS |
| 1366 |
((struct sockaddr_in *)&sockname)->sin_len = namelen; |
| 1367 |
#endif |
| 1368 |
((struct sockaddr_in *)&sockname)->sin_family = AF_INET; |
| 1369 |
((struct sockaddr_in *)&sockname)->sin_port = htons(sport); |
| 1370 |
((struct sockaddr_in *)&sockname)->sin_addr.s_addr = htonl(INADDR_ANY); |
| 1371 |
} else { |
| 1372 |
namelen = sizeof (struct sockaddr_in6); |
| 1373 |
#ifdef SIN6_LEN |
| 1374 |
((struct sockaddr_in6 *)&sockname)->sin6_len = sizeof(sockname); |
| 1375 |
#endif |
| 1376 |
((struct sockaddr_in6 *)&sockname)->sin6_family = AF_INET6; |
| 1377 |
((struct sockaddr_in6 *)&sockname)->sin6_port = htons(sport); |
| 1378 |
((struct sockaddr_in6 *)&sockname)->sin6_addr = in6addr_any; |
| 1379 |
} |
| 1380 |
#else |
| 1381 |
#ifdef BSD44SOCKETS |
| 1382 |
sockname.sin_len = sizeof (sockname); |
| 1383 |
#endif |
| 1384 |
sockname.sin_family = AF_INET; |
| 1385 |
sockname.sin_port = htons (sport); |
| 1386 |
sockname.sin_addr.s_addr = htonl (INADDR_ANY); |
| 1387 |
#endif |
| 1388 |
|
| 1389 |
if ((status = TRANS(SocketCreateListener) (ciptr, |
| 1390 |
(struct sockaddr *) &sockname, namelen, flags)) < 0) |
| 1391 |
{ |
| 1392 |
prmsg (1, |
| 1393 |
"SocketINETCreateListener: ...SocketCreateListener() failed\n"); |
| 1394 |
return status; |
| 1395 |
} |
| 1396 |
|
| 1397 |
if (TRANS(SocketINETGetAddr) (ciptr) < 0) |
| 1398 |
{ |
| 1399 |
prmsg (1, |
| 1400 |
"SocketINETCreateListener: ...SocketINETGetAddr() failed\n"); |
| 1401 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1402 |
} |
| 1403 |
|
| 1404 |
return 0; |
| 1405 |
} |
| 1406 |
|
| 1407 |
#endif /* TCPCONN */ |
| 1408 |
|
| 1409 |
|
| 1410 |
#ifdef UNIXCONN |
| 1411 |
|
| 1412 |
static int |
| 1413 |
TRANS(SocketUNIXCreateListener) (XtransConnInfo ciptr, const char *port, |
| 1414 |
unsigned int flags) |
| 1415 |
|
| 1416 |
{ |
| 1417 |
struct sockaddr_un sockname; |
| 1418 |
int namelen; |
| 1419 |
int oldUmask; |
| 1420 |
int status; |
| 1421 |
unsigned int mode; |
| 1422 |
char tmpport[108]; |
| 1423 |
|
| 1424 |
int abstract = 0; |
| 1425 |
#ifdef HAVE_ABSTRACT_SOCKETS |
| 1426 |
abstract = ciptr->transptr->flags & TRANS_ABSTRACT; |
| 1427 |
#endif |
| 1428 |
|
| 1429 |
prmsg (2, "SocketUNIXCreateListener(%s)\n", |
| 1430 |
port ? port : "NULL"); |
| 1431 |
|
| 1432 |
/* Make sure the directory is created */ |
| 1433 |
|
| 1434 |
oldUmask = umask (0); |
| 1435 |
|
| 1436 |
#ifdef UNIX_DIR |
| 1437 |
#ifdef HAS_STICKY_DIR_BIT |
| 1438 |
mode = 01777; |
| 1439 |
#else |
| 1440 |
mode = 0777; |
| 1441 |
#endif |
| 1442 |
if (!abstract && trans_mkdir(UNIX_DIR, mode) == -1) { |
| 1443 |
prmsg (1, "SocketUNIXCreateListener: mkdir(%s) failed, errno = %d\n", |
| 1444 |
UNIX_DIR, errno); |
| 1445 |
(void) umask (oldUmask); |
| 1446 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1447 |
} |
| 1448 |
#endif |
| 1449 |
|
| 1450 |
memset(&sockname, 0, sizeof(sockname)); |
| 1451 |
sockname.sun_family = AF_UNIX; |
| 1452 |
|
| 1453 |
if (!(port && *port)) { |
| 1454 |
snprintf (tmpport, sizeof(tmpport), "%s%ld", UNIX_PATH, (long)getpid()); |
| 1455 |
port = tmpport; |
| 1456 |
} |
| 1457 |
if (set_sun_path(port, UNIX_PATH, sockname.sun_path, abstract) != 0) { |
| 1458 |
prmsg (1, "SocketUNIXCreateListener: path too long\n"); |
| 1459 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1460 |
} |
| 1461 |
|
| 1462 |
#if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) |
| 1463 |
sockname.sun_len = strlen(sockname.sun_path); |
| 1464 |
#endif |
| 1465 |
|
| 1466 |
#if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 1467 |
namelen = SUN_LEN(&sockname); |
| 1468 |
#else |
| 1469 |
namelen = strlen(sockname.sun_path) + offsetof(struct sockaddr_un, sun_path); |
| 1470 |
#endif |
| 1471 |
|
| 1472 |
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) |
| 1473 |
fprintf(stderr, "SocketUNIXCreateListener: Unlinking path [%s] for ciptr at [%p].\n", |
| 1474 |
sockname.sun_path, (void *) ciptr); |
| 1475 |
#endif |
| 1476 |
if (abstract) { |
| 1477 |
sockname.sun_path[0] = '\0'; |
| 1478 |
namelen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&sockname.sun_path[1]); |
| 1479 |
} |
| 1480 |
else |
| 1481 |
unlink (sockname.sun_path); |
| 1482 |
|
| 1483 |
if ((status = TRANS(SocketCreateListener) (ciptr, |
| 1484 |
(struct sockaddr *) &sockname, namelen, flags)) < 0) |
| 1485 |
{ |
| 1486 |
prmsg (1, |
| 1487 |
"SocketUNIXCreateListener: ...SocketCreateListener() failed\n"); |
| 1488 |
(void) umask (oldUmask); |
| 1489 |
return status; |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* |
| 1493 |
* Now that the listener is esablished, create the addr info for |
| 1494 |
* this connection. getpeername() doesn't work for UNIX Domain Sockets |
| 1495 |
* on some systems (hpux at least), so we will just do it manually, instead |
| 1496 |
* of calling something like TRANS(SocketUNIXGetAddr). |
| 1497 |
*/ |
| 1498 |
|
| 1499 |
namelen = sizeof (sockname); /* this will always make it the same size */ |
| 1500 |
|
| 1501 |
if ((ciptr->addr = malloc (namelen)) == NULL) |
| 1502 |
{ |
| 1503 |
prmsg (1, |
| 1504 |
"SocketUNIXCreateListener: Can't allocate space for the addr\n"); |
| 1505 |
(void) umask (oldUmask); |
| 1506 |
return TRANS_CREATE_LISTENER_FAILED; |
| 1507 |
} |
| 1508 |
|
| 1509 |
if (abstract) |
| 1510 |
sockname.sun_path[0] = '@'; |
| 1511 |
|
| 1512 |
ciptr->family = sockname.sun_family; |
| 1513 |
ciptr->addrlen = namelen; |
| 1514 |
memcpy (ciptr->addr, &sockname, ciptr->addrlen); |
| 1515 |
|
| 1516 |
(void) umask (oldUmask); |
| 1517 |
|
| 1518 |
return 0; |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
static int |
| 1523 |
TRANS(SocketUNIXResetListener) (XtransConnInfo ciptr) |
| 1524 |
|
| 1525 |
{ |
| 1526 |
/* |
| 1527 |
* See if the unix domain socket has disappeared. If it has, recreate it. |
| 1528 |
*/ |
| 1529 |
|
| 1530 |
struct sockaddr_un *unsock = (struct sockaddr_un *) ciptr->addr; |
| 1531 |
struct stat statb; |
| 1532 |
int status = TRANS_RESET_NOOP; |
| 1533 |
unsigned int mode; |
| 1534 |
int abstract = 0; |
| 1535 |
#ifdef HAVE_ABSTRACT_SOCKETS |
| 1536 |
abstract = ciptr->transptr->flags & TRANS_ABSTRACT; |
| 1537 |
#endif |
| 1538 |
|
| 1539 |
prmsg (3, "SocketUNIXResetListener(%p,%d)\n", (void *) ciptr, ciptr->fd); |
| 1540 |
|
| 1541 |
if (!abstract && ( |
| 1542 |
stat (unsock->sun_path, &statb) == -1 || |
| 1543 |
((statb.st_mode & S_IFMT) != |
| 1544 |
#if defined(NCR) || defined(SCO325) || !defined(S_IFSOCK) |
| 1545 |
S_IFIFO |
| 1546 |
#else |
| 1547 |
S_IFSOCK |
| 1548 |
#endif |
| 1549 |
))) |
| 1550 |
{ |
| 1551 |
int oldUmask = umask (0); |
| 1552 |
|
| 1553 |
#ifdef UNIX_DIR |
| 1554 |
#ifdef HAS_STICKY_DIR_BIT |
| 1555 |
mode = 01777; |
| 1556 |
#else |
| 1557 |
mode = 0777; |
| 1558 |
#endif |
| 1559 |
if (trans_mkdir(UNIX_DIR, mode) == -1) { |
| 1560 |
prmsg (1, "SocketUNIXResetListener: mkdir(%s) failed, errno = %d\n", |
| 1561 |
UNIX_DIR, errno); |
| 1562 |
(void) umask (oldUmask); |
| 1563 |
return TRANS_RESET_FAILURE; |
| 1564 |
} |
| 1565 |
#endif |
| 1566 |
|
| 1567 |
close (ciptr->fd); |
| 1568 |
unlink (unsock->sun_path); |
| 1569 |
|
| 1570 |
if ((ciptr->fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 1571 |
{ |
| 1572 |
TRANS(FreeConnInfo) (ciptr); |
| 1573 |
(void) umask (oldUmask); |
| 1574 |
return TRANS_RESET_FAILURE; |
| 1575 |
} |
| 1576 |
|
| 1577 |
if (bind (ciptr->fd, (struct sockaddr *) unsock, ciptr->addrlen) < 0) |
| 1578 |
{ |
| 1579 |
close (ciptr->fd); |
| 1580 |
TRANS(FreeConnInfo) (ciptr); |
| 1581 |
return TRANS_RESET_FAILURE; |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (listen (ciptr->fd, BACKLOG) < 0) |
| 1585 |
{ |
| 1586 |
close (ciptr->fd); |
| 1587 |
TRANS(FreeConnInfo) (ciptr); |
| 1588 |
(void) umask (oldUmask); |
| 1589 |
return TRANS_RESET_FAILURE; |
| 1590 |
} |
| 1591 |
|
| 1592 |
umask (oldUmask); |
| 1593 |
|
| 1594 |
status = TRANS_RESET_NEW_FD; |
| 1595 |
} |
| 1596 |
|
| 1597 |
return status; |
| 1598 |
} |
| 1599 |
|
| 1600 |
#endif /* UNIXCONN */ |
| 1601 |
|
| 1602 |
|
| 1603 |
#ifdef TCPCONN |
| 1604 |
|
| 1605 |
static XtransConnInfo |
| 1606 |
TRANS(SocketINETAccept) (XtransConnInfo ciptr, int *status) |
| 1607 |
|
| 1608 |
{ |
| 1609 |
XtransConnInfo newciptr; |
| 1610 |
struct sockaddr_in sockname; |
| 1611 |
SOCKLEN_T namelen = sizeof(sockname); |
| 1612 |
|
| 1613 |
prmsg (2, "SocketINETAccept(%p,%d)\n", (void *) ciptr, ciptr->fd); |
| 1614 |
|
| 1615 |
if ((newciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) |
| 1616 |
{ |
| 1617 |
prmsg (1, "SocketINETAccept: malloc failed\n"); |
| 1618 |
*status = TRANS_ACCEPT_BAD_MALLOC; |
| 1619 |
return NULL; |
| 1620 |
} |
| 1621 |
|
| 1622 |
if ((newciptr->fd = accept (ciptr->fd, |
| 1623 |
(struct sockaddr *) &sockname, (void *)&namelen)) < 0) |
| 1624 |
{ |
| 1625 |
#ifdef WIN32 |
| 1626 |
errno = WSAGetLastError(); |
| 1627 |
#endif |
| 1628 |
prmsg (1, "SocketINETAccept: accept() failed\n"); |
| 1629 |
free (newciptr); |
| 1630 |
*status = TRANS_ACCEPT_FAILED; |
| 1631 |
return NULL; |
| 1632 |
} |
| 1633 |
|
| 1634 |
#ifdef TCP_NODELAY |
| 1635 |
{ |
| 1636 |
/* |
| 1637 |
* turn off TCP coalescence for INET sockets |
| 1638 |
*/ |
| 1639 |
|
| 1640 |
int tmp = 1; |
| 1641 |
setsockopt (newciptr->fd, IPPROTO_TCP, TCP_NODELAY, |
| 1642 |
(char *) &tmp, sizeof (int)); |
| 1643 |
} |
| 1644 |
#endif |
| 1645 |
|
| 1646 |
/* |
| 1647 |
* Get this address again because the transport may give a more |
| 1648 |
* specific address now that a connection is established. |
| 1649 |
*/ |
| 1650 |
|
| 1651 |
if (TRANS(SocketINETGetAddr) (newciptr) < 0) |
| 1652 |
{ |
| 1653 |
prmsg (1, |
| 1654 |
"SocketINETAccept: ...SocketINETGetAddr() failed:\n"); |
| 1655 |
close (newciptr->fd); |
| 1656 |
free (newciptr); |
| 1657 |
*status = TRANS_ACCEPT_MISC_ERROR; |
| 1658 |
return NULL; |
| 1659 |
} |
| 1660 |
|
| 1661 |
if (TRANS(SocketINETGetPeerAddr) (newciptr) < 0) |
| 1662 |
{ |
| 1663 |
prmsg (1, |
| 1664 |
"SocketINETAccept: ...SocketINETGetPeerAddr() failed:\n"); |
| 1665 |
close (newciptr->fd); |
| 1666 |
if (newciptr->addr) free (newciptr->addr); |
| 1667 |
free (newciptr); |
| 1668 |
*status = TRANS_ACCEPT_MISC_ERROR; |
| 1669 |
return NULL; |
| 1670 |
} |
| 1671 |
|
| 1672 |
*status = 0; |
| 1673 |
|
| 1674 |
return newciptr; |
| 1675 |
} |
| 1676 |
|
| 1677 |
#endif /* TCPCONN */ |
| 1678 |
|
| 1679 |
|
| 1680 |
#ifdef UNIXCONN |
| 1681 |
static XtransConnInfo |
| 1682 |
TRANS(SocketUNIXAccept) (XtransConnInfo ciptr, int *status) |
| 1683 |
|
| 1684 |
{ |
| 1685 |
XtransConnInfo newciptr; |
| 1686 |
struct sockaddr_un sockname; |
| 1687 |
SOCKLEN_T namelen = sizeof sockname; |
| 1688 |
|
| 1689 |
prmsg (2, "SocketUNIXAccept(%p,%d)\n", (void *) ciptr, ciptr->fd); |
| 1690 |
|
| 1691 |
if ((newciptr = calloc (1, sizeof(struct _XtransConnInfo))) == NULL) |
| 1692 |
{ |
| 1693 |
prmsg (1, "SocketUNIXAccept: malloc() failed\n"); |
| 1694 |
*status = TRANS_ACCEPT_BAD_MALLOC; |
| 1695 |
return NULL; |
| 1696 |
} |
| 1697 |
|
| 1698 |
if ((newciptr->fd = accept (ciptr->fd, |
| 1699 |
(struct sockaddr *) &sockname, (void *)&namelen)) < 0) |
| 1700 |
{ |
| 1701 |
prmsg (1, "SocketUNIXAccept: accept() failed\n"); |
| 1702 |
free (newciptr); |
| 1703 |
*status = TRANS_ACCEPT_FAILED; |
| 1704 |
return NULL; |
| 1705 |
} |
| 1706 |
|
| 1707 |
ciptr->addrlen = namelen; |
| 1708 |
/* |
| 1709 |
* Get the socket name and the peer name from the listener socket, |
| 1710 |
* since this is unix domain. |
| 1711 |
*/ |
| 1712 |
|
| 1713 |
if ((newciptr->addr = malloc (ciptr->addrlen)) == NULL) |
| 1714 |
{ |
| 1715 |
prmsg (1, |
| 1716 |
"SocketUNIXAccept: Can't allocate space for the addr\n"); |
| 1717 |
close (newciptr->fd); |
| 1718 |
free (newciptr); |
| 1719 |
*status = TRANS_ACCEPT_BAD_MALLOC; |
| 1720 |
return NULL; |
| 1721 |
} |
| 1722 |
|
| 1723 |
/* |
| 1724 |
* if the socket is abstract, we already modified the address to have a |
| 1725 |
* @ instead of the initial NUL, so no need to do that again here. |
| 1726 |
*/ |
| 1727 |
|
| 1728 |
newciptr->addrlen = ciptr->addrlen; |
| 1729 |
memcpy (newciptr->addr, ciptr->addr, newciptr->addrlen); |
| 1730 |
|
| 1731 |
if ((newciptr->peeraddr = malloc (ciptr->addrlen)) == NULL) |
| 1732 |
{ |
| 1733 |
prmsg (1, |
| 1734 |
"SocketUNIXAccept: Can't allocate space for the addr\n"); |
| 1735 |
close (newciptr->fd); |
| 1736 |
if (newciptr->addr) free (newciptr->addr); |
| 1737 |
free (newciptr); |
| 1738 |
*status = TRANS_ACCEPT_BAD_MALLOC; |
| 1739 |
return NULL; |
| 1740 |
} |
| 1741 |
|
| 1742 |
newciptr->peeraddrlen = ciptr->addrlen; |
| 1743 |
memcpy (newciptr->peeraddr, ciptr->addr, newciptr->addrlen); |
| 1744 |
|
| 1745 |
newciptr->family = AF_UNIX; |
| 1746 |
|
| 1747 |
*status = 0; |
| 1748 |
|
| 1749 |
return newciptr; |
| 1750 |
} |
| 1751 |
|
| 1752 |
#endif /* UNIXCONN */ |
| 1753 |
|
| 1754 |
#endif /* TRANS_SERVER */ |
| 1755 |
|
| 1756 |
|
| 1757 |
#ifdef TRANS_CLIENT |
| 1758 |
|
| 1759 |
#ifdef TCPCONN |
| 1760 |
|
| 1761 |
#if defined(IPv6) && defined(AF_INET6) |
| 1762 |
struct addrlist { |
| 1763 |
struct addrinfo * addr; |
| 1764 |
struct addrinfo * firstaddr; |
| 1765 |
char port[PORTBUFSIZE]; |
| 1766 |
char host[MAXHOSTNAMELEN]; |
| 1767 |
}; |
| 1768 |
static struct addrlist *addrlist = NULL; |
| 1769 |
#endif |
| 1770 |
|
| 1771 |
|
| 1772 |
static int |
| 1773 |
TRANS(SocketINETConnect) (XtransConnInfo ciptr, |
| 1774 |
const char *host, const char *port) |
| 1775 |
|
| 1776 |
{ |
| 1777 |
struct sockaddr * socketaddr = NULL; |
| 1778 |
int socketaddrlen = 0; |
| 1779 |
int res; |
| 1780 |
#if defined(IPv6) && defined(AF_INET6) |
| 1781 |
struct addrinfo hints; |
| 1782 |
char ntopbuf[INET6_ADDRSTRLEN]; |
| 1783 |
int resetonce = 0; |
| 1784 |
#else |
| 1785 |
struct sockaddr_in sockname; |
| 1786 |
struct hostent *hostp; |
| 1787 |
struct servent *servp; |
| 1788 |
unsigned long tmpaddr; |
| 1789 |
#endif |
| 1790 |
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS |
| 1791 |
_Xgethostbynameparams hparams; |
| 1792 |
_Xgetservbynameparams sparams; |
| 1793 |
#endif |
| 1794 |
#ifdef X11_t |
| 1795 |
char portbuf[PORTBUFSIZE]; |
| 1796 |
#endif |
| 1797 |
|
| 1798 |
char hostnamebuf[256]; /* tmp space */ |
| 1799 |
|
| 1800 |
prmsg (2,"SocketINETConnect(%d,%s,%s)\n", ciptr->fd, host, port); |
| 1801 |
|
| 1802 |
if (!host) |
| 1803 |
{ |
| 1804 |
hostnamebuf[0] = '\0'; |
| 1805 |
(void) TRANS(GetHostname) (hostnamebuf, sizeof hostnamebuf); |
| 1806 |
host = hostnamebuf; |
| 1807 |
} |
| 1808 |
|
| 1809 |
#ifdef X11_t |
| 1810 |
/* |
| 1811 |
* X has a well known port, that is transport dependent. It is easier |
| 1812 |
* to handle it here, than try and come up with a transport independent |
| 1813 |
* representation that can be passed in and resolved the usual way. |
| 1814 |
* |
| 1815 |
* The port that is passed here is really a string containing the idisplay |
| 1816 |
* from ConnectDisplay(). |
| 1817 |
*/ |
| 1818 |
|
| 1819 |
if (is_numeric (port)) |
| 1820 |
{ |
| 1821 |
long tmpport = X_TCP_PORT + strtol (port, (char**)NULL, 10); |
| 1822 |
snprintf (portbuf, sizeof(portbuf), "%lu", tmpport); |
| 1823 |
port = portbuf; |
| 1824 |
} |
| 1825 |
#endif |
| 1826 |
|
| 1827 |
#if defined(IPv6) && defined(AF_INET6) |
| 1828 |
{ |
| 1829 |
if (addrlist != NULL) { |
| 1830 |
if (strcmp(host,addrlist->host) || strcmp(port,addrlist->port)) { |
| 1831 |
if (addrlist->firstaddr) |
| 1832 |
freeaddrinfo(addrlist->firstaddr); |
| 1833 |
addrlist->firstaddr = NULL; |
| 1834 |
} |
| 1835 |
} else { |
| 1836 |
addrlist = malloc(sizeof(struct addrlist)); |
| 1837 |
addrlist->firstaddr = NULL; |
| 1838 |
} |
| 1839 |
|
| 1840 |
if (addrlist->firstaddr == NULL) { |
| 1841 |
strncpy(addrlist->port, port, sizeof(addrlist->port)); |
| 1842 |
addrlist->port[sizeof(addrlist->port) - 1] = '\0'; |
| 1843 |
strncpy(addrlist->host, host, sizeof(addrlist->host)); |
| 1844 |
addrlist->host[sizeof(addrlist->host) - 1] = '\0'; |
| 1845 |
|
| 1846 |
bzero(&hints,sizeof(hints)); |
| 1847 |
hints.ai_socktype = Sockettrans2devtab[ciptr->index].devcotsname; |
| 1848 |
|
| 1849 |
res = getaddrinfo(host,port,&hints,&addrlist->firstaddr); |
| 1850 |
if (res != 0) { |
| 1851 |
prmsg (1, "SocketINETConnect() can't get address " |
| 1852 |
"for %s:%s: %s\n", host, port, gai_strerror(res)); |
| 1853 |
ESET(EINVAL); |
| 1854 |
return TRANS_CONNECT_FAILED; |
| 1855 |
} |
| 1856 |
for (res = 0, addrlist->addr = addrlist->firstaddr; |
| 1857 |
addrlist->addr ; res++) { |
| 1858 |
addrlist->addr = addrlist->addr->ai_next; |
| 1859 |
} |
| 1860 |
prmsg(4,"Got New Address list with %d addresses\n", res); |
| 1861 |
res = 0; |
| 1862 |
addrlist->addr = NULL; |
| 1863 |
} |
| 1864 |
|
| 1865 |
while (socketaddr == NULL) { |
| 1866 |
if (addrlist->addr == NULL) { |
| 1867 |
if (resetonce) { |
| 1868 |
/* Already checked entire list - no usable addresses */ |
| 1869 |
prmsg (1, "SocketINETConnect() no usable address " |
| 1870 |
"for %s:%s\n", host, port); |
| 1871 |
return TRANS_CONNECT_FAILED; |
| 1872 |
} else { |
| 1873 |
/* Go back to beginning of list */ |
| 1874 |
resetonce = 1; |
| 1875 |
addrlist->addr = addrlist->firstaddr; |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
socketaddr = addrlist->addr->ai_addr; |
| 1880 |
socketaddrlen = addrlist->addr->ai_addrlen; |
| 1881 |
|
| 1882 |
if (addrlist->addr->ai_family == AF_INET) { |
| 1883 |
struct sockaddr_in *sin = (struct sockaddr_in *) socketaddr; |
| 1884 |
|
| 1885 |
prmsg (4,"SocketINETConnect() sockname.sin_addr = %s\n", |
| 1886 |
inet_ntop(addrlist->addr->ai_family,&sin->sin_addr, |
| 1887 |
ntopbuf,sizeof(ntopbuf))); |
| 1888 |
|
| 1889 |
prmsg (4,"SocketINETConnect() sockname.sin_port = %d\n", |
| 1890 |
ntohs(sin->sin_port)); |
| 1891 |
|
| 1892 |
if (Sockettrans2devtab[ciptr->index].family == AF_INET6) { |
| 1893 |
if (strcmp(Sockettrans2devtab[ciptr->index].transname, |
| 1894 |
"tcp") == 0) { |
| 1895 |
XtransConnInfo newciptr; |
| 1896 |
|
| 1897 |
/* |
| 1898 |
* Our socket is an IPv6 socket, but the address is |
| 1899 |
* IPv4. Close it and get an IPv4 socket. This is |
| 1900 |
* needed for IPv4 connections to work on platforms |
| 1901 |
* that don't allow IPv4 over IPv6 sockets. |
| 1902 |
*/ |
| 1903 |
TRANS(SocketINETClose)(ciptr); |
| 1904 |
newciptr = TRANS(SocketOpenCOTSClientBase)( |
| 1905 |
"tcp", "tcp", host, port, ciptr->index); |
| 1906 |
if (newciptr) |
| 1907 |
ciptr->fd = newciptr->fd; |
| 1908 |
if (!newciptr || |
| 1909 |
Sockettrans2devtab[newciptr->index].family != |
| 1910 |
AF_INET) { |
| 1911 |
socketaddr = NULL; |
| 1912 |
prmsg (4,"SocketINETConnect() Cannot get IPv4 " |
| 1913 |
" socketfor IPv4 address\n"); |
| 1914 |
} |
| 1915 |
if (newciptr) |
| 1916 |
free(newciptr); |
| 1917 |
} else { |
| 1918 |
socketaddr = NULL; |
| 1919 |
prmsg (4,"SocketINETConnect Skipping IPv4 address\n"); |
| 1920 |
} |
| 1921 |
} |
| 1922 |
} else if (addrlist->addr->ai_family == AF_INET6) { |
| 1923 |
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) socketaddr; |
| 1924 |
|
| 1925 |
prmsg (4,"SocketINETConnect() sockname.sin6_addr = %s\n", |
| 1926 |
inet_ntop(addrlist->addr->ai_family, |
| 1927 |
&sin6->sin6_addr,ntopbuf,sizeof(ntopbuf))); |
| 1928 |
prmsg (4,"SocketINETConnect() sockname.sin6_port = %d\n", |
| 1929 |
ntohs(sin6->sin6_port)); |
| 1930 |
|
| 1931 |
if (Sockettrans2devtab[ciptr->index].family == AF_INET) { |
| 1932 |
if (strcmp(Sockettrans2devtab[ciptr->index].transname, |
| 1933 |
"tcp") == 0) { |
| 1934 |
XtransConnInfo newciptr; |
| 1935 |
|
| 1936 |
/* |
| 1937 |
* Close the IPv4 socket and try to open an IPv6 socket. |
| 1938 |
*/ |
| 1939 |
TRANS(SocketINETClose)(ciptr); |
| 1940 |
newciptr = TRANS(SocketOpenCOTSClientBase)( |
| 1941 |
"tcp", "tcp", host, port, -1); |
| 1942 |
if (newciptr) |
| 1943 |
ciptr->fd = newciptr->fd; |
| 1944 |
if (!newciptr || |
| 1945 |
Sockettrans2devtab[newciptr->index].family != |
| 1946 |
AF_INET6) { |
| 1947 |
socketaddr = NULL; |
| 1948 |
prmsg (4,"SocketINETConnect() Cannot get IPv6 " |
| 1949 |
"socket for IPv6 address\n"); |
| 1950 |
} |
| 1951 |
if (newciptr) |
| 1952 |
free(newciptr); |
| 1953 |
} |
| 1954 |
else |
| 1955 |
{ |
| 1956 |
socketaddr = NULL; |
| 1957 |
prmsg (4,"SocketINETConnect() Skipping IPv6 address\n"); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
} else { |
| 1961 |
socketaddr = NULL; /* Unsupported address type */ |
| 1962 |
} |
| 1963 |
if (socketaddr == NULL) { |
| 1964 |
addrlist->addr = addrlist->addr->ai_next; |
| 1965 |
} |
| 1966 |
} |
| 1967 |
} |
| 1968 |
#else |
| 1969 |
{ |
| 1970 |
/* |
| 1971 |
* Build the socket name. |
| 1972 |
*/ |
| 1973 |
|
| 1974 |
#ifdef BSD44SOCKETS |
| 1975 |
sockname.sin_len = sizeof (struct sockaddr_in); |
| 1976 |
#endif |
| 1977 |
sockname.sin_family = AF_INET; |
| 1978 |
|
| 1979 |
/* |
| 1980 |
* fill in sin_addr |
| 1981 |
*/ |
| 1982 |
|
| 1983 |
#ifndef INADDR_NONE |
| 1984 |
#define INADDR_NONE ((in_addr_t) 0xffffffff) |
| 1985 |
#endif |
| 1986 |
|
| 1987 |
/* check for ww.xx.yy.zz host string */ |
| 1988 |
|
| 1989 |
if (isascii (host[0]) && isdigit (host[0])) { |
| 1990 |
tmpaddr = inet_addr (host); /* returns network byte order */ |
| 1991 |
} else { |
| 1992 |
tmpaddr = INADDR_NONE; |
| 1993 |
} |
| 1994 |
|
| 1995 |
prmsg (4,"SocketINETConnect() inet_addr(%s) = %x\n", host, tmpaddr); |
| 1996 |
|
| 1997 |
if (tmpaddr == INADDR_NONE) { |
| 1998 |
if ((hostp = _XGethostbyname(host,hparams)) == NULL) { |
| 1999 |
prmsg (1,"SocketINETConnect: Can't get address for %s\n", |
| 2000 |
host); |
| 2001 |
ESET(EINVAL); |
| 2002 |
return TRANS_CONNECT_FAILED; |
| 2003 |
} |
| 2004 |
if (hostp->h_addrtype != AF_INET) { /* is IP host? */ |
| 2005 |
prmsg (1,"SocketINETConnect: not INET host%s\n", host); |
| 2006 |
ESET(EPROTOTYPE); |
| 2007 |
return TRANS_CONNECT_FAILED; |
| 2008 |
} |
| 2009 |
|
| 2010 |
memcpy ((char *) &sockname.sin_addr, (char *) hostp->h_addr, |
| 2011 |
sizeof (sockname.sin_addr)); |
| 2012 |
|
| 2013 |
} else { |
| 2014 |
sockname.sin_addr.s_addr = tmpaddr; |
| 2015 |
} |
| 2016 |
|
| 2017 |
/* |
| 2018 |
* fill in sin_port |
| 2019 |
*/ |
| 2020 |
|
| 2021 |
/* Check for number in the port string */ |
| 2022 |
|
| 2023 |
if (!is_numeric (port)) { |
| 2024 |
if ((servp = _XGetservbyname (port,"tcp",sparams)) == NULL) { |
| 2025 |
prmsg (1,"SocketINETConnect: can't get service for %s\n", |
| 2026 |
port); |
| 2027 |
return TRANS_CONNECT_FAILED; |
| 2028 |
} |
| 2029 |
sockname.sin_port = htons (servp->s_port); |
| 2030 |
} else { |
| 2031 |
long tmpport = strtol (port, (char**)NULL, 10); |
| 2032 |
if (tmpport < 1024 || tmpport > USHRT_MAX) |
| 2033 |
return TRANS_CONNECT_FAILED; |
| 2034 |
sockname.sin_port = htons (((unsigned short) tmpport)); |
| 2035 |
} |
| 2036 |
|
| 2037 |
prmsg (4,"SocketINETConnect: sockname.sin_port = %d\n", |
| 2038 |
ntohs(sockname.sin_port)); |
| 2039 |
socketaddr = (struct sockaddr *) &sockname; |
| 2040 |
socketaddrlen = sizeof(sockname); |
| 2041 |
} |
| 2042 |
#endif |
| 2043 |
|
| 2044 |
/* |
| 2045 |
* Turn on socket keepalive so the client process will eventually |
| 2046 |
* be notified with a SIGPIPE signal if the display server fails |
| 2047 |
* to respond to a periodic transmission of messages |
| 2048 |
* on the connected socket. |
| 2049 |
* This is useful to avoid hung application processes when the |
| 2050 |
* processes are not spawned from the xdm session and |
| 2051 |
* the display server terminates abnormally. |
| 2052 |
* (Someone turned off the power switch.) |
| 2053 |
*/ |
| 2054 |
|
| 2055 |
{ |
| 2056 |
int tmp = 1; |
| 2057 |
setsockopt (ciptr->fd, SOL_SOCKET, SO_KEEPALIVE, |
| 2058 |
(char *) &tmp, sizeof (int)); |
| 2059 |
} |
| 2060 |
|
| 2061 |
/* |
| 2062 |
* Do the connect() |
| 2063 |
*/ |
| 2064 |
|
| 2065 |
if (connect (ciptr->fd, socketaddr, socketaddrlen ) < 0) |
| 2066 |
{ |
| 2067 |
#ifdef WIN32 |
| 2068 |
int olderrno = WSAGetLastError(); |
| 2069 |
#else |
| 2070 |
int olderrno = errno; |
| 2071 |
#endif |
| 2072 |
|
| 2073 |
/* |
| 2074 |
* If the error was ECONNREFUSED, the server may be overloaded |
| 2075 |
* and we should try again. |
| 2076 |
* |
| 2077 |
* If the error was EWOULDBLOCK or EINPROGRESS then the socket |
| 2078 |
* was non-blocking and we should poll using select |
| 2079 |
* |
| 2080 |
* If the error was EINTR, the connect was interrupted and we |
| 2081 |
* should try again. |
| 2082 |
* |
| 2083 |
* If multiple addresses are found for a host then we should |
| 2084 |
* try to connect again with a different address for a larger |
| 2085 |
* number of errors that made us quit before, since those |
| 2086 |
* could be caused by trying to use an IPv6 address to contact |
| 2087 |
* a machine with an IPv4-only server or other reasons that |
| 2088 |
* only affect one of a set of addresses. |
| 2089 |
*/ |
| 2090 |
|
| 2091 |
if (olderrno == ECONNREFUSED || olderrno == EINTR |
| 2092 |
#if defined(IPv6) && defined(AF_INET6) |
| 2093 |
|| (((addrlist->addr->ai_next != NULL) || |
| 2094 |
(addrlist->addr != addrlist->firstaddr)) && |
| 2095 |
(olderrno == ENETUNREACH || olderrno == EAFNOSUPPORT || |
| 2096 |
olderrno == EADDRNOTAVAIL || olderrno == ETIMEDOUT |
| 2097 |
#if defined(EHOSTDOWN) |
| 2098 |
|| olderrno == EHOSTDOWN |
| 2099 |
#endif |
| 2100 |
)) |
| 2101 |
#endif |
| 2102 |
) |
| 2103 |
res = TRANS_TRY_CONNECT_AGAIN; |
| 2104 |
else if (olderrno == EWOULDBLOCK || olderrno == EINPROGRESS) |
| 2105 |
res = TRANS_IN_PROGRESS; |
| 2106 |
else |
| 2107 |
{ |
| 2108 |
prmsg (2,"SocketINETConnect: Can't connect: errno = %d\n", |
| 2109 |
olderrno); |
| 2110 |
|
| 2111 |
res = TRANS_CONNECT_FAILED; |
| 2112 |
} |
| 2113 |
} else { |
| 2114 |
res = 0; |
| 2115 |
|
| 2116 |
|
| 2117 |
/* |
| 2118 |
* Sync up the address fields of ciptr. |
| 2119 |
*/ |
| 2120 |
|
| 2121 |
if (TRANS(SocketINETGetAddr) (ciptr) < 0) |
| 2122 |
{ |
| 2123 |
prmsg (1, |
| 2124 |
"SocketINETConnect: ...SocketINETGetAddr() failed:\n"); |
| 2125 |
res = TRANS_CONNECT_FAILED; |
| 2126 |
} |
| 2127 |
|
| 2128 |
else if (TRANS(SocketINETGetPeerAddr) (ciptr) < 0) |
| 2129 |
{ |
| 2130 |
prmsg (1, |
| 2131 |
"SocketINETConnect: ...SocketINETGetPeerAddr() failed:\n"); |
| 2132 |
res = TRANS_CONNECT_FAILED; |
| 2133 |
} |
| 2134 |
} |
| 2135 |
|
| 2136 |
#if defined(IPv6) && defined(AF_INET6) |
| 2137 |
if (res != 0) { |
| 2138 |
addrlist->addr = addrlist->addr->ai_next; |
| 2139 |
} |
| 2140 |
#endif |
| 2141 |
|
| 2142 |
return res; |
| 2143 |
} |
| 2144 |
|
| 2145 |
#endif /* TCPCONN */ |
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
#ifdef UNIXCONN |
| 2150 |
|
| 2151 |
/* |
| 2152 |
* Make sure 'host' is really local. |
| 2153 |
*/ |
| 2154 |
|
| 2155 |
static int |
| 2156 |
UnixHostReallyLocal (const char *host) |
| 2157 |
|
| 2158 |
{ |
| 2159 |
char hostnamebuf[256]; |
| 2160 |
|
| 2161 |
TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf)); |
| 2162 |
|
| 2163 |
if (strcmp (hostnamebuf, host) == 0) |
| 2164 |
{ |
| 2165 |
return (1); |
| 2166 |
} else { |
| 2167 |
#if defined(IPv6) && defined(AF_INET6) |
| 2168 |
struct addrinfo *localhostaddr; |
| 2169 |
struct addrinfo *otherhostaddr; |
| 2170 |
struct addrinfo *i, *j; |
| 2171 |
int equiv = 0; |
| 2172 |
|
| 2173 |
if (getaddrinfo(hostnamebuf, NULL, NULL, &localhostaddr) != 0) |
| 2174 |
return 0; |
| 2175 |
if (getaddrinfo(host, NULL, NULL, &otherhostaddr) != 0) { |
| 2176 |
freeaddrinfo(localhostaddr); |
| 2177 |
return 0; |
| 2178 |
} |
| 2179 |
|
| 2180 |
for (i = localhostaddr; i != NULL && equiv == 0; i = i->ai_next) { |
| 2181 |
for (j = otherhostaddr; j != NULL && equiv == 0; j = j->ai_next) { |
| 2182 |
if (i->ai_family == j->ai_family) { |
| 2183 |
if (i->ai_family == AF_INET) { |
| 2184 |
struct sockaddr_in *sinA |
| 2185 |
= (struct sockaddr_in *) i->ai_addr; |
| 2186 |
struct sockaddr_in *sinB |
| 2187 |
= (struct sockaddr_in *) j->ai_addr; |
| 2188 |
struct in_addr *A = &sinA->sin_addr; |
| 2189 |
struct in_addr *B = &sinB->sin_addr; |
| 2190 |
|
| 2191 |
if (memcmp(A,B,sizeof(struct in_addr)) == 0) { |
| 2192 |
equiv = 1; |
| 2193 |
} |
| 2194 |
} else if (i->ai_family == AF_INET6) { |
| 2195 |
struct sockaddr_in6 *sinA |
| 2196 |
= (struct sockaddr_in6 *) i->ai_addr; |
| 2197 |
struct sockaddr_in6 *sinB |
| 2198 |
= (struct sockaddr_in6 *) j->ai_addr; |
| 2199 |
struct in6_addr *A = &sinA->sin6_addr; |
| 2200 |
struct in6_addr *B = &sinB->sin6_addr; |
| 2201 |
|
| 2202 |
if (memcmp(A,B,sizeof(struct in6_addr)) == 0) { |
| 2203 |
equiv = 1; |
| 2204 |
} |
| 2205 |
} |
| 2206 |
} |
| 2207 |
} |
| 2208 |
} |
| 2209 |
|
| 2210 |
freeaddrinfo(localhostaddr); |
| 2211 |
freeaddrinfo(otherhostaddr); |
| 2212 |
return equiv; |
| 2213 |
#else |
| 2214 |
/* |
| 2215 |
* A host may have more than one network address. If any of the |
| 2216 |
* network addresses of 'host' (specified to the connect call) |
| 2217 |
* match any of the network addresses of 'hostname' (determined |
| 2218 |
* by TRANS(GetHostname)), then the two hostnames are equivalent, |
| 2219 |
* and we know that 'host' is really a local host. |
| 2220 |
*/ |
| 2221 |
char specified_local_addr_list[10][4]; |
| 2222 |
int scount, equiv, i, j; |
| 2223 |
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS |
| 2224 |
_Xgethostbynameparams hparams; |
| 2225 |
#endif |
| 2226 |
struct hostent *hostp; |
| 2227 |
|
| 2228 |
if ((hostp = _XGethostbyname (host,hparams)) == NULL) |
| 2229 |
return (0); |
| 2230 |
|
| 2231 |
scount = 0; |
| 2232 |
while (hostp->h_addr_list[scount] && scount <= 8) |
| 2233 |
{ |
| 2234 |
/* |
| 2235 |
* The 2nd call to gethostname() overrides the data |
| 2236 |
* from the 1st call, so we must save the address list. |
| 2237 |
*/ |
| 2238 |
|
| 2239 |
specified_local_addr_list[scount][0] = |
| 2240 |
hostp->h_addr_list[scount][0]; |
| 2241 |
specified_local_addr_list[scount][1] = |
| 2242 |
hostp->h_addr_list[scount][1]; |
| 2243 |
specified_local_addr_list[scount][2] = |
| 2244 |
hostp->h_addr_list[scount][2]; |
| 2245 |
specified_local_addr_list[scount][3] = |
| 2246 |
hostp->h_addr_list[scount][3]; |
| 2247 |
scount++; |
| 2248 |
} |
| 2249 |
if ((hostp = _XGethostbyname (hostnamebuf,hparams)) == NULL) |
| 2250 |
return (0); |
| 2251 |
|
| 2252 |
equiv = 0; |
| 2253 |
i = 0; |
| 2254 |
|
| 2255 |
while (i < scount && !equiv) |
| 2256 |
{ |
| 2257 |
j = 0; |
| 2258 |
|
| 2259 |
while (hostp->h_addr_list[j]) |
| 2260 |
{ |
| 2261 |
if ((specified_local_addr_list[i][0] == |
| 2262 |
hostp->h_addr_list[j][0]) && |
| 2263 |
(specified_local_addr_list[i][1] == |
| 2264 |
hostp->h_addr_list[j][1]) && |
| 2265 |
(specified_local_addr_list[i][2] == |
| 2266 |
hostp->h_addr_list[j][2]) && |
| 2267 |
(specified_local_addr_list[i][3] == |
| 2268 |
hostp->h_addr_list[j][3])) |
| 2269 |
{ |
| 2270 |
/* They're equal, so we're done */ |
| 2271 |
|
| 2272 |
equiv = 1; |
| 2273 |
break; |
| 2274 |
} |
| 2275 |
|
| 2276 |
j++; |
| 2277 |
} |
| 2278 |
|
| 2279 |
i++; |
| 2280 |
} |
| 2281 |
return (equiv); |
| 2282 |
#endif |
| 2283 |
} |
| 2284 |
} |
| 2285 |
|
| 2286 |
static int |
| 2287 |
TRANS(SocketUNIXConnect) (XtransConnInfo ciptr, |
| 2288 |
const char *host, const char *port) |
| 2289 |
|
| 2290 |
{ |
| 2291 |
struct sockaddr_un sockname; |
| 2292 |
SOCKLEN_T namelen; |
| 2293 |
|
| 2294 |
|
| 2295 |
int abstract = 0; |
| 2296 |
#ifdef HAVE_ABSTRACT_SOCKETS |
| 2297 |
abstract = ciptr->transptr->flags & TRANS_ABSTRACT; |
| 2298 |
#endif |
| 2299 |
|
| 2300 |
prmsg (2,"SocketUNIXConnect(%d,%s,%s)\n", ciptr->fd, host, port); |
| 2301 |
|
| 2302 |
/* |
| 2303 |
* Make sure 'host' is really local. If not, we return failure. |
| 2304 |
* The reason we make this check is because a process may advertise |
| 2305 |
* a "local" network ID for which it can accept connections, but if |
| 2306 |
* a process on a remote machine tries to connect to this network ID, |
| 2307 |
* we know for sure it will fail. |
| 2308 |
*/ |
| 2309 |
|
| 2310 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2311 |
if (host && *host && host[0]!='/' && strcmp(host, "unix") != 0 && strcasecmp(host, "nx") != 0 && |
| 2312 |
strncasecmp(host, "nx,", 3) != 0 && !UnixHostReallyLocal (host)) |
| 2313 |
#else |
| 2314 |
if (host && *host && host[0]!='/' && strcmp (host, "unix") != 0 && !UnixHostReallyLocal (host)) |
| 2315 |
#endif |
| 2316 |
{ |
| 2317 |
prmsg (1, |
| 2318 |
"SocketUNIXConnect: Cannot connect to non-local host %s\n", |
| 2319 |
host); |
| 2320 |
return TRANS_CONNECT_FAILED; |
| 2321 |
} |
| 2322 |
|
| 2323 |
|
| 2324 |
/* |
| 2325 |
* Check the port. |
| 2326 |
*/ |
| 2327 |
|
| 2328 |
if (!port || !*port) |
| 2329 |
{ |
| 2330 |
prmsg (1,"SocketUNIXConnect: Missing port specification\n"); |
| 2331 |
return TRANS_CONNECT_FAILED; |
| 2332 |
} |
| 2333 |
|
| 2334 |
/* |
| 2335 |
* Build the socket name. |
| 2336 |
*/ |
| 2337 |
|
| 2338 |
sockname.sun_family = AF_UNIX; |
| 2339 |
|
| 2340 |
if (set_sun_path(port, UNIX_PATH, sockname.sun_path, abstract) != 0) { |
| 2341 |
prmsg (1, "SocketUNIXConnect: path too long\n"); |
| 2342 |
return TRANS_CONNECT_FAILED; |
| 2343 |
} |
| 2344 |
|
| 2345 |
#if (defined(BSD44SOCKETS) || defined(__UNIXWARE__)) |
| 2346 |
sockname.sun_len = strlen (sockname.sun_path); |
| 2347 |
#endif |
| 2348 |
|
| 2349 |
#if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 2350 |
namelen = SUN_LEN (&sockname); |
| 2351 |
#else |
| 2352 |
namelen = strlen (sockname.sun_path) + offsetof(struct sockaddr_un, sun_path); |
| 2353 |
#endif |
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2358 |
|
| 2359 |
if (ciptr->priv != NULL) |
| 2360 |
{ |
| 2361 |
if (TRANS(SocketConnectConnInfo) (ciptr, host, port) != 0) |
| 2362 |
{ |
| 2363 |
return TRANS_CONNECT_FAILED; |
| 2364 |
} |
| 2365 |
|
| 2366 |
goto SocketUNIXConnectPost; |
| 2367 |
} |
| 2368 |
|
| 2369 |
#endif |
| 2370 |
|
| 2371 |
/* |
| 2372 |
* Adjust the socket path if using abstract sockets. |
| 2373 |
* Done here because otherwise all the strlen() calls above would fail. |
| 2374 |
*/ |
| 2375 |
|
| 2376 |
if (abstract) { |
| 2377 |
sockname.sun_path[0] = '\0'; |
| 2378 |
} |
| 2379 |
|
| 2380 |
/* |
| 2381 |
* Do the connect() |
| 2382 |
*/ |
| 2383 |
|
| 2384 |
if (connect (ciptr->fd, (struct sockaddr *) &sockname, namelen) < 0) |
| 2385 |
{ |
| 2386 |
int olderrno = errno; |
| 2387 |
int connected = 0; |
| 2388 |
|
| 2389 |
if (!connected) |
| 2390 |
{ |
| 2391 |
errno = olderrno; |
| 2392 |
|
| 2393 |
/* |
| 2394 |
* If the error was ENOENT, the server may be starting up; we used |
| 2395 |
* to suggest to try again in this case with |
| 2396 |
* TRANS_TRY_CONNECT_AGAIN, but this introduced problems for |
| 2397 |
* processes still referencing stale sockets in their environment. |
| 2398 |
* Hence, we now return a hard error, TRANS_CONNECT_FAILED, and it |
| 2399 |
* is suggested that higher level stacks handle retries on their |
| 2400 |
* level when they face a slow starting server. |
| 2401 |
* |
| 2402 |
* If the error was EWOULDBLOCK or EINPROGRESS then the socket |
| 2403 |
* was non-blocking and we should poll using select |
| 2404 |
* |
| 2405 |
* If the error was EINTR, the connect was interrupted and we |
| 2406 |
* should try again. |
| 2407 |
*/ |
| 2408 |
|
| 2409 |
if (olderrno == EWOULDBLOCK || olderrno == EINPROGRESS) |
| 2410 |
return TRANS_IN_PROGRESS; |
| 2411 |
else if (olderrno == EINTR) |
| 2412 |
return TRANS_TRY_CONNECT_AGAIN; |
| 2413 |
else if (olderrno == ENOENT || olderrno == ECONNREFUSED) { |
| 2414 |
/* If opening as abstract socket failed, try again normally */ |
| 2415 |
if (abstract) { |
| 2416 |
ciptr->transptr->flags &= ~(TRANS_ABSTRACT); |
| 2417 |
return TRANS_TRY_CONNECT_AGAIN; |
| 2418 |
} else { |
| 2419 |
return TRANS_CONNECT_FAILED; |
| 2420 |
} |
| 2421 |
} else { |
| 2422 |
prmsg (2,"SocketUNIXConnect: Can't connect: errno = %d\n", |
| 2423 |
EGET()); |
| 2424 |
|
| 2425 |
return TRANS_CONNECT_FAILED; |
| 2426 |
} |
| 2427 |
} |
| 2428 |
} |
| 2429 |
|
| 2430 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2431 |
|
| 2432 |
SocketUNIXConnectPost: |
| 2433 |
|
| 2434 |
#endif |
| 2435 |
|
| 2436 |
/* |
| 2437 |
* Get the socket name and the peer name from the connect socket, |
| 2438 |
* since this is unix domain. |
| 2439 |
*/ |
| 2440 |
|
| 2441 |
if ((ciptr->addr = malloc(namelen)) == NULL || |
| 2442 |
(ciptr->peeraddr = malloc(namelen)) == NULL) |
| 2443 |
{ |
| 2444 |
prmsg (1, |
| 2445 |
"SocketUNIXCreateListener: Can't allocate space for the addr\n"); |
| 2446 |
return TRANS_CONNECT_FAILED; |
| 2447 |
} |
| 2448 |
|
| 2449 |
if (abstract) |
| 2450 |
sockname.sun_path[0] = '@'; |
| 2451 |
|
| 2452 |
ciptr->family = AF_UNIX; |
| 2453 |
ciptr->addrlen = namelen; |
| 2454 |
ciptr->peeraddrlen = namelen; |
| 2455 |
memcpy (ciptr->addr, &sockname, ciptr->addrlen); |
| 2456 |
memcpy (ciptr->peeraddr, &sockname, ciptr->peeraddrlen); |
| 2457 |
|
| 2458 |
return 0; |
| 2459 |
} |
| 2460 |
|
| 2461 |
#endif /* UNIXCONN */ |
| 2462 |
|
| 2463 |
#endif /* TRANS_CLIENT */ |
| 2464 |
|
| 2465 |
|
| 2466 |
static int |
| 2467 |
TRANS(SocketBytesReadable) (XtransConnInfo ciptr, BytesReadable_t *pend) |
| 2468 |
|
| 2469 |
{ |
| 2470 |
prmsg (2,"SocketBytesReadable(%p,%d,%p)\n", |
| 2471 |
(void *) ciptr, ciptr->fd, (void *) pend); |
| 2472 |
|
| 2473 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2474 |
|
| 2475 |
if (ciptr->priv) |
| 2476 |
{ |
| 2477 |
if (NXTransRunning(ciptr->fd) == 0) |
| 2478 |
{ |
| 2479 |
/* |
| 2480 |
* Force the application to shut down the |
| 2481 |
* socket if the NX transport is gone. We |
| 2482 |
* may probably save this additional call. |
| 2483 |
*/ |
| 2484 |
|
| 2485 |
#ifdef NX_TRANS_DEBUG |
| 2486 |
fprintf(stderr, "SocketBytesReadable: NX transport not running for descriptor [%d].\n", |
| 2487 |
ciptr->fd); |
| 2488 |
#endif |
| 2489 |
ESET(EPIPE); |
| 2490 |
|
| 2491 |
return -1; |
| 2492 |
} |
| 2493 |
else |
| 2494 |
{ |
| 2495 |
/* |
| 2496 |
* Emulate BytesReadable. Some X applications may use the system |
| 2497 |
* select() in their main loop, instead of the _XSelect() that is |
| 2498 |
* replaced by NX. Still these applications use _XEventsQueued to |
| 2499 |
* poll events from the X connection, and _XEventsQueued uses the |
| 2500 |
* NX _XSelect(), so it is generally possible to let the client |
| 2501 |
* yield the control to NX and let it handle the I/O on the proxy |
| 2502 |
* descriptors even if the application is not explicitly designed |
| 2503 |
* to work as a NX agent. |
| 2504 |
*/ |
| 2505 |
|
| 2506 |
#ifdef NX_TRANS_DEBUG |
| 2507 |
|
| 2508 |
int result; |
| 2509 |
|
| 2510 |
result = NXTransReadable(ciptr->fd, (int *) pend); |
| 2511 |
|
| 2512 |
fprintf(stderr, "SocketBytesReadable: Descriptor [%d] result [%d] readable [%ld].\n", |
| 2513 |
ciptr->fd, result, *pend); |
| 2514 |
|
| 2515 |
return result; |
| 2516 |
#else |
| 2517 |
return NXTransReadable(ciptr->fd, (int *) pend); |
| 2518 |
#endif |
| 2519 |
} |
| 2520 |
} |
| 2521 |
|
| 2522 |
#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2523 |
|
| 2524 |
#ifdef WIN32 |
| 2525 |
{ |
| 2526 |
int ret = ioctlsocket ((SOCKET) ciptr->fd, FIONREAD, (u_long *) pend); |
| 2527 |
if (ret == SOCKET_ERROR) errno = WSAGetLastError(); |
| 2528 |
return ret; |
| 2529 |
} |
| 2530 |
#else |
| 2531 |
return ioctl (ciptr->fd, FIONREAD, (char *) pend); |
| 2532 |
#endif /* WIN32 */ |
| 2533 |
} |
| 2534 |
|
| 2535 |
#if XTRANS_SEND_FDS |
| 2536 |
|
| 2537 |
static void |
| 2538 |
appendFd(struct _XtransConnFd **prev, int fd, int do_close) |
| 2539 |
{ |
| 2540 |
struct _XtransConnFd *cf, *new; |
| 2541 |
|
| 2542 |
new = malloc (sizeof (struct _XtransConnFd)); |
| 2543 |
if (!new) { |
| 2544 |
/* XXX mark connection as broken */ |
| 2545 |
close(fd); |
| 2546 |
return; |
| 2547 |
} |
| 2548 |
new->next = 0; |
| 2549 |
new->fd = fd; |
| 2550 |
new->do_close = do_close; |
| 2551 |
/* search to end of list */ |
| 2552 |
for (; (cf = *prev); prev = &(cf->next)); |
| 2553 |
*prev = new; |
| 2554 |
} |
| 2555 |
|
| 2556 |
static int |
| 2557 |
removeFd(struct _XtransConnFd **prev) |
| 2558 |
{ |
| 2559 |
struct _XtransConnFd *cf; |
| 2560 |
int fd; |
| 2561 |
|
| 2562 |
if ((cf = *prev)) { |
| 2563 |
*prev = cf->next; |
| 2564 |
fd = cf->fd; |
| 2565 |
free(cf); |
| 2566 |
} else |
| 2567 |
fd = -1; |
| 2568 |
return fd; |
| 2569 |
} |
| 2570 |
|
| 2571 |
static void |
| 2572 |
discardFd(struct _XtransConnFd **prev, struct _XtransConnFd *upto, int do_close) |
| 2573 |
{ |
| 2574 |
struct _XtransConnFd *cf, *next; |
| 2575 |
|
| 2576 |
for (cf = *prev; cf != upto; cf = next) { |
| 2577 |
next = cf->next; |
| 2578 |
if (do_close || cf->do_close) |
| 2579 |
close(cf->fd); |
| 2580 |
free(cf); |
| 2581 |
} |
| 2582 |
*prev = upto; |
| 2583 |
} |
| 2584 |
|
| 2585 |
static void |
| 2586 |
cleanupFds(XtransConnInfo ciptr) |
| 2587 |
{ |
| 2588 |
/* Clean up the send list but don't close the fds */ |
| 2589 |
discardFd(&ciptr->send_fds, NULL, 0); |
| 2590 |
/* Clean up the recv list and *do* close the fds */ |
| 2591 |
discardFd(&ciptr->recv_fds, NULL, 1); |
| 2592 |
} |
| 2593 |
|
| 2594 |
static int |
| 2595 |
nFd(struct _XtransConnFd **prev) |
| 2596 |
{ |
| 2597 |
struct _XtransConnFd *cf; |
| 2598 |
int n = 0; |
| 2599 |
|
| 2600 |
for (cf = *prev; cf; cf = cf->next) |
| 2601 |
n++; |
| 2602 |
return n; |
| 2603 |
} |
| 2604 |
|
| 2605 |
static int |
| 2606 |
TRANS(SocketRecvFd) (XtransConnInfo ciptr) |
| 2607 |
{ |
| 2608 |
prmsg (2, "SocketRecvFd(%d)\n", ciptr->fd); |
| 2609 |
return removeFd(&ciptr->recv_fds); |
| 2610 |
} |
| 2611 |
|
| 2612 |
static int |
| 2613 |
TRANS(SocketSendFd) (XtransConnInfo ciptr, int fd, int do_close) |
| 2614 |
{ |
| 2615 |
appendFd(&ciptr->send_fds, fd, do_close); |
| 2616 |
return 0; |
| 2617 |
} |
| 2618 |
|
| 2619 |
static int |
| 2620 |
TRANS(SocketRecvFdInvalid)(XtransConnInfo ciptr) |
| 2621 |
{ |
| 2622 |
errno = EINVAL; |
| 2623 |
return -1; |
| 2624 |
} |
| 2625 |
|
| 2626 |
static int |
| 2627 |
TRANS(SocketSendFdInvalid)(XtransConnInfo ciptr, int fd, int do_close) |
| 2628 |
{ |
| 2629 |
errno = EINVAL; |
| 2630 |
return -1; |
| 2631 |
} |
| 2632 |
|
| 2633 |
#define MAX_FDS 128 |
| 2634 |
|
| 2635 |
union fd_pass { |
| 2636 |
struct cmsghdr cmsghdr; |
| 2637 |
char buf[CMSG_SPACE(MAX_FDS * sizeof(int))]; |
| 2638 |
}; |
| 2639 |
|
| 2640 |
#endif /* XTRANS_SEND_FDS */ |
| 2641 |
|
| 2642 |
static int |
| 2643 |
TRANS(SocketRead) (XtransConnInfo ciptr, char *buf, int size) |
| 2644 |
|
| 2645 |
{ |
| 2646 |
prmsg (2,"SocketRead(%d,%p,%d)\n", ciptr->fd, (void *) buf, size); |
| 2647 |
|
| 2648 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2649 |
|
| 2650 |
/* |
| 2651 |
* If we have a valid priv pointer then this |
| 2652 |
* is an internal connection to the proxy. |
| 2653 |
* In this case we should emulate the read. |
| 2654 |
*/ |
| 2655 |
|
| 2656 |
if (ciptr->priv) |
| 2657 |
{ |
| 2658 |
int result; |
| 2659 |
|
| 2660 |
result = NXTransRead(ciptr->fd, buf, size); |
| 2661 |
|
| 2662 |
#ifdef NX_TRANS_DEBUG |
| 2663 |
if (result < 0 && EGET() == EAGAIN) |
| 2664 |
{ |
| 2665 |
fprintf(stderr, "SocketRead: Read from descriptor [%d] would block.\n", |
| 2666 |
ciptr->fd); |
| 2667 |
} |
| 2668 |
else |
| 2669 |
{ |
| 2670 |
fprintf(stderr, "SocketRead: Read [%d] bytes from descriptor [%d].\n", |
| 2671 |
result, ciptr->fd); |
| 2672 |
} |
| 2673 |
#endif |
| 2674 |
return result; |
| 2675 |
} |
| 2676 |
else |
| 2677 |
{ |
| 2678 |
/* FIXME: same code as below, should be possible without duplication */ |
| 2679 |
#if XTRANS_SEND_FDS |
| 2680 |
struct iovec iov = { |
| 2681 |
.iov_base = buf, |
| 2682 |
.iov_len = size |
| 2683 |
}; |
| 2684 |
union fd_pass cmsgbuf; |
| 2685 |
struct msghdr msg = { |
| 2686 |
.msg_name = NULL, |
| 2687 |
.msg_namelen = 0, |
| 2688 |
.msg_iov = &iov, |
| 2689 |
.msg_iovlen = 1, |
| 2690 |
.msg_control = cmsgbuf.buf, |
| 2691 |
.msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) |
| 2692 |
}; |
| 2693 |
|
| 2694 |
size = recvmsg(ciptr->fd, &msg, 0); |
| 2695 |
if (size >= 0) { |
| 2696 |
struct cmsghdr *hdr; |
| 2697 |
|
| 2698 |
for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { |
| 2699 |
if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { |
| 2700 |
int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); |
| 2701 |
int i; |
| 2702 |
int *fd = (int *) CMSG_DATA(hdr); |
| 2703 |
|
| 2704 |
for (i = 0; i < nfd; i++) |
| 2705 |
appendFd(&ciptr->recv_fds, fd[i], 0); |
| 2706 |
} |
| 2707 |
} |
| 2708 |
} |
| 2709 |
return size; |
| 2710 |
#else |
| 2711 |
return read (ciptr->fd, buf, size); |
| 2712 |
#endif /* XTRANS_SEND_FDS */ |
| 2713 |
} |
| 2714 |
|
| 2715 |
#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2716 |
|
| 2717 |
#if defined(WIN32) |
| 2718 |
{ |
| 2719 |
int ret = recv ((SOCKET)ciptr->fd, buf, size, 0); |
| 2720 |
#ifdef WIN32 |
| 2721 |
if (ret == SOCKET_ERROR) errno = WSAGetLastError(); |
| 2722 |
#endif |
| 2723 |
return ret; |
| 2724 |
} |
| 2725 |
#else |
| 2726 |
#if XTRANS_SEND_FDS |
| 2727 |
{ |
| 2728 |
struct iovec iov = { |
| 2729 |
.iov_base = buf, |
| 2730 |
.iov_len = size |
| 2731 |
}; |
| 2732 |
union fd_pass cmsgbuf; |
| 2733 |
struct msghdr msg = { |
| 2734 |
.msg_name = NULL, |
| 2735 |
.msg_namelen = 0, |
| 2736 |
.msg_iov = &iov, |
| 2737 |
.msg_iovlen = 1, |
| 2738 |
.msg_control = cmsgbuf.buf, |
| 2739 |
.msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) |
| 2740 |
}; |
| 2741 |
|
| 2742 |
size = recvmsg(ciptr->fd, &msg, 0); |
| 2743 |
if (size >= 0) { |
| 2744 |
struct cmsghdr *hdr; |
| 2745 |
|
| 2746 |
for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { |
| 2747 |
if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { |
| 2748 |
int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); |
| 2749 |
int i; |
| 2750 |
int *fd = (int *) CMSG_DATA(hdr); |
| 2751 |
|
| 2752 |
for (i = 0; i < nfd; i++) |
| 2753 |
appendFd(&ciptr->recv_fds, fd[i], 0); |
| 2754 |
} |
| 2755 |
} |
| 2756 |
} |
| 2757 |
return size; |
| 2758 |
} |
| 2759 |
#else |
| 2760 |
return read (ciptr->fd, buf, size); |
| 2761 |
#endif /* XTRANS_SEND_FDS */ |
| 2762 |
#endif /* WIN32 */ |
| 2763 |
|
| 2764 |
#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2765 |
} |
| 2766 |
|
| 2767 |
static int |
| 2768 |
TRANS(SocketReadv) (XtransConnInfo ciptr, struct iovec *buf, int size) |
| 2769 |
|
| 2770 |
{ |
| 2771 |
prmsg (2,"SocketReadv(%d,%p,%d)\n", ciptr->fd, (void *) buf, size); |
| 2772 |
|
| 2773 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2774 |
|
| 2775 |
/* |
| 2776 |
* If we have a valid priv pointer then this |
| 2777 |
* is an internal connection to the proxy. |
| 2778 |
* In this case we should emulate the readv. |
| 2779 |
*/ |
| 2780 |
|
| 2781 |
if (ciptr->priv) |
| 2782 |
{ |
| 2783 |
return NXTransReadVector(ciptr->fd, buf, size); |
| 2784 |
} |
| 2785 |
else |
| 2786 |
{ |
| 2787 |
/* FIXME: same code as below, should be possible without duplication */ |
| 2788 |
#if XTRANS_SEND_FDS |
| 2789 |
union fd_pass cmsgbuf; |
| 2790 |
struct msghdr msg = { |
| 2791 |
.msg_name = NULL, |
| 2792 |
.msg_namelen = 0, |
| 2793 |
.msg_iov = buf, |
| 2794 |
.msg_iovlen = size, |
| 2795 |
.msg_control = cmsgbuf.buf, |
| 2796 |
.msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) |
| 2797 |
}; |
| 2798 |
|
| 2799 |
size = recvmsg(ciptr->fd, &msg, 0); |
| 2800 |
if (size >= 0) { |
| 2801 |
struct cmsghdr *hdr; |
| 2802 |
|
| 2803 |
for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { |
| 2804 |
if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { |
| 2805 |
int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); |
| 2806 |
int i; |
| 2807 |
int *fd = (int *) CMSG_DATA(hdr); |
| 2808 |
|
| 2809 |
for (i = 0; i < nfd; i++) |
| 2810 |
appendFd(&ciptr->recv_fds, fd[i], 0); |
| 2811 |
} |
| 2812 |
} |
| 2813 |
} |
| 2814 |
return size; |
| 2815 |
#else |
| 2816 |
return READV (ciptr, buf, size); |
| 2817 |
#endif |
| 2818 |
} |
| 2819 |
|
| 2820 |
#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2821 |
|
| 2822 |
#if XTRANS_SEND_FDS |
| 2823 |
{ |
| 2824 |
union fd_pass cmsgbuf; |
| 2825 |
struct msghdr msg = { |
| 2826 |
.msg_name = NULL, |
| 2827 |
.msg_namelen = 0, |
| 2828 |
.msg_iov = buf, |
| 2829 |
.msg_iovlen = size, |
| 2830 |
.msg_control = cmsgbuf.buf, |
| 2831 |
.msg_controllen = CMSG_LEN(MAX_FDS * sizeof(int)) |
| 2832 |
}; |
| 2833 |
|
| 2834 |
size = recvmsg(ciptr->fd, &msg, 0); |
| 2835 |
if (size >= 0) { |
| 2836 |
struct cmsghdr *hdr; |
| 2837 |
|
| 2838 |
for (hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) { |
| 2839 |
if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SCM_RIGHTS) { |
| 2840 |
int nfd = (hdr->cmsg_len - CMSG_LEN(0)) / sizeof (int); |
| 2841 |
int i; |
| 2842 |
int *fd = (int *) CMSG_DATA(hdr); |
| 2843 |
|
| 2844 |
for (i = 0; i < nfd; i++) |
| 2845 |
appendFd(&ciptr->recv_fds, fd[i], 0); |
| 2846 |
} |
| 2847 |
} |
| 2848 |
} |
| 2849 |
return size; |
| 2850 |
} |
| 2851 |
#else |
| 2852 |
return READV (ciptr, buf, size); |
| 2853 |
#endif |
| 2854 |
|
| 2855 |
#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2856 |
} |
| 2857 |
|
| 2858 |
|
| 2859 |
static int |
| 2860 |
TRANS(SocketWritev) (XtransConnInfo ciptr, struct iovec *buf, int size) |
| 2861 |
|
| 2862 |
{ |
| 2863 |
prmsg (2,"SocketWritev(%d,%p,%d)\n", ciptr->fd, (void *) buf, size); |
| 2864 |
|
| 2865 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2866 |
|
| 2867 |
/* |
| 2868 |
* If we have a valid priv pointer then this |
| 2869 |
* is an internal connection to the proxy. |
| 2870 |
* In this case we should emulate the writev. |
| 2871 |
*/ |
| 2872 |
|
| 2873 |
if (ciptr->priv) |
| 2874 |
{ |
| 2875 |
return NXTransWriteVector(ciptr->fd, buf, size); |
| 2876 |
} |
| 2877 |
#endif |
| 2878 |
|
| 2879 |
#if XTRANS_SEND_FDS |
| 2880 |
if (ciptr->send_fds) |
| 2881 |
{ |
| 2882 |
union fd_pass cmsgbuf; |
| 2883 |
int nfd = nFd(&ciptr->send_fds); |
| 2884 |
struct _XtransConnFd *cf = ciptr->send_fds; |
| 2885 |
struct msghdr msg = { |
| 2886 |
.msg_name = NULL, |
| 2887 |
.msg_namelen = 0, |
| 2888 |
.msg_iov = buf, |
| 2889 |
.msg_iovlen = size, |
| 2890 |
.msg_control = cmsgbuf.buf, |
| 2891 |
.msg_controllen = CMSG_LEN(nfd * sizeof(int)) |
| 2892 |
}; |
| 2893 |
struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); |
| 2894 |
int i; |
| 2895 |
int *fds; |
| 2896 |
|
| 2897 |
hdr->cmsg_len = msg.msg_controllen; |
| 2898 |
hdr->cmsg_level = SOL_SOCKET; |
| 2899 |
hdr->cmsg_type = SCM_RIGHTS; |
| 2900 |
|
| 2901 |
fds = (int *) CMSG_DATA(hdr); |
| 2902 |
/* Set up fds */ |
| 2903 |
for (i = 0; i < nfd; i++) { |
| 2904 |
fds[i] = cf->fd; |
| 2905 |
cf = cf->next; |
| 2906 |
} |
| 2907 |
|
| 2908 |
i = sendmsg(ciptr->fd, &msg, 0); |
| 2909 |
if (i > 0) |
| 2910 |
discardFd(&ciptr->send_fds, cf, 0); |
| 2911 |
return i; |
| 2912 |
} |
| 2913 |
#endif |
| 2914 |
return WRITEV (ciptr, buf, size); |
| 2915 |
} |
| 2916 |
|
| 2917 |
static int |
| 2918 |
TRANS(SocketWrite) (XtransConnInfo ciptr, char *buf, int size) |
| 2919 |
|
| 2920 |
{ |
| 2921 |
prmsg (2,"SocketWrite(%d,%p,%d)\n", ciptr->fd, (void *) buf, size); |
| 2922 |
|
| 2923 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 2924 |
|
| 2925 |
/* |
| 2926 |
* If we have a valid priv pointer then this |
| 2927 |
* is an internal connection to the proxy. |
| 2928 |
* In this case we should emulate the write. |
| 2929 |
*/ |
| 2930 |
|
| 2931 |
if (ciptr->priv) |
| 2932 |
{ |
| 2933 |
int result; |
| 2934 |
|
| 2935 |
result = NXTransWrite(ciptr->fd, buf, size); |
| 2936 |
|
| 2937 |
#ifdef NX_TRANS_DEBUG |
| 2938 |
if (result < 0 && EGET() == EAGAIN) |
| 2939 |
{ |
| 2940 |
fprintf(stderr, "SocketWrite: Write on descriptor [%d] would block.\n", |
| 2941 |
ciptr->fd); |
| 2942 |
} |
| 2943 |
else |
| 2944 |
{ |
| 2945 |
fprintf(stderr, "SocketWrite: Written [%d] bytes on descriptor [%d].\n", |
| 2946 |
result, ciptr->fd); |
| 2947 |
} |
| 2948 |
#endif |
| 2949 |
return result; |
| 2950 |
} |
| 2951 |
else |
| 2952 |
{ |
| 2953 |
/* FIXME: same code as below, should be possible without duplication */ |
| 2954 |
#if XTRANS_SEND_FDS |
| 2955 |
if (ciptr->send_fds) |
| 2956 |
{ |
| 2957 |
struct iovec iov; |
| 2958 |
|
| 2959 |
iov.iov_base = buf; |
| 2960 |
iov.iov_len = size; |
| 2961 |
return TRANS(SocketWritev)(ciptr, &iov, 1); |
| 2962 |
} |
| 2963 |
#endif /* XTRANS_SEND_FDS */ |
| 2964 |
return write (ciptr->fd, buf, size); |
| 2965 |
} |
| 2966 |
|
| 2967 |
#else /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2968 |
|
| 2969 |
#if defined(WIN32) |
| 2970 |
{ |
| 2971 |
int ret = send ((SOCKET)ciptr->fd, buf, size, 0); |
| 2972 |
#ifdef WIN32 |
| 2973 |
if (ret == SOCKET_ERROR) errno = WSAGetLastError(); |
| 2974 |
#endif |
| 2975 |
return ret; |
| 2976 |
} |
| 2977 |
#else |
| 2978 |
#if XTRANS_SEND_FDS |
| 2979 |
if (ciptr->send_fds) |
| 2980 |
{ |
| 2981 |
struct iovec iov; |
| 2982 |
|
| 2983 |
iov.iov_base = buf; |
| 2984 |
iov.iov_len = size; |
| 2985 |
return TRANS(SocketWritev)(ciptr, &iov, 1); |
| 2986 |
} |
| 2987 |
#endif /* XTRANS_SEND_FDS */ |
| 2988 |
return write (ciptr->fd, buf, size); |
| 2989 |
#endif /* WIN32 */ |
| 2990 |
|
| 2991 |
#endif /* #if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) */ |
| 2992 |
} |
| 2993 |
|
| 2994 |
static int |
| 2995 |
TRANS(SocketDisconnect) (XtransConnInfo ciptr) |
| 2996 |
|
| 2997 |
{ |
| 2998 |
prmsg (2,"SocketDisconnect(%p,%d)\n", (void *) ciptr, ciptr->fd); |
| 2999 |
|
| 3000 |
#ifdef WIN32 |
| 3001 |
{ |
| 3002 |
int ret = shutdown (ciptr->fd, 2); |
| 3003 |
if (ret == SOCKET_ERROR) errno = WSAGetLastError(); |
| 3004 |
return ret; |
| 3005 |
} |
| 3006 |
#else |
| 3007 |
return shutdown (ciptr->fd, 2); /* disallow further sends and receives */ |
| 3008 |
#endif |
| 3009 |
} |
| 3010 |
|
| 3011 |
|
| 3012 |
#ifdef TCPCONN |
| 3013 |
static int |
| 3014 |
TRANS(SocketINETClose) (XtransConnInfo ciptr) |
| 3015 |
|
| 3016 |
{ |
| 3017 |
prmsg (2,"SocketINETClose(%p,%d)\n", (void *) ciptr, ciptr->fd); |
| 3018 |
|
| 3019 |
#ifdef WIN32 |
| 3020 |
{ |
| 3021 |
int ret = close (ciptr->fd); |
| 3022 |
if (ret == SOCKET_ERROR) errno = WSAGetLastError(); |
| 3023 |
return ret; |
| 3024 |
} |
| 3025 |
#else |
| 3026 |
return close (ciptr->fd); |
| 3027 |
#endif |
| 3028 |
} |
| 3029 |
|
| 3030 |
#endif /* TCPCONN */ |
| 3031 |
|
| 3032 |
|
| 3033 |
#ifdef UNIXCONN |
| 3034 |
static int |
| 3035 |
TRANS(SocketUNIXClose) (XtransConnInfo ciptr) |
| 3036 |
{ |
| 3037 |
/* |
| 3038 |
* If this is the server side, then once the socket is closed, |
| 3039 |
* it must be unlinked to completely close it |
| 3040 |
*/ |
| 3041 |
|
| 3042 |
struct sockaddr_un *sockname = (struct sockaddr_un *) ciptr->addr; |
| 3043 |
int ret; |
| 3044 |
|
| 3045 |
prmsg (2,"SocketUNIXClose(%p,%d)\n", (void *) ciptr, ciptr->fd); |
| 3046 |
|
| 3047 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 3048 |
|
| 3049 |
if (ciptr->priv) |
| 3050 |
{ |
| 3051 |
TRANS(SocketCloseConnInfo) (ciptr); |
| 3052 |
} |
| 3053 |
|
| 3054 |
#endif |
| 3055 |
|
| 3056 |
#if XTRANS_SEND_FDS |
| 3057 |
cleanupFds(ciptr); |
| 3058 |
#endif |
| 3059 |
ret = close(ciptr->fd); |
| 3060 |
|
| 3061 |
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) |
| 3062 |
fprintf(stderr, "SocketUNIXClose: Flags are [%d] for ciptr at [%p] check is [%d].\n", |
| 3063 |
ciptr->flags, (void *) ciptr, (ciptr->flags && sockname |
| 3064 |
&& sockname->sun_family == AF_UNIX && sockname->sun_path[0])); |
| 3065 |
#endif |
| 3066 |
|
| 3067 |
if (ciptr->flags |
| 3068 |
&& sockname |
| 3069 |
&& sockname->sun_family == AF_UNIX |
| 3070 |
&& sockname->sun_path[0]) |
| 3071 |
{ |
| 3072 |
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST) |
| 3073 |
if (!(ciptr->flags & TRANS_NOUNLINK |
| 3074 |
|| ciptr->transptr->flags & TRANS_ABSTRACT)) |
| 3075 |
{ |
| 3076 |
fprintf(stderr, "SocketUNIXClose: Unlinking path [%s] for ciptr at [%p].\n", |
| 3077 |
sockname->sun_path, (void *) ciptr); |
| 3078 |
unlink (sockname->sun_path); |
| 3079 |
} |
| 3080 |
#else |
| 3081 |
if (!(ciptr->flags & TRANS_NOUNLINK |
| 3082 |
|| ciptr->transptr->flags & TRANS_ABSTRACT)) |
| 3083 |
unlink (sockname->sun_path); |
| 3084 |
#endif |
| 3085 |
} |
| 3086 |
|
| 3087 |
return ret; |
| 3088 |
} |
| 3089 |
|
| 3090 |
static int |
| 3091 |
TRANS(SocketUNIXCloseForCloning) (XtransConnInfo ciptr) |
| 3092 |
|
| 3093 |
{ |
| 3094 |
/* |
| 3095 |
* Don't unlink path. |
| 3096 |
*/ |
| 3097 |
|
| 3098 |
int ret; |
| 3099 |
|
| 3100 |
prmsg (2,"SocketUNIXCloseForCloning(%p,%d)\n", |
| 3101 |
(void *) ciptr, ciptr->fd); |
| 3102 |
|
| 3103 |
#if defined(NX_TRANS_SOCKET) && defined(TRANS_CLIENT) |
| 3104 |
|
| 3105 |
if (ciptr->priv) |
| 3106 |
{ |
| 3107 |
TRANS(SocketCloseConnInfo) (ciptr); |
| 3108 |
} |
| 3109 |
|
| 3110 |
#endif |
| 3111 |
|
| 3112 |
#if XTRANS_SEND_FDS |
| 3113 |
cleanupFds(ciptr); |
| 3114 |
#endif |
| 3115 |
ret = close(ciptr->fd); |
| 3116 |
|
| 3117 |
return ret; |
| 3118 |
} |
| 3119 |
|
| 3120 |
#endif /* UNIXCONN */ |
| 3121 |
|
| 3122 |
|
| 3123 |
#ifdef TCPCONN |
| 3124 |
# ifdef TRANS_SERVER |
| 3125 |
static const char* tcp_nolisten[] = { |
| 3126 |
"inet", |
| 3127 |
#if defined(IPv6) && defined(AF_INET6) |
| 3128 |
"inet6", |
| 3129 |
#endif |
| 3130 |
NULL |
| 3131 |
}; |
| 3132 |
# endif |
| 3133 |
|
| 3134 |
Xtransport TRANS(SocketTCPFuncs) = { |
| 3135 |
/* Socket Interface */ |
| 3136 |
"tcp", |
| 3137 |
TRANS_ALIAS, |
| 3138 |
#ifdef TRANS_CLIENT |
| 3139 |
TRANS(SocketOpenCOTSClient), |
| 3140 |
#endif /* TRANS_CLIENT */ |
| 3141 |
#ifdef TRANS_SERVER |
| 3142 |
tcp_nolisten, |
| 3143 |
TRANS(SocketOpenCOTSServer), |
| 3144 |
#endif /* TRANS_SERVER */ |
| 3145 |
#ifdef TRANS_REOPEN |
| 3146 |
TRANS(SocketReopenCOTSServer), |
| 3147 |
#endif |
| 3148 |
TRANS(SocketSetOption), |
| 3149 |
#ifdef TRANS_SERVER |
| 3150 |
TRANS(SocketINETCreateListener), |
| 3151 |
NULL, /* ResetListener */ |
| 3152 |
TRANS(SocketINETAccept), |
| 3153 |
#endif /* TRANS_SERVER */ |
| 3154 |
#ifdef TRANS_CLIENT |
| 3155 |
TRANS(SocketINETConnect), |
| 3156 |
#endif /* TRANS_CLIENT */ |
| 3157 |
TRANS(SocketBytesReadable), |
| 3158 |
TRANS(SocketRead), |
| 3159 |
TRANS(SocketWrite), |
| 3160 |
TRANS(SocketReadv), |
| 3161 |
TRANS(SocketWritev), |
| 3162 |
#if XTRANS_SEND_FDS |
| 3163 |
TRANS(SocketSendFdInvalid), |
| 3164 |
TRANS(SocketRecvFdInvalid), |
| 3165 |
#endif |
| 3166 |
TRANS(SocketDisconnect), |
| 3167 |
TRANS(SocketINETClose), |
| 3168 |
TRANS(SocketINETClose), |
| 3169 |
}; |
| 3170 |
|
| 3171 |
Xtransport TRANS(SocketINETFuncs) = { |
| 3172 |
/* Socket Interface */ |
| 3173 |
"inet", |
| 3174 |
0, |
| 3175 |
#ifdef TRANS_CLIENT |
| 3176 |
TRANS(SocketOpenCOTSClient), |
| 3177 |
#endif /* TRANS_CLIENT */ |
| 3178 |
#ifdef TRANS_SERVER |
| 3179 |
NULL, |
| 3180 |
TRANS(SocketOpenCOTSServer), |
| 3181 |
#endif /* TRANS_SERVER */ |
| 3182 |
#ifdef TRANS_REOPEN |
| 3183 |
TRANS(SocketReopenCOTSServer), |
| 3184 |
#endif |
| 3185 |
TRANS(SocketSetOption), |
| 3186 |
#ifdef TRANS_SERVER |
| 3187 |
TRANS(SocketINETCreateListener), |
| 3188 |
NULL, /* ResetListener */ |
| 3189 |
TRANS(SocketINETAccept), |
| 3190 |
#endif /* TRANS_SERVER */ |
| 3191 |
#ifdef TRANS_CLIENT |
| 3192 |
TRANS(SocketINETConnect), |
| 3193 |
#endif /* TRANS_CLIENT */ |
| 3194 |
TRANS(SocketBytesReadable), |
| 3195 |
TRANS(SocketRead), |
| 3196 |
TRANS(SocketWrite), |
| 3197 |
TRANS(SocketReadv), |
| 3198 |
TRANS(SocketWritev), |
| 3199 |
#if XTRANS_SEND_FDS |
| 3200 |
TRANS(SocketSendFdInvalid), |
| 3201 |
TRANS(SocketRecvFdInvalid), |
| 3202 |
#endif |
| 3203 |
TRANS(SocketDisconnect), |
| 3204 |
TRANS(SocketINETClose), |
| 3205 |
TRANS(SocketINETClose), |
| 3206 |
}; |
| 3207 |
|
| 3208 |
#if defined(IPv6) && defined(AF_INET6) |
| 3209 |
Xtransport TRANS(SocketINET6Funcs) = { |
| 3210 |
/* Socket Interface */ |
| 3211 |
"inet6", |
| 3212 |
0, |
| 3213 |
#ifdef TRANS_CLIENT |
| 3214 |
TRANS(SocketOpenCOTSClient), |
| 3215 |
#endif /* TRANS_CLIENT */ |
| 3216 |
#ifdef TRANS_SERVER |
| 3217 |
NULL, |
| 3218 |
TRANS(SocketOpenCOTSServer), |
| 3219 |
#endif /* TRANS_SERVER */ |
| 3220 |
#ifdef TRANS_REOPEN |
| 3221 |
TRANS(SocketReopenCOTSServer), |
| 3222 |
#endif |
| 3223 |
TRANS(SocketSetOption), |
| 3224 |
#ifdef TRANS_SERVER |
| 3225 |
TRANS(SocketINETCreateListener), |
| 3226 |
NULL, /* ResetListener */ |
| 3227 |
TRANS(SocketINETAccept), |
| 3228 |
#endif /* TRANS_SERVER */ |
| 3229 |
#ifdef TRANS_CLIENT |
| 3230 |
TRANS(SocketINETConnect), |
| 3231 |
#endif /* TRANS_CLIENT */ |
| 3232 |
TRANS(SocketBytesReadable), |
| 3233 |
TRANS(SocketRead), |
| 3234 |
TRANS(SocketWrite), |
| 3235 |
TRANS(SocketReadv), |
| 3236 |
TRANS(SocketWritev), |
| 3237 |
#if XTRANS_SEND_FDS |
| 3238 |
TRANS(SocketSendFdInvalid), |
| 3239 |
TRANS(SocketRecvFdInvalid), |
| 3240 |
#endif |
| 3241 |
TRANS(SocketDisconnect), |
| 3242 |
TRANS(SocketINETClose), |
| 3243 |
TRANS(SocketINETClose), |
| 3244 |
}; |
| 3245 |
#endif /* IPv6 */ |
| 3246 |
#endif /* TCPCONN */ |
| 3247 |
|
| 3248 |
#ifdef UNIXCONN |
| 3249 |
#if !defined(LOCALCONN) |
| 3250 |
Xtransport TRANS(SocketLocalFuncs) = { |
| 3251 |
/* Socket Interface */ |
| 3252 |
"local", |
| 3253 |
#ifdef HAVE_ABSTRACT_SOCKETS |
| 3254 |
TRANS_ABSTRACT, |
| 3255 |
#else |
| 3256 |
0, |
| 3257 |
#endif |
| 3258 |
#ifdef TRANS_CLIENT |
| 3259 |
TRANS(SocketOpenCOTSClient), |
| 3260 |
#endif /* TRANS_CLIENT */ |
| 3261 |
#ifdef TRANS_SERVER |
| 3262 |
NULL, |
| 3263 |
TRANS(SocketOpenCOTSServer), |
| 3264 |
#endif /* TRANS_SERVER */ |
| 3265 |
#ifdef TRANS_REOPEN |
| 3266 |
TRANS(SocketReopenCOTSServer), |
| 3267 |
#endif |
| 3268 |
TRANS(SocketSetOption), |
| 3269 |
#ifdef TRANS_SERVER |
| 3270 |
TRANS(SocketUNIXCreateListener), |
| 3271 |
TRANS(SocketUNIXResetListener), |
| 3272 |
TRANS(SocketUNIXAccept), |
| 3273 |
#endif /* TRANS_SERVER */ |
| 3274 |
#ifdef TRANS_CLIENT |
| 3275 |
TRANS(SocketUNIXConnect), |
| 3276 |
#endif /* TRANS_CLIENT */ |
| 3277 |
TRANS(SocketBytesReadable), |
| 3278 |
TRANS(SocketRead), |
| 3279 |
TRANS(SocketWrite), |
| 3280 |
TRANS(SocketReadv), |
| 3281 |
TRANS(SocketWritev), |
| 3282 |
#if XTRANS_SEND_FDS |
| 3283 |
TRANS(SocketSendFd), |
| 3284 |
TRANS(SocketRecvFd), |
| 3285 |
#endif |
| 3286 |
TRANS(SocketDisconnect), |
| 3287 |
TRANS(SocketUNIXClose), |
| 3288 |
TRANS(SocketUNIXCloseForCloning), |
| 3289 |
}; |
| 3290 |
#endif /* !LOCALCONN */ |
| 3291 |
# ifdef TRANS_SERVER |
| 3292 |
# if !defined(LOCALCONN) |
| 3293 |
static const char* unix_nolisten[] = { "local" , NULL }; |
| 3294 |
# endif |
| 3295 |
# endif |
| 3296 |
|
| 3297 |
Xtransport TRANS(SocketUNIXFuncs) = { |
| 3298 |
/* Socket Interface */ |
| 3299 |
"unix", |
| 3300 |
#if !defined(LOCALCONN) && !defined(HAVE_ABSTRACT_SOCKETS) |
| 3301 |
TRANS_ALIAS, |
| 3302 |
#else |
| 3303 |
0, |
| 3304 |
#endif |
| 3305 |
#ifdef TRANS_CLIENT |
| 3306 |
TRANS(SocketOpenCOTSClient), |
| 3307 |
#endif /* TRANS_CLIENT */ |
| 3308 |
#ifdef TRANS_SERVER |
| 3309 |
#if !defined(LOCALCONN) |
| 3310 |
unix_nolisten, |
| 3311 |
#else |
| 3312 |
NULL, |
| 3313 |
#endif |
| 3314 |
TRANS(SocketOpenCOTSServer), |
| 3315 |
#endif /* TRANS_SERVER */ |
| 3316 |
#ifdef TRANS_REOPEN |
| 3317 |
TRANS(SocketReopenCOTSServer), |
| 3318 |
#endif |
| 3319 |
TRANS(SocketSetOption), |
| 3320 |
#ifdef TRANS_SERVER |
| 3321 |
TRANS(SocketUNIXCreateListener), |
| 3322 |
TRANS(SocketUNIXResetListener), |
| 3323 |
TRANS(SocketUNIXAccept), |
| 3324 |
#endif /* TRANS_SERVER */ |
| 3325 |
#ifdef TRANS_CLIENT |
| 3326 |
TRANS(SocketUNIXConnect), |
| 3327 |
#endif /* TRANS_CLIENT */ |
| 3328 |
TRANS(SocketBytesReadable), |
| 3329 |
TRANS(SocketRead), |
| 3330 |
TRANS(SocketWrite), |
| 3331 |
TRANS(SocketReadv), |
| 3332 |
TRANS(SocketWritev), |
| 3333 |
#if XTRANS_SEND_FDS |
| 3334 |
TRANS(SocketSendFd), |
| 3335 |
TRANS(SocketRecvFd), |
| 3336 |
#endif |
| 3337 |
TRANS(SocketDisconnect), |
| 3338 |
TRANS(SocketUNIXClose), |
| 3339 |
TRANS(SocketUNIXCloseForCloning), |
| 3340 |
}; |
| 3341 |
|
| 3342 |
#endif /* UNIXCONN */ |