From 0c59da8a7310e1e3811f873fd0106ccebdb53c53 Mon Sep 17 00:00:00 2001 From: Nate Straz Date: Wed, 24 Aug 2005 21:46:50 +0000 Subject: Move send_packet() and recv_packet() to sockutil.c so qarsh can use them too. --- qarsh/qarsh.c | 49 +++++++++++++++++++++---------------------------- qarsh/qarshd.c | 38 ++------------------------------------ qarsh/sockutil.c | 40 +++++++++++++++++++++++++++++++++++++++- qarsh/sockutil.h | 2 ++ 4 files changed, 64 insertions(+), 65 deletions(-) diff --git a/qarsh/qarsh.c b/qarsh/qarsh.c index 0c90568..20e7023 100644 --- a/qarsh/qarsh.c +++ b/qarsh/qarsh.c @@ -63,8 +63,8 @@ int run_remote_cmd(char *cmdline) { struct qa_packet *qp; - char *packetbuf; - int packetsize; + char *buf; + int bufsize; int rc; int p_in, p_out, p_err; /* Port numbers */ int l_in, l_out, l_err; /* listening sockets */ @@ -83,10 +83,8 @@ run_remote_cmd(char *cmdline) qp = make_qp_runcmd(cmdline, p_in, p_out, p_err); qp->qp_seq = 1; - packetbuf = qptostr(qp, &packetbuf, &packetsize); + send_packet(qarsh_fd, qp); qpfree(qp); - write(qarsh_fd, packetbuf, packetsize); - free(packetbuf); /* Get the stdin, stdout, and stderr connections up before we do work */ FD_ZERO(&readfds); @@ -146,20 +144,20 @@ run_remote_cmd(char *cmdline) if (fcntl(fileno(stdin), F_SETFL, O_NONBLOCK) != 0) { fprintf(stderr, "fcntl stdin O_NONBLOCK failed, %d: %s\n", errno, strerror(errno)); } - packetbuf = malloc(1024); - memset(packetbuf, 0, 1024); + buf = malloc(1024); + memset(buf, 0, 1024); for (;;) { testfds = readfds; - memset(packetbuf, 0, 1024); + memset(buf, 0, 1024); nset = select(FD_SETSIZE, &testfds, NULL, NULL, NULL); if (FD_ISSET(fileno(stdin), &testfds)) { do { - packetsize = read(fileno(stdin), packetbuf, 1024); - write(c_in, packetbuf, packetsize); - } while (packetsize == 1024); - if (packetsize == 0) { + bufsize = read(fileno(stdin), buf, 1024); + write(c_in, buf, bufsize); + } while (bufsize == 1024); + if (bufsize == 0) { FD_CLR(fileno(stdin), &readfds); close(c_in); c_in = 0; @@ -167,10 +165,10 @@ run_remote_cmd(char *cmdline) } if (c_out && FD_ISSET(c_out, &testfds)) { do { - packetsize = read(c_out, packetbuf, 1024); - write(fileno(stdout), packetbuf, packetsize); - } while (packetsize == 1024); - if (packetsize == 0) { + bufsize = read(c_out, buf, 1024); + write(fileno(stdout), buf, bufsize); + } while (bufsize == 1024); + if (bufsize == 0) { FD_CLR(c_out, &readfds); close(c_out); c_out = 0; @@ -178,22 +176,17 @@ run_remote_cmd(char *cmdline) } if (c_err && FD_ISSET(c_err, &testfds)) { do { - packetsize = read(c_err, packetbuf, 1024); - write(fileno(stderr), packetbuf, packetsize); - } while (packetsize == 1024); - if (packetsize == 0) { + bufsize = read(c_err, buf, 1024); + write(fileno(stderr), buf, bufsize); + } while (bufsize == 1024); + if (bufsize == 0) { FD_CLR(c_err, &readfds); close(c_err); c_err = 0; } } if (FD_ISSET(qarsh_fd, &testfds)) { - packetsize = read(qarsh_fd, packetbuf, 1024); - if (packetsize == 0) { - qp = NULL; - break; - } - qp = parse_packets(packetbuf, packetsize); + qp = recv_packet(qarsh_fd); /* dump_qp(qp); */ if (qp && qp->qp_type == QP_CMDEXIT) { @@ -206,7 +199,7 @@ run_remote_cmd(char *cmdline) if (c_err) close(c_err); if (qp == NULL) { fprintf(stderr, "Remote command exited with unknown state\n"); - free(packetbuf); + free(buf); return 127; } if (WIFSIGNALED(qp->qp_cmdexit.qp_status)) { @@ -215,7 +208,7 @@ run_remote_cmd(char *cmdline) rc = WEXITSTATUS(qp->qp_cmdexit.qp_status); } qpfree(qp); - free(packetbuf); + free(buf); return rc; } diff --git a/qarsh/qarshd.c b/qarsh/qarshd.c index 437645d..9a90782 100644 --- a/qarsh/qarshd.c +++ b/qarsh/qarshd.c @@ -83,57 +83,23 @@ run_cmd(const char *cmd, int p_in, int p_out, int p_err) return pid; } -struct qa_packet * -recv_packet(int fd) -{ - char *packetbuf; - int packetsize; - struct qa_packet *qp = NULL; - - packetbuf = malloc(QARSH_MAX_PACKET_SIZE); - memset(packetbuf, 0, QARSH_MAX_PACKET_SIZE); - - packetsize = read(fd, packetbuf, QARSH_MAX_PACKET_SIZE); - if (packetsize > 0) { - qp = parse_packets(packetbuf, packetsize); - } - free(packetbuf); - return qp; -} - -int -send_packet(int fd, struct qa_packet *qp) -{ - char *packetbuf; - int packetsize; - - packetbuf = malloc(1024); - memset(packetbuf, 0, 1024); - packetbuf = qptostr(qp, &packetbuf, &packetsize); - - return write(fd, packetbuf, packetsize); -} - void handle_packets(int infd) { fd_set rfds; - int highfd, nfd; + int nfd; struct timeval timeout; struct qa_packet *qp = NULL, *rp = NULL; pid_t child_pid = 0; int child_status; - /* set up the select structures */ - highfd = infd+1; - for (;;) { FD_SET(infd, &rfds); timeout.tv_sec = 5; timeout.tv_usec = 0; - nfd = select(highfd, &rfds, NULL, NULL, &timeout); + nfd = select(infd+1, &rfds, NULL, NULL, &timeout); if (nfd < 0) { syslog(LOG_ERR, "select errno %d, %s\n", errno, strerror(errno)); } else if (nfd > 0) { diff --git a/qarsh/sockutil.c b/qarsh/sockutil.c index e5ac22e..b84e456 100644 --- a/qarsh/sockutil.c +++ b/qarsh/sockutil.c @@ -5,6 +5,10 @@ #include #include #include +#include +#include + +#include "qarsh_packet.h" /* Some generic socket related functions to make things easier */ @@ -49,7 +53,9 @@ connect_to_host(char *hostname, int port) struct sockaddr_in haddr; int sd; - h = gethostbyname(hostname); + if ((h = gethostbyname(hostname)) == NULL) { + return -1; + } haddr.sin_family = h->h_addrtype; haddr.sin_port = htons(port); memcpy(&haddr.sin_addr, h->h_addr, h->h_length); @@ -79,3 +85,35 @@ connect_to_peer(struct sockaddr_in *peer, int port) } return sd; } + + +struct qa_packet * +recv_packet(int fd) +{ + char *packetbuf; + int packetsize; + struct qa_packet *qp = NULL; + + packetbuf = malloc(QARSH_MAX_PACKET_SIZE); + memset(packetbuf, 0, QARSH_MAX_PACKET_SIZE); + + packetsize = read(fd, packetbuf, QARSH_MAX_PACKET_SIZE); + if (packetsize > 0) { + qp = parse_packets(packetbuf, packetsize); + } + free(packetbuf); + return qp; +} + +int +send_packet(int fd, struct qa_packet *qp) +{ + char *packetbuf; + int packetsize; + + packetbuf = malloc(1024); + memset(packetbuf, 0, 1024); + packetbuf = qptostr(qp, &packetbuf, &packetsize); + + return write(fd, packetbuf, packetsize); +} diff --git a/qarsh/sockutil.h b/qarsh/sockutil.h index 36d021a..6e4b8dd 100644 --- a/qarsh/sockutil.h +++ b/qarsh/sockutil.h @@ -4,3 +4,5 @@ int getsockport(int sd); int bind_any(int minport); int connect_to_host(char *hostname, int port); int connect_to_peer(struct sockaddr_in *peer, int port); +struct qa_packet * recv_packet(int fd); +int send_packet(int fd, struct qa_packet *qp); -- cgit