From 34084f179197b52c4eeaad7db62cbabe04d99ab7 Mon Sep 17 00:00:00 2001 From: Artem Prilutskiy Date: Tue, 23 May 2017 14:19:04 +0300 Subject: [PATCH] .. --- DigestPlay.c | 42 ++++++++++++------------------------------ RewindClient.c | 34 +++++++++++++++++++++++++--------- RewindClient.h | 7 +++++-- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/DigestPlay.c b/DigestPlay.c index f4e5907..53bf446 100644 --- a/DigestPlay.c +++ b/DigestPlay.c @@ -28,25 +28,6 @@ #define CLIENT_NAME "DigestPlay " STRING(VERSION) " " BUILD -typedef uint8_t Integer24[3]; - -struct FullLC -{ - uint8_t code; - uint8_t feature; - uint8_t options; - Integer24 destination; - Integer24 source; - Integer24 sum; -}; - -void EncodeInteger24(uint32_t value, Integer24 data) -{ - data[0] = (value >> 16) & 0xff; - data[1] = (value >> 8) & 0xff; - data[2] = value & 0xff; -} - int main(int argc, char* argv[]) { printf("\n"); @@ -62,8 +43,8 @@ int main(int argc, char* argv[]) const char* location = NULL; const char* password = NULL; - struct FullLC header; - memset(&header, 0, sizeof(struct FullLC)); + struct RewindSuperHeader header; + memset(&header, 0, sizeof(struct RewindSuperHeader)); // Start up @@ -108,7 +89,7 @@ int main(int argc, char* argv[]) value = strtol(optarg, NULL, 10); if (value > 0) { - EncodeInteger24(value, header.source); + header.sourceID = htole32(value); control |= 0b01000; } break; @@ -117,7 +98,7 @@ int main(int argc, char* argv[]) value = strtol(optarg, NULL, 10); if (value > 0) { - EncodeInteger24(value, header.destination); + header.destinationID = htole32(value); control |= 0b10000; } break; @@ -141,7 +122,7 @@ int main(int argc, char* argv[]) // Create Rewind client context - struct RewindContext* context = CreateRewindClient(number, CLIENT_NAME); + struct RewindContext* context = CreateRewindContext(number, CLIENT_NAME); if (context == NULL) { @@ -158,7 +139,7 @@ int main(int argc, char* argv[]) (memcmp(buffer, DSD_MAGIC_TEXT, length) != 0)) { printf("Error checking input data format\n"); - ReleaseRewindClient(context); + ReleaseRewindContext(context); return EXIT_FAILURE; } @@ -169,7 +150,7 @@ int main(int argc, char* argv[]) if (result < 0) { printf("Cannot connect to the server (%i)\n", result); - ReleaseRewindClient(context); + ReleaseRewindContext(context); return EXIT_FAILURE; } @@ -190,9 +171,10 @@ int main(int argc, char* argv[]) printf("Playing...\n"); - TransmitRewindData(context, REWIND_TYPE_DMR_DATA_BASE + 1, REWIND_FLAG_REAL_TIME_1, &header, sizeof(struct FullLC)); - TransmitRewindData(context, REWIND_TYPE_DMR_DATA_BASE + 1, REWIND_FLAG_REAL_TIME_1, &header, sizeof(struct FullLC)); - TransmitRewindData(context, REWIND_TYPE_DMR_DATA_BASE + 1, REWIND_FLAG_REAL_TIME_1, &header, sizeof(struct FullLC)); + header.type = htole32(SESSION_TYPE_GROUP_VOICE); + TransmitRewindData(context, REWIND_TYPE_SUPER_HEADER, REWIND_FLAG_REAL_TIME_1, &header, sizeof(struct RewindSuperHeader)); + TransmitRewindData(context, REWIND_TYPE_SUPER_HEADER, REWIND_FLAG_REAL_TIME_1, &header, sizeof(struct RewindSuperHeader)); + TransmitRewindData(context, REWIND_TYPE_SUPER_HEADER, REWIND_FLAG_REAL_TIME_1, &header, sizeof(struct RewindSuperHeader)); // Main loop @@ -234,7 +216,7 @@ int main(int argc, char* argv[]) printf("Done\n"); TransmitRewindCloae(context); - ReleaseRewindClient(context); + ReleaseRewindContext(context); return EXIT_SUCCESS; }; diff --git a/RewindClient.c b/RewindClient.c index ff1ffee..e00bec7 100644 --- a/RewindClient.c +++ b/RewindClient.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include @@ -27,7 +29,10 @@ #endif #define BUFFER_SIZE 256 -#define ATTEMPT_COUNT 5 + +#define CONNECTION_ATTEMPT_COUNT 5 +#define CONNECTION_RECEIVE_TIMEOUT 2 +#define CONNECTION_CONNECT_TIMEOUT 5 static int CompareAddresses(struct sockaddr* value1, struct sockaddr_in6* value2) { @@ -53,7 +58,7 @@ static int CompareAddresses(struct sockaddr* value1, struct sockaddr_in6* value2 return -1; } -struct RewindContext* CreateRewindClient(uint32_t number, const char* verion) +struct RewindContext* CreateRewindContext(uint32_t number, const char* verion) { struct utsname name; struct timeval interval; @@ -103,7 +108,7 @@ struct RewindContext* CreateRewindClient(uint32_t number, const char* verion) return context; } -void ReleaseRewindClient(struct RewindContext* context) +void ReleaseRewindContext(struct RewindContext* context) { if (context != NULL) { @@ -179,7 +184,9 @@ int ConnectRewindClient(struct RewindContext* context, const char* location, con struct RewindData* buffer = (struct RewindData*)alloca(BUFFER_SIZE); ssize_t length; - size_t attempt = ATTEMPT_COUNT; + size_t attempt = CONNECTION_ATTEMPT_COUNT; + time_t threshold = time(NULL) + CONNECTION_CONNECT_TIMEOUT; + uint8_t* digest = (uint8_t*)alloca(SHA256_DIGEST_LENGTH); struct RewindConfigurationData data; @@ -194,8 +201,14 @@ int ConnectRewindClient(struct RewindContext* context, const char* location, con memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; +#ifdef __linux__ hints.ai_flags = AI_ADDRCONFIG; hints.ai_family = AF_UNSPEC; +#endif +#ifdef __MACH__ + hints.ai_flags = AI_V4MAPPED; + hints.ai_family = AF_INET6; +#endif if (getaddrinfo(location, port, &hints, &context->address) != 0) { @@ -205,16 +218,19 @@ int ConnectRewindClient(struct RewindContext* context, const char* location, con // Do login procedure - while (attempt > 0) + while ((attempt > 0) && (threshold > time(NULL))) { TransmitRewindData(context, REWIND_TYPE_KEEP_ALIVE, REWIND_FLAG_NONE, context->data, context->length); length = ReceiveRewindData(context, buffer, BUFFER_SIZE); - if (length < CLIENT_ERROR_SOCKET_IO) - { - // Possible we got socket timeout + if ((length == CLIENT_ERROR_WRONG_ADDRESS) || + (length == CLIENT_ERROR_SOCKET_IO) && + ((errno == EWOULDBLOCK) || + (errno == EAGAIN))) + continue; + + if (length < 0) return length; - } switch (buffer->type) { diff --git a/RewindClient.h b/RewindClient.h index df941cd..0b84531 100644 --- a/RewindClient.h +++ b/RewindClient.h @@ -15,6 +15,9 @@ extern "C" { #endif +#define SESSION_TYPE_PRIVATE_VOICE 5 +#define SESSION_TYPE_GROUP_VOICE 7 + #define CLIENT_ERROR_SUCCESS 0 #define CLIENT_ERROR_SOCKET_IO -1 #define CLIENT_ERROR_WRONG_ADDRESS -2 @@ -33,8 +36,8 @@ struct RewindContext size_t length; }; -struct RewindContext* CreateRewindClient(uint32_t number, const char* verion); -void ReleaseRewindClient(struct RewindContext* context); +struct RewindContext* CreateRewindContext(uint32_t number, const char* verion); +void ReleaseRewindContext(struct RewindContext* context); void TransmitRewindData(struct RewindContext* context, uint16_t type, uint16_t flag, void* data, size_t length); ssize_t ReceiveRewindData(struct RewindContext* context, struct RewindData* buffer, ssize_t length);