X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Flibmpdclient.c;h=e0dfe2ceb39adb8aa85e79b0207106b2fedda74f;hb=52e86f4bf6054450ebc31076ff3c25d793ae96cf;hp=d66b3ab8ea25d91e113ffe8754e2459d4d63804f;hpb=a6a4a4c548d92952759d7cc99bfe6628b27a2e56;p=monky diff --git a/src/libmpdclient.c b/src/libmpdclient.c index d66b3ab..e0dfe2c 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -1,4 +1,7 @@ -/* libmpdclient +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- + * vim: ts=4 sw=4 noet ai cindent syntax=c + * + * libmpdclient * (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com) * This project's homepage is: http://www.musicpd.org * @@ -29,9 +32,7 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id$ - * - * */ + */ #include "conky.h" #include "libmpdclient.h" @@ -39,11 +40,8 @@ #include #include #include -#include #include -#include #include -#include #include #include @@ -54,6 +52,7 @@ # include # include # include +# include # include #endif @@ -121,6 +120,38 @@ static int do_connect_fail(mpd_Connection *connection, } #endif /* !WIN32 */ +static int uds_connect(mpd_Connection *connection, const char *host, + float timeout) +{ + struct sockaddr_un addr; + + strncpy(addr.sun_path, host, sizeof(addr.sun_path)-1); + addr.sun_family = AF_UNIX; + addr.sun_path[sizeof(addr.sun_path)-1] = 0; + connection->sock = socket(AF_UNIX, SOCK_STREAM, 0); + + if (connection->sock < 0) { + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, + "problems creating socket: %s", strerror(errno)); + connection->error = MPD_ERROR_SYSTEM; + return -1; + } + + mpd_setConnectionTimeout(connection, timeout); + + /* connect stuff */ + if (do_connect_fail(connection, (struct sockaddr *)&addr, SUN_LEN(&addr))) { + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, + "problems connecting socket: %s", strerror(errno)); + closesocket(connection->sock); + connection->sock = -1; + connection->error = MPD_ERROR_SYSTEM; + return -1; + } + + return 0; +} + #ifdef MPD_HAVE_GAI static int mpd_connect(mpd_Connection *connection, const char *host, int port, float timeout) @@ -131,9 +162,12 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, struct addrinfo *res = NULL; struct addrinfo *addrinfo = NULL; + if (*host == '/') + return uds_connect(connection, host, timeout); + /* Setup hints */ hints.ai_flags = AI_ADDRCONFIG; - hints.ai_family = PF_UNSPEC; + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_addrlen = 0; @@ -195,27 +229,41 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, static int mpd_connect(mpd_Connection *connection, const char *host, int port, float timeout) { - struct hostent *he; + struct hostent he, *he_res = 0; + int he_errno; + char hostbuff[2048]; struct sockaddr *dest; int destlen; struct sockaddr_in sin; - if (!(he = gethostbyname(host))) { + if (*host == '/') + return uds_connect(connection, host, timeout); + +#ifdef HAVE_GETHOSTBYNAME_R + if (gethostbyname_r(rhost, &he, hostbuff, sizeof(hostbuff), &he_res, &he_errno)) { // get the host info + snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, + "%s ('%s')", hstrerror(h_errno), host); + connection->error = MPD_ERROR_UNKHOST; + return -1; + } +#else /* HAVE_GETHOSTBYNAME_R */ + if (!(he_res = gethostbyname(host))) { snprintf(connection->errorStr, MPD_ERRORSTR_MAX_LENGTH, "host \"%s\" not found", host); connection->error = MPD_ERROR_UNKHOST; return -1; } +#endif /* HAVE_GETHOSTBYNAME_R */ memset(&sin, 0, sizeof(struct sockaddr_in)); - /* dest.sin_family = he->h_addrtype; */ + /* dest.sin_family = he_res->h_addrtype; */ sin.sin_family = AF_INET; sin.sin_port = htons(port); - switch (he->h_addrtype) { + switch (he_res->h_addrtype) { case AF_INET: - memcpy((char *) &sin.sin_addr.s_addr, (char *) he->h_addr, - he->h_length); + memcpy((char *) &sin.sin_addr.s_addr, (char *) he_res->h_addr, + he_res->h_length); dest = (struct sockaddr *) &sin; destlen = sizeof(struct sockaddr_in); break;