1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 83 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 133 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/* $Id: matchmaker.c 3750 2009-04-14 12:43:36Z mark_ellis $ */
#define _BSD_SOURCE 1
#define _SVID_SOURCE 1
#include "matchmaker.h"
#include "rapi.h"
#include <synce_log.h>
#include <synce_ini.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static const char* PARTNERS =
"Software\\Microsoft\\Windows CE Services\\Partners";
static const char* CURRENT_PARTNER = "PCur";
static const char* PARTNER_ID = "PId";
static const char* PARTNER_NAME = "PName";
static const char* RRA_DIRECTORY = "rra";
static const char* PARTERSHIP_FILENAME = "partner";
static const char* PARTERSHIP_SECTION = "partnership";
struct _RRA_Matchmaker
{
HKEY keys[3];
};
#define KEY_PARTNERS 0
#define KEY_PARTNER_1 1
#define KEY_PARTNER_2 2
#define KEY_COUNT 3
RRA_Matchmaker* rra_matchmaker_new()
{
HKEY partnersKey;
if (rapi_reg_create_key(
HKEY_LOCAL_MACHINE,
PARTNERS,
&partnersKey))
{
RRA_Matchmaker* result = calloc(1, sizeof(RRA_Matchmaker));
if (result)
result->keys[KEY_PARTNERS] = partnersKey;
return result;
}
else
return NULL;
}
void rra_matchmaker_destroy(RRA_Matchmaker* matchmaker)
{
if (matchmaker)
{
int i;
for (i = 0; i < KEY_COUNT; i++)
if (matchmaker->keys[i])
CeRegCloseKey(matchmaker->keys[i]);
free(matchmaker);
}
}
static bool rra_matchmaker_create_key(RRA_Matchmaker* matchmaker, uint32_t index)
{
if (index == 1 || index == 2)
{
if (matchmaker->keys[index])
{
/* Already have this key */
return true;
}
else
{
char name[MAX_PATH];
snprintf(name, sizeof(name), "%s\\P%i", PARTNERS, index);
return rapi_reg_create_key(HKEY_LOCAL_MACHINE, name, &matchmaker->keys[index]);
}
}
else
return false;
}
static bool rra_matchmaker_open_key(RRA_Matchmaker* matchmaker, uint32_t index)
{
if (index == 1 || index == 2)
{
if (matchmaker->keys[index])
{
/* Already have this key */
return true;
}
else
{
char name[MAX_PATH];
snprintf(name, sizeof(name), "%s\\P%i", PARTNERS, index);
return rapi_reg_open_key(HKEY_LOCAL_MACHINE, name, &matchmaker->keys[index]);
}
}
else
return false;
}
bool rra_matchmaker_set_current_partner(RRA_Matchmaker* matchmaker, uint32_t index)
{
return
(index == 1 || index == 2) &&
rapi_reg_set_dword(matchmaker->keys[KEY_PARTNERS], CURRENT_PARTNER, index);
}
bool rra_matchmaker_get_current_partner(RRA_Matchmaker* matchmaker, uint32_t* index)
{
return
rapi_reg_query_dword(matchmaker->keys[KEY_PARTNERS], CURRENT_PARTNER, index);
}
static bool rra_matchmaker_set_partner_id(RRA_Matchmaker* matchmaker, uint32_t index, uint32_t id)
{
bool success =
rra_matchmaker_create_key(matchmaker, index) &&
rapi_reg_set_dword(matchmaker->keys[index], PARTNER_ID, id);
return success;
}
bool rra_matchmaker_get_partner_id(RRA_Matchmaker* matchmaker, uint32_t index, uint32_t* id)
{
bool success =
rra_matchmaker_open_key(matchmaker, index) &&
rapi_reg_query_dword(matchmaker->keys[index], PARTNER_ID, id);
return success;
}
static bool rra_matchmaker_set_partner_name(RRA_Matchmaker* matchmaker, uint32_t index, const char* name)
{
bool success =
rra_matchmaker_open_key(matchmaker, index) &&
rapi_reg_set_string(matchmaker->keys[index], PARTNER_NAME, name);
return success;
}
bool rra_matchmaker_get_partner_name(RRA_Matchmaker* matchmaker, uint32_t index, char** name)
{
bool success =
rra_matchmaker_open_key(matchmaker, index) &&
rapi_reg_query_string(matchmaker->keys[index], PARTNER_NAME, name);
return success;
}
static char* rra_matchmaker_get_filename(uint32_t id)
{
char* path = NULL;
char filename[256];
if (!synce_get_subdirectory(RRA_DIRECTORY, &path))
return NULL;
snprintf(filename, sizeof(filename), "%s/%s-%08x", path, PARTERSHIP_FILENAME, id);
free(path);
return strdup(filename);
}
bool rra_matchmaker_new_partnership(RRA_Matchmaker* matchmaker, uint32_t index)
{
bool success = false;
char hostname[256];
uint32_t other_id = 0;
uint32_t id = 0;
char* filename = NULL;
char* p;
if (index != 1 && index != 2)
{
synce_error("Invalid partnership index: %i", index);
goto exit;
}
if (!rra_matchmaker_get_partner_id(matchmaker, index, &id))
id = 0;
if (0 != id)
{
synce_error("Partnership exists, not overwriting at index: %i", index);
goto exit;
}
if (0 != gethostname(hostname, sizeof(hostname)))
{
synce_error("Failed to get hostname");
goto exit;
}
/* only use the part of the hostname before the first '.' char */
for (p = hostname; *p != '\0'; p++)
if ('.' == *p)
{
*p = '\0';
break;
}
if (!rra_matchmaker_get_partner_id(matchmaker, 3-index, &other_id))
other_id = 0;
srandom(time(NULL));
for (;;)
{
char* filename = NULL;
struct stat dummy;
/* better id generation? */
id = (uint32_t)random();
filename = rra_matchmaker_get_filename(id);
if (0 == stat(filename, &dummy))
id = 0; /* file exists, try another id */
free(filename);
if (0 == id || 0xffffffff == id || other_id == id)
continue;
break;
}
success =
rra_matchmaker_set_partner_id(matchmaker, index, id) &&
rra_matchmaker_set_partner_name(matchmaker, index, hostname);
if (success)
{
FILE* file = NULL;
if (!(filename = rra_matchmaker_get_filename(id)))
{
synce_error("Failed to get filename for partner id %08x", id);
goto exit;
}
if ((file = fopen(filename, "w")) == NULL)
{
synce_error("Failed to open file for writing: %s", filename);
goto exit;
}
fprintf(file,
"[device]\n"
"name=%s\n"
"\n"
"[%s]\n"
"%s=%i\n"
"%s=%i\n"
"%s=%s\n"
,
rapi_connection_get_name(NULL),
PARTERSHIP_SECTION,
CURRENT_PARTNER, index,
PARTNER_ID, id,
PARTNER_NAME, hostname);
fclose(file);
}
exit:
if (filename)
free(filename);
return success;
}
bool rra_matchmaker_clear_partnership(RRA_Matchmaker* matchmaker, uint32_t index)
{
bool success = false;
uint32_t id = 0;
char* filename = NULL;
if (index != 1 && index != 2)
{
synce_error("Bad index: %i", index);
goto exit;
}
if (!rra_matchmaker_get_partner_id(matchmaker, index, &id))
id = 0;
success =
rra_matchmaker_set_partner_id(matchmaker, index, 0) &&
rra_matchmaker_set_partner_name(matchmaker, index, "");
if (success)
{
if (!(filename = rra_matchmaker_get_filename(id)))
{
synce_error("Failed to get filename for partner id %08x", id);
goto exit;
}
if (remove(filename))
{
synce_error("Failed to erase file: %s", filename);
goto exit;
}
}
exit:
if (filename)
free(filename);
return success;
}
bool rra_matchmaker_have_partnership_at(RRA_Matchmaker* matchmaker, uint32_t index)
{
bool success = false;
uint32_t id;
SynceIni* ini = NULL;
if (!rra_matchmaker_get_partner_id(matchmaker, index, &id))
id = 0;
/* see if we have a partnership file */
if (id)
{
char* filename = rra_matchmaker_get_filename(id);
if (!filename)
{
synce_error("Failed to get filename for partner id %08x", id);
goto exit;
}
ini = synce_ini_new(filename);
free(filename);
if (ini)
{
const char* local_name = synce_ini_get_string(ini, PARTERSHIP_SECTION, PARTNER_NAME);
char* remote_name = NULL;
/* verify that the hostnames match */
if (local_name &&
rra_matchmaker_get_partner_name(matchmaker, index, &remote_name) &&
remote_name &&
0 == strcmp(local_name, remote_name))
{
free(remote_name);
success = true;
goto exit;
}
else
{
synce_trace("Local host name '%s' and remote host name '%s' do not match",
local_name, remote_name);
}
}
else
{
synce_trace("Partnership file not found for ID %08x", id);
}
}
else
{
synce_trace("Partnership slot %i is empty on device", index);
}
exit:
synce_ini_destroy(ini);
return success;
}
bool rra_matchmaker_have_partnership(RRA_Matchmaker* matchmaker, uint32_t* index)
{
bool success = false;
int i;
for (i = 0; i < 2; i++)
{
if (rra_matchmaker_have_partnership_at(matchmaker, i+1))
{
*index = i+1;
success = true;
goto exit;
}
}
exit:
return success;
}
bool rra_matchmaker_create_partnership(RRA_Matchmaker* matchmaker, uint32_t* index)
{
bool success = false;
int i;
uint32_t id;
if ((success = rra_matchmaker_have_partnership(matchmaker, index)))
goto exit;
/* If we get here, we have no partnership with the device.
We try to create a partnership on an empty slot */
for (i = 0; i < 2; i++)
{
if (!rra_matchmaker_get_partner_id(matchmaker, i+1, &id))
id = 0;
if (0 == id)
{
if (rra_matchmaker_new_partnership(matchmaker, i+1))
{
*index = i+1;
success = true;
goto exit;
}
}
}
synce_error("Partnership not found and there are no empty partner slots on device.");
exit:
if (success)
success = rra_matchmaker_set_current_partner(matchmaker, *index);
return success;
}
|