diff options
author | Nate Straz <nstraz@redhat.com> | 2005-08-24 21:46:50 +0000 |
---|---|---|
committer | Nate Straz <nstraz@redhat.com> | 2005-08-24 21:46:50 +0000 |
commit | 0c59da8a7310e1e3811f873fd0106ccebdb53c53 (patch) | |
tree | c2705f5e8e5db9c158686ae853136343c3027226 /qarsh/sockutil.c | |
parent | 5e1aaf26033163543a0758fe50e52b7b17a9e530 (diff) | |
download | qarsh-0c59da8a7310e1e3811f873fd0106ccebdb53c53.tar.gz qarsh-0c59da8a7310e1e3811f873fd0106ccebdb53c53.tar.xz qarsh-0c59da8a7310e1e3811f873fd0106ccebdb53c53.zip |
Move send_packet() and recv_packet() to sockutil.c so qarsh can use them too.
Diffstat (limited to 'qarsh/sockutil.c')
-rw-r--r-- | qarsh/sockutil.c | 40 |
1 files changed, 39 insertions, 1 deletions
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 <netdb.h> #include <errno.h> #include <syslog.h> +#include <stdlib.h> +#include <unistd.h> + +#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); +} |