summaryrefslogtreecommitdiffstats
path: root/sockutil.c
diff options
context:
space:
mode:
authorNathan Straz <nstraz@redhat.com>2013-08-13 15:05:06 -0400
committerNathan Straz <nstraz@redhat.com>2013-09-11 17:49:12 -0400
commit5be1f6c7a3dbfd172c243a08d9f46867b3ca413f (patch)
treee71d47c63ac40adcb7b9e865f5360583fb9bc424 /sockutil.c
parent0a361ddae1649b0a3102e113d16e6f7f07f6d7e8 (diff)
downloadqarsh-5be1f6c7a3dbfd172c243a08d9f46867b3ca413f.tar.gz
qarsh-5be1f6c7a3dbfd172c243a08d9f46867b3ca413f.tar.xz
qarsh-5be1f6c7a3dbfd172c243a08d9f46867b3ca413f.zip
Get qacp local to remote working on main socket
I removed the buffering layer from recv_packet because it made the logic too complex around the pselect in qarshd. Now only read as much as needed to get each packet. qarshd adds an array for remote file descriptors which is only a stub for now. This needs to be expanded to allow multiple file transfers at the same time for runcmd.
Diffstat (limited to 'sockutil.c')
-rw-r--r--sockutil.c82
1 files changed, 33 insertions, 49 deletions
diff --git a/sockutil.c b/sockutil.c
index 63af812..1a53e62 100644
--- a/sockutil.c
+++ b/sockutil.c
@@ -202,75 +202,59 @@ connect_to_peer(struct sockaddr_storage *peer, int port)
* All incoming and outgoing packets go through this function.
* Caller should make sure the fd is ready to read.
*/
-typedef struct {
- char buf[QARSH_MAX_PACKET_SIZE];
- int start;
- int end;
-} Buffer;
-
struct qa_packet *
recv_packet(int fd)
{
- static Buffer pb = { "", 0, 0 };
+ char buf[QARSH_MAX_PACKET_SIZE];
uint32_t packetsize;
- struct qa_packet *qp = NULL;
- int ret;
-
- if (pb.start == pb.end && pb.start)
- pb.start = pb.end = 0;
+ int ret = 0;
-recv_read:
- ret = read(fd, pb.buf, QARSH_MAX_PACKET_SIZE - pb.start);
- if (ret > 0) {
- pb.end = pb.start + ret;
- /* get packet size from the packet */
- packetsize = ntohl(*(uint32_t *)(pb.buf+pb.start));
- if (packetsize > QARSH_MAX_PACKET_SIZE) {
- printf("Packet size too large, %d > %d\n", packetsize, QARSH_MAX_PACKET_SIZE);
- return NULL;
- } else if (packetsize <= (ret - sizeof packetsize)) {
- pb.start += sizeof packetsize;
- qp = parse_packet(pb.buf + pb.start, pb.end - pb.start);
- } else { /* packetsize > what we read, we need to read more */
- printf("Compacting buffer to fit rest of packet, %d read, %d left\n", ret, packetsize - ret);
- memmove(pb.buf, pb.buf + pb.start, pb.end - pb.start);
- pb.end -= pb.start;
- pb.start = 0;
- goto recv_read;
- }
-
- /* Get the packet buffer ready for the next call */
- if (packetsize == (ret - sizeof packetsize)) { /* The read was a complete packet */
- pb.start = pb.end = 0;
- printf("Complete read used\n");
- } else {
- pb.start += packetsize;
- memmove(pb.buf, pb.buf + pb.start, pb.end - pb.start);
- pb.end -= pb.start;
- pb.start = 0;
- }
+ if ((ret = read(fd, &packetsize, sizeof packetsize)) < 0) {
+ fprintf(stderr, "Read error while reading packet size: %s\n", strerror(errno));
+ return NULL;
+ } else if (ret == 0) {
+ return NULL;
+ } else if (ret != sizeof packetsize) {
+ fprintf(stderr, "Did not read packet size");
+ return NULL;
+ }
+ packetsize = ntohl(packetsize);
+ if (packetsize > QARSH_MAX_PACKET_SIZE) {
+ fprintf(stderr, "Packet size too large, %d > %d\n", packetsize, QARSH_MAX_PACKET_SIZE);
+ return NULL;
+ }
+ if ((ret = read(fd, buf, packetsize)) < 0) {
+ fprintf(stderr, "Read error while reading packet data: %s\n", strerror(errno));
+ return NULL;
}
- return qp;
+ if (ret != packetsize) {
+ fprintf(stderr, "Read something other than packetsize bytes, %d != %d\n",
+ ret, packetsize);
+ return NULL;
+ }
+
+ return parse_packet(buf, packetsize);
}
int
send_packet(int fd, struct qa_packet *qp)
{
- Buffer pb = { "", 0, 0 };
+ char buf[QARSH_MAX_PACKET_SIZE];
uint32_t netsize;
+ int len;
ssize_t ret = -1;
struct iovec iovs[2];
qp->qp_seq = packet_seq++;
- pb.end = qptostr(qp, pb.buf, QARSH_MAX_PACKET_SIZE - sizeof netsize);
+ len = qptostr(qp, buf, QARSH_MAX_PACKET_SIZE - sizeof netsize);
- if (pb.end > 0) {
- netsize = htonl(pb.end);
+ if (len > 0) {
+ netsize = htonl(len);
iovs[0].iov_base = &netsize;
iovs[0].iov_len = sizeof netsize;
- iovs[1].iov_base = pb.buf;
- iovs[1].iov_len = pb.end;
+ iovs[1].iov_base = buf;
+ iovs[1].iov_len = len;
ret = writev(fd, iovs, 2);
}