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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
|
/* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mobile Application Link.
*
* The Initial Developer of the Original Code is AvantGo, Inc.
* Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
* AvantGo, Inc. All Rights Reserved.
*
* Contributor(s):
*/
// Owner: linus
#include <AGProtocol.h>
#include <AGBufferWriter.h>
#include <AGDigest.h>
#include <AGUtil.h>
#if !defined(__palmos__) && !defined(_WIN32_WCE)
//#define DEBUG_PROTOCOL
#endif
// We do this a lot.
#define ADD_STRING_LEN(str, strLen) { \
if (str != NULL) { \
strLen = strlen(str); \
} \
len += AGCompactSize(strLen); \
len += strLen; \
}
//PENDING(klobad) convert all the simple commands to MACROS to this guy??
ExportFunc void AGWriteCommand(AGWriter *w, int32 command,
int32 commandDataLen,
void *commandData)
{
AGWriteCompactInt(w, command);
AGWriteCompactInt(w, commandDataLen);
if(commandDataLen > 0) {
AGWriteBytes(w, commandData, commandDataLen);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteCommand(%u, %u) - %s\n", command, commandDataLen, AGProtocolCommandName(command));
#endif
}
ExportFunc void AGWriteMAGIC(AGWriter *w)
{
AGWriteInt8(w, AG_PROTOCOL_MAGIC_HIGH);
AGWriteInt8(w, AG_PROTOCOL_MAGIC_LOW);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteMAGIC(%u)\n", (AG_PROTOCOL_MAGIC_HIGH << 8) | AG_PROTOCOL_MAGIC_LOW);
#endif
}
ExportFunc void AGWriteMAJORVERSION(AGWriter *w, int8 major)
{
AGWriteInt8(w, major);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteMAJORVERSION(%d)\n", major);
#endif
}
ExportFunc void AGWriteMINORVERSION(AGWriter *w, int8 minor)
{
AGWriteInt8(w, minor);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteMINORVERSION(%d)\n", minor);
#endif
}
ExportFunc void AGWriteEND(AGWriter *w)
{
AGWriteCompactInt(w, AG_END_CMD);
AGWriteCompactInt(w, 0);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteEND\n");
#endif
}
ExportFunc void AGReadMAGIC(AGReader *r, uint16 *magic)
{
int32 high, low;
high = AGReadInt8(r);
low = AGReadInt8(r);
*magic = (high << 8) | low;
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadMAGIC(%u)\n", *magic);
#endif
}
ExportFunc void AGReadMAJORVERSION(AGReader *r, int8 *major)
{
*major = AGReadInt8(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadMAJORVERSION(%d)\n", *major);
#endif
}
ExportFunc void AGReadMINORVERSION(AGReader *r, int8 *minor)
{
*minor = AGReadInt8(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadMINORVERSION(%d)\n", *minor);
#endif
}
ExportFunc void AGReadEND(AGReader *r)
{
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadEND\n");
#endif
}
ExportFunc void AGWritePING(AGWriter *w)
{
AGWriteCompactInt(w, AG_PING_CMD);
AGWriteCompactInt(w, 0);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWritePING\n");
#endif
}
ExportFunc void AGReadPING(AGReader *r)
{
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadPING\n");
#endif
}
ExportFunc void AGWriteXMLDATA(AGWriter *w,
int32 dataLen,
void *dataBytes)
{
int32 len = 0;
len += AGCompactSize(dataLen);
len += dataLen;
AGWriteCompactInt(w, AG_XMLDATA_CMD);
AGWriteCompactInt(w, len);
AGWriteCompactInt(w, dataLen);
AGWriteBytes(w, dataBytes, dataLen);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteXMLDATA(%d)\n", len);
#endif
}
ExportFunc void AGReadXMLDATA(AGReader *r,
int32 *dataLen,
void **dataBytes)
{
*dataLen = AGReadCompactInt(r);
if (*dataLen < 0) {
return;
} else if (dataLen == 0) {
*dataBytes = NULL;
} else {
*dataBytes = malloc(*dataLen);
AGReadBytes(r, *dataBytes, *dataLen);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadXMLDATA(%d)\n", *dataLen);
#endif
}
ExportFunc void AGWriteHELLO(AGWriter *w, char *username, uint8 digestAuth[16],
uint8 nonce[16], int32 availableBytes, int32 cookieLength,
void *cookie)
{
int32 len = 0, usernameLen = 0;
ADD_STRING_LEN(username, usernameLen);
if (!AGDigestNull(digestAuth)) {
len += AGCompactSize(16);
len += 16; /* Add length of digestAuth */
} else
len += AGCompactSize(0);
if (!AGDigestNull(nonce)) {
len += AGCompactSize(16);
len += 16; /* Add length of nonce */
} else
len += AGCompactSize(0);
len += AGCompactSize(availableBytes);
len += AGCompactSize(cookieLength);
len += cookieLength;
// Write the command.
AGWriteCompactInt(w, AG_HELLO_CMD);
AGWriteCompactInt(w, len);
/* Write username */
AGWriteString(w, username, usernameLen);
/* Write digestAuth, 0 if nonce is unknown or invalid */
if (AGDigestNull(digestAuth))
AGWriteCompactInt(w, 0);
else {
AGWriteCompactInt(w, 16);
AGWriteBytes(w, digestAuth, 16);
}
/* Write nonce, 0 if nonce is unknown or invalid */
if (AGDigestNull(nonce))
AGWriteCompactInt(w, 0);
else {
AGWriteCompactInt(w, 16);
AGWriteBytes(w, nonce, 16);
}
AGWriteCompactInt(w, availableBytes);
AGWriteCompactInt(w, cookieLength);
AGWriteBytes(w, cookie, cookieLength);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteHELLO\n");
#endif
}
ExportFunc void AGReadHELLO(AGReader *r, char **username, uint8 digestAuth[16],
uint8 nonce[16], int32 *availableBytes, int32 *cookieLength,
void **cookie)
{
int32 digestLen, nonceLen; /* we know the digest is 16 bytes, but if not supplied
we will skip it */
*username = AGReadString(r);
digestLen = AGReadCompactInt(r);
if (!digestLen)
memset(digestAuth, 0, 16); /* this will tell server the client has no nonce */
else
AGReadBytes(r, digestAuth, 16);
nonceLen = AGReadCompactInt(r);
if (!nonceLen)
memset(nonce, 0, 16); /* this will tell server the client has no nonce */
else
AGReadBytes(r, nonce, 16);
*availableBytes = AGReadCompactInt(r);
*cookieLength = AGReadCompactInt(r);
if (*cookieLength < 0) {
return;
} else if (*cookieLength == 0) {
*cookie = NULL;
} else {
*cookie = malloc(*cookieLength);
AGReadBytes(r, *cookie, *cookieLength);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadHELLO cookieLen=%d\n", *cookieLength);
#endif
}
ExportFunc void AGWriteHELLO2(AGWriter *w, char *username, uint8 digestAuth[16],
uint8 nonce[16], int32 availableBytes, int32 cookieLength,
void *cookie, uint32 serveruid)
{
//PENDING(klobad) this is annoying to not be able to reuse the
//AGWriteHELLO() code because the length is enbedded in there
int32 len = 0, usernameLen = 0;
ADD_STRING_LEN(username, usernameLen);
if (!AGDigestNull(digestAuth)) {
len += AGCompactSize(16);
len += 16; /* Add length of digestAuth */
} else
len += AGCompactSize(0);
if (!AGDigestNull(nonce)) {
len += AGCompactSize(16);
len += 16; /* Add length of nonce */
} else
len += AGCompactSize(0);
len += AGCompactSize(availableBytes);
len += AGCompactSize(cookieLength);
len += cookieLength;
len += AGCompactSize(serveruid);
// Write the command.
AGWriteCompactInt(w, AG_HELLO_CMD);
AGWriteCompactInt(w, len);
/* Write username */
AGWriteString(w, username, usernameLen);
/* Write digestAuth, 0 if nonce is unknown or invalid */
if (AGDigestNull(digestAuth))
AGWriteCompactInt(w, 0);
else {
AGWriteCompactInt(w, 16);
AGWriteBytes(w, digestAuth, 16);
}
/* Write nonce, 0 if nonce is unknown or invalid */
if (AGDigestNull(nonce))
AGWriteCompactInt(w, 0);
else {
AGWriteCompactInt(w, 16);
AGWriteBytes(w, nonce, 16);
}
AGWriteCompactInt(w, availableBytes);
AGWriteCompactInt(w, cookieLength);
AGWriteBytes(w, cookie, cookieLength);
AGWriteCompactInt(w, serveruid);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteHELLO2\n");
#endif
}
ExportFunc void AGReadHELLO2(AGReader *r, char **username, uint8 digestAuth[16],
uint8 nonce[16], int32 *availableBytes, int32 *cookieLength,
void **cookie, uint32 *serveruid)
{
AGReadHELLO(r, username, digestAuth, nonce, availableBytes, cookieLength,
cookie);
*serveruid = AGReadCompactInt(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadHELLO2(%d)\n", *serveruid);
#endif
}
ExportFunc void AGWriteDEVICEINFO(AGWriter *w,
char *osName, char *osVersion,
int32 colorDepth, int32 screenWidth, int32 screenHeight,
char *serialNumber, char *language, char *charset,
int32 platformDataLength, void *platformData)
{
int32 len = 0, osNameLen = 0, osVersionLen = 0, serialNumberLen = 0,
languageLen = 0, charsetLen = 0;
// compute len
ADD_STRING_LEN(osName, osNameLen);
ADD_STRING_LEN(osVersion, osVersionLen);
len += AGCompactSize(colorDepth);
len += AGCompactSize(screenWidth);
len += AGCompactSize(screenHeight);
ADD_STRING_LEN(serialNumber, serialNumberLen);
ADD_STRING_LEN(language, languageLen);
ADD_STRING_LEN(charset, charsetLen);
len += AGCompactSize(platformDataLength);
len += platformDataLength;
// write the command
AGWriteCompactInt(w, AG_DEVICEINFO_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, osName, osNameLen);
AGWriteString(w, osVersion, osVersionLen);
AGWriteCompactInt(w, colorDepth);
AGWriteCompactInt(w, screenWidth);
AGWriteCompactInt(w, screenHeight);
AGWriteString(w, serialNumber, serialNumberLen);
AGWriteString(w, language, languageLen);
AGWriteString(w, charset, charsetLen);
AGWriteCompactInt(w, platformDataLength);
AGWriteBytes(w, platformData, platformDataLength);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteDEVICEINFO\n");
#endif
}
ExportFunc void AGReadDEVICEINFO(AGReader *r,
char **osName, char **osVersion,
int32 *colorDepth, int32 *screenWidth, int32 *screenHeight,
char **serialNumber, char **language, char **charset,
int32 *platformDataLength, void **platformData)
{
*osName = AGReadString(r);
*osVersion = AGReadString(r);
*colorDepth = AGReadCompactInt(r);
*screenWidth = AGReadCompactInt(r);
*screenHeight = AGReadCompactInt(r);
*serialNumber = AGReadString(r);
*language = AGReadString(r);
*charset = AGReadString(r);
*platformDataLength = AGReadCompactInt(r);
if (*platformDataLength < 0) {
return;
} else if (*platformDataLength == 0) {
*platformData = NULL;
} else {
*platformData = malloc(*platformDataLength);
AGReadBytes(r, *platformData, *platformDataLength);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadDEVICEINFO(%s, %s, %d, %d, %d, %s, %s, %s, %d)\n",
*osName, *osVersion, *colorDepth, *screenWidth, *screenHeight,
*serialNumber, *language, *charset, *platformDataLength);
#endif
}
ExportFunc void AGWriteSENDDEVICEINFO(AGWriter *w, AGBool send)
{
AGWriteCompactInt(w, AG_SENDDEVICEINFO_CMD);
AGWriteCompactInt(w, 1);
AGWriteBoolean(w, send);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteSENDDEVICEINFO\n");
#endif
}
ExportFunc void AGReadSENDDEVICEINFO(AGReader *r, AGBool *send)
{
*send = AGReadBoolean(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadSENDDEVICEINFO\n");
#endif
}
ExportFunc void AGWriteDATABASECONFIG(AGWriter *w, char *dbname,
AGDBConfigType config, AGBool sendRecordPlatformData,
int32 platformDataLength, void *platformData)
{
int32 len = 0, dbnameLen = 0;
ADD_STRING_LEN(dbname, dbnameLen);
len += AGCompactSize(config);
len += 1; // 1 boolean - sendRecordPlatformData
len += AGCompactSize(platformDataLength);
len += platformDataLength;
// write the command
AGWriteCompactInt(w, AG_DATABASECONFIG_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, dbname, dbnameLen);
AGWriteCompactInt(w, config);
AGWriteBoolean(w, sendRecordPlatformData);
AGWriteCompactInt(w, platformDataLength);
AGWriteBytes(w, platformData, platformDataLength);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteDATABASECONFIG\n");
#endif
}
ExportFunc void AGReadDATABASECONFIG(AGReader *r, char **dbname,
AGDBConfigType *config, AGBool *sendRecordPlatformData,
int32 *platformDataLength, void **platformData)
{
*dbname = AGReadString(r);
*config = (AGDBConfigType)AGReadCompactInt(r);
*sendRecordPlatformData = AGReadBoolean(r);
*platformDataLength = AGReadCompactInt(r);
if (*platformDataLength < 0) {
return;
} else if (*platformDataLength == 0) {
*platformData = NULL;
} else {
*platformData = malloc(*platformDataLength);
AGReadBytes(r, *platformData, *platformDataLength);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadDATABASECONFIG\n");
#endif
}
ExportFunc void AGWriteSERVERCONFIG(AGWriter *w, char *friendlyName,
char *userUrl, char *message,
char *serverUri,
AGBool clientShouldHashPasswords,
AGBool allowSecureClientConnect,
uint32 connectTimeoutSeconds,
uint32 writeTimeoutSeconds,
uint32 readTimeoutSeconds)
{
int32 len = 0, friendlyNameLen = 0, userUrlLen = 0,
messageLen = 0, serverUriLen = 0;
int8 flags = 0;
ADD_STRING_LEN(friendlyName, friendlyNameLen);
ADD_STRING_LEN(userUrl, userUrlLen);
ADD_STRING_LEN(message, messageLen);
ADD_STRING_LEN(serverUri, serverUriLen);
len += sizeof(int8); /* flags */
len += AGCompactSize(connectTimeoutSeconds);
len += AGCompactSize(writeTimeoutSeconds);
len += AGCompactSize(readTimeoutSeconds);
AGWriteCompactInt(w, AG_SERVERCONFIG_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, friendlyName, friendlyNameLen);
AGWriteString(w, userUrl, userUrlLen);
AGWriteString(w, message, messageLen);
AGWriteString(w, serverUri, serverUriLen);
if (clientShouldHashPasswords)
BIS(flags, AG_CLIENT_HASH_PASSWORD);
if (allowSecureClientConnect)
BIS(flags, AG_ALLOW_SECURE_CLIENT_CONNECT);
AGWriteInt8(w, flags);
AGWriteCompactInt(w, connectTimeoutSeconds);
AGWriteCompactInt(w, writeTimeoutSeconds);
AGWriteCompactInt(w, readTimeoutSeconds);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteSERVERCONFIG\n");
#endif
}
ExportFunc void AGReadSERVERCONFIG(AGReader *r, char **friendlyName,
char **userUrl, char **message,
char **serverUri,
AGBool *clientShouldHashPasswords,
AGBool *allowSecureClientConnect,
uint32 *connectTimeoutSeconds,
uint32 *writeTimeoutSeconds,
uint32 *readTimeoutSeconds)
{
int8 flags;
*friendlyName = AGReadString(r);
*userUrl = AGReadString(r);
*message = AGReadString(r);
*serverUri = AGReadString(r);
flags = AGReadInt8(r);
if (BIT(flags, AG_CLIENT_HASH_PASSWORD))
*clientShouldHashPasswords = 1;
else
*clientShouldHashPasswords = 0;
if (BIT(flags, AG_ALLOW_SECURE_CLIENT_CONNECT))
*allowSecureClientConnect = 1;
else
*allowSecureClientConnect = 0;
*connectTimeoutSeconds = AGReadCompactInt(r);
*writeTimeoutSeconds = AGReadCompactInt(r);
*readTimeoutSeconds = AGReadCompactInt(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadSERVERCONFIG\n");
#endif
}
ExportFunc void AGWriteCOOKIE(AGWriter *w, int32 cookieLength, void *cookie)
{
int32 len = 0;
len += AGCompactSize(cookieLength);
len += cookieLength;
AGWriteCompactInt(w, AG_COOKIE_CMD);
AGWriteCompactInt(w, len);
AGWriteCompactInt(w, cookieLength);
AGWriteBytes(w, cookie, cookieLength);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteCOOKIE(%d)\n", cookieLength);
#endif
}
ExportFunc void AGReadNONCE(AGReader *r, uint8 nonce[16])
{
AGReadBytes(r, nonce, 16);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadNONCE");
#endif
}
ExportFunc void AGWriteNONCE(AGWriter *w, uint8 nonce[16])
{
AGWriteCompactInt(w, AG_NONCE_CMD);
AGWriteCompactInt(w, 16);
AGWriteBytes(w, nonce, 16);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteNONCE");
#endif
}
ExportFunc void AGReadCOOKIE(AGReader *r, int32 *cookieLength, void **cookie)
{
*cookieLength = AGReadCompactInt(r);
if (*cookieLength < 0) {
return;
} else if (cookieLength == 0) {
*cookie = NULL;
} else {
*cookie = malloc(*cookieLength);
AGReadBytes(r, *cookie, *cookieLength);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadCOOKIE(%d)\n", *cookieLength);
#endif
}
ExportFunc void AGWriteTASK(AGWriter *w, char *currentTask, AGBool bufferable)
{
int32 len = 0, currentTaskLen = 0;
ADD_STRING_LEN(currentTask, currentTaskLen);
len += 1;
AGWriteCompactInt(w, AG_TASK_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, currentTask, currentTaskLen);
AGWriteBoolean(w, bufferable);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteTASK(%s, %d)\n", currentTask, bufferable);
#endif
}
ExportFunc void AGReadTASK(AGReader *r, char **currentTask, AGBool *bufferable)
{
*currentTask = AGReadString(r);
*bufferable = AGReadBoolean(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadTASK(%s, %d)\n", *currentTask, *bufferable);
#endif
}
ExportFunc void AGWriteITEM(AGWriter *w, int32 currentItemNumber,
int32 totalItemCount, char *currentItem)
{
int32 len = 0, currentItemLen = 0;
len += AGCompactSize(currentItemNumber);
len += AGCompactSize(totalItemCount);
ADD_STRING_LEN(currentItem, currentItemLen);
AGWriteCompactInt(w, AG_ITEM_CMD);
AGWriteCompactInt(w, len);
AGWriteCompactInt(w, currentItemNumber);
AGWriteCompactInt(w, totalItemCount);
AGWriteString(w, currentItem, currentItemLen);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteITEM(%d, %d, %s)\n", currentItemNumber, totalItemCount, currentItem);
#endif
}
ExportFunc void AGReadITEM(AGReader *r, int32 *currentItemNumber,
int32 *totalItemCount, char **currentItem)
{
*currentItemNumber = AGReadCompactInt(r);
*totalItemCount = AGReadCompactInt(r);
*currentItem = AGReadString(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadITEM\n");
#endif
}
ExportFunc void AGWriteDELETEDATABASE(AGWriter *w, char *dbname)
{
int32 len = 0, dbnameLen = 0;
ADD_STRING_LEN(dbname, dbnameLen);
AGWriteCompactInt(w, AG_DELETEDATABASE_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, dbname, dbnameLen);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteDELETEDATABASE(%s)\n", dbname);
#endif
}
ExportFunc void AGReadDELETEDATABASE(AGReader *r, char **dbname)
{
*dbname = AGReadString(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadDELETEDATABASE\n");
#endif
}
ExportFunc void AGWriteOPENDATABASE(AGWriter *w, char *dbname)
{
int32 len = 0, dbnameLen = 0;
ADD_STRING_LEN(dbname, dbnameLen);
AGWriteCompactInt(w, AG_OPENDATABASE_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, dbname, dbnameLen);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteOPENDATABASE(%s)\n", dbname);
#endif
}
ExportFunc void AGReadOPENDATABASE(AGReader *r, char **dbname)
{
*dbname = AGReadString(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadOPENDATABASE(%s)\n", *dbname);
#endif
}
ExportFunc void AGWriteCLOSEDATABASE(AGWriter *w)
{
AGWriteCompactInt(w, AG_CLOSEDATABASE_CMD);
AGWriteCompactInt(w, 0);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteCLOSEDATABASE\n");
#endif
}
ExportFunc void AGReadCLOSEDATABASE(AGReader *r)
{
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadCLOSEDATABASE\n");
#endif
}
ExportFunc void AGWriteCLEARMODS(AGWriter *w)
{
AGWriteCompactInt(w, AG_CLEARMODS_CMD);
AGWriteCompactInt(w, 0);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteCLEARMODS\n");
#endif
}
ExportFunc void AGReadCLEARMODS(AGReader *r)
{
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadCLEARMODS\n");
#endif
}
ExportFunc void AGWriteGOODBYE(AGWriter *w, AGSyncStatus syncStatus,
int32 errorCode, char *errorMsg)
{
int32 len = 0, errorMsgLen = 0;
len += AGCompactSize(syncStatus);
len += AGCompactSize(errorCode);
ADD_STRING_LEN(errorMsg, errorMsgLen);
AGWriteCompactInt(w, AG_GOODBYE_CMD);
AGWriteCompactInt(w, len);
AGWriteCompactInt(w, syncStatus);
AGWriteCompactInt(w, errorCode);
AGWriteString(w, errorMsg, errorMsgLen);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteGOODBYE(%d, %d, %s)\n", syncStatus, errorCode, errorMsg);
#endif
}
ExportFunc void AGReadGOODBYE(AGReader *r, AGSyncStatus *syncStatus,
int32 *errorCode, char **errorMsg)
{
*syncStatus = (AGSyncStatus)AGReadCompactInt(r);
*errorCode = AGReadCompactInt(r);
*errorMsg = AGReadString(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadGOODBYE(%d, %d, %s)\n", *syncStatus, *errorCode, *errorMsg);
#endif
}
ExportFunc void AGWriteRECORD(AGWriter *w, int32 uid, AGRecordStatus mod,
int32 recordDataLength, void *recordData,
int32 platformDataLength, void *platformData)
{
int32 len = 0;
len += 4; // uid
len += AGCompactSize(mod);
len += AGCompactSize(recordDataLength);
len += recordDataLength;
len += AGCompactSize(platformDataLength);
len += platformDataLength;
AGWriteCompactInt(w, AG_RECORD_CMD);
AGWriteCompactInt(w, len);
AGWriteInt32(w, uid);
AGWriteCompactInt(w, mod);
AGWriteCompactInt(w, recordDataLength);
AGWriteBytes(w, recordData, recordDataLength);
AGWriteCompactInt(w, platformDataLength);
AGWriteBytes(w, platformData, platformDataLength);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteRECORD(%d, %d, %d, %d)\n", uid, mod, recordDataLength, platformDataLength);
#endif
}
#ifdef __palmos__
ExportFunc void AGReadRECORDNoCopy(AGBufferReader *r,
int32 *uid, AGRecordStatus *mod,
int32 *recordDataLength, void **recordData,
int32 *platformDataLength, void **platformData)
{
*uid = AGReadInt32((AGReader *)r);
*mod = (AGRecordStatus)AGReadCompactInt((AGReader *)r);
*recordDataLength = AGReadCompactInt((AGReader *)r);
if (*recordDataLength < 0) {
return;
} else if (*recordDataLength == 0) {
*recordData = NULL;
} else {
*recordData = r->buffer + r->currentIndex;
AGBufferReaderSkipBytes(r, *recordDataLength);
}
*platformDataLength = AGReadCompactInt((AGReader *)r);
if (*platformDataLength < 0) {
return;
} else if (*platformDataLength == 0) {
*platformData = NULL;
} else {
*platformData = r->buffer + r->currentIndex;
AGBufferReaderSkipBytes(r, *platformDataLength);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadRECORDNoCopy(%d, %d, %d, %d)\n", *uid, *mod, *recordDataLength, *platformDataLength);
#endif
}
#endif
ExportFunc void AGReadRECORD(AGReader *r, int32 *uid, AGRecordStatus *mod,
int32 *recordDataLength, void **recordData,
int32 *platformDataLength, void **platformData)
{
*uid = AGReadInt32(r);
*mod = (AGRecordStatus)AGReadCompactInt(r);
*recordDataLength = AGReadCompactInt(r);
if (*recordDataLength < 0) {
return;
} else if (*recordDataLength == 0) {
*recordData = NULL;
} else {
*recordData = malloc(*recordDataLength);
AGReadBytes(r, *recordData, *recordDataLength);
}
*platformDataLength = AGReadCompactInt(r);
if (*platformDataLength < 0) {
return;
} else if (*platformDataLength == 0) {
*platformData = NULL;
} else {
*platformData = malloc(*platformDataLength);
AGReadBytes(r, *platformData, *platformDataLength);
}
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadRECORD(%d, %d, %d, %d)\n", *uid, *mod, *recordDataLength, *platformDataLength);
#endif
}
ExportFunc void AGWriteUNKNOWNDATABASE(AGWriter *w, char *dbname)
{
int32 len = 0, dbnameLen = 0;
ADD_STRING_LEN(dbname, dbnameLen);
AGWriteCompactInt(w, AG_UNKNOWNDATABASE_CMD);
AGWriteCompactInt(w, len);
AGWriteString(w, dbname, dbnameLen);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGWriteUNKNOWNDATABASE\n");
#endif
}
ExportFunc void AGReadUNKNOWNDATABASE(AGReader *r, char **dbname)
{
*dbname = AGReadString(r);
#ifdef DEBUG_PROTOCOL
printf("\t agprotocol.c - AGReadUNKNOWNDATABASE\n");
#endif
}
ExportFunc void AGWriteNEWIDS(AGWriter *w, AGArray *newids)
{
int32 i, count, len = 0;
if(!newids || AGArrayCount(newids) < 1) {
count = 0;
} else {
count = AGArrayCount(newids);
}
len += AGCompactSize(count);
len += (count * 4);
AGWriteCompactInt(w, AG_NEWIDS_CMD);
AGWriteCompactInt(w, len);
AGWriteCompactInt(w, count);
if(count > 0) {
for(i = 0; i < count; i++)
AGWriteInt32(w, (uint32)AGArrayElementAt(newids, i));
}
}
ExportFunc void AGReadNEWIDS(AGReader *r, AGArray **newids)
{
int32 i, count;
*newids = NULL;
count = AGReadCompactInt(r);
if(count > 0) {
*newids = AGArrayNew(AGIntegerElements, count);
for(i = 0; i < count; i++)
AGArrayAppend(*newids, (void *)AGReadInt32(r));
}
}
ExportFunc void AGWriteEXPANSION(AGWriter *w,
int32 expansionCommand,
int32 commandLength,
void *commandBytes)
{
int32 len = 0;
len += AGCompactSize(expansionCommand);
len += AGCompactSize(commandLength);
len += commandLength;
AGWriteCompactInt(w, AG_EXPANSION_CMD);
AGWriteCompactInt(w, len);
AGWriteCompactInt(w, expansionCommand);
AGWriteCompactInt(w, commandLength);
AGWriteBytes(w, commandBytes, commandLength);
}
ExportFunc void AGReadEXPANSION(AGReader *r,
int32 *expansionCommand,
int32 *commandLength,
void **commandBytes)
{
*expansionCommand = AGReadCompactInt(r);
*commandLength = AGReadCompactInt(r);
if(*commandLength) {
*commandBytes = malloc(*commandLength);
AGReadBytes(r, *commandBytes, *commandLength);
}
}
ExportFunc void AGWriteEXPANSION_RESOURCE(AGWriter *w,
uint32 resourceType,
uint32 resourceLen,
void* resource)
{
int32 len = 0;
AGBufferWriter * tw = NULL;
len += AGCompactSize(resourceType);
len += AGCompactSize(resourceLen);
len += resourceLen;
tw = AGBufferWriterNew(len);
AGWriteCompactInt((AGWriter*)tw, resourceType);
AGWriteCompactInt((AGWriter*)tw, resourceLen);
if (resourceLen > 0)
AGWriteBytes((AGWriter*)tw, resource, resourceLen);
AGWriteEXPANSION(w,
AG_EXPANSION_RESOURCE,
len,
AGBufferWriterGetBuffer(tw));
AGBufferWriterFree(tw);
}
ExportFunc void AGReadEXPANSION_RESOURCE(AGReader *r,
int32* resourceType,
int32* resourceLen,
void** resource)
{
AGReadEXPANSION(r, resourceType, resourceLen, resource);
}
void AGWriteEXPANSION_CHANGESERVERCONFIG(AGWriter *w,
AGBool disableServer,
int32 flags,
char *serverName,
int16 serverPort,
char *userName,
int32 passwordLen,
uint8 *password,
AGBool connectSecurely,
AGBool notRemovable)
{
int32 len = 0, serverNameLen = 0, userNameLen = 0;
AGBufferWriter * tw = NULL;
len += 1; /* disableServer */
len += AGCompactSize(flags);
ADD_STRING_LEN(serverName, serverNameLen);
len += AGCompactSize(serverPort);
ADD_STRING_LEN(userName, userNameLen);
len += AGCompactSize(passwordLen);
len += passwordLen; /* password */
len += 1; /* connectSecurely */
len += 1; /* notRemovable */
tw = AGBufferWriterNew(len);
AGWriteBoolean((AGWriter*)tw, disableServer);
AGWriteCompactInt((AGWriter*)tw, flags);
AGWriteString((AGWriter*)tw, serverName, serverNameLen);
AGWriteCompactInt((AGWriter*)tw, serverPort);
AGWriteString((AGWriter*)tw, userName, userNameLen);
AGWriteCompactInt((AGWriter*)tw, passwordLen);
if (passwordLen > 0)
AGWriteBytes((AGWriter*)tw, password, passwordLen);
AGWriteBoolean((AGWriter*)tw, connectSecurely);
AGWriteBoolean((AGWriter*)tw, notRemovable);
AGWriteEXPANSION(w,
AG_EXPANSION_CHANGESERVERCONFIG,
len,
AGBufferWriterGetBuffer(tw));
AGBufferWriterFree(tw);
}
ExportFunc void AGReadEXPANSION_CHANGESERVERCONFIG(AGReader *r,
AGBool *disableServer,
int32 *flags,
char **serverName,
int16 *serverPort,
char **userName,
int32 *passwordLen,
uint8 **password,
AGBool *connectSecurely,
AGBool *notRemovable)
{
*disableServer = AGReadBoolean(r);
*flags = AGReadCompactInt(r);
*serverName = AGReadString(r);
*serverPort = AGReadCompactInt(r);
*userName = AGReadString(r);
*passwordLen = AGReadCompactInt(r);
if (0 == *passwordLen)
*password = NULL;
else {
/* Failure is not an option! */
*password = (uint8*)malloc(*passwordLen);
AGReadBytes(r, *password, *passwordLen);
}
*connectSecurely = AGReadBoolean(r);
*notRemovable = AGReadBoolean(r);
}
ExportFunc char *AGProtocolCommandName(AGCommand command)
{
switch(command) {
case AG_END_CMD: return "AG_END_CMD";
case AG_EXPANSION_CMD: return "AG_EXPANSION_CMD";
case AG_HELLO_CMD: return "AG_HELLO_CMD";
case AG_DEVICEINFO_CMD: return "AG_DEVICEINFO_CMD";
case AG_SENDDEVICEINFO_CMD: return "AG_SENDDEVICEINFO_CMD";
case AG_DATABASECONFIG_CMD: return "AG_DATABASECONFIG_CMD";
case AG_SERVERCONFIG_CMD: return "AG_SERVERCONFIG_CMD";
case AG_COOKIE_CMD: return "AG_COOKIE_CMD";
case AG_TASK_CMD: return "AG_TASK_CMD";
case AG_ITEM_CMD: return "AG_ITEM_CMD";
case AG_DELETEDATABASE_CMD: return "AG_DELETEDATABASE_CMD";
case AG_OPENDATABASE_CMD: return "AG_OPENDATABASE_CMD";
case AG_CLOSEDATABASE_CMD: return "AG_CLOSEDATABASE_CMD";
case AG_CLEARMODS_CMD: return "AG_CLEARMODS_CMD";
case AG_GOODBYE_CMD: return "AG_GOODBYE_CMD";
case AG_RECORD_CMD: return "AG_RECORD_CMD";
case AG_NONCE_CMD: return "AG_NONCE_CMD";
case AG_NEWIDS_CMD: return "AG_NEWIDS_CMD";
case AG_UNKNOWNDATABASE_CMD: return "AG_UNKNOWNDATABASE_CMD";
case AG_PING_CMD: return "AG_PING_CMD";
case AG_XMLDATA_CMD: return "AG_XMLDATA_CMD";
case AG_LASTCOMMAND: return "AG_LASTCOMMAND"; // This should never happen
}
return NULL;
}
|