4
Fork 0

Added new return code to check connection timeout

This commit is contained in:
Artem Prilutskiy 2017-06-01 09:12:43 +03:00
parent 5caace3ebf
commit e12561b85f
2 changed files with 20 additions and 15 deletions

View file

@ -206,7 +206,7 @@ int ConnectRewindClient(struct RewindContext* context, const char* location, con
struct RewindData* buffer = (struct RewindData*)alloca(BUFFER_SIZE); struct RewindData* buffer = (struct RewindData*)alloca(BUFFER_SIZE);
ssize_t length; ssize_t length;
size_t attempt = CONNECTION_ATTEMPT_COUNT; size_t attempt = 0;
time_t threshold = time(NULL) + CONNECTION_CONNECT_TIMEOUT; time_t threshold = time(NULL) + CONNECTION_CONNECT_TIMEOUT;
uint8_t* digest = (uint8_t*)alloca(SHA256_DIGEST_LENGTH); uint8_t* digest = (uint8_t*)alloca(SHA256_DIGEST_LENGTH);
@ -240,7 +240,7 @@ int ConnectRewindClient(struct RewindContext* context, const char* location, con
// Do login procedure // Do login procedure
while ((attempt > 0) && (threshold > time(NULL))) while (threshold > time(NULL))
{ {
TransmitRewindData(context, REWIND_TYPE_KEEP_ALIVE, REWIND_FLAG_NONE, context->data, context->length); TransmitRewindData(context, REWIND_TYPE_KEEP_ALIVE, REWIND_FLAG_NONE, context->data, context->length);
length = ReceiveRewindData(context, buffer, BUFFER_SIZE); length = ReceiveRewindData(context, buffer, BUFFER_SIZE);
@ -254,31 +254,35 @@ int ConnectRewindClient(struct RewindContext* context, const char* location, con
if (length < 0) if (length < 0)
return length; return length;
switch (buffer->type) switch (le16toh(buffer->type))
{ {
case htole16(REWIND_TYPE_CHALLENGE): case REWIND_TYPE_CHALLENGE:
length -= sizeof(struct RewindData); if (attempt < CONNECTION_ATTEMPT_COUNT)
length += sprintf(buffer->data + length, "%s", password); {
SHA256(buffer->data, length, digest); length -= sizeof(struct RewindData);
length += sprintf(buffer->data + length, "%s", password);
SHA256(buffer->data, length, digest);
TransmitRewindData(context, REWIND_TYPE_AUTHENTICATION, REWIND_FLAG_NONE, digest, SHA256_DIGEST_LENGTH); TransmitRewindData(context, REWIND_TYPE_AUTHENTICATION, REWIND_FLAG_NONE, digest, SHA256_DIGEST_LENGTH);
break;
case htole16(REWIND_TYPE_KEEP_ALIVE): attempt ++;
continue;
}
return CLIENT_ERROR_ATTEMPT_EXCEEDED;
case REWIND_TYPE_KEEP_ALIVE:
if (options != 0) if (options != 0)
{ {
data.options = htole32(options); data.options = htole32(options);
TransmitRewindData(context, REWIND_TYPE_CONFIGURATION, REWIND_FLAG_NONE, &data, sizeof(struct RewindConfigurationData)); TransmitRewindData(context, REWIND_TYPE_CONFIGURATION, REWIND_FLAG_NONE, &data, sizeof(struct RewindConfigurationData));
break; continue;
} }
case htole16(REWIND_TYPE_CONFIGURATION): case REWIND_TYPE_CONFIGURATION:
return CLIENT_ERROR_SUCCESS; return CLIENT_ERROR_SUCCESS;
} }
attempt --;
} }
return CLIENT_ERROR_ATTEMPT_EXCEEDED; return CLIENT_ERROR_RESPONSE_TIMEOUT;
} }

View file

@ -24,6 +24,7 @@ extern "C"
#define CLIENT_ERROR_WRONG_DATA -3 #define CLIENT_ERROR_WRONG_DATA -3
#define CLIENT_ERROR_DNS_RESOLVE -4 #define CLIENT_ERROR_DNS_RESOLVE -4
#define CLIENT_ERROR_ATTEMPT_EXCEEDED -5 #define CLIENT_ERROR_ATTEMPT_EXCEEDED -5
#define CLIENT_ERROR_RESPONSE_TIMEOUT -6
struct RewindContext struct RewindContext
{ {