From ba554dc716c0098f0ede5bf61ee41f163721e836 Mon Sep 17 00:00:00 2001 From: Goldenkrew3000 Date: Wed, 11 Feb 2026 17:09:50 +1000 Subject: [PATCH] Finished initClientConnection() in socket --- src/socket.c | 366 +++++++++++++++++++++++++++++++++++++-------------- src/socket.h | 24 ++++ 2 files changed, 291 insertions(+), 99 deletions(-) diff --git a/src/socket.c b/src/socket.c index 0efc803..ab9c852 100644 --- a/src/socket.c +++ b/src/socket.c @@ -8,14 +8,14 @@ #include #include #include +#include #include #include - #include #include #include -#include - +#include "external/cJSON.h" +#include "configHandler.h" #include "socket.h" #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; struct sockaddr_un server_addr; struct sockaddr_un client_addr; +extern configHandler_config_t* configObj; void socketHandler_read(); -void socketHandler_initClientConnection(); -void socketHandler_getConnInfo(); -#define SOCKSIG_CLIENT_CONNECT 0xE3566C2E -const uint32_t SockSig_Client_Connect = 0xE3566C2E; -#define SOCKSIG_SIZE 0x1F7E8BCF -#define SOCKSIG_ACK 0x7253FF87 -const uint32_t SockSig_Ack = 0x7253FF87; -const uint32_t SockSig_GetConnInfo = 0x8E4F6B01; -const uint32_t SockSig_Size = 0x1F7E8BCF; + +int SockSig_Length = 4; + +const uint32_t OSSP_Sock_ACK = 0x7253FF87; +const uint32_t OSSP_Sock_CliConn = 0xE3566C2E; +const uint32_t OSSP_Sock_GetConnInfo = 0x8E4F6B01; +const uint32_t OSSP_Sock_Size = 0x1F7E8BCF; + +const uint32_t OSSP_Sock_ClientGetReq = 0x210829CF; + void socket_setup() { @@ -87,16 +89,21 @@ void socket_setup() { - //char buffer[256]; - //client_len = sizeof(struct sockaddr_un); - //read(client_fd, buffer, 256); - //printf("Received: %s\n", buffer); - //printf("Client len: %d\n", client_len); + printf("------------------------------\n"); - // Cleanup - //close(client_fd); - //close(server_fd); - //unlink(SOCKET_PATH); + + // Receive ClientGetReq with Size + int size = 0; + 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() { @@ -107,14 +114,10 @@ void socketHandler_cleanup() { 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 uint32_t util_byteArrToUint32BE(char buf[]) { @@ -124,89 +127,254 @@ uint32_t util_byteArrToUint32BE(char buf[]) { return retVal; } -void socketHandler_initClientConnection() { - // If reached here, a client has connected to the AF_UNIX socket +// Byte Array to uint32_t Little Endian +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 cliConnSize = 4; - char buf[4] = { 0x00 }; - rc = read(client_fd, buf, cliConnSize); - if (rc != cliConnSize) { - printf("Received data was not correct.\n"); +int socketHandler_initClientConnection() { + /* + * - Wait for client to send OSSP_Sock_CliConn + * - Send OSSP_Sock_ACK back + * - Wait for client to send OSSP_Sock_GetConnInfo + * - 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) { - printf("Received CliConn signature is not valid.\n"); - // TODO - } else { - printf("Client connected!\n"); + printf("[SocketHandler] Failed to receive OSSP_Sock_ACK (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_ACK, AckBE); + return 1; } - - // Send Server ACK - 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(); + printf("[SocketHandler] Received OSSP_Sock_ACK.\n"); + return 0; } -typedef struct { - uint32_t signature; - uint16_t size; -} __attribute__((packed)) OSSP_Sock_Size_Data; -void socketHandler_getConnInfo() { - // Receive OSSP_Sock_GetConnInfo - int sigSize = 4; + +int socketHandler_receiveCliConn() { char buf[4] = { 0x00 }; - rc = read(client_fd, buf, sigSize); - if (rc != sigSize) { - printf("OSSP_Sock_GetConnInfo Error 1\n"); - // TODO - } else { - printf("Signature verified.\n"); + rc = read(client_fd, buf, SockSig_Length); + if (rc != SockSig_Length) { + printf("[SocketHandler] Failed to receive OSSP_Sock_CliConn (Signature is the wrong length).\n"); + return 1; } - // Assemble JSON - char* jsonInfo = "{ 'ossp_version': '0.3a', 'server_addr': 'https://eumak.hojuix.org' }"; - int jsonLen = strlen(jsonInfo); - printf("JSON: %s\n", jsonInfo); - printf("JSON Length: %d\n", jsonLen); + //uint32_t CliConnBE = util_byteArrToUint32BE(buf); + uint32_t CliConnBE = util_byteArrToUint32LE(buf); - // Send OSSP_Sock_Size - OSSP_Sock_Size_Data ossp_sock; - ossp_sock.signature = SockSig_Size; - ossp_sock.size = jsonLen; - int sizeLen = 6; - rc = send(client_fd, (char*)&ossp_sock, sizeLen, 0); - if (rc != sizeLen) { - 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"); } + rc = memcmp(&CliConnBE, &OSSP_Sock_CliConn, SockSig_Length); + if (rc != 0) { + printf("[SocketHandler] Failed to receive OSSP_Sock_CliConn (Signature is invalid. Expected 0x%.8x, Received 0x%.8x).\n", OSSP_Sock_CliConn, CliConnBE); + return 1; + } + printf("[SocketHandler] Received OSSP_Sock_CliConn.\n"); + return 0; } -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 - -- } diff --git a/src/socket.h b/src/socket.h index f9c49e6..a7ee50d 100644 --- a/src/socket.h +++ b/src/socket.h @@ -1,6 +1,30 @@ #ifndef _SOCKET_H #define _SOCKET_H +#include + +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(); +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