aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-09-15 13:27:33 +0200
committerMax Kellermann <max@duempel.org>2008-09-15 13:27:33 +0200
commit5f844850f75932d43c3a8bf03d0821bd438295bc (patch)
tree16429998465a566d9ef295fa70186ee1aa3c874c /src
parent83adc590c89c657e36da23b828f45ed7a881ec7a (diff)
downloadmpd-5f844850f75932d43c3a8bf03d0821bd438295bc.tar.gz
mpd-5f844850f75932d43c3a8bf03d0821bd438295bc.tar.xz
mpd-5f844850f75932d43c3a8bf03d0821bd438295bc.zip
added support for unix domain sockets
If a host name starts with a slash, it is assumed to be a unix domain socket path. The port is ignored. This code is disabled on WIN32, until someone tests it.
Diffstat (limited to '')
-rw-r--r--src/libmpdclient.c59
1 files changed, 58 insertions, 1 deletions
diff --git a/src/libmpdclient.c b/src/libmpdclient.c
index a52ce23ca..48d7d4f5f 100644
--- a/src/libmpdclient.c
+++ b/src/libmpdclient.c
@@ -51,6 +51,10 @@
# include <netdb.h>
#endif
+#ifndef WIN32
+#include <sys/un.h>
+#endif
+
#ifndef MSG_DONTWAIT
# define MSG_DONTWAIT 0
#endif
@@ -328,6 +332,53 @@ static int mpd_parseWelcome(mpd_Connection * connection, const char * host, int
return 0;
}
+#ifndef WIN32
+static int mpd_connect_un(mpd_Connection * connection,
+ const char * host, float timeout)
+{
+ int error, flags;
+ size_t path_length;
+ struct sockaddr_un sun;
+
+ path_length = strlen(host);
+ if (path_length >= sizeof(sun.sun_path)) {
+ strcpy(connection->errorStr, "unix socket path is too long");
+ connection->error = MPD_ERROR_UNKHOST;
+ return -1;
+ }
+
+ sun.sun_family = AF_UNIX;
+ memcpy(sun.sun_path, host, path_length + 1);
+
+ connection->sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (connection->sock < 0) {
+ strcpy(connection->errorStr, "problems creating socket");
+ connection->error = MPD_ERROR_SYSTEM;
+ return -1;
+ }
+
+ mpd_setConnectionTimeout(connection, timeout);
+
+ flags = fcntl(connection->sock, F_GETFL, 0);
+ fcntl(connection->sock, F_SETFL, flags | O_NONBLOCK);
+
+ error = connect(connection->sock, (struct sockaddr*)&sun, sizeof(sun));
+ if (error < 0) {
+ /* try the next address family */
+ close(connection->sock);
+ connection->sock = 0;
+
+ snprintf(connection->errorStr,MPD_BUFFER_MAX_LENGTH,
+ "problems connecting to \"%s\": %s",
+ host, strerror(errno));
+ connection->error = MPD_ERROR_CONNPORT;
+ return -1;
+ }
+
+ return 0;
+}
+#endif /* WIN32 */
+
mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) {
int err;
char * rt;
@@ -350,7 +401,13 @@ mpd_Connection * mpd_newConnection(const char * host, int port, float timeout) {
if (winsock_dll_error(connection))
return connection;
- if (mpd_connect(connection, host, port, timeout) < 0)
+#ifndef WIN32
+ if (host[0] == '/')
+ err = mpd_connect_un(connection, host, timeout);
+ else
+#endif
+ err = mpd_connect(connection, host, port, timeout);
+ if (err < 0)
return connection;
while(!(rt = strstr(connection->buffer,"\n"))) {