diff options
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); +} |