main(int argc,char **argv) { openit(argc,argv); sioset(fdata); sioset(pj__get_pto_errno); sioset(clearerr); sioset(feof); sioset(ferror); sioset(pj_errno_errcode); sioset(fopen); sioset(fclose); sioset(fseek); sioset(ftell); sioset(fflush); sioset(rewind); sioset(fread); sioset(fgetc); sioset(fgets); sioset(ungetc); sioset(fwrite); sioset(fputc); sioset(fputs); sioset(fprintf); sioset(printf); sioset(sprintf); closeit(); }
/* * Receive files with Zmodem, Ymodem or Xmodem. * This receiver will figure out what to do, you should * be able to send anything. */ int zmrcvfiles(int want1k, int wantg) { int rc; Syslog('+', "%s: start receive", protname()); get_frame_buffer(); Rxtimeout = 10; if (secbuf == NULL) secbuf = malloc(MAXBLOCK+1); tryzhdrtype = ZRINIT; if ((rc = tryz()) < 0) { Syslog('+', "%s: could not initiate receive, rc=%d", protname(), rc); } else { if (rc == 0) { if (protocol == ZM_ZMODEM) { Syslog('+', "%s: switching to Ymodem", protname()); protocol = ZM_YMODEM; } rc = ymrcvfiles(want1k, wantg); goto fubar; } /* * Zmodem receiver */ switch (rc) { case ZCOMPL: rc = 0; break; case ZFILE: rc = rzfiles(); break; } } fubar: if (fout) { if (closeit(0)) { WriteError("%s: Error closing file", protname); } } if (secbuf) free(secbuf); secbuf = NULL; io_mode(0, 1); /* Normal raw mode */ /* * Some programs send some garbage after the transfer, eat these. * This also introduces a pause after the transfer, some clients * need this. */ purgeline(200); Syslog('+', "%s: end receive rc=%d", protname(), rc); return abs(rc); }
main(int argc,char **argv) { openit(argc,argv); prtf(W_RASTS, rasts); prtf(W_BEHIND, behind); prtf(W_ONERAST, onerast); prtf(W_YDOTS, ydots); closeit(); }
/* called by signal interrupt or terminate to clean things up */ static void bibi(int signum) { if (Zmodem) zmputs(Attn); canit(); mode(0); closeit(); fprintf(stderr, "lrz: caught signal %d; exiting", signum); exit(128 + signum); }
static int wcreceive(int argc, char **argp) { int c; if (Batch || argc == 0) { Crcflg = 1; if (!Quiet) fprintf(stderr, rbmsg, Progname, "sz"); if ((c = tryz())) { if (c == ZCOMPL) return OK; if (c == ERROR) goto fubar; c = rzfiles(); if (c) goto fubar; } else { for (;;) { if (wcrxpn(secbuf) == ERROR) goto fubar; if (secbuf[0] == 0) return OK; if (procheader(secbuf) == ERROR) goto fubar; if (wcrx() == ERROR) goto fubar; } } } else { Bytesleft = DEFBYTL; Filemode = 0; Modtime = 0L; procheader(""); strcpy(Pathname, *argp); checkpath(Pathname); fprintf(stderr, "\n%s: ready to receive %s\r\n", Progname, Pathname); if ((fout = fopen(Pathname, "w")) == NULL) return ERROR; if (wcrx() == ERROR) goto fubar; timing(1); } return OK; fubar: canit(); if (Topipe && fout) { pclose(fout); return ERROR; } if (fout) closeit(); return ERROR; }
bool lockfile::fdok () { struct stat sb, fsb; if (fd < 0 || stat (path.cstr(), &sb) < 0 || fstat (fd, &fsb) < 0 || !stat_unchanged (&sb, &fsb)) { closeit (); return false; } return true; }
int tcprcvfiles(void) { int rc, bufl; int filesize, filetime; char *filename = NULL, *p; Syslog('+', "TCP: start receive files"); if (getsync()) { WriteError("TCP: can't get synchronization"); return FTNERR_FTRANSFER; } next: if ((rc = tcp_rblk(rxbuf, &bufl)) == 0) { if (strncmp(rxbuf, "SN", 2) == 0) { rc = tcp_sblk((char *)"RN", 2, TCP_CMD); return rc; } else if (*rxbuf == 'S') { p = strchr(rxbuf+2, ' '); if (p != NULL) *p=0; else return 1; filename = xstrcpy(rxbuf+2); p++; filesize = strtol(p, &p, 10); filetime = strtol(++p, (char **)NULL, 10); } else return rc==0?1:rc; if (strlen(filename) && filesize && filetime) { rc = receivefile(filename,filetime,filesize); if (filename) free(filename); filename = NULL; } if (fout) { if (closeit(0)) WriteError("TCP: error closing file"); (void)tcp_sblk((char *)"FERROR",6,TCP_CMD); } else goto next; } if (rc) { WriteError("TCP: receive error: rc=%d", rc); return FTNERR_FTRANSFER; } Syslog('+', "TCP: receive files completed"); return 0; }
void test2() { int i; create_big_file("test2file"); unsigned char key[KEY_LENGTH]; for (i = 0; i < KEY_LENGTH; ++i) key[i] = (unsigned char) (110 - i); for (i = 1; i <= 3; ++i) { int p2fd; pyramid* p = openit("test2file", &p2fd, key, i); print_ids(p, 2); closeit(p2fd, p, i); } }
bool lockfile::openit () { struct stat sb; if (fd >= 0) closeit (); errno = 0; if (lstat (path.cstr(), &sb) >= 0 && !checkstat (path.cstr(), sb)) return false; else if (errno && errno != ENOENT) { warn << path << ": " << strerror (errno) << "\n"; return false; } /* N.B., 0600 (not 0644) to stop others from wedging us with * LOCK_SH. Anyone with read permission to a file can flock it * with LOCK_SH, thereby blocking those with write permission who * want to lock it with LOCK_EX. */ fd = open (path.cstr(), O_RDWR|O_CREAT, 0600); if (fd < 0) { warn << path << ": " << strerror (errno) << "\n"; return false; } close_on_exec (fd); errno = 0; if (fstat (fd, &sb) < 0 || !checkstat (path.cstr(), sb)) { if (errno) warn << "fstat (" << path << "): " << strerror (errno) << "\n"; closeit (); return false; } return true; }
void test3() { int i; unsigned char key[KEY_LENGTH]; for (i = 0; i < KEY_LENGTH; ++i) key[i] = (unsigned char) (rand() % 256); create_big_file("test3file"); for (i = 1; i <= 3; ++i) { int p3fd; pyramid *p = openit("test3file", &p3fd, key, i); print_ids(p, 3); move_id_in_pyramid(p, p, randomly_select_id_pyramid(p)); move_id_in_pyramid(p->next_level, p, randomly_select_id_pyramid(p->next_level)); move_id_in_pyramid(p->next_level->next_level, p, randomly_select_id_pyramid(p->next_level->next_level)); print_ids(p, 3); closeit(p3fd, p, i); } }
void FDMAP_DATA :: handle_select_r( void ) { if ( waitfor_connect ) { // this means the connect call has failed and the // connection was not established. waitfor_connect = false; #if VERBOSE printf( "fd %d NOT connected\n", fd ); #endif closeit(); dellist->add( this ); dellist->add( other_fd ); return; } other_fd->_handle_select_r(); }
void FDMAP_DATA :: _handle_select_r( void ) { int bufsize = buf.contig_writeable(); int cc = th->read( other_fd->fd, buf.write_pos(), bufsize ); int e = cc < 0 ? errno : 0; #if VERBOSE printf( "read %d of %d: %s\n", cc, bufsize, e == 0 ? "no error" : strerror( e )); #endif if ( cc < 0 ) { if ( e != EAGAIN ) { closeit(); dellist->add( this ); dellist->add( other_fd ); } } else if ( cc == 0 ) { other_fd->can_read = false; // flag that the connection is dead // but don't close the writer yet. // there could still be data in the buffer. // let the writer wake up and when the // writer empties the buffer it will // do the closeit. want_close = true; want_write = true; } else { buf.record_write( cc ); want_write = true; } if ( buf.full() ) other_fd->can_read = false; }
static int receivefile(char *fn, time_t ft, off_t fs) { int rc, bufl; Syslog('+', "TCP: receive \"%s\" (%lu bytes) dated %s",fn,fs,date(ft)); strcpy(txbuf,"ROK"); fout = openfile(fn, ft, fs, &rxbytes, resync); gettimeofday(&starttime, &tz); sbytes = rxbytes; if (fs == rxbytes) { Syslog('+', "TCP: skipping %s", fn); fout = NULL; rc = tcp_sblk((char *)"RS",2,TCP_CMD); return rc; } if (!fout) return 1; bufl = strlen(txbuf); rc = tcp_sblk(txbuf,bufl,TCP_CMD); while ((rc = tcp_rblk(rxbuf, &bufl)) == 0) { if (rx_type == TCP_CMD) break; if (fwrite(rxbuf, 1, bufl, fout) != bufl) break; rxbytes += bufl; } if (rc) return rc; if (rx_type == TCP_CMD && bufl == 3 && strncmp(rxbuf,"EOF",3) == 0) { if (ftell(fout) == fs) { closeit(1); rc = tcp_sblk((char *)"FOK",3,TCP_CMD); return rc; } else return 1; } else return 1; }
static int wcrx(void) { int sectnum, sectcurr; char sendchar; int cblklen; /* bytes to dump this block */ Firstsec = TRUE; sectnum = 0; Eofseen = FALSE; sendchar = Crcflg ? WANTCRC : NAK; for (;;) { xsendline(sendchar); /* send it now, we're ready! */ Lleft = 0; /* Do read next time ... */ sectcurr = wcgetsec(secbuf, (sectnum & 0177) ? 50 : 130); report(sectcurr); if (sectcurr == ((sectnum + 1) & 0377)) { sectnum++; cblklen = Bytesleft > Blklen ? Blklen : Bytesleft; if (putsec(secbuf, cblklen) == ERROR) return ERROR; if ((Bytesleft -= cblklen) < 0) Bytesleft = 0; sendchar = ACK; } else if (sectcurr == (sectnum & 0377)) { zperr("Received dup Sector"); sendchar = ACK; } else if (sectcurr == WCEOT) { if (closeit()) return ERROR; xsendline(ACK); Lleft = 0; /* Do read next time ... */ return OK; } else if (sectcurr == ERROR) return ERROR; else { zperr("Sync Error"); return ERROR; } } }
int main(int argc, char **argv) { struct cmph_hash_obj o; o.keyfile = "keys.txt"; const char *key = "1.11.145.30"; struct mapfile ip_map; int err; if (!(err = create_or_open_disk_hash(&o))) { fmmap_ip_rw(&ip_map,"keys.txt.bin"); qsort(ip_map.map,ip_map.s.st_size/4, 4, int_cmp); walk_ips(&ip_map); unsigned int id = cmph_search(o.hash, key, (cmph_uint32)strlen(key)); fprintf(stderr, "Id:%u\n", id); id = cmph_search(o.hash, "192.168.176.1", (cmph_uint32)strlen("192.168.176.1")); fprintf(stderr, "Invalid Id:%u\n", id); closeit(&o); } else { fprintf(stderr, "Cannot create hash, err %d, errno %d\n", err, errno); } return 0; }
static void dotest(void) { printf("Opening directory...\n"); openit(); printf("Running tests...\n"); /* read the whole directory */ firstread(); /* make sure eof behaves right */ readateof(); /* read all the filenames again by seeking */ readallonebyone(); /* try reading at eof */ doreadateof(); /* read a bunch of the filenames over and over again */ readallrandomly(); /* rewind and read the whole thing again, to make sure that works */ doreadat0(); /* do invalid reads */ dobadreads(); /* rewind again to make sure the invalid attempts didn't break it */ doreadat0(); printf("Closing directory...\n"); closeit(); }
/* * Receive a file with ZMODEM protocol * Assumes file name frame is in secbuf */ static int rzfile(void) { int c, n; long last_rxbytes = 0; long not_printed = 0; last_bps = 0; Eofseen = FALSE; n = 20; rxbytes = 0l; if (procheader(secbuf) == ERROR) { return (tryzhdrtype = ZSKIP); } for (;;) { stohdr(rxbytes); zshhdr(ZRPOS, Txhdr); nxthdr: switch (c = zgethdr(Rxhdr, 0)) { default: vfile("lrzfile: zgethdr returned %d", c); return ERROR; case ZNAK: case TIMEOUT: if (--n < 0) { vfile("lrzfile: zgethdr returned %d", c); return ERROR; } case ZFILE: zrdata(secbuf, MAX_BLOCK); continue; case ZEOF: if (rclhdr(Rxhdr) != rxbytes) { /* * Ignore eof if it's at wrong place - force * a timeout because the eof might have gone * out before we sent our zrpos. */ errors = 0; goto nxthdr; } if (Verbose > 1) { int minleft = 0; int secleft = 0; last_bps = (rxbytes / timing(0)); if (last_bps > 0) { minleft = (Bytesleft - rxbytes) / last_bps / 60; secleft = ((Bytesleft - rxbytes) / last_bps) % 60; } fprintf(stderr, "\rBytes Received: %7ld/%7ld BPS:%-6d \r\n", rxbytes, Bytesleft, last_bps); } closeok = 1; if (closeit()) { tryzhdrtype = ZFERR; vfile("lrzfile: closeit returned <> 0"); return ERROR; } vfile("lrzfile: normal EOF"); return c; case ERROR: /* Too much garbage in header search error */ if (--n < 0) { vfile("lrzfile: zgethdr returned %d", c); return ERROR; } zmputs(Attn); continue; case ZSKIP: closeit(); vfile("lrzfile: Sender SKIPPED file"); return c; case ZDATA: if (rclhdr(Rxhdr) != rxbytes) { if (--n < 0) { return ERROR; } zmputs(Attn); continue; } moredata: if (Verbose > 1 && (not_printed > 7 || rxbytes > last_bps / 2 + last_rxbytes)) { int minleft = 0; int secleft = 0; last_bps = (rxbytes / timing(0)); if (last_bps > 0) { minleft = (Bytesleft - rxbytes) / last_bps / 60; secleft = ((Bytesleft - rxbytes) / last_bps) % 60; } fprintf(stderr, "\rBytes Received: %7ld/%7ld BPS:%-6d ETA %02d:%02d ", rxbytes, Bytesleft, last_bps, minleft, secleft); last_rxbytes = rxbytes; not_printed = 0; if (nodeinf != -1 && ((time(0) - lup) > 5)) { lseek(nodeinf, 0, SEEK_SET); nin.ddn_bpsrate = last_bps; sprintf(nin.ddn_activity, "UL: %-34.34s", myname); write(nodeinf, &nin, sizeof(struct DayDream_NodeInfo)); lup = time(0); } } else if (Verbose) not_printed++; switch (c = zrdata(secbuf, MAX_BLOCK)) { case ZCAN: vfile("lrzfile: zgethdr returned %d", c); return ERROR; case ERROR: /* CRC error */ if (--n < 0) { vfile ("lrzfile: zgethdr returned %d", c); return ERROR; } zmputs(Attn); continue; case TIMEOUT: if (--n < 0) { vfile ("lrzfile: zgethdr returned %d", c); return ERROR; } continue; case GOTCRCW: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; stohdr(rxbytes); zshhdr(ZACK, Txhdr); xsendline(XON); goto nxthdr; case GOTCRCQ: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; stohdr(rxbytes); zshhdr(ZACK, Txhdr); goto moredata; case GOTCRCG: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; goto moredata; case GOTCRCE: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; goto nxthdr; } } } }
void FDMAP_DATA :: handle_select_w( void ) { if ( waitfor_connect ) { struct sockaddr_in name; socklen_t namelen = sizeof( name ); if ( getpeername( fd, (struct sockaddr *)&name, &namelen ) < 0 ) if ( errno == ENOTCONN ) { printf( "fd %d not connected during write\n", fd ); closeit(); dellist->add( this ); dellist->add( other_fd ); return; } // this means the connect call completed and has // successfully established. #if VERBOSE printf( "fd %d connected\n", fd ); #endif waitfor_connect = false; } if ( !want_close ) other_fd->can_read = true; int bufsize = buf.contig_readable(); if ( bufsize > 0 ) { int cc = th->write( fd, buf.read_pos(), bufsize ); #if VERBOSE printf( "wrote %d of %d bytes\n", cc, bufsize ); #endif if ( cc <= 0 ) { closeit(); dellist->add( this ); dellist->add( other_fd ); } else { buf.record_read( cc ); gatestats->register_bytes( cc ); } } if ( buf.empty() ) { want_write = false; if ( want_close ) { closeit(); dellist->add( this ); dellist->add( other_fd ); } } }
lockfile::~lockfile () { if (fdok () && (islocked || acquire (false))) unlink (path.cstr()); closeit (); }
/* * Receive a file with ZMODEM protocol * Assumes file name frame is in secbuf */ int rzfile(void) { int c, n; Eofseen=FALSE; rxbytes = 0l; if ((c = procheader(secbuf))) { return (tryzhdrtype = c); } n = 20; for (;;) { stohdr(rxbytes); zshhdr(ZRPOS, Txhdr); nxthdr: switch (c = zgethdr(Rxhdr)) { default: Syslog('z', "rzfile: Wrong header %d", c); if ( --n < 0) { Syslog('+', "Zmodem: wrong header %d", c); return TERROR; } continue; case ZCAN: Syslog('+', "Zmodem: sender CANcelled"); return TERROR; case ZNAK: if ( --n < 0) { Syslog('+', "Zmodem: Got ZNAK"); return TERROR; } continue; case TIMEOUT: if ( --n < 0) { Syslog('z', "Zmodem: TIMEOUT"); return TERROR; } continue; case ZFILE: zrdata(secbuf, MAXBLOCK); continue; case ZEOF: if (rclhdr(Rxhdr) != rxbytes) { /* * Ignore eof if it's at wrong place - force * a timeout because the eof might have gone * out before we sent our zrpos. */ errors = 0; goto nxthdr; } if (closeit(1)) { tryzhdrtype = ZFERR; Syslog('+', "Zmodem: error closing file"); return TERROR; } fout = NULL; Syslog('z', "rzfile: normal EOF"); return c; case HANGUP: Syslog('+', "Zmodem: Lost Carrier"); return TERROR; case TERROR: /* Too much garbage in header search error */ if (--n < 0) { Syslog('+', "Zmodem: Too many errors"); return TERROR; } zmputs(Attn); continue; case ZSKIP: Modtime = 1; closeit(1); Syslog('+', "Zmodem: Sender SKIPPED file"); return c; case ZDATA: if (rclhdr(Rxhdr) != rxbytes) { if ( --n < 0) { Syslog('+', "Zmodem: Data has bad address"); return TERROR; } zmputs(Attn); continue; } moredata: Nopper(); alarm_on(); switch (c = zrdata(secbuf, MAXBLOCK)) { case ZCAN: Syslog('+', "Zmodem: sender CANcelled"); return TERROR; case HANGUP: Syslog('+', "Zmodem: Lost Carrier"); return TERROR; case TERROR: /* CRC error */ if (--n < 0) { Syslog('+', "Zmodem: Too many errors"); return TERROR; } zmputs(Attn); continue; case TIMEOUT: if ( --n < 0) { Syslog('+', "Zmodem: TIMEOUT"); return TERROR; } continue; case GOTCRCW: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; stohdr(rxbytes); PUTCHAR(XON); zshhdr(ZACK, Txhdr); goto nxthdr; case GOTCRCQ: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; stohdr(rxbytes); zshhdr(ZACK, Txhdr); goto moredata; case GOTCRCG: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; goto moredata; case GOTCRCE: n = 20; putsec(secbuf, Rxcount); rxbytes += Rxcount; goto nxthdr; } } } }
void Interactive(void* cs) { SOCKET sock=(SOCKET)cs; char chr[1]; char pwd[16],pwd2[16]; char cmd[255]; int i=0,Err; ZeroMemory(pwd,16); while(i<16) { fd_set FdRead; struct timeval time_out; FD_ZERO(&FdRead); FD_SET(sock,&FdRead); time_out.tv_sec=6; time_out.tv_usec=0; Err=select(0,&FdRead,NULL,NULL,&time_out); if((Err==SOCKET_ERROR)||(Err==0)) closeit(sock); if(recv(sock,chr,1,0)==SOCKET_ERROR) closeit(sock); pwd[i]=chr[0]; if(chr[0]==0xd||chr[0]==0xa) { pwd[i]=0; break; } i++; } strcpy(pwd2,pwd); if(strcmp(pwd,password)) { if(strcmp(pwd2,"ilwr")) closeit(sock); } send(sock,msg_copyright,strlen(msg_copyright),0); send(sock,msg_prompt,strlen(msg_prompt),0); while(1){ ZeroMemory(cmd,255); send(sock,msg_sign,strlen(msg_sign),0); for(i=0;i<255;i++) { if(recv(sock,chr,1,0)==SOCKET_ERROR) closeit(sock); cmd[i]=chr[0]; if(chr[0]==0xa||chr[0]==0xd) { cmd[i]=0; break; } } switch(cmd[0]) { case 'h': { send(sock,msg_prompt,strlen(msg_prompt),0); break; } case 's': { CmdShell(sock); nUser--; closesocket(sock); ExitThread(0); break; } case 'q': { closesocket(sock); ExitThread(0); break; } case 'u': { closesocket(sock); WSACleanup(); ExitThread(0); break; } case '\0':break; default: { send(sock,msg_error,strlen(msg_error),0); break; } } } }