chooser.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* *
  24. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  25. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  26. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  27. * (c) Copyright 1993, 1994 Novell, Inc. *
  28. */
  29. /*
  30. * $TOG: chooser.c /main/8 1998/03/04 19:26:30 mgreess $
  31. *
  32. Copyright (c) 1990 X Consortium
  33. Permission is hereby granted, free of charge, to any person obtaining a copy
  34. of this software and associated documentation files (the "Software"), to deal
  35. in the Software without restriction, including without limitation the rights
  36. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  37. copies of the Software, and to permit persons to whom the Software is
  38. furnished to do so, subject to the following conditions:
  39. The above copyright notice and this permission notice shall be included in
  40. all copies or substantial portions of the Software.
  41. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  42. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  43. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  44. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  45. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  46. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  47. Except as contained in this notice, the name of the X Consortium shall not be
  48. used in advertising or otherwise to promote the sale, use or other dealings
  49. in this Software without prior written authorization from the X Consortium.
  50. *
  51. * Author: Keith Packard, MIT X Consortium
  52. */
  53. /*
  54. * Chooser - display a menu of names and let the user select one
  55. */
  56. /*
  57. * Layout:
  58. *
  59. * +--------------------------------------------------+
  60. * | +------------------+ |
  61. * | | Label | |
  62. * | +------------------+ |
  63. * | +-+--------------+ |
  64. * | |^| name-1 | |
  65. * | ||| name-2 | |
  66. * | |v| name-3 | |
  67. * | | | name-4 | |
  68. * | | | name-5 | |
  69. * | | | name-6 | |
  70. * | +----------------+ |
  71. * | cancel accept ping |
  72. * +--------------------------------------------------+
  73. */
  74. #include <X11/Intrinsic.h>
  75. #include <X11/StringDefs.h>
  76. #include <X11/Xatom.h>
  77. #include <Xm/Xm.h>
  78. #include <Xm/DragC.h>
  79. #include <Xm/List.h>
  80. #define CHOOSER
  81. #include "dm.h"
  82. #include "vg.h"
  83. #include "vgmsg.h"
  84. #include <X11/Xdmcp.h>
  85. #include <sys/types.h>
  86. #include <stdio.h>
  87. #include <ctype.h>
  88. #include <locale.h>
  89. #ifdef SVR4
  90. #include <sys/sockio.h>
  91. #endif
  92. #include <sys/socket.h>
  93. #include <netinet/in.h>
  94. #include <sys/ioctl.h>
  95. #include <arpa/inet.h>
  96. /*
  97. * From Xm/XmStringI.h
  98. */
  99. extern XtPointer _XmStringUngenerate (
  100. XmString string,
  101. XmStringTag tag,
  102. XmTextType tag_type,
  103. XmTextType output_type);
  104. #define BROADCAST_HOSTNAME "BROADCAST"
  105. #ifndef ishexdigit
  106. #define ishexdigit(c) (isdigit(c) || 'a' <= (c) && (c) <= 'f')
  107. #endif
  108. #ifdef __convex__
  109. # include <sync/queue.h>
  110. # include <sync/sema.h>
  111. #endif
  112. #include <net/if.h>
  113. #include <netdb.h>
  114. Widget toplevel, label, viewport, paned, list, box, cancel, acceptit, ping;
  115. extern Widget chooser_list;
  116. extern int orig_argc;
  117. extern char **orig_argv;
  118. extern int amChooser;
  119. extern void MakeOptionsProc();
  120. static Arg chooserArgs[25]; /** Hopefully enough args **/
  121. static void CvtStringToARRAY8();
  122. static struct _app_resources {
  123. ARRAY8Ptr xdmAddress;
  124. ARRAY8Ptr clientAddress;
  125. int connectionType;
  126. } app_resources;
  127. static int FromHex (char *s, char *d, int len);
  128. #define offset(field) XtOffsetOf(struct _app_resources, field)
  129. #define XtRARRAY8 "ARRAY8"
  130. static XtResource resources[] = {
  131. {"xdmAddress", "XdmAddress", XtRARRAY8, sizeof (ARRAY8Ptr),
  132. offset (xdmAddress), XtRString, NULL },
  133. {"clientAddress", "ClientAddress", XtRARRAY8, sizeof (ARRAY8Ptr),
  134. offset (clientAddress), XtRString, NULL },
  135. {"connectionType", "ConnectionType", XtRInt, sizeof (int),
  136. offset (connectionType), XtRImmediate, (XtPointer) 0 }
  137. };
  138. #undef offset
  139. static XrmOptionDescRec options[] = {
  140. "-xdmaddress", "*xdmAddress", XrmoptionSepArg, NULL,
  141. "-clientaddress", "*clientAddress", XrmoptionSepArg, NULL,
  142. "-connectionType", "*connectionType", XrmoptionSepArg, NULL,
  143. };
  144. typedef struct _hostAddr {
  145. struct _hostAddr *next;
  146. struct sockaddr *addr;
  147. int addrlen;
  148. xdmOpCode type;
  149. } HostAddr;
  150. static HostAddr *hostAddrdb;
  151. typedef struct _hostName {
  152. struct _hostName *next;
  153. char *fullname;
  154. int willing;
  155. ARRAY8 hostname, status;
  156. CARD16 connectionType;
  157. ARRAY8 hostaddr;
  158. } HostName;
  159. static HostName *hostNamedb;
  160. static int socketFD;
  161. static int pingTry;
  162. #define PING_INTERVAL 2000
  163. #define TRIES 3
  164. static XdmcpBuffer directBuffer, broadcastBuffer;
  165. static XdmcpBuffer buffer;
  166. // dtchooser.c
  167. void MakeBackground(void);
  168. void MakeButtons(void);
  169. void MakeChooser(void);
  170. /* ARGSUSED */
  171. static void
  172. PingHosts (XtPointer closure, XtIntervalId *id)
  173. {
  174. HostAddr *hosts;
  175. for (hosts = hostAddrdb; hosts; hosts = hosts->next)
  176. {
  177. if (hosts->type == QUERY)
  178. XdmcpFlush (socketFD, &directBuffer, (XdmcpNetaddr) hosts->addr, hosts->addrlen);
  179. else
  180. XdmcpFlush (socketFD, &broadcastBuffer, (XdmcpNetaddr) hosts->addr, hosts->addrlen);
  181. }
  182. if (++pingTry < TRIES)
  183. XtAddTimeOut (PING_INTERVAL, PingHosts, (XtPointer) 0);
  184. }
  185. char **NameTable;
  186. int NameTableSize;
  187. #if defined(__STDC__)
  188. static int
  189. HostnameCompare (const void *a, const void *b)
  190. #else
  191. static int
  192. HostnameCompare (char *a, char *b)
  193. #endif
  194. {
  195. return strcmp (*(char **)a, *(char **)b);
  196. }
  197. static void
  198. RebuildTable (int size)
  199. {
  200. char **newTable = 0;
  201. HostName *names;
  202. int i;
  203. XmStringTable newStringTable;
  204. Arg listArgs[10];
  205. int numArgs;
  206. if (size)
  207. {
  208. newTable = (char **) malloc (size * sizeof (char *));
  209. if (!newTable)
  210. return;
  211. for (names = hostNamedb, i = 0; names; names = names->next, i++)
  212. newTable[i] = names->fullname;
  213. qsort (newTable, size, sizeof (char *), HostnameCompare);
  214. }
  215. else {
  216. XmListDeleteAllItems(chooser_list);
  217. if (NameTable)
  218. free((char *)NameTable);
  219. NameTableSize = 0;
  220. return;
  221. }
  222. /***********************************/
  223. /** Reload the data in the XmList **/
  224. /***********************************/
  225. newStringTable = (XmStringTable)malloc(size * sizeof(XmString));
  226. if (!newStringTable) {
  227. free((char *)newTable);
  228. return;
  229. }
  230. for (i = 0; i < size; i++) {
  231. newStringTable[i] = XmStringCreateLocalized(newTable[i]);
  232. }
  233. numArgs = 0;
  234. XtSetArg(listArgs[numArgs], XmNitemCount, size); numArgs++;
  235. XtSetArg(listArgs[numArgs], XmNitems, newStringTable); numArgs++;
  236. XtSetValues(chooser_list, listArgs, numArgs);
  237. free((char *) newStringTable);
  238. if (NameTable)
  239. free ((char *) NameTable);
  240. NameTable = newTable;
  241. NameTableSize = size;
  242. }
  243. static void
  244. RebuildTableAdd (int size)
  245. {
  246. char **newTable = 0;
  247. HostName *names;
  248. int i;
  249. XmString tempString;
  250. int position;
  251. if (size)
  252. {
  253. newTable = (char **) malloc (size * sizeof (char *));
  254. if (!newTable)
  255. return;
  256. for (names = hostNamedb, i = 0; names; names = names->next, i++)
  257. newTable[i] = names->fullname;
  258. qsort (newTable, size, sizeof (char *), HostnameCompare);
  259. }
  260. for (i = 0; i < size; i++) {
  261. tempString = XmStringCreateLocalized(newTable[i]);
  262. if ((position = XmListItemPos(chooser_list, tempString)) == 0) {
  263. /****************************/
  264. /** need to add a new host **/
  265. /****************************/
  266. XmListAddItemUnselected(chooser_list, tempString, (i+1));
  267. XmStringFree(tempString);
  268. break;
  269. }
  270. XmStringFree(tempString);
  271. }
  272. if (NameTable)
  273. free ((char *) NameTable);
  274. NameTable = newTable;
  275. NameTableSize = size;
  276. }
  277. static int
  278. AddHostname (ARRAY8Ptr hostname, ARRAY8Ptr status, struct sockaddr *addr, int willing)
  279. {
  280. HostName *new, **names, *name;
  281. ARRAY8 hostAddr;
  282. CARD16 connectionType;
  283. int fulllen;
  284. int update = 0;
  285. switch (addr->sa_family)
  286. {
  287. case AF_INET:
  288. hostAddr.data = (CARD8 *) &((struct sockaddr_in *) addr)->sin_addr;
  289. hostAddr.length = 4;
  290. connectionType = FamilyInternet;
  291. break;
  292. default:
  293. hostAddr.data = (CARD8 *) "";
  294. hostAddr.length = 0;
  295. connectionType = FamilyLocal;
  296. break;
  297. }
  298. for (names = &hostNamedb; *names; names = & (*names)->next)
  299. {
  300. name = *names;
  301. if (connectionType == name->connectionType &&
  302. XdmcpARRAY8Equal (&hostAddr, &name->hostaddr))
  303. {
  304. if (XdmcpARRAY8Equal (status, &name->status))
  305. {
  306. return 0;
  307. }
  308. update = 1;
  309. break;
  310. }
  311. }
  312. if (!*names)
  313. {
  314. new = (HostName *) malloc (sizeof (HostName));
  315. if (!new)
  316. return 0;
  317. if (hostname->length)
  318. {
  319. switch (addr->sa_family)
  320. {
  321. case AF_INET:
  322. {
  323. struct hostent *hostent;
  324. char *host;
  325. hostent = gethostbyaddr ((char *)hostAddr.data, hostAddr.length, AF_INET);
  326. if (hostent)
  327. {
  328. XdmcpDisposeARRAY8 (hostname);
  329. host = hostent->h_name;
  330. XdmcpAllocARRAY8 (hostname, strlen (host));
  331. memmove( hostname->data, host, hostname->length);
  332. }
  333. }
  334. }
  335. }
  336. if (!XdmcpAllocARRAY8 (&new->hostaddr, hostAddr.length))
  337. {
  338. free ((char *) new);
  339. return 0;
  340. }
  341. memmove( new->hostaddr.data, hostAddr.data, hostAddr.length);
  342. new->connectionType = connectionType;
  343. new->hostname = *hostname;
  344. *names = new;
  345. new->next = 0;
  346. NameTableSize++;
  347. }
  348. else
  349. {
  350. new = *names;
  351. free (new->fullname);
  352. XdmcpDisposeARRAY8 (&new->status);
  353. XdmcpDisposeARRAY8 (hostname);
  354. }
  355. new->willing = willing;
  356. new->status = *status;
  357. hostname = &new->hostname;
  358. fulllen = hostname->length;
  359. if (fulllen < 30)
  360. fulllen = 30;
  361. new->fullname = malloc (fulllen + status->length + 10);
  362. if (!new->fullname)
  363. {
  364. new->fullname = "Unknown";
  365. }
  366. else
  367. {
  368. sprintf (new->fullname, "%-30.*s %*.*s",
  369. hostname->length, hostname->data,
  370. status->length, status->length, status->data);
  371. }
  372. if (update)
  373. RebuildTable (NameTableSize);
  374. else
  375. RebuildTableAdd (NameTableSize);
  376. return 1;
  377. }
  378. static void
  379. DisposeHostname (HostName *host)
  380. {
  381. XdmcpDisposeARRAY8 (&host->hostname);
  382. XdmcpDisposeARRAY8 (&host->hostaddr);
  383. XdmcpDisposeARRAY8 (&host->status);
  384. free ((char *) host->fullname);
  385. free ((char *) host);
  386. }
  387. static int
  388. RemoveHostname (HostName *host)
  389. {
  390. HostName **prev, *hosts;
  391. prev = &hostNamedb;;
  392. for (hosts = hostNamedb; hosts; hosts = hosts->next)
  393. {
  394. if (hosts == host)
  395. break;
  396. prev = &hosts->next;
  397. }
  398. if (!hosts)
  399. return 0;
  400. *prev = host->next;
  401. DisposeHostname (host);
  402. NameTableSize--;
  403. RebuildTable (NameTableSize);
  404. return 1;
  405. }
  406. static void
  407. EmptyHostnames (void)
  408. {
  409. HostName *hosts, *next;
  410. for (hosts = hostNamedb; hosts; hosts = next)
  411. {
  412. next = hosts->next;
  413. DisposeHostname (hosts);
  414. }
  415. NameTableSize = 0;
  416. hostNamedb = 0;
  417. RebuildTable (NameTableSize);
  418. }
  419. /* ARGSUSED */
  420. static void
  421. ReceivePacket (XtPointer closure, int *source, XtInputId *id)
  422. {
  423. XdmcpHeader header;
  424. ARRAY8 authenticationName;
  425. ARRAY8 hostname;
  426. ARRAY8 status;
  427. int saveHostname = 0;
  428. struct sockaddr addr;
  429. int addrlen;
  430. addrlen = sizeof (addr);
  431. if (!XdmcpFill (socketFD, &buffer, (XdmcpNetaddr) &addr, &addrlen))
  432. return;
  433. if (!XdmcpReadHeader (&buffer, &header))
  434. return;
  435. if (header.version != XDM_PROTOCOL_VERSION)
  436. return;
  437. hostname.data = 0;
  438. status.data = 0;
  439. authenticationName.data = 0;
  440. switch (header.opcode) {
  441. case WILLING:
  442. if (XdmcpReadARRAY8 (&buffer, &authenticationName) &&
  443. XdmcpReadARRAY8 (&buffer, &hostname) &&
  444. XdmcpReadARRAY8 (&buffer, &status))
  445. {
  446. if (header.length == 6 + authenticationName.length +
  447. hostname.length + status.length)
  448. {
  449. if (AddHostname (&hostname, &status, &addr, header.opcode == (int) WILLING))
  450. saveHostname = 1;
  451. }
  452. }
  453. XdmcpDisposeARRAY8 (&authenticationName);
  454. break;
  455. case UNWILLING:
  456. if (XdmcpReadARRAY8 (&buffer, &hostname) &&
  457. XdmcpReadARRAY8 (&buffer, &status))
  458. {
  459. if (header.length == 4 + hostname.length + status.length)
  460. {
  461. if (AddHostname (&hostname, &status, &addr, header.opcode == (int) WILLING))
  462. saveHostname = 1;
  463. }
  464. }
  465. break;
  466. default:
  467. break;
  468. }
  469. if (!saveHostname)
  470. {
  471. XdmcpDisposeARRAY8 (&hostname);
  472. XdmcpDisposeARRAY8 (&status);
  473. }
  474. }
  475. int
  476. RegisterHostaddr (struct sockaddr *addr, int len, xdmOpCode type)
  477. {
  478. HostAddr *host, **prev;
  479. host = (HostAddr *) malloc (sizeof (HostAddr));
  480. if (!host)
  481. return 0;
  482. host->addr = (struct sockaddr *) malloc (len);
  483. if (!host->addr)
  484. {
  485. free ((char *) host);
  486. return 0;
  487. }
  488. memmove( (char *) host->addr, (char *) addr, len);
  489. host->addrlen = len;
  490. host->type = type;
  491. for (prev = &hostAddrdb; *prev; prev = &(*prev)->next)
  492. ;
  493. *prev = host;
  494. host->next = NULL;
  495. return 1;
  496. }
  497. /*
  498. * Register the address for this host.
  499. * Called with each of the names on the command line.
  500. * The special name "BROADCAST" looks up all the broadcast
  501. * addresses on the local host.
  502. */
  503. int
  504. RegisterHostname (char *name)
  505. {
  506. struct hostent *hostent;
  507. struct sockaddr_in in_addr;
  508. struct ifconf ifc;
  509. struct ifreq *ifr;
  510. struct sockaddr broad_addr;
  511. char buf[2048];
  512. int n;
  513. if (!strcmp (name, BROADCAST_HOSTNAME))
  514. {
  515. ifc.ifc_len = sizeof (buf);
  516. ifc.ifc_buf = buf;
  517. if (ioctl (socketFD, (int) SIOCGIFCONF, (char *) &ifc) < 0)
  518. return 0;
  519. for (ifr = ifc.ifc_req
  520. #if defined(__NetBSD__)
  521. ; (char *)ifr < ifc.ifc_buf + ifc.ifc_len;
  522. ifr = (struct ifreq *)((char *)ifr + sizeof (struct ifreq) +
  523. (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr) ?
  524. ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr) : 0))
  525. #else
  526. , n = ifc.ifc_len / sizeof (struct ifreq); --n >= 0; ifr++
  527. #endif
  528. )
  529. {
  530. if (ifr->ifr_addr.sa_family != AF_INET)
  531. continue;
  532. broad_addr = ifr->ifr_addr;
  533. ((struct sockaddr_in *) &broad_addr)->sin_addr.s_addr =
  534. htonl (INADDR_BROADCAST);
  535. #ifdef SIOCGIFBRDADDR
  536. {
  537. struct ifreq broad_req;
  538. broad_req = *ifr;
  539. if (ioctl (socketFD, SIOCGIFFLAGS, (char *) &broad_req) != -1 &&
  540. (broad_req.ifr_flags & IFF_BROADCAST) &&
  541. (broad_req.ifr_flags & IFF_UP)
  542. )
  543. {
  544. broad_req = *ifr;
  545. if (ioctl (socketFD, SIOCGIFBRDADDR, &broad_req) != -1)
  546. broad_addr = broad_req.ifr_addr;
  547. else
  548. continue;
  549. }
  550. else
  551. continue;
  552. }
  553. #endif
  554. in_addr = *((struct sockaddr_in *) &broad_addr);
  555. in_addr.sin_port = htons (XDM_UDP_PORT);
  556. #ifdef BSD44SOCKETS
  557. in_addr.sin_len = sizeof(in_addr);
  558. #endif
  559. RegisterHostaddr ((struct sockaddr *)&in_addr, sizeof (in_addr),
  560. BROADCAST_QUERY);
  561. }
  562. }
  563. else
  564. {
  565. /* address as hex string, e.g., "12180022" (depreciated) */
  566. if (strlen(name) == 8 &&
  567. FromHex(name, (char *)&in_addr.sin_addr, strlen(name)) == 0)
  568. {
  569. in_addr.sin_family = AF_INET;
  570. }
  571. /* Per RFC 1123, check first for IP address in dotted-decimal form */
  572. else if ((in_addr.sin_addr.s_addr = inet_addr(name)) != -1)
  573. in_addr.sin_family = AF_INET;
  574. else
  575. {
  576. hostent = gethostbyname (name);
  577. if (!hostent)
  578. return 0;
  579. if (hostent->h_addrtype != AF_INET || hostent->h_length != 4)
  580. return 0;
  581. in_addr.sin_family = hostent->h_addrtype;
  582. memmove( &in_addr.sin_addr, hostent->h_addr, 4);
  583. }
  584. in_addr.sin_port = htons (XDM_UDP_PORT);
  585. #ifdef BSD44SOCKETS
  586. in_addr.sin_len = sizeof(in_addr);
  587. #endif
  588. RegisterHostaddr ((struct sockaddr *)&in_addr, sizeof (in_addr),
  589. QUERY);
  590. }
  591. return 1;
  592. }
  593. static ARRAYofARRAY8 AuthenticationNames;
  594. static int
  595. RegisterAuthenticationName (char *name, int namelen)
  596. {
  597. ARRAY8Ptr authName;
  598. if (!XdmcpReallocARRAYofARRAY8 (&AuthenticationNames,
  599. AuthenticationNames.length + 1))
  600. return 0;
  601. authName = &AuthenticationNames.data[AuthenticationNames.length-1];
  602. if (!XdmcpAllocARRAY8 (authName, namelen))
  603. return 0;
  604. memmove( authName->data, name, namelen);
  605. return 1;
  606. }
  607. int
  608. InitXDMCP (char **argv)
  609. {
  610. int soopts = 1;
  611. XdmcpHeader header;
  612. int i;
  613. int optlen;
  614. header.version = XDM_PROTOCOL_VERSION;
  615. header.opcode = (CARD16) BROADCAST_QUERY;
  616. header.length = 1;
  617. for (i = 0; i < (int)AuthenticationNames.length; i++)
  618. header.length += 2 + AuthenticationNames.data[i].length;
  619. XdmcpWriteHeader (&broadcastBuffer, &header);
  620. XdmcpWriteARRAYofARRAY8 (&broadcastBuffer, &AuthenticationNames);
  621. header.version = XDM_PROTOCOL_VERSION;
  622. header.opcode = (CARD16) QUERY;
  623. header.length = 1;
  624. for (i = 0; i < (int)AuthenticationNames.length; i++)
  625. header.length += 2 + AuthenticationNames.data[i].length;
  626. XdmcpWriteHeader (&directBuffer, &header);
  627. XdmcpWriteARRAYofARRAY8 (&directBuffer, &AuthenticationNames);
  628. if ((socketFD = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
  629. return 0;
  630. #ifdef SO_BROADCAST
  631. soopts = 1;
  632. if (setsockopt (socketFD, SOL_SOCKET, SO_BROADCAST, (char *)&soopts, sizeof (soopts)) < 0)
  633. perror ("setsockopt");
  634. #endif
  635. XtAddInput (socketFD, (XtPointer) XtInputReadMask, ReceivePacket,
  636. (XtPointer) 0);
  637. while (*argv)
  638. {
  639. RegisterHostname (*argv);
  640. ++argv;
  641. }
  642. pingTry = 0;
  643. PingHosts ((XtPointer)NULL, (XtIntervalId *)NULL);
  644. return 1;
  645. }
  646. void
  647. Choose (HostName *h)
  648. {
  649. if (app_resources.xdmAddress)
  650. {
  651. struct sockaddr_in in_addr;
  652. struct sockaddr *addr;
  653. int family;
  654. int len;
  655. int fd;
  656. char buf[1024];
  657. XdmcpBuffer buffer;
  658. char *xdm;
  659. xdm = (char *) app_resources.xdmAddress->data;
  660. family = (xdm[0] << 8) + xdm[1];
  661. switch (family) {
  662. case AF_INET:
  663. #ifdef BSD44SOCKETS
  664. in_addr.sin_len = sizeof(in_addr);
  665. #endif
  666. in_addr.sin_family = family;
  667. memmove( &in_addr.sin_port, xdm + 2, 2);
  668. memmove( &in_addr.sin_addr, xdm + 4, 4);
  669. addr = (struct sockaddr *) &in_addr;
  670. len = sizeof (in_addr);
  671. break;
  672. default:
  673. fprintf (stderr, "Unhandled protocol family %d\n", family);
  674. exit (REMANAGE_DISPLAY);
  675. }
  676. if ((fd = socket (family, SOCK_STREAM, 0)) == -1)
  677. {
  678. fprintf (stderr, "Cannot create response socket\n");
  679. exit (REMANAGE_DISPLAY);
  680. }
  681. if (connect (fd, addr, len) == -1)
  682. {
  683. fprintf (stderr, "Cannot connect to xdm\n");
  684. exit (REMANAGE_DISPLAY);
  685. }
  686. buffer.data = (BYTE *) buf;
  687. buffer.size = sizeof (buf);
  688. buffer.pointer = 0;
  689. buffer.count = 0;
  690. XdmcpWriteARRAY8 (&buffer, app_resources.clientAddress);
  691. XdmcpWriteCARD16 (&buffer, (CARD16) app_resources.connectionType);
  692. XdmcpWriteARRAY8 (&buffer, &h->hostaddr);
  693. if(-1 == write (fd, (char *)buffer.data, buffer.pointer)) {
  694. perror(strerror(errno));
  695. }
  696. close (fd);
  697. }
  698. else
  699. {
  700. int i;
  701. printf ("%u\n", h->connectionType);
  702. for (i = 0; i < (int)h->hostaddr.length; i++)
  703. printf ("%u%s", h->hostaddr.data[i],
  704. i == h->hostaddr.length - 1 ? "\n" : " ");
  705. }
  706. }
  707. /* ARGSUSED */
  708. void
  709. DoAccept (Widget w, XEvent *event, String *params, Cardinal *num_params)
  710. {
  711. HostName *h;
  712. XmStringTable selectedItem;
  713. int selectedCount;
  714. int i;
  715. char *text;
  716. /*********************************/
  717. /** see if anything is selected **/
  718. /*********************************/
  719. i = 0;
  720. XtSetArg(chooserArgs[i], XmNselectedItemCount, &selectedCount); i++;
  721. XtGetValues(chooser_list, chooserArgs, i);
  722. if (selectedCount != 1) {
  723. XBell (XtDisplay (toplevel), 0);
  724. return;
  725. }
  726. /**********************************************/
  727. /** retrieve the selected item from the list **/
  728. /**********************************************/
  729. i = 0;
  730. XtSetArg(chooserArgs[i], XmNselectedItems, &selectedItem); i++;
  731. XtGetValues(chooser_list, chooserArgs, i);
  732. text = (char*) _XmStringUngenerate(
  733. selectedItem[0], NULL,
  734. XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
  735. if (NULL == text) return;
  736. for (h = hostNamedb; h; h = h->next)
  737. if (!strcmp (text, h->fullname))
  738. {
  739. Choose (h);
  740. }
  741. if (NULL != text) XtFree(text);
  742. exit (OBEYSESS_DISPLAY);
  743. }
  744. /* ARGSUSED */
  745. static void
  746. DoCheckWilling (Widget w, XEvent *event, String *params, Cardinal *num_params)
  747. {
  748. HostName *h;
  749. XmStringTable selectedItem;
  750. int selectedCount;
  751. int i;
  752. char *text;
  753. /*********************************/
  754. /** see if anything is selected **/
  755. /*********************************/
  756. i = 0;
  757. XtSetArg(chooserArgs[i], XmNselectedItemCount, &selectedCount); i++;
  758. XtGetValues(chooser_list, chooserArgs, i);
  759. if (selectedCount != 1) {
  760. return;
  761. }
  762. /**********************************************/
  763. /** retrieve the selected item from the list **/
  764. /**********************************************/
  765. i = 0;
  766. XtSetArg(chooserArgs[i], XmNselectedItems, &selectedItem); i++;
  767. XtGetValues(chooser_list, chooserArgs, i);
  768. text = (char*) _XmStringUngenerate(
  769. selectedItem[0], NULL,
  770. XmMULTIBYTE_TEXT, XmMULTIBYTE_TEXT);
  771. if (NULL == text) return;
  772. for (h = hostNamedb; h; h = h->next)
  773. if (!strcmp (text, h->fullname))
  774. if (!h->willing)
  775. XmListDeselectAllItems (chooser_list);
  776. if (NULL != text) XtFree(text);
  777. }
  778. /* ARGSUSED */
  779. void
  780. DoCancel (Widget w, XEvent *event, String *params, Cardinal *num_params)
  781. {
  782. exit (OBEYSESS_DISPLAY);
  783. }
  784. /* ARGSUSED */
  785. void
  786. DoPing (Widget w, XEvent *event, String *params, Cardinal *num_params)
  787. {
  788. EmptyHostnames ();
  789. pingTry = 0;
  790. PingHosts ((XtPointer)NULL, (XtIntervalId *)NULL);
  791. }
  792. static XtActionsRec app_actions[] = {
  793. "Accept", DoAccept,
  794. "Cancel", DoCancel,
  795. "CheckWilling", DoCheckWilling,
  796. "Ping", DoPing,
  797. };
  798. int
  799. main (int argc, char **argv)
  800. {
  801. Arg position[3];
  802. Dimension width, height;
  803. Position x, y;
  804. int i;
  805. char *xsetup;
  806. /******************************/
  807. /** set the locale, and **/
  808. /** open the message catalog **/
  809. /******************************/
  810. setlocale(LC_ALL, "");
  811. XtSetLanguageProc( NULL, NULL, NULL );
  812. langenv = getenv("LANG");
  813. /******************************************/
  814. /** set the font paths **/
  815. /******************************************/
  816. if ( (xsetup = getenv("XSETUP")) != NULL)
  817. if(system(xsetup) == -1)
  818. fprintf (stderr, "dtchooser: Cannot source %s\n",xsetup);
  819. /******************************************/
  820. /** save argc and argv for RespondLangCB **/
  821. /******************************************/
  822. amChooser = 1; /** tell RespondLangCB we are a chooser **/
  823. orig_argc = argc;
  824. orig_argv = (char **)malloc(sizeof(char *) * (orig_argc+1));
  825. for (i = 0; i < orig_argc; i++) {
  826. orig_argv[i] = argv[i];
  827. }
  828. orig_argv[orig_argc] = NULL;
  829. toplevel = XtInitialize (argv[0], "Dtlogin", options, XtNumber(options),
  830. &argc, argv);
  831. XtVaSetValues(XmGetXmDisplay(XtDisplay(toplevel)),
  832. XmNdragInitiatorProtocolStyle, XmDRAG_NONE,
  833. NULL);
  834. XtAddConverter(XtRString, XtRARRAY8, CvtStringToARRAY8, NULL, 0);
  835. XtGetApplicationResources (toplevel, (XtPointer) &app_resources, resources,
  836. XtNumber (resources), NULL, (Cardinal) 0);
  837. dpyinfo.dpy = XtDisplay(toplevel);
  838. /* dpyinfo.name = "";*/
  839. dpyinfo.screen = DefaultScreen(dpyinfo.dpy);
  840. dpyinfo.root = RootWindow (dpyinfo.dpy, dpyinfo.screen);
  841. dpyinfo.depth = DefaultDepth (dpyinfo.dpy, dpyinfo.screen);
  842. dpyinfo.width = DisplayWidth (dpyinfo.dpy, dpyinfo.screen);
  843. dpyinfo.height = DisplayHeight(dpyinfo.dpy, dpyinfo.screen);
  844. dpyinfo.black_pixel = BlackPixel (dpyinfo.dpy, dpyinfo.screen);
  845. dpyinfo.visual = DefaultVisual(dpyinfo.dpy, dpyinfo.screen);
  846. /*
  847. * build widgets...
  848. */
  849. MakeBackground(); /* login_shell, table, matte */
  850. MakeLogo(); /* logo, logo_pixmap, logo_shadow */
  851. MakeButtons(); /* Push Buttons */
  852. MakeChooser(); /* Chooser List ... */
  853. if (appInfo.optionsDelay == 0 )
  854. MakeOptionsMenu(); /* make option_button pop-up menu */
  855. else
  856. XtAddTimeOut((unsigned long) appInfo.optionsDelay * 1000, MakeOptionsProc, NULL);
  857. #if 0
  858. /*
  859. * center ourselves on the screen
  860. */
  861. XtSetMappedWhenManaged(toplevel, FALSE);
  862. XtRealizeWidget (toplevel);
  863. #endif
  864. XtRealizeWidget (login_shell);
  865. XtPopup(login_shell, XtGrabNone);
  866. #if 0
  867. XtSetArg (position[0], XtNwidth, &width);
  868. XtSetArg (position[1], XtNheight, &height);
  869. XtGetValues (login_shell, position, (Cardinal) 2);
  870. x = (Position)(WidthOfScreen (XtScreen (toplevel)) - width) / 2;
  871. y = (Position)(HeightOfScreen (XtScreen (toplevel)) - height) / 3;
  872. XtSetArg (position[0], XtNx, x);
  873. XtSetArg (position[1], XtNy, y);
  874. XtSetValues (login_shell, position, (Cardinal) 2);
  875. #endif
  876. /*
  877. * Run
  878. */
  879. #if 0
  880. XtMapWidget(toplevel);
  881. #endif
  882. InitXDMCP (argv + 1);
  883. XtMainLoop ();
  884. return 0;
  885. }
  886. /* Converts the hex string s of length len into the byte array d.
  887. Returns 0 if s was a legal hex string, 1 otherwise.
  888. */
  889. static int
  890. FromHex (char *s, char *d, int len)
  891. {
  892. int t;
  893. int ret = len&1; /* odd-length hex strings are illegal */
  894. while (len >= 2)
  895. {
  896. #define HexChar(c) ('0' <= (c) && (c) <= '9' ? (c) - '0' : (c) - 'a' + 10)
  897. if (!ishexdigit(*s))
  898. ret = 1;
  899. t = HexChar (*s) << 4;
  900. s++;
  901. if (!ishexdigit(*s))
  902. ret = 1;
  903. t += HexChar (*s);
  904. s++;
  905. *d++ = t;
  906. len -= 2;
  907. }
  908. return ret;
  909. }
  910. /*ARGSUSED*/
  911. static void
  912. CvtStringToARRAY8 (XrmValuePtr args, Cardinal *num_args, XrmValuePtr fromVal, XrmValuePtr toVal)
  913. {
  914. static ARRAY8Ptr dest;
  915. char *s;
  916. int len;
  917. dest = (ARRAY8Ptr) XtMalloc (sizeof (ARRAY8));
  918. len = fromVal->size;
  919. s = (char *) fromVal->addr;
  920. if (!XdmcpAllocARRAY8 (dest, len >> 1))
  921. XtStringConversionWarning ((char *) fromVal->addr, XtRARRAY8);
  922. else
  923. {
  924. FromHex (s, (char *) dest->data, len);
  925. }
  926. toVal->addr = (caddr_t) &dest;
  927. toVal->size = sizeof (ARRAY8Ptr);
  928. }