Lines 86-95
Link Here
|
86 |
|
86 |
|
87 |
#define HELPINDENT ((int) sizeof ("connect")) |
87 |
#define HELPINDENT ((int) sizeof ("connect")) |
88 |
|
88 |
|
89 |
#ifndef MAXHOSTNAMELEN |
|
|
90 |
#define MAXHOSTNAMELEN 64 |
91 |
#endif MAXHOSTNAMELEN |
92 |
|
93 |
#if defined(HAS_IPPROTO_IP) && defined(IP_TOS) |
89 |
#if defined(HAS_IPPROTO_IP) && defined(IP_TOS) |
94 |
int tos = -1; |
90 |
int tos = -1; |
95 |
#endif /* defined(HAS_IPPROTO_IP) && defined(IP_TOS) */ |
91 |
#endif /* defined(HAS_IPPROTO_IP) && defined(IP_TOS) */ |
Lines 98-104
Link Here
|
98 |
|
94 |
|
99 |
|
95 |
|
100 |
char *hostname; |
96 |
char *hostname; |
101 |
static char _hostname[MAXHOSTNAMELEN]; |
97 |
static char *_hostname; |
102 |
|
98 |
|
103 |
//typedef int (*intrtn_t)(int argc, const char *argv[]); |
99 |
//typedef int (*intrtn_t)(int argc, const char *argv[]); |
104 |
|
100 |
|
Lines 161-167
Link Here
|
161 |
assert(argc>=1); |
157 |
assert(argc>=1); |
162 |
if (nargs>=0 && argc!=nargs+1) { |
158 |
if (nargs>=0 && argc!=nargs+1) { |
163 |
fprintf(stderr, "Wrong number of arguments for command.\n"); |
159 |
fprintf(stderr, "Wrong number of arguments for command.\n"); |
164 |
fprintf(stderr, "Try %s ? for help\n", argv[0]); |
160 |
fprintf(stderr, "Try ? %s for help\n", argv[0]); |
165 |
return 0; /* is this right? */ |
161 |
return 0; /* is this right? */ |
166 |
} |
162 |
} |
167 |
if (nargs==-2) { |
163 |
if (nargs==-2) { |
Lines 480-485
Link Here
|
480 |
int send_tncmd(int (*func)(int, int), const char *cmd, const char *name) { |
476 |
int send_tncmd(int (*func)(int, int), const char *cmd, const char *name) { |
481 |
char **cpp; |
477 |
char **cpp; |
482 |
extern char *telopts[]; |
478 |
extern char *telopts[]; |
|
|
479 |
long opt; |
483 |
|
480 |
|
484 |
if (isprefix(name, "help") || isprefix(name, "?")) { |
481 |
if (isprefix(name, "help") || isprefix(name, "?")) { |
485 |
register int col, len; |
482 |
register int col, len; |
Lines 506-521
Link Here
|
506 |
name, cmd); |
503 |
name, cmd); |
507 |
return 0; |
504 |
return 0; |
508 |
} |
505 |
} |
|
|
506 |
|
507 |
opt = cpp - telopts; |
509 |
if (cpp == 0) { |
508 |
if (cpp == 0) { |
510 |
fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n", |
509 |
char *end; |
|
|
510 |
|
511 |
opt = strtol(name, &end, 10); |
512 |
if (*end || opt < 0 || opt > 255) { |
513 |
fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n", |
511 |
name, cmd); |
514 |
name, cmd); |
512 |
return 0; |
515 |
return 0; |
|
|
516 |
} |
513 |
} |
517 |
} |
514 |
if (!connected) { |
518 |
if (!connected) { |
515 |
printf("?Need to be connected first.\n"); |
519 |
printf("?Need to be connected first.\n"); |
516 |
return 0; |
520 |
return 0; |
517 |
} |
521 |
} |
518 |
(*func)(cpp - telopts, 1); |
522 |
(*func)(opt, 1); |
519 |
return 1; |
523 |
return 1; |
520 |
} |
524 |
} |
521 |
|
525 |
|
Lines 689-697
Link Here
|
689 |
"print encryption debugging information" }, |
693 |
"print encryption debugging information" }, |
690 |
#endif |
694 |
#endif |
691 |
|
695 |
|
692 |
{ "skiprc", "don't read ~/.telnetrc file", |
696 |
{ "skiprc", "don't read the telnetrc files", |
693 |
NULL, &skiprc, |
697 |
NULL, &skiprc, |
694 |
"read ~/.telnetrc file" }, |
698 |
"read the telnetrc files" }, |
695 |
{ "binary", |
699 |
{ "binary", |
696 |
"sending and receiving of binary data", |
700 |
"sending and receiving of binary data", |
697 |
togbinary, NULL, |
701 |
togbinary, NULL, |
Lines 1615-1629
Link Here
|
1615 |
#endif |
1619 |
#endif |
1616 |
|
1620 |
|
1617 |
int tn(int argc, const char *argv[]) { |
1621 |
int tn(int argc, const char *argv[]) { |
1618 |
register struct hostent *host = 0; |
|
|
1619 |
struct sockaddr_in sn; |
1622 |
struct sockaddr_in sn; |
1620 |
struct servent *sp = 0; |
|
|
1621 |
char *srp = NULL; |
1623 |
char *srp = NULL; |
1622 |
int srlen; |
1624 |
int srlen; |
1623 |
|
1625 |
int family = 0; |
1624 |
const char *cmd, *volatile user = 0; |
1626 |
const char *cmd, *volatile user = 0, *srchostp = 0; |
1625 |
const char *portp = NULL; |
1627 |
const char *portp = NULL; |
1626 |
char *hostp = NULL; |
1628 |
char *hostp = NULL; |
|
|
1629 |
char *resolv_hostp; |
1630 |
struct addrinfo hints; |
1631 |
struct addrinfo *hostaddr = 0; |
1632 |
int res; |
1633 |
char name[NI_MAXHOST]; |
1634 |
char service[NI_MAXSERV]; |
1635 |
struct addrinfo *tmpaddr; |
1627 |
|
1636 |
|
1628 |
/* clear the socket address prior to use */ |
1637 |
/* clear the socket address prior to use */ |
1629 |
memset(&sn, 0, sizeof(sn)); |
1638 |
memset(&sn, 0, sizeof(sn)); |
Lines 1632-1637
Link Here
|
1632 |
printf("?Already connected to %s\n", hostname); |
1641 |
printf("?Already connected to %s\n", hostname); |
1633 |
return 0; |
1642 |
return 0; |
1634 |
} |
1643 |
} |
|
|
1644 |
if (_hostname) { |
1645 |
delete[] _hostname; |
1646 |
_hostname = 0; |
1647 |
} |
1635 |
if (argc < 2) { |
1648 |
if (argc < 2) { |
1636 |
(void) strcpy(line, "open "); |
1649 |
(void) strcpy(line, "open "); |
1637 |
printf("(to) "); |
1650 |
printf("(to) "); |
Lines 1657-1667
Link Here
|
1657 |
--argc; |
1670 |
--argc; |
1658 |
continue; |
1671 |
continue; |
1659 |
} |
1672 |
} |
|
|
1673 |
if (strcmp(*argv, "-b") == 0) { |
1674 |
--argc; ++argv; |
1675 |
if (argc == 0) |
1676 |
goto usage; |
1677 |
srchostp = *argv++; |
1678 |
--argc; |
1679 |
continue; |
1680 |
} |
1660 |
if (strcmp(*argv, "-a") == 0) { |
1681 |
if (strcmp(*argv, "-a") == 0) { |
1661 |
--argc; ++argv; |
1682 |
--argc; ++argv; |
1662 |
autologin = 1; |
1683 |
autologin = 1; |
1663 |
continue; |
1684 |
continue; |
1664 |
} |
1685 |
} |
|
|
1686 |
if (strcmp(*argv, "-6") == 0) { |
1687 |
--argc; ++argv; |
1688 |
#ifdef AF_INET6 |
1689 |
family = AF_INET6; |
1690 |
#else |
1691 |
puts("IPv6 unsupported"); |
1692 |
#endif |
1693 |
continue; |
1694 |
} |
1695 |
if (strcmp(*argv, "-4") == 0) { |
1696 |
--argc; ++argv; |
1697 |
family = AF_INET; |
1698 |
continue; |
1699 |
} |
1665 |
if (hostp == 0) { |
1700 |
if (hostp == 0) { |
1666 |
/* this leaks memory - FIXME */ |
1701 |
/* this leaks memory - FIXME */ |
1667 |
hostp = strdup(*argv++); |
1702 |
hostp = strdup(*argv++); |
Lines 1680-1685
Link Here
|
1680 |
if (hostp == 0) |
1715 |
if (hostp == 0) |
1681 |
goto usage; |
1716 |
goto usage; |
1682 |
|
1717 |
|
|
|
1718 |
resolv_hostp = hostp; |
1719 |
|
1683 |
#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP) |
1720 |
#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP) |
1684 |
if (hostp[0] == '@' || hostp[0] == '!') { |
1721 |
if (hostp[0] == '@' || hostp[0] == '!') { |
1685 |
if ((hostname = strrchr(hostp, ':')) == NULL) |
1722 |
if ((hostname = strrchr(hostp, ':')) == NULL) |
Lines 1696-1773
Link Here
|
1696 |
} else { |
1733 |
} else { |
1697 |
sn.sin_addr.s_addr = temp; |
1734 |
sn.sin_addr.s_addr = temp; |
1698 |
sn.sin_family = AF_INET; |
1735 |
sn.sin_family = AF_INET; |
|
|
1736 |
/* |
1737 |
* For source route we just make sure to get the IP given |
1738 |
* on the command line when looking up the port. |
1739 |
*/ |
1740 |
resolv_hostp = inet_ntoa(sn.sin_addr); |
1699 |
} |
1741 |
} |
1700 |
} |
1742 |
} |
1701 |
else { |
|
|
1702 |
#endif |
1703 |
if (inet_aton(hostp, &sn.sin_addr)) { |
1704 |
sn.sin_family = AF_INET; |
1705 |
strcpy(_hostname, hostp); |
1706 |
hostname = _hostname; |
1707 |
} |
1708 |
else { |
1709 |
host = gethostbyname(hostp); |
1710 |
if (host) { |
1711 |
sn.sin_family = host->h_addrtype; |
1712 |
if (host->h_length > (int)sizeof(sn.sin_addr)) { |
1713 |
host->h_length = sizeof(sn.sin_addr); |
1714 |
} |
1715 |
#if defined(h_addr) /* In 4.3, this is a #define */ |
1716 |
memcpy((caddr_t)&sn.sin_addr, |
1717 |
host->h_addr_list[0], host->h_length); |
1718 |
#else /* defined(h_addr) */ |
1719 |
memcpy((caddr_t)&sn.sin_addr, host->h_addr, host->h_length); |
1720 |
#endif /* defined(h_addr) */ |
1721 |
strncpy(_hostname, host->h_name, sizeof(_hostname)); |
1722 |
_hostname[sizeof(_hostname)-1] = '\0'; |
1723 |
hostname = _hostname; |
1724 |
} else { |
1725 |
herror(hostp); |
1726 |
return 0; |
1727 |
} |
1728 |
} |
1729 |
#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP) |
1730 |
} |
1731 |
#endif |
1743 |
#endif |
|
|
1744 |
|
1745 |
/* User port or the default name of telnet. */ |
1732 |
if (portp) { |
1746 |
if (portp) { |
1733 |
if (*portp == '-') { |
1747 |
if (*portp == '-') { |
1734 |
portp++; |
1748 |
portp++; |
1735 |
telnetport = 1; |
1749 |
telnetport = 1; |
1736 |
} else |
1750 |
} else |
1737 |
telnetport = 0; |
1751 |
telnetport = 0; |
1738 |
sn.sin_port = atoi(portp); |
1752 |
} |
1739 |
if (sn.sin_port == 0) { |
|
|
1740 |
sp = getservbyname(portp, "tcp"); |
1741 |
if (sp) |
1742 |
sn.sin_port = sp->s_port; |
1743 |
else { |
1744 |
printf("%s: bad port number\n", portp); |
1745 |
return 0; |
1746 |
} |
1747 |
} |
1748 |
else { |
1749 |
sn.sin_port = htons(sn.sin_port); |
1750 |
} |
1751 |
} |
1752 |
else { |
1753 |
else { |
1753 |
if (sp == 0) { |
1754 |
portp = "telnet"; |
1754 |
sp = getservbyname("telnet", "tcp"); |
|
|
1755 |
if (sp == 0) { |
1756 |
fprintf(stderr, "telnet: tcp/telnet: unknown service\n"); |
1757 |
return 0; |
1758 |
} |
1759 |
sn.sin_port = sp->s_port; |
1760 |
} |
1761 |
telnetport = 1; |
1755 |
telnetport = 1; |
1762 |
} |
1756 |
} |
1763 |
printf("Trying %s...\n", inet_ntoa(sn.sin_addr)); |
1757 |
|
|
|
1758 |
/* We only understand SOCK_STREAM sockets. */ |
1759 |
memset(&hints, 0, sizeof(hints)); |
1760 |
hints.ai_socktype = SOCK_STREAM; |
1761 |
hints.ai_flags = AI_NUMERICHOST; |
1762 |
hints.ai_family = family; |
1763 |
|
1764 |
if (srchostp) { |
1765 |
res = getaddrinfo(srchostp, "0", &hints, &hostaddr); |
1766 |
if (res) { |
1767 |
fprintf(stderr, "telnet: could not resolve %s: %s\n", srchostp, |
1768 |
gai_strerror(res)); |
1769 |
return 0; |
1770 |
} |
1771 |
hints.ai_family = hostaddr->ai_family; |
1772 |
res = nlink.bind(hostaddr); |
1773 |
freeaddrinfo(hostaddr); |
1774 |
if (res < 0) |
1775 |
return 0; |
1776 |
} |
1777 |
|
1778 |
/* Resolve both the host and service simultaneously. */ |
1779 |
res = getaddrinfo(resolv_hostp, portp, &hints, &hostaddr); |
1780 |
if (res == EAI_NONAME) { |
1781 |
hints.ai_flags = AI_CANONNAME; |
1782 |
res = getaddrinfo(resolv_hostp, portp, &hints, &hostaddr); |
1783 |
} else if (hostaddr) { |
1784 |
hostaddr->ai_canonname = 0; |
1785 |
} |
1786 |
if (res || !hostaddr) { |
1787 |
fprintf(stderr, "telnet: could not resolve %s/%s: %s\n", resolv_hostp, portp, gai_strerror(res)); |
1788 |
return 0; |
1789 |
} |
1790 |
|
1791 |
/* Try to connect to every listed round robin IP. */ |
1792 |
tmpaddr = hostaddr; |
1793 |
errno = 0; |
1764 |
do { |
1794 |
do { |
1765 |
int x = nlink.connect(debug, host, &sn, srp, srlen, tos); |
1795 |
int x; |
1766 |
if (!x) return 0; |
1796 |
|
1767 |
else if (x==1) continue; |
1797 |
if (!tmpaddr) { |
|
|
1798 |
if (errno) |
1799 |
perror("telnet: Unable to connect to remote host"); |
1800 |
else |
1801 |
fputs("telnet: Unable to connect to remote host: " |
1802 |
"Bad port number\n", stderr); |
1803 |
err: |
1804 |
freeaddrinfo(hostaddr); |
1805 |
return 0; |
1806 |
} |
1807 |
|
1808 |
if (tmpaddr->ai_family == AF_UNIX) { |
1809 |
nextaddr: |
1810 |
tmpaddr = tmpaddr->ai_next; |
1811 |
continue; |
1812 |
} |
1813 |
|
1814 |
getnameinfo(tmpaddr->ai_addr, tmpaddr->ai_addrlen, |
1815 |
name, sizeof(name), service, sizeof(service), |
1816 |
NI_NUMERICHOST | NI_NUMERICSERV); |
1817 |
|
1818 |
printf("Trying %s...\n", name); |
1819 |
x = nlink.connect(debug, tmpaddr, srp, srlen, tos); |
1820 |
if (!x) |
1821 |
goto err; |
1822 |
else if (x==1) |
1823 |
goto nextaddr; |
1824 |
|
1768 |
connected++; |
1825 |
connected++; |
1769 |
} while (connected == 0); |
1826 |
} while (connected == 0); |
1770 |
cmdrc(hostp, hostname); |
1827 |
if (tmpaddr->ai_canonname == 0) { |
|
|
1828 |
hostname = new char[strlen(hostp)+1]; |
1829 |
strcpy(hostname, hostp); |
1830 |
} |
1831 |
else { |
1832 |
hostname = new char[strlen(tmpaddr->ai_canonname)+1]; |
1833 |
strcpy(hostname, tmpaddr->ai_canonname); |
1834 |
} |
1835 |
|
1836 |
cmdrc(hostp, hostname, portp); |
1837 |
freeaddrinfo(hostaddr); |
1771 |
if (autologin && user == NULL) { |
1838 |
if (autologin && user == NULL) { |
1772 |
struct passwd *pw; |
1839 |
struct passwd *pw; |
1773 |
|
1840 |
|
Lines 2013-2042
Link Here
|
2013 |
return 0; |
2080 |
return 0; |
2014 |
} |
2081 |
} |
2015 |
|
2082 |
|
2016 |
static char *rcname = 0; |
2083 |
static void readrc(const char *m1, const char *m2, const char *port, |
2017 |
static char rcbuf[128]; |
2084 |
const char *rcname) |
2018 |
|
2085 |
{ |
2019 |
void cmdrc(const char *m1, const char *m2) { |
|
|
2020 |
FILE *rcfile; |
2086 |
FILE *rcfile; |
2021 |
int gotmachine = 0; |
2087 |
int gotmachine = 0; |
2022 |
int l1 = strlen(m1); |
2088 |
int l1 = strlen(m1); |
2023 |
int l2 = strlen(m2); |
2089 |
int l2 = strlen(m2); |
2024 |
char m1save[64]; |
2090 |
int lport = strlen(port); |
2025 |
|
2091 |
char m1save[l1 + 1]; |
2026 |
if (skiprc) return; |
2092 |
char portsave[lport + 1]; |
2027 |
|
2093 |
|
2028 |
strcpy(m1save, m1); |
2094 |
strcpy(m1save, m1); |
2029 |
m1 = m1save; |
2095 |
m1 = m1save; |
2030 |
|
2096 |
strcpy(portsave, port); |
2031 |
if (rcname == 0) { |
2097 |
port = portsave; |
2032 |
rcname = getenv("HOME"); |
|
|
2033 |
if (rcname) |
2034 |
strcpy(rcbuf, rcname); |
2035 |
else |
2036 |
rcbuf[0] = '\0'; |
2037 |
strcat(rcbuf, "/.telnetrc"); |
2038 |
rcname = rcbuf; |
2039 |
} |
2040 |
|
2098 |
|
2041 |
rcfile = fopen(rcname, "r"); |
2099 |
rcfile = fopen(rcname, "r"); |
2042 |
if (!rcfile) return; |
2100 |
if (!rcfile) return; |
Lines 2061-2066
Link Here
|
2061 |
strncpy(line, &line[7], sizeof(line) - 7); |
2119 |
strncpy(line, &line[7], sizeof(line) - 7); |
2062 |
else |
2120 |
else |
2063 |
continue; |
2121 |
continue; |
|
|
2122 |
|
2123 |
if (line[0] == ':') { |
2124 |
if (!strncasecmp(&line[1], port, lport)) |
2125 |
continue; |
2126 |
strncpy(line, &line[lport + 1], sizeof(line) - lport - 1); |
2127 |
} |
2128 |
|
2064 |
if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n') |
2129 |
if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n') |
2065 |
continue; |
2130 |
continue; |
2066 |
gotmachine = 1; |
2131 |
gotmachine = 1; |
Lines 2073-2078
Link Here
|
2073 |
fclose(rcfile); |
2138 |
fclose(rcfile); |
2074 |
} |
2139 |
} |
2075 |
|
2140 |
|
|
|
2141 |
void cmdrc(const char *m1, const char *m2, const char *port) { |
2142 |
char *rcname = NULL; |
2143 |
|
2144 |
if (skiprc) return; |
2145 |
|
2146 |
readrc(m1, m2, port, "/etc/telnetrc"); |
2147 |
if (asprintf (&rcname, "%s/.telnetrc", getenv ("HOME")) == -1) |
2148 |
{ |
2149 |
perror ("asprintf"); |
2150 |
return; |
2151 |
} |
2152 |
readrc(m1, m2, port, rcname); |
2153 |
free (rcname); |
2154 |
} |
2155 |
|
2076 |
#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP) |
2156 |
#if defined(IP_OPTIONS) && defined(HAS_IPPROTO_IP) |
2077 |
|
2157 |
|
2078 |
/* |
2158 |
/* |