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 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
|
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2018 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// GUI RPC server side (the actual RPCs)
#include "cpp.h"
#ifdef __APPLE__
#include <Carbon/Carbon.h>
#include <libproc.h>
#endif
#ifdef _WIN32
#include "boinc_win.h"
#else
#include "config.h"
#include <cstdio>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#include <vector>
#include <cstring>
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#endif
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
#include "error_numbers.h"
#include "filesys.h"
#include "network.h"
#include "parse.h"
#include "str_replace.h"
#include "str_util.h"
#include "url.h"
#include "util.h"
#include "client_state.h"
#include "client_msgs.h"
#include "client_state.h"
#include "cs_proxy.h"
#include "cs_notice.h"
#include "file_names.h"
#include "project.h"
#include "result.h"
using std::string;
using std::vector;
static void auth_failure(MIOFILE& fout) {
fout.printf("<unauthorized/>\n");
}
void GUI_RPC_CONN::handle_auth1(MIOFILE& fout) {
sprintf(nonce, "%f", dtime());
fout.printf("<nonce>%s</nonce>\n", nonce);
}
int GUI_RPC_CONN::handle_auth2(char* buf, MIOFILE& fout) {
char nonce_hash[256], nonce_hash_correct[256], buf2[512];
if (!parse_str(buf, "<nonce_hash>", nonce_hash, 256)) {
auth_failure(fout);
return ERR_AUTHENTICATOR;
}
snprintf(buf2, sizeof(buf2), "%s%s", nonce, gstate.gui_rpcs.password);
md5_block((const unsigned char*)buf2, (int)strlen(buf2), nonce_hash_correct);
if (strcmp(nonce_hash, nonce_hash_correct)) {
auth_failure(fout);
return ERR_AUTHENTICATOR;
}
fout.printf("<authorized/>\n");
auth_needed = false;
return 0;
}
// client passes its version, but ignore it for now
//
static void handle_exchange_versions(GUI_RPC_CONN& grc) {
grc.mfout.printf(
"<server_version>\n"
" <major>%d</major>\n"
" <minor>%d</minor>\n"
" <release>%d</release>\n"
"</server_version>\n",
BOINC_MAJOR_VERSION,
BOINC_MINOR_VERSION,
BOINC_RELEASE
);
}
static void handle_get_simple_gui_info(GUI_RPC_CONN& grc) {
unsigned int i;
grc.mfout.printf("<simple_gui_info>\n");
for (i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
p->write_state(grc.mfout, true);
}
gstate.write_tasks_gui(grc.mfout, true);
grc.mfout.printf("</simple_gui_info>\n");
}
static void handle_get_project_status(GUI_RPC_CONN& grc) {
unsigned int i;
grc.mfout.printf("<projects>\n");
for (i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
p->write_state(grc.mfout, true);
}
grc.mfout.printf("</projects>\n");
}
static void handle_get_disk_usage(GUI_RPC_CONN& grc) {
unsigned int i;
double size, boinc_non_project, d_allowed, boinc_total;
grc.mfout.printf("<disk_usage_summary>\n");
int retval = get_filesystem_info(
gstate.host_info.d_total, gstate.host_info.d_free
);
if (retval) {
msg_printf(0, MSG_INTERNAL_ERROR,
"get_filesystem_info(): %s", boincerror(retval)
);
}
dir_size(".", boinc_non_project, false);
dir_size("locale", size, false);
boinc_non_project += size;
#ifdef __APPLE__
if (gstate.launched_by_manager) {
// If launched by Manager, get Manager's size on disk
char path[MAXPATHLEN];
double manager_size = 0.0;
OSStatus err = noErr;
retval = proc_pidpath(getppid(), path, sizeof(path));
if (retval <= 0) err = fnfErr;
if (! err) dir_size(path, manager_size, true);
if (! err) boinc_non_project += manager_size;
}
#endif
boinc_total = boinc_non_project;
gstate.get_disk_usages();
for (i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
grc.mfout.printf(
"<project>\n"
" <master_url>%s</master_url>\n"
" <disk_usage>%f</disk_usage>\n"
"</project>\n",
p->master_url, p->disk_usage
);
boinc_total += p->disk_usage;
}
d_allowed = gstate.allowed_disk_usage(gstate.total_disk_usage);
grc.mfout.printf(
"<d_total>%f</d_total>\n"
"<d_free>%f</d_free>\n"
"<d_boinc>%f</d_boinc>\n"
"<d_allowed>%f</d_allowed>\n",
gstate.host_info.d_total,
gstate.host_info.d_free,
boinc_non_project,
d_allowed
);
grc.mfout.printf("</disk_usage_summary>\n");
}
static PROJECT* get_project(GUI_RPC_CONN& grc, string url) {
if (url.empty()) {
grc.mfout.printf("<error>Missing project URL</error>\n");
return 0;
}
PROJECT* p = gstate.lookup_project(url.c_str());
if (!p) {
grc.mfout.printf("<error>No such project</error>\n");
return 0 ;
}
return p;
}
static PROJECT* get_project_parse(GUI_RPC_CONN& grc) {
string url;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_string("project_url", url)) continue;
}
return get_project(grc, url);
}
static void handle_project_reset(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
gstate.request_schedule_cpus("project reset by user");
gstate.request_work_fetch("project reset by user");
gstate.reset_project(p, false);
grc.mfout.printf("<success/>\n");
}
static void handle_project_suspend(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "project suspended by user");
p->suspend();
grc.mfout.printf("<success/>\n");
}
static void handle_project_resume(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "project resumed by user");
p->resume();
grc.mfout.printf("<success/>\n");
}
static void handle_project_detach(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
if (p->attached_via_acct_mgr) {
msg_printf(p, MSG_INFO,
"This project must be detached using the account manager web site."
);
grc.mfout.printf("<error>must detach using account manager</error>");
return;
}
gstate.detach_project(p);
gstate.request_schedule_cpus("project detached by user");
gstate.request_work_fetch("project detached by user");
grc.mfout.printf("<success/>\n");
}
static void handle_project_update(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "update requested by user");
p->sched_rpc_pending = RPC_REASON_USER_REQ;
p->min_rpc_time = 0;
#if 1
rss_feeds.trigger_fetch(p);
#endif
gstate.request_work_fetch("project updated by user");
grc.mfout.printf("<success/>\n");
}
static void handle_project_nomorework(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "work fetch suspended by user");
p->dont_request_more_work = true;
grc.mfout.printf("<success/>\n");
}
static void handle_project_allowmorework(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "work fetch resumed by user");
p->dont_request_more_work = false;
gstate.request_work_fetch("project work fetch resumed by user");
grc.mfout.printf("<success/>\n");
}
static void handle_project_detach_when_done(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "detach when done set by user");
p->detach_when_done = true;
p->dont_request_more_work = true;
grc.mfout.printf("<success/>\n");
}
static void handle_project_dont_detach_when_done(GUI_RPC_CONN& grc) {
PROJECT* p = get_project_parse(grc);
if (!p) return;
gstate.set_client_state_dirty("Project modified by user");
msg_printf(p, MSG_INFO, "detach when done cleared by user");
p->detach_when_done = false;
p->dont_request_more_work = false;
grc.mfout.printf("<success/>\n");
}
static void handle_set_run_mode(GUI_RPC_CONN& grc) {
double duration = 0;
bool btemp;
int mode=-1;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_double("duration", duration)) continue;
if (grc.xp.parse_bool("always", btemp)) {
mode = RUN_MODE_ALWAYS;
continue;
}
if (grc.xp.parse_bool("never", btemp)) {
mode = RUN_MODE_NEVER;
continue;
}
if (grc.xp.parse_bool("auto", btemp)) {
mode = RUN_MODE_AUTO;
continue;
}
if (grc.xp.parse_bool("restore", btemp)) {
mode = RUN_MODE_RESTORE;
continue;
}
}
if (mode < 0) {
grc.mfout.printf("<error>Missing mode</error>\n");
return;
}
gstate.cpu_run_mode.set(mode, duration);
grc.mfout.printf("<success/>\n");
}
static void handle_set_gpu_mode(GUI_RPC_CONN& grc) {
double duration = 0;
bool btemp;
int mode=-1;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_double("duration", duration)) continue;
if (grc.xp.parse_bool("always", btemp)) {
mode = RUN_MODE_ALWAYS;
continue;
}
if (grc.xp.parse_bool("never", btemp)) {
mode = RUN_MODE_NEVER;
continue;
}
if (grc.xp.parse_bool("auto", btemp)) {
mode = RUN_MODE_AUTO;
continue;
}
if (grc.xp.parse_bool("restore", btemp)) {
mode = RUN_MODE_RESTORE;
continue;
}
}
if (mode < 0) {
grc.mfout.printf("<error>Missing mode</error>\n");
return;
}
gstate.gpu_run_mode.set(mode, duration);
gstate.request_schedule_cpus("GPU mode changed");
grc.mfout.printf("<success/>\n");
}
// On Android, get product name, OS name, OS version, and MAC addr from GUI,
//
static void handle_set_host_info(GUI_RPC_CONN& grc) {
while (!grc.xp.get_tag()) {
if (grc.xp.match_tag("host_info")) {
HOST_INFO hi;
int retval = hi.parse(grc.xp);
if (retval) {
grc.mfout.printf("<error>host_info parse error</error>\n");
return;
}
if (strlen(hi.product_name)) {
safe_strcpy(gstate.host_info.product_name, hi.product_name);
}
// this will always be "Android"
//
if (strlen(hi.os_name)) {
safe_strcpy(gstate.host_info.os_name, hi.os_name);
}
// We already have the Linux kernel version;
// append the Android version.
//
if (strlen(hi.os_version)) {
if (!strstr(gstate.host_info.os_version, "Android")) {
safe_strcat(gstate.host_info.os_version, " (Android ");
safe_strcat(gstate.host_info.os_version, hi.os_version);
safe_strcat(gstate.host_info.os_version, ")");
}
}
grc.mfout.printf("<success/>\n");
gstate.set_client_state_dirty("set_host_info RPC");
return;
}
}
grc.mfout.printf("<error>Missing host_info</error>\n");
}
static void handle_set_network_mode(GUI_RPC_CONN& grc) {
double duration = 0;
bool btemp;
int mode=-1;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_double("duration", duration)) continue;
if (grc.xp.parse_bool("always", btemp)) {
mode = RUN_MODE_ALWAYS;
continue;
}
if (grc.xp.parse_bool("never", btemp)) {
mode = RUN_MODE_NEVER;
continue;
}
if (grc.xp.parse_bool("auto", btemp)) {
mode = RUN_MODE_AUTO;
continue;
}
if (grc.xp.parse_bool("restore", btemp)) {
mode = RUN_MODE_RESTORE;
continue;
}
}
if (mode < 0) {
grc.mfout.printf("<error>Missing mode</error>\n");
return;
}
// user is turning network on/off explicitly,
// so disable the "5 minute grace period" mechanism
//
gstate.gui_rpcs.time_of_last_rpc_needing_network = 0;
gstate.network_run_mode.set(mode, duration);
grc.mfout.printf("<success/>\n");
}
static void handle_run_benchmarks(GUI_RPC_CONN& grc) {
gstate.start_cpu_benchmarks();
grc.mfout.printf("<success/>\n");
}
static void handle_set_proxy_settings(GUI_RPC_CONN& grc) {
gui_proxy_info.parse(grc.xp);
gstate.set_client_state_dirty("Set proxy settings RPC");
grc.mfout.printf("<success/>\n");
select_proxy_info();
// tell running apps to reread app_info file (for F@h)
//
gstate.active_tasks.request_reread_app_info();
}
static void handle_get_proxy_settings(GUI_RPC_CONN& grc) {
gui_proxy_info.write(grc.mfout);
}
// params:
// [ <seqno>n</seqno> ]
// return only msgs with seqno > n; if absent or zero, return all
//
static void handle_get_messages(GUI_RPC_CONN& grc) {
int seqno=0;
bool translatable = false;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_int("seqno", seqno)) continue;
if (grc.xp.parse_bool("translatable", translatable)) continue;
}
message_descs.write(seqno, grc.mfout, translatable);
}
static void handle_get_message_count(GUI_RPC_CONN& grc) {
grc.mfout.printf("<seqno>%d</seqno>\n", message_descs.highest_seqno());
}
// <retry_file_transfer>
// <project_url>XXX</project_url>
// <filename>XXX</filename>
// </retry_file_transfer>
//
static void handle_file_transfer_op(GUI_RPC_CONN& grc, const char* op) {
string project_url, filename;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_string("filename", filename)) continue;
if (grc.xp.parse_string("project_url", project_url)) continue;
}
PROJECT* p = get_project(grc, project_url);
if (!p) return;
if (filename.empty()) {
grc.mfout.printf("<error>Missing filename</error>\n");
return;
}
FILE_INFO* f = gstate.lookup_file_info(p, filename.c_str());
if (!f) {
grc.mfout.printf("<error>No such file</error>\n");
return;
}
PERS_FILE_XFER* pfx = f->pers_file_xfer;
if (!pfx) {
grc.mfout.printf("<error>No such transfer waiting</error>\n");
return;
}
if (!strcmp(op, "retry")) {
pfx->next_request_time = 0;
// leave file-level backoff mode
f->project->file_xfer_backoff(pfx->is_upload).file_xfer_succeeded();
// and leave project-level backoff mode
} else if (!strcmp(op, "abort")) {
f->pers_file_xfer->abort();
} else {
grc.mfout.printf("<error>unknown op</error>\n");
return;
}
gstate.set_client_state_dirty("File transfer RPC");
grc.mfout.printf("<success/>\n");
}
static void handle_retry_file_transfer(GUI_RPC_CONN& grc) {
handle_file_transfer_op(grc, "retry");
}
static void handle_abort_file_transfer(GUI_RPC_CONN& grc) {
handle_file_transfer_op(grc, "abort");
}
static void handle_result_op(GUI_RPC_CONN& grc, const char* op) {
RESULT* rp;
char result_name[256];
ACTIVE_TASK* atp;
string project_url;
safe_strcpy(result_name, "");
while (!grc.xp.get_tag()) {
if (grc.xp.parse_str("name", result_name, sizeof(result_name))) continue;
if (grc.xp.parse_string("project_url", project_url)) continue;
}
PROJECT* p = get_project(grc, project_url);
if (!p) return;
if (!strlen(result_name)) {
grc.mfout.printf("<error>Missing result name</error>\n");
return;
}
rp = gstate.lookup_result(p, result_name);
if (!rp) {
grc.mfout.printf("<error>no such result</error>\n");
return;
}
if (!strcmp(op, "abort")) {
msg_printf(p, MSG_INFO, "task %s aborted by user", result_name);
atp = gstate.lookup_active_task_by_result(rp);
if (atp) {
atp->abort_task(EXIT_ABORTED_VIA_GUI, "aborted by user");
} else {
rp->abort_inactive(EXIT_ABORTED_VIA_GUI);
}
gstate.request_work_fetch("result aborted by user");
} else if (!strcmp(op, "suspend")) {
msg_printf(p, MSG_INFO, "task %s suspended by user", result_name);
rp->suspended_via_gui = true;
gstate.request_work_fetch("task suspended by user");
} else if (!strcmp(op, "resume")) {
msg_printf(p, MSG_INFO, "task %s resumed by user", result_name);
rp->suspended_via_gui = false;
}
gstate.request_schedule_cpus("task suspended, resumed or aborted by user");
gstate.set_client_state_dirty("Result RPC");
grc.mfout.printf("<success/>\n");
}
static void handle_suspend_result(GUI_RPC_CONN& grc) {
handle_result_op(grc, "suspend");
}
static void handle_resume_result(GUI_RPC_CONN& grc) {
handle_result_op(grc, "resume");
}
static void handle_abort_result(GUI_RPC_CONN& grc) {
handle_result_op(grc, "abort");
}
static void handle_get_host_info(GUI_RPC_CONN& grc) {
gstate.host_info.write(grc.mfout, true, true);
}
static void handle_get_screensaver_tasks(GUI_RPC_CONN& grc) {
unsigned int i;
ACTIVE_TASK* atp;
grc.mfout.printf(
"<handle_get_screensaver_tasks>\n"
" <suspend_reason>%d</suspend_reason>\n",
gstate.suspend_reason
);
for (i=0; i<gstate.active_tasks.active_tasks.size(); i++) {
atp = gstate.active_tasks.active_tasks[i];
if (atp->scheduler_state == CPU_SCHED_SCHEDULED) {
atp->result->write_gui(grc.mfout);
}
}
grc.mfout.printf("</handle_get_screensaver_tasks>\n");
}
static void handle_quit(GUI_RPC_CONN& grc) {
gstate.requested_exit = true;
grc.mfout.printf("<success/>\n");
}
static void handle_acct_mgr_info(GUI_RPC_CONN& grc) {
grc.mfout.printf(
"<acct_mgr_info>\n"
" <acct_mgr_url>%s</acct_mgr_url>\n"
" <acct_mgr_name>%s</acct_mgr_name>\n",
gstate.acct_mgr_info.master_url,
gstate.acct_mgr_info.project_name
);
if (strlen(gstate.acct_mgr_info.login_name)
|| strlen(gstate.acct_mgr_info.authenticator)
) {
grc.mfout.printf(" <have_credentials/>\n");
}
if (gstate.acct_mgr_info.cookie_required) {
grc.mfout.printf(" <cookie_required/>\n");
grc.mfout.printf(
" <cookie_failure_url>%s</cookie_failure_url>\n",
gstate.acct_mgr_info.cookie_failure_url
);
}
grc.mfout.printf("</acct_mgr_info>\n");
}
static void handle_get_statistics(GUI_RPC_CONN& grc) {
grc.mfout.printf("<statistics>\n");
for (unsigned int i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
p->write_statistics(grc.mfout);
}
grc.mfout.printf("</statistics>\n");
}
static void handle_get_cc_status(GUI_RPC_CONN& grc) {
grc.mfout.printf(
"<cc_status>\n"
" <network_status>%d</network_status>\n"
" <ams_password_error>%d</ams_password_error>\n"
" <task_suspend_reason>%d</task_suspend_reason>\n"
" <task_mode>%d</task_mode>\n"
" <task_mode_perm>%d</task_mode_perm>\n"
" <task_mode_delay>%f</task_mode_delay>\n"
" <gpu_suspend_reason>%d</gpu_suspend_reason>\n"
" <gpu_mode>%d</gpu_mode>\n"
" <gpu_mode_perm>%d</gpu_mode_perm>\n"
" <gpu_mode_delay>%f</gpu_mode_delay>\n"
" <network_suspend_reason>%d</network_suspend_reason>\n"
" <network_mode>%d</network_mode>\n"
" <network_mode_perm>%d</network_mode_perm>\n"
" <network_mode_delay>%f</network_mode_delay>\n"
" <disallow_attach>%d</disallow_attach>\n"
" <simple_gui_only>%d</simple_gui_only>\n"
" <max_event_log_lines>%d</max_event_log_lines>\n",
net_status.network_status(),
gstate.acct_mgr_info.password_error?1:0,
gstate.suspend_reason,
gstate.cpu_run_mode.get_current(),
gstate.cpu_run_mode.get_perm(),
gstate.cpu_run_mode.delay(),
gpu_suspend_reason,
gstate.gpu_run_mode.get_current(),
gstate.gpu_run_mode.get_perm(),
gstate.gpu_run_mode.delay(),
gstate.network_suspend_reason,
gstate.network_run_mode.get_current(),
gstate.network_run_mode.get_perm(),
gstate.network_run_mode.delay(),
cc_config.disallow_attach?1:0,
cc_config.simple_gui_only?1:0,
cc_config.max_event_log_lines
);
if (grc.au_mgr_state == AU_MGR_QUIT_REQ) {
grc.mfout.printf(
" <manager_must_quit>1</manager_must_quit>\n"
);
grc.au_mgr_state = AU_MGR_QUIT_SENT;
}
grc.mfout.printf(
"</cc_status>\n"
);
}
static void handle_network_available(GUI_RPC_CONN& grc) {
net_status.network_available();
grc.mfout.printf("<success/>\n");
}
static void handle_get_project_init_status(GUI_RPC_CONN& grc) {
// If we're already attached to the project specified in the
// project init file, delete the file.
//
for (unsigned i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
if (!strcmp(p->master_url, gstate.project_init.url)) {
gstate.project_init.remove();
break;
}
}
grc.mfout.printf(
"<get_project_init_status>\n"
" <url>%s</url>\n"
" <name>%s</name>\n"
" <team_name>%s</team_name>\n"
" <setup_cookie>%s</setup_cookie>\n"
" %s\n"
" %s\n"
"</get_project_init_status>\n",
gstate.project_init.url,
gstate.project_init.name,
gstate.project_init.team_name,
gstate.project_init.setup_cookie,
strlen(gstate.project_init.account_key)?"<has_account_key/>":"",
gstate.project_init.embedded?"<embedded/>":""
);
}
void handle_get_project_config(GUI_RPC_CONN& grc) {
string url;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_string("url", url)) continue;
}
if (url.empty()) {
grc.mfout.printf("<error>no url</error>\n");
return;
}
canonicalize_master_url(url);
grc.get_project_config_op.do_rpc(url);
grc.mfout.printf("<success/>\n");
}
void handle_get_project_config_poll(GUI_RPC_CONN& grc) {
if (grc.get_project_config_op.error_num) {
grc.mfout.printf(
"<project_config>\n"
" <error_num>%d</error_num>\n"
"</project_config>\n",
grc.get_project_config_op.error_num
);
} else {
const char *p = grc.get_project_config_op.reply.c_str();
const char *q = strstr(p, "<project_config");
if (!q) q = "<project_config/>\n";
grc.mfout.printf("%s", q);
}
}
void handle_lookup_account(GUI_RPC_CONN& grc) {
ACCOUNT_IN ai;
MIOFILE in;
ai.parse(grc.xp);
if ((!ai.url.size() || !ai.email_addr.size() || !ai.passwd_hash.size()) && !ai.server_assigned_cookie) {
grc.mfout.printf("<error>missing URL, email address, or password</error>\n");
return;
}
grc.lookup_account_op.do_rpc(ai);
grc.mfout.printf("<success/>\n");
}
void handle_lookup_account_poll(GUI_RPC_CONN& grc) {
if (grc.lookup_account_op.error_num) {
grc.mfout.printf(
"<account_out>\n"
" <error_num>%d</error_num>\n"
"</account_out>\n",
grc.lookup_account_op.error_num
);
} else {
const char *p = grc.lookup_account_op.reply.c_str();
const char *q = strstr(p, "<account_out");
if (!q) q = strstr(p, "<error");
if (!q) q = "<account_out/>\n";
grc.mfout.printf("%s", q);
}
}
void handle_create_account(GUI_RPC_CONN& grc) {
ACCOUNT_IN ai;
ai.parse(grc.xp);
grc.create_account_op.do_rpc(ai);
grc.mfout.printf("<success/>\n");
}
void handle_create_account_poll(GUI_RPC_CONN& grc) {
if (grc.create_account_op.error_num) {
grc.mfout.printf(
"<account_out>\n"
" <error_num>%d</error_num>\n"
"</account_out>\n",
grc.create_account_op.error_num
);
} else {
grc.mfout.printf("%s", grc.create_account_op.reply.c_str());
}
}
static void handle_project_attach(GUI_RPC_CONN& grc) {
string url, authenticator, project_name;
bool use_config_file = false;
bool already_attached = false;
unsigned int i;
int retval;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_bool("use_config_file", use_config_file)) continue;
if (grc.xp.parse_string("project_url", url)) continue;
if (grc.xp.parse_string("authenticator", authenticator)) continue;
if (grc.xp.parse_string("project_name", project_name)) continue;
}
// Get URL/auth from project_init.xml?
//
if (use_config_file) {
if (!strlen(gstate.project_init.url)) {
grc.mfout.printf("<error>Missing URL</error>\n");
return;
}
if (!strlen(gstate.project_init.account_key)) {
grc.mfout.printf("<error>Missing authenticator</error>\n");
return;
}
url = gstate.project_init.url;
authenticator = gstate.project_init.account_key;
} else {
if (url.empty()) {
grc.mfout.printf("<error>Missing URL</error>\n");
return;
}
if (authenticator.empty()) {
grc.mfout.printf("<error>Missing authenticator</error>\n");
return;
}
}
for (i=0; i<gstate.projects.size(); i++) {
PROJECT* p = gstate.projects[i];
if (url == p->master_url) already_attached = true;
}
if (already_attached) {
grc.mfout.printf("<error>Already attached to project</error>\n");
return;
}
// clear messages from previous attach to project.
//
gstate.project_attach.messages.clear();
gstate.project_attach.error_num = gstate.add_project(
url.c_str(), authenticator.c_str(), project_name.c_str(), false
);
// if project_init.xml refers to this project,
// delete the file, otherwise we'll just
// reattach the next time the client starts
//
if (!strcmp(url.c_str(), gstate.project_init.url)) {
retval = gstate.project_init.remove();
if (retval) {
msg_printf(NULL, MSG_INTERNAL_ERROR,
"Can't delete project init file: %s", boincerror(retval)
);
}
}
grc.mfout.printf("<success/>\n");
}
static void handle_project_attach_poll(GUI_RPC_CONN& grc) {
unsigned int i;
grc.mfout.printf(
"<project_attach_reply>\n"
);
for (i=0; i<gstate.project_attach.messages.size(); i++) {
grc.mfout.printf(
" <message>%s</message>\n",
gstate.project_attach.messages[i].c_str()
);
}
grc.mfout.printf(
" <error_num>%d</error_num>\n",
gstate.project_attach.error_num
);
grc.mfout.printf(
"</project_attach_reply>\n"
);
}
// This RPC, regrettably, serves 3 purposes
// - to join an account manager
// pass URL of account manager and account name/passwd
// - to trigger an RPC to the current account manager
// either
// pass URL/name/passwd hash of current AM
// TODO: get rid of this option;
// the manager shouldn't have to keep track of this info
// or pass <use_config_file/> flag: do RPC to current AM
// - to quit an account manager
// url/name/passwd args are null
//
static void handle_acct_mgr_rpc(GUI_RPC_CONN& grc) {
string url, name, password, authenticator;
string password_hash, name_lc;
bool use_config_file = false;
bool bad_arg = false;
bool url_found=false, name_found=false, password_found = false;
ACCT_MGR_INFO ami;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_string("url", url)) {
url_found = true;
continue;
}
if (grc.xp.parse_string("name", name)) {
name_found = true;
continue;
}
if (grc.xp.parse_string("password", password)) {
password_found = true;
continue;
}
if (grc.xp.parse_bool("use_config_file", use_config_file)) continue;
}
if (use_config_file) {
// really means: use current AM
//
if (!gstate.acct_mgr_info.using_am()) {
bad_arg = true;
msg_printf(NULL, MSG_INTERNAL_ERROR,
"Not using account manager"
);
} else {
ami = gstate.acct_mgr_info;
}
} else {
bad_arg = !url_found || !name_found || !password_found;
if (!bad_arg) {
name_lc = name;
downcase_string(name_lc);
if (!starts_with(password, "hash:")) {
password_hash = md5_string(password+name_lc);
} else {
// Remove 'hash:'
password_hash = password.substr(5);
}
safe_strcpy(ami.master_url, url.c_str());
safe_strcpy(ami.login_name, name.c_str());
safe_strcpy(ami.password_hash, password_hash.c_str());
safe_strcpy(ami.authenticator, authenticator.c_str());
}
}
if (bad_arg) {
grc.mfout.printf("<error>bad arg</error>\n");
} else if (gstate.acct_mgr_info.using_am()
&& !url.empty()
&& !gstate.acct_mgr_info.same_am(url.c_str(), name.c_str(), password_hash.c_str(), authenticator.c_str())
){
grc.mfout.printf("<error>attached to a different AM - detach first</error>\n");
} else {
gstate.acct_mgr_op.do_rpc(ami, true);
grc.mfout.printf("<success/>\n");
}
}
static void handle_acct_mgr_rpc_poll(GUI_RPC_CONN& grc) {
grc.mfout.printf(
"<acct_mgr_rpc_reply>\n"
);
if (gstate.acct_mgr_op.error_str.size()) {
grc.mfout.printf(
" <message>%s</message>\n",
gstate.acct_mgr_op.error_str.c_str()
);
}
grc.mfout.printf(
" <error_num>%d</error_num>\n",
gstate.acct_mgr_op.error_num
);
grc.mfout.printf(
"</acct_mgr_rpc_reply>\n"
);
}
static void handle_get_newer_version(GUI_RPC_CONN& grc) {
gstate.new_version_check(true);
grc.mfout.printf(
"<newer_version>%s</newer_version>\n"
"<download_url>%s</download_url>\n",
gstate.newer_version.c_str(),
nvc_config.client_download_url.c_str()
);
}
static void handle_get_global_prefs_file(GUI_RPC_CONN& grc) {
GLOBAL_PREFS p;
bool found;
int retval = p.parse_file(
GLOBAL_PREFS_FILE_NAME, gstate.main_host_venue, found
);
if (retval) {
grc.mfout.printf("<error>%d</error>\n", retval);
return;
}
p.write(grc.mfout);
}
static void handle_get_global_prefs_working(GUI_RPC_CONN& grc) {
gstate.global_prefs.write(grc.mfout);
}
static void handle_get_global_prefs_override(GUI_RPC_CONN& grc) {
string s;
int retval = read_file_string(GLOBAL_PREFS_OVERRIDE_FILE, s);
if (!retval) {
strip_whitespace(s);
grc.mfout.printf("%s\n", s.c_str());
} else {
grc.mfout.printf("<error>no prefs override file</error>\n");
}
}
static void handle_set_global_prefs_override(GUI_RPC_CONN& grc) {
int retval;
char buf[65536];
retval = grc.xp.element_contents("</set_global_prefs_override>", buf, sizeof(buf));
if (!retval) {
if (strlen(buf)) {
FILE* f = boinc_fopen(GLOBAL_PREFS_OVERRIDE_FILE, "w");
if (f) {
fprintf(f, "%s\n", buf);
fclose(f);
retval = 0;
} else {
retval = ERR_FOPEN;
}
} else {
retval = boinc_delete_file(GLOBAL_PREFS_OVERRIDE_FILE);
}
}
if (retval) {
grc.mfout.printf("<status>%d</status>\n", retval);
} else {
grc.mfout.printf("<success/>\n");
}
}
static void read_all_projects_list_file(GUI_RPC_CONN& grc) {
string s;
int retval = read_file_string(ALL_PROJECTS_LIST_FILENAME, s);
if (!retval) {
strip_whitespace(s);
const char *q = strstr(s.c_str(), "<projects");
if (!q) q = "<projects/>";
grc.mfout.printf("%s\n", q);
}
}
static void handle_get_state(GUI_RPC_CONN& grc) {
gstate.write_state_gui(grc.mfout);
}
static void handle_get_cc_config(GUI_RPC_CONN& grc) {
string s;
int retval = read_file_string(CONFIG_FILE, s);
if (!retval) {
strip_whitespace(s);
grc.mfout.printf("%s\n", s.c_str());
}
}
static void handle_get_app_config(GUI_RPC_CONN& grc) {
string url;
string s;
char path[MAXPATHLEN];
while (!grc.xp.get_tag()) {
if (grc.xp.parse_string("url", url)) continue;
}
PROJECT* p = gstate.lookup_project(url.c_str());
if (!p) {
grc.mfout.printf("<error>no such project</error>");
return;
}
sprintf(path, "%s/%s", p->project_dir(), APP_CONFIG_FILE_NAME);
printf("path: %s\n", path);
int retval = read_file_string(path, s);
if (retval) {
grc.mfout.printf("<error>app_config.xml not found</error>\n");
} else {
strip_whitespace(s);
grc.mfout.printf("%s\n", s.c_str());
}
}
static void handle_set_cc_config(GUI_RPC_CONN& grc) {
int retval;
char buf[65536];
retval = grc.xp.element_contents("</set_cc_config>", buf, sizeof(buf));
if (!retval) {
if (strlen(buf)) {
FILE* f = boinc_fopen(CONFIG_FILE, "w");
if (f) {
fprintf(f, "%s\n", buf);
fclose(f);
retval = 0;
} else {
retval = ERR_FOPEN;
}
} else {
retval = boinc_delete_file(CONFIG_FILE);
}
}
if (retval) {
grc.mfout.printf("<status>%d</status>\n", retval);
} else {
grc.mfout.printf("<success/>\n");
}
}
static void handle_set_app_config(GUI_RPC_CONN& grc) {
APP_CONFIGS ac;
string url;
MSG_VEC mv;
LOG_FLAGS lf;
int parse_retval = -1;
while (!grc.xp.get_tag()) {
if (grc.xp.match_tag("app_config")) {
lf.init();
parse_retval = ac.parse(grc.xp, mv, lf);
} else if (grc.xp.parse_string("url", url)) {
continue;
}
}
if (parse_retval) {
grc.mfout.printf("<error>XML parse failed<error/>\n");
return;
}
PROJECT* p = gstate.lookup_project(url.c_str());
if (!p) {
grc.mfout.printf("<error>no such project<error/>\n");
return;
}
char path[MAXPATHLEN];
sprintf(path, "%s/app_config.xml", p->project_dir());
FILE* f = boinc_fopen(path, "w");
if (!f) {
msg_printf(p, MSG_INTERNAL_ERROR,
"Can't open app config file %s", path
);
grc.mfout.printf("<error>can't open app_config.xml file<error/>\n");
return;
}
MIOFILE mf;
mf.init_file(f);
ac.write(mf);
fclose(f);
grc.mfout.printf("<success/>\n");
}
static void handle_get_notices(GUI_RPC_CONN& grc) {
int seqno = 0;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_int("seqno", seqno)) continue;
}
notices.write(seqno, grc, false);
}
static void handle_get_notices_public(GUI_RPC_CONN& grc) {
int seqno = 0;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_int("seqno", seqno)) continue;
}
notices.write(seqno, grc, true);
}
static void handle_get_old_results(GUI_RPC_CONN& grc) {
print_old_results(grc.mfout);
}
static void handle_get_results(GUI_RPC_CONN& grc) {
bool active_only = false;
while (!grc.xp.get_tag()) {
if (grc.xp.parse_bool("active_only", active_only)) continue;
}
grc.mfout.printf("<results>\n");
gstate.write_tasks_gui(grc.mfout, active_only);
grc.mfout.printf("</results>\n");
}
static void handle_get_all_projects_list(GUI_RPC_CONN& grc) {
read_all_projects_list_file(grc);
}
static void handle_get_file_transfers(GUI_RPC_CONN& grc) {
gstate.write_file_transfers_gui(grc.mfout);
}
static void handle_read_global_prefs_override(GUI_RPC_CONN& grc) {
grc.mfout.printf("<success/>\n");
gstate.read_global_prefs();
gstate.request_schedule_cpus("Preferences override");
gstate.request_work_fetch("Preferences override");
}
static void handle_read_cc_config(GUI_RPC_CONN& grc) {
grc.mfout.printf("<success/>\n");
read_config_file(false);
cc_config.show();
log_flags.show();
gstate.set_ncpus();
process_gpu_exclusions();
// also reread app_config.xml files
//
check_app_config();
gstate.request_schedule_cpus("Core client configuration");
gstate.request_work_fetch("Core client configuration");
set_no_rsc_config();
}
static void handle_get_daily_xfer_history(GUI_RPC_CONN& grc) {
daily_xfer_history.write_xml(grc.mfout);
}
static bool complete_post_request(char* buf) {
if (strncmp(buf, "POST", 4)) return false;
char* p = strstr(buf, "Content-Length: ");
if (!p) return false;
p += strlen("Content-Length: ");
int n = atoi(p);
p = strstr(p, "\r\n\r\n");
if (!p) return false;
p += 4;
if ((int)strlen(p) < n) return false;
return true;
}
static void handle_set_language(GUI_RPC_CONN& grc) {
while (!grc.xp.get_tag()) {
if (grc.xp.parse_str("language", gstate.language, sizeof(gstate.language))) {
gstate.set_client_state_dirty("set_language");
grc.mfout.printf("<success/>\n");
return;
}
}
grc.mfout.printf("<error>no language found</error>\n");
}
static void handle_report_device_status(GUI_RPC_CONN& grc) {
DEVICE_STATUS d;
while (!grc.xp.get_tag()) {
if (grc.xp.match_tag("device_status")) {
int retval = d.parse(grc.xp);
if (log_flags.android_debug) {
if (retval) {
msg_printf(0, MSG_INFO,
"report_device_status RPC parse failed: %d", retval
);
} else {
msg_printf(0, MSG_INFO,
"Android device status:"
);
msg_printf(0, MSG_INFO,
"On AC: %s; on USB: %s; on WiFi: %s; user active: %s",
d.on_ac_power?"yes":"no",
d.on_usb_power?"yes":"no",
d.wifi_online?"yes":"no",
d.user_active?"yes":"no"
);
msg_printf(0, MSG_INFO,
"Battery: charge pct: %f; temp %f state %s",
d.battery_charge_pct,
d.battery_temperature_celsius,
battery_state_string(d.battery_state)
);
}
}
if (!retval) {
// if the GUI reported a device name, use it
//
if (strlen(d.device_name)) {
if (strcmp(d.device_name, gstate.host_info.domain_name)) {
safe_strcpy(gstate.host_info.domain_name, d.device_name);
gstate.set_client_state_dirty("Device name changed");
}
}
gstate.device_status = d;
gstate.device_status_time = gstate.now;
grc.mfout.printf("<success/>\n");
return;
}
}
}
grc.mfout.printf("<error/>\n");
}
int DEVICE_STATUS::parse(XML_PARSER& xp) {
while (!xp.get_tag()) {
if (xp.match_tag("/device_status")) {
return 0;
}
if (xp.parse_bool("on_ac_power", on_ac_power)) continue;
if (xp.parse_bool("on_usb_power", on_usb_power)) continue;
if (xp.parse_double("battery_charge_pct", battery_charge_pct)) continue;
if (xp.parse_int("battery_state", battery_state)) continue;
if (xp.parse_double("battery_temperature_celsius", battery_temperature_celsius)) continue;
if (xp.parse_bool("wifi_online", wifi_online)) continue;
if (xp.parse_bool("user_active", user_active)) continue;
if (xp.parse_str("device_name", device_name, sizeof(device_name))) continue;
}
return ERR_XML_PARSE;
}
// Some of the RPCs have empty-element request messages.
// We accept both <foo/> and <foo></foo>
//
#define match_req(buf, tag) (match_tag(buf, "<" tag ">") || match_tag(buf, "<" tag "/>"))
typedef void (*GUI_RPC_HANDLER)(GUI_RPC_CONN&);
struct GUI_RPC {
const char* req_tag;
char alt_req_tag[256];
GUI_RPC_HANDLER handler;
bool auth_required;
// operations that require authentication only for non-local clients.
// Use this only for information that should be available to people
// sharing this computer (e.g. what jobs are running)
// but not for anything sensitive (passwords etc.)
bool enable_network;
// RPCs that should enable network communication for 5 minutes,
// overriding other factors.
// Things like attaching projects, etc.
bool read_only; // doesn't modify the client's data structs
GUI_RPC(const char* req, GUI_RPC_HANDLER h, bool ar, bool en, bool ro) {
req_tag = req;
safe_strcpy(alt_req_tag, req);
safe_strcat(alt_req_tag, "/");
handler = h;
auth_required = ar;
enable_network = en;
read_only = ro;
}
};
// local auth required
// enable network
// read-only
GUI_RPC gui_rpcs[] = {
GUI_RPC("exchange_versions", handle_exchange_versions, false, false, true),
GUI_RPC("get_all_projects_list", handle_get_all_projects_list, false, false, true),
GUI_RPC("get_cc_status", handle_get_cc_status, false, false, true),
GUI_RPC("get_disk_usage", handle_get_disk_usage, false, false, true),
GUI_RPC("get_daily_xfer_history", handle_get_daily_xfer_history,
false, false, true),
GUI_RPC("get_file_transfers", handle_get_file_transfers, false, false, true),
GUI_RPC("get_host_info", handle_get_host_info, false, false, true),
GUI_RPC("get_messages", handle_get_messages, false, false, true),
GUI_RPC("get_message_count", handle_get_message_count, false, false, true),
GUI_RPC("get_newer_version", handle_get_newer_version, false, false, true),
GUI_RPC("get_notices_public", handle_get_notices_public, false, false, true),
GUI_RPC("get_old_results", handle_get_old_results, false, false, true),
GUI_RPC("get_project_status", handle_get_project_status, false, false, true),
GUI_RPC("get_results", handle_get_results, false, false, true),
GUI_RPC("get_screensaver_tasks", handle_get_screensaver_tasks, false, false, true),
GUI_RPC("get_simple_gui_info", handle_get_simple_gui_info, false, false, true),
GUI_RPC("get_state", handle_get_state, false, false, true),
GUI_RPC("get_statistics", handle_get_statistics, false, false, true),
// ops requiring local auth start here
GUI_RPC("abort_file_transfer", handle_abort_file_transfer, true, false, false),
GUI_RPC("abort_result", handle_abort_result, true, false, false),
GUI_RPC("acct_mgr_info", handle_acct_mgr_info, true, false, true),
GUI_RPC("get_app_config", handle_get_app_config, true, false, false),
GUI_RPC("get_cc_config", handle_get_cc_config, true, false, false),
GUI_RPC("get_global_prefs_file", handle_get_global_prefs_file, true, false, false),
GUI_RPC("get_global_prefs_override", handle_get_global_prefs_override,
true, false, false),
GUI_RPC("get_global_prefs_working", handle_get_global_prefs_working,
true, false, false),
GUI_RPC("get_notices", handle_get_notices, true, false, true),
GUI_RPC("get_project_init_status", handle_get_project_init_status,
true, false, false),
GUI_RPC("get_proxy_settings", handle_get_proxy_settings, true, false, true),
GUI_RPC("network_available", handle_network_available, true, false, false),
GUI_RPC("project_allowmorework", handle_project_allowmorework, true, false, false),
GUI_RPC("project_detach", handle_project_detach, true, false, false),
GUI_RPC("project_detach_when_done", handle_project_detach_when_done,
true, false, false),
GUI_RPC("project_dont_detach_when_done", handle_project_dont_detach_when_done,
true, false, false),
GUI_RPC("project_nomorework", handle_project_nomorework, true, false, false),
GUI_RPC("project_resume", handle_project_resume, true, false, false),
GUI_RPC("project_suspend", handle_project_suspend, true, false, false),
GUI_RPC("quit", handle_quit, true, false, false),
GUI_RPC("read_cc_config", handle_read_cc_config, true, false, false),
GUI_RPC("read_global_prefs_override", handle_read_global_prefs_override,
true, false, false),
GUI_RPC("report_device_status", handle_report_device_status, true, false, false),
GUI_RPC("resume_result", handle_resume_result, true, false, false),
GUI_RPC("run_benchmarks", handle_run_benchmarks, true, false, false),
GUI_RPC("set_app_config", handle_set_app_config, true, false, false),
GUI_RPC("set_cc_config", handle_set_cc_config, true, false, false),
GUI_RPC("set_global_prefs_override", handle_set_global_prefs_override,
true, false, false),
GUI_RPC("set_gpu_mode", handle_set_gpu_mode, true, false, false),
GUI_RPC("set_host_info", handle_set_host_info, true, false, false),
GUI_RPC("set_language", handle_set_language, true, false, false),
GUI_RPC("set_network_mode", handle_set_network_mode, true, false, false),
GUI_RPC("set_proxy_settings", handle_set_proxy_settings, true, false, false),
GUI_RPC("set_run_mode", handle_set_run_mode, true, false, false),
GUI_RPC("suspend_result", handle_suspend_result, true, false, false),
// ops requiring temporary network access start here
GUI_RPC("acct_mgr_rpc", handle_acct_mgr_rpc, true, true, false),
GUI_RPC("acct_mgr_rpc_poll", handle_acct_mgr_rpc_poll, true, true, false),
GUI_RPC("create_account", handle_create_account, true, true, false),
GUI_RPC("create_account_poll", handle_create_account_poll, true, true, false),
GUI_RPC("get_project_config", handle_get_project_config, true, true, false),
GUI_RPC("get_project_config_poll", handle_get_project_config_poll,
true, true, false),
GUI_RPC("lookup_account", handle_lookup_account, true, true, false),
GUI_RPC("lookup_account_poll", handle_lookup_account_poll, true, true, false),
GUI_RPC("project_attach", handle_project_attach, true, true, false),
GUI_RPC("project_attach_poll", handle_project_attach_poll, true, true, false),
GUI_RPC("project_reset", handle_project_reset, true, true, false),
GUI_RPC("project_update", handle_project_update, true, true, false),
GUI_RPC("retry_file_transfer", handle_retry_file_transfer, true, true, false),
};
// return nonzero only if we need to close the connection
//
static int handle_rpc_aux(GUI_RPC_CONN& grc) {
int retval = 0;
grc.mfin.init_buf_read(grc.request_msg);
if (grc.xp.get_tag()) return ERR_XML_PARSE; // parse <boinc_gui_rpc_request>
if (grc.xp.get_tag()) return ERR_XML_PARSE; // parse the request tag
for (unsigned int i=0; i<sizeof(gui_rpcs)/sizeof(GUI_RPC); i++) {
GUI_RPC& gr = gui_rpcs[i];
if (!grc.xp.match_tag(gr.req_tag) && !grc.xp.match_tag(gr.alt_req_tag)) {
continue;
}
if (gr.auth_required && grc.auth_needed) {
auth_failure(grc.mfout);
if (grc.sent_unauthorized) {
retval = ERR_AUTHENTICATOR;
}
grc.sent_unauthorized = true;
return retval;
}
if (gr.enable_network) {
gstate.gui_rpcs.time_of_last_rpc_needing_network = gstate.now;
}
(*gr.handler)(grc);
return 0;
}
grc.mfout.printf("<error>unrecognized op: %s</error>\n", grc.xp.parsed_tag);
return 0;
}
// return nonzero only if we need to close the connection
//
int GUI_RPC_CONN::handle_rpc() {
int n, retval=0;
char* p;
int left = GUI_RPC_REQ_MSG_SIZE - request_nbytes;
#ifdef _WIN32
n = recv(sock, request_msg+request_nbytes, left, 0);
#else
n = read(sock, request_msg+request_nbytes, left);
#endif
if (n <= 0) {
request_nbytes = 0;
return ERR_READ;
}
request_nbytes += n;
// buffer full?
if (request_nbytes >= GUI_RPC_REQ_MSG_SIZE) {
request_nbytes = 0;
return ERR_READ;
}
request_msg[request_nbytes] = 0;
if (!strncmp(request_msg, "OPTIONS", 7)) {
char buf[1024];
snprintf(buf, sizeof(buf),
"HTTP/1.1 200 OK\n"
"Server: BOINC client\n"
"Access-Control-Allow-Origin: *\n"
"Access-Control-Allow-Methods: POST, GET, OPTIONS\n"
"Content-Length: 0\n"
"Keep-Alive: timeout=2, max=100\n"
"Connection: Keep-Alive\n"
"Content-Type: text/plain\n\n"
);
send(sock, buf, (int)strlen(buf), 0);
request_nbytes = 0;
if (log_flags.gui_rpc_debug) {
msg_printf(0, MSG_INFO,
"[gui_rpc] processed OPTIONS"
);
}
return 0;
}
bool http_request;
if (complete_post_request(request_msg)) {
http_request = true;
} else {
p = strchr(request_msg, 3);
if (p) {
*p = 0;
http_request = false;
} else {
if (log_flags.gui_rpc_debug) {
msg_printf(0, MSG_INFO,
"[gui_rpc] partial GUI RPC Command = '%s'\n", request_msg
);
}
return 0;
}
}
request_nbytes = 0;
if (log_flags.gui_rpc_debug) {
msg_printf(0, MSG_INFO,
"[gui_rpc] GUI RPC Command = '%s'\n", request_msg
);
}
// Policy:
// - the first auth failure gets an error message; after that, disconnect
// - if we get an unexpected auth1 or auth2, disconnect
mfout.printf("<boinc_gui_rpc_reply>\n");
if (match_req(request_msg, "auth1")) {
if (got_auth1 && auth_needed) {
retval = ERR_AUTHENTICATOR;
} else {
handle_auth1(mfout);
got_auth1 = true;
}
} else if (match_req(request_msg, "auth2")) {
if ((!got_auth1 || got_auth2) && auth_needed) {
retval = ERR_AUTHENTICATOR;
} else {
retval = handle_auth2(request_msg, mfout);
got_auth2 = true;
}
} else if (auth_needed && !is_local) {
auth_failure(mfout);
if (sent_unauthorized) {
retval = ERR_AUTHENTICATOR;
}
sent_unauthorized = true;
} else {
retval = handle_rpc_aux(*this);
}
mfout.printf("</boinc_gui_rpc_reply>\n\003");
mout.get_buf(p, n);
if (http_request) {
char buf[1024];
snprintf(buf, sizeof(buf),
"HTTP/1.1 200 OK\n"
"Date: Fri, 31 Dec 1999 23:59:59 GMT\n"
"Server: BOINC client\n"
"Connection: close\n"
"Content-Type: text/xml; charset=utf-8\n"
"Content-Length: %d\n\n"
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n",
n
);
send(sock, buf, (int)strlen(buf), 0);
}
if (p) {
send(sock, p, n, 0);
p[n-1]=0; // replace 003 with NULL
if (log_flags.gui_rpc_debug) {
if (n > 128) p[128] = 0;
msg_printf(0, MSG_INFO,
"[gui_rpc] GUI RPC reply: '%s'\n", p
);
}
free(p);
}
return retval;
}
|