PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/freebsd/crypto/heimdal/appl/telnet/telnet/utilities.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 864 lines | 783 code | 35 blank | 46 comment | 135 complexity | 8c456b7518276c40624c980790d8b8ba MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. * Copyright (c) 1988, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #define TELOPTS
  34. #define TELCMDS
  35. #define SLC_NAMES
  36. #include "telnet_locl.h"
  37. RCSID("$Id: utilities.c 10587 2001-08-29 00:45:23Z assar $");
  38. FILE *NetTrace = 0; /* Not in bss, since needs to stay */
  39. int prettydump;
  40. /*
  41. * SetSockOpt()
  42. *
  43. * Compensate for differences in 4.2 and 4.3 systems.
  44. */
  45. int
  46. SetSockOpt(int fd, int level, int option, int yesno)
  47. {
  48. #ifdef HAVE_SETSOCKOPT
  49. #ifndef NOT43
  50. return setsockopt(fd, level, option,
  51. (void *)&yesno, sizeof yesno);
  52. #else /* NOT43 */
  53. if (yesno == 0) { /* Can't do that in 4.2! */
  54. fprintf(stderr, "Error: attempt to turn off an option 0x%x.\n",
  55. option);
  56. return -1;
  57. }
  58. return setsockopt(fd, level, option, 0, 0);
  59. #endif /* NOT43 */
  60. #else
  61. return -1;
  62. #endif
  63. }
  64. /*
  65. * The following are routines used to print out debugging information.
  66. */
  67. char NetTraceFile[256] = "(standard output)";
  68. void
  69. SetNetTrace(char *file)
  70. {
  71. if (NetTrace && NetTrace != stdout)
  72. fclose(NetTrace);
  73. if (file && (strcmp(file, "-") != 0)) {
  74. NetTrace = fopen(file, "w");
  75. if (NetTrace) {
  76. strlcpy(NetTraceFile, file, sizeof(NetTraceFile));
  77. return;
  78. }
  79. fprintf(stderr, "Cannot open %s.\n", file);
  80. }
  81. NetTrace = stdout;
  82. strlcpy(NetTraceFile, "(standard output)", sizeof(NetTraceFile));
  83. }
  84. void
  85. Dump(char direction, unsigned char *buffer, int length)
  86. {
  87. # define BYTES_PER_LINE 32
  88. unsigned char *pThis;
  89. int offset;
  90. offset = 0;
  91. while (length) {
  92. /* print one line */
  93. fprintf(NetTrace, "%c 0x%x\t", direction, offset);
  94. pThis = buffer;
  95. if (prettydump) {
  96. buffer = buffer + min(length, BYTES_PER_LINE/2);
  97. while (pThis < buffer) {
  98. fprintf(NetTrace, "%c%.2x",
  99. (((*pThis)&0xff) == 0xff) ? '*' : ' ',
  100. (*pThis)&0xff);
  101. pThis++;
  102. }
  103. length -= BYTES_PER_LINE/2;
  104. offset += BYTES_PER_LINE/2;
  105. } else {
  106. buffer = buffer + min(length, BYTES_PER_LINE);
  107. while (pThis < buffer) {
  108. fprintf(NetTrace, "%.2x", (*pThis)&0xff);
  109. pThis++;
  110. }
  111. length -= BYTES_PER_LINE;
  112. offset += BYTES_PER_LINE;
  113. }
  114. if (NetTrace == stdout) {
  115. fprintf(NetTrace, "\r\n");
  116. } else {
  117. fprintf(NetTrace, "\n");
  118. }
  119. if (length < 0) {
  120. fflush(NetTrace);
  121. return;
  122. }
  123. /* find next unique line */
  124. }
  125. fflush(NetTrace);
  126. }
  127. void
  128. printoption(char *direction, int cmd, int option)
  129. {
  130. if (!showoptions)
  131. return;
  132. if (cmd == IAC) {
  133. if (TELCMD_OK(option))
  134. fprintf(NetTrace, "%s IAC %s", direction, TELCMD(option));
  135. else
  136. fprintf(NetTrace, "%s IAC %d", direction, option);
  137. } else {
  138. char *fmt;
  139. fmt = (cmd == WILL) ? "WILL" : (cmd == WONT) ? "WONT" :
  140. (cmd == DO) ? "DO" : (cmd == DONT) ? "DONT" : 0;
  141. if (fmt) {
  142. fprintf(NetTrace, "%s %s ", direction, fmt);
  143. if (TELOPT_OK(option))
  144. fprintf(NetTrace, "%s", TELOPT(option));
  145. else if (option == TELOPT_EXOPL)
  146. fprintf(NetTrace, "EXOPL");
  147. else
  148. fprintf(NetTrace, "%d", option);
  149. } else
  150. fprintf(NetTrace, "%s %d %d", direction, cmd, option);
  151. }
  152. if (NetTrace == stdout) {
  153. fprintf(NetTrace, "\r\n");
  154. fflush(NetTrace);
  155. } else {
  156. fprintf(NetTrace, "\n");
  157. }
  158. return;
  159. }
  160. void
  161. optionstatus(void)
  162. {
  163. int i;
  164. for (i = 0; i < 256; i++) {
  165. if (do_dont_resp[i]) {
  166. if (TELOPT_OK(i))
  167. printf("resp DO_DONT %s: %d\n", TELOPT(i), do_dont_resp[i]);
  168. else if (TELCMD_OK(i))
  169. printf("resp DO_DONT %s: %d\n", TELCMD(i), do_dont_resp[i]);
  170. else
  171. printf("resp DO_DONT %d: %d\n", i,
  172. do_dont_resp[i]);
  173. if (my_want_state_is_do(i)) {
  174. if (TELOPT_OK(i))
  175. printf("want DO %s\n", TELOPT(i));
  176. else if (TELCMD_OK(i))
  177. printf("want DO %s\n", TELCMD(i));
  178. else
  179. printf("want DO %d\n", i);
  180. } else {
  181. if (TELOPT_OK(i))
  182. printf("want DONT %s\n", TELOPT(i));
  183. else if (TELCMD_OK(i))
  184. printf("want DONT %s\n", TELCMD(i));
  185. else
  186. printf("want DONT %d\n", i);
  187. }
  188. } else {
  189. if (my_state_is_do(i)) {
  190. if (TELOPT_OK(i))
  191. printf(" DO %s\n", TELOPT(i));
  192. else if (TELCMD_OK(i))
  193. printf(" DO %s\n", TELCMD(i));
  194. else
  195. printf(" DO %d\n", i);
  196. }
  197. }
  198. if (will_wont_resp[i]) {
  199. if (TELOPT_OK(i))
  200. printf("resp WILL_WONT %s: %d\n", TELOPT(i), will_wont_resp[i]);
  201. else if (TELCMD_OK(i))
  202. printf("resp WILL_WONT %s: %d\n", TELCMD(i), will_wont_resp[i]);
  203. else
  204. printf("resp WILL_WONT %d: %d\n",
  205. i, will_wont_resp[i]);
  206. if (my_want_state_is_will(i)) {
  207. if (TELOPT_OK(i))
  208. printf("want WILL %s\n", TELOPT(i));
  209. else if (TELCMD_OK(i))
  210. printf("want WILL %s\n", TELCMD(i));
  211. else
  212. printf("want WILL %d\n", i);
  213. } else {
  214. if (TELOPT_OK(i))
  215. printf("want WONT %s\n", TELOPT(i));
  216. else if (TELCMD_OK(i))
  217. printf("want WONT %s\n", TELCMD(i));
  218. else
  219. printf("want WONT %d\n", i);
  220. }
  221. } else {
  222. if (my_state_is_will(i)) {
  223. if (TELOPT_OK(i))
  224. printf(" WILL %s\n", TELOPT(i));
  225. else if (TELCMD_OK(i))
  226. printf(" WILL %s\n", TELCMD(i));
  227. else
  228. printf(" WILL %d\n", i);
  229. }
  230. }
  231. }
  232. }
  233. void
  234. printsub(int direction, unsigned char *pointer, int length)
  235. {
  236. int i;
  237. unsigned char buf[512];
  238. if (showoptions || direction == 0 ||
  239. (want_status_response && (pointer[0] == TELOPT_STATUS))) {
  240. if (direction) {
  241. fprintf(NetTrace, "%s IAC SB ",
  242. (direction == '<')? "RCVD":"SENT");
  243. if (length >= 3) {
  244. int j;
  245. i = pointer[length-2];
  246. j = pointer[length-1];
  247. if (i != IAC || j != SE) {
  248. fprintf(NetTrace, "(terminated by ");
  249. if (TELOPT_OK(i))
  250. fprintf(NetTrace, "%s ", TELOPT(i));
  251. else if (TELCMD_OK(i))
  252. fprintf(NetTrace, "%s ", TELCMD(i));
  253. else
  254. fprintf(NetTrace, "%d ", i);
  255. if (TELOPT_OK(j))
  256. fprintf(NetTrace, "%s", TELOPT(j));
  257. else if (TELCMD_OK(j))
  258. fprintf(NetTrace, "%s", TELCMD(j));
  259. else
  260. fprintf(NetTrace, "%d", j);
  261. fprintf(NetTrace, ", not IAC SE!) ");
  262. }
  263. }
  264. length -= 2;
  265. }
  266. if (length < 1) {
  267. fprintf(NetTrace, "(Empty suboption??\?)");
  268. if (NetTrace == stdout)
  269. fflush(NetTrace);
  270. return;
  271. }
  272. switch (pointer[0]) {
  273. case TELOPT_TTYPE:
  274. fprintf(NetTrace, "TERMINAL-TYPE ");
  275. switch (pointer[1]) {
  276. case TELQUAL_IS:
  277. fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
  278. break;
  279. case TELQUAL_SEND:
  280. fprintf(NetTrace, "SEND");
  281. break;
  282. default:
  283. fprintf(NetTrace,
  284. "- unknown qualifier %d (0x%x).",
  285. pointer[1], pointer[1]);
  286. }
  287. break;
  288. case TELOPT_TSPEED:
  289. fprintf(NetTrace, "TERMINAL-SPEED");
  290. if (length < 2) {
  291. fprintf(NetTrace, " (empty suboption??\?)");
  292. break;
  293. }
  294. switch (pointer[1]) {
  295. case TELQUAL_IS:
  296. fprintf(NetTrace, " IS ");
  297. fprintf(NetTrace, "%.*s", length-2, (char *)pointer+2);
  298. break;
  299. default:
  300. if (pointer[1] == 1)
  301. fprintf(NetTrace, " SEND");
  302. else
  303. fprintf(NetTrace, " %d (unknown)", pointer[1]);
  304. for (i = 2; i < length; i++)
  305. fprintf(NetTrace, " ?%d?", pointer[i]);
  306. break;
  307. }
  308. break;
  309. case TELOPT_LFLOW:
  310. fprintf(NetTrace, "TOGGLE-FLOW-CONTROL");
  311. if (length < 2) {
  312. fprintf(NetTrace, " (empty suboption??\?)");
  313. break;
  314. }
  315. switch (pointer[1]) {
  316. case LFLOW_OFF:
  317. fprintf(NetTrace, " OFF"); break;
  318. case LFLOW_ON:
  319. fprintf(NetTrace, " ON"); break;
  320. case LFLOW_RESTART_ANY:
  321. fprintf(NetTrace, " RESTART-ANY"); break;
  322. case LFLOW_RESTART_XON:
  323. fprintf(NetTrace, " RESTART-XON"); break;
  324. default:
  325. fprintf(NetTrace, " %d (unknown)", pointer[1]);
  326. }
  327. for (i = 2; i < length; i++)
  328. fprintf(NetTrace, " ?%d?", pointer[i]);
  329. break;
  330. case TELOPT_NAWS:
  331. fprintf(NetTrace, "NAWS");
  332. if (length < 2) {
  333. fprintf(NetTrace, " (empty suboption??\?)");
  334. break;
  335. }
  336. if (length == 2) {
  337. fprintf(NetTrace, " ?%d?", pointer[1]);
  338. break;
  339. }
  340. fprintf(NetTrace, " %d %d (%d)",
  341. pointer[1], pointer[2],
  342. (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
  343. if (length == 4) {
  344. fprintf(NetTrace, " ?%d?", pointer[3]);
  345. break;
  346. }
  347. fprintf(NetTrace, " %d %d (%d)",
  348. pointer[3], pointer[4],
  349. (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
  350. for (i = 5; i < length; i++)
  351. fprintf(NetTrace, " ?%d?", pointer[i]);
  352. break;
  353. #if defined(AUTHENTICATION)
  354. case TELOPT_AUTHENTICATION:
  355. fprintf(NetTrace, "AUTHENTICATION");
  356. if (length < 2) {
  357. fprintf(NetTrace, " (empty suboption??\?)");
  358. break;
  359. }
  360. switch (pointer[1]) {
  361. case TELQUAL_REPLY:
  362. case TELQUAL_IS:
  363. fprintf(NetTrace, " %s ", (pointer[1] == TELQUAL_IS) ?
  364. "IS" : "REPLY");
  365. if (AUTHTYPE_NAME_OK(pointer[2]))
  366. fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[2]));
  367. else
  368. fprintf(NetTrace, "%d ", pointer[2]);
  369. if (length < 3) {
  370. fprintf(NetTrace, "(partial suboption??\?)");
  371. break;
  372. }
  373. fprintf(NetTrace, "%s|%s",
  374. ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
  375. "CLIENT" : "SERVER",
  376. ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
  377. "MUTUAL" : "ONE-WAY");
  378. auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
  379. fprintf(NetTrace, "%s", buf);
  380. break;
  381. case TELQUAL_SEND:
  382. i = 2;
  383. fprintf(NetTrace, " SEND ");
  384. while (i < length) {
  385. if (AUTHTYPE_NAME_OK(pointer[i]))
  386. fprintf(NetTrace, "%s ", AUTHTYPE_NAME(pointer[i]));
  387. else
  388. fprintf(NetTrace, "%d ", pointer[i]);
  389. if (++i >= length) {
  390. fprintf(NetTrace, "(partial suboption??\?)");
  391. break;
  392. }
  393. fprintf(NetTrace, "%s|%s ",
  394. ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
  395. "CLIENT" : "SERVER",
  396. ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
  397. "MUTUAL" : "ONE-WAY");
  398. ++i;
  399. }
  400. break;
  401. case TELQUAL_NAME:
  402. i = 2;
  403. fprintf(NetTrace, " NAME \"");
  404. while (i < length)
  405. putc(pointer[i++], NetTrace);
  406. putc('"', NetTrace);
  407. break;
  408. default:
  409. for (i = 2; i < length; i++)
  410. fprintf(NetTrace, " ?%d?", pointer[i]);
  411. break;
  412. }
  413. break;
  414. #endif
  415. #if defined(ENCRYPTION)
  416. case TELOPT_ENCRYPT:
  417. fprintf(NetTrace, "ENCRYPT");
  418. if (length < 2) {
  419. fprintf(NetTrace, " (empty suboption?)");
  420. break;
  421. }
  422. switch (pointer[1]) {
  423. case ENCRYPT_START:
  424. fprintf(NetTrace, " START");
  425. break;
  426. case ENCRYPT_END:
  427. fprintf(NetTrace, " END");
  428. break;
  429. case ENCRYPT_REQSTART:
  430. fprintf(NetTrace, " REQUEST-START");
  431. break;
  432. case ENCRYPT_REQEND:
  433. fprintf(NetTrace, " REQUEST-END");
  434. break;
  435. case ENCRYPT_IS:
  436. case ENCRYPT_REPLY:
  437. fprintf(NetTrace, " %s ", (pointer[1] == ENCRYPT_IS) ?
  438. "IS" : "REPLY");
  439. if (length < 3) {
  440. fprintf(NetTrace, " (partial suboption?)");
  441. break;
  442. }
  443. if (ENCTYPE_NAME_OK(pointer[2]))
  444. fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[2]));
  445. else
  446. fprintf(NetTrace, " %d (unknown)", pointer[2]);
  447. encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
  448. fprintf(NetTrace, "%s", buf);
  449. break;
  450. case ENCRYPT_SUPPORT:
  451. i = 2;
  452. fprintf(NetTrace, " SUPPORT ");
  453. while (i < length) {
  454. if (ENCTYPE_NAME_OK(pointer[i]))
  455. fprintf(NetTrace, "%s ", ENCTYPE_NAME(pointer[i]));
  456. else
  457. fprintf(NetTrace, "%d ", pointer[i]);
  458. i++;
  459. }
  460. break;
  461. case ENCRYPT_ENC_KEYID:
  462. fprintf(NetTrace, " ENC_KEYID ");
  463. goto encommon;
  464. case ENCRYPT_DEC_KEYID:
  465. fprintf(NetTrace, " DEC_KEYID ");
  466. goto encommon;
  467. default:
  468. fprintf(NetTrace, " %d (unknown)", pointer[1]);
  469. encommon:
  470. for (i = 2; i < length; i++)
  471. fprintf(NetTrace, " %d", pointer[i]);
  472. break;
  473. }
  474. break;
  475. #endif
  476. case TELOPT_LINEMODE:
  477. fprintf(NetTrace, "LINEMODE ");
  478. if (length < 2) {
  479. fprintf(NetTrace, " (empty suboption??\?)");
  480. break;
  481. }
  482. switch (pointer[1]) {
  483. case WILL:
  484. fprintf(NetTrace, "WILL ");
  485. goto common;
  486. case WONT:
  487. fprintf(NetTrace, "WONT ");
  488. goto common;
  489. case DO:
  490. fprintf(NetTrace, "DO ");
  491. goto common;
  492. case DONT:
  493. fprintf(NetTrace, "DONT ");
  494. common:
  495. if (length < 3) {
  496. fprintf(NetTrace, "(no option??\?)");
  497. break;
  498. }
  499. switch (pointer[2]) {
  500. case LM_FORWARDMASK:
  501. fprintf(NetTrace, "Forward Mask");
  502. for (i = 3; i < length; i++)
  503. fprintf(NetTrace, " %x", pointer[i]);
  504. break;
  505. default:
  506. fprintf(NetTrace, "%d (unknown)", pointer[2]);
  507. for (i = 3; i < length; i++)
  508. fprintf(NetTrace, " %d", pointer[i]);
  509. break;
  510. }
  511. break;
  512. case LM_SLC:
  513. fprintf(NetTrace, "SLC");
  514. for (i = 2; i < length - 2; i += 3) {
  515. if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
  516. fprintf(NetTrace, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
  517. else
  518. fprintf(NetTrace, " %d", pointer[i+SLC_FUNC]);
  519. switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
  520. case SLC_NOSUPPORT:
  521. fprintf(NetTrace, " NOSUPPORT"); break;
  522. case SLC_CANTCHANGE:
  523. fprintf(NetTrace, " CANTCHANGE"); break;
  524. case SLC_VARIABLE:
  525. fprintf(NetTrace, " VARIABLE"); break;
  526. case SLC_DEFAULT:
  527. fprintf(NetTrace, " DEFAULT"); break;
  528. }
  529. fprintf(NetTrace, "%s%s%s",
  530. pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
  531. pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
  532. pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
  533. if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
  534. SLC_FLUSHOUT| SLC_LEVELBITS))
  535. fprintf(NetTrace, "(0x%x)", pointer[i+SLC_FLAGS]);
  536. fprintf(NetTrace, " %d;", pointer[i+SLC_VALUE]);
  537. if ((pointer[i+SLC_VALUE] == IAC) &&
  538. (pointer[i+SLC_VALUE+1] == IAC))
  539. i++;
  540. }
  541. for (; i < length; i++)
  542. fprintf(NetTrace, " ?%d?", pointer[i]);
  543. break;
  544. case LM_MODE:
  545. fprintf(NetTrace, "MODE ");
  546. if (length < 3) {
  547. fprintf(NetTrace, "(no mode??\?)");
  548. break;
  549. }
  550. {
  551. char tbuf[64];
  552. snprintf(tbuf, sizeof(tbuf),
  553. "%s%s%s%s%s",
  554. pointer[2]&MODE_EDIT ? "|EDIT" : "",
  555. pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
  556. pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
  557. pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
  558. pointer[2]&MODE_ACK ? "|ACK" : "");
  559. fprintf(NetTrace, "%s", tbuf[1] ? &tbuf[1] : "0");
  560. }
  561. if (pointer[2]&~(MODE_MASK))
  562. fprintf(NetTrace, " (0x%x)", pointer[2]);
  563. for (i = 3; i < length; i++)
  564. fprintf(NetTrace, " ?0x%x?", pointer[i]);
  565. break;
  566. default:
  567. fprintf(NetTrace, "%d (unknown)", pointer[1]);
  568. for (i = 2; i < length; i++)
  569. fprintf(NetTrace, " %d", pointer[i]);
  570. }
  571. break;
  572. case TELOPT_STATUS: {
  573. char *cp;
  574. int j, k;
  575. fprintf(NetTrace, "STATUS");
  576. switch (pointer[1]) {
  577. default:
  578. if (pointer[1] == TELQUAL_SEND)
  579. fprintf(NetTrace, " SEND");
  580. else
  581. fprintf(NetTrace, " %d (unknown)", pointer[1]);
  582. for (i = 2; i < length; i++)
  583. fprintf(NetTrace, " ?%d?", pointer[i]);
  584. break;
  585. case TELQUAL_IS:
  586. if (--want_status_response < 0)
  587. want_status_response = 0;
  588. if (NetTrace == stdout)
  589. fprintf(NetTrace, " IS\r\n");
  590. else
  591. fprintf(NetTrace, " IS\n");
  592. for (i = 2; i < length; i++) {
  593. switch(pointer[i]) {
  594. case DO: cp = "DO"; goto common2;
  595. case DONT: cp = "DONT"; goto common2;
  596. case WILL: cp = "WILL"; goto common2;
  597. case WONT: cp = "WONT"; goto common2;
  598. common2:
  599. i++;
  600. if (TELOPT_OK((int)pointer[i]))
  601. fprintf(NetTrace, " %s %s", cp, TELOPT(pointer[i]));
  602. else
  603. fprintf(NetTrace, " %s %d", cp, pointer[i]);
  604. if (NetTrace == stdout)
  605. fprintf(NetTrace, "\r\n");
  606. else
  607. fprintf(NetTrace, "\n");
  608. break;
  609. case SB:
  610. fprintf(NetTrace, " SB ");
  611. i++;
  612. j = k = i;
  613. while (j < length) {
  614. if (pointer[j] == SE) {
  615. if (j+1 == length)
  616. break;
  617. if (pointer[j+1] == SE)
  618. j++;
  619. else
  620. break;
  621. }
  622. pointer[k++] = pointer[j++];
  623. }
  624. printsub(0, &pointer[i], k - i);
  625. if (i < length) {
  626. fprintf(NetTrace, " SE");
  627. i = j;
  628. } else
  629. i = j - 1;
  630. if (NetTrace == stdout)
  631. fprintf(NetTrace, "\r\n");
  632. else
  633. fprintf(NetTrace, "\n");
  634. break;
  635. default:
  636. fprintf(NetTrace, " %d", pointer[i]);
  637. break;
  638. }
  639. }
  640. break;
  641. }
  642. break;
  643. }
  644. case TELOPT_XDISPLOC:
  645. fprintf(NetTrace, "X-DISPLAY-LOCATION ");
  646. switch (pointer[1]) {
  647. case TELQUAL_IS:
  648. fprintf(NetTrace, "IS \"%.*s\"", length-2, (char *)pointer+2);
  649. break;
  650. case TELQUAL_SEND:
  651. fprintf(NetTrace, "SEND");
  652. break;
  653. default:
  654. fprintf(NetTrace, "- unknown qualifier %d (0x%x).",
  655. pointer[1], pointer[1]);
  656. }
  657. break;
  658. case TELOPT_NEW_ENVIRON:
  659. fprintf(NetTrace, "NEW-ENVIRON ");
  660. #ifdef OLD_ENVIRON
  661. goto env_common1;
  662. case TELOPT_OLD_ENVIRON:
  663. fprintf(NetTrace, "OLD-ENVIRON");
  664. env_common1:
  665. #endif
  666. switch (pointer[1]) {
  667. case TELQUAL_IS:
  668. fprintf(NetTrace, "IS ");
  669. goto env_common;
  670. case TELQUAL_SEND:
  671. fprintf(NetTrace, "SEND ");
  672. goto env_common;
  673. case TELQUAL_INFO:
  674. fprintf(NetTrace, "INFO ");
  675. env_common:
  676. {
  677. int noquote = 2;
  678. for (i = 2; i < length; i++ ) {
  679. switch (pointer[i]) {
  680. case NEW_ENV_VALUE:
  681. #ifdef OLD_ENVIRON
  682. /* case NEW_ENV_OVAR: */
  683. if (pointer[0] == TELOPT_OLD_ENVIRON) {
  684. fprintf(NetTrace, "\" VAR " + noquote);
  685. } else
  686. #endif /* OLD_ENVIRON */
  687. fprintf(NetTrace, "\" VALUE " + noquote);
  688. noquote = 2;
  689. break;
  690. case NEW_ENV_VAR:
  691. #ifdef OLD_ENVIRON
  692. /* case OLD_ENV_VALUE: */
  693. if (pointer[0] == TELOPT_OLD_ENVIRON) {
  694. fprintf(NetTrace, "\" VALUE " + noquote);
  695. } else
  696. #endif /* OLD_ENVIRON */
  697. fprintf(NetTrace, "\" VAR " + noquote);
  698. noquote = 2;
  699. break;
  700. case ENV_ESC:
  701. fprintf(NetTrace, "\" ESC " + noquote);
  702. noquote = 2;
  703. break;
  704. case ENV_USERVAR:
  705. fprintf(NetTrace, "\" USERVAR " + noquote);
  706. noquote = 2;
  707. break;
  708. default:
  709. if (isprint(pointer[i]) && pointer[i] != '"') {
  710. if (noquote) {
  711. putc('"', NetTrace);
  712. noquote = 0;
  713. }
  714. putc(pointer[i], NetTrace);
  715. } else {
  716. fprintf(NetTrace, "\" %03o " + noquote,
  717. pointer[i]);
  718. noquote = 2;
  719. }
  720. break;
  721. }
  722. }
  723. if (!noquote)
  724. putc('"', NetTrace);
  725. break;
  726. }
  727. }
  728. break;
  729. default:
  730. if (TELOPT_OK(pointer[0]))
  731. fprintf(NetTrace, "%s (unknown)", TELOPT(pointer[0]));
  732. else
  733. fprintf(NetTrace, "%d (unknown)", pointer[0]);
  734. for (i = 1; i < length; i++)
  735. fprintf(NetTrace, " %d", pointer[i]);
  736. break;
  737. }
  738. if (direction) {
  739. if (NetTrace == stdout)
  740. fprintf(NetTrace, "\r\n");
  741. else
  742. fprintf(NetTrace, "\n");
  743. }
  744. if (NetTrace == stdout)
  745. fflush(NetTrace);
  746. }
  747. }
  748. /* EmptyTerminal - called to make sure that the terminal buffer is empty.
  749. * Note that we consider the buffer to run all the
  750. * way to the kernel (thus the select).
  751. */
  752. void
  753. EmptyTerminal(void)
  754. {
  755. fd_set outs;
  756. FD_ZERO(&outs);
  757. if (tout >= FD_SETSIZE)
  758. ExitString("fd too large", 1);
  759. if (TTYBYTES() == 0) {
  760. FD_SET(tout, &outs);
  761. select(tout+1, 0, &outs, 0,
  762. (struct timeval *) 0); /* wait for TTLOWAT */
  763. } else {
  764. while (TTYBYTES()) {
  765. ttyflush(0);
  766. FD_SET(tout, &outs);
  767. select(tout+1, 0, &outs, 0,
  768. (struct timeval *) 0); /* wait for TTLOWAT */
  769. }
  770. }
  771. }
  772. void
  773. SetForExit(void)
  774. {
  775. setconnmode(0);
  776. do {
  777. telrcv(); /* Process any incoming data */
  778. EmptyTerminal();
  779. } while (ring_full_count(&netiring)); /* While there is any */
  780. setcommandmode();
  781. fflush(stdout);
  782. fflush(stderr);
  783. setconnmode(0);
  784. EmptyTerminal(); /* Flush the path to the tty */
  785. setcommandmode();
  786. }
  787. void
  788. Exit(int returnCode)
  789. {
  790. SetForExit();
  791. exit(returnCode);
  792. }
  793. void
  794. ExitString(char *string, int returnCode)
  795. {
  796. SetForExit();
  797. fwrite(string, 1, strlen(string), stderr);
  798. exit(returnCode);
  799. }