Logo Search packages:      
Sourcecode: libmpdclient version File versions  Download package

connection.c

/* libmpdclient
   (c) 2003-2009 The Music Player Daemon Project
   This project's homepage is: http://www.musicpd.org

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   - Neither the name of the Music Player Daemon nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <mpd/connection.h>
#include <mpd/async.h>
#include <mpd/parser.h>
#include <mpd/password.h>
#include "resolver.h"
#include "sync.h"
#include "socket.h"
#include "internal.h"
#include "iasync.h"
#include "config.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#define MPD_WELCOME_MESSAGE   "OK MPD "

static bool
mpd_parse_welcome(struct mpd_connection *connection, const char *output)
{
      const char *tmp;
      char * test;

      if (strncmp(output,MPD_WELCOME_MESSAGE,strlen(MPD_WELCOME_MESSAGE))) {
            mpd_error_code(&connection->error, MPD_ERROR_MALFORMED);
            mpd_error_message(&connection->error,
                          "Malformed connect message received");
            return false;
      }

      tmp = &output[strlen(MPD_WELCOME_MESSAGE)];
      connection->version[0] = strtol(tmp, &test, 10);
      if (test == tmp) {
            mpd_error_code(&connection->error, MPD_ERROR_MALFORMED);
            mpd_error_message(&connection->error,
                          "Malformed version number in connect message");
            return false;
      }

      if (*test == '.') {
            connection->version[1] = strtol(test + 1, &test, 10);
            if (*test == '.')
                  connection->version[2] = strtol(test + 1, &test, 10);
            else
                  connection->version[2] = 0;
      } else {
            connection->version[1] = 0;
            connection->version[2] = 0;
      }

      return true;
}

void
mpd_connection_sync_error(struct mpd_connection *connection)
{
      if (mpd_async_copy_error(connection->async, &connection->error)) {
            /* no error noticed by async: must be a timeout in the
               sync.c code */
            mpd_error_code(&connection->error, MPD_ERROR_TIMEOUT);
            mpd_error_message(&connection->error, "Timeout");
      }
}

/**
 * Parses the password from the host specification in the form
 * "password@hostname".
 *
 * @param host_p a pointer to the "host" variable, which may be
 * modified by this function
 * @return an allocated password string, or NULL if there was no
 * password
 */
static const char *
mpd_parse_host_password(const char *host, char **password_r)
{
      const char *at;
      char *password;

      assert(password_r != NULL);
      assert(*password_r == NULL);

      if (host == NULL)
            return host;

      at = strchr(host, '@');
      if (at == NULL)
            return host;

      password = malloc(at - host + 1);
      if (password != NULL) {
            /* silently ignoring out-of-memory */
            memcpy(password, host, at - host);
            password[at - host] = 0;
            *password_r = password;
      }

      return at + 1;
}

/**
 * Parses the host specification.  If not specified, it attempts to
 * load it from the environment variable MPD_HOST.
 */
static const char *
mpd_check_host(const char *host, char **password_r)
{
      assert(password_r != NULL);
      assert(*password_r == NULL);

      if (host == NULL)
            host = getenv("MPD_HOST");

      if (host != NULL)
            host = mpd_parse_host_password(host, password_r);

      return host;
}

/**
 * Parses the port specification.  If not specified (0), it attempts
 * to load it from the environment variable MPD_PORT.
 */
static unsigned
mpd_check_port(unsigned port)
{
      if (port == 0) {
            const char *env_port = getenv("MPD_PORT");
            if (env_port != NULL)
                  port = atoi(env_port);
      }

      return port;
}

static int
mpd_connect(const char *host, unsigned port, const struct timeval *timeout,
          struct mpd_error_info *error)
{
#ifdef DEFAULT_SOCKET
      if (host == NULL && port == 0) {
            int fd = mpd_socket_connect(DEFAULT_SOCKET, 0, timeout, error);
            if (fd >= 0)
                  return fd;

            mpd_error_clear(error);
      }
#endif

      if (host == NULL)
            host = DEFAULT_HOST;

      if (port == 0)
            port = DEFAULT_PORT;

      return mpd_socket_connect(host, port, timeout, error);
}

struct mpd_connection *
00192 mpd_connection_new(const char *host, unsigned port, unsigned timeout_ms)
{
      struct mpd_connection *connection = malloc(sizeof(*connection));
      bool success;
      int fd;
      const char *line;
      char *password = NULL;


      if (connection == NULL)
            return NULL;

      mpd_error_init(&connection->error);
      connection->async = NULL;
      connection->parser = NULL;
      connection->receiving = false;
      connection->sending_command_list = false;
      connection->pair_state = PAIR_STATE_NONE;
      connection->request = NULL;

      if (!mpd_socket_global_init(&connection->error))
            return connection;

      if (timeout_ms == 0)
            /* 30s is the default */
            timeout_ms = 30000;

      mpd_connection_set_timeout(connection, timeout_ms);

      host = mpd_check_host(host, &password);
      port = mpd_check_port(port);

      fd = mpd_connect(host, port, &connection->timeout, &connection->error);
      if (fd < 0) {
            free(password);
            return connection;
      }

      connection->async = mpd_async_new(fd);
      if (connection->async == NULL) {
            free(password);
            mpd_socket_close(fd);
            mpd_error_code(&connection->error, MPD_ERROR_OOM);
            return connection;
      }

      connection->parser = mpd_parser_new();
      if (connection->parser == NULL) {
            free(password);
            mpd_error_code(&connection->error, MPD_ERROR_OOM);
            return connection;
      }

      line = mpd_sync_recv_line(connection->async, &connection->timeout);
      if (line == NULL) {
            free(password);
            mpd_connection_sync_error(connection);
            return connection;
      }

      success = mpd_parse_welcome(connection, line);

      if (password != NULL) {
            if (success)
                  mpd_run_password(connection, password);
            free(password);
      }

      return connection;
}

struct mpd_connection *
00264 mpd_connection_new_async(struct mpd_async *async, const char *welcome)
{
      struct mpd_connection *connection = malloc(sizeof(*connection));

      assert(async != NULL);
      assert(welcome != NULL);

      if (connection == NULL)
            return NULL;

      mpd_error_init(&connection->error);
      connection->async = async;
      connection->timeout.tv_sec = 30;
      connection->timeout.tv_usec = 0;
      connection->parser = NULL;
      connection->receiving = false;
      connection->sending_command_list = false;
      connection->pair_state = PAIR_STATE_NONE;
      connection->request = NULL;

      if (!mpd_socket_global_init(&connection->error))
            return connection;

      connection->parser = mpd_parser_new();
      if (connection->parser == NULL) {
            mpd_error_code(&connection->error, MPD_ERROR_OOM);
            return connection;
      }

      mpd_parse_welcome(connection, welcome);

      return connection;
}

00298 void mpd_connection_free(struct mpd_connection *connection)
{
      assert(connection->pair_state != PAIR_STATE_FLOATING);

      if (connection->parser != NULL)
            mpd_parser_free(connection->parser);

      if (connection->async != NULL)
            mpd_async_free(connection->async);

      if (connection->request) free(connection->request);

      mpd_error_deinit(&connection->error);

      free(connection);
}

void
00316 mpd_connection_set_timeout(struct mpd_connection *connection,
                     unsigned timeout_ms)
{
      assert(timeout_ms > 0);

      connection->timeout.tv_sec = timeout_ms / 1000;
      connection->timeout.tv_usec = timeout_ms % 1000;
}

int
00326 mpd_connection_get_fd(const struct mpd_connection *connection)
{
      return mpd_async_get_fd(connection->async);
}

struct mpd_async *
00332 mpd_connection_get_async(struct mpd_connection *connection)
{
      return connection->async;
}

const unsigned *
00338 mpd_connection_get_server_version(const struct mpd_connection *connection)
{
      return connection->version;
}

int
00344 mpd_connection_cmp_server_version(const struct mpd_connection *connection,
                          unsigned major, unsigned minor,
                          unsigned patch)
{
      const unsigned *v = connection->version;

      if (v[0] > major || (v[0] == major &&
                       (v[1] > minor || (v[1] == minor &&
                                     v[2] > patch))))
            return 1;
      else if (v[0] == major && v[1] == minor && v[2] == patch)
            return 0;
      else
            return -1;
}

Generated by  Doxygen 1.6.0   Back to index