| 1 |
/* |
| 2 |
|
| 3 |
Copyright 1993, 1994, 1998 The Open Group |
| 4 |
|
| 5 |
Permission to use, copy, modify, distribute, and sell this software and its |
| 6 |
documentation for any purpose is hereby granted without fee, provided that |
| 7 |
the above copyright notice appear in all copies and that both that |
| 8 |
copyright notice and this permission notice appear in supporting |
| 9 |
documentation. |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included |
| 12 |
in all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
|
| 22 |
Except as contained in this notice, the name of The Open Group shall |
| 23 |
not be used in advertising or otherwise to promote the sale, use or |
| 24 |
other dealings in this Software without prior written authorization |
| 25 |
from The Open Group. |
| 26 |
|
| 27 |
* Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA |
| 28 |
* |
| 29 |
* All Rights Reserved |
| 30 |
* |
| 31 |
* Permission to use, copy, modify, and distribute this software and its |
| 32 |
* documentation for any purpose and without fee is hereby granted, provided |
| 33 |
* that the above copyright notice appear in all copies and that both that |
| 34 |
* copyright notice and this permission notice appear in supporting |
| 35 |
* documentation, and that the name NCR not be used in advertising |
| 36 |
* or publicity pertaining to distribution of the software without specific, |
| 37 |
* written prior permission. NCR makes no representations about the |
| 38 |
* suitability of this software for any purpose. It is provided "as is" |
| 39 |
* without express or implied warranty. |
| 40 |
* |
| 41 |
* NCR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 42 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 43 |
* NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 44 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 45 |
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 46 |
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 47 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 48 |
*/ |
| 49 |
|
| 50 |
#include <ctype.h> |
| 51 |
#include <stdlib.h> |
| 52 |
#include <string.h> |
| 53 |
#ifdef HAVE_SYSTEMD_DAEMON |
| 54 |
#include <systemd/sd-daemon.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
/* |
| 58 |
* The transport table contains a definition for every transport (protocol) |
| 59 |
* family. All operations that can be made on the transport go through this |
| 60 |
* table. |
| 61 |
* |
| 62 |
* Each transport is assigned a unique transport id. |
| 63 |
* |
| 64 |
* New transports can be added by adding an entry in this table. |
| 65 |
* For compatiblity, the transport ids should never be renumbered. |
| 66 |
* Always add to the end of the list. |
| 67 |
*/ |
| 68 |
|
| 69 |
#define TRANS_TLI_INET_INDEX 1 |
| 70 |
#define TRANS_TLI_TCP_INDEX 2 |
| 71 |
#define TRANS_TLI_TLI_INDEX 3 |
| 72 |
#define TRANS_SOCKET_UNIX_INDEX 4 |
| 73 |
#define TRANS_SOCKET_LOCAL_INDEX 5 |
| 74 |
#define TRANS_SOCKET_INET_INDEX 6 |
| 75 |
#define TRANS_SOCKET_TCP_INDEX 7 |
| 76 |
#define TRANS_DNET_INDEX 8 |
| 77 |
#define TRANS_LOCAL_LOCAL_INDEX 9 |
| 78 |
#define TRANS_LOCAL_PTS_INDEX 10 |
| 79 |
#define TRANS_LOCAL_NAMED_INDEX 11 |
| 80 |
/* 12 used to be ISC, but that's gone. */ |
| 81 |
#define TRANS_LOCAL_SCO_INDEX 13 |
| 82 |
#define TRANS_SOCKET_INET6_INDEX 14 |
| 83 |
#define TRANS_LOCAL_PIPE_INDEX 15 |
| 84 |
|
| 85 |
|
| 86 |
static |
| 87 |
Xtransport_table Xtransports[] = { |
| 88 |
#if defined(TCPCONN) |
| 89 |
{ &TRANS(SocketTCPFuncs), TRANS_SOCKET_TCP_INDEX }, |
| 90 |
#if defined(IPv6) && defined(AF_INET6) |
| 91 |
{ &TRANS(SocketINET6Funcs), TRANS_SOCKET_INET6_INDEX }, |
| 92 |
#endif /* IPv6 */ |
| 93 |
{ &TRANS(SocketINETFuncs), TRANS_SOCKET_INET_INDEX }, |
| 94 |
#endif /* TCPCONN */ |
| 95 |
#if defined(UNIXCONN) |
| 96 |
#if !defined(LOCALCONN) |
| 97 |
{ &TRANS(SocketLocalFuncs), TRANS_SOCKET_LOCAL_INDEX }, |
| 98 |
#endif /* !LOCALCONN */ |
| 99 |
{ &TRANS(SocketUNIXFuncs), TRANS_SOCKET_UNIX_INDEX }, |
| 100 |
#endif /* UNIXCONN */ |
| 101 |
#if defined(LOCALCONN) |
| 102 |
{ &TRANS(LocalFuncs), TRANS_LOCAL_LOCAL_INDEX }, |
| 103 |
#ifndef __sun |
| 104 |
{ &TRANS(PTSFuncs), TRANS_LOCAL_PTS_INDEX }, |
| 105 |
#endif /* __sun */ |
| 106 |
#if defined(SVR4) || defined(__SVR4) |
| 107 |
{ &TRANS(NAMEDFuncs), TRANS_LOCAL_NAMED_INDEX }, |
| 108 |
#endif |
| 109 |
#ifdef __sun |
| 110 |
{ &TRANS(PIPEFuncs), TRANS_LOCAL_PIPE_INDEX }, |
| 111 |
#endif /* __sun */ |
| 112 |
#if defined(__SCO__) || defined(__UNIXWARE__) |
| 113 |
{ &TRANS(SCOFuncs), TRANS_LOCAL_SCO_INDEX }, |
| 114 |
#endif /* __SCO__ || __UNIXWARE__ */ |
| 115 |
#endif /* LOCALCONN */ |
| 116 |
}; |
| 117 |
|
| 118 |
#define NUMTRANS (sizeof(Xtransports)/sizeof(Xtransport_table)) |
| 119 |
|
| 120 |
|
| 121 |
#ifdef WIN32 |
| 122 |
#define ioctl ioctlsocket |
| 123 |
#endif |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
/* |
| 128 |
* These are a few utility function used by the public interface functions. |
| 129 |
*/ |
| 130 |
|
| 131 |
void |
| 132 |
TRANS(FreeConnInfo) (XtransConnInfo ciptr) |
| 133 |
|
| 134 |
{ |
| 135 |
prmsg (3,"FreeConnInfo(%p)\n", (void *) ciptr); |
| 136 |
|
| 137 |
if (ciptr->addr) |
| 138 |
free (ciptr->addr); |
| 139 |
|
| 140 |
if (ciptr->peeraddr) |
| 141 |
free (ciptr->peeraddr); |
| 142 |
|
| 143 |
if (ciptr->port) |
| 144 |
free (ciptr->port); |
| 145 |
|
| 146 |
free (ciptr); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
#define PROTOBUFSIZE 20 |
| 151 |
|
| 152 |
static Xtransport * |
| 153 |
TRANS(SelectTransport) (const char *protocol) |
| 154 |
|
| 155 |
{ |
| 156 |
char protobuf[PROTOBUFSIZE]; |
| 157 |
int i; |
| 158 |
|
| 159 |
prmsg (3,"SelectTransport(%s)\n", protocol); |
| 160 |
|
| 161 |
/* |
| 162 |
* Force Protocol to be lowercase as a way of doing |
| 163 |
* a case insensitive match. |
| 164 |
*/ |
| 165 |
|
| 166 |
strncpy (protobuf, protocol, PROTOBUFSIZE - 1); |
| 167 |
protobuf[PROTOBUFSIZE-1] = '\0'; |
| 168 |
|
| 169 |
for (i = 0; i < PROTOBUFSIZE && protobuf[i] != '\0'; i++) |
| 170 |
if (isupper ((unsigned char)protobuf[i])) |
| 171 |
protobuf[i] = tolower ((unsigned char)protobuf[i]); |
| 172 |
|
| 173 |
/* Look at all of the configured protocols */ |
| 174 |
|
| 175 |
for (i = 0; i < NUMTRANS; i++) |
| 176 |
{ |
| 177 |
if (!strcmp (protobuf, Xtransports[i].transport->TransName)) |
| 178 |
return Xtransports[i].transport; |
| 179 |
} |
| 180 |
|
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
#ifndef TEST_t |
| 185 |
static |
| 186 |
#endif /* TEST_t */ |
| 187 |
int |
| 188 |
TRANS(ParseAddress) (const char *address, |
| 189 |
char **protocol, char **host, char **port) |
| 190 |
|
| 191 |
{ |
| 192 |
/* |
| 193 |
* For the font library, the address is a string formatted |
| 194 |
* as "protocol/host:port[/catalogue]". Note that the catologue |
| 195 |
* is optional. At this time, the catologue info is ignored, but |
| 196 |
* we have to parse it anyways. |
| 197 |
* |
| 198 |
* Other than fontlib, the address is a string formatted |
| 199 |
* as "protocol/host:port". |
| 200 |
* |
| 201 |
* If the protocol part is missing, then assume TCP. |
| 202 |
* If the protocol part and host part are missing, then assume local. |
| 203 |
* If a "::" is found then assume DNET. |
| 204 |
*/ |
| 205 |
|
| 206 |
char *mybuf, *tmpptr; |
| 207 |
const char *_protocol; |
| 208 |
char *_host, *_port; |
| 209 |
char hostnamebuf[256]; |
| 210 |
int _host_len; |
| 211 |
|
| 212 |
prmsg (3,"ParseAddress(%s)\n", address); |
| 213 |
|
| 214 |
/* Copy the string so it can be changed */ |
| 215 |
|
| 216 |
tmpptr = mybuf = strdup (address); |
| 217 |
|
| 218 |
/* Parse the string to get each component */ |
| 219 |
|
| 220 |
/* Get the protocol part */ |
| 221 |
|
| 222 |
_protocol = mybuf; |
| 223 |
|
| 224 |
|
| 225 |
if ( ((mybuf = strchr (mybuf,'/')) == NULL) && |
| 226 |
((mybuf = strrchr (tmpptr,':')) == NULL) ) |
| 227 |
{ |
| 228 |
/* address is in a bad format */ |
| 229 |
*protocol = NULL; |
| 230 |
*host = NULL; |
| 231 |
*port = NULL; |
| 232 |
free (tmpptr); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
if (*mybuf == ':') |
| 237 |
{ |
| 238 |
/* |
| 239 |
* If there is a hostname, then assume tcp, otherwise |
| 240 |
* it must be local. |
| 241 |
*/ |
| 242 |
if (mybuf == tmpptr) |
| 243 |
{ |
| 244 |
/* There is neither a protocol or host specified */ |
| 245 |
_protocol = "local"; |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
/* There is a hostname specified */ |
| 250 |
_protocol = "tcp"; |
| 251 |
mybuf = tmpptr; /* reset to the begining of the host ptr */ |
| 252 |
} |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
/* *mybuf == '/' */ |
| 257 |
|
| 258 |
*mybuf ++= '\0'; /* put a null at the end of the protocol */ |
| 259 |
|
| 260 |
if (strlen(_protocol) == 0) |
| 261 |
{ |
| 262 |
/* |
| 263 |
* If there is a hostname, then assume tcp, otherwise |
| 264 |
* it must be local. |
| 265 |
*/ |
| 266 |
if (*mybuf != ':') |
| 267 |
_protocol = "tcp"; |
| 268 |
else |
| 269 |
_protocol = "local"; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/* Get the host part */ |
| 274 |
|
| 275 |
_host = mybuf; |
| 276 |
|
| 277 |
if ((mybuf = strrchr (mybuf,':')) == NULL) |
| 278 |
{ |
| 279 |
*protocol = NULL; |
| 280 |
*host = NULL; |
| 281 |
*port = NULL; |
| 282 |
free (tmpptr); |
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
*mybuf ++= '\0'; |
| 287 |
|
| 288 |
_host_len = strlen(_host); |
| 289 |
if (_host_len == 0) |
| 290 |
{ |
| 291 |
TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf)); |
| 292 |
_host = hostnamebuf; |
| 293 |
} |
| 294 |
#if defined(IPv6) && defined(AF_INET6) |
| 295 |
/* hostname in IPv6 [numeric_addr]:0 form? */ |
| 296 |
else if ( (_host_len > 3) && |
| 297 |
((strcmp(_protocol, "tcp") == 0) || (strcmp(_protocol, "inet6") == 0)) |
| 298 |
&& (*_host == '[') && (*(_host + _host_len - 1) == ']') ) { |
| 299 |
struct sockaddr_in6 sin6; |
| 300 |
|
| 301 |
*(_host + _host_len - 1) = '\0'; |
| 302 |
|
| 303 |
/* Verify address is valid IPv6 numeric form */ |
| 304 |
if (inet_pton(AF_INET6, _host + 1, &sin6) == 1) { |
| 305 |
/* It is. Use it as such. */ |
| 306 |
_host++; |
| 307 |
_protocol = "inet6"; |
| 308 |
} else { |
| 309 |
/* It's not, restore it just in case some other code can use it. */ |
| 310 |
*(_host + _host_len - 1) = ']'; |
| 311 |
} |
| 312 |
} |
| 313 |
#endif |
| 314 |
|
| 315 |
|
| 316 |
/* Get the port */ |
| 317 |
|
| 318 |
_port = mybuf; |
| 319 |
|
| 320 |
#if defined(FONT_t) || defined(FS_t) |
| 321 |
/* |
| 322 |
* Is there an optional catalogue list? |
| 323 |
*/ |
| 324 |
|
| 325 |
if ((mybuf = strchr (mybuf,'/')) != NULL) |
| 326 |
*mybuf ++= '\0'; |
| 327 |
|
| 328 |
/* |
| 329 |
* The rest, if any, is the (currently unused) catalogue list. |
| 330 |
* |
| 331 |
* _catalogue = mybuf; |
| 332 |
*/ |
| 333 |
#endif |
| 334 |
|
| 335 |
#ifdef HAVE_LAUNCHD |
| 336 |
/* launchd sockets will look like 'local//tmp/launch-XgkNns/:0' */ |
| 337 |
if(address != NULL && strlen(address)>8 && (!strncmp(address,"local//",7))) { |
| 338 |
_protocol="local"; |
| 339 |
_host=""; |
| 340 |
_port=address+6; |
| 341 |
} |
| 342 |
#endif |
| 343 |
|
| 344 |
/* |
| 345 |
* Now that we have all of the components, allocate new |
| 346 |
* string space for them. |
| 347 |
*/ |
| 348 |
|
| 349 |
if ((*protocol = strdup (_protocol)) == NULL) |
| 350 |
{ |
| 351 |
/* Malloc failed */ |
| 352 |
*port = NULL; |
| 353 |
*host = NULL; |
| 354 |
*protocol = NULL; |
| 355 |
free (tmpptr); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
if ((*host = strdup (_host)) == NULL) |
| 360 |
{ |
| 361 |
/* Malloc failed */ |
| 362 |
*port = NULL; |
| 363 |
*host = NULL; |
| 364 |
free (*protocol); |
| 365 |
*protocol = NULL; |
| 366 |
free (tmpptr); |
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
if ((*port = strdup (_port)) == NULL) |
| 371 |
{ |
| 372 |
/* Malloc failed */ |
| 373 |
*port = NULL; |
| 374 |
free (*host); |
| 375 |
*host = NULL; |
| 376 |
free (*protocol); |
| 377 |
*protocol = NULL; |
| 378 |
free (tmpptr); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
free (tmpptr); |
| 383 |
|
| 384 |
return 1; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
/* |
| 389 |
* TRANS(Open) does all of the real work opening a connection. The only |
| 390 |
* funny part about this is the type parameter which is used to decide which |
| 391 |
* type of open to perform. |
| 392 |
*/ |
| 393 |
|
| 394 |
static XtransConnInfo |
| 395 |
TRANS(Open) (int type, const char *address) |
| 396 |
|
| 397 |
{ |
| 398 |
char *protocol = NULL, *host = NULL, *port = NULL; |
| 399 |
XtransConnInfo ciptr = NULL; |
| 400 |
Xtransport *thistrans; |
| 401 |
|
| 402 |
prmsg (2,"Open(%d,%s)\n", type, address); |
| 403 |
|
| 404 |
#if defined(WIN32) && defined(TCPCONN) |
| 405 |
if (TRANS(WSAStartup)()) |
| 406 |
{ |
| 407 |
prmsg (1,"Open: WSAStartup failed\n"); |
| 408 |
return NULL; |
| 409 |
} |
| 410 |
#endif |
| 411 |
|
| 412 |
/* Parse the Address */ |
| 413 |
|
| 414 |
if (TRANS(ParseAddress) (address, &protocol, &host, &port) == 0) |
| 415 |
{ |
| 416 |
prmsg (1,"Open: Unable to Parse address %s\n", address); |
| 417 |
return NULL; |
| 418 |
} |
| 419 |
|
| 420 |
/* Determine the transport type */ |
| 421 |
|
| 422 |
if ((thistrans = TRANS(SelectTransport) (protocol)) == NULL) |
| 423 |
{ |
| 424 |
prmsg (1,"Open: Unable to find transport for %s\n", |
| 425 |
protocol); |
| 426 |
|
| 427 |
free (protocol); |
| 428 |
free (host); |
| 429 |
free (port); |
| 430 |
return NULL; |
| 431 |
} |
| 432 |
|
| 433 |
/* Open the transport */ |
| 434 |
|
| 435 |
switch (type) |
| 436 |
{ |
| 437 |
case XTRANS_OPEN_COTS_CLIENT: |
| 438 |
#ifdef TRANS_CLIENT |
| 439 |
ciptr = thistrans->OpenCOTSClient(thistrans, protocol, host, port); |
| 440 |
#endif /* TRANS_CLIENT */ |
| 441 |
break; |
| 442 |
case XTRANS_OPEN_COTS_SERVER: |
| 443 |
#ifdef TRANS_SERVER |
| 444 |
ciptr = thistrans->OpenCOTSServer(thistrans, protocol, host, port); |
| 445 |
#endif /* TRANS_SERVER */ |
| 446 |
break; |
| 447 |
default: |
| 448 |
prmsg (1,"Open: Unknown Open type %d\n", type); |
| 449 |
} |
| 450 |
|
| 451 |
if (ciptr == NULL) |
| 452 |
{ |
| 453 |
if (!(thistrans->flags & TRANS_DISABLED)) |
| 454 |
{ |
| 455 |
prmsg (1,"Open: transport open failed for %s/%s:%s\n", |
| 456 |
protocol, host, port); |
| 457 |
} |
| 458 |
free (protocol); |
| 459 |
free (host); |
| 460 |
free (port); |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
|
| 464 |
ciptr->transptr = thistrans; |
| 465 |
ciptr->port = port; /* We need this for TRANS(Reopen) */ |
| 466 |
|
| 467 |
free (protocol); |
| 468 |
free (host); |
| 469 |
|
| 470 |
return ciptr; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
#ifdef TRANS_REOPEN |
| 475 |
|
| 476 |
/* |
| 477 |
* We might want to create an XtransConnInfo object based on a previously |
| 478 |
* opened connection. For example, the font server may clone itself and |
| 479 |
* pass file descriptors to the parent. |
| 480 |
*/ |
| 481 |
|
| 482 |
static XtransConnInfo |
| 483 |
TRANS(Reopen) (int type, int trans_id, int fd, const char *port) |
| 484 |
|
| 485 |
{ |
| 486 |
XtransConnInfo ciptr = NULL; |
| 487 |
Xtransport *thistrans = NULL; |
| 488 |
char *save_port; |
| 489 |
int i; |
| 490 |
|
| 491 |
prmsg (2,"Reopen(%d,%d,%s)\n", trans_id, fd, port); |
| 492 |
|
| 493 |
/* Determine the transport type */ |
| 494 |
|
| 495 |
for (i = 0; i < NUMTRANS; i++) |
| 496 |
if (Xtransports[i].transport_id == trans_id) |
| 497 |
{ |
| 498 |
thistrans = Xtransports[i].transport; |
| 499 |
break; |
| 500 |
} |
| 501 |
|
| 502 |
if (thistrans == NULL) |
| 503 |
{ |
| 504 |
prmsg (1,"Reopen: Unable to find transport id %d\n", |
| 505 |
trans_id); |
| 506 |
|
| 507 |
return NULL; |
| 508 |
} |
| 509 |
|
| 510 |
if ((save_port = strdup (port)) == NULL) |
| 511 |
{ |
| 512 |
prmsg (1,"Reopen: Unable to malloc port string\n"); |
| 513 |
|
| 514 |
return NULL; |
| 515 |
} |
| 516 |
|
| 517 |
/* Get a new XtransConnInfo object */ |
| 518 |
|
| 519 |
switch (type) |
| 520 |
{ |
| 521 |
case XTRANS_OPEN_COTS_SERVER: |
| 522 |
ciptr = thistrans->ReopenCOTSServer(thistrans, fd, port); |
| 523 |
break; |
| 524 |
default: |
| 525 |
prmsg (1,"Reopen: Bad Open type %d\n", type); |
| 526 |
} |
| 527 |
|
| 528 |
if (ciptr == NULL) |
| 529 |
{ |
| 530 |
prmsg (1,"Reopen: transport open failed\n"); |
| 531 |
free (save_port); |
| 532 |
return NULL; |
| 533 |
} |
| 534 |
|
| 535 |
ciptr->transptr = thistrans; |
| 536 |
ciptr->port = save_port; |
| 537 |
|
| 538 |
return ciptr; |
| 539 |
} |
| 540 |
|
| 541 |
#endif /* TRANS_REOPEN */ |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
/* |
| 546 |
* These are the public interfaces to this Transport interface. |
| 547 |
* These are the only functions that should have knowledge of the transport |
| 548 |
* table. |
| 549 |
*/ |
| 550 |
|
| 551 |
#ifdef TRANS_CLIENT |
| 552 |
|
| 553 |
XtransConnInfo |
| 554 |
TRANS(OpenCOTSClient) (const char *address) |
| 555 |
|
| 556 |
{ |
| 557 |
prmsg (2,"OpenCOTSClient(%s)\n", address); |
| 558 |
return TRANS(Open) (XTRANS_OPEN_COTS_CLIENT, address); |
| 559 |
} |
| 560 |
|
| 561 |
#endif /* TRANS_CLIENT */ |
| 562 |
|
| 563 |
|
| 564 |
#ifdef TRANS_SERVER |
| 565 |
|
| 566 |
XtransConnInfo |
| 567 |
TRANS(OpenCOTSServer) (const char *address) |
| 568 |
|
| 569 |
{ |
| 570 |
prmsg (2,"OpenCOTSServer(%s)\n", address); |
| 571 |
return TRANS(Open) (XTRANS_OPEN_COTS_SERVER, address); |
| 572 |
} |
| 573 |
|
| 574 |
#endif /* TRANS_SERVER */ |
| 575 |
|
| 576 |
|
| 577 |
#ifdef TRANS_REOPEN |
| 578 |
|
| 579 |
XtransConnInfo |
| 580 |
TRANS(ReopenCOTSServer) (int trans_id, int fd, const char *port) |
| 581 |
|
| 582 |
{ |
| 583 |
prmsg (2,"ReopenCOTSServer(%d, %d, %s)\n", trans_id, fd, port); |
| 584 |
return TRANS(Reopen) (XTRANS_OPEN_COTS_SERVER, trans_id, fd, port); |
| 585 |
} |
| 586 |
|
| 587 |
int |
| 588 |
TRANS(GetReopenInfo) (XtransConnInfo ciptr, |
| 589 |
int *trans_id, int *fd, char **port) |
| 590 |
|
| 591 |
{ |
| 592 |
int i; |
| 593 |
|
| 594 |
for (i = 0; i < NUMTRANS; i++) |
| 595 |
if (Xtransports[i].transport == ciptr->transptr) |
| 596 |
{ |
| 597 |
*trans_id = Xtransports[i].transport_id; |
| 598 |
*fd = ciptr->fd; |
| 599 |
|
| 600 |
if ((*port = strdup (ciptr->port)) == NULL) |
| 601 |
return 0; |
| 602 |
else |
| 603 |
return 1; |
| 604 |
} |
| 605 |
|
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
#endif /* TRANS_REOPEN */ |
| 610 |
|
| 611 |
|
| 612 |
int |
| 613 |
TRANS(SetOption) (XtransConnInfo ciptr, int option, int arg) |
| 614 |
|
| 615 |
{ |
| 616 |
int fd = ciptr->fd; |
| 617 |
int ret = 0; |
| 618 |
|
| 619 |
prmsg (2,"SetOption(%d,%d,%d)\n", fd, option, arg); |
| 620 |
|
| 621 |
/* |
| 622 |
* For now, all transport type use the same stuff for setting options. |
| 623 |
* As long as this is true, we can put the common code here. Once a more |
| 624 |
* complicated transport such as shared memory or an OSI implementation |
| 625 |
* that uses the session and application libraries is implemented, this |
| 626 |
* code may have to move to a transport dependent function. |
| 627 |
* |
| 628 |
* ret = ciptr->transptr->SetOption (ciptr, option, arg); |
| 629 |
*/ |
| 630 |
|
| 631 |
switch (option) |
| 632 |
{ |
| 633 |
case TRANS_NONBLOCKING: |
| 634 |
switch (arg) |
| 635 |
{ |
| 636 |
case 0: |
| 637 |
/* Set to blocking mode */ |
| 638 |
break; |
| 639 |
case 1: /* Set to non-blocking mode */ |
| 640 |
|
| 641 |
#if defined(O_NONBLOCK) && !defined(SCO325) |
| 642 |
ret = fcntl (fd, F_GETFL, 0); |
| 643 |
if (ret != -1) |
| 644 |
ret = fcntl (fd, F_SETFL, ret | O_NONBLOCK); |
| 645 |
#else |
| 646 |
#ifdef FIOSNBIO |
| 647 |
{ |
| 648 |
int arg; |
| 649 |
arg = 1; |
| 650 |
ret = ioctl (fd, FIOSNBIO, &arg); |
| 651 |
} |
| 652 |
#else |
| 653 |
#if defined(WIN32) |
| 654 |
{ |
| 655 |
#ifdef WIN32 |
| 656 |
u_long arg; |
| 657 |
#else |
| 658 |
int arg; |
| 659 |
#endif |
| 660 |
arg = 1; |
| 661 |
/* IBM TCP/IP understands this option too well: it causes TRANS(Read) to fail |
| 662 |
* eventually with EWOULDBLOCK */ |
| 663 |
ret = ioctl (fd, FIONBIO, &arg); |
| 664 |
} |
| 665 |
#else |
| 666 |
ret = fcntl (fd, F_GETFL, 0); |
| 667 |
#ifdef FNDELAY |
| 668 |
ret = fcntl (fd, F_SETFL, ret | FNDELAY); |
| 669 |
#else |
| 670 |
ret = fcntl (fd, F_SETFL, ret | O_NDELAY); |
| 671 |
#endif |
| 672 |
#endif /* WIN32 */ |
| 673 |
#endif /* FIOSNBIO */ |
| 674 |
#endif /* O_NONBLOCK */ |
| 675 |
break; |
| 676 |
default: |
| 677 |
/* Unknown option */ |
| 678 |
break; |
| 679 |
} |
| 680 |
break; |
| 681 |
case TRANS_CLOSEONEXEC: |
| 682 |
#ifdef F_SETFD |
| 683 |
#ifdef FD_CLOEXEC |
| 684 |
ret = fcntl (fd, F_SETFD, FD_CLOEXEC); |
| 685 |
#else |
| 686 |
ret = fcntl (fd, F_SETFD, 1); |
| 687 |
#endif /* FD_CLOEXEC */ |
| 688 |
#endif /* F_SETFD */ |
| 689 |
break; |
| 690 |
} |
| 691 |
|
| 692 |
return ret; |
| 693 |
} |
| 694 |
|
| 695 |
#ifdef TRANS_SERVER |
| 696 |
|
| 697 |
int |
| 698 |
TRANS(CreateListener) (XtransConnInfo ciptr, const char *port, unsigned int flags) |
| 699 |
|
| 700 |
{ |
| 701 |
return ciptr->transptr->CreateListener (ciptr, port, flags); |
| 702 |
} |
| 703 |
|
| 704 |
int |
| 705 |
TRANS(Received) (const char * protocol) |
| 706 |
|
| 707 |
{ |
| 708 |
Xtransport *trans; |
| 709 |
int i = 0, ret = 0; |
| 710 |
|
| 711 |
prmsg (5, "Received(%s)\n", protocol); |
| 712 |
|
| 713 |
if ((trans = TRANS(SelectTransport)(protocol)) == NULL) |
| 714 |
{ |
| 715 |
prmsg (1,"Received: unable to find transport: %s\n", |
| 716 |
protocol); |
| 717 |
|
| 718 |
return -1; |
| 719 |
} |
| 720 |
if (trans->flags & TRANS_ALIAS) { |
| 721 |
if (trans->nolisten) |
| 722 |
while (trans->nolisten[i]) { |
| 723 |
ret |= TRANS(Received)(trans->nolisten[i]); |
| 724 |
i++; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
trans->flags |= TRANS_RECEIVED; |
| 729 |
return ret; |
| 730 |
} |
| 731 |
|
| 732 |
int |
| 733 |
TRANS(NoListen) (const char * protocol) |
| 734 |
|
| 735 |
{ |
| 736 |
Xtransport *trans; |
| 737 |
int i = 0, ret = 0; |
| 738 |
|
| 739 |
if ((trans = TRANS(SelectTransport)(protocol)) == NULL) |
| 740 |
{ |
| 741 |
prmsg (1,"TransNoListen: unable to find transport: %s\n", |
| 742 |
protocol); |
| 743 |
|
| 744 |
return -1; |
| 745 |
} |
| 746 |
if (trans->flags & TRANS_ALIAS) { |
| 747 |
if (trans->nolisten) |
| 748 |
while (trans->nolisten[i]) { |
| 749 |
ret |= TRANS(NoListen)(trans->nolisten[i]); |
| 750 |
i++; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
trans->flags |= TRANS_NOLISTEN; |
| 755 |
return ret; |
| 756 |
} |
| 757 |
|
| 758 |
int |
| 759 |
TRANS(Listen) (const char * protocol) |
| 760 |
{ |
| 761 |
Xtransport *trans; |
| 762 |
int i = 0, ret = 0; |
| 763 |
|
| 764 |
if ((trans = TRANS(SelectTransport)(protocol)) == NULL) |
| 765 |
{ |
| 766 |
prmsg (1,"TransListen: unable to find transport: %s\n", |
| 767 |
protocol); |
| 768 |
|
| 769 |
return -1; |
| 770 |
} |
| 771 |
if (trans->flags & TRANS_ALIAS) { |
| 772 |
if (trans->nolisten) |
| 773 |
while (trans->nolisten[i]) { |
| 774 |
ret |= TRANS(Listen)(trans->nolisten[i]); |
| 775 |
i++; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
trans->flags &= ~TRANS_NOLISTEN; |
| 780 |
return ret; |
| 781 |
} |
| 782 |
|
| 783 |
int |
| 784 |
TRANS(IsListening) (const char * protocol) |
| 785 |
{ |
| 786 |
Xtransport *trans; |
| 787 |
|
| 788 |
if ((trans = TRANS(SelectTransport)(protocol)) == NULL) |
| 789 |
{ |
| 790 |
prmsg (1,"TransIsListening: unable to find transport: %s\n", |
| 791 |
protocol); |
| 792 |
|
| 793 |
return 0; |
| 794 |
} |
| 795 |
|
| 796 |
return !(trans->flags & TRANS_NOLISTEN); |
| 797 |
} |
| 798 |
|
| 799 |
int |
| 800 |
TRANS(ResetListener) (XtransConnInfo ciptr) |
| 801 |
|
| 802 |
{ |
| 803 |
if (ciptr->transptr->ResetListener) |
| 804 |
return ciptr->transptr->ResetListener (ciptr); |
| 805 |
else |
| 806 |
return TRANS_RESET_NOOP; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
XtransConnInfo |
| 811 |
TRANS(Accept) (XtransConnInfo ciptr, int *status) |
| 812 |
|
| 813 |
{ |
| 814 |
XtransConnInfo newciptr; |
| 815 |
|
| 816 |
prmsg (2,"Accept(%d)\n", ciptr->fd); |
| 817 |
|
| 818 |
newciptr = ciptr->transptr->Accept (ciptr, status); |
| 819 |
|
| 820 |
if (newciptr) |
| 821 |
newciptr->transptr = ciptr->transptr; |
| 822 |
|
| 823 |
return newciptr; |
| 824 |
} |
| 825 |
|
| 826 |
#endif /* TRANS_SERVER */ |
| 827 |
|
| 828 |
|
| 829 |
#ifdef TRANS_CLIENT |
| 830 |
|
| 831 |
int |
| 832 |
TRANS(Connect) (XtransConnInfo ciptr, const char *address) |
| 833 |
|
| 834 |
{ |
| 835 |
char *protocol; |
| 836 |
char *host; |
| 837 |
char *port; |
| 838 |
int ret; |
| 839 |
|
| 840 |
prmsg (2,"Connect(%d,%s)\n", ciptr->fd, address); |
| 841 |
|
| 842 |
if (TRANS(ParseAddress) (address, &protocol, &host, &port) == 0) |
| 843 |
{ |
| 844 |
prmsg (1,"Connect: Unable to Parse address %s\n", |
| 845 |
address); |
| 846 |
return -1; |
| 847 |
} |
| 848 |
|
| 849 |
#ifdef HAVE_LAUNCHD |
| 850 |
if (!host) host=strdup(""); |
| 851 |
#endif |
| 852 |
|
| 853 |
if (!port || !*port) |
| 854 |
{ |
| 855 |
prmsg (1,"Connect: Missing port specification in %s\n", |
| 856 |
address); |
| 857 |
if (protocol) free (protocol); |
| 858 |
if (host) free (host); |
| 859 |
return -1; |
| 860 |
} |
| 861 |
|
| 862 |
ret = ciptr->transptr->Connect (ciptr, host, port); |
| 863 |
|
| 864 |
if (protocol) free (protocol); |
| 865 |
if (host) free (host); |
| 866 |
if (port) free (port); |
| 867 |
|
| 868 |
return ret; |
| 869 |
} |
| 870 |
|
| 871 |
#endif /* TRANS_CLIENT */ |
| 872 |
|
| 873 |
|
| 874 |
int |
| 875 |
TRANS(BytesReadable) (XtransConnInfo ciptr, BytesReadable_t *pend) |
| 876 |
|
| 877 |
{ |
| 878 |
return ciptr->transptr->BytesReadable (ciptr, pend); |
| 879 |
} |
| 880 |
|
| 881 |
int |
| 882 |
TRANS(Read) (XtransConnInfo ciptr, char *buf, int size) |
| 883 |
|
| 884 |
{ |
| 885 |
return ciptr->transptr->Read (ciptr, buf, size); |
| 886 |
} |
| 887 |
|
| 888 |
int |
| 889 |
TRANS(Write) (XtransConnInfo ciptr, char *buf, int size) |
| 890 |
|
| 891 |
{ |
| 892 |
return ciptr->transptr->Write (ciptr, buf, size); |
| 893 |
} |
| 894 |
|
| 895 |
int |
| 896 |
TRANS(Readv) (XtransConnInfo ciptr, struct iovec *buf, int size) |
| 897 |
|
| 898 |
{ |
| 899 |
return ciptr->transptr->Readv (ciptr, buf, size); |
| 900 |
} |
| 901 |
|
| 902 |
int |
| 903 |
TRANS(Writev) (XtransConnInfo ciptr, struct iovec *buf, int size) |
| 904 |
|
| 905 |
{ |
| 906 |
return ciptr->transptr->Writev (ciptr, buf, size); |
| 907 |
} |
| 908 |
|
| 909 |
#if XTRANS_SEND_FDS |
| 910 |
int |
| 911 |
TRANS(SendFd) (XtransConnInfo ciptr, int fd, int do_close) |
| 912 |
{ |
| 913 |
return ciptr->transptr->SendFd(ciptr, fd, do_close); |
| 914 |
} |
| 915 |
|
| 916 |
int |
| 917 |
TRANS(RecvFd) (XtransConnInfo ciptr) |
| 918 |
{ |
| 919 |
return ciptr->transptr->RecvFd(ciptr); |
| 920 |
} |
| 921 |
#endif |
| 922 |
|
| 923 |
int |
| 924 |
TRANS(Disconnect) (XtransConnInfo ciptr) |
| 925 |
|
| 926 |
{ |
| 927 |
return ciptr->transptr->Disconnect (ciptr); |
| 928 |
} |
| 929 |
|
| 930 |
int |
| 931 |
TRANS(Close) (XtransConnInfo ciptr) |
| 932 |
|
| 933 |
{ |
| 934 |
int ret; |
| 935 |
|
| 936 |
prmsg (2,"Close(%d)\n", ciptr->fd); |
| 937 |
|
| 938 |
ret = ciptr->transptr->Close (ciptr); |
| 939 |
|
| 940 |
TRANS(FreeConnInfo) (ciptr); |
| 941 |
|
| 942 |
return ret; |
| 943 |
} |
| 944 |
|
| 945 |
int |
| 946 |
TRANS(CloseForCloning) (XtransConnInfo ciptr) |
| 947 |
|
| 948 |
{ |
| 949 |
int ret; |
| 950 |
|
| 951 |
prmsg (2,"CloseForCloning(%d)\n", ciptr->fd); |
| 952 |
|
| 953 |
ret = ciptr->transptr->CloseForCloning (ciptr); |
| 954 |
|
| 955 |
TRANS(FreeConnInfo) (ciptr); |
| 956 |
|
| 957 |
return ret; |
| 958 |
} |
| 959 |
|
| 960 |
int |
| 961 |
TRANS(IsLocal) (XtransConnInfo ciptr) |
| 962 |
|
| 963 |
{ |
| 964 |
return (ciptr->family == AF_UNIX); |
| 965 |
} |
| 966 |
|
| 967 |
#ifdef NX_TRANS_SOCKET |
| 968 |
/* this is needed for pre-xcb X11 as we have in NX */ |
| 969 |
int |
| 970 |
TRANS(GetMyAddr) (XtransConnInfo ciptr, int *familyp, int *addrlenp, |
| 971 |
Xtransaddr **addrp) |
| 972 |
|
| 973 |
{ |
| 974 |
prmsg (2,"GetMyAddr(%d)\n", ciptr->fd); |
| 975 |
|
| 976 |
*familyp = ciptr->family; |
| 977 |
*addrlenp = ciptr->addrlen; |
| 978 |
|
| 979 |
if ((*addrp = (Xtransaddr *) malloc (ciptr->addrlen)) == NULL) |
| 980 |
{ |
| 981 |
prmsg (1,"GetMyAddr: malloc failed\n"); |
| 982 |
return -1; |
| 983 |
} |
| 984 |
|
| 985 |
memcpy(*addrp, ciptr->addr, ciptr->addrlen); |
| 986 |
|
| 987 |
return 0; |
| 988 |
} |
| 989 |
#endif |
| 990 |
|
| 991 |
int |
| 992 |
TRANS(GetPeerAddr) (XtransConnInfo ciptr, int *familyp, int *addrlenp, |
| 993 |
Xtransaddr **addrp) |
| 994 |
|
| 995 |
{ |
| 996 |
prmsg (2,"GetPeerAddr(%d)\n", ciptr->fd); |
| 997 |
|
| 998 |
*familyp = ciptr->family; |
| 999 |
*addrlenp = ciptr->peeraddrlen; |
| 1000 |
|
| 1001 |
if ((*addrp = malloc (ciptr->peeraddrlen)) == NULL) |
| 1002 |
{ |
| 1003 |
prmsg (1,"GetPeerAddr: malloc failed\n"); |
| 1004 |
return -1; |
| 1005 |
} |
| 1006 |
|
| 1007 |
memcpy(*addrp, ciptr->peeraddr, ciptr->peeraddrlen); |
| 1008 |
|
| 1009 |
return 0; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
int |
| 1014 |
TRANS(GetConnectionNumber) (XtransConnInfo ciptr) |
| 1015 |
|
| 1016 |
{ |
| 1017 |
return ciptr->fd; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
/* |
| 1022 |
* These functions are really utility functions, but they require knowledge |
| 1023 |
* of the internal data structures, so they have to be part of the Transport |
| 1024 |
* Independant API. |
| 1025 |
*/ |
| 1026 |
|
| 1027 |
#ifdef TRANS_SERVER |
| 1028 |
|
| 1029 |
static int |
| 1030 |
complete_network_count (void) |
| 1031 |
|
| 1032 |
{ |
| 1033 |
int count = 0; |
| 1034 |
int found_local = 0; |
| 1035 |
int i; |
| 1036 |
|
| 1037 |
/* |
| 1038 |
* For a complete network, we only need one LOCALCONN transport to work |
| 1039 |
*/ |
| 1040 |
|
| 1041 |
for (i = 0; i < NUMTRANS; i++) |
| 1042 |
{ |
| 1043 |
if (Xtransports[i].transport->flags & TRANS_ALIAS |
| 1044 |
|| Xtransports[i].transport->flags & TRANS_NOLISTEN) |
| 1045 |
continue; |
| 1046 |
|
| 1047 |
if (Xtransports[i].transport->flags & TRANS_LOCAL) |
| 1048 |
found_local = 1; |
| 1049 |
else |
| 1050 |
count++; |
| 1051 |
} |
| 1052 |
|
| 1053 |
return (count + found_local); |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
static int |
| 1058 |
receive_listening_fds(const char* port, XtransConnInfo* temp_ciptrs, |
| 1059 |
int* count_ret) |
| 1060 |
|
| 1061 |
{ |
| 1062 |
#ifdef HAVE_SYSTEMD_DAEMON |
| 1063 |
XtransConnInfo ciptr; |
| 1064 |
int i, systemd_listen_fds; |
| 1065 |
|
| 1066 |
systemd_listen_fds = sd_listen_fds(1); |
| 1067 |
if (systemd_listen_fds < 0) |
| 1068 |
{ |
| 1069 |
prmsg (1, "receive_listening_fds: sd_listen_fds error: %s\n", |
| 1070 |
strerror(-systemd_listen_fds)); |
| 1071 |
return -1; |
| 1072 |
} |
| 1073 |
|
| 1074 |
for (i = 0; i < systemd_listen_fds && *count_ret < NUMTRANS; i++) |
| 1075 |
{ |
| 1076 |
struct sockaddr_storage a; |
| 1077 |
int ti; |
| 1078 |
const char* tn; |
| 1079 |
socklen_t al; |
| 1080 |
|
| 1081 |
al = sizeof(a); |
| 1082 |
if (getsockname(i + SD_LISTEN_FDS_START, (struct sockaddr*)&a, &al) < 0) { |
| 1083 |
prmsg (1, "receive_listening_fds: getsockname error: %s\n", |
| 1084 |
strerror(errno)); |
| 1085 |
return -1; |
| 1086 |
} |
| 1087 |
|
| 1088 |
switch (a.ss_family) |
| 1089 |
{ |
| 1090 |
case AF_UNIX: |
| 1091 |
ti = TRANS_SOCKET_UNIX_INDEX; |
| 1092 |
if (*((struct sockaddr_un*)&a)->sun_path == '\0' && |
| 1093 |
al > sizeof(sa_family_t)) |
| 1094 |
tn = "local"; |
| 1095 |
else |
| 1096 |
tn = "unix"; |
| 1097 |
break; |
| 1098 |
case AF_INET: |
| 1099 |
ti = TRANS_SOCKET_INET_INDEX; |
| 1100 |
tn = "inet"; |
| 1101 |
break; |
| 1102 |
#if defined(IPv6) && defined(AF_INET6) |
| 1103 |
case AF_INET6: |
| 1104 |
ti = TRANS_SOCKET_INET6_INDEX; |
| 1105 |
tn = "inet6"; |
| 1106 |
break; |
| 1107 |
#endif /* IPv6 */ |
| 1108 |
default: |
| 1109 |
prmsg (1, "receive_listening_fds:" |
| 1110 |
"Got unknown socket address family\n"); |
| 1111 |
return -1; |
| 1112 |
} |
| 1113 |
|
| 1114 |
ciptr = TRANS(ReopenCOTSServer)(ti, i + SD_LISTEN_FDS_START, port); |
| 1115 |
if (!ciptr) |
| 1116 |
{ |
| 1117 |
prmsg (1, "receive_listening_fds:" |
| 1118 |
"Got NULL while trying to reopen socket received from systemd.\n"); |
| 1119 |
return -1; |
| 1120 |
} |
| 1121 |
|
| 1122 |
prmsg (5, "receive_listening_fds: received listener for %s, %d\n", |
| 1123 |
tn, ciptr->fd); |
| 1124 |
temp_ciptrs[(*count_ret)++] = ciptr; |
| 1125 |
TRANS(Received)(tn); |
| 1126 |
} |
| 1127 |
#endif /* HAVE_SYSTEMD_DAEMON */ |
| 1128 |
return 0; |
| 1129 |
} |
| 1130 |
|
| 1131 |
#ifdef XQUARTZ_EXPORTS_LAUNCHD_FD |
| 1132 |
extern int xquartz_launchd_fd; |
| 1133 |
#endif |
| 1134 |
|
| 1135 |
int |
| 1136 |
TRANS(MakeAllCOTSServerListeners) (const char *port, int *partial, |
| 1137 |
int *count_ret, XtransConnInfo **ciptrs_ret) |
| 1138 |
|
| 1139 |
{ |
| 1140 |
char buffer[256]; /* ??? What size ?? */ |
| 1141 |
XtransConnInfo ciptr, temp_ciptrs[NUMTRANS]; |
| 1142 |
int status, i, j; |
| 1143 |
|
| 1144 |
#if defined(IPv6) && defined(AF_INET6) |
| 1145 |
int ipv6_succ = 0; |
| 1146 |
#endif |
| 1147 |
prmsg (2,"MakeAllCOTSServerListeners(%s,%p)\n", |
| 1148 |
port ? port : "NULL", (void *) ciptrs_ret); |
| 1149 |
|
| 1150 |
*count_ret = 0; |
| 1151 |
|
| 1152 |
#ifdef XQUARTZ_EXPORTS_LAUNCHD_FD |
| 1153 |
fprintf(stderr, "Launchd socket fd: %d\n", xquartz_launchd_fd); |
| 1154 |
if(xquartz_launchd_fd != -1) { |
| 1155 |
if((ciptr = TRANS(ReopenCOTSServer(TRANS_SOCKET_LOCAL_INDEX, |
| 1156 |
xquartz_launchd_fd, getenv("DISPLAY"))))==NULL) |
| 1157 |
fprintf(stderr,"Got NULL while trying to Reopen launchd port\n"); |
| 1158 |
else |
| 1159 |
temp_ciptrs[(*count_ret)++] = ciptr; |
| 1160 |
} |
| 1161 |
#endif |
| 1162 |
|
| 1163 |
if (receive_listening_fds(port, temp_ciptrs, count_ret) < 0) |
| 1164 |
return -1; |
| 1165 |
|
| 1166 |
for (i = 0; i < NUMTRANS; i++) |
| 1167 |
{ |
| 1168 |
Xtransport *trans = Xtransports[i].transport; |
| 1169 |
unsigned int flags = 0; |
| 1170 |
|
| 1171 |
if (trans->flags&TRANS_ALIAS || trans->flags&TRANS_NOLISTEN || |
| 1172 |
trans->flags&TRANS_RECEIVED) |
| 1173 |
continue; |
| 1174 |
|
| 1175 |
snprintf(buffer, sizeof(buffer), "%s/:%s", |
| 1176 |
trans->TransName, port ? port : ""); |
| 1177 |
|
| 1178 |
prmsg (5,"MakeAllCOTSServerListeners: opening %s\n", |
| 1179 |
buffer); |
| 1180 |
|
| 1181 |
if ((ciptr = TRANS(OpenCOTSServer(buffer))) == NULL) |
| 1182 |
{ |
| 1183 |
if (trans->flags & TRANS_DISABLED) |
| 1184 |
continue; |
| 1185 |
|
| 1186 |
prmsg (1, |
| 1187 |
"MakeAllCOTSServerListeners: failed to open listener for %s\n", |
| 1188 |
trans->TransName); |
| 1189 |
continue; |
| 1190 |
} |
| 1191 |
#if defined(IPv6) && defined(AF_INET6) |
| 1192 |
if ((Xtransports[i].transport_id == TRANS_SOCKET_INET_INDEX |
| 1193 |
&& ipv6_succ)) |
| 1194 |
flags |= ADDR_IN_USE_ALLOWED; |
| 1195 |
#endif |
| 1196 |
|
| 1197 |
if ((status = TRANS(CreateListener (ciptr, port, flags))) < 0) |
| 1198 |
{ |
| 1199 |
if (status == TRANS_ADDR_IN_USE) |
| 1200 |
{ |
| 1201 |
/* |
| 1202 |
* We failed to bind to the specified address because the |
| 1203 |
* address is in use. It must be that a server is already |
| 1204 |
* running at this address, and this function should fail. |
| 1205 |
*/ |
| 1206 |
|
| 1207 |
prmsg (1, |
| 1208 |
"MakeAllCOTSServerListeners: server already running\n"); |
| 1209 |
|
| 1210 |
for (j = 0; j < *count_ret; j++) |
| 1211 |
TRANS(Close) (temp_ciptrs[j]); |
| 1212 |
|
| 1213 |
*count_ret = 0; |
| 1214 |
*ciptrs_ret = NULL; |
| 1215 |
*partial = 0; |
| 1216 |
return -1; |
| 1217 |
} |
| 1218 |
else |
| 1219 |
{ |
| 1220 |
prmsg (1, |
| 1221 |
"MakeAllCOTSServerListeners: failed to create listener for %s\n", |
| 1222 |
trans->TransName); |
| 1223 |
|
| 1224 |
continue; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
#if defined(IPv6) && defined(AF_INET6) |
| 1229 |
if (Xtransports[i].transport_id == TRANS_SOCKET_INET6_INDEX) |
| 1230 |
ipv6_succ = 1; |
| 1231 |
#endif |
| 1232 |
|
| 1233 |
prmsg (5, |
| 1234 |
"MakeAllCOTSServerListeners: opened listener for %s, %d\n", |
| 1235 |
trans->TransName, ciptr->fd); |
| 1236 |
|
| 1237 |
temp_ciptrs[*count_ret] = ciptr; |
| 1238 |
(*count_ret)++; |
| 1239 |
} |
| 1240 |
|
| 1241 |
*partial = (*count_ret < complete_network_count()); |
| 1242 |
|
| 1243 |
prmsg (5, |
| 1244 |
"MakeAllCOTSServerListeners: partial=%d, actual=%d, complete=%d \n", |
| 1245 |
*partial, *count_ret, complete_network_count()); |
| 1246 |
|
| 1247 |
if (*count_ret > 0) |
| 1248 |
{ |
| 1249 |
if ((*ciptrs_ret = malloc ( |
| 1250 |
*count_ret * sizeof (XtransConnInfo))) == NULL) |
| 1251 |
{ |
| 1252 |
return -1; |
| 1253 |
} |
| 1254 |
|
| 1255 |
for (i = 0; i < *count_ret; i++) |
| 1256 |
{ |
| 1257 |
(*ciptrs_ret)[i] = temp_ciptrs[i]; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
else |
| 1261 |
*ciptrs_ret = NULL; |
| 1262 |
|
| 1263 |
return 0; |
| 1264 |
} |
| 1265 |
|
| 1266 |
#endif /* TRANS_SERVER */ |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
/* |
| 1271 |
* These routines are not part of the X Transport Interface, but they |
| 1272 |
* may be used by it. |
| 1273 |
*/ |
| 1274 |
|
| 1275 |
|
| 1276 |
#ifdef WIN32 |
| 1277 |
|
| 1278 |
/* |
| 1279 |
* emulate readv |
| 1280 |
*/ |
| 1281 |
|
| 1282 |
static int TRANS(ReadV) (XtransConnInfo ciptr, struct iovec *iov, int iovcnt) |
| 1283 |
|
| 1284 |
{ |
| 1285 |
int i, len, total; |
| 1286 |
char *base; |
| 1287 |
|
| 1288 |
ESET(0); |
| 1289 |
for (i = 0, total = 0; i < iovcnt; i++, iov++) { |
| 1290 |
len = iov->iov_len; |
| 1291 |
base = iov->iov_base; |
| 1292 |
while (len > 0) { |
| 1293 |
register int nbytes; |
| 1294 |
nbytes = TRANS(Read) (ciptr, base, len); |
| 1295 |
if (nbytes < 0 && total == 0) return -1; |
| 1296 |
if (nbytes <= 0) return total; |
| 1297 |
ESET(0); |
| 1298 |
len -= nbytes; |
| 1299 |
total += nbytes; |
| 1300 |
base += nbytes; |
| 1301 |
} |
| 1302 |
} |
| 1303 |
return total; |
| 1304 |
} |
| 1305 |
|
| 1306 |
|
| 1307 |
/* |
| 1308 |
* emulate writev |
| 1309 |
*/ |
| 1310 |
|
| 1311 |
static int TRANS(WriteV) (XtransConnInfo ciptr, struct iovec *iov, int iovcnt) |
| 1312 |
|
| 1313 |
{ |
| 1314 |
int i, len, total; |
| 1315 |
char *base; |
| 1316 |
|
| 1317 |
ESET(0); |
| 1318 |
for (i = 0, total = 0; i < iovcnt; i++, iov++) { |
| 1319 |
len = iov->iov_len; |
| 1320 |
base = iov->iov_base; |
| 1321 |
while (len > 0) { |
| 1322 |
register int nbytes; |
| 1323 |
nbytes = TRANS(Write) (ciptr, base, len); |
| 1324 |
if (nbytes < 0 && total == 0) return -1; |
| 1325 |
if (nbytes <= 0) return total; |
| 1326 |
ESET(0); |
| 1327 |
len -= nbytes; |
| 1328 |
total += nbytes; |
| 1329 |
base += nbytes; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
return total; |
| 1333 |
} |
| 1334 |
|
| 1335 |
#endif /* WIN32 */ |
| 1336 |
|
| 1337 |
|
| 1338 |
#if defined(_POSIX_SOURCE) || defined(USG) || defined(SVR4) || defined(__SVR4) || defined(__SCO__) |
| 1339 |
#ifndef NEED_UTSNAME |
| 1340 |
#define NEED_UTSNAME |
| 1341 |
#endif |
| 1342 |
#include <sys/utsname.h> |
| 1343 |
#endif |
| 1344 |
|
| 1345 |
/* |
| 1346 |
* TRANS(GetHostname) - similar to gethostname but allows special processing. |
| 1347 |
*/ |
| 1348 |
|
| 1349 |
int TRANS(GetHostname) (char *buf, int maxlen) |
| 1350 |
|
| 1351 |
{ |
| 1352 |
int len; |
| 1353 |
|
| 1354 |
#ifdef NEED_UTSNAME |
| 1355 |
struct utsname name; |
| 1356 |
|
| 1357 |
uname (&name); |
| 1358 |
len = strlen (name.nodename); |
| 1359 |
if (len >= maxlen) len = maxlen - 1; |
| 1360 |
strncpy (buf, name.nodename, len); |
| 1361 |
buf[len] = '\0'; |
| 1362 |
#else |
| 1363 |
buf[0] = '\0'; |
| 1364 |
(void) gethostname (buf, maxlen); |
| 1365 |
buf [maxlen - 1] = '\0'; |
| 1366 |
len = strlen(buf); |
| 1367 |
#endif /* NEED_UTSNAME */ |
| 1368 |
return len; |
| 1369 |
} |