📄 monkey.c
字号:
result = kErrLISTFailed; cip->errNo = result; } result = kNoErr; } else if (result == kErrGeneric) { result = kErrLISTFailed; if ((cip->errNo == kErrAcceptDataSocket) || (cip->errNo == kErrNewStreamSocket)) MonkeyCloseHost(cip); cip->errNo = result; }#else /* NO_SIGNALS */ if (result == 0) { /* This line sets the buffer pointer so that the first thing * BufferGets will do is reset and fill the buffer using * real I/O. */ secBufPtr = secondaryBuf + sizeof(secondaryBuf); secBufLimit = (char *) 0; for ( ; ; ) {skip: nread = BufferGets(line, sizeof(line), cip->dataSocket, secondaryBuf, &secBufPtr, &secBufLimit, sizeof(secondaryBuf)); if (nread <= 0) { if (nread < 0) break; } else { cip->bytesTransferred += (long) nread; ftype = line[0]; if ((ftype == '-') || (ftype == 'd')) { tok = line + strlen(line); while (*--tok != ' ') { if (tok < line) goto skip; } fname[0] = ftype; fname[1] = ' '; fname[2] = '\0'; STRNCAT(fname, tok + 1); AddLine(lines, fname); } } } result = FTPEndDataCmd(cip, 1); FTPStopIOTimer(cip); if (result < 0) { result = kErrLISTFailed; cip->errNo = result; } result = kNoErr; } else if (result == kErrGeneric) { result = kErrLISTFailed; if ((cip->errNo == kErrAcceptDataSocket) || (cip->errNo == kErrNewStreamSocket)) MonkeyCloseHost(cip); cip->errNo = result; }#endif /* NO_SIGNALS */ return (result);} /* FileLIST */static intFileNLST(const FTPCIPtr cip, FTPLineListPtr lines, const char *lsflags){ char secondaryBuf[512]; char line[128]; char *cp1, *cp2, *tok; int ftype; char fname[128];#ifndef NO_SIGNALS char *secBufPtr, *secBufLimit; int nread; int result;#else /* NO_SIGNALS */ SReadlineInfo lsSrl; int result;#endif /* NO_SIGNALS */ InitLineList(lines); FTPInitIOTimer(cip); FTPStartIOTimer(cip); result = FTPStartDataCmd(cip, kNetReading, kTypeAscii, (longest_int) 0, "%s%s", "NLST", lsflags);#ifdef NO_SIGNALS if (result == 0) { if (InitSReadlineInfo(&lsSrl, cip->dataSocket, secondaryBuf, sizeof(secondaryBuf), (int) cip->xferTimeout, 1) < 0) { /* Not really fdopen, but close in what we're trying to do. */ result = kErrFdopenR; cip->errNo = kErrFdopenR; /* Error(cip, kDoPerror, "Could not fdopen.\n"); */ return (result); } for (;;) { result = SReadline(&lsSrl, line, sizeof(line) - 2); if (result == kTimeoutErr) { /* timeout */ /* Error(cip, kDontPerror, "Could not directory listing data -- timed out.\n"); */ cip->errNo = kErrDataTimedOut; return (cip->errNo); } else if (result == 0) { /* end of listing -- done */ cip->numListings++; break; } else if (result < 0) { /* error */ /* Error(cip, kDoPerror, "Could not read directory listing data"); */ result = kErrLISTFailed; cip->errNo = kErrLISTFailed; break; } else if (strstr(line, "ermission denied") != NULL) { result = kErrLISTFailed; cip->errNo = kErrLISTFailed; break; } cip->bytesTransferred += (long) result; /* Parse files out of possibly * multicolumn output. */ for (cp1 = line; ; cp1 = NULL) { tok = strtok(cp1, " \r\n\t"); if (tok == NULL) break; cp2 = tok + strlen(tok) - 1; switch (*cp2) { case '*': case '@': *cp2 = '\0'; ftype = '-'; break; case '/': *cp2 = '\0'; ftype = 'd'; break; default: ftype = '-'; break; } fname[0] = (char) ftype; fname[1] = (char) ' '; fname[2] = '\0'; STRNCAT(fname, tok); AddLine(lines, fname); } } DisposeSReadlineInfo(&lsSrl); result = FTPEndDataCmd(cip, 1); FTPStopIOTimer(cip); if (result < 0) { result = kErrLISTFailed; cip->errNo = result; } result = kNoErr; } else if (result == kErrGeneric) { result = kErrLISTFailed; if ((cip->errNo == kErrAcceptDataSocket) || (cip->errNo == kErrNewStreamSocket)) MonkeyCloseHost(cip); cip->errNo = result; }#else /* NO_SIGNALS */ if (result == 0) { /* This line sets the buffer pointer so that the first thing * BufferGets will do is reset and fill the buffer using * real I/O. */ secBufPtr = secondaryBuf + sizeof(secondaryBuf); secBufLimit = (char *) 0; for ( ; ; ) { nread = BufferGets(line, sizeof(line), cip->dataSocket, secondaryBuf, &secBufPtr, &secBufLimit, sizeof(secondaryBuf)); if (nread <= 0) { if (nread < 0) break; } else { cip->bytesTransferred += (long) nread; /* Parse files out of possibly * multicolumn output. */ for (cp1 = line; ; cp1 = NULL) { tok = strtok(cp1, " \r\n\t"); if (tok == NULL) break; cp2 = tok + strlen(tok) - 1; switch (*cp2) { case '*': case '@': *cp2 = '\0'; ftype = '-'; break; case '/': *cp2 = '\0'; ftype = 'd'; break; default: ftype = '-'; break; } fname[0] = ftype; fname[1] = ' '; fname[2] = '\0'; STRNCAT(fname, tok); AddLine(lines, fname); } } } result = FTPEndDataCmd(cip, 1); FTPStopIOTimer(cip); if (result < 0) { result = kErrLISTFailed; cip->errNo = result; } result = kNoErr; } else if (result == kErrGeneric) { result = kErrLISTFailed; if ((cip->errNo == kErrAcceptDataSocket) || (cip->errNo == kErrNewStreamSocket)) MonkeyCloseHost(cip); cip->errNo = result; }#endif /* NO_SIGNALS */ return (result);} /* FileNLST */static intFileList(const FTPCIPtr cip){ int p, rc; FTPLinePtr lp; struct timeval t0; if (gHaveDir == 1) { DisposeLineListContents(&gDir); gHaveDir = 0; } p = Ri(3); if (p == 0) { MsgStart("ls -F"); gettimeofday(&t0, NULL); rc = FileNLST(cip, &gDir, " -F"); } else if (p == 1) { MsgStart("dir"); gettimeofday(&t0, NULL); rc = FileLIST(cip, &gDir, ""); } else { MsgStart("ls -CF"); gettimeofday(&t0, NULL); rc = FileNLST(cip, &gDir, " -CF"); } if (rc >= 0) { gHaveDir = 1; printf("%7.3f sec " PRINTF_LONG_LONG " bytes, %.1f k/s\n", cip->sec, cip->bytesTransferred, cip->kBytesPerSec); if (gPrintLists != 0) { for (lp=gDir.first; lp != NULL; lp=lp->next) printf(" %s\n", lp->line); } } return (rc);} /* FileList */static char *RandomFile(void){ FTPLinePtr lp; int nfiles, pick, i; if (gHaveDir == 0) return NULL; for (lp=gDir.first, nfiles=0; lp != NULL; lp=lp->next) if (lp->line[0] == '-') nfiles++; if (nfiles == 0) return NULL; pick = Ri(nfiles); for (lp=gDir.first, i=0; lp != NULL; lp=lp->next) { if (lp->line[0] == '-') { if (pick == i) return (lp->line + 2); i++; } } return NULL;} /* RandomFile */static char *RandomDir(void){ FTPLinePtr lp; int ndirs, pick, i; if (gHaveDir == 0) return NULL; for (lp=gDir.first, ndirs=0; lp != NULL; lp=lp->next) if (lp->line[0] == 'd') ndirs++; if (ndirs == 0) return NULL; pick = Ri(ndirs); for (lp=gDir.first, i=0; lp != NULL; lp=lp->next) { if (lp->line[0] == 'd') { if (pick == i) return (lp->line + 2); i++; } } return NULL;} /* RandomDir */static voidShell(const FTPCIPtr cip){ int i, maxw, needlist = 1; int p; char *randdir, *randfile; char s1[128]; char errstr[128]; struct timeval t0; double dura; time_t now; int result; for (i=1; i<c_max; i++) { gWeights[i] += gWeights[i-1]; } maxw = gWeights[i-1]; for (gLoops = 0; ((gMaxLoops < 1) || (gLoops < gMaxLoops)); gLoops++) { time(&now); if ((gAlarmClock != 0) && (now >= gAlarmClock)) { printf("%-5d time up!\n", gLoops); return; } p = Ri(maxw); for (i=0; i<c_max; i++) { if (p < gWeights[i]) break; } /* Lost connection? */ if (cip->connected == 0) i = c_quit; switch (i) { case c_quit: if (gQuitMode == kYesCloseYesQuit) { printf("%-5d quit\n", gLoops); return; } else if (gQuitMode == kYesCloseNoQuit) { /* Close, then re-open */ MonkeyCloseHost(cip); sleep((unsigned int) Ri(3) + 3); MonkeyOpenHost(cip); needlist = 1; } /* else kNoCloseNoQuit */ break; case c_cd: if (Rp(20)) { (void) MonkeyChdir(cip, ".."); needlist = 1; } else if (Rp(20)) { (void) MonkeyChdir(cip, gMainDir); needlist = 1; } else {cd: if (needlist) { if (FileList(cip) >= 0) needlist = 0; ++gLoops; } if ((needlist == 0) && ((randdir = RandomDir()) != NULL)) { if (MonkeyChdir(cip, randdir) == 0) needlist = 1; } else { (void) MonkeyChdir(cip, gMainDir); needlist = 1; } } break; case c_pwd: MsgStart("pwd"); gettimeofday(&t0, NULL); (void) FTPGetCWD(cip, s1, sizeof(s1)); /* pwd */ dura = FTPDuration(&t0); printf("%7.3f sec\n", dura); break; case c_dir: if (FileList(cip) >= 0) needlist = 0; break; case c_get: if (needlist) { if (FileList(cip) >= 0) needlist = 0; ++gLoops; } if (needlist == 0) { randfile = RandomFile(); if (randfile == NULL) goto cd; /* pick a random file to get */ MsgStart("get %s", randfile); gettimeofday(&t0, NULL); result = FTPReadOneFile(cip, randfile); dura = FTPDuration(&t0); if (result == kNoErr) { printf("%7.3f sec " PRINTF_LONG_LONG " bytes, %.1f k/s\n", cip->sec, cip->bytesTransferred, cip->kBytesPerSec); } else { printf("%7.3f sec %s\n", dura, FTPStrError2(cip, result, errstr, sizeof(errstr), kErrCouldNotStartDataTransfer)); } } break; } sleep((unsigned int) Ri(4)); }} /* Shell */main_void_return_tmain(int argc, char **argv){ int result, c; int seed; time_t now; struct tm *ltp; char dstr[64]; char *password; GetoptInfo opt; InitWinsock(); result = FTPInitLibrary(&li); if (result < 0) { fprintf(kErrStream, "Init library error %d.\n", result); DisposeWinsock(); exit(3); } result = FTPInitConnectionInfo(&li, &fi, kDefaultFTPBufSize); if (result < 0) { fprintf(kErrStream, "Init connection info error %d.\n", result); DisposeWinsock(); exit(4); } fi.debugLog = NULL; fi.errLog = kErrStream; STRNCPY(fi.user, "anonymous"); STRNCPY(fi.pass, "monkey@bowser.nintendo.co.jp"); seed = -1; GetoptReset(&opt); while ((c = Getopt(&opt, argc, argv, "a:QRSCD:m:ls:d:e:P:p:u:")) > 0) switch(c) { case 'a': time(&gAlarmClock); gAlarmClock += (time_t) atoi(opt.arg); break; case 'm': gMaxLoops = atoi(opt.arg); break; case 's': seed = atoi(opt.arg); break; case 'C': gCLNT = 1; break; case 'D': gMainDir = opt.arg; break; case 'l': gPrintLists = !gPrintLists; break; case 'Q': gQuitMode = kNoCloseNoQuit; break; case 'R': gQuitMode = kYesCloseNoQuit; break; case 'S': gQuitMode = kYesCloseYesQuit; break; case 'P': fi.port = atoi(opt.arg); break; case 'u': STRNCPY(fi.user, opt.arg); break; case 'p': STRNCPY(fi.pass, opt.arg); /* Don't recommend doing this! */ break; case 'e': if (strchr(opt.arg, ',') == NULL) gErrOutOnPurpose = atoi(opt.arg); else (void) sscanf(opt.arg, "%d,%d", &gErrOutOnPurpose, &gErrOutTypeToUse); break; case 'd': fi.debugLog = fopen(opt.arg, "a"); break; default: Usage(); } if (opt.ind > argc - 1) Usage(); if (strcmp(fi.user, "anonymous") && strcmp(fi.user, "ftp")) { if (fi.pass[0] == '\0') { password = GetPass2("Password: "); if (password != NULL) { STRNCPY(fi.pass, password); /* Don't leave cleartext password in memory. */ memset(password, 0, strlen(fi.pass)); } } } /* The library destroys it each time. */ STRNCPY(gPasswd, fi.pass); if (seed < 0) {#if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__) seed = ((unsigned int) time(NULL)) & 0xffff;#else seed = (time(NULL) & 0xff) << 8; seed |= (getpid() & 0xff);#endif } srand((unsigned int) seed); gSeed = seed; STRNCPY(fi.host, argv[opt.ind]); gettimeofday(&gMonkeyStart, NULL); MonkeyOpenHost(&fi); Shell(&fi); MonkeyCloseHost(&fi); time(&now); ltp = localtime(&now); strftime(dstr, sizeof(dstr), "%Y-%m-%d %H:%M:%S", ltp); printf("\n-- Finished at %s (program runtime = %.1f sec) --\n", dstr, FTPDuration(&gMonkeyStart)); DisposeWinsock(); exit(0);} /* main */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -