diff options
Diffstat (limited to 'lib/libfetch')
-rw-r--r-- | lib/libfetch/common.c | 34 | ||||
-rw-r--r-- | lib/libfetch/common.h | 2 |
2 files changed, 30 insertions, 6 deletions
diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index 05104fe..e4dbf3a 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -31,6 +31,7 @@ #include <sys/param.h> #include <sys/socket.h> #include <sys/time.h> +#include <sys/uio.h> #include <netinet/in.h> #include <errno.h> @@ -58,6 +59,9 @@ static struct fetcherr _netdb_errlist[] = { { -1, FETCH_UNKNOWN, "Unknown resolver error" } }; +/* End-of-Line */ +static char ENDL[2] = "\r\n"; + /*** Error-reporting functions ***********************************************/ @@ -193,18 +197,17 @@ _fetch_connect(char *host, int port, int af, int verbose) _fetch_info("connecting to %s:%d", host, port); /* try to connect */ - sd = -1; - for (res = res0; res; res = res->ai_next) { + for (sd = -1, res = res0; res; res = res->ai_next) { if ((sd = socket(res->ai_family, res->ai_socktype, - res->ai_protocol)) < 0) + res->ai_protocol)) == -1) continue; - if (connect(sd, res->ai_addr, res->ai_addrlen) >= 0) + if (connect(sd, res->ai_addr, res->ai_addrlen) != -1) break; close(sd); sd = -1; } freeaddrinfo(res0); - if (sd < 0) { + if (sd == -1) { _fetch_syserr(); return -1; } @@ -295,6 +298,27 @@ _fetch_getln(int fd, char **buf, size_t *size, size_t *len) } +/* + * Write a line of text to a socket w/ timeout + * XXX currently does not enforce timeout + */ +int +_fetch_putln(int fd, char *str, size_t len) +{ + struct iovec iov[2]; + ssize_t wlen; + + /* XXX should enforce timeout */ + iov[0].iov_base = str; + iov[0].iov_len = len; + iov[1].iov_base = ENDL; + iov[1].iov_len = sizeof ENDL; + wlen = writev(fd, iov, 2); + DEBUG(fprintf(stderr, "\033[1m>>> %s\n\033[m", str)); + return (wlen != len); +} + + /*** Directory-related utility functions *************************************/ int diff --git a/lib/libfetch/common.h b/lib/libfetch/common.h index 79bd54d..c39e879 100644 --- a/lib/libfetch/common.h +++ b/lib/libfetch/common.h @@ -32,7 +32,6 @@ #define _COMMON_H_INCLUDED /* Structure used for error message lists */ -#define ERRCAT_ struct fetcherr { const int num, cat; const char *string; @@ -43,6 +42,7 @@ void _fetch_syserr(void); void _fetch_info(char *fmt, ...); int _fetch_connect(char *host, int port, int af, int verbose); int _fetch_getln(int fd, char **buf, size_t *size, size_t *len); +int _fetch_putln(int fd, char *str, size_t len); int _fetch_add_entry(struct url_ent **p, int *size, int *len, char *name, struct url_stat *stat); |