+ if (fold) fclose(fold);
+ fold= 0;
+
+ r= fstat(fileno(f),&stab);
+ if (r) diee("could not fstat source file `%s'",filename);
+
+ if (!S_ISREG(stab.st_mode))
+ die(8,0,-1,"source file `%s' is not a plain file",filename);
+
+ uint8_t hbuf[9]= {
+ REPLMSG_FILE64,
+ stab.st_size >> 28 >> 28,
+ stab.st_size >> 24 >> 24,
+ stab.st_size >> 16 >> 24,
+ stab.st_size >> 8 >> 24,
+ stab.st_size >> 24,
+ stab.st_size >> 16,
+ stab.st_size >> 8,
+ stab.st_size
+ };
+
+ bandlimit_sendstart();
+
+ r= fwrite(hbuf,1,9,commso); if (r!=9) die_badsend();
+
+ copyfile(f, copydie_inputfile,filename,
+ commso, copydie_commso,0,
+ stab.st_size, 1);
+
+ send_flush();
+
+ c= fgetc(commsi); if (c==EOF) die_badrecv("ack");
+ if (c!=REPLMSG_GO) die_protocol("got %#02x instead of GO",c);
+
+ bandlimit_sendend(stab.st_size, &interval_usec);
+
+ fold= f;
+ told= told_file;
+ }
+}
+
+void usagemessage(void) {
+ puts("usage: rcopy-repeatedly [<options>] <file> <file>\n"
+ " <file> may be <local-file> or [<user>@]<host>:<file>\n"
+ " exactly one of each of the two forms must be provided\n"
+ " a file is taken as remote if it has a : before the first /\n"
+ "options\n"
+ " --help\n");
+}
+
+typedef struct {
+ const char *userhost, *path;
+} FileSpecification;
+
+static FileSpecification srcspec, dstspec;
+
+static void of__server(const struct cmdinfo *ci, const char *val) {
+ int ncount= nsargs + 1 + !!val;
+ sargs= xrealloc(sargs, sizeof(*sargs) * ncount);
+ sargs[nsargs++]= ci->olong;
+ if (val)
+ sargs[nsargs++]= val;
+}
+
+static int of__server_int(const struct cmdinfo *ci, const char *val) {
+ of__server(ci,val);
+ long v;
+ char *ep;
+ errno= 0; v= strtol(val,&ep,10);
+ if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
+ badusage("bad integer argument `%s' for --%s",val,ci->olong);
+ return v;
+}
+
+static void of_help(const struct cmdinfo *ci, const char *val) {
+ usagemessage();
+ if (ferror(stdout)) diee("could not write usage message to stdout");
+ exit(0);
+}
+
+static void of_bw(const struct cmdinfo *ci, const char *val) {
+ int pct= of__server_int(ci,val);
+ if (pct<1 || pct>100)
+ badusage("bandwidth percentage must be between 1 and 100 inclusive");
+ *(double*)ci->parg= pct * 0.01;
+}
+
+static void of_server_int(const struct cmdinfo *ci, const char *val) {
+ *(int*)ci->parg= of__server_int(ci,val);
+}
+
+static const struct cmdinfo cmdinfos[]= {
+ { "help", .call= of_help },
+ { "max-bandwidth-percent-mean", 0,1,.call=of_bw,.parg=&max_bw_prop_mean },
+ { "max-bandwidth-percent-burst",0,1,.call=of_bw,.parg=&max_bw_prop_burst },
+ { "tx-block-size", 0,1,.call=of_server_int, .parg=&txblocksz },
+ { "min-interval-usec", 0,1,.call=of_server_int, .parg=&min_interval_usec },
+ { "rcopy-repeatedly", 0,1, .sassignto=&rcopy_repeatedly_program },
+ { "ssh-program", 0,1, .sassignto=&rsh_program },
+ { "receiver", .iassignto=&server_upcopy, .arg=0 },
+ { "sender", .iassignto=&server_upcopy, .arg=1 },
+ { 0 }
+};
+
+static void server(const char *filename) {
+ int c;
+ commsi= stdin;
+ commso= stdout;
+ fprintf(commso, "%s0002 %c\n", banner, server_upcopy?'u':'d');
+ send_flush();
+ c= fgetc(commsi); if (c==EOF) die_badrecv("initial go");
+ if (c!=REPLMSG_GO) die_protocol("initial go message was %#02x instead",c);
+
+ if (server_upcopy)
+ sender(filename);
+ else
+ receiver(filename);
+}
+
+static void client(void) {
+ int uppipe[2], downpipe[2], r;
+ pid_t child;
+
+ mpipe(uppipe);
+ mpipe(downpipe);
+
+ FileSpecification *remotespec= srcspec.userhost ? &srcspec : &dstspec;
+ const char *remotemode= srcspec.userhost ? "--sender" : "--receiver";
+
+ sargs= xrealloc(sargs, sizeof(*sargs) * (7 + nsargs));
+ memmove(sargs+5, sargs, sizeof(*sargs) * nsargs);
+ sargs[0]= rsh_program;
+ sargs[1]= remotespec->userhost;
+ sargs[2]= rcopy_repeatedly_program;
+ sargs[3]= remotemode;
+ sargs[4]= "--";
+ sargs[5+nsargs]= remotespec->path;
+ sargs[6+nsargs]= 0;
+
+ child= fork();
+ if (child==-1) diee("fork failed");
+ if (!child) {
+ mdup2(downpipe[0],0);
+ mdup2(uppipe[1],1);
+ close(uppipe[0]); close(downpipe[0]);
+ close(uppipe[1]); close(downpipe[1]);
+
+ execvp(rsh_program, (char**)sargs);
+ diee("failed to execute rsh program `%s'",rsh_program);
+ }
+
+ commso= fdopen(downpipe[1],"wb");
+ commsi= fdopen(uppipe[0],"rb");
+ if (!commso || !commsi) diee("fdopen failed");
+ close(downpipe[0]);
+ close(uppipe[1]);
+
+ char banbuf[sizeof(banner)-1 + 5 + 1];
+ r= fread(banbuf,1,sizeof(banbuf)-1,commsi);
+ if (ferror(commsi)) die_badrecv("read banner");
+
+ if (r!=sizeof(banbuf)-1 ||
+ memcmp(banbuf,banner,sizeof(banner)-1) ||
+ banbuf[sizeof(banner)-1 + 4] != ' ') {
+ const char **sap;
+ int count=0;
+ for (count=0, sap=sargs; *sap; sap++) count+= strlen(*sap)+1;
+ char *cmdline= xmalloc(count+1);
+ cmdline[0]=' ';
+ for (sap=sargs; *sap; sap++) {
+ strcat(cmdline," ");
+ strcat(cmdline,*sap);
+ }
+
+ die(8,0,-1,"did not receive banner as expected -"
+ " shell dirty? ssh broken?\n"
+ " try running\n"
+ " %s\n"
+ " and expect the first line to be\n"
+ " %s",
+ cmdline, banner);
+ }
+
+ banbuf[sizeof(banbuf)-1]= 0;
+ char *ep;
+ long decllen= strtoul(banbuf + sizeof(banner)-1, &ep, 16);
+ if (ep!=banbuf + sizeof(banner)-1 + 4 || *ep!=' ')
+ die_protocol("declaration length syntax error (`%s')",ep);
+ assert(decllen <= sizeof(mainbuf));
+ if (decllen<2) die_protocol("declaration too short");
+
+ r= fread(mainbuf,1,decllen,commsi);
+ if (r!=decllen) die_badrecv("declaration");
+ if (mainbuf[decllen-1] != '\n')
+ die_protocol("declaration missing final newline");
+ if (mainbuf[0] != (remotespec==&srcspec ? 'u' : 'd'))
+ die_protocol("declaration incorrect direction indicator");
+
+ sendbyte(REPLMSG_GO);