www.pudn.com > tftp.rar > COMMANDS.C
/* ** This product contains: ** "Restricted Materials of IBM" ** (c) Copyright IBM Corp. 1987 ** All Rights Reserved ** Licensed Materials-Property of IBM ** ** See Copyright Instructions, G120-2083 ** */ #include#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "tftpdefs.h" /* ** File: ** commands.c ** ** Routines: ** server (spool) - become a tftp server ** connect (cmdline) - connect to a remote host ** chmode (newmode) - change file transfer mode ** transfer (dir,cmdline) - transfer a file to or from a remote host ** showstats () - show the current tftp session status ** help () - print out help information */ /*------------------------------------------------------------------------*/ /* ** Routine: ** server (spool) ** ** Purpose: ** To initialize tftp as a server, then wait patiently for either ** commands (single key strokes) or files ** ** Input Parameters: ** spool - flag determining whether we should spool or not ** ** External Dependencies: ** conn - tftp connection structure ** */ void server (spool) int spool; { int c; tfsinit (tfprint,tfdone,spool,conn.flags & F_EOFMARK); tfs_on(); if (conn.flags & F_VERBOSE) if (spool) printf ("Spooling\n"); else printf ("Serving\n"); while (1) { while ((c = h19key()) == NONE) tk_yield(); switch (tolower(c)) { case 'q': /* Shutdown the server */ tfsclose(); return; case '+': tfs_on(); printf ("TFTP server turned on\n"); break; case '-': tfs_off(); printf ("TFTP server turned off\n"); break; case 's': net_stats (stdout); break; default: printf ("TFTP Server commands:\n"); printf ("\tq\tExit TFTP Server\n"); printf ("\ts\tPrint network statistics\n"); printf ("\t-\tTurn TFTP Server off\n"); printf ("\t+\tTurn TFTP Server on\n"); } } } /*------------------------------------------------------------------------*/ void connect (cmdline) char *cmdline; { char temp[HOSTNAMELEN]; char *fhost = NULL; fhost = strtok(cmdline," "); if (fhost == NULL) { printf ("(to) "); gets (temp); fhost = strtok(temp," "); if (fhost == NULL) return; } conn.host = resolve_name(fhost); if (conn.host == E_NAMEUNKNOWN) { printf ("Unknown host: %s\n",fhost); *conn.hostname = '\0'; } else if (conn.host == E_NAMETMO) { printf ("Name servers not responding\n"); *conn.hostname = '\0'; } else { strcpy(conn.hostname,fhost); if (conn.flags & F_VERBOSE) if (isdigit(conn.hostname[1])) printf ("Will connect to %s\n",conn.hostname,conn.host); else printf ("Will connect to %s (%a)\n",conn.hostname,conn.host); } } /*------------------------------------------------------------------------*/ void chmode (newmode) char *newmode; { char *mode; int len; mode = strtok(newmode," "); len = strlen(strlwr(mode)); if (mode == NULL) printf ("Mode: %s\n",(conn.mode == M_BINARY) ? "binary" : "netascii"); else if (strncmp(mode,"binary",len) == 0 || strncmp(mode,"octet",len) == 0 || strncmp(mode,"image",len) == 0) { conn.mode = M_BINARY; if (conn.flags & F_VERBOSE) printf ("Mode set to Binary\n"); } else if (strncmp(mode,"ascii",len) == 0 || strncmp(mode,"netascii",len) == 0) { conn.mode = M_ASCII; if (conn.flags & F_VERBOSE) printf ("Mode set to Netascii\n"); } else { printf ("Unknown mode '%s'\n",newmode); if (conn.flags & F_VERBOSE) printf ("Usage: Mode [Binary | Image | Octet | Ascii | Netascii]\n"); } } /*------------------------------------------------------------------------*/ void transfer (dir,cmdline) int dir; char *cmdline; { unsigned long bps = 0; unsigned long deltat = 0; unsigned long size = 0; char *fname1 = NULL; char *fname2 = NULL; char localfile[FILENAMELEN]; char foreignfile[FILENAMELEN]; char temp[FILENAMELEN]; if ((fname1 = strtok(cmdline," ")) != NULL) fname2 = strtok(NULL," "); if (dir == D_PUT) { if (fname1 != NULL) if (access(fname1,A_READ) == 0) strcpy (localfile,fname1); else { printf ("Can't open %s: %s\n",fname1,sys_errlist[errno]); return; } else if (p_getlocal(localfile) == NULL) return; if (fname2 != NULL) strcpy (foreignfile,fname2); else if (getforeign(foreignfile) == NULL) strcpy (foreignfile,localfile); } else { if (fname1 != NULL) strcpy (foreignfile,fname1); else if (getforeign(foreignfile) == NULL) return; if (fname2 != NULL) if (g_local_ok(fname2)) strcpy (localfile,fname2); else return; else if (g_getlocal(localfile,foreignfile) == NULL) return; } if (*conn.hostname == '\0') { connect(""); if (*conn.hostname == '\0') return; } if (conn.flags & F_VERBOSE) if (dir == D_PUT) printf ("Putting file '%s' to %s (%a) as '%s'\n", localfile,conn.hostname,conn.host,foreignfile); else printf ("Getting file '%s' from %s (%a) as '%s'\n", foreignfile,conn.hostname,conn.host,localfile); size = tftpuse ( conn.host, localfile, foreignfile, dir, conn.mode, size, &deltat, conn.flags & F_EOFMARK ); if (size == 0) { printf ("Transfer not successful\n"); if (dir == D_GET) unlink (localfile); } else if (conn.flags & F_VERBOSE) { bps = (deltat == 0) ? 0 : size * 8L * TPS/deltat; deltat = deltat/TPS; printf ("Transfer successful: "); printf ("%U bytes in %U secs, %U bits/second\n",size,deltat,bps); } } /*------------------------------------------------------------------------*/ void showstats() { if (conn.hostname[0] == '\0') printf ("Not connected\n"); else printf ("Connected to %s (%a)\n",conn.hostname,conn.host); printf ("Mode: %s\t",(conn.mode == M_BINARY) ? "Binary" : "Netascii"); printf ("Verbose: %s\t",(conn.flags & F_VERBOSE) ? "On" : "Off"); printf ("Tracing: %s\n",(conn.flags & F_TRACE) ? "On" : "Off"); printf ("Write EOF mark: %s\t",(conn.flags & F_EOFMARK) ? "On" : "Off"); printf ("Clobber: %s\n",(conn.flags & F_CLOBBER) ? "On" : "Off"); } /*------------------------------------------------------------------------*/ void help() { printf ("Commands: (Command abbreviations indicated by capital letters)\n\n"); printf ("\tServer - become a tftp server\n"); printf ("\tSPool - TFTP serve with spooling\n"); printf ("\tConnect [remote host] - connect to remote host\n"); printf ("\tMode [Binary|Ascii] - change file transfer mode\n"); printf ("\tPut [localfile] [foreignfile] - transfer file to remote host\n"); printf ("\tGet [foreignfile] [localfile] - get file from remote host\n"); printf ("\tQuit - exit %s\n",_whoami); printf ("\tVerbose - toggle verbose mode\n"); printf ("\tTrace - toggle trace mode\n"); printf ("\tCLobber - toggle overwrite existing files flag\n"); printf ("\tEofmark - toggle write eof mark flag\n"); printf ("\tSTatus - show current status\n"); printf ("\tBinary (or Image or Octet) - change mode to binary\n"); printf ("\tAscii (or Netascii) - change mode to netascii\n"); printf ("\t? - print help information\n"); printf ("\t![command] - escape to DOS\n"); }