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