2019-10-21 04:28:57 +02:00
|
|
|
#include "sonic.h"
|
|
|
|
|
2019-10-21 03:11:54 +02:00
|
|
|
#include "util.h"
|
2019-10-22 01:42:46 +02:00
|
|
|
#include "link.h"
|
2019-10-21 04:28:57 +02:00
|
|
|
#include "network.h"
|
|
|
|
|
2019-10-22 02:13:28 +02:00
|
|
|
#include <expat.h>
|
2019-10-21 03:11:54 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-10-21 04:28:57 +02:00
|
|
|
typedef struct {
|
|
|
|
char *server;
|
|
|
|
char *username;
|
|
|
|
char *password;
|
|
|
|
char *client;
|
|
|
|
char *api_version;
|
|
|
|
} SonicConfigStruct;
|
|
|
|
|
|
|
|
static SonicConfigStruct SONIC_CONFIG;
|
|
|
|
|
2019-10-21 03:11:54 +02:00
|
|
|
/**
|
2019-10-21 04:28:57 +02:00
|
|
|
* \brief initalise Subsonic configuration struct
|
2019-10-21 03:11:54 +02:00
|
|
|
*/
|
2019-10-21 04:28:57 +02:00
|
|
|
void sonic_config_init(const char *server, const char *username,
|
|
|
|
const char *password)
|
2019-10-21 03:11:54 +02:00
|
|
|
{
|
2019-10-21 04:28:57 +02:00
|
|
|
SONIC_CONFIG.server = strndup(server, MAX_PATH_LEN);
|
|
|
|
/* Correct for the extra '/' */
|
|
|
|
size_t server_url_len = strnlen(SONIC_CONFIG.server, MAX_PATH_LEN) - 1;
|
|
|
|
if (SONIC_CONFIG.server[server_url_len] == '/') {
|
|
|
|
SONIC_CONFIG.server[server_url_len] = '\0';
|
|
|
|
}
|
|
|
|
SONIC_CONFIG.username = strndup(username, MAX_FILENAME_LEN);
|
|
|
|
SONIC_CONFIG.password = strndup(password, MAX_FILENAME_LEN);
|
|
|
|
SONIC_CONFIG.client = DEFAULT_USER_AGENT;
|
|
|
|
/*
|
|
|
|
* API 1.13.0 is the minimum version that supports
|
|
|
|
* salt authentication scheme
|
|
|
|
*/
|
|
|
|
SONIC_CONFIG.api_version = "1.13.0";
|
2019-10-21 03:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-21 04:28:57 +02:00
|
|
|
* \brief generate authentication string
|
2019-10-21 03:11:54 +02:00
|
|
|
*/
|
2019-10-21 04:28:57 +02:00
|
|
|
static char *sonic_gen_auth_str()
|
2019-10-21 03:11:54 +02:00
|
|
|
{
|
2019-10-21 04:28:57 +02:00
|
|
|
char *salt = generate_salt();
|
|
|
|
size_t password_len = strnlen(SONIC_CONFIG.password, MAX_FILENAME_LEN);
|
|
|
|
size_t password_salt_len = password_len + strnlen(salt, MAX_FILENAME_LEN);
|
|
|
|
char *password_salt = CALLOC(password_salt_len + 1, sizeof(char));
|
|
|
|
strncat(password_salt, SONIC_CONFIG.password, MAX_FILENAME_LEN);
|
|
|
|
strncat(password_salt + password_len, salt, MAX_FILENAME_LEN);
|
|
|
|
char *token = generate_md5sum(password_salt);
|
|
|
|
char *auth_str = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
|
|
|
|
snprintf(auth_str, MAX_PATH_LEN,
|
|
|
|
".view?u=%s&t=%s&s=%s&v=%s&c=%s",
|
|
|
|
SONIC_CONFIG.username, token, salt,
|
|
|
|
SONIC_CONFIG.api_version, SONIC_CONFIG.client);
|
|
|
|
free(salt);
|
|
|
|
free(token);
|
|
|
|
return auth_str;
|
|
|
|
}
|
2019-10-21 03:11:54 +02:00
|
|
|
|
2019-10-21 04:28:57 +02:00
|
|
|
/**
|
|
|
|
* \brief generate the first half of the request URL
|
|
|
|
*/
|
|
|
|
static char *sonic_gen_url_first_part(char *method)
|
|
|
|
{
|
|
|
|
char *auth_str = sonic_gen_auth_str();
|
|
|
|
char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
|
|
|
|
snprintf(url, MAX_PATH_LEN, "%s/rest/%s%s", SONIC_CONFIG.server, method,
|
|
|
|
auth_str);
|
|
|
|
free(auth_str);
|
|
|
|
return url;
|
|
|
|
}
|
2019-10-21 03:11:54 +02:00
|
|
|
|
2019-10-22 01:42:46 +02:00
|
|
|
LinkTable *sonic_LinkTable_new(const int id)
|
2019-10-21 04:28:57 +02:00
|
|
|
{
|
|
|
|
char *url;
|
|
|
|
if (id > 0) {
|
|
|
|
char *first_part = sonic_gen_url_first_part("getMusicDirectory");
|
|
|
|
url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
|
|
|
|
snprintf(url, MAX_PATH_LEN, "%s&id=%d", first_part, id);
|
|
|
|
free(first_part);
|
|
|
|
} else {
|
|
|
|
url = sonic_gen_url_first_part("getIndexes");
|
2019-10-21 03:11:54 +02:00
|
|
|
}
|
2019-10-21 04:28:57 +02:00
|
|
|
|
2019-10-22 01:42:46 +02:00
|
|
|
LinkTable *linktbl = LinkTable_alloc(url);
|
2019-10-21 04:28:57 +02:00
|
|
|
|
2019-10-22 01:42:46 +02:00
|
|
|
/* start downloading the base URL */
|
|
|
|
MemoryStruct buf = Link_to_MemoryStruct(linktbl->links[0]);
|
|
|
|
if (buf.size == 0) {
|
|
|
|
LinkTable_free(linktbl);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-10-21 03:11:54 +02:00
|
|
|
|
2019-10-22 02:13:28 +02:00
|
|
|
printf("%s", buf.memory);
|
2019-10-21 03:11:54 +02:00
|
|
|
|
2019-10-22 02:13:28 +02:00
|
|
|
free(buf.memory);
|
2019-10-22 01:42:46 +02:00
|
|
|
free(url);
|
|
|
|
return NULL;
|
2019-10-21 03:11:54 +02:00
|
|
|
}
|
2019-10-22 01:42:46 +02:00
|
|
|
|
|
|
|
// int main(int argc, char **argv)
|
|
|
|
// {
|
|
|
|
// (void) argc;
|
|
|
|
// (void) argv;
|
|
|
|
//
|
|
|
|
// sonic_config_init(argv[1], argv[2], argv[3]);
|
|
|
|
// sonic_LinkTable_new(0);
|
|
|
|
// sonic_LinkTable_new(3);
|
|
|
|
// }
|