⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 txfr.c

📁 一个使用des加密传输的unix下的login程序的服务器端和客户端
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * @(#) txfr.c  RCS: $Revision: 1.8 $ $Date: 95/03/08 15:46:54 $ */#if defined(_POSIX_SOURCE)#undef _POSIX_SOURCE	/* otherwise <sys/types.h> won't define fd_set */#endif#include <sys/types.h>		/* BSD type fd_set FD_SET macros */#include <sys/time.h>		/* BSD struct timeval */#if !defined(_POSIX_SOURCE)	/* in case the above include files defined it */#define _POSIX_SOURCE		/* yes, I know posix doesn't have select */#endif#include <unistd.h>		/* alarm read write */#include <stdlib.h>		/* exit malloc */#include <stdio.h>#include <errno.h>		/* EINTR EAGAIN */#include <signal.h>		/* SIGCHLD */#include <string.h>		/* memset */#include <fcntl.h>		/* O_NONBLOCK */#include <sys/time.h>		/* select *//* * 25-Jan-95: gl8f@fermi.clas.virginia.edu (Greg Lindahl) * 25-Jan-95: skrenta@pbm.com (Rich Skrent) */#if defined(AIX) || defined(__DYNIX__) 	/* { */#include <sys/select.h>		/* AIX 3.2.5 mainframe */#endif 	/* } */#include "posignal.h"#include "log.h"#include "auth.h"#include "txfr.h"#include "deslogin.h"extern unsigned long inBytes, outBytes;#if !defined(WRITE_RETRY_TIME)#define WRITE_RETRY_TIME	1000	/* ms retry time on EAGAIN for write */#endif#if !defined(sparc) && !defined(sun3)volatile #endifunsigned gotalarm = 0;/* * In POSIX, the read or write call will be interrupted by a signal.  If so,  * the read may return with -1 and errno=EINTR, or, if multi-byte, may return  * a result of the number of bytes read. */void alarmCatcher(sig)   int sig;{   gotalarm++;}#if !defined(sparc) && !defined(sun3)volatile #endifunsigned txfrChld = 0;void chldCatcher(sig)   int sig;{   txfrChld++;   if (debug) {      log("%s: (txfr) SIGCHLD #%d\n", progName, txfrChld);   }}int recvData(fd, buf, size, cbuf, cs)   int fd;   char *buf, *cbuf;   unsigned size;   register cipherKey cs;{   register int rcount = read(fd, cbuf, size);   if (debug > 2) {      log("read(%d,%d)=%d(%d)\n", fd, size, rcount, errno);   }   if (rcount <= 0) {      /*       * Linux (and some other machines) always emit this message with       * errno "I/O error" when the connection is shut down correctly.       */      if ((rcount < 0) && ((verbose) || (debug))) {	 log("%s: (recv) read %d bytes fd %d failed--%s\n",	     progName, size, fd, ERRMSG);      }      return rcount;   }   cipherData(buf, cbuf, rcount, cs);   return rcount;}int sendData(fd, buf, size)   int fd;   char *buf;   unsigned size;{   int wcount;   wcount = write(fd, buf, size);   if (debug > 2) {      log("write(%d,%d)=%d(%d)\n", fd, size, wcount, errno);   }   /*     * We could get -1 and EAGAIN if a write is attempted for less than     * fpathconf(_PC_PIPE_BUF) and there isn't space yet.  In this case,    * if we just returned immediately, we could chew up a lot of CPU time    * with select continually returning true. Gag! Just pause for a     * WRITE_RETRY_TIME and return.    */   if (wcount == 0) {      log("%s: (send) write %d bytes fd %d returned 0\n", progName, size, fd);      exit(1);   }   if (wcount < 0) {      if (errno != EAGAIN) {	 log("%s: (send) write %d bytes fd %d failed--%s\n",	    progName, size, fd, ERRMSG);	 exit(1);      }      if (debug || verbose) {	 log("%s: (send) write %d bytes fd %d busy; Retry in %lu msec\n",	    progName, size, fd, WRITE_RETRY_TIME);      }      wcount = nap(WRITE_RETRY_TIME);      if (wcount != 0) {	 log("%s: (send) nap(%lu) failed--%s\n",	    progName, WRITE_RETRY_TIME, ERRMSG);	 exit(1);      }   }   return wcount;}/* * Set the nonblocking flag for the given fd with error report. * returns the previous nonblock flag or -1 for falure (exits). */int setnonblock(fd, flag)   int fd, flag;{   int res, oldflag;   oldflag = fcntl(fd, F_GETFL);   if (oldflag < 0) {      log("%s: (setnonblock) fcntl(%d) F_GETFL failed--%s\n", 	 progName, fd, ERRMSG);      exit(1);   }   if (flag) {      res = fcntl(fd, F_SETFL, oldflag | O_NONBLOCK);   } else {      res = fcntl(fd, F_SETFL, oldflag & ~O_NONBLOCK);   }   if (res < 0) {      log("%s: (setnonblock) fcntl(%d) F_SETFL failed--%s\n", 	 progName, fd, ERRMSG);      exit(1);   }   return ((oldflag & O_NONBLOCK) == O_NONBLOCK);}/* * Sleep for a specified number of milliseconds * * Returns: 0 if successful, -1 if unsuccessful */int nap(ms)   unsigned long ms;{   struct timeval to;   register int res;   to.tv_sec  = ms / 1000;   to.tv_usec = (ms % 1000) * 1000;   res = select(0, 0, 0, 0, &to);		/* wait a short while */   return res;}/*  * Select on the master pty before the slave has a successful open behaves * totally differently on every machine I tried.  There appears to be no * machine independent way to do both of the following: * *  1) Determine when the slave has been opened *  2) Prevent multiple processes from opening the slave. * * If there was a way to do 2, what we should *really* do is to do a full * bore authentication protocal between the slave and tha master.  Since * there is no way to achieve 2 except file permissions on the slave pty, * there isn't any point; anyone that can break the file permissions can * intercept data between the slave pty and the process using it (which * will be the user shell, so we can't help here. *  * On DEC Alpha, select lies and returns true for read on the master pty when  * the slave hasn't opened it yet.  A subsequent read will fail errno=EIO. * It fails to return true for write at all. * It fails to return true for exception. * * On DEC Ultrix 4.3, select returns true for read on the master pty when  * the slave hasn't opened it yet.  A subsequent read will fail errno=EIO. * It also returns true for write.  * It also returns true for exception. * * Do whatever it takes to make sure that txfr won't abort before the slave * side of the pty has been opened.  Because select is so unpredicable before * the slave is open, we have to throw up our hands and poll until we don't * get an I/O error.  Since we can't poll transparently we have to have * the process on the slave side send us something with mkReady. * * Returns: 0 - if timeout *          1 - success *         <0 - failure (Could be from SIGCHLD) */int waitReady(fd, buf, size, timeout)    int fd;    char *buf;   unsigned size;   unsigned timeout;{   pfv oldHandler;   struct timeval to;   int res = 0;   gotalarm = 0;   oldHandler = posignal(SIGALRM, alarmCatcher);   alarm(timeout / 1000);   while (!res && !gotalarm) {      res = read(fd, buf, size);      if (debug) {	 log("%s: (waitReady) read=%d gotalarm=%d\n", progName, res, gotalarm);      }      if (res < 0) {	 if (errno != EIO) {	    break;	 }	 to.tv_sec  = 0;		/* some selects update to on return */	 to.tv_usec = 500000;		/* 500 ms polling interval */	 res = select(0, 0, 0, 0, &to);		/* wait a short while */	 if (gotalarm) {	    res = 0;	/* it was -1 from select being interrupted by signal */	 } /* res could still be -1 if SIGCHLD */      }   }   alarm(0);   posignal(SIGALRM, oldHandler);   return res;		/* res > 0 for success, < 0 failure, 0 timeout */}int mkReady(fd, buf, size, timeout)    int fd;    char *buf;   unsigned size;   unsigned timeout;{   pfv oldHandler;   struct timeval to;   int res = 0;   gotalarm = 0;   oldHandler = posignal(SIGALRM, alarmCatcher);   alarm(timeout / 1000);   while (!res && !gotalarm) {      res = write(fd, buf, size);      if (debug > 1) {	 log("%s: (mkReady) write=%d gotalarm=%d\n", progName, res, gotalarm);      }      if (res < 0) {	 if (errno != EIO) {	    break;	 }	 to.tv_sec  = 0;		/* some selects update to on return */	 to.tv_usec = 500000;		/* 500 ms polling interval */	 res = select(0, 0, 0, 0, &to);		/* wait a short while */	 if (gotalarm) {	    res = 0;	/* it was -1 from select being interrupted by signal */	 }       }   }   alarm(0);   posignal(SIGALRM, oldHandler);   return res;		/* res > 0 for success, < 0 failure, 0 timeout */}/* * Transfer data bidirectionally using a buffer of given size. * Returns when timeout (ms) expires (0 = timeout) *         or when eof (res = 1) *         or error (res = -1); *         or interrupt (res = -2) (or exception, slave closed pty) * * NOTE: there appears to be a bug in HP-UX where a read can return EAGAIN *       even if select returns true for both read and write on a tty fd. * On the DEC Alpha: *       If the slave side of a pty isn't open yet, the master side's select *       will return true for read, and the read call will fail with  *       EIO. */int txfr(fd1, fdin, fdout, bufSize, timeout, key)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -