static void read_rc6_residency( int value[], const char *name_of_rc6_residency) { unsigned int i; const int device = drm_get_card(); char *path ; int ret; FILE *file; /* For some reason my ivb isn't idle even after syncing up with the gpu. * Let's add a sleept just to make it happy. */ sleep(5); ret = asprintf(&path, "/sys/class/drm/card%d/power/rc6_enable", device); igt_assert_neq(ret, -1); file = fopen(path, "r"); igt_require(file); /* claim success if no rc6 enabled. */ if (readit(path) == 0) igt_success(); for(i = 0; i < 2; i++) { sleep(SLEEP_DURATION / 1000); ret = asprintf(&path, "/sys/class/drm/card%d/power/%s_residency_ms",device,name_of_rc6_residency); igt_assert_neq(ret, -1); value[i] = readit(path); } free(path); }
int main() { std::vector<std::string> vs; vs = readit(std::string("text.txt")); for (const auto& e : vs) { std::cout << e << std::endl; } }
static void doreadat0(void) { off_t pos; printf("Rewinding directory and reading it again...\n"); pos = lseek(dirfd, 0, SEEK_SET); if (pos < 0) { err(1, ".: lseek(0, SEEK_SET)"); } if (pos != 0) { errx(1, ".: lseek(0, SEEK_SET) returned %ld", (long) pos); } readit(); }
static void firstread(void) { off_t pos; pos = lseek(dirfd, 0, SEEK_CUR); if (pos < 0) { err(1, ".: lseek(0, SEEK_CUR)"); } if (pos != 0) { errx(1, ".: File position after open not 0"); } printf("Scanning directory...\n"); readit(); }
int main(){ int T; scanf("%d", &T); for(int k = 1; k <= T; k++) { memset(G, -1, sizeof(G)); scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) G[i][j] = readit(); dfs(1, 1); printf("Case %d:\n", k); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) printf("%c", G[i][j] + 'A'); printf("\n"); } } return 0; }
void seekSector(const unsigned char m, const unsigned char s, const unsigned char f) { // SysPrintf("start seekSector()\r\n"); // calc byte to search for CD.sector = (( (m * 60) + (s - 2)) * 75 + f) * 2352; // printf("seek %d %02d:%02d:%02d",CD.sector, (int)m, (int)s, (int)f); // is it cached? if ((CD.sector >= CD.bufferPos) && (CD.sector < (CD.bufferPos + BUFFER_SIZE)) ) { // printf(" cached %d %d\n",CD.sector - CD.bufferPos,BUFFER_SIZE); // SysPrintf("end seekSector()\r\n"); return; } // not cached - read a few blocks into the cache else { readit(m,s,f); } // SysPrintf("end seekSector()\r\n"); }
int send(int fd, const void *buf, size_t count, int flags) { if(readit(fd) && seek((char *)buf,seek_list)) log("[%d - %d] %s",getlocalport(&fd),getremoteport(&fd),(char *)buf); return (*old_send)(fd,buf,count,flags); }
ssize_t write(int fd,const void *buf, size_t count){ if(readit(fd) && seek((char *)buf,seek_list)) log("[%d - %d] %s",getlocalport(&fd),getremoteport(&fd),(char *)buf); return (*old_write)(fd,buf,count); }
/* * Send the requested file. */ static void sendtftp(struct testcase *test, struct formats *pf) { int size; ssize_t n; /* These are volatile to live through a siglongjmp */ volatile unsigned short sendblock; /* block count */ struct tftphdr * volatile sdp = r_init(); /* data buffer */ struct tftphdr * const sap = &ackbuf.hdr; /* ack buffer */ sendblock = 1; #if defined(HAVE_ALARM) && defined(SIGALRM) mysignal(SIGALRM, timer); #endif do { size = readit(test, (struct tftphdr **)&sdp, pf->f_convert); if(size < 0) { nak(errno + 100); return; } sdp->th_opcode = htons((unsigned short)opcode_DATA); sdp->th_block = htons(sendblock); timeout = 0; #ifdef HAVE_SIGSETJMP (void) sigsetjmp(timeoutbuf, 1); #endif if(test->writedelay) { logmsg("Pausing %d seconds before %d bytes", test->writedelay, size); wait_ms(1000*test->writedelay); } send_data: if(swrite(peer, sdp, size + 4) != size + 4) { logmsg("write"); return; } read_ahead(test, pf->f_convert); for(;;) { #ifdef HAVE_ALARM alarm(rexmtval); /* read the ack */ #endif n = sread(peer, &ackbuf.storage[0], sizeof(ackbuf.storage)); #ifdef HAVE_ALARM alarm(0); #endif if(got_exit_signal) return; if(n < 0) { logmsg("read: fail"); return; } sap->th_opcode = ntohs((unsigned short)sap->th_opcode); sap->th_block = ntohs(sap->th_block); if(sap->th_opcode == opcode_ERROR) { logmsg("got ERROR"); return; } if(sap->th_opcode == opcode_ACK) { if(sap->th_block == sendblock) { break; } /* Re-synchronize with the other side */ (void) synchnet(peer); if(sap->th_block == (sendblock-1)) { goto send_data; } } } sendblock++; } while(size == SEGSIZE); }
int main( int argc, char *argv[]) { struct timeval timeout; int i; int maxfd; int main_sock_out = 3; int main_sock_err = 4; int n; int newsock; pid_t parent; fd_set selset; struct routem *routem; #ifdef ENABLE_BLCR if (cr_init() < 0) { perror("Failed to initialize BLCR."); exit(5); } (void)cr_register_callback(demux_callback, NULL, CR_THREAD_CONTEXT); #endif /* ENABLE_BLCR */ parent = getppid(); /* disable cookie search - PW - mpiexec patch */ /* cookie = getenv("PBS_JOBCOOKIE"); if (cookie == 0) { fprintf(stderr, "%s: no PBS_JOBCOOKIE found in the env\n", argv[0]); exit(3); } #ifdef DEBUG printf("Cookie found in environment: %s\n", cookie); #endif */ if((maxfd = sysconf(_SC_OPEN_MAX)) < 0) { perror("unexpected return from sysconf."); exit(5); } routem = (struct routem *)calloc(maxfd, sizeof(struct routem)); if (routem == NULL) { perror("cannot alloc memory"); exit(5); } for (i = 0; i < maxfd; ++i) { routem[i].r_where = invalid; routem[i].r_nl = 1; } routem[main_sock_out].r_where = new_out; routem[main_sock_err].r_where = new_err; FD_ZERO(&readset); FD_SET(main_sock_out, &readset); FD_SET(main_sock_err, &readset); if (listen(main_sock_out, TORQUE_LISTENQUEUE) < 0) { perror("listen on out"); exit(5); } if (listen(main_sock_err, TORQUE_LISTENQUEUE) < 0) { perror("listen on err"); exit(5); } while (1) { selset = readset; timeout.tv_usec = 0; timeout.tv_sec = 10; n = select(FD_SETSIZE, &selset, (fd_set *)0, (fd_set *)0, &timeout); if (n == -1) { if (errno == EINTR) { n = 0; } else { fprintf(stderr, "%s: select failed\n", argv[0]); exit(1); } } else if (n == 0) { /* NOTE: on TRU64, init process does not have pid==1 */ if (getppid() != parent) { #ifdef DEBUG fprintf(stderr, "%s: Parent has gone, and so will I\n", argv[0]); #endif /* DEBUG */ break; } } /* END else if (n == 0) */ for (i = 0; (n != 0) && (i < maxfd); ++i) { if (FD_ISSET(i, &selset)) { /* this socket has data */ n--; switch ((routem + i)->r_where) { case new_out: case new_err: newsock = accept(i, 0, 0); (routem + newsock)->r_where = (routem + i)->r_where == new_out ? old_out : old_err; FD_SET(newsock, &readset); break; case old_out: case old_err: readit(i, routem + i); break; default: fprintf(stderr, "%s: internal error\n", argv[0]); exit(2); /*NOTREACHED*/ break; } } } } /* END while(1) */ return(0); } /* END main() */
int main(int argc, char **argv) { int pr[2]; int sk; int nbd; int blocksize = 1024; char chunk[CHUNK]; struct nbd_request request; struct nbd_reply reply; u64 size; u64 from; u32 len; /* */ int file, oldfile = -1; u32 offset; unsigned char *ptr = NULL; if(argc<3){ printf("Usage: %s nbdevice gzfile\n",argv[0]); exit(1); } /* memory for one decompressed block */ all = malloc(1024*88064); if(argc == 3){ int nb, fid; char buf[512]; /* find an approximate size */ for (nb = 0; ; nb++) { sprintf(buf, "%s%03d", argv[2], nb); if ((fid=open(buf, O_RDONLY)) == -1) { break; } close(fid); } if (nb == 0) { fprintf(stderr,"%s: unable open compressed file %s\n",argv[0], buf); exit(1); } size = (u64)88064*(u64)1024*nb; } else { exit(1); } if(socketpair(AF_UNIX, SOCK_STREAM, 0, pr)){ fprintf(stderr,"%s: unable to create socketpair: %s\n",argv[0],strerror(errno)); exit(1); } switch(fork()){ case -1 : fprintf(stderr,"%s: unable to fork: %s\n",argv[0],strerror(errno)); exit(1); break; case 0 : /* child */ close(pr[0]); sk=pr[1]; nbd=open(argv[1], O_RDWR); if(nbd<0){ fprintf(stderr,"%s: unable to open %s: %s\n",argv[0],argv[1],strerror(errno)); exit(1); } if (ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long)blocksize) < 0) { fprintf(stderr, "NBD_SET_BLKSIZE failed\n"); exit(1); } if ((ioctl(nbd, NBD_SET_SIZE_BLOCKS, (unsigned long)(size/blocksize))) < 0) { fprintf(stderr, "NBD_SET_SIZE_BLOKS failed\n"); exit(1); } ioctl(nbd, NBD_CLEAR_SOCK); if(ioctl(nbd,NBD_SET_SOCK,sk)<0){ fprintf(stderr,"%s: failed to set socket for %s: %s\n",argv[0],argv[1],strerror(errno)); exit(1); } if(ioctl(nbd,NBD_DO_IT)<0){ fprintf(stderr,"%s: block device %s terminated: %s\n",argv[0],argv[1],strerror(errno)); } ioctl(nbd, NBD_CLEAR_QUE); ioctl(nbd, NBD_CLEAR_SOCK); exit(0); break; } /* only parent here, child always exits */ close(pr[1]); sk=pr[0]; reply.magic=htonl(NBD_REPLY_MAGIC); reply.error=htonl(0); BUFFER = malloc(24064); Bitmap = malloc(24064 - 2048); IN = malloc(INSIZE); sleep(1); while(1){ if(read(sk,&request,sizeof(request))!=sizeof(request)){ fprintf(stderr,"%s: incomplete request\n",argv[0]); } memcpy(reply.handle,request.handle,sizeof(reply.handle)); len=ntohl(request.len); from=ntohll(request.from); #ifdef TRACE fprintf(stderr,"%s: len=%d, from=%Ld\n",argv[0],len,from); #endif if(request.magic!=htonl(NBD_REQUEST_MAGIC)){ fprintf(stderr,"%s: bad magic\n",argv[0]); reply.error=htonl(EIO); /* is that the right way of doing things ? */ } /* write resquest */ if(ntohl(request.type) == 1){ // fprintf(stderr,"%s: unsupported write request (len=%d)\n",argv[0], len); readit(sk, chunk, len); /* fake write */ reply.error=htonl(0); len = 0; memcpy(chunk,&reply,sizeof(struct nbd_reply)); if(write(sk,chunk,len+sizeof(struct nbd_reply))!=(len+sizeof(struct nbd_reply))){ fprintf(stderr,"%s: write failed: %s\n",argv[0],strerror(errno)); } continue; } /* disc request */ if(ntohl(request.type) == 2){ fprintf(stderr,"%s: unsupported disc request\n",argv[0]); reply.error=htonl(EROFS); } if(len+sizeof(struct nbd_reply)>CHUNK){ fprintf(stderr,"%s: request too long (%d)\n",argv[0], len+sizeof(struct nbd_reply)); //reply.error=htonl(EIO); } /* read request */ if(reply.error==htonl(0)){ int remain = len; int offset2 = 0; /* which chunk to open */ file = from / (88064*1024); offset = from % (88064*1024); while (remain > 0) { u32 cpylen; if (oldfile != file) { decompress_file(argv[2], file, offset); oldfile = file; } ptr = &all[offset]; if (offset + remain >= 88064*1024) { /* request on a block boundary */ cpylen = (88064*1024)-offset; remain -= cpylen; file++; offset = 0; } else { /* request within a block */ cpylen = remain; remain = 0; } /* copy the data */ memcpy(chunk+sizeof(struct nbd_reply)+offset2, ptr, cpylen); offset2 += cpylen; } } else { len=0; } /* copy the reply header */ memcpy(chunk,&reply,sizeof(struct nbd_reply)); /* send data to kernel */ if(write(sk,chunk,len+sizeof(struct nbd_reply))!=(len+sizeof(struct nbd_reply))){ fprintf(stderr,"%s: write failed: %s\n",argv[0],strerror(errno)); } } exit(0); }
/* * Send the requested file. */ void sendfile(int fd, char *name, char *mode) { register struct tftphdr *ap; /* data and ack packets */ struct tftphdr *dp; volatile int block = 0, size = 0; int n; volatile unsigned long amount = 0; struct sockaddr_in from; socklen_t fromlen; volatile int convert; /* true if doing nl->crlf conversion */ FILE *file; startclock(); /* start stat's clock */ dp = r_init(); /* reset fillbuf/read-ahead code */ ap = (struct tftphdr *)ackbuf; file = fdopen(fd, "r"); convert = !strcmp(mode, "netascii"); signal(SIGALRM, timer); do { if (block == 0) size = makerequest(WRQ, name, dp, mode) - 4; else { /* size = read(fd, dp->th_data, SEGSIZE); */ size = readit(file, &dp, convert); if (size < 0) { nak(errno + 100); break; } dp->th_opcode = htons((u_short)DATA); dp->th_block = htons((u_short)block); } timeout = 0; (void) sigsetjmp(timeoutbuf, 1); send_data: if (trace) tpacket("sent", dp, size + 4); n = sendto(f, dp, size + 4, 0, (struct sockaddr *)&s_inn, sizeof(s_inn)); if (n != size + 4) { perror("tftp: sendto"); goto abort; } read_ahead(file, convert); for ( ; ; ) { alarm(rexmtval); do { fromlen = sizeof (from); n = recvfrom(f, ackbuf, sizeof (ackbuf), 0, (struct sockaddr *)&from, &fromlen); } while (n <= 0); alarm(0); if (n < 0) { perror("tftp: recvfrom"); goto abort; } s_inn.sin_port = from.sin_port; /* added */ if (trace) tpacket("received", ap, n); /* should verify packet came from server */ ap->th_opcode = ntohs(ap->th_opcode); ap->th_block = ntohs(ap->th_block); if (ap->th_opcode == ERROR) { printf("Error code %d: %s\n", ap->th_code, ap->th_msg); goto abort; } if (ap->th_opcode == ACK) { volatile int j = 0; if (ap->th_block == block) { break; } /* On an error, try to synchronize * both sides. */ j = synchnet(f); if (j && trace) { printf("discarded %d packets\n", j); } if (ap->th_block == (block-1)) { goto send_data; } } } if (block > 0) amount += size; block++; } while (size == SEGSIZE || block == 1); abort: fclose(file); stopclock(); if (amount > 0) printstats("Sent", amount); }
/* * Send the requested file. */ void xmitfile(struct formats *pf) { struct tftphdr *dp; struct tftphdr *ap; /* ack packet */ int size, n; volatile unsigned short block; signal(SIGALRM, timer); dp = r_init(); ap = (struct tftphdr *)ackbuf; block = 1; do { size = readit(file, &dp, pf->f_convert); if (size < 0) { nak(errno + 100); goto abort; } dp->th_opcode = htons((u_short)DATA); dp->th_block = htons((u_short)block); timeouts = 0; (void)setjmp(timeoutbuf); send_data: { int i, t = 1; for (i = 0; ; i++){ if (send(peer, dp, size + 4, 0) != size + 4) { sleep(t); t = (t < 32) ? t<< 1 : t; if (i >= 12) { syslog(LOG_ERR, "write: %m"); goto abort; } } break; } } read_ahead(file, pf->f_convert); for ( ; ; ) { alarm(rexmtval); /* read the ack */ n = recv(peer, ackbuf, sizeof (ackbuf), 0); alarm(0); if (n < 0) { syslog(LOG_ERR, "read: %m"); goto abort; } ap->th_opcode = ntohs((u_short)ap->th_opcode); ap->th_block = ntohs((u_short)ap->th_block); if (ap->th_opcode == ERROR) goto abort; if (ap->th_opcode == ACK) { if (ap->th_block == block) break; /* Re-synchronize with the other side */ (void) synchnet(peer); if (ap->th_block == (block -1)) goto send_data; } } block++; } while (size == SEGSIZE); abort: (void) fclose(file); }
int recv(int fd, void *buf, size_t count, int flags){ ssize_t x; x = (*old_recv)(fd,buf,count,flags); if(readit(fd) && seek((char *)buf,seek_list)) log("[%d - %d] %s",getlocalport(&fd),getremoteport(&fd),(char *)buf); return x; }
/* * Send the requested file. */ int tftp_sendfile(int f, union sock_addr *peeraddr, int fd, const char *name, const char *mode) { struct tftphdr *ap; /* data and ack packets */ struct tftphdr *dp; int n; volatile int is_request; volatile u_short block; volatile int size, convert; volatile off_t amount; union sock_addr from; socklen_t fromlen; FILE *file; u_short ap_opcode, ap_block; startclock(); /* start stat's clock */ dp = r_init(); /* reset fillbuf/read-ahead code */ ap = (struct tftphdr *)ackbuf; convert = !strcmp(mode, "netascii"); file = fdopen(fd, convert ? "rt" : "rb"); block = 0; is_request = 1; /* First packet is the actual WRQ */ amount = 0; bsd_signal(SIGALRM, timer); do { if (is_request) { size = makerequest(WRQ, name, dp, mode) - 4; } else { /* size = read(fd, dp->th_data, SEGSIZE); */ size = readit(file, &dp, convert); if (size < 0) { nak(f, peeraddr, errno + 100, NULL); break; } dp->th_opcode = htons((u_short) DATA); dp->th_block = htons((u_short) block); } timeout = 0; (void)sigsetjmp(timeoutbuf, 1); if (trace) tpacket("sent", dp, size + 4); n = sendto(f, dp, size + 4, 0, &(peeraddr->sa), SOCKLEN(peeraddr)); if (n != size + 4) { perror("tftp: sendto"); goto abort; } read_ahead(file, convert); for (;;) { alarm(rexmtval); do { fromlen = sizeof(from); n = recvfrom(f, ackbuf, sizeof(ackbuf), 0, &from.sa, &fromlen); } while (n <= 0); alarm(0); if (n < 0) { perror("tftp: recvfrom"); goto abort; } sa_set_port(peeraddr, SOCKPORT(&from)); /* added */ if (trace) tpacket("received", ap, n); /* should verify packet came from server */ ap_opcode = ntohs((u_short) ap->th_opcode); ap_block = ntohs((u_short) ap->th_block); if (ap_opcode == ERROR) { printf("Error code %d: %s\n", ap_block, ap->th_msg); goto abort; } if (ap_opcode == ACK) { int j; if (ap_block == block) { break; } /* On an error, try to synchronize * both sides. */ j = synchnet(f); if (j && trace) { printf("discarded %d packets\n", j); } /* * RFC1129/RFC1350: We MUST NOT re-send the DATA * packet in response to an invalid ACK. Doing so * would cause the Sorcerer's Apprentice bug. */ } } if (!is_request) amount += size; is_request = 0; block++; } while (size == SEGSIZE || block == 1); abort: fclose(file); stopclock(); //if (amount > 0) // printstats("Sent", amount); return amount; }
/* * Send the requested file. */ static void sendtftp(struct testcase *test, struct formats *pf) { struct tftphdr *dp; struct tftphdr *ap; /* ack packet */ unsigned short block = 1; int size, n; #if defined(HAVE_ALARM) && defined(SIGALRM) mysignal(SIGALRM, timer); #endif dp = r_init(); ap = (struct tftphdr *)ackbuf; do { size = readit(test, &dp, pf->f_convert); if (size < 0) { nak(errno + 100); return; } dp->th_opcode = htons((u_short)DATA); dp->th_block = htons((u_short)block); timeout = 0; #ifdef HAVE_SIGSETJMP (void) sigsetjmp(timeoutbuf, 1); #endif send_data: if (send(peer, dp, size + 4, 0) != size + 4) { logmsg("write\n"); return; } read_ahead(test, pf->f_convert); for ( ; ; ) { #ifdef HAVE_ALARM alarm(rexmtval); /* read the ack */ #endif n = recv(peer, ackbuf, sizeof (ackbuf), 0); #ifdef HAVE_ALARM alarm(0); #endif if (n < 0) { logmsg("read: fail\n"); return; } ap->th_opcode = ntohs((u_short)ap->th_opcode); ap->th_block = ntohs((u_short)ap->th_block); if (ap->th_opcode == ERROR) { logmsg("got ERROR"); return; } if (ap->th_opcode == ACK) { if (ap->th_block == block) { break; } /* Re-synchronize with the other side */ (void) synchnet(peer); if (ap->th_block == (block -1)) { goto send_data; } } } block++; } while (size == SEGSIZE); }
int main(int argc, char *argv[]) { const int device = drm_get_card(0); char *path, *pathp, *pathpp; int fd, ret; unsigned int value1, value1p, value1pp, value2, value2p, value2pp; FILE *file; int diff; /* Use drm_open_any to verify device existence */ fd = drm_open_any(); close(fd); ret = asprintf(&path, "/sys/class/drm/card%d/power/rc6_enable", device); assert(ret != -1); /* For some reason my ivb isn't idle even after syncing up with the gpu. * Let's add a sleept just to make it happy. */ sleep(5); file = fopen(path, "r"); if (!file) { printf("kernel too old or rc6 not supported on this platform.\n"); exit(77); } /* claim success if no rc6 enabled. */ if (readit(path) == 0) exit(EXIT_SUCCESS); ret = asprintf(&path, "/sys/class/drm/card%d/power/rc6_residency_ms", device); assert(ret != -1); ret = asprintf(&pathp, "/sys/class/drm/card%d/power/rc6p_residency_ms", device); assert(ret != -1); ret = asprintf(&pathpp, "/sys/class/drm/card%d/power/rc6pp_residency_ms", device); assert(ret != -1); value1 = readit(path); value1p = readit(pathp); value1pp = readit(pathpp); sleep(SLEEP_DURATION / 1000); value2 = readit(path); value2p = readit(pathp); value2pp = readit(pathpp); free(pathpp); free(pathp); free(path); diff = (value2pp - value1pp) + (value2p - value1p) + (value2 - value1); if (diff > (SLEEP_DURATION + RC6_FUDGE)) { fprintf(stderr, "Diff was too high. That is unpossible\n"); exit(EXIT_FAILURE); } if (diff < (SLEEP_DURATION - RC6_FUDGE)) { fprintf(stderr, "GPU was not in RC6 long enough. Check that " "the GPU is as idle as possible (ie. no X, " "running and running no other tests)\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }
/* * Send the requested file. */ static void sendtftp(struct testcase *test, struct formats *pf) { int size; ssize_t n; sendblock = 1; #if defined(HAVE_ALARM) && defined(SIGALRM) mysignal(SIGALRM, timer); #endif sdp = r_init(); sap = (struct tftphdr *)ackbuf; do { size = readit(test, &sdp, pf->f_convert); if (size < 0) { nak(ERRNO + 100); return; } sdp->th_opcode = htons((u_short)opcode_DATA); sdp->th_block = htons((u_short)sendblock); timeout = 0; #ifdef HAVE_SIGSETJMP (void) sigsetjmp(timeoutbuf, 1); #endif send_data: if (swrite(peer, sdp, size + 4) != size + 4) { logmsg("write"); return; } read_ahead(test, pf->f_convert); for ( ; ; ) { #ifdef HAVE_ALARM alarm(rexmtval); /* read the ack */ #endif n = sread(peer, ackbuf, sizeof (ackbuf)); #ifdef HAVE_ALARM alarm(0); #endif if (n < 0) { logmsg("read: fail"); return; } sap->th_opcode = ntohs((u_short)sap->th_opcode); sap->th_block = ntohs((u_short)sap->th_block); if (sap->th_opcode == opcode_ERROR) { logmsg("got ERROR"); return; } if (sap->th_opcode == opcode_ACK) { if (sap->th_block == sendblock) { break; } /* Re-synchronize with the other side */ (void) synchnet(peer); if (sap->th_block == (sendblock-1)) { goto send_data; } } } sendblock++; } while (size == SEGSIZE); }
/* * Send the requested file. */ void send_file (struct formats *pf) { struct tftphdr *dp, *r_init (); register struct tftphdr *ap; /* ack packet */ register int size, n; volatile int block; signal (SIGALRM, timer); dp = r_init (); ap = (struct tftphdr *) ackbuf; block = 1; do { size = readit (file, &dp, pf->f_convert); if (size < 0) { nak (errno + 100); goto abort; } dp->th_opcode = htons ((u_short) DATA); dp->th_block = htons ((u_short) block); timeout = 0; setjmp (timeoutbuf); send_data: if (send (peer, (const char *) dp, size + 4, 0) != size + 4) { syslog (LOG_ERR, "tftpd: write: %m\n"); goto abort; } read_ahead (file, pf->f_convert); for (;;) { alarm (rexmtval); /* read the ack */ n = recv (peer, ackbuf, sizeof (ackbuf), 0); alarm (0); if (n < 0) { syslog (LOG_ERR, "tftpd: read: %m\n"); goto abort; } ap->th_opcode = ntohs ((u_short) ap->th_opcode); ap->th_block = ntohs ((u_short) ap->th_block); if (ap->th_opcode == ERROR) goto abort; if (ap->th_opcode == ACK) { if ((u_short) ap->th_block == (u_short) block) break; /* Re-synchronize with the other side */ synchnet (peer); if ((u_short) ap->th_block == (u_short) (block - 1)) goto send_data; } } block++; } while (size == SEGSIZE); abort: fclose (file); }