From 0fbaf9ea5bdd6b7a347d09dc4bb135cc777c7543 Mon Sep 17 00:00:00 2001 From: Nathan Straz Date: Tue, 13 Aug 2013 16:53:37 -0400 Subject: Get qacp remote to local working --- qacp.c | 48 +++++++++++++++++++----------------------------- qarshd.c | 59 +++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/qacp.c b/qacp.c index 63cedf9..db6fb90 100644 --- a/qacp.c +++ b/qacp.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -186,6 +187,7 @@ qacp_sendonefile(const char *host, const char *srcfile, const char *destfile) } send_packet(qacp_fd, qp); offset += nbytes; + qpfree(qp); } while (nbytes > 0); sendone_failure: @@ -219,14 +221,8 @@ void qacp_recvonefile(const char *host, const char *srcfile, const char *destfile) { struct qa_packet *qp; - struct sockaddr_in daddr; - socklen_t dlen; - int sd; - int port; - int insd; int outfd; - char buf[BUFSIZ]; - ssize_t nread, nwrote; + ssize_t nwrote; off_t nleft; struct qp_rstat_pkt *rstatp; int rstaterrno; @@ -245,38 +241,35 @@ qacp_recvonefile(const char *host, const char *srcfile, const char *destfile) fchmod(outfd, rstatp->qp_st_mode); - sd = bind_any(QARSH_MINPORT, qarsh_ss_family); - port = getsockport(sd); - /* Recall that the packet types are qarshd-centric, so if we want * to recv a file from the host running qarshd we have to tell * qarshd to send a file. */ - qp = make_qp_sendfile(srcfile, port); + qp = make_qp_sendfile(srcfile, outfd); send_packet(qacp_fd, qp); qpfree(qp); - dlen = sizeof daddr; - insd = accept(sd, (struct sockaddr *) &daddr, &dlen); - /* Read/write file */ nleft = rstatp->qp_st_size; while (nleft > 0) { - nread = read(insd, buf, BUFSIZ); - if (nread < 0) { - fprintf(stderr, "read() error: %s\n", strerror(errno)); - break; - } else if (nread == 0) { /* EOF */ - break; - } - - nwrote = write(outfd, buf, nread); - if (nwrote < 0) { - fprintf(stderr, "write() error: %s\n", strerror(errno)); + qp = recv_packet(qacp_fd); + if (qp) { + assert(qp->qp_type == QP_DATA); + assert(qp->qp_data.qp_remfd == outfd); + assert(rstatp->qp_st_size - nleft == qp->qp_data.qp_offset); + nwrote = write(outfd, qp->qp_data.qp_blob, qp->qp_data.qp_count); + if (nwrote < 0) { + fprintf(stderr, "write() error: %s\n", strerror(errno)); + break; + } + assert(nwrote == qp->qp_data.qp_count); + nleft -= qp->qp_data.qp_count; + qpfree(qp); + } else { + fprintf(stderr, "Did not receive a packet\n"); break; } - nleft -= nread; } if (nleft != 0) { @@ -286,9 +279,6 @@ qacp_recvonefile(const char *host, const char *srcfile, const char *destfile) (long long int)nleft, (long long int)rstatp->qp_st_size); } - - close(sd); - close(insd); close(outfd); /* Await our return code from qarshd */ diff --git a/qarshd.c b/qarshd.c index a81055b..950135e 100644 --- a/qarshd.c +++ b/qarshd.c @@ -181,45 +181,57 @@ receive_data(struct remotefd *rfd, struct qa_packet *qp) } ssize_t -pushfile(const char *path, int of_port) +pushfile(const char *path, int remfd) { - int outsd; int infd; - off_t offset = 0; - ssize_t nbytes; + off_t offset; + ssize_t nbytes, fsize = 0; struct stat sb; - - outsd = connect_to_peer(&peername, of_port); - if (outsd == -1) { - syslog(LOG_WARNING, "Connect to of_port (%d) failed: %s\n", - of_port, strerror(errno)); - return -1; - } + const int bufsize = 16384; + char buf[bufsize]; + struct qa_packet *qp; infd = open(path, O_RDONLY); if (infd == -1) { syslog(LOG_WARNING, "Could not open %s: %s\n", path, strerror(errno)); - close(outsd); return -1; } - if (strcmp(saved_path, path) == 0) { - nbytes = sendfile(outsd, infd, &offset, saved_stat.st_size); - } else if (fstat(infd, &sb) < 0) { - syslog(LOG_WARNING, "Could not stat %s: %s\n", - path, strerror(errno)); + if (strcmp(saved_path, path)) { + if (fstat(infd, &sb) < 0) { + syslog(LOG_WARNING, "Could not stat %s: %s\n", + path, strerror(errno)); + close(infd); + return -1; + } else { + fsize = sb.st_size; + } + } else { + fsize = saved_stat.st_size; + } + if (!fsize) { + fprintf(stderr, "Unknown file size to send\n"); close(infd); - close(outsd); return -1; - } else { /* fstat filled in sb */ - nbytes = sendfile(outsd, infd, &offset, sb.st_size); } + offset = 0; + do { + nbytes = read(infd, buf, bufsize); + if (nbytes < 0) { + fprintf(stderr, "read() error: %s\n", strerror(errno)); + qp = make_qp_returncode(-1, errno, strerror(errno)); + } else { + qp = make_qp_data(remfd, offset, nbytes, buf); + } + send_packet(fileno(stdout), qp); + offset += nbytes; + qpfree(qp); + } while (nbytes > 0); close(infd); - close(outsd); - return nbytes; + return offset; } void @@ -342,8 +354,7 @@ handle_packets(int infd) } break; case QP_SENDFILE: - syslog(LOG_INFO, "Got a QP_SENDFILE with path = %s, " - "remfd = %d\n", + syslog(LOG_INFO, "Sending file %s, remfd = %d\n", qp->qp_sendfile.qp_path, qp->qp_sendfile.qp_remfd); nbytes = pushfile(qp->qp_sendfile.qp_path, -- cgit