X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/887035a593c8c0a1af853657c80046e17dc5581a..820ebe3b54a21cfb8d42e43c567d952415c1d05d:/plink.c diff --git a/plink.c b/plink.c index 5ac8b216..d61bbeb5 100644 --- a/plink.c +++ b/plink.c @@ -40,7 +40,7 @@ void modalfatalbox(char *p, ...) WSACleanup(); cleanup_exit(1); } -void connection_fatal(char *p, ...) +void connection_fatal(void *frontend, char *p, ...) { va_list ap; fprintf(stderr, "FATAL ERROR: "); @@ -62,18 +62,20 @@ void cmdline_error(char *p, ...) exit(1); } -static char *password = NULL; - HANDLE inhandle, outhandle, errhandle; DWORD orig_console_mode; WSAEVENT netevent; +static Backend *back; +static void *backhandle; +static Config cfg; + int term_ldisc(Terminal *term, int mode) { return FALSE; } -void ldisc_update(int echo, int edit) +void ldisc_update(void *frontend, int echo, int edit) { /* Update stdin read mode to reflect changes in line discipline. */ DWORD mode; @@ -164,9 +166,9 @@ void try_output(int is_stderr) } } -int from_backend(void *frontend_handle, int is_stderr, char *data, int len) +int from_backend(void *frontend_handle, int is_stderr, + const char *data, int len) { - HANDLE h = (is_stderr ? errhandle : outhandle); int osize, esize; assert(len > 0); @@ -205,6 +207,7 @@ static void usage(void) printf(" -batch disable all interactive prompts\n"); printf("The following options only apply to SSH connections:\n"); printf(" -pw passw login with specified password\n"); + printf(" -D listen-port Dynamic SOCKS-based port forwarding\n"); printf(" -L listen-port:host:port Forward local port to " "remote address\n"); printf(" -R listen-port:host:port Forward remote port to" @@ -253,6 +256,7 @@ int main(int argc, char **argv) int skcount, sksize; int connopen; int exitcode; + int errors; ssh_get_line = console_get_line; @@ -272,6 +276,7 @@ int main(int argc, char **argv) do_defaults(NULL, &cfg); default_protocol = cfg.protocol; default_port = cfg.port; + errors = 0; { /* * Override the default protocol if PLINK_PROTOCOL is set. @@ -292,16 +297,21 @@ int main(int argc, char **argv) while (--argc) { char *p = *++argv; if (*p == '-') { - int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), 1); + int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), + 1, &cfg); if (ret == -2) { fprintf(stderr, "plink: option \"%s\" requires an argument\n", p); + errors = 1; } else if (ret == 2) { --argc, ++argv; } else if (ret == 1) { continue; } else if (!strcmp(p, "-batch")) { console_batch_mode = 1; + } else { + fprintf(stderr, "plink: unknown option \"%s\"\n", p); + errors = 1; } } else if (*p) { if (!*cfg.host) { @@ -401,13 +411,13 @@ int main(int argc, char **argv) while (*p) { if (cmdlen >= cmdsize) { cmdsize = cmdlen + 512; - command = srealloc(command, cmdsize); + command = sresize(command, cmdsize, char); } command[cmdlen++]=*p++; } if (cmdlen >= cmdsize) { cmdsize = cmdlen + 512; - command = srealloc(command, cmdsize); + command = sresize(command, cmdsize, char); } command[cmdlen++]=' '; /* always add trailing space */ if (--argc) p = *++argv; @@ -423,6 +433,9 @@ int main(int argc, char **argv) } } + if (errors) + return 1; + if (!*cfg.host) { usage(); } @@ -451,7 +464,7 @@ int main(int argc, char **argv) /* * Perform command-line overrides on session configuration. */ - cmdline_run_saved(); + cmdline_run_saved(&cfg); /* * Trim a colon suffix off the hostname if it's there. @@ -529,11 +542,15 @@ int main(int argc, char **argv) int nodelay = cfg.tcp_nodelay && (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR); - error = back->init(NULL, cfg.host, cfg.port, &realhost, nodelay); + error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, + &realhost, nodelay); if (error) { fprintf(stderr, "Unable to open connection:\n%s", error); return 1; } + logctx = log_init(NULL, &cfg); + back->provide_logctx(backhandle, logctx); + console_provide_logctx(logctx); sfree(realhost); } connopen = 1; @@ -585,7 +602,7 @@ int main(int argc, char **argv) while (1) { int n; - if (!sending && back->sendok()) { + if (!sending && back->sendok(backhandle)) { /* * Create a separate thread to read from stdin. This is * a total pain, but I can't find another way to do it: @@ -635,7 +652,7 @@ int main(int argc, char **argv) /* Expand the buffer if necessary. */ if (i > sksize) { sksize = i + 16; - sklist = srealloc(sklist, sksize * sizeof(*sklist)); + sklist = sresize(sklist, sksize, SOCKET); } /* Retrieve the sockets into sklist. */ @@ -677,11 +694,11 @@ int main(int argc, char **argv) } else if (n == 1) { reading = 0; noise_ultralight(idata.len); - if (connopen && back->socket() != NULL) { + if (connopen && back->socket(backhandle) != NULL) { if (idata.len > 0) { - back->send(idata.buffer, idata.len); + back->send(backhandle, idata.buffer, idata.len); } else { - back->special(TS_EOF); + back->special(backhandle, TS_EOF); } } } else if (n == 2) { @@ -693,8 +710,8 @@ int main(int argc, char **argv) bufchain_consume(&stdout_data, odata.lenwritten); if (bufchain_size(&stdout_data) > 0) try_output(0); - if (connopen && back->socket() != NULL) { - back->unthrottle(bufchain_size(&stdout_data) + + if (connopen && back->socket(backhandle) != NULL) { + back->unthrottle(backhandle, bufchain_size(&stdout_data) + bufchain_size(&stderr_data)); } } else if (n == 3) { @@ -706,22 +723,22 @@ int main(int argc, char **argv) bufchain_consume(&stderr_data, edata.lenwritten); if (bufchain_size(&stderr_data) > 0) try_output(1); - if (connopen && back->socket() != NULL) { - back->unthrottle(bufchain_size(&stdout_data) + + if (connopen && back->socket(backhandle) != NULL) { + back->unthrottle(backhandle, bufchain_size(&stdout_data) + bufchain_size(&stderr_data)); } } - if (!reading && back->sendbuffer() < MAX_STDIN_BACKLOG) { + if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { SetEvent(idata.eventback); reading = 1; } - if ((!connopen || back->socket() == NULL) && + if ((!connopen || back->socket(backhandle) == NULL) && bufchain_size(&stdout_data) == 0 && bufchain_size(&stderr_data) == 0) break; /* we closed the connection */ } WSACleanup(); - exitcode = back->exitcode(); + exitcode = back->exitcode(backhandle); if (exitcode < 0) { fprintf(stderr, "Remote process exit code unavailable\n"); exitcode = 1; /* this is an error condition */