Compare commits

..

2 Commits

Author SHA1 Message Date
ba554dc716 Finished initClientConnection() in socket 2026-02-11 17:09:50 +10:00
34f562149e Added global variable for OSSP version 2026-02-11 17:00:24 +10:00
4 changed files with 295 additions and 99 deletions

View File

@@ -36,6 +36,7 @@ int configHandler_Read(configHandler_config_t** configObj) {
(*configObj)->internal_opensubsonic_clientName = NULL; (*configObj)->internal_opensubsonic_clientName = NULL;
(*configObj)->internal_opensubsonic_loginSalt = NULL; (*configObj)->internal_opensubsonic_loginSalt = NULL;
(*configObj)->internal_opensubsonic_loginToken = NULL; (*configObj)->internal_opensubsonic_loginToken = NULL;
(*configObj)->internal_ossp_version = NULL;
(*configObj)->listenbrainz_enable = false; (*configObj)->listenbrainz_enable = false;
(*configObj)->listenbrainz_token = NULL; (*configObj)->listenbrainz_token = NULL;
(*configObj)->lastfm_enable = false; (*configObj)->lastfm_enable = false;
@@ -71,6 +72,7 @@ int configHandler_Read(configHandler_config_t** configObj) {
// Set internal configuration values // Set internal configuration values
(*configObj)->internal_opensubsonic_version = strdup("1.8.0"); (*configObj)->internal_opensubsonic_version = strdup("1.8.0");
(*configObj)->internal_opensubsonic_clientName = strdup("Hojuix_OSSP"); (*configObj)->internal_opensubsonic_clientName = strdup("Hojuix_OSSP");
(*configObj)->internal_ossp_version = strdup("v0.4a");
// Form the path to the config JSON // Form the path to the config JSON
char* config_path = NULL; char* config_path = NULL;
@@ -477,6 +479,7 @@ void configHandler_Free(configHandler_config_t** configObj) {
if ((*configObj)->internal_opensubsonic_clientName != NULL) { free((*configObj)->internal_opensubsonic_clientName); } if ((*configObj)->internal_opensubsonic_clientName != NULL) { free((*configObj)->internal_opensubsonic_clientName); }
if ((*configObj)->internal_opensubsonic_loginSalt != NULL) { free((*configObj)->internal_opensubsonic_loginSalt); } if ((*configObj)->internal_opensubsonic_loginSalt != NULL) { free((*configObj)->internal_opensubsonic_loginSalt); }
if ((*configObj)->internal_opensubsonic_loginToken != NULL) { free((*configObj)->internal_opensubsonic_loginToken); } if ((*configObj)->internal_opensubsonic_loginToken != NULL) { free((*configObj)->internal_opensubsonic_loginToken); }
if ((*configObj)->internal_ossp_version != NULL) { free((*configObj)->internal_ossp_version); }
if ((*configObj)->listenbrainz_token != NULL) { free((*configObj)->listenbrainz_token); } if ((*configObj)->listenbrainz_token != NULL) { free((*configObj)->listenbrainz_token); }
if ((*configObj)->lastfm_username != NULL) { free((*configObj)->lastfm_username); } if ((*configObj)->lastfm_username != NULL) { free((*configObj)->lastfm_username); }
if ((*configObj)->lastfm_password != NULL) { free((*configObj)->lastfm_password); } if ((*configObj)->lastfm_password != NULL) { free((*configObj)->lastfm_password); }

View File

@@ -25,6 +25,7 @@ typedef struct {
char* internal_opensubsonic_clientName; // (Internal) Opensubsonic Client Name char* internal_opensubsonic_clientName; // (Internal) Opensubsonic Client Name
char* internal_opensubsonic_loginSalt; // (Internal) Opensubsonic Login Salt char* internal_opensubsonic_loginSalt; // (Internal) Opensubsonic Login Salt
char* internal_opensubsonic_loginToken; // (Internal) Opensubsonic Login Token char* internal_opensubsonic_loginToken; // (Internal) Opensubsonic Login Token
char* internal_ossp_version; // (Internal) OSSP Version
// Scrobbler Settings // Scrobbler Settings
bool listenbrainz_enable; // Enable ListenBrainz Scrobbling bool listenbrainz_enable; // Enable ListenBrainz Scrobbling

View File

@@ -8,14 +8,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include "external/cJSON.h"
#include "configHandler.h"
#include "socket.h" #include "socket.h"
#define SOCKET_PATH "/tmp/ossp_socket" // TODO Make this configurable through the configuration file #define SOCKET_PATH "/tmp/ossp_socket" // TODO Make this configurable through the configuration file
@@ -25,19 +25,21 @@ static int rc = -1;
socklen_t client_len; socklen_t client_len;
struct sockaddr_un server_addr; struct sockaddr_un server_addr;
struct sockaddr_un client_addr; struct sockaddr_un client_addr;
extern configHandler_config_t* configObj;
void socketHandler_read(); void socketHandler_read();
void socketHandler_initClientConnection();
void socketHandler_getConnInfo(); int SockSig_Length = 4;
#define SOCKSIG_CLIENT_CONNECT 0xE3566C2E
const uint32_t SockSig_Client_Connect = 0xE3566C2E; const uint32_t OSSP_Sock_ACK = 0x7253FF87;
#define SOCKSIG_SIZE 0x1F7E8BCF const uint32_t OSSP_Sock_CliConn = 0xE3566C2E;
#define SOCKSIG_ACK 0x7253FF87 const uint32_t OSSP_Sock_GetConnInfo = 0x8E4F6B01;
const uint32_t SockSig_Ack = 0x7253FF87; const uint32_t OSSP_Sock_Size = 0x1F7E8BCF;
const uint32_t SockSig_GetConnInfo = 0x8E4F6B01;
const uint32_t SockSig_Size = 0x1F7E8BCF; const uint32_t OSSP_Sock_ClientGetReq = 0x210829CF;
void socket_setup() { void socket_setup() {
@@ -87,16 +89,21 @@ void socket_setup() {
//char buffer[256]; printf("------------------------------\n");
//client_len = sizeof(struct sockaddr_un);
//read(client_fd, buffer, 256);
//printf("Received: %s\n", buffer);
//printf("Client len: %d\n", client_len);
// Cleanup
//close(client_fd); // Receive ClientGetReq with Size
//close(server_fd); int size = 0;
//unlink(SOCKET_PATH); socketHandler_receiveCliGetReq(&size);
printf("Size to alloc: %d bytes\n", size);
char* reqBuf = malloc(size);
// Send ACK
socketHandler_sendAck();
// Receive JSON data
socketHandler_receiveJson(&reqBuf, size);
printf("Received JSON: %s\n", reqBuf);
} }
void socketHandler_cleanup() { void socketHandler_cleanup() {
@@ -107,14 +114,10 @@ void socketHandler_cleanup() {
unlink(SOCKET_PATH); unlink(SOCKET_PATH);
} }
void socketHandler_read() {
char buf[16];
int buflen = 16;
for (int i = 0; i < buflen; i++) { buf[i] = 0x00; }
rc = read(client_fd, buf, buflen);
printf("Read %d: %s\n", buflen, buf);
printf("RC was %d\n", rc);
}
// Byte Array to uint32_t Big Endian // Byte Array to uint32_t Big Endian
uint32_t util_byteArrToUint32BE(char buf[]) { uint32_t util_byteArrToUint32BE(char buf[]) {
@@ -124,89 +127,254 @@ uint32_t util_byteArrToUint32BE(char buf[]) {
return retVal; return retVal;
} }
void socketHandler_initClientConnection() { // Byte Array to uint32_t Little Endian
// If reached here, a client has connected to the AF_UNIX socket uint32_t util_byteArrToUint32LE(char buf[]) {
uint32_t retVal = 0x0;
retVal = buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
return retVal;
}
// Receive CliConn int socketHandler_initClientConnection() {
int cliConnSize = 4; /*
char buf[4] = { 0x00 }; * - Wait for client to send OSSP_Sock_CliConn
rc = read(client_fd, buf, cliConnSize); * - Send OSSP_Sock_ACK back
if (rc != cliConnSize) { * - Wait for client to send OSSP_Sock_GetConnInfo
printf("Received data was not correct.\n"); * - Send back OSSP_Sock_Size
* - Wait for client to send OSSP_Sock_ACK
* - Send JSON
* - Wait for client to send OSSP_Sock_ACK
*/
if (socketHandler_receiveCliConn() != 0) {
printf("[SocketHandler] initClientConnection() failed.\n");
return 1;
} }
uint32_t cliConn = util_byteArrToUint32BE(buf);
rc = memcmp(&cliConn, &SockSig_Client_Connect, 4); if (socketHandler_sendAck() != 0) {
printf("[SocketHandler] initClientConnection() failed.\n");
return 1;
}
if (socketHandler_receiveGetConnInfo() != 0) {
printf("[SockerHandler] initClientConnection() failed.\n");
return 1;
}
// Form JSON of connection info
char* serverAddr = NULL;
rc = asprintf(&serverAddr, "%s://%s", configObj->opensubsonic_protocol, configObj->opensubsonic_server);
if (rc == -1) {
printf("[SocketHandler] asprintf() failed.\n");
return 1;
}
cJSON* connInfoObj = cJSON_CreateObject();
cJSON_AddItemToObject(connInfoObj, "ossp_version", cJSON_CreateString(configObj->internal_ossp_version));
cJSON_AddItemToObject(connInfoObj, "server_addr", cJSON_CreateString(serverAddr));
char* connInfoStr = cJSON_PrintUnformatted(connInfoObj);
int connInfoLen = strlen(connInfoStr);
free(serverAddr);
cJSON_Delete(connInfoObj);
if (socketHandler_sendSize(connInfoLen) != 0) {
printf("[SockerHandler] initClientConnection() failed.\n");
return 1;
}
if (socketHandler_receiveAck() != 0) {
printf("[SockerHandler] initClientConnection() failed.\n");
return 1;
}
if (socketHandler_sendJson(connInfoStr, connInfoLen) != 0) {
printf("[SockerHandler] initClientConnection() failed.\n");
return 1;
}
if (socketHandler_receiveAck() != 0) {
printf("[SockerHandler] initClientConnection() failed.\n");
return 1;
}
return 0;
}
int socketHandler_sendAck() {
printf("[SocketHandler] Sending OSSP_Sock_ACK.\n");
rc = send(client_fd, &OSSP_Sock_ACK, SockSig_Length, 0);
if (rc != SockSig_Length) {
printf("[SocketHandler] Failed to send OSSP_Sock_ACK.\n");
return 1;
}
return 0;
}
int socketHandler_receiveAck() {
char buf[4] = { 0x00 };
rc = read(client_fd, buf, SockSig_Length);
if (rc != SockSig_Length) {
printf("[SocketHandler] Failed to receive OSSP_Sock_ACK (Signature is the wrong length).\n");
return 1;
}
uint32_t AckBE = util_byteArrToUint32LE(buf);
rc = memcmp(&AckBE, &OSSP_Sock_ACK, SockSig_Length);
if (rc != 0) { if (rc != 0) {
printf("Received CliConn signature is not valid.\n"); printf("[SocketHandler] Failed to receive OSSP_Sock_ACK (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_ACK, AckBE);
// TODO return 1;
} else {
printf("Client connected!\n");
} }
printf("[SocketHandler] Received OSSP_Sock_ACK.\n");
// Send Server ACK return 0;
rc = send(client_fd, &SockSig_Ack, 4, 0);
if (rc != 4) {
printf("Failed to send Server ACK.\n");
} else {
printf("Sent Server ACK.\n");
}
// Deal with connection info
socketHandler_getConnInfo();
} }
typedef struct {
uint32_t signature;
uint16_t size;
} __attribute__((packed)) OSSP_Sock_Size_Data;
void socketHandler_getConnInfo() {
// Receive OSSP_Sock_GetConnInfo int socketHandler_receiveCliConn() {
int sigSize = 4;
char buf[4] = { 0x00 }; char buf[4] = { 0x00 };
rc = read(client_fd, buf, sigSize); rc = read(client_fd, buf, SockSig_Length);
if (rc != sigSize) { if (rc != SockSig_Length) {
printf("OSSP_Sock_GetConnInfo Error 1\n"); printf("[SocketHandler] Failed to receive OSSP_Sock_CliConn (Signature is the wrong length).\n");
// TODO return 1;
} else {
printf("Signature verified.\n");
} }
// Assemble JSON //uint32_t CliConnBE = util_byteArrToUint32BE(buf);
char* jsonInfo = "{ 'ossp_version': '0.3a', 'server_addr': 'https://eumak.hojuix.org' }"; uint32_t CliConnBE = util_byteArrToUint32LE(buf);
int jsonLen = strlen(jsonInfo);
printf("JSON: %s\n", jsonInfo);
printf("JSON Length: %d\n", jsonLen);
// Send OSSP_Sock_Size rc = memcmp(&CliConnBE, &OSSP_Sock_CliConn, SockSig_Length);
OSSP_Sock_Size_Data ossp_sock; if (rc != 0) {
ossp_sock.signature = SockSig_Size; printf("[SocketHandler] Failed to receive OSSP_Sock_CliConn (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_CliConn, CliConnBE);
ossp_sock.size = jsonLen; return 1;
int sizeLen = 6; }
rc = send(client_fd, (char*)&ossp_sock, sizeLen, 0); printf("[SocketHandler] Received OSSP_Sock_CliConn.\n");
if (rc != sizeLen) { return 0;
printf("OSSP_Sock_Size failed.\n");
} else { printf("OSSP_Sock_Size sent.\n"); }
// Wait for ACK
char bufb[4] = { 0x00 };
rc = read(client_fd, bufb, sigSize);
if (rc != sigSize) {
printf("Invalid ACK sig size");
} else { printf("Sig verified\n"); }
// Send JSON
rc = send(client_fd, jsonInfo, jsonLen, 0);
if (rc != jsonLen) {
printf("Failed to send JSON.\n");
} else { printf("Sent JSON.\n"); }
} }
void sockerHandler_sendAck() {
// Send OSSP_Sock_ACK to the client
int socketHandler_receiveGetConnInfo() {
char buf[4] = { 0x00 };
rc = read(client_fd, buf, SockSig_Length);
if (rc != SockSig_Length) {
printf("[SocketHandler] Failed to receive OSSP_Sock_GetConnInfo (Signature is the wrong length).\n");
return 1;
}
uint32_t GetConnInfoBE = util_byteArrToUint32LE(buf);
rc = memcmp(&GetConnInfoBE, &OSSP_Sock_GetConnInfo, SockSig_Length);
if (rc != 0) {
printf("[SocketHandler] Failed to receive OSSP_Sock_GetConnInfo (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_GetConnInfo, GetConnInfoBE);
return 1;
}
printf("[SocketHandler] Received OSSP_Sock_GetConnInfo.\n");
return 0;
} }
void socketHandler_receiveAck() {
// Receive OSSP_Sock_ACK from the client
int socketHandler_sendSize(uint32_t size) {
printf("[SocketHandler] Sending OSSP_Sock_Size.\n");
OSSP_Sock_Size_t sizeData;
sizeData.signature = OSSP_Sock_Size;
sizeData.size = size;
rc = send(client_fd, (char*)&sizeData, 8, 0);
if (rc != 8) {
printf("[SocketHandler] Failed to send OSSP_Sock_Size.\n");
return 1;
}
return 0;
}
int socketHandler_receiveSize(uint32_t* size) {
char buf[8] = { 0x00 };
rc = read(client_fd, buf, 8);
if (rc != 8) {
printf("[SocketHandler] Failed to receive OSSP_Sock_Size (Invalid size).\n");
return 1;
}
OSSP_Sock_Size_t* sizeData = (OSSP_Sock_Size_t*)&buf;
rc = memcmp(&sizeData->signature, &OSSP_Sock_Size, 4);
if (rc != 0) {
printf("[SocketHandler] Failed to receive OSSP_Sock_Size (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_Size, sizeData->signature);
return 1;
}
printf("[SocketHandler] Received OSSP_Sock_Size.\n");
*size = sizeData->size;
return 0;
}
int socketHandler_receiveCliGetReq(int* size) {
char buf[8] = { 0x00 };
rc = read(client_fd, buf, 8);
if (rc != 8) {
printf("[SocketHandler] Failed to receive OSSP_Sock_ClientGetReq (Invalid size).\n");
return 1;
}
OSSP_Sock_ClientGetReq_t* clientGetReq = (OSSP_Sock_ClientGetReq_t*)&buf;
rc = memcmp(&clientGetReq->signature, &OSSP_Sock_ClientGetReq, 4);
if (rc != 0) {
printf("[SocketHandler] Failed to receive OSSP_Sock_ClientGetReq (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_ClientGetReq, clientGetReq->signature);
return 1;
}
printf("[SocketHandler] Received OSSP_Sock_ClientGetReq.\n");
*size = clientGetReq->size;
return 0;
}
int socketHandler_receiveJson(char** data, int size) {
rc = read(client_fd, *data, size);
if (rc != size) {
printf("[SocketHandler] Failed to receive generic JSON data.\n");
return 1;
}
printf("[SocketHandler] Received generic JSON data.\n");
return 0;
}
int socketHandler_sendJson(char* json, int size) {
printf("[SocketHandler] Sending JSON.\n");
rc = send(client_fd, json, size, 0);
if (rc != size) {
printf("[SocketHandler] Failed to send JSON.\n");
return 1;
}
return 0;
}
int socketHandler_processClientGetReq() {
// Step 1 - Client sends GetReq with size
// Step 2 - Server allocates memory for future client request
// Step 3 - Server responds ACK
// Step 4 - --
} }

View File

@@ -1,6 +1,30 @@
#ifndef _SOCKET_H #ifndef _SOCKET_H
#define _SOCKET_H #define _SOCKET_H
#include <stdint.h>
typedef struct {
uint32_t signature;
uint32_t size;
} __attribute__((packed)) OSSP_Sock_Size_t;
typedef struct {
uint32_t signature;
uint32_t size;
} __attribute__((packed)) OSSP_Sock_ClientGetReq_t;
void socket_setup(); void socket_setup();
int socketHandler_initClientConnection();
int socketHandler_sendAck();
int socketHandler_receiveAck();
int socketHandler_receiveCliConn();
int socketHandler_receiveGetConnInfo();
int socketHandler_sendSize(uint32_t size);
int socketHandler_receiveSize(uint32_t* size);
int socketHandler_receiveCliGetReq(int* size);
int socketHandler_receiveJson(char** data, int size);
int socketHandler_sendJson(char* json, int size);
#endif #endif