X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Flibmpdclient.c;h=e0dfe2ceb39adb8aa85e79b0207106b2fedda74f;hb=fd334774172ae43c8e55aba76ce06b3d61407517;hp=acc2c2e1b5eac930afabadf5acfbf96ba6b9b48e;hpb=db1c0061b02e9907c2c302d01499a33931fb0eb6;p=monky diff --git a/src/libmpdclient.c b/src/libmpdclient.c index acc2c2e..e0dfe2c 100644 --- a/src/libmpdclient.c +++ b/src/libmpdclient.c @@ -1,4 +1,5 @@ /* -*- 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) @@ -31,8 +32,6 @@ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * vim: ts=4 sw=4 noet ai cindent syntax=c - * */ #include "conky.h" @@ -53,6 +52,7 @@ # include # include # include +# include # include #endif @@ -120,9 +120,41 @@ 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 mpd_timeout) + float timeout) { int error; char service[INTLEN + 1]; @@ -130,13 +162,12 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, struct addrinfo *res = NULL; struct addrinfo *addrinfo = NULL; - /* Setup hints - * - * XXX: limit address family to PF_INET here. - * MPD does not support IPv6 yet, so if GAI returns - * an IPv6 address, the later connect() will fail. */ + if (*host == '/') + return uds_connect(connection, host, timeout); + + /* Setup hints */ hints.ai_flags = AI_ADDRCONFIG; - hints.ai_family = PF_INET; + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_addrlen = 0; @@ -170,7 +201,7 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, return -1; } - mpd_setConnectionTimeout(connection, mpd_timeout); + mpd_setConnectionTimeout(connection, timeout); /* connect stuff */ if (do_connect_fail(connection, res->ai_addr, res->ai_addrlen)) { @@ -196,7 +227,7 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, } #else /* !MPD_HAVE_GAI */ static int mpd_connect(mpd_Connection *connection, const char *host, int port, - float mpd_timeout) + float timeout) { struct hostent he, *he_res = 0; int he_errno; @@ -205,6 +236,9 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, int destlen; struct sockaddr_in sin; + 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, @@ -249,7 +283,7 @@ static int mpd_connect(mpd_Connection *connection, const char *host, int port, return -1; } - mpd_setConnectionTimeout(connection, mpd_timeout); + mpd_setConnectionTimeout(connection, timeout); /* connect stuff */ if (do_connect_fail(connection, dest, destlen)) { @@ -320,11 +354,11 @@ static void mpd_freeReturnElement(mpd_ReturnElement *re) free(re); } -void mpd_setConnectionTimeout(mpd_Connection *connection, float mpd_timeout) +void mpd_setConnectionTimeout(mpd_Connection *connection, float timeout) { connection->timeout.tv_sec = (int) timeout; connection->timeout.tv_usec = - (int) ((mpd_timeout - connection->timeout.tv_sec) * 1e6 + 0.5); + (int) ((timeout - connection->timeout.tv_sec) * 1e6 + 0.5); } static int mpd_parseWelcome(mpd_Connection *connection, const char *host, @@ -361,7 +395,7 @@ static int mpd_parseWelcome(mpd_Connection *connection, const char *host, return 0; } -mpd_Connection *mpd_newConnection(const char *host, int port, float mpd_timeout) +mpd_Connection *mpd_newConnection(const char *host, int port, float timeout) { int err; char *rt; @@ -387,7 +421,7 @@ mpd_Connection *mpd_newConnection(const char *host, int port, float mpd_timeout) return connection; } - if (mpd_connect(connection, host, port, mpd_timeout) < 0) { + if (mpd_connect(connection, host, port, timeout) < 0) { return connection; }