telnet.c 60 KB
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 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Este programa está nomeado como telnet.c e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 * licinio@bb.com.br		(Licínio Luis Branco)
 * kraucer@bb.com.br		(Kraucer Fernandes Mazuco)
 *
 */

#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"


/**
 * @brief Initializes and manages a telnet socket to the given IBM host.
 */

#if defined(_WIN32)
#include <winsock2.h>
#include <windows.h>
#endif

#ifdef __MINGW32__
#include <sys/time.h>
#endif

#ifndef ANDROID
#include <stdlib.h>
#endif // !ANDROID

#include <config.h>

#include <internals.h>
#include <errno.h>

#if defined(_WIN32)
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#endif

#define TELCMDS 1
#define TELOPTS 1
#include "arpa_telnet.h"

#if !defined(_WIN32)
#include <arpa/inet.h>
#endif

#include <errno.h>
#include <fcntl.h>

#if defined(_WIN32)
#include <lib3270/win32.h>
#else
#include <netdb.h>
#endif

// #include <stdarg.h>

#include "tn3270e.h"
#include "3270ds.h"

// #include "appres.h"

#include "ansic.h"
#include "ctlrc.h"
#include "hostc.h"
#include "kybdc.h"
// #include "macrosc.h"
#include "popupsc.h"
// #include "proxyc.h"
//#include "resolverc.h"
#include "statusc.h"
// #include "tablesc.h"
#include "telnetc.h"
#include "trace_dsc.h"
#include "utilc.h"
#include "w3miscc.h"
#include "xioc.h"
#include "screen.h"

#include <lib3270/internals.h>
#include <lib3270/trace.h>
#include <lib3270/log.h>
#include <lib3270/toggle.h>

#if !defined(TELOPT_NAWS) /*[*/
#define TELOPT_NAWS	31
#endif /*]*/

#if !defined(TELOPT_STARTTLS) /*[*/
#define TELOPT_STARTTLS	46
#endif /*]*/
#define TLS_FOLLOWS	1

#define BUFSZ		16384
#define TRACELINE	72

#if defined(X3270_TN3270E)
#define E_OPT(n)	(1 << (n))
#endif // X3270_TN3270E

struct _ansictl ansictl = { 0 };

static int telnet_fsm(H3270 *session, unsigned char c);
static void net_rawout(H3270 *session, unsigned const char *buf, size_t len);
static void check_in3270(H3270 *session);
static void store3270in(H3270 *hSession, unsigned char c);
static void check_linemode(H3270 *hSession, Boolean init);
static int net_connected(H3270 *session);

static void continue_tls(H3270 *hSession, unsigned char *sbbuf, int len);

#if defined(X3270_TN3270E) /*[*/
static int tn3270e_negotiate(H3270 *hSession);
#endif /*]*/
static int process_eor(H3270 *hSession);
#if defined(X3270_TN3270E) /*[*/
#if defined(X3270_TRACE) /*[*/
static const char *tn3270e_function_names(const unsigned char *, int);
#endif /*]*/
static void tn3270e_subneg_send(H3270 *hSession, unsigned char, unsigned long);
static unsigned long tn3270e_fdecode(const unsigned char *, int);
static void tn3270e_ack(H3270 *hSession);
static void tn3270e_nak(H3270 *hSession, enum pds);
#endif /*]*/

#if defined(X3270_ANSI) /*[*/
static void do_data(H3270 *hSession, char c);
static void do_intr(H3270 *hSession, char c);
static void do_quit(H3270 *hSession, char c);
static void do_cerase(H3270 *hSession, char c);
static void do_werase(H3270 *hSession, char c);
static void do_kill(H3270 *hSession, char c);
static void do_rprnt(H3270 *hSession, char c);
static void do_eof(H3270 *hSession, char c);
static void do_eol(H3270 *hSession, char c);
static void do_lnext(H3270 *hSession, char c);
// static char parse_ctlchar(char *s);
static void cooked_init(H3270 *hSession);
#endif /*]*/

#if defined(X3270_TRACE) /*[*/
static const char *cmd(int c);
static const char *opt(unsigned char c);
static const char *nnn(int c);
#else /*][*/
#if defined(__GNUC__) /*[*/
#else /*][*/
#endif /*]*/
#define cmd(x) 0
#define opt(x) 0
#define nnn(x) 0
#endif /*]*/

/* telnet states */
#define TNS_DATA	0	/* receiving data */
#define TNS_IAC		1	/* got an IAC */
#define TNS_WILL	2	/* got an IAC WILL */
#define TNS_WONT	3	/* got an IAC WONT */
#define TNS_DO		4	/* got an IAC DO */
#define TNS_DONT	5	/* got an IAC DONT */
#define TNS_SB		6	/* got an IAC SB */
#define TNS_SB_IAC	7	/* got an IAC after an IAC SB */

/* telnet predefined messages */
static unsigned char	do_opt[]	= 	{ 	IAC, DO, '_' };
static unsigned char	dont_opt[]	= 	{	IAC, DONT, '_' };
static unsigned char	will_opt[]	= 	{	IAC, WILL, '_' };
static unsigned char	wont_opt[]	= 	{	IAC, WONT, '_' };

#if defined(X3270_TN3270E) /*[*/
static const unsigned char	functions_req[] = {	IAC, SB, TELOPT_TN3270E, TN3270E_OP_FUNCTIONS };
#endif /*]*/

#if defined(X3270_TRACE) /*[*/
static const char *telquals[2] = { "IS", "SEND" };
#endif /*]*/
#if defined(X3270_TN3270E) /*[*/
#if defined(X3270_TRACE) /*[*/
static const char *reason_code[8] = { "CONN-PARTNER", "DEVICE-IN-USE",
                                      "INV-ASSOCIATE", "INV-NAME", "INV-DEVICE-TYPE", "TYPE-NAME-ERROR",
                                      "UNKNOWN-ERROR", "UNSUPPORTED-REQ"
                                    };
#define rsn(n)	(((n) <= TN3270E_REASON_UNSUPPORTED_REQ) ? \
			reason_code[(n)] : "??")
#endif /*]*/
static const char *function_name[5] = { "BIND-IMAGE", "DATA-STREAM-CTL",
                                        "RESPONSES", "SCS-CTL-CODES", "SYSREQ"
                                      };
#define fnn(n)	(((n) <= TN3270E_FUNC_SYSREQ) ? \
			function_name[(n)] : "??")
#if defined(X3270_TRACE) /*[*/
static const char *data_type[9] = { "3270-DATA", "SCS-DATA", "RESPONSE",
                                    "BIND-IMAGE", "UNBIND", "NVT-DATA", "REQUEST", "SSCP-LU-DATA",
                                    "PRINT-EOJ"
                                  };
#define e_dt(n)	(((n) <= TN3270E_DT_PRINT_EOJ) ? \
			data_type[(n)] : "??")
static const char *req_flag[1] = { " ERR-COND-CLEARED" };
#define e_rq(fn, n) (((fn) == TN3270E_DT_REQUEST) ? \
			(((n) <= TN3270E_RQF_ERR_COND_CLEARED) ? \
			req_flag[(n)] : " ??") : "")
static const char *hrsp_flag[3] = { "NO-RESPONSE", "ERROR-RESPONSE",
                                    "ALWAYS-RESPONSE"
                                  };
#define e_hrsp(n) (((n) <= TN3270E_RSF_ALWAYS_RESPONSE) ? \
			hrsp_flag[(n)] : "??")
static const char *trsp_flag[2] = { "POSITIVE-RESPONSE", "NEGATIVE-RESPONSE" };
#define e_trsp(n) (((n) <= TN3270E_RSF_NEGATIVE_RESPONSE) ? \
			trsp_flag[(n)] : "??")
#define e_rsp(fn, n) (((fn) == TN3270E_DT_RESPONSE) ? e_trsp(n) : e_hrsp(n))
#endif /*]*/
#endif /*]*/

#define XMIT_ROWS	hSession->max.rows
#define XMIT_COLS	hSession->max.cols

#if defined(_WIN32) /*[*/
#define socket_errno()	WSAGetLastError()
#define SE_EWOULDBLOCK	WSAEWOULDBLOCK
#define SE_ECONNRESET	WSAECONNRESET
#define SE_EINTR	WSAEINTR
#define SE_EAGAIN	WSAEINPROGRESS
#define SE_EPIPE	WSAECONNABORTED
#define SE_EINPROGRESS	WSAEINPROGRESS
#define SOCK_IOCTL(s, f, v)	ioctlsocket(s, f, (void *)v)
#else /*][*/
#define socket_errno()	errno
#define SE_EWOULDBLOCK	EWOULDBLOCK
#define SE_ECONNRESET	ECONNRESET
#define SE_EINTR	EINTR
#define SE_EAGAIN	EAGAIN
#define SE_EPIPE	EPIPE

#if defined(EINPROGRESS) /*[*/
#define SE_EINPROGRESS	EINPROGRESS
#endif /*]*/

#define SOCK_IOCTL	ioctl
#endif /*]*/


/*--[ Implement ]------------------------------------------------------------------------------------*/

static union {
	struct sockaddr sa;
	struct sockaddr_in sin;
#if defined(AF_INET6) /*[*/
	struct sockaddr_in6 sin6;
#endif /*]*/
} haddr;
socklen_t ha_len = sizeof(haddr);

void popup_a_sockerr(H3270 *hSession, char *fmt, ...) {
#if defined(_WIN32)
	const char *msg = lib3270_win32_strerror(socket_errno());
#else
	const char *msg = strerror(errno);
#endif // WIN32

	va_list args;
	char *text;

	va_start(args, fmt);
	text = lib3270_vsprintf(fmt, args);
	va_end(args);

	lib3270_write_log(
	    hSession,
	    "3270",
	    "Network error:\n%s\n%s",
	    text,
	    msg
	);

	lib3270_popup_dialog(
	    hSession,
	    LIB3270_NOTIFY_ERROR,
	    _( "Network error" ),
	    text,
	    "%s",
	    msg
	);

	lib3270_free(text);

}

// Set up the LU list.
static void setup_lus(H3270 *hSession) {
	hSession->lu.associated = CN;
	hSession->connected_type = CN;

	if(hSession->lu.names) {
		hSession->lu.curr	= hSession->lu.names;
		hSession->lu.try	= * hSession->lu.curr;
	} else {
		hSession->lu.curr	= (char **)NULL;
		hSession->lu.try	= CN;
	}

	/*
	char *lu;
	char *comma;
	int n_lus = 1;
	int i;


	if (!hSession->lu.names[0])
	{
		Replace(hSession->lus, NULL);
		hSession->curr_lu = (char **)NULL;
		hSession->try_lu = CN;
		return;
	}

	//
	// Count the commas in the LU names.  That plus one is the
	// number of LUs to try.
	//
	lu = hSession->lu.names;
	while ((comma = strchr(lu, ',')) != CN)
	{
		n_lus++;
		lu++;
	}

	//
	// Allocate enough memory to construct an argv[] array for
	// the LUs.
	//
	Replace(hSession->lus,(char **)lib3270_malloc((n_lus+1) * sizeof(char *) + strlen(hSession->lu.names) + 1));

	// Copy each LU into the array.
	lu = (char *)(hSession->lus + n_lus + 1);
	(void) strcpy(lu, hSession->lu.names);

	i = 0;
	do
	{
		hSession->lus[i++] = lu;
		comma = strchr(lu, ',');
		if (comma != CN)
		{
			*comma = '\0';
			lu = comma + 1;
		}
	} while (comma != CN);

	hSession->lus[i]	= CN;
	*/

}

static int net_connected(H3270 *hSession) {

	// Set up SSL.
	trace_dsn(hSession,"Connected to %s%s.\n", hSession->host.current,hSession->ssl.host ? " using SSL": "");

	if(hSession->ssl.host && hSession->ssl.state == LIB3270_SSL_UNDEFINED) {
		if(lib3270_start_tls(hSession))
			return -1;
	}

	lib3270_setup_session(hSession);
	lib3270_notify_tls(hSession);

	return 0;
}

/**
 * Set up telnet options.
 *
 * Called just after a sucessfull connect to setup tn3270 state.
 *
 * @param hSession	3270 session to setup.
 *
 */
LIB3270_EXPORT void lib3270_setup_session(H3270 *hSession) {
	(void) memset((char *) hSession->myopts, 0, sizeof(hSession->myopts));
	(void) memset((char *) hSession->hisopts, 0, sizeof(hSession->hisopts));

#if defined(X3270_TN3270E)
	hSession->e_funcs = E_OPT(TN3270E_FUNC_BIND_IMAGE) | E_OPT(TN3270E_FUNC_RESPONSES) | E_OPT(TN3270E_FUNC_SYSREQ);
	hSession->e_xmit_seq = 0;
	hSession->response_required = TN3270E_RSF_NO_RESPONSE;
#endif

	hSession->need_tls_follows = 0;
	hSession->telnet_state = TNS_DATA;
	hSession->ibptr = hSession->ibuf;

	// clear statistics and flags
	time(&hSession->ns_time);
	hSession->ns_brcvd  = 0;
	hSession->ns_rrcvd  = 0;
	hSession->ns_bsent  = 0;
	hSession->ns_rsent  = 0;
	hSession->syncing   = 0;
	hSession->tn3270e_negotiated = 0;
	hSession->tn3270e_submode = E_NONE;
	hSession->tn3270e_bound = 0;

	setup_lus(hSession);

	check_linemode(hSession,True);

	// write out the passthru hostname and port nubmer
	/*
	if (hSession->passthru_host)
	{
		unsigned char *buffer = (unsigned char *) xs_buffer("%s %d\r\n", hSession->hostname, hSession->current_port);
		hSession->write(hSession, buffer, strlen((char *) buffer));
		lib3270_free(buffer);
		trace_ds(hSession,"SENT HOSTNAME %s:%d\n", hSession->hostname, hSession->current_port);
	}
	*/

	non_blocking(hSession,True);

}

///
/// @brief Connection_complete.
///
/// The connection appears to be complete (output is possible or input
/// appeared ready but recv() returned EWOULDBLOCK).  Complete the
/// connection-completion processing.
///
static void connection_complete(H3270 *session) {
	if (non_blocking(session,False) < 0) {
		host_disconnect(session,True);
		return;
	}
	lib3270_set_connected_initial(session);
	net_connected(session);
}

///	@brief Disconnect from host.
void net_disconnect(H3270 *hSession) {
	if(hSession->xio.write) {
		lib3270_remove_poll(hSession, hSession->xio.write);
		hSession->xio.write = 0;
	}

	hSession->network.module->disconnect(hSession);

	trace_dsn(hSession,"SENT disconnect\n");

	// We're not connected to an LU any more.
	hSession->lu.associated = CN;
	status_lu(hSession,CN);

}

LIB3270_EXPORT void lib3270_data_recv(H3270 *hSession, size_t nr, const unsigned char *netrbuf) {
	register const unsigned char * cp;

//	trace("%s: nr=%d",__FUNCTION__,(int) nr);

	trace_netdata(hSession, '<', netrbuf, nr);

	hSession->ns_brcvd += nr;
	for (cp = netrbuf; cp < (netrbuf + nr); cp++) {
		if(telnet_fsm(hSession,*cp)) {
			(void) ctlr_dbcs_postprocess(hSession);
			host_disconnect(hSession,True);
			return;
		}
	}

#if defined(X3270_ANSI)
	if (IN_ANSI) {
		(void) ctlr_dbcs_postprocess(hSession);
	}

	if (hSession->ansi_data) {
		trace_dsn(hSession,"\n");
		hSession->ansi_data = 0;
	}
#endif // X3270_ANSI
}

/**
 * @brief Called by the toolkit whenever there is input available on the socket.
 *
 * Called by the toolkit whenever there is input available on the
 * socket.  Reads the data, processes the special telnet commands
 * and calls process_ds to process the 3270 data stream.
 *
 * @param hSession	Session handle
 *
 */
void net_input(H3270 *hSession, int GNUC_UNUSED(fd), LIB3270_IO_FLAG GNUC_UNUSED(flag), void GNUC_UNUSED(*dunno)) {
//	register unsigned char	* cp;
	int						  nr;
	unsigned char			  buffer[BUFSZ];

	CHECK_SESSION_HANDLE(hSession);

	memset(buffer,0,BUFSZ);

#if defined(_WIN32)
	for (;;)
#endif
	{
//		if (hSession->connection.sock < 0)
//			return;

#if defined(X3270_ANSI)
		hSession->ansi_data = 0;
#endif

		/*
				if (hSession->ssl.con != NULL)
					nr = SSL_read(hSession->ssl.con, (char *) buffer, BUFSZ);
				else
					nr = hSession->network.module->recv(hSession, buffer, BUFSZ);
		*/

		nr = hSession->network.module->recv(hSession, buffer, BUFSZ);

		debug("%s: recv=%d",__FUNCTION__,nr);

		if (nr < 0) {
			if (nr == -EWOULDBLOCK) {
				return;
			}

			if(HALF_CONNECTED && nr == -EAGAIN) {
				debug("%s: Received a -EAGAIN with half-connect",__FUNCTION__);
				connection_complete(hSession);
				return;
			}

			trace_dsn(hSession,"RCVD socket error %d\n", -nr);

			host_disconnect(hSession,True);
			return;
		} else if (nr == 0) {
			// Host disconnected.
			trace_dsn(hSession,"RCVD disconnect\n");
			host_disconnect(hSession,False);
			return;
		}

		// Process the data.
		if (HALF_CONNECTED) {
			debug("%s: Received a %d with half-connect",__FUNCTION__,nr);
			if (non_blocking(hSession,False) < 0) {
				host_disconnect(hSession,True);
				return;
			}
			lib3270_set_connected_initial(hSession);
			if(net_connected(hSession))
				return;
		}

		lib3270_data_recv(hSession, nr, buffer);

	}

}

/**
 *	@brief Put a 16-bit value in a buffer.
 *	@return The number of bytes required.
 */
static int set16(char *buf, int n) {
	char *b0 = buf;

	n %= 256 * 256;
	if ((n / 256) == IAC)
		*(unsigned char *)buf++ = IAC;
	*buf++ = (n / 256);
	n %= 256;
	if (n == IAC)
		*(unsigned char *)buf++ = IAC;
	*buf++ = n;
	return buf - b0;
}

/*
 * send_naws
 *	Send a Telnet window size sub-option negotation.
 */
static void send_naws(H3270 *hSession) {
	char naws_msg[14];
	int naws_len = 0;

	(void) sprintf(naws_msg, "%c%c%c", IAC, SB, TELOPT_NAWS);
	naws_len += 3;
	naws_len += set16(naws_msg + naws_len, XMIT_COLS);
	naws_len += set16(naws_msg + naws_len, XMIT_ROWS);
	(void) sprintf(naws_msg + naws_len, "%c%c", IAC, SE);
	naws_len += 2;
	net_rawout(hSession,(unsigned char *)naws_msg, naws_len);
	trace_dsn(hSession,"SENT %s NAWS %d %d %s\n", cmd(SB), XMIT_COLS, XMIT_ROWS, cmd(SE));
}

///
/// @brief Advance 'try_lu' to the next desired LU name.
///
static void next_lu(H3270 *hSession) {
	if (hSession->lu.curr != (char **)NULL && (hSession->lu.try = *++hSession->lu.curr) == CN)
		hSession->lu.curr = (char **)NULL;
}

///
///	@brief Telnet finite-state machine.
///
///	@return 0 for okay, -1 for errors.
///
static int telnet_fsm(H3270 *hSession, unsigned char c) {
#if defined(X3270_ANSI) /*[*/
	char	*see_chr;
	int	sl;
#endif /*]*/

	switch (hSession->telnet_state) {
	case TNS_DATA:	/* normal data processing */
		if (c == IAC) {	/* got a telnet command */
			hSession->telnet_state = TNS_IAC;
#if defined(X3270_ANSI) /*[*/
			if (hSession->ansi_data) {
				trace_dsn(hSession,"\n");
				hSession->ansi_data = 0;
			}
#endif /*]*/
			break;
		}
		if (IN_NEITHER) {	/* now can assume ANSI mode */
#if defined(X3270_ANSI)/*[*/
			if (hSession->linemode)
				cooked_init(hSession);
#endif /*]*/
			host_in3270(hSession,LIB3270_CONNECTED_ANSI);
			lib3270_kybdlock_clear(hSession,KL_AWAITING_FIRST);
			status_reset(hSession);
			ps_process(hSession);
		}
		if (IN_ANSI && !IN_E) {
#if defined(X3270_ANSI) /*[*/
			if (!hSession->ansi_data) {
				trace_dsn(hSession,"<.. ");
				hSession->ansi_data = 4;
			}
			see_chr = ctl_see((int) c);
			hSession->ansi_data += (sl = strlen(see_chr));
			if (hSession->ansi_data >= TRACELINE) {
				trace_dsn(hSession," ...\n... ");
				hSession->ansi_data = 4 + sl;
			}
			trace_dsn(hSession,"%s",see_chr);
			if (!hSession->syncing) {
				if (hSession->linemode && hSession->onlcr && c == '\n')
					ansi_process(hSession,(unsigned int) '\r');
				ansi_process(hSession,(unsigned int) c);
			}
#endif /*]*/
		} else {
			store3270in(hSession,c);
		}
		break;
	case TNS_IAC:	/* process a telnet command */
		if (c != EOR && c != IAC) {
			trace_dsn(hSession,"RCVD %s ", cmd(c));
		}

		switch (c) {
		case IAC:	/* escaped IAC, insert it */
			if (IN_ANSI && !IN_E) {
#if defined(X3270_ANSI) /*[*/
				if (!hSession->ansi_data) {
					trace_dsn(hSession,"<.. ");
					hSession->ansi_data = 4;
				}
				see_chr = ctl_see((int) c);
				hSession->ansi_data += (sl = strlen(see_chr));
				if (hSession->ansi_data >= TRACELINE) {
					trace_dsn(hSession," ...\n ...");
					hSession->ansi_data = 4 + sl;
				}
				trace_dsn(hSession,"%s",see_chr);
				ansi_process(hSession,(unsigned int) c);
#endif /*]*/
			} else {
				store3270in(hSession,c);
			}
			hSession->telnet_state = TNS_DATA;
			break;

		case EOR:	/* eor, process accumulated input */

			if (IN_3270 || (IN_E && hSession->tn3270e_negotiated)) {
				hSession->ns_rrcvd++;
				if (process_eor(hSession))
					return -1;
			} else {
				lib3270_popup_dialog(
				    hSession,
				    LIB3270_NOTIFY_WARNING,_("Warning"),
				    _( "EOR received when not in 3270 mode, ignored." ),
				    NULL
				);
			}
			trace_dsn(hSession,"RCVD EOR\n");
			hSession->ibptr = hSession->ibuf;
			hSession->telnet_state = TNS_DATA;
			break;

		case WILL:
			hSession->telnet_state = TNS_WILL;
			break;

		case WONT:
			hSession->telnet_state = TNS_WONT;
			break;

		case DO:
			hSession->telnet_state = TNS_DO;
			break;

		case DONT:
			hSession->telnet_state = TNS_DONT;
			break;

		case SB:
			hSession->telnet_state = TNS_SB;
			if (hSession->sbbuf == (unsigned char *)NULL)
				hSession->sbbuf = (unsigned char *)lib3270_malloc(1024);
			hSession->sbptr = hSession->sbbuf;
			break;

		case DM:
			trace_dsn(hSession,"\n");
			if (hSession->syncing) {
				hSession->syncing = 0;
				x_except_on(hSession);
			}
			hSession->telnet_state = TNS_DATA;
			break;

		case GA:
		case NOP:
			trace_dsn(hSession,"\n");
			hSession->telnet_state = TNS_DATA;
			break;

		default:
			trace_dsn(hSession,"???\n");
			hSession->telnet_state = TNS_DATA;
			break;
		}
		break;
	case TNS_WILL:	/* telnet WILL DO OPTION command */
		trace_dsn(hSession,"%s\n", opt(c));
		switch (c) {
		case TELOPT_SGA:
		case TELOPT_BINARY:
		case TELOPT_EOR:
		case TELOPT_TTYPE:
		case TELOPT_ECHO:
#if defined(X3270_TN3270E)
		case TELOPT_TN3270E:
#endif
			if (c != TELOPT_TN3270E || !hSession->non_tn3270e_host) {
				if (!hSession->hisopts[c]) {
					hSession->hisopts[c] = 1;
					do_opt[2] = c;
					net_rawout(hSession,do_opt, sizeof(do_opt));
					trace_dsn(hSession,"SENT %s %s\n",
					          cmd(DO), opt(c));

					/*
					 * For UTS, volunteer to do EOR when
					 * they do.
					 */
					if (c == TELOPT_EOR && !hSession->myopts[c]) {
						hSession->myopts[c] = 1;
						will_opt[2] = c;
						net_rawout(hSession,will_opt,sizeof(will_opt));
						trace_dsn(hSession,"SENT %s %s\n",cmd(WILL), opt(c));
					}

					check_in3270(hSession);
					check_linemode(hSession,False);
				}
				break;
			}

		default:
			dont_opt[2] = c;
			net_rawout(hSession,dont_opt, sizeof(dont_opt));
			trace_dsn(hSession,"SENT %s %s\n", cmd(DONT), opt(c));
			break;
		}
		hSession->telnet_state = TNS_DATA;
		break;
	case TNS_WONT:	/* telnet WONT DO OPTION command */
		trace_dsn(hSession,"%s\n", opt(c));
		if (hSession->hisopts[c]) {
			hSession->hisopts[c] = 0;
			dont_opt[2] = c;
			net_rawout(hSession, dont_opt, sizeof(dont_opt));
			trace_dsn(hSession,"SENT %s %s\n", cmd(DONT), opt(c));
			check_in3270(hSession);
			check_linemode(hSession,False);
		}

		hSession->telnet_state = TNS_DATA;
		break;
	case TNS_DO:	/* telnet PLEASE DO OPTION command */
		trace_dsn(hSession,"%s\n", opt(c));
		switch (c) {
		case TELOPT_BINARY:
		case TELOPT_EOR:
		case TELOPT_TTYPE:
		case TELOPT_SGA:
		case TELOPT_NAWS:
		case TELOPT_TM:
#if defined(X3270_TN3270E) /*[*/
		case TELOPT_TN3270E:
#endif /*]*/
		case TELOPT_STARTTLS:
			if (c == TELOPT_TN3270E && hSession->non_tn3270e_host)
				goto wont;
			if (c == TELOPT_TM && !hSession->bsd_tm)
				goto wont;

			trace("hSession->myopts[c]=%d",hSession->myopts[c]);
			if (!hSession->myopts[c]) {
				if (c != TELOPT_TM)
					hSession->myopts[c] = 1;
				will_opt[2] = c;
				net_rawout(hSession, will_opt, sizeof(will_opt));
				trace_dsn(hSession,"SENT %s %s\n", cmd(WILL), opt(c));
				check_in3270(hSession);
				check_linemode(hSession,False);
			}
			if (c == TELOPT_NAWS)
				send_naws(hSession);
			if (c == TELOPT_STARTTLS) {
				static unsigned char follows_msg[] = {
					IAC, SB, TELOPT_STARTTLS,
					TLS_FOLLOWS, IAC, SE
				};

				//
				// Send IAC SB STARTTLS FOLLOWS IAC SE
				// to announce that what follows is TLS.
				//
				net_rawout(hSession, follows_msg, sizeof(follows_msg));
				trace_dsn(hSession,"SENT %s %s FOLLOWS %s\n",
				          cmd(SB),
				          opt(TELOPT_STARTTLS),
				          cmd(SE));

				debug("%s: %s requires TLS/SSL",__FUNCTION__,opt(TELOPT_STARTTLS));
				hSession->need_tls_follows = 1;
			}
			break;

		default:
wont:
			wont_opt[2] = c;
			net_rawout(hSession, wont_opt, sizeof(wont_opt));
			trace_dsn(hSession,"SENT %s %s\n", cmd(WONT), opt(c));
			break;
		}
		hSession->telnet_state = TNS_DATA;
		break;
	case TNS_DONT:	/* telnet PLEASE DON'T DO OPTION command */
		trace_dsn(hSession,"%s\n", opt(c));
		if (hSession->myopts[c]) {
			hSession->myopts[c] = 0;
			wont_opt[2] = c;
			net_rawout(hSession, wont_opt, sizeof(wont_opt));
			trace_dsn(hSession,"SENT %s %s\n", cmd(WONT), opt(c));
			check_in3270(hSession);
			check_linemode(hSession,False);
		}
		hSession->telnet_state = TNS_DATA;
		break;
	case TNS_SB:	// telnet sub-option string command
		if (c == IAC)
			hSession->telnet_state = TNS_SB_IAC;
		else
			*hSession->sbptr++ = c;
		break;
	case TNS_SB_IAC:	// telnet sub-option string command
		*hSession->sbptr++ = c;
		if (c == SE) {
			hSession->telnet_state = TNS_DATA;
			if (hSession->sbbuf[0] == TELOPT_TTYPE && hSession->sbbuf[1] == TELQUAL_SEND) {
				int tt_len, tb_len;
				char *tt_out;

				trace_dsn(hSession,"%s %s\n", opt(hSession->sbbuf[0]),telquals[hSession->sbbuf[1]]);

				if (hSession->lu.names != (char **)NULL && hSession->lu.try == CN) {
					// None of the LUs worked.
					popup_an_error(hSession, _( "Cannot connect to specified LU" ) );
					return -1;
				}

				tt_len = strlen(hSession->termtype);
				if (hSession->lu.try != CN && *hSession->lu.try) {
					tt_len += strlen(hSession->lu.try) + 1;
					hSession->lu.associated = hSession->lu.try;
				} else {
					hSession->lu.associated = CN;
				}

				status_lu(hSession,hSession->lu.associated);

				tb_len = 4 + tt_len + 2;
				tt_out = lib3270_malloc(tb_len + 1);
				(void) sprintf(tt_out, "%c%c%c%c%s%s%s%c%c",
				               IAC, SB, TELOPT_TTYPE, TELQUAL_IS,
				               hSession->termtype,
				               (hSession->lu.try != CN && *hSession->lu.try) ? "@" : "",
				               (hSession->lu.try != CN && *hSession->lu.try) ? hSession->lu.try : "",
				               IAC, SE);
				net_rawout(hSession, (unsigned char *)tt_out, tb_len);

				debug("\n\n\ntermtype=%s lu.try=[%s] - [%s]\n\n\n",
				      hSession->termtype,
				      (hSession->lu.try != CN && *hSession->lu.try) ? "@" : "",
				      (hSession->lu.try != CN && *hSession->lu.try) ? hSession->lu.try : ""
				     );

				trace_dsn(hSession,"SENT %s %s %s %.*s %s\n",
				          cmd(SB), opt(TELOPT_TTYPE),
				          telquals[TELQUAL_IS],
				          tt_len, tt_out + 4,
				          cmd(SE));
				lib3270_free(tt_out);

				/* Advance to the next LU name. */
				next_lu(hSession);
			}
#if defined(X3270_TN3270E) /*[*/
			else if (hSession->myopts[TELOPT_TN3270E] && hSession->sbbuf[0] == TELOPT_TN3270E) {
				if (tn3270e_negotiate(hSession))
					return -1;
			}
#endif /*]*/
			else if (hSession->need_tls_follows && hSession->myopts[TELOPT_STARTTLS] && hSession->sbbuf[0] == TELOPT_STARTTLS) {
				continue_tls(hSession,hSession->sbbuf, hSession->sbptr - hSession->sbbuf);
			}

		} else {
			hSession->telnet_state = TNS_SB;
		}
		break;
	}
	return 0;
}

/// @brief Process a STARTTLS subnegotiation.
static void continue_tls(H3270 *hSession, unsigned char *sbbuf, int len) {
	// Whatever happens, we're not expecting another SB STARTTLS.
	hSession->need_tls_follows = 0;

	// Make sure the option is FOLLOWS.
	if (len < 2 || sbbuf[1] != TLS_FOLLOWS) {
		/* Trace the junk. */
		trace_dsn(hSession,"%s ? %s\n", opt(TELOPT_STARTTLS), cmd(SE));
		popup_an_error(hSession,_( "TLS negotiation failure"));
		net_disconnect(hSession);
		return;
	}

	// Trace what we got.
	trace_dsn(hSession,"%s FOLLOWS %s\n", opt(TELOPT_STARTTLS), cmd(SE));

	hSession->ssl.host = 1;	// Set host type as SSL.
	if(lib3270_start_tls(hSession)) {
		lib3270_disconnect(hSession);
		return;
	}

	lib3270_notify_tls(hSession);

}

#if defined(X3270_TN3270E) /*[*/
/// @brief Send a TN3270E terminal type request.
static void tn3270e_request(H3270 *hSession) {
	int tt_len, tb_len;
	char *tt_out;
	char *t;

	tt_len = strlen(hSession->termtype);
	if (hSession->lu.try != CN && *hSession->lu.try)
		tt_len += strlen(hSession->lu.try) + 1;

	tb_len = 5 + tt_len + 2;
	tt_out = lib3270_malloc(tb_len + 1);
	t = tt_out;
	t += sprintf(tt_out, "%c%c%c%c%c%s",
	             IAC, SB, TELOPT_TN3270E, TN3270E_OP_DEVICE_TYPE,
	             TN3270E_OP_REQUEST, hSession->termtype);

	/* Convert 3279 to 3278, per the RFC. */
	if (tt_out[12] == '9')
		tt_out[12] = '8';

	if (hSession->lu.try != CN && *hSession->lu.try)
		t += sprintf(t, "%c%s", TN3270E_OP_CONNECT, hSession->lu.try);

	(void) sprintf(t, "%c%c", IAC, SE);

	net_rawout(hSession, (unsigned char *)tt_out, tb_len);

	trace_dsn(
	    hSession,"SENT %s %s DEVICE-TYPE REQUEST %.*s%s%s %s\n",
	    cmd(SB),
	    opt(TELOPT_TN3270E),
	    (int) strlen(hSession->termtype),
	    tt_out + 5,
	    (hSession->lu.try != CN && *hSession->lu.try) ? " CONNECT " : "",
	    (hSession->lu.try != CN && *hSession->lu.try) ? hSession->lu.try : "",
	    cmd(SE)
	);

	lib3270_free(tt_out);
}

/*
 * Back off of TN3270E.
 */
static void backoff_tn3270e(H3270 *hSession, const char *why) {
	trace_dsn(hSession,"Aborting TN3270E: %s\n", why);

	/* Tell the host 'no'. */
	wont_opt[2] = TELOPT_TN3270E;
	net_rawout(hSession, wont_opt, sizeof(wont_opt));
	trace_dsn(hSession,"SENT %s %s\n", cmd(WONT), opt(TELOPT_TN3270E));

	/* Restore the LU list; we may need to run it again in TN3270 mode. */
	setup_lus(hSession);

	/* Reset our internal state. */
	hSession->myopts[TELOPT_TN3270E] = 0;
	check_in3270(hSession);
}

/*
 * Negotiation of TN3270E options.
 * Returns 0 if okay, -1 if we have to give up altogether.
 */
static int tn3270e_negotiate(H3270 *hSession) {
//	#define LU_MAX	32
//	static char reported_lu[LU_MAX+1];
//	static char reported_type[LU_MAX+1];

	int sblen;
	unsigned long e_rcvd;

	/* Find out how long the subnegotiation buffer is. */
	for (sblen = 0; ; sblen++) {
		if (hSession->sbbuf[sblen] == SE)
			break;
	}

	trace_dsn(hSession,"TN3270E ");

	switch (hSession->sbbuf[1]) {

	case TN3270E_OP_SEND:

		if (hSession->sbbuf[2] == TN3270E_OP_DEVICE_TYPE) {

			/* Host wants us to send our device type. */
			trace_dsn(hSession,"SEND DEVICE-TYPE SE\n");

			tn3270e_request(hSession);
		} else {
			trace_dsn(hSession,"SEND ??%u SE\n", hSession->sbbuf[2]);
		}
		break;

	case TN3270E_OP_DEVICE_TYPE:

		/* Device type negotiation. */
		trace_dsn(hSession,"DEVICE-TYPE ");

		switch (hSession->sbbuf[2]) {
		case TN3270E_OP_IS: {
			int tnlen, snlen;

			/* Device type success. */

			/* Isolate the terminal type and session. */
			tnlen = 0;
			while (hSession->sbbuf[3+tnlen] != SE &&
			        hSession->sbbuf[3+tnlen] != TN3270E_OP_CONNECT)
				tnlen++;
			snlen = 0;
			if (hSession->sbbuf[3+tnlen] == TN3270E_OP_CONNECT) {
				while(hSession->sbbuf[3+tnlen+1+snlen] != SE)
					snlen++;
			}
			trace_dsn(hSession,"IS %.*s CONNECT %.*s SE\n",
			          tnlen, &hSession->sbbuf[3],
			          snlen, &hSession->sbbuf[3+tnlen+1]);

			/* Remember the LU. */
			if (tnlen) {
				if (tnlen > LIB3270_LU_MAX)
					tnlen = LIB3270_LU_MAX;
				(void)strncpy(hSession->reported_type,(char *)&hSession->sbbuf[3], tnlen);
				hSession->reported_type[tnlen] = '\0';
				hSession->connected_type = hSession->reported_type;
			}
			if (snlen) {
				if (snlen > LIB3270_LU_MAX)
					snlen = LIB3270_LU_MAX;
				(void)strncpy(hSession->lu.reported,(char *)&hSession->sbbuf[3+tnlen+1], snlen);
				hSession->lu.reported[snlen] = '\0';
				hSession->lu.associated = hSession->lu.reported;
				status_lu(hSession,hSession->lu.associated);
			}

			/* Tell them what we can do. */
			tn3270e_subneg_send(hSession, TN3270E_OP_REQUEST, hSession->e_funcs);
			break;
		}
		case TN3270E_OP_REJECT:

			/* Device type failure. */

			trace_dsn(hSession,"REJECT REASON %s SE\n", rsn(hSession->sbbuf[4]));
			if (hSession->sbbuf[4] == TN3270E_REASON_INV_DEVICE_TYPE ||
			        hSession->sbbuf[4] == TN3270E_REASON_UNSUPPORTED_REQ) {
				backoff_tn3270e(hSession,_( "Host rejected device type or request type" ));
				break;
			}

			next_lu(hSession);
			if (hSession->lu.try != CN) {
				// Try the next LU.
				tn3270e_request(hSession);
			} else if (hSession->lu.names != (char **)NULL) {
				// No more LUs to try.  Give up.
				backoff_tn3270e(hSession,_("Host rejected resource(s)"));
			} else {
				backoff_tn3270e(hSession,_("Device type rejected"));
			}

			break;
		default:
			trace_dsn(hSession,"??%u SE\n", hSession->sbbuf[2]);
			break;
		}
		break;

	case TN3270E_OP_FUNCTIONS:

		/* Functions negotiation. */
		trace_dsn(hSession,"FUNCTIONS ");

		switch (hSession->sbbuf[2]) {

		case TN3270E_OP_REQUEST:

			/* Host is telling us what functions they want. */
			trace_dsn(hSession,"REQUEST %s SE\n",tn3270e_function_names(hSession->sbbuf+3, sblen-3));

			e_rcvd = tn3270e_fdecode(hSession->sbbuf+3, sblen-3);
			if ((e_rcvd == hSession->e_funcs) || (hSession->e_funcs & ~e_rcvd)) {
				/* They want what we want, or less.  Done. */
				hSession->e_funcs = e_rcvd;
				tn3270e_subneg_send(hSession, TN3270E_OP_IS, hSession->e_funcs);
				hSession->tn3270e_negotiated = 1;
				trace_dsn(hSession,"TN3270E option negotiation complete.\n");
				check_in3270(hSession);
			} else {
				/*
				 * They want us to do something we can't.
				 * Request the common subset.
				 */
				hSession->e_funcs &= e_rcvd;
				tn3270e_subneg_send(hSession, TN3270E_OP_REQUEST,hSession->e_funcs);
			}
			break;

		case TN3270E_OP_IS:

			/* They accept our last request, or a subset thereof. */
			trace_dsn(hSession,"IS %s SE\n",tn3270e_function_names(hSession->sbbuf+3, sblen-3));
			e_rcvd = tn3270e_fdecode(hSession->sbbuf+3, sblen-3);
			if (e_rcvd != hSession->e_funcs) {
				if (hSession->e_funcs & ~e_rcvd) {
					/*
					 * They've removed something.  This is
					 * technically illegal, but we can
					 * live with it.
					 */
					hSession->e_funcs = e_rcvd;
				} else {
					/*
					 * They've added something.  Abandon
					 * TN3270E, they're brain dead.
					 */
					backoff_tn3270e(hSession,_( "Host illegally added function(s)" ));
					break;
				}
			}
			hSession->tn3270e_negotiated = 1;
			trace_dsn(hSession,"TN3270E option negotiation complete.\n");
			check_in3270(hSession);
			break;

		default:
			trace_dsn(hSession,"??%u SE\n", hSession->sbbuf[2]);
			break;
		}
		break;

	default:
		trace_dsn(hSession,"??%u SE\n", hSession->sbbuf[1]);
	}

	/* Good enough for now. */
	return 0;
}

#if defined(X3270_TRACE) /*[*/
/* Expand a string of TN3270E function codes into text. */
static const char *
tn3270e_function_names(const unsigned char *buf, int len) {
	int i;
	static char text_buf[1024];
	char *s = text_buf;

	if (!len)
		return("(null)");
	for (i = 0; i < len; i++) {
		s += sprintf(s, "%s%s", (s == text_buf) ? "" : " ",
		             fnn(buf[i]));
	}
	return text_buf;
}
#endif /*]*/

/* Transmit a TN3270E FUNCTIONS REQUEST or FUNCTIONS IS message. */
static void tn3270e_subneg_send(H3270 *hSession, unsigned char op, unsigned long funcs) {
	unsigned char proto_buf[7 + 32];
	int proto_len;
	int i;

	/* Construct the buffers. */
	(void) memcpy(proto_buf, functions_req, 4);
	proto_buf[4] = op;
	proto_len = 5;
	for (i = 0; i < 32; i++) {
		if (funcs & E_OPT(i))
			proto_buf[proto_len++] = i;
	}

	/* Complete and send out the protocol message. */
	proto_buf[proto_len++] = IAC;
	proto_buf[proto_len++] = SE;
	net_rawout(hSession, proto_buf, proto_len);

	/* Complete and send out the trace text. */
	trace_dsn(hSession,"SENT %s %s FUNCTIONS %s %s %s\n",
	          cmd(SB), opt(TELOPT_TN3270E),
	          (op == TN3270E_OP_REQUEST)? "REQUEST": "IS",
	          tn3270e_function_names(proto_buf + 5, proto_len - 7),
	          cmd(SE));
}

/* Translate a string of TN3270E functions into a bit-map. */
static unsigned long
tn3270e_fdecode(const unsigned char *buf, int len) {
	unsigned long r = 0L;
	int i;

	/* Note that this code silently ignores options >= 32. */
	for (i = 0; i < len; i++) {
		if (buf[i] < 32)
			r |= E_OPT(buf[i]);
	}
	return r;
}
#endif /*]*/

#if defined(X3270_TN3270E) /*[*/
static void process_bind(H3270 *hSession, unsigned char *buf, int buflen) {
	int namelen, i;

	(void) memset(hSession->plu_name, '\0', sizeof(hSession->plu_name));

	/* Make sure it's a BIND. */
	if (buflen < 1 || buf[0] != BIND_RU) {
		return;
	}
	buf++;
	buflen--;

	/* Extract the PLU name. */
	if (buflen < BIND_OFF_PLU_NAME + BIND_PLU_NAME_MAX)
		return;
	namelen = buf[BIND_OFF_PLU_NAME_LEN];
	if (namelen > BIND_PLU_NAME_MAX)
		namelen = BIND_PLU_NAME_MAX;
	for (i = 0; i < namelen; i++) {
		hSession->plu_name[i] = hSession->charset.ebc2asc[buf[BIND_OFF_PLU_NAME + i]];
	}
}
#endif /*]*/

static int process_eor(H3270 *hSession) {

	trace("%s: syncing=%s",__FUNCTION__,hSession->syncing ? "Yes" : "No");

	if (hSession->syncing || !(hSession->ibptr - hSession->ibuf))
		return(0);

#if defined(X3270_TN3270E) /*[*/
	if (IN_E) {
		tn3270e_header *h = (tn3270e_header *) hSession->ibuf;
		unsigned char *s;
		enum pds rv;

		trace_dsn(hSession,"RCVD TN3270E(%s%s %s %u)\n",
		          e_dt(h->data_type),
		          e_rq(h->data_type, h->request_flag),
		          e_rsp(h->data_type, h->response_flag),
		          h->seq_number[0] << 8 | h->seq_number[1]);

		switch (h->data_type) {
		case TN3270E_DT_3270_DATA:
			if ((hSession->e_funcs & E_OPT(TN3270E_FUNC_BIND_IMAGE)) &&
			        !hSession->tn3270e_bound)
				return 0;
			hSession->tn3270e_submode = E_3270;
			check_in3270(hSession);
			hSession->response_required = h->response_flag;
			rv = process_ds(hSession, hSession->ibuf + EH_SIZE,(hSession->ibptr - hSession->ibuf) - EH_SIZE);
			if (rv < 0 &&
			        hSession->response_required != TN3270E_RSF_NO_RESPONSE)
				tn3270e_nak(hSession,rv);
			else if (rv == PDS_OKAY_NO_OUTPUT &&
			         hSession->response_required == TN3270E_RSF_ALWAYS_RESPONSE)
				tn3270e_ack(hSession);
			hSession->response_required = TN3270E_RSF_NO_RESPONSE;
			return 0;
		case TN3270E_DT_BIND_IMAGE:
			if (!(hSession->e_funcs & E_OPT(TN3270E_FUNC_BIND_IMAGE)))
				return 0;
			process_bind(hSession, hSession->ibuf + EH_SIZE, (hSession->ibptr - hSession->ibuf) - EH_SIZE);
			trace_dsn(hSession,"< BIND PLU-name '%s'\n", hSession->plu_name);
			hSession->tn3270e_bound = 1;
			check_in3270(hSession);
			return 0;
		case TN3270E_DT_UNBIND:
			if (!(hSession->e_funcs & E_OPT(TN3270E_FUNC_BIND_IMAGE)))
				return 0;
			hSession->tn3270e_bound = 0;
			if (hSession->tn3270e_submode == E_3270)
				hSession->tn3270e_submode = E_NONE;
			check_in3270(hSession);
			return 0;
		case TN3270E_DT_NVT_DATA:
			/* In tn3270e NVT mode */
			hSession->tn3270e_submode = E_NVT;
			check_in3270(hSession);
			for (s = hSession->ibuf; s < hSession->ibptr; s++) {
				ansi_process(hSession,*s++);
			}
			return 0;
		case TN3270E_DT_SSCP_LU_DATA:
			if (!(hSession->e_funcs & E_OPT(TN3270E_FUNC_BIND_IMAGE)))
				return 0;
			hSession->tn3270e_submode = E_SSCP;
			check_in3270(hSession);
			ctlr_write_sscp_lu(hSession, hSession->ibuf + EH_SIZE,(hSession->ibptr - hSession->ibuf) - EH_SIZE);
			return 0;
		default:
			/* Should do something more extraordinary here. */
			return 0;
		}
	} else
#endif /*]*/
	{
		(void) process_ds(hSession, hSession->ibuf, hSession->ibptr - hSession->ibuf);
	}
	return 0;
}

/// @brief Called when there is an exceptional condition on the socket.
void net_exception(H3270 *session, int GNUC_UNUSED(fd), LIB3270_IO_FLAG GNUC_UNUSED(flag), void GNUC_UNUSED(*dunno)) {
	debug("%s",__FUNCTION__);

	trace_dsn(session,"RCVD urgent data indication\n");
	if (!session->syncing) {
		session->syncing = 1;

		if(session->xio.except) {
			lib3270_remove_poll(session, session->xio.except);
			session->xio.except = NULL;
		}
	}
}

/**
 *	@brief send a 3270 record
 *
 * Flavors of Network Output:
 *
 * 3270 mode
 *
 *  ANSI mode; call each other in turn
 *	net_sendc	net_cookout for 1 byte
 *	net_sends	net_cookout for a null-terminated string
 *	net_cookout	send user data with cooked-mode processing, ANSI mode
 *	net_cookedout	send user data, ANSI mode, already cooked
 *	net_rawout	send telnet protocol data, ANSI mode
 *
 */

LIB3270_INTERNAL int lib3270_sock_send(H3270 *hSession, unsigned const char *buf, int len) {
	int rc = hSession->network.module->send(hSession, buf, len);

	if(rc > 0)
		return rc;

	// Send error, notify
	trace_dsn(hSession,"SND socket error %d\n", -rc);

	return -1;
}

/**
 * @brief Send out raw telnet data.
 *
 * We assume that there will always be enough space to buffer what we want to transmit,
 * so we don't handle EAGAIN or EWOULDBLOCK.
 *
 * @param hSession	Session handle.
 * @param buf		Buffer to send.
 * @param len		Buffer length
 *
 */
static void net_rawout(H3270 *hSession, unsigned const char *buf, size_t len) {
	trace_netdata(hSession, '>', buf, len);

	while (len) {
		int nw = lib3270_sock_send(hSession,buf,len);

		if (nw > 0) {
			// Data sent
			hSession->ns_bsent += nw;
			len -= nw;
			buf += nw;
		} else if(nw < 0) {
			host_disconnect(hSession,True);
			return;
		}
	}
}

#if defined(X3270_ANSI)

/**
 * @brief Send user data out in ANSI mode, without cooked-mode processing.
 */
static void net_cookedout(H3270 *hSession, const char *buf, int len) {

	if (lib3270_get_toggle(hSession,LIB3270_TOGGLE_DS_TRACE)) {
		int i;

		trace_dsn(hSession,">");
		for (i = 0; i < len; i++)
			trace_dsn(hSession," %s", ctl_see((int) *(buf+i)));
		trace_dsn(hSession,"\n");
	}

	net_rawout(hSession,(unsigned const char *) buf, len);
}

/***
 * @brief Send output in ANSI mode, including cooked-mode processing if appropriate.
 */
static void net_cookout(H3270 *hSession, const char *buf, int len) {

	if (!IN_ANSI || (hSession->kybdlock & KL_AWAITING_FIRST))
		return;

	if (hSession->linemode) {
		register int	i;
		char	c;

		for (i = 0; i < len; i++) {
			c = buf[i];

			/* Input conversions. */
			if (!hSession->lnext && c == '\r' && hSession->icrnl)
				c = '\n';
			else if (!hSession->lnext && c == '\n' && hSession->inlcr)
				c = '\r';

			/* Backslashes. */
			if (c == '\\' && !hSession->backslashed)
				hSession->backslashed = 1;
			else
				hSession->backslashed = 0;

			/* Control chars. */
			if (c == '\n')
				do_eol(hSession,c);
			else if (c == ansictl.vintr)
				do_intr(hSession, c);
			else if (c == ansictl.vquit)
				do_quit(hSession,c);
			else if (c == ansictl.verase)
				do_cerase(hSession,c);
			else if (c == ansictl.vkill)
				do_kill(hSession,c);
			else if (c == ansictl.vwerase)
				do_werase(hSession,c);
			else if (c == ansictl.vrprnt)
				do_rprnt(hSession,c);
			else if (c == ansictl.veof)
				do_eof(hSession,c);
			else if (c == ansictl.vlnext)
				do_lnext(hSession,c);
			else if (c == 0x08 || c == 0x7f) /* Yes, a hack. */
				do_cerase(hSession,c);
			else
				do_data(hSession,c);
		}
		return;
	} else
		net_cookedout(hSession, buf, len);
}

/**
 * @brief Cooked mode input processing.
 */

static void cooked_init(H3270 *hSession) {
	if (hSession->lbuf == (unsigned char *)NULL)
		hSession->lbuf = (unsigned char *)lib3270_malloc(BUFSZ);
	hSession->lbptr = hSession->lbuf;
	hSession->lnext = 0;
	hSession->backslashed = 0;
}

static void ansi_process_s(H3270 *hSession, const char *data) {
	while (*data)
		ansi_process(hSession,(unsigned int) *data++);
}

static void forward_data(H3270 *hSession) {
	net_cookedout(hSession, (char *) hSession->lbuf, hSession->lbptr - hSession->lbuf);
	cooked_init(hSession);
}

static void do_data(H3270 *hSession, char c) {
	if (hSession->lbptr+1 < hSession->lbuf + BUFSZ) {
		*hSession->lbptr++ = c;
		if (c == '\r')
			*hSession->lbptr++ = '\0';
		if (c == '\t')
			ansi_process(hSession,(unsigned int) c);
		else
			ansi_process_s(hSession,ctl_see((int) c));
	} else
		ansi_process_s(hSession,"\007");

	hSession->lnext = 0;
	hSession->backslashed = 0;
}

static void do_intr(H3270 *hSession, char c) {
	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}
	ansi_process_s(hSession,ctl_see((int) c));
	cooked_init(hSession);
	net_interrupt(hSession);
}

static void do_quit(H3270 *hSession, char c) {
	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}
	ansi_process_s(hSession,ctl_see((int) c));
	cooked_init(hSession);
	net_break(hSession);
}

static void do_cerase(H3270 *hSession, char c) {
	int len;

	if (hSession->backslashed) {
		hSession->lbptr--;
		ansi_process_s(hSession,"\b");
		do_data(hSession,c);
		return;
	}

	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}

	if (hSession->lbptr > hSession->lbuf) {
		len = strlen(ctl_see((int) *--hSession->lbptr));

		while (len--)
			ansi_process_s(hSession,"\b \b");
	}
}

static void do_werase(H3270 *hSession, char c) {
	int any = 0;
	int len;

	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}

	while (hSession->lbptr > hSession->lbuf) {
		char ch;

		ch = *--hSession->lbptr;

		if (ch == ' ' || ch == '\t') {
			if (any) {
				++hSession->lbptr;
				break;
			}
		} else
			any = 1;
		len = strlen(ctl_see((int) ch));

		while (len--)
			ansi_process_s(hSession,"\b \b");
	}
}

static void do_kill(H3270 *hSession, char c) {
	int i, len;

	if (hSession->backslashed) {
		hSession->lbptr--;
		ansi_process_s(hSession,"\b");
		do_data(hSession,c);
		return;
	}

	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}

	while (hSession->lbptr > hSession->lbuf) {
		len = strlen(ctl_see((int) *--hSession->lbptr));

		for (i = 0; i < len; i++)
			ansi_process_s(hSession,"\b \b");
	}
}

static void do_rprnt(H3270 *hSession, char c) {
	unsigned char *p;

	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}

	ansi_process_s(hSession,ctl_see((int) c));
	ansi_process_s(hSession,"\r\n");
	for (p = hSession->lbuf; p < hSession->lbptr; p++)
		ansi_process_s(hSession,ctl_see((int) *p));
}

static void do_eof(H3270 *hSession, char c) {
	if (hSession->backslashed) {
		hSession->lbptr--;
		ansi_process_s(hSession,"\b");
		do_data(hSession,c);
		return;
	}

	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}

	do_data(hSession,c);
	forward_data(hSession);
}

static void do_eol(H3270 *hSession, char c) {
	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}

	if (hSession->lbptr+2 >= hSession->lbuf + BUFSZ) {
		ansi_process_s(hSession,"\007");
		return;
	}

	*hSession->lbptr++ = '\r';
	*hSession->lbptr++ = '\n';
	ansi_process_s(hSession,"\r\n");
	forward_data(hSession);
}

static void do_lnext(H3270 *hSession, char c) {
	if (hSession->lnext) {
		do_data(hSession,c);
		return;
	}
	hSession->lnext = 1;
	ansi_process_s(hSession,"^\b");
}
#endif /*]*/


const char * lib3270_connection_state_get_name(const LIB3270_CSTATE cstate) {
	static const char *state_names[] = {
		N_("Unconnected"),
		N_("Resolving"),
		N_("Pending"),
		N_("Connected initial"),
		N_("TN3270 NVT"),
		N_("TN3270 3270"),
		N_("TN3270E"),
		N_("TN3270E NVT"),
		N_("TN3270E SSCP-LU"),
		N_("TN3270E 3270")
	};

	if(cstate > (sizeof(state_names)/sizeof(state_names[0])))
		return _("Unknown");

	return dgettext(GETTEXT_PACKAGE,state_names[cstate]);
}

LIB3270_EXPORT const char * lib3270_state_get_name(const LIB3270_STATE state) {
	static const char *state_names[] = {
		N_("Resolving"),
		N_("Connecting"),
		N_("Half connect"),
		N_("Connect"),
		N_("3270 Mode"),
		N_("Line mode"),
		N_("Remodel"),
		N_("Printer"),
		N_("Exiting"),
		N_("Charset"),
	};

	if(state > (sizeof(state_names)/sizeof(state_names[0])))
		return _("Unknown");

	return dgettext(GETTEXT_PACKAGE,state_names[state]);

}


/**
 * Check for switches between NVT, SSCP-LU and 3270 modes.
 *
 * @param hSession	Session handle.
 *
 */
static void check_in3270(H3270 *hSession) {
	LIB3270_CSTATE new_cstate = LIB3270_NOT_CONNECTED;


#if defined(X3270_TN3270E) /*[*/
	if (hSession->myopts[TELOPT_TN3270E]) {
		if (!hSession->tn3270e_negotiated)
			new_cstate = LIB3270_CONNECTED_INITIAL_E;
		else switch (hSession->tn3270e_submode) {
			case E_NONE:
				new_cstate = LIB3270_CONNECTED_INITIAL_E;
				break;
			case E_NVT:
				new_cstate = LIB3270_CONNECTED_NVT;
				break;
			case E_3270:
				new_cstate = LIB3270_CONNECTED_TN3270E;
				break;
			case E_SSCP:
				new_cstate = LIB3270_CONNECTED_SSCP;
				break;
			}
	} else
#endif /*]*/
		if (hSession->myopts[TELOPT_BINARY] &&
		        hSession->myopts[TELOPT_EOR] &&
		        hSession->myopts[TELOPT_TTYPE] &&
		        hSession->hisopts[TELOPT_BINARY] &&
		        hSession->hisopts[TELOPT_EOR]) {
			new_cstate = LIB3270_CONNECTED_3270;
		} else if (hSession->connection.state == LIB3270_CONNECTED_INITIAL) {
			/* Nothing has happened, yet. */
			return;
		} else {
			new_cstate = LIB3270_CONNECTED_ANSI;
		}

	if (new_cstate != hSession->connection.state) {
#if defined(X3270_TN3270E) /*[*/
		int was_in_e = IN_E;
#endif /*]*/

#if defined(X3270_TN3270E)
		//
		// If we've now switched between non-TN3270E mode and
		// TN3270E mode, reset the LU list so we can try again
		// in the new mode.
		//
		if (hSession->lu.names != (char **)NULL && was_in_e != IN_E) {
			hSession->lu.curr = hSession->lu.names;
			hSession->lu.try = *hSession->lu.curr;
		}
#endif

		// Allocate the initial 3270 input buffer.
		if(new_cstate >= LIB3270_CONNECTED_INITIAL && !(hSession->ibuf_size && hSession->ibuf)) {
			hSession->ibuf 		= (unsigned char *) lib3270_malloc(BUFSIZ);
			hSession->ibuf_size	= BUFSIZ;
			hSession->ibptr		= hSession->ibuf;
		}

#if defined(X3270_ANSI)
		// Reinitialize line mode.
		if ((new_cstate == LIB3270_CONNECTED_ANSI && hSession->linemode) ||
		        new_cstate == LIB3270_CONNECTED_NVT)
			cooked_init(hSession);
#endif

#if defined(X3270_TN3270E)
		// If we fell out of TN3270E, remove the state.
		if (!hSession->myopts[TELOPT_TN3270E]) {
			hSession->tn3270e_negotiated = 0;
			hSession->tn3270e_submode = E_NONE;
			hSession->tn3270e_bound = 0;
		}
#endif
		trace_dsn(hSession,"Now operating in %s mode.\n",lib3270_connection_state_get_name(new_cstate));
		host_in3270(hSession,new_cstate);
	}
}

/*
 * store3270in
 *	Store a character in the 3270 input buffer, checking for buffer
 *	overflow and reallocating ibuf if necessary.
 */
static void store3270in(H3270 *hSession, unsigned char c) {
	if(hSession->ibptr - hSession->ibuf >= hSession->ibuf_size) {
		hSession->ibuf_size += BUFSIZ;
		hSession->ibuf = (unsigned char *) lib3270_realloc((char *) hSession->ibuf, hSession->ibuf_size);
		hSession->ibptr = hSession->ibuf + hSession->ibuf_size - BUFSIZ;
	}
	*hSession->ibptr++ = c;
}

/**
 * Ensure that <n> more characters will fit in the 3270 output buffer.
 *
 * Allocates the buffer in BUFSIZ chunks.
 * Allocates hidden space at the front of the buffer for TN3270E.
 *
 * @param hSession	3270 session handle.
 * @param n			Number of characters to set.
 */
void space3270out(H3270 *hSession, int n) {
	unsigned nc = 0;	/* amount of data currently in obuf */
	unsigned more = 0;

	if (hSession->output.length)
		nc = hSession->output.ptr - hSession->output.buf;

	while ((nc + n + EH_SIZE) > (hSession->output.length + more)) {
		more += BUFSIZ;
	}

	if (more) {
		hSession->output.length += more;
		hSession->output.base = (unsigned char *)Realloc((char *) hSession->output.base,hSession->output.length);
		hSession->output.buf = hSession->output.base + EH_SIZE;
		hSession->output.ptr = hSession->output.buf + nc;
	}
}


/**
 *	Set the session variable 'linemode', which says whether we are in
 *	character-by-character mode or line mode.
 */
static void check_linemode(H3270 *hSession, Boolean init) {
	int wasline = hSession->linemode;

	/*
	 * The next line is a deliberate kluge to effectively ignore the SGA
	 * option.  If the host will echo for us, we assume
	 * character-at-a-time; otherwise we assume fully cooked by us.
	 *
	 * This allows certain IBM hosts which volunteer SGA but refuse
	 * ECHO to operate more-or-less normally, at the expense of
	 * implementing the (hopefully useless) "character-at-a-time, local
	 * echo" mode.
	 *
	 * We still implement "switch to line mode" and "switch to character
	 * mode" properly by asking for both SGA and ECHO to be off or on, but
	 * we basically ignore the reply for SGA.
	 */
	hSession->linemode = hSession->hisopts[TELOPT_ECHO] ? 0 : 1 /* && !hisopts[TELOPT_SGA] */;

	if (init || hSession->linemode != wasline) {
		lib3270_st_changed(hSession,LIB3270_STATE_LINE_MODE, hSession->linemode);
		if (!init) {
			trace_dsn(hSession,"Operating in %s mode.\n",hSession->linemode ? "line" : "character-at-a-time");
		}
#if defined(X3270_ANSI) /*[*/
		if (IN_ANSI && hSession->linemode)
			cooked_init(hSession);
#endif /*]*/
	}
}

#if defined(X3270_TRACE)

///
/// @brief Expands a number to a character string, for displaying unknown telnet commands and options.
///
static const char *
nnn(int c) {
	static char	buf[64];
	memset(buf,0,sizeof(buf));
	(void) snprintf(buf, 63, "%d", c);
	return buf;
}

///
/// @brief Expands a TELNET command into a character string.
///
static const char * cmd(int c) {
	if (TELCMD_OK(c))
		return TELCMD(c);
	else
		return nnn(c);
}

///
/// @brief Expands a TELNET option into a character string.
///
static const char * opt(unsigned char c) {
	if (c == TELOPT_TN3270E)
		return "TN3270E";
	else if (c == TELOPT_STARTTLS)
		return "START-TLS";
	else if (TELOPT_OK(c))
		return TELOPT(c);
	else
		return nnn((int)c);
}

#define LINEDUMP_MAX	32

void trace_netdata(H3270 *hSession, char direction, unsigned const char *buf, int len) {
#define NETDUMP_MAX 121

	if (lib3270_get_toggle(hSession,LIB3270_TOGGLE_NETWORK_TRACE)) {
		char l1[NETDUMP_MAX+2];
		char l2[NETDUMP_MAX+2];
		char l3[NETDUMP_MAX+2];

		int offset;
		int col = 0;

		{
			time_t ltime;
			time(&ltime);

#ifdef HAVE_LOCALTIME_R
			struct tm tm;
			strftime(l1, 81, "%x %X", localtime_r(&ltime,&tm));
#else
			strftime(l1, 81, "%x %X", localtime(&ltime));
#endif // HAVE_LOCALTIME_R
		}

		lib3270_write_nettrace(hSession,"%c %s %s data len=%d\n\n",direction,l1,direction == '>' ? "SEND" : "RECV", len);

		for (offset = 0; offset < len; offset++) {
			unsigned char text[4];

			text[0] = hSession->charset.ebc2asc[buf[offset]];
			l1[col] = (text[0] >= ' ' ? text[0] : '.');

			snprintf((char *) text,4,"%02x",buf[offset]);
			l2[col] = text[0];
			l3[col] = text[1];

			if(++col >= NETDUMP_MAX) {
				l1[col] = l2[col] = l3[col] = 0;
				lib3270_write_nettrace(hSession,"\t%s\n\t%s\n\t%s\n\n",l1,l2,l3);
				col = 0;
			}
		}

		if(col) {
			l1[col] = l2[col] = l3[col] = 0;
			lib3270_write_nettrace(hSession,"\t%s\n\t%s\n\t%s\n\n",l1,l2,l3);
		}

	} else if (lib3270_get_toggle(hSession,LIB3270_TOGGLE_DS_TRACE)) {
		int offset;
		struct timeval ts;
		double tdiff;

		(void) gettimeofday(&ts, (struct timezone *)NULL);

		if (IN_3270) {
			tdiff = ((1.0e6 * (double)(ts.tv_sec - hSession->ds_ts.tv_sec)) +
			         (double)(ts.tv_usec - hSession->ds_ts.tv_usec)) / 1.0e6;
			trace_dsn(hSession,"%c +%gs\n", direction, tdiff);
		}

		hSession->ds_ts = ts;
		for (offset = 0; offset < len; offset++) {
			if (!(offset % LINEDUMP_MAX))
				trace_dsn(hSession,"%s%c 0x%-3x ",(offset ? "\n" : ""), direction, offset);
			trace_dsn(hSession,"%02x", buf[offset]);
		}
		trace_dsn(hSession,"\n");
	}

}
#endif // X3270_TRACE


/**
 * Send 3270 output over the network.
 *
 * Send 3270 output over the network:
 *	- Prepend TN3270E header
 *	- Expand IAC to IAC IAC
 *	- Append IAC EOR
 *
 * @param hSession Session handle
 *
 */
void net_output(H3270 *hSession) {
	static unsigned char *xobuf = NULL;
	static int xobuf_len = 0;
	int need_resize = 0;
	unsigned char *nxoptr, *xoptr;

#if defined(X3270_TN3270E)
#define BSTART	((IN_TN3270E || IN_SSCP) ? hSession->output.base : hSession->output.buf)
#else
#define BSTART	obuf
#endif

#if defined(X3270_TN3270E) /*[*/
	/* Set the TN3720E header. */
	if (IN_TN3270E || IN_SSCP) {
		tn3270e_header *h = (tn3270e_header *) hSession->output.base;

		/* Check for sending a TN3270E response. */
		if (hSession->response_required == TN3270E_RSF_ALWAYS_RESPONSE) {
			tn3270e_ack(hSession);
			hSession->response_required = TN3270E_RSF_NO_RESPONSE;
		}

		/* Set the outbound TN3270E header. */
		h->data_type = IN_TN3270E ? TN3270E_DT_3270_DATA : TN3270E_DT_SSCP_LU_DATA;
		h->request_flag = 0;
		h->response_flag = 0;
		h->seq_number[0] = (hSession->e_xmit_seq >> 8) & 0xff;
		h->seq_number[1] = hSession->e_xmit_seq & 0xff;

		trace_dsn(hSession,"SENT TN3270E(%s NO-RESPONSE %u)\n",IN_TN3270E ? "3270-DATA" : "SSCP-LU-DATA", hSession->e_xmit_seq);
		if (hSession->e_funcs & E_OPT(TN3270E_FUNC_RESPONSES))
			hSession->e_xmit_seq = (hSession->e_xmit_seq + 1) & 0x7fff;
	}
#endif /*]*/

	/* Reallocate the expanded output buffer. */
	while (xobuf_len <  (hSession->output.ptr - BSTART + 1) * 2) {
		xobuf_len += BUFSZ;
		need_resize++;
	}

	if (need_resize) {
		Replace(xobuf, (unsigned char *)lib3270_malloc(xobuf_len));
	}

	/* Copy and expand IACs. */
	xoptr = xobuf;
	nxoptr = BSTART;
	while (nxoptr < hSession->output.ptr) {
		if ((*xoptr++ = *nxoptr++) == IAC) {
			*xoptr++ = IAC;
		}
	}

	/* Append the IAC EOR and transmit. */
	*xoptr++ = IAC;
	*xoptr++ = EOR;
	net_rawout(hSession,xobuf, xoptr - xobuf);

	trace_dsn(hSession,"SENT EOR\n");
	hSession->ns_rsent++;
#undef BSTART
}

#if defined(X3270_TN3270E) /*[*/
/* Send a TN3270E positive response to the server. */
static void tn3270e_ack(H3270 *hSession) {
	unsigned char rsp_buf[10];
	tn3270e_header *h_in = (tn3270e_header *) hSession->ibuf;
	int rsp_len = 0;

	rsp_len = 0;
	rsp_buf[rsp_len++] = TN3270E_DT_RESPONSE;	    /* data_type */
	rsp_buf[rsp_len++] = 0;				    /* request_flag */
	rsp_buf[rsp_len++] = TN3270E_RSF_POSITIVE_RESPONSE; /* response_flag */
	rsp_buf[rsp_len++] = h_in->seq_number[0];	    /* seq_number[0] */

	if (h_in->seq_number[0] == IAC)
		rsp_buf[rsp_len++] = IAC;

	rsp_buf[rsp_len++] = h_in->seq_number[1];	    /* seq_number[1] */

	if (h_in->seq_number[1] == IAC)
		rsp_buf[rsp_len++] = IAC;

	rsp_buf[rsp_len++] = TN3270E_POS_DEVICE_END;
	rsp_buf[rsp_len++] = IAC;
	rsp_buf[rsp_len++] = EOR;
	trace_dsn(hSession,"SENT TN3270E(RESPONSE POSITIVE-RESPONSE %u) DEVICE-END\n",h_in->seq_number[0] << 8 | h_in->seq_number[1]);
	net_rawout(hSession, rsp_buf, rsp_len);
}

/* Send a TN3270E negative response to the server. */
static void tn3270e_nak(H3270 *hSession, enum pds rv) {
	unsigned char rsp_buf[10];
	tn3270e_header *h_in = (tn3270e_header *) hSession->ibuf;
	int rsp_len = 0;
	char *neg = NULL;

	rsp_buf[rsp_len++] = TN3270E_DT_RESPONSE;	    /* data_type */
	rsp_buf[rsp_len++] = 0;				    /* request_flag */
	rsp_buf[rsp_len++] = TN3270E_RSF_NEGATIVE_RESPONSE; /* response_flag */
	rsp_buf[rsp_len++] = h_in->seq_number[0];	    /* seq_number[0] */
	if (h_in->seq_number[0] == IAC)
		rsp_buf[rsp_len++] = IAC;
	rsp_buf[rsp_len++] = h_in->seq_number[1];	    /* seq_number[1] */
	if (h_in->seq_number[1] == IAC)
		rsp_buf[rsp_len++] = IAC;
	switch (rv) {
	default:
	case PDS_BAD_CMD:
		rsp_buf[rsp_len++] = TN3270E_NEG_COMMAND_REJECT;
		neg = "COMMAND-REJECT";
		break;
	case PDS_BAD_ADDR:
		rsp_buf[rsp_len++] = TN3270E_NEG_OPERATION_CHECK;
		neg = "OPERATION-CHECK";
		break;
	}
	rsp_buf[rsp_len++] = IAC;
	rsp_buf[rsp_len++] = EOR;
	trace_dsn(hSession,"SENT TN3270E(RESPONSE NEGATIVE-RESPONSE %u) %s\n",h_in->seq_number[0] << 8 | h_in->seq_number[1], neg);
	net_rawout(hSession, rsp_buf, rsp_len);
}
#endif /*]*/

#if defined(X3270_TRACE) /*[*/
/*
 * Add IAC EOR to a buffer.
 */
void
net_add_eor(unsigned char *buf, int len) {
	buf[len++] = IAC;
	buf[len++] = EOR;
}
#endif /*]*/


#if defined(X3270_ANSI) /*[*/
/**
 * Send a character of user data over the network in ANSI mode.
 *
 * @param hSession	Session handle.
 * @param c			Character to send.
 *
 */
void
net_sendc(H3270 *hSession, char c) {
	if (c == '\r' && !hSession->linemode) {
		/* CR must be quoted */
		net_cookout(hSession,"\r\0", 2);
	} else {
		net_cookout(hSession,&c, 1);
	}
}

/**
 * Send a null-terminated string of user data in ANSI mode.
 *
 * @param hSession	Session handle.
 * @param s			String to send.
 */
void net_sends(H3270 *hSession,const char *s) {
	net_cookout(hSession, s, strlen(s));
}

/**
 * Sends the ERASE character in ANSI mode.
 *
 */
void net_send_erase(H3270 *hSession) {
	net_cookout(hSession, &ansictl.verase, 1);
}

/**
 *	Sends the KILL character in ANSI mode.
 */
void net_send_kill(H3270 *hSession) {
	net_cookout(hSession, &ansictl.vkill, 1);
}

/**
 * Sends the WERASE character in ANSI mode.
 */
void net_send_werase(H3270 *hSession) {
	net_cookout(hSession, &ansictl.vwerase, 1);
}
#endif /*]*/

/*
#if defined(X3270_MENUS)

void net_charmode(H3270 *hSession)
{
	if (!CONNECTED)
		return;

	if (!hisopts[TELOPT_ECHO])
	{
		do_opt[2] = TELOPT_ECHO;
		net_rawout(do_opt, sizeof(do_opt));
		trace_dsn(hSession,"SENT %s %s\n", cmd(DO), opt(TELOPT_ECHO));
	}

	if (!hisopts[TELOPT_SGA])
	{
		do_opt[2] = TELOPT_SGA;
		net_rawout(do_opt, sizeof(do_opt));
		trace_dsn(hSession,"SENT %s %s\n", cmd(DO), opt(TELOPT_SGA));
	}
}
#endif
*/

/*
 * net_break
 *	Send telnet break, which is used to implement 3270 ATTN.
 *
 */
void net_break(H3270 *hSession) {
	static const unsigned char buf[] = { IAC, BREAK };

	/* I don't know if we should first send TELNET synch ? */
	net_rawout(hSession, buf, sizeof(buf));
	trace_dsn(hSession,"SENT BREAK\n");
}

/*
 * net_interrupt
 *	Send telnet IP.
 *
 */
void net_interrupt(H3270 *hSession) {
	static const unsigned char buf[] = { IAC, IP };

	/* I don't know if we should first send TELNET synch ? */
	net_rawout(hSession, buf, sizeof(buf));
	trace_dsn(hSession,"SENT IP\n");
}

/*
 * net_abort
 *	Send telnet AO.
 *
 */
#if defined(X3270_TN3270E) /*[*/
void net_abort(H3270 *hSession) {
	static const unsigned char buf[] = { IAC, AO };

	if (hSession->e_funcs & E_OPT(TN3270E_FUNC_SYSREQ)) {
		/*
		 * I'm not sure yet what to do here.  Should the host respond
		 * to the AO by sending us SSCP-LU data (and putting us into
		 * SSCP-LU mode), or should we put ourselves in it?
		 * Time, and testers, will tell.
		 */
		switch (hSession->tn3270e_submode) {
		case E_NONE:
		case E_NVT:
			break;

		case E_SSCP:
			net_rawout(hSession, buf, sizeof(buf));
			trace_dsn(hSession,"SENT AO\n");
			if (hSession->tn3270e_bound || !(hSession->e_funcs & E_OPT(TN3270E_FUNC_BIND_IMAGE))) {
				hSession->tn3270e_submode = E_3270;
				check_in3270(hSession);
			}
			break;

		case E_3270:
			net_rawout(hSession, buf, sizeof(buf));
			trace_dsn(hSession,"SENT AO\n");
			hSession->tn3270e_submode = E_SSCP;
			check_in3270(hSession);
			break;
		}
	}
}
#endif /*]*/

/* Return the local address for the socket. */

/*
int net_getsockname(const H3270 *session, void *buf, int *len)
{
	if (session->connection.sock < 0)
		return -1;
	return getsockname(session->connection.sock, buf, (socklen_t *)(void *)len);
}
*/