RewindClient.c 7.28 KB
Newer Older
Artem Prilutskiy's avatar
Artem Prilutskiy committed
1
2
3
4
#include "RewindClient.h"

#include <unistd.h>
#include <string.h>
Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
5
6
#include <errno.h>
#include <time.h>
Artem Prilutskiy's avatar
Artem Prilutskiy committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <stdlib.h>
#include <stdio.h>

#include <sys/socket.h>
#include <sys/utsname.h>

#ifdef USE_OPENSSL
#include <openssl/sha.h>
#endif

#ifndef HEADER_SHA_H
#include "sha256.h"
#define SHA256_DIGEST_LENGTH  SHA256_BLOCK_SIZE
#define SHA256(data, length, hash) \
  { \
    SHA256_CTX context; \
    sha256_init(&context); \
    sha256_update(&context, data, length); \
    sha256_final(&context, hash); \
  }
#endif

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifdef __linux__
#include <endian.h>
#include <byteswap.h>
#endif

#ifdef __MACH__
#include <mach/mach.h>
#include <machine/endian.h>

#define htobe16(value)  OSSwapHostToBigInt16(value)
#define be16toh(value)  OSSwapBigToHostInt16(value)
#define htobe32(value)  OSSwapHostToBigInt32(value)
#define be32toh(value)  OSSwapBigToHostInt32(value)

#define htole16(value)  OSSwapHostToLittleInt16(value)
#define le16toh(value)  OSSwapLittleToHostInt16(value)
#define htole32(value)  OSSwapHostToLittleInt32(value)
#define le32toh(value)  OSSwapLittleToHostInt32(value)

#define __bswap_16(value)  OSSwapConstInt16(value)
#define __bswap_32(value)  OSSwapConstInt32(value)
#endif

Artem Prilutskiy's avatar
Artem Prilutskiy committed
53
#define BUFFER_SIZE    256
Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
54
55
56
57

#define CONNECTION_ATTEMPT_COUNT    5
#define CONNECTION_RECEIVE_TIMEOUT  2
#define CONNECTION_CONNECT_TIMEOUT  5
Artem Prilutskiy's avatar
Artem Prilutskiy committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

static int CompareAddresses(struct sockaddr* value1, struct sockaddr_in6* value2)
{
  struct sockaddr_in* value;

  if ((value1->sa_family == AF_INET) &&
      (value2->sin6_family == AF_INET6) &&
      (IN6_IS_ADDR_V4MAPPED(&value2->sin6_addr)))
  {
    value = (struct sockaddr_in*)value1;
    return
      (value->sin_addr.s_addr - value2->sin6_addr.__in6_u.__u6_addr32[3]) |
      (value->sin_port        - value2->sin6_port);
  }

  if ((value1->sa_family == AF_INET6) &&
      (value2->sin6_family == AF_INET6))
  {
    // Compare full socket address
    return memcmp(value1, value2, sizeof(struct sockaddr_in6));
  }

  return -1;
}

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
83
struct RewindContext* CreateRewindContext(uint32_t number, const char* verion)
Artem Prilutskiy's avatar
Artem Prilutskiy committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
  struct utsname name;
  struct timeval interval;
  struct sockaddr_in6 address;
  struct RewindContext* context = (struct RewindContext*)calloc(1, sizeof(struct RewindContext));

  if (context != NULL)
  {
    // Create socket

    address.sin6_family = AF_INET6;
    address.sin6_addr = in6addr_any;
    address.sin6_port = 0;
    address.sin6_scope_id = 0;

    interval.tv_sec  = 2;
    interval.tv_usec = 0;

    context->handle = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    if ((context->handle < 0) ||
        (bind(context->handle, (struct sockaddr*)&address, sizeof(struct sockaddr_in6)) < 0) ||
        (setsockopt(context->handle, SOL_SOCKET, SO_RCVTIMEO, &interval, sizeof(struct timeval)) < 0))
    {
      close(context->handle);
      free(context);
      return NULL;
    }

    // Create supplimentary data

    uname(&name);

    context->data = (struct RewindVersionData*)malloc(BUFFER_SIZE);

    context->length  = sizeof(struct RewindVersionData);
    context->length += sprintf(
      context->data->description,
      "%s %s %s",
      verion,
      name.sysname,
      name.machine);

    context->data->number = htole32(number);
    context->data->service = REWIND_SERVICE_SIMPLE_APPLICATION;
  }

  return context;
}

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
133
void ReleaseRewindContext(struct RewindContext* context)
Artem Prilutskiy's avatar
Artem Prilutskiy committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
  if (context != NULL)
  {
    freeaddrinfo(context->address);
    close(context->handle);
    free(context->data);
    free(context);
  }
}

void TransmitRewindData(struct RewindContext* context, uint16_t type, uint16_t flag, void* data, size_t length)
{
  struct msghdr message;
  struct iovec vectors[2];

  size_t index;
  uint32_t number;

  struct RewindData header;

  memset(&header, 0, sizeof(struct RewindData));
  memcpy(&header, REWIND_PROTOCOL_SIGN, REWIND_SIGN_LENGTH);
 
  index = flag & REWIND_FLAG_REAL_TIME_1;
  number = context->counters[index];

  header.type   = htole16(type);
  header.flags  = htole16(flag);
  header.number = htole32(number);
  header.length = htole16(length);

  vectors[0].iov_base    = &header;
  vectors[0].iov_len     = sizeof(struct RewindData);
  vectors[1].iov_base    = data;
  vectors[1].iov_len     = length;
  message.msg_name       = context->address->ai_addr;
  message.msg_namelen    = context->address->ai_addrlen;
  message.msg_iov        = vectors;
  message.msg_iovlen     = 2;
  message.msg_control    = NULL;
  message.msg_controllen = 0;
  message.msg_flags      = 0;

  sendmsg(context->handle, &message, 0);

  context->counters[index] ++;
}

ssize_t ReceiveRewindData(struct RewindContext* context, struct RewindData* buffer, ssize_t length)
{
  struct sockaddr_in6 address;
  socklen_t size = sizeof(struct sockaddr_in6);

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
187
  length = recvfrom(context->handle, buffer, length, 0, (struct sockaddr*)&address, &size);
Artem Prilutskiy's avatar
Artem Prilutskiy committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

  if (length < 0)
    return CLIENT_ERROR_SOCKET_IO;

  if (CompareAddresses(context->address->ai_addr, &address) != 0)
    return CLIENT_ERROR_WRONG_ADDRESS;

  if ((length < sizeof(struct RewindData)) ||
      (memcmp(buffer, REWIND_PROTOCOL_SIGN, REWIND_SIGN_LENGTH) != 0))
    return CLIENT_ERROR_WRONG_DATA;

  return length;
}

int ConnectRewindClient(struct RewindContext* context, const char* location, const char* port, const char* password, uint32_t options)
{
  struct addrinfo hints;

  struct RewindData* buffer = (struct RewindData*)alloca(BUFFER_SIZE);
  ssize_t length;

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
209
210
211
  size_t attempt = CONNECTION_ATTEMPT_COUNT;
  time_t threshold = time(NULL) + CONNECTION_CONNECT_TIMEOUT;

Artem Prilutskiy's avatar
Artem Prilutskiy committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  uint8_t* digest = (uint8_t*)alloca(SHA256_DIGEST_LENGTH);

  struct RewindConfigurationData data;

  // Resolve server IP address

  if (context->address != NULL)
  {
    freeaddrinfo(context->address);
    context->address = NULL;
  }

  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_DGRAM;
Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
226
#ifdef __linux__
Artem Prilutskiy's avatar
Artem Prilutskiy committed
227
228
  hints.ai_flags = AI_ADDRCONFIG;
  hints.ai_family = AF_UNSPEC;
Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
229
230
231
232
233
#endif
#ifdef __MACH__
  hints.ai_flags = AI_V4MAPPED;
  hints.ai_family = AF_INET6;
#endif
Artem Prilutskiy's avatar
Artem Prilutskiy committed
234
235
236
237
238
239
240
241
242

  if (getaddrinfo(location, port, &hints, &context->address) != 0)
  {
    // Could not resolve address
    return CLIENT_ERROR_DNS_RESOLVE;
  }

  // Do login procedure

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
243
  while ((attempt > 0) && (threshold > time(NULL)))
Artem Prilutskiy's avatar
Artem Prilutskiy committed
244
245
246
247
  {
    TransmitRewindData(context, REWIND_TYPE_KEEP_ALIVE, REWIND_FLAG_NONE, context->data, context->length);
    length = ReceiveRewindData(context, buffer, BUFFER_SIZE);

Artem Prilutskiy's avatar
..    
Artem Prilutskiy committed
248
249
250
251
252
253
254
    if ((length == CLIENT_ERROR_WRONG_ADDRESS) ||
        (length == CLIENT_ERROR_SOCKET_IO) &&
        ((errno == EWOULDBLOCK) ||
         (errno == EAGAIN)))
      continue;

    if (length < 0)
Artem Prilutskiy's avatar
Artem Prilutskiy committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
      return length;

    switch (buffer->type)
    {
      case htole16(REWIND_TYPE_CHALLENGE):
        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);
        break;

      case htole16(REWIND_TYPE_KEEP_ALIVE):
        if (options != 0)
        {
          data.options = htole32(options);

          TransmitRewindData(context, REWIND_TYPE_CONFIGURATION, REWIND_FLAG_NONE, &data, sizeof(struct RewindConfigurationData));
          break;
        }

      case htole16(REWIND_TYPE_CONFIGURATION):
        return CLIENT_ERROR_SUCCESS;
    }

    attempt --;
  }

  return CLIENT_ERROR_ATTEMPT_EXCEEDED;
}