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

📄 txfr.c

📁 一个使用des加密传输的unix下的login程序的服务器端和客户端
💻 C
📖 第 1 页 / 共 2 页
字号:
   int fd1, fdout, fdin;   unsigned bufSize;   unsigned timeout;   keyType key;{   char 	*cbuf = (char *) malloc(bufSize);   char 	*buf1 = (char *) malloc(bufSize);    char 	*bufin = (char *) malloc(bufSize);    struct timeval to, tov, *top = (struct timeval *) 0;	/* Not POSIX */   unsigned     max_fd = fd1;   fd_set       rmask, wmask, emask;   int 	        result = -1, oldblockin = 0, oldblockout = 0, oldblock1 = 0;   int		fdinEof = 0;   cipherKey    cs1 = (cipherKey) 0, csin = (cipherKey) 0;   pfv		oldChld;   register int res, rcount1 = 0, rcountin = 0, wcount;   register char *bufp1, *bufpin;   if (fdout > max_fd) max_fd = fdout;   if (fdin > max_fd) max_fd = fdin;   if (buf1  == (char *) 0) goto txfr_done;   if (bufin == (char *) 0) goto txfr_done;   if (cbuf  == (char *) 0) goto txfr_done;#if 0   key = (keyType) 0;			/* for debug! */#endif   cs1  = mkCipherKey(key, 1);			/* decrypt */   csin = mkCipherKey(key, 0);			/* encrypt */   if (cs1   == (cipherKey) 0) goto txfr_done;   if (csin  == (cipherKey) 0) goto txfr_done;   bufp1 = buf1;   bufpin = bufin;   if (timeout != 0) {      to.tv_sec  = (timeout / 1000);      to.tv_usec = (timeout % 1000) * 1000;      top = &tov;   }   /*    * We must use non-blocking IO to ensure that write calls won't block.    * Read calls are safe anyway.    */   oldblock1   = setnonblock(fd1, 1);   oldblockin  = setnonblock(fdin, 1);		/* must be before fdout */   oldblockout = setnonblock(fdout, 1);   txfrChld = 0;   oldChld = posignal(SIGCHLD, chldCatcher);   if (oldChld == (pfv) -1) {      log("%s: (txfr) sigaction SIGCHLD failed--%s\n", 	 progName, ERRMSG);      exit(1);   }   FD_ZERO(&emask);    do {      FD_ZERO(&rmask); 	   FD_ZERO(&wmask); 	      FD_SET(fd1, &emask); #if 0		/* we don't want exception checking on tty or files */      FD_SET(fdout, &emask); FD_SET(fdin, &emask);#endif      if (rcount1  != 0) FD_SET(fdout, &wmask); else {	 FD_SET(fd1,  &rmask);       }      if (rcountin != 0) FD_SET(fd1,   &wmask); else if (!fdinEof) {	 FD_SET(fdin, &rmask);      }      if (top != (struct timeval *) 0) {	 tov.tv_sec  = to.tv_sec;	 tov.tv_usec = to.tv_usec;      }      if (debug > 2) {	 log("select(%u,%u,%u,%u)\n", max_fd+1,	    *(unsigned *)&rmask, *(unsigned *)&wmask, *(unsigned *)&emask);      }      if (txfrChld != 0) {		/* must be right next to select */	 break;      }	/* race condition right here */      /*       * HP-UX incorrectly declares select with int * args instead of fd_set       * Ignore the warnings for select for HP-UX.       */      res = select(max_fd+1, &rmask, &wmask, &emask, top);      if (debug > 2) {	 log("select returned: %u(%u,%u,%u)\n", res,	    *(unsigned *)&rmask, *(unsigned *)&wmask, *(unsigned *)&emask);      }      if (res == 0) {			/* timeout */	 result = 0;	 break;      }      if (res < 0) {			/* error */	 if (errno == EINTR) {		/* signal (SIGCHLD occurred) */	    result = -2;	    break;	 } else {	    log("%s: select failed--%s\n", progName, ERRMSG);	    break;	 }      }      /*       * This block will never be triggered because there is no way for a        * close on a slave pty to be detected on non-hpux systems.       */      if (FD_ISSET(fd1, &emask) #if 0      || FD_ISSET(fdout, &emask)       || FD_ISSET(fdin, &emask)#endif      ) {	/* close */	 result = -2;	 break;      }      if (FD_ISSET(fd1, &rmask)) {	 rcount1 = recvData(fd1, buf1, bufSize, cbuf, cs1);	 if (rcount1 <= 0) {	    if (rcount1 == 0) {	       result = 1;	       break;	    } else {	       result = -1;	       break;	    }	 }	 bufp1    = buf1;	 inBytes += rcount1;      }      if (FD_ISSET(fdin, &rmask)) {	 rcountin = recvData(fdin, bufin, bufSize, cbuf, csin);	 if (rcountin <= 0) {	    if (rcountin == 0) {	       fdinEof = 1;	    } else {	       result = -1;	       break;	    }	 }	 bufpin    = bufin;	 outBytes += rcountin;      }      if (FD_ISSET(fd1, &wmask)) {	 wcount    = sendData(fd1, bufpin, rcountin);	 rcountin -= wcount;	 bufpin   += wcount;      }      if (FD_ISSET(fdout, &wmask)) {	 wcount   = sendData(fdout, bufp1, rcount1);	 rcount1 -= wcount;	 bufp1   += wcount;      }   } while (txfrChld == 0);   posignal(SIGCHLD, oldChld);   setnonblock(fd1, oldblock1);   setnonblock(fdin, oldblockin);   /*    * We must blindly assume that we don't want non-blocking I/O when whe    * leave.  The reason is that two fd's may actually be connected to the    * same device.  If this is the case, then it's possible to switch the    * nonblocking mode incorrectly.  (e.g. setting NON_BLOCK in fdin also    * changes it on fdout)    */   setnonblock(fdout, oldblockin);	/* oldblockin is not a bug */txfr_done:   destroyCipherKey(&csin);   destroyCipherKey(&cs1);   free(bufin);   free(buf1);   free(cbuf);   return result;}/* * Input a string of upto size chars with timout terminated by CR LF or NULL * Input: *     size - the maximum number of characters to read * * Output: *     *buf - The characters read not including terminator *            A '\0' character is always added to the end of buf * * Returns: the number of characters read (not including terminator) *          This is true even if we were interrupted by an alarm. */int getString(fd, buf, size, timeout)   int fd;   char *buf;   unsigned size;   unsigned timeout;{   register unsigned count = size;   register char *chp = buf;   char ch;   register unsigned seconds = (unsigned) ((timeout + (1000L-1)) / 1000L);   register int rcount;   pfv oldHandler;   gotalarm = 0;   oldHandler = posignal(SIGALRM, alarmCatcher);   alarm(seconds);   while (count != 0) {      rcount = read(fd, &ch, 1);      if (rcount <= 0 || gotalarm != 0) break;      if (ch == '\r' || ch == '\n' || ch == '\0') break;      *chp   = ch;      count -= rcount;      chp   += rcount;   }   alarm(0);   posignal(SIGALRM, oldHandler);   *chp = '\0';   return size - count;}/* * Read a block of binary data of the given size.   * Return the number of bytes read (0 if timeout, or read error). */int getBlock(fd, buf, size, timeout)   int fd;   char *buf;   unsigned size;   unsigned timeout;{   register unsigned seconds = (unsigned) ((timeout + (1000-1)) / 1000);   register int rcount;   pfv oldHandler;   gotalarm = 0;   oldHandler = posignal(SIGALRM, alarmCatcher);   alarm(seconds);   rcount = read(fd, buf, size);   alarm(0);   posignal(SIGALRM, oldHandler);   /*    * No error checking is reported here. If we got a a signal, we could get    * either -1/errno=EINTR, or a short read, depending upon implementation.    */   if (rcount < 0) {      rcount = 0;   }   return rcount;}/* * Encipher all the data from sfd to dfd.  Send the entire shmeel in timeout * seconds, abort if longer.  This routine is used for sending a nologin * message contained in the file /etc/nologin. */int cipherCopy(dfd, sfd, timeout, key)   int dfd, sfd;   unsigned timeout;   keyType key;{   int 		rcount, wcount = 0;   char 	buf[128], cbuf[128];    register char *bufp = buf;   pfv 		oldHandler;   cipherKey 	cs = mkCipherKey(key, 0);			/* encrypt */   if (cs == (cipherKey) 0) return -1;   gotalarm = 0;   oldHandler = posignal(SIGALRM, alarmCatcher);   alarm(timeout);   do {      while ((rcount = read(sfd, bufp, 1)) > 0) {	 if (*bufp == '\n') {	    *bufp++ = '\r';	    *bufp   = '\n';	 }	 bufp++;	 if (bufp >= buf + 127 || gotalarm != 0) break;      }      if (gotalarm != 0) break;      rcount = bufp - buf;      bufp   = buf;      cipherData(cbuf, buf, rcount, cs);      wcount = write(dfd, cbuf, rcount);      if (wcount <= 0) break;   } while (gotalarm == 0);   alarm(0);   posignal(SIGALRM, oldHandler);   destroyCipherKey(&cs);   if (rcount < 0 || wcount < 0) return -1;   return 0;}

⌨️ 快捷键说明

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