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 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
|
/******************************************************************************
(c) 2001-2005 Christine Caulfield christine.caulfield@googlemail.com
(c) 2003 Dmitri Popov
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program 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 General Public License for more details.
******************************************************************************/
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#ifdef HAVE_NET_ETHERNET_H
#include <net/ethernet.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <syslog.h>
#include <ctype.h>
#include <regex.h>
#include <stdlib.h>
#include <utmp.h>
#include <grp.h>
#include <signal.h>
#include <assert.h>
#include <netinet/in.h>
#include <list>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <iomanip>
#include "lat.h"
#include "utils.h"
#include "session.h"
#include "localport.h"
#include "connection.h"
#include "circuit.h"
#include "latcpcircuit.h"
#include "llogincircuit.h"
#include "server.h"
#include "services.h"
#include "lat_messages.h"
#include "dn_endian.h"
#if defined (__APPLE__) && !defined(_SOCKLEN_T)
typedef int socklen_t;
#endif
// Remove any dangling symlinks in the /dev/lat directory
void LATServer::tidy_dev_directory()
{
DIR *dir;
struct dirent *de;
char current_dir[PATH_MAX];
getcwd(current_dir, sizeof(current_dir));
chdir(LAT_DIRECTORY);
dir = opendir(LAT_DIRECTORY);
if (!dir) return; // Doesn't exist - this is OK
while ( (de = readdir(dir)) )
{
if (de->d_name[0] != '.')
unlink(de->d_name);
}
closedir(dir);
chdir(current_dir);
}
unsigned char *LATServer::get_local_node(void)
{
unsigned int i;
if (local_name[0] == '\0')
{
struct utsname uts;
char *trailer;
uname(&uts);
if (strchr(uts.nodename, '.'))
*strchr(uts.nodename, '.') = '\0';
// This gets rid of the name-Computer bit on Darwin
trailer = strstr(uts.nodename, "-Computer");
if (trailer)
*trailer = '\0';
// LAT node names must be <= 16 characters long
if (strlen(uts.nodename) > 16)
uts.nodename[16] = '\0';
strcpy((char *)local_name, uts.nodename);
// Make it all upper case
for (i=0; i<strlen((char *)local_name); i++)
{
if (islower(local_name[i])) local_name[i] = toupper(local_name[i]);
}
}
return local_name;
}
void LATServer::alarm_signal(int sig)
{
if (Instance()->alarm_mode == 0)
{
Instance()->send_service_announcement(sig);
}
Instance()->send_solicit_messages(sig);
}
// Send ENQUIRY for a node - mainly needed for DS90L servers that
// don't advertise.
void LATServer::send_enq(const char *node)
{
unsigned char packet[1600];
static unsigned char id = 1;
LAT_Enquiry *enqmsg = (LAT_Enquiry *)packet;
int ptr = sizeof(LAT_Enquiry);
enqmsg->cmd = LAT_CCMD_ENQUIRE;
enqmsg->dummy = 0;
enqmsg->hiver = LAT_VERSION;
enqmsg->lover = LAT_VERSION;
enqmsg->latver = LAT_VERSION;
enqmsg->latver_eco = LAT_VERSION_ECO;
enqmsg->mtu = dn_htons(1500);
enqmsg->id = id++;
enqmsg->retrans_timer = 75; // * 10ms - give it more time to respond
add_string(packet, &ptr, (const unsigned char *)node);
packet[ptr++] = 1; /* Length of group data */
packet[ptr++] = 1; /* Group mask */
add_string(packet, &ptr, local_name);
packet[ptr++] = 0x00;
packet[ptr++] = 0x00;
packet[ptr++] = 0x00;
packet[ptr++] = 0x00;
/* This is the LAT multicast address */
static unsigned char addr[6] = { 0x09, 0x00, 0x2b, 0x00, 0x00, 0x0f };
debuglog(("send_enq : node: %s, local_name: %s\n", node, local_name));
for (int i=0; i<num_interfaces;i++)
{
if (iface->send_packet(interface_num[i], addr, packet, ptr) < 0)
{
debuglog(("Error send packet\n"));
interface_error(interface_num[i], errno);
}
else
{
interface_errs[interface_num[i]] = 0; // Clear errors
}
}
LATServices::Instance()->touch_dummy_node_respond_counter(node);
}
void LATServer::add_slave_node(const char *node_name)
{
sig_blk_t _block(SIGALRM);
for (std::list<std::string>::iterator iter = slave_nodes.begin();
iter != slave_nodes.end();
iter++)
{
if (*iter == node_name) {
// do not duplicate nodes
return;
}
}
slave_nodes.push_front(node_name);
}
/* Called on the multicast timer - advertise our service on the LAN */
void LATServer::send_service_announcement(int sig)
{
unsigned char packet[1600];
int ptr;
struct utsname uinfo;
char *myname;
// Only send it if we have some services
if (servicelist.size())
{
LAT_ServiceAnnounce *announce = (LAT_ServiceAnnounce *)packet;
ptr = sizeof(LAT_ServiceAnnounce);
announce->cmd = LAT_CCMD_SERVICE;
announce->circuit_timer = circuit_timer;
announce->hiver = LAT_VERSION;
announce->lover = LAT_VERSION;
announce->latver = LAT_VERSION;
announce->latver_eco = LAT_VERSION_ECO;
announce->incarnation = --multicast_incarnation;
announce->flags = 0x1f;
announce->mtu = dn_htons(1500);
announce->multicast_timer = multicast_timer;
if (do_shutdown)
{
announce->node_status = 3; // Not accepting connections
}
else
{
announce->node_status = 2; // Accepting connections
}
// Send group codes
if (groups_set)
{
announce->group_length = 32;
memcpy(&packet[ptr], groups, 32);
ptr += 32;
announce->flags |= 1;
}
else
{
announce->group_length = 1;
packet[ptr++] = 01;
}
/* Get host info */
uname(&uinfo);
// Node name
myname = (char*)get_local_node();
packet[ptr++] = strlen(myname);
strcpy((char*)packet+ptr, myname);
ptr += strlen(myname);
// Greeting
packet[ptr++] = strlen((char*)greeting);
strcpy((char*)packet+ptr, (char*)greeting);
ptr += strlen((char*)greeting);
// Number of services
packet[ptr++] = servicelist.size();
std::list<serviceinfo>::iterator i(servicelist.begin());
for (; i != servicelist.end(); i++)
{
// Service rating
unsigned char real_rating;
if (i->get_static())
{
real_rating = i->get_rating();
}
else
{
/* Calculate dynamic rating */
real_rating = (unsigned char)(i->get_rating() / (get_loadavg()+1.0));
debuglog(("Dynamic service rating is %d\n", real_rating));
}
packet[ptr++] = real_rating;
// Service name
const std::string name = i->get_name();
packet[ptr++] = name.length();
strcpy((char *)packet+ptr, i->get_name().c_str());
ptr += name.length();
// Service Identification
std::string id = i->get_id();
if (id.length() == 0)
{
// Default service identification string
char stringbuf[1024];
sprintf(stringbuf, "%s %s", uinfo.sysname, uinfo.release);
id = std::string(stringbuf);
}
packet[ptr++] = id.length();
strcpy((char *)packet+ptr, id.c_str());
ptr += id.length();
// Make sure the service table knows about all our services
unsigned char dummy_macaddr[6];
memset(dummy_macaddr, 0, sizeof(dummy_macaddr));
if (!do_shutdown)
LATServices::Instance()->add_service(std::string((char*)get_local_node()),
name,
id, real_rating, 0, dummy_macaddr);
}
// Not sure what node service classes are
// probably somthing to do with port services and stuff.
packet[ptr++] = 0x01; // Node service classes length
packet[ptr++] = 0x01; // Node service classes
packet[ptr++] = 0x00;
packet[ptr++] = 0x00;
unsigned char addr[6];
/* This is the LAT multicast address */
addr[0] = 0x09;
addr[1] = 0x00;
addr[2] = 0x2b;
addr[3] = 0x00;
addr[4] = 0x00;
addr[5] = 0x0f;
for (int i=0; i<num_interfaces;i++)
{
if (iface->send_packet(interface_num[i], addr, packet, ptr) < 0)
{
debuglog(("sending service announcement, send error: %d\n", errno));
interface_error(interface_num[i], errno);
}
else
interface_errs[interface_num[i]] = 0; // Clear errors
}
}
/* Send it every minute */
signal(SIGALRM, &alarm_signal);
alarm(multicast_timer);
}
// Send solicit messages to slave nodes
void LATServer::send_solicit_messages(int sig)
{
static unsigned int counter = 0;
static unsigned int last_list_size = 0;
if (alarm_mode == 0)
{
counter = 0;
alarm_mode = 1;
debuglog(("set alarm_mode to 1\n"));
if (!known_slave_nodes.empty())
{
std::string known_node = known_slave_nodes.front();
known_slave_nodes.pop_front();
slave_nodes.push_back(known_node);
debuglog(("known(%d) => slave(%d) : %s\n", known_slave_nodes.size(),
slave_nodes.size(), known_node.c_str()));
if (slave_nodes.size() == 1)
{
alarm_mode = 0;
debuglog(("set alarm_mode to 0 - one slave\n"));
send_enq(slave_nodes.front().c_str());
// alarm() is already charged by send_service_announcement()
return;
}
}
}
else
{
if (slave_nodes.size() < last_list_size)
{
counter -= last_list_size - slave_nodes.size();
}
}
if (slave_nodes.size() > counter && !slave_nodes.empty())
{
std::string node_name = slave_nodes.front();
slave_nodes.pop_front();
slave_nodes.push_back(node_name);
send_enq(node_name.c_str());
alarm(1);
counter++;
last_list_size = slave_nodes.size();
}
else
{
alarm_mode = 0;
//well, it's not quite correct, I know...
alarm(counter >= multicast_timer ? 1 : multicast_timer - counter);
debuglog(("set alarm_mode to 0, timer %d\n", counter >= multicast_timer ? 1 : multicast_timer - counter));
}
}
// Log an error against an interface. If we get three of these
// then we remove it.
// If that means we have no interfaces, then closedown.
void LATServer::interface_error(int ifnum, int err)
{
syslog(LOG_ERR, "Error on interface %s: %s\n", iface->ifname(ifnum).c_str(), strerror(err));
// Too many errors, remove it
if (++interface_errs[ifnum] > 3)
{
int i,j;
syslog(LOG_ERR, "Interface will be removed from LATD\n");
// Look for it
for (i=0; i<num_interfaces; i++)
{
if (interface_num[i] == ifnum) goto remove_if;
}
// Ugh, didn't find it. that can't be right!
if (i>=num_interfaces)
{
syslog(LOG_ERR, "Don't seem to have a reference to this interface....\n");
return;
}
remove_if:
// Shuffle the list down
for (j=i; j<num_interfaces-1; j++)
{
interface_num[j] = interface_num[j+1];
}
if (--num_interfaces == 0)
{
syslog(LOG_ERR, "No valid interfaces left. LATD closing down\n");
// No point in going down cleanly as that just sends network messages
// and we have nowhere to send then through.
exit(9);
}
}
}
/* Main loop */
void LATServer::run()
{
int status;
// Bind interfaces
for (int i=0; i<num_interfaces;i++)
{
iface->set_lat_multicast(interface_num[i]);
}
// Add it/them to the sockets list
if (iface->one_fd_per_interface())
{
for (int i=0; i<num_interfaces;i++)
{
fdlist.push_back(fdinfo(iface->get_fd(interface_num[i]), 0, LAT_SOCKET));
}
}
else
{
fdlist.push_back(fdinfo(iface->get_fd(0), 0, LAT_SOCKET));
}
// Open LATCP socket
unlink(LATCP_SOCKNAME);
latcp_socket = socket(PF_UNIX, SOCK_STREAM, 0);
if (latcp_socket < 0)
{
syslog(LOG_ERR, "Can't create latcp socket: %m");
exit(1);
}
struct sockaddr_un sockaddr;
strcpy(sockaddr.sun_path, LATCP_SOCKNAME);
sockaddr.sun_family = AF_UNIX;
if (bind(latcp_socket, (struct sockaddr *)&sockaddr, sizeof(sockaddr)))
{
syslog(LOG_ERR, "can't bind latcp socket: %m");
exit(1);
}
if (listen(latcp_socket, 1) != 0)
{
syslog(LOG_ERR, "listen latcp: %m");
}
// Make sure only root can talk to us via the latcp socket
chmod(LATCP_SOCKNAME, 0600);
fdlist.push_back(fdinfo(latcp_socket, 0, LATCP_RENDEZVOUS));
// Open llogin socket
unlink(LLOGIN_SOCKNAME);
llogin_socket = socket(PF_UNIX, SOCK_STREAM, 0);
if (llogin_socket < 0)
{
syslog(LOG_ERR, "Can't create llogin socket: %m");
exit(1);
}
strcpy(sockaddr.sun_path, LLOGIN_SOCKNAME);
sockaddr.sun_family = AF_UNIX;
if (bind(llogin_socket, (struct sockaddr *)&sockaddr, sizeof(sockaddr)))
{
syslog(LOG_ERR, "can't bind llogin socket: %m");
exit(1);
}
if (listen(llogin_socket, 1) != 0)
{
syslog(LOG_ERR, "listen llogin: %m");
}
// Make sure everyone can use it
chmod(LLOGIN_SOCKNAME, 0666);
fdlist.push_back(fdinfo(llogin_socket, 0, LLOGIN_RENDEZVOUS));
// Don't start sending service announcements
// until we get an UNLOCK message from latcp.
// We rely on POSIX's select() behaviour in that we use the
// time left in the timeval parameter to make sure the circuit timer
// goes off at a reasonably predictable interval.
#ifndef __linux__
struct timeval next_circuit_time;
next_circuit_time.tv_sec = 0;
next_circuit_time.tv_usec = 0;
#endif
struct timeval tv = {0, circuit_timer*10000};
time_t last_node_expiry = time(NULL);
do_shutdown = false;
do
{
fd_set fds;
FD_ZERO(&fds);
std::list<fdinfo>::iterator i(fdlist.begin());
for (; i != fdlist.end(); i++)
{
if (i->active())
FD_SET(i->get_fd(), &fds);
}
// Only Linux seems to have this select behaviour...
// for BSDs we need to work our for ourselves the delay
// until the next circuit timer tick.
#ifndef __linux__
struct timeval now;
gettimeofday(&now, NULL);
if (next_circuit_time.tv_sec == 0)
{
next_circuit_time = now;
next_circuit_time.tv_usec += circuit_timer*10000;
next_circuit_time.tv_sec += (next_circuit_time.tv_usec / 1000000);
next_circuit_time.tv_usec = (next_circuit_time.tv_usec % 1000000);
}
tv = next_circuit_time;
if (tv.tv_sec < now.tv_sec
|| (tv.tv_sec == now.tv_sec
&& tv.tv_usec <= now.tv_usec))
{
tv.tv_sec = tv.tv_usec = 0;
}
else
{
tv.tv_sec -= now.tv_sec;
if ((tv.tv_usec -= now.tv_usec) < 0)
{
tv.tv_sec--;
tv.tv_usec += 1000000;
}
}
#endif
status = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
if (status < 0)
{
if (errno != EINTR)
{
syslog(LOG_WARNING, "Error in select: %m");
debuglog(("Error in select: %s\n", strerror(errno)));
do_shutdown = true;
}
}
else
{
if (status == 0) // Circuit timer
{
for (int i=1; i<MAX_CONNECTIONS; i++)
if (connections[i])
connections[i]->circuit_timer();
tv.tv_usec = circuit_timer*10000;
// Is it time we expired some more nodes ?
if (time(NULL) - last_node_expiry > 15)
LATServices::Instance()->expire_nodes();
#ifndef __linux__
next_circuit_time.tv_sec = 0;
#endif
}
else
{
// Unix will never scale while this is necessary
std::list<fdinfo>::iterator fdl(fdlist.begin());
for (; fdl != fdlist.end(); fdl++)
{
if (fdl->active() &&
FD_ISSET(fdl->get_fd(), &fds))
{
process_data(*fdl);
}
}
}
}
// Tidy deleted sessions
if (!dead_session_list.empty())
{
std::list<deleted_session>::iterator dsl(dead_session_list.begin());
for (; dsl != dead_session_list.end(); dsl++)
{
delete_entry(*dsl);
}
dead_session_list.clear();
}
// Tidy deleted connections
if (!dead_connection_list.empty())
{
std::list<int>::iterator dcl(dead_connection_list.begin());
for (; dcl != dead_connection_list.end(); dcl++)
{
if (connections[*dcl])
{
delete connections[*dcl];
connections[*dcl] = NULL;
}
}
dead_connection_list.clear();
}
} while (!do_shutdown);
send_service_announcement(-1); // Say we are unavailable
close(latcp_socket);
unlink(LATCP_SOCKNAME);
unlink(LLOGIN_SOCKNAME);
tidy_dev_directory();
}
/* LAT socket has something for us */
void LATServer::read_lat(int sock)
{
unsigned char buf[1600];
unsigned char macaddr[6];
int len;
int i;
int ifn;
bool more = true;
LAT_Header *header = (LAT_Header *)buf;
while (more)
{
len = iface->recv_packet(sock, ifn, macaddr, buf, sizeof(buf), more);
if (len == 0)
continue; // Probably a rogue packet
if (len < 0)
{
if (errno != EINTR && errno != EAGAIN)
{
syslog(LOG_ERR, "recvmsg: %m");
return;
}
}
// Not listening yet, but we must read the message otherwise we
// we will spin until latcp unlocks us.
if (locked)
continue;
// Parse & dispatch it.
switch(header->cmd)
{
case LAT_CCMD_SREPLY:
case LAT_CCMD_SDATA:
case LAT_CCMD_SESSION:
{
debuglog(("session cmd for connid %d\n", header->remote_connid));
LATConnection *conn = NULL;
if(header->remote_connid < MAX_CONNECTIONS)
{
conn = connections[header->remote_connid];
}
if (conn)
{
conn->process_session_cmd(buf, len, macaddr);
}
else
{
// Message format error
send_connect_error(2, header, ifn, macaddr);
}
}
break;
case LAT_CCMD_CONNECT:
{
// Make a new connection
// Check that the connection is really for one of our services
unsigned char name[256];
int ptr = sizeof(LAT_Start);
get_string(buf, &ptr, name);
debuglog(("got connect for node %s\n", name));
if (strcmp((char *)name, (char *)get_local_node()))
{
// How the &?* did that happen?
send_connect_error(2, header, ifn, macaddr);
continue;
}
// Make a new connection.
if ( ((i=make_new_connection(buf, len, ifn, header, macaddr) )) > 0)
{
debuglog(("Made new connection: %d\n", i));
connections[i]->send_connect_ack();
}
}
break;
case LAT_CCMD_CONACK:
{
LATConnection *conn = NULL;
debuglog(("Got connect ACK for %d\n", header->remote_connid));
if (header->remote_connid < MAX_CONNECTIONS)
{
conn = connections[header->remote_connid];
}
if (conn)
{
conn->got_connect_ack(buf);
}
else
{
// Insufficient resources
send_connect_error(7, header, ifn, macaddr);
}
}
break;
case LAT_CCMD_CONREF:
case LAT_CCMD_DISCON:
{
debuglog(("Disconnecting connection %d: status %x(%s)\n",
header->remote_connid,
buf[sizeof(LAT_Header)],
lat_messages::connection_disconnect_msg(buf[sizeof(LAT_Header)]) ));
if (header->remote_connid < MAX_CONNECTIONS)
{
LATConnection *conn = connections[header->remote_connid];
if (conn)
{
// We don't delete clients, we just quiesce them.
if (conn->isClient())
{
conn->disconnect_client();
if (conn->num_clients() == 0)
{
delete conn;
connections[header->remote_connid] = NULL;
}
}
else
{
delete conn;
connections[header->remote_connid] = NULL;
}
}
}
else
{
// Message format error
send_connect_error(2, header, ifn, macaddr);
}
}
break;
case LAT_CCMD_SERVICE:
// Keep a list of known services
add_services(buf, len, ifn, macaddr);
break;
case LAT_CCMD_ENQUIRE:
reply_to_enq(buf, len, ifn, macaddr);
break;
case LAT_CCMD_ENQREPLY:
got_enqreply(buf, len, ifn, macaddr);
break;
case LAT_CCMD_STATUS:
forward_status_messages(buf, len);
break;
// Request for a reverse-LAT connection.
case LAT_CCMD_COMMAND:
process_command_msg(buf, len, ifn, macaddr);
break;
}
}
}
// Add an FD to the list of FDs to listen to
void LATServer::add_fd(int fd, fd_type type)
{
std::list<fdinfo>::iterator fdi;
debuglog(("Add_fd: %d\n", fd));
fdi = find(fdlist.begin(), fdlist.end(), fd);
if (fdi != fdlist.end()) return; // Already exists
fdlist.push_back(fdinfo(fdinfo(fd, NULL, type)));
}
// Remove FD from the FD list
void LATServer::remove_fd(int fd)
{
std::list<fdinfo>::iterator fdi;
debuglog(("remove_fd: %d\n", fd));
fdi = find(fdlist.begin(), fdlist.end(), fd);
if (fdi == fdlist.end()) return; // Does not exist
fdlist.remove(*fdi);
}
// Change the DISABLED state of a PTY fd
void LATServer::set_fd_state(int fd, bool disabled)
{
std::list<fdinfo>::iterator fdi;
debuglog(("set_fd_state: %d, %d\n", fd, disabled));
fdi = find(fdlist.begin(), fdlist.end(), fd);
if (fdi == fdlist.end()) return; // Does not exist
fdi->set_disabled(disabled);
}
/* Send a LAT message to a specified MAC address */
int LATServer::send_message(unsigned char *buf, int len, int interface, unsigned char *macaddr)
{
if (len < 46) len = 46; // Minimum packet length
if (len%2) len++; // Must be an even number
#ifdef PACKET_LOSS
static int c=0;
if ((++c % PACKET_LOSS) == 0) {
debuglog(("NOT SENDING THIS MESSAGE!\n"));
return 0;
}
#endif
if (interface == 0) // Send to all
{
for (int i=0; i<num_interfaces;i++)
{
if (iface->send_packet(interface_num[i], macaddr, buf, len) < 0)
{
interface_error(interface_num[i], errno);
}
else
interface_errs[interface_num[i]] = 0; // Clear errors
}
}
else
{
if (iface->send_packet(interface, macaddr, buf, len) < 0)
{
interface_error(interface, errno);
return -1;
}
interface_errs[interface] = 0; // Clear errors
}
return 0;
}
/* Get the system load average */
double LATServer::get_loadavg(void)
{
double avg[3];
if (getloadavg(avg, 3) > 0)
{
return avg[0];
}
else
{
return 0;
}
}
// Forward status messages to their recipient connection objects.
void LATServer::forward_status_messages(unsigned char *inbuf, int len)
{
int ptr = sizeof(LAT_Status);
unsigned char node[256];
get_string(inbuf, &ptr, node);
// Forward all the StatusEntry messages
while (ptr <= len)
{
if (ptr%2) ptr++; // Word aligned
LAT_StatusEntry *entry = (LAT_StatusEntry *)&inbuf[ptr];
if (entry->length == 0) break;
ptr += sizeof(LAT_StatusEntry);
get_string(inbuf, &ptr, node); // Service name
ptr += inbuf[ptr]+1; // Past port name
ptr += inbuf[ptr]+1; // Past service description
if (entry->request_id < MAX_CONNECTIONS &&
connections[entry->request_id])
connections[entry->request_id]->got_status(node, entry);
}
}
/* Reply to an ENQUIRE message with our MAC address & service name */
void LATServer::reply_to_enq(unsigned char *inbuf, int len, int interface,
unsigned char *remote_mac)
{
int inptr, outptr, i;
unsigned char outbuf[1600];
unsigned char req_service[1600];
LAT_Header *outhead = (LAT_Header *)outbuf;
LAT_Header *inhead = (LAT_Header *)inbuf;
inptr = sizeof(LAT_Header)+4;
/* This is the service being enquired about */
get_string(inbuf, &inptr, req_service);
debuglog(("got ENQ for %s\n", req_service));
// Ignore empty requests:: TODO: is this right?? - maybe we issue
// a response with all our services in it.
if (strlen((char*)req_service) == 0) return;
unsigned char reply_macaddr[6];
unsigned char *reply_node;
// See if it is for us if we're not doing responder work.
if (!responder)
{
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), (char *)req_service);
if (sii == servicelist.end()) return; // Not ours
}
std::string node;
if (LATServices::Instance()->get_highest(std::string((char *)req_service),
node, reply_macaddr, &interface))
{
reply_node = (unsigned char *)node.c_str();
}
else
{
return; // nevver 'eard of 'im.
}
debuglog(("Sending ENQ reply for %s\n", req_service));
inptr += 2;
outptr = sizeof(LAT_Header);
outbuf[outptr++] = 0x93;
outbuf[outptr++] = 0x07;
outbuf[outptr++] = 0x00;
outbuf[outptr++] = 0x00;
/* The service's MAC address */
outbuf[outptr++] = 0x06;
outbuf[outptr++] = 0x00;
for (i=0; i<5; i++)
outbuf[outptr++] = reply_macaddr[i];
/* Don't know what this is */
outbuf[outptr++] = 0x14;
outbuf[outptr++] = 0x00;
add_string(outbuf, &outptr, reply_node);
outbuf[outptr++] = 0x01;
outbuf[outptr++] = 0x01;
add_string(outbuf, &outptr, req_service);
outhead->cmd = LAT_CCMD_ENQREPLY;
outhead->num_slots = 0;
outhead->remote_connid = inhead->remote_connid;
outhead->local_connid = inhead->local_connid;
outhead->sequence_number = inhead->sequence_number;
outhead->ack_number = inhead->ack_number;
send_message(outbuf, outptr, interface, remote_mac);
}
void LATServer::shutdown()
{
do_shutdown = true;
}
void LATServer::init(bool _static_rating, int _rating,
char *_service, char *_greeting, char **_interfaces,
int _verbosity, int _timer)
{
// Server is locked until latcp has finished
locked = true;
/* Initialise the platform-specific interface code */
iface = LATinterfaces::Create();
if (iface->Start(LATinterfaces::ProtoLAT) == -1)
{
syslog(LOG_ERR, "Can't create LAT protocol socket: %m\n");
exit(1);
}
#ifdef ENABLE_DEFAULT_SERVICE
// Add the default session
servicelist.push_back(serviceinfo(_service,
_rating,
_static_rating));
#endif
strcpy((char *)greeting, _greeting);
verbosity = _verbosity;
// Convert all the interface names to numbers and check they are usable
if (_interfaces[0])
{
int i=0;
while (_interfaces[i])
{
interface_num[num_interfaces] = iface->find_interface(_interfaces[i]);
if (interface_num[num_interfaces] == -1)
{
syslog(LOG_ERR, "Can't use interface %s: ignored\n", _interfaces[i]);
}
else
{
interface_errs[num_interfaces] = 0; // Clear errors
num_interfaces++;
}
i++;
}
if (num_interfaces == 0)
{
syslog(LOG_ERR, "No usable interfaces, latd exiting.\n");
exit(9);
}
}
else
{
iface->get_all_interfaces(interface_num, num_interfaces);
}
// Remove any old /dev/lat symlinks
tidy_dev_directory();
// Save these two for any newly added services
rating = _rating;
static_rating = _static_rating;
next_connection = 1;
multicast_incarnation = 0;
circuit_timer = _timer/10;
local_name[0] = '\0'; // Use default node name
memset(connections, 0, sizeof(connections));
// Enable user group 0
memset(user_groups, 0, 32);
user_groups[0] = 1;
// Look in /etc/group for a group called "lat"
struct group *gr = getgrnam("lat");
if (gr)
lat_group = gr->gr_gid;
else
lat_group = 0;
}
// Create a new connection object for this remote node.
int LATServer::make_new_connection(unsigned char *buf, int len,
int interface,
LAT_Header *header,
unsigned char *macaddr)
{
int i;
i = get_next_connection_number();
if (i >= 0)
{
next_connection = i+1;
connections[i] = new LATConnection(i, buf, len, interface,
header->sequence_number,
header->ack_number,
macaddr);
}
else
{
// Number of virtual circuits exceeded
send_connect_error(9, header, interface, macaddr);
return -1;
}
return i;
}
void LATServer::delete_session(int conn, unsigned char id, int fd)
{
// Add this to the list and delete them later
deleted_session s(LOCAL_PTY, conn, id, fd);
dead_session_list.push_back(s);
}
void LATServer::delete_connection(int conn)
{
// Add this to the list and delete them later
debuglog(("connection %d pending deletion\n", conn));
dead_connection_list.push_back(conn);
}
// Got a reply from a DS90L - add it to the services list in a dummy service
void LATServer::got_enqreply(unsigned char *buf, int len, int interface, unsigned char *macaddr)
{
int off = 14;
unsigned char node_addr[6];
char node_description[256];
char node_name[256];
memset(node_addr, 0x00, sizeof(node_addr));
if (0 == memcmp(node_addr, buf + off, sizeof(node_addr)))
{
return;
}
memset(node_addr, 0xFF, sizeof(node_addr));
if (0 == memcmp(node_addr, buf + off, sizeof(node_addr)))
{
return;
}
// Node MAC address
memcpy(node_addr, buf + off, sizeof(node_addr));
// Skip destination node name
off = 22;
off += buf[off] + 1;
// Skip node groups
off += buf[off] + 1;
get_string(buf, &off, (unsigned char*)node_name);
get_string(buf, &off, (unsigned char*)node_description);
#if defined(debuglog)
if (memcmp(node_addr, macaddr, sizeof(node_addr))) {
debuglog(("got_enqreply : macaddr is different : %02hhX-%02hhX-%02hhX-%02hhX-%02hhX-%02hhX\n",
macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]));
}
#endif
// Add it to the dummy service.
debuglog(("got_enqreply : node: '%s' : %02hhX-%02hhX-%02hhX-%02hhX-%02hhX-%02hhX\n",
node_name,
node_addr[0], node_addr[1], node_addr[2], node_addr[3], node_addr[4], node_addr[5]));
LATServices::Instance()->add_service(std::string(node_name),
std::string(""), // Dummy service name
std::string(node_description),
0,
interface, node_addr);
{
sig_blk_t _block(SIGALRM);
std::list<std::string>::iterator sl_iter;
sl_iter = find(slave_nodes.begin(), slave_nodes.end(), node_name);
if (sl_iter != slave_nodes.end()) {
debuglog(("got_enqreply : to remove from slave(%d), node: '%s'\n",
slave_nodes.size(), sl_iter->c_str()));
slave_nodes.erase(sl_iter);
}
sl_iter = find(known_slave_nodes.begin(), known_slave_nodes.end(), node_name);
if (sl_iter == known_slave_nodes.end()) {
known_slave_nodes.push_back(node_name);
debuglog(("got_enqreply : added to known(%d), node: '%s'\n",
known_slave_nodes.size(),
node_name));
}
}
}
// Got a COMMAND message from a remote node. it probably wants
// to set up a reverse-LAT session to us.
void LATServer::process_command_msg(unsigned char *buf, int len, int interface, unsigned char *macaddr)
{
LAT_Command *msg = (LAT_Command *)buf;
int ptr = sizeof(LAT_Command);
unsigned char remnode[256];
unsigned char remport[256];
unsigned char service[256];
unsigned char portname[256];
// TODO: Check the params in the LAT_Command message...
debuglog(("REVLAT cmd: %d, opcode:%d request_id: %d\n", msg->cmd, msg->opcode, msg->request_id));
ptr += buf[ptr]+1; // Skip past groups for now.
ptr += buf[ptr]+1; // Skip past groups for now.
// Get the remote node name.
get_string(buf, &ptr, remnode);
get_string(buf, &ptr, remport);
ptr += buf[ptr]+1; // Skip past greeting...
get_string(buf, &ptr, service);
get_string(buf, &ptr, portname);
debuglog(("Got request for reverse-LAT connection to %s/%s from %s/%s\n",
service, portname, remnode, remport));
// Make a new connection
// We can reuse connections here...
int connid = find_connection_by_node((char *)remnode);
if (connid == -1)
{
connid = get_next_connection_number();
connections[connid] = new LATConnection(connid,
(char *)service,
(char *)remport,
(char *)portname,
(char *)remnode,
false,
true);
}
// Make a reverse-session and connect it.
LAT_SessionStartCmd startcmd;
memset(&startcmd, 0, sizeof(startcmd));
startcmd.dataslotsize = 255;
if (connections[connid]->create_reverse_session((const char *)service,
(const char *)&startcmd,
dn_ntohs(msg->request_id),
interface, macaddr) == -1)
{
// If we created this connection just for the new session
// then get rid of it.
if (connections[connid]->num_clients() == 0)
delete_connection(connid);
}
}
// Add services received from a service announcement multicast
void LATServer::add_services(unsigned char *buf, int len, int interface, unsigned char *macaddr)
{
LAT_ServiceAnnounce *announce = (LAT_ServiceAnnounce *)buf;
int ptr = sizeof(LAT_ServiceAnnounce);
unsigned char service_groups[32];
unsigned char nodename[256];
unsigned char greeting[255];
unsigned char service[255];
unsigned char ident[255];
// Set the groups to all zeros initially
memset(service_groups, 0, sizeof(service_groups));
// Make sure someone isn't pulling our leg.
if (announce->group_length > 32)
announce->group_length = 32;
// Get group numbers
memcpy(service_groups, buf+ptr, announce->group_length);
// Compare with our user groups mask (which is always either completely
// empty or the full 32 bytes)
int i;
bool gotone = false;
for (i=0; i<announce->group_length; i++)
{
if (user_groups[i] & service_groups[i])
{
gotone = true;
break;
}
}
if (!gotone)
{
debuglog(("remote node not in our user groups list\n"));
return;
}
ptr += announce->group_length;
get_string(buf, &ptr, nodename);
get_string(buf, &ptr, greeting);
int num_services = buf[ptr++];
debuglog(("service announcement: status = %d\n", announce->node_status));
// Get all the services & register them
for (int i=1; i<=num_services; i++)
{
int rating = buf[ptr++];
get_string(buf, &ptr, service);
get_string(buf, &ptr, ident);
if ( (announce->node_status & 1) == 0)
{
LATServices::Instance()->add_service(std::string((char*)nodename),
std::string((char*)service),
std::string((char*)ident),
rating,
interface, macaddr);
}
else
{
LATServices::Instance()->remove_node(std::string((char*)nodename));
}
}
}
// Wait for data available on a client PTY
void LATServer::add_pty(LocalPort *port, int fd)
{
fdlist.push_back(fdinfo(fd, port, LOCAL_PTY));
}
// Got a new connection from latcp
void LATServer::accept_latcp(int fd)
{
struct sockaddr_un socka;
socklen_t sl = sizeof(socka);
int latcp_client_fd = accept(fd, (struct sockaddr *)&socka, &sl);
if (latcp_client_fd >= 0)
{
debuglog(("Got LATCP connection\n"));
latcp_circuits[latcp_client_fd] = new LATCPCircuit(latcp_client_fd);
fdlist.push_back(fdinfo(latcp_client_fd, 0, LATCP_SOCKET));
}
else
syslog(LOG_WARNING, "accept on latcp failed: %m");
}
// Got a new connection from llogin
void LATServer::accept_llogin(int fd)
{
struct sockaddr_un socka;
socklen_t sl = sizeof(socka);
int llogin_client_fd = accept(fd, (struct sockaddr *)&socka, &sl);
if (llogin_client_fd >= 0)
{
debuglog(("Got llogin connection\n"));
latcp_circuits[llogin_client_fd] = new LLOGINCircuit(llogin_client_fd);
fdlist.push_back(fdinfo(llogin_client_fd, 0, LLOGIN_SOCKET));
}
else
syslog(LOG_WARNING, "accept on llogin failed: %m");
}
// Read a request from LATCP
void LATServer::read_latcp(int fd)
{
debuglog(("Got command on latcp socket: %d\n", fd));
if (!latcp_circuits[fd]->do_command())
{
// Mark the FD for removal
deleted_session s(LATCP_SOCKET, 0, 0, fd);
dead_session_list.push_back(s);
}
}
// Read a request from LLOGIN
void LATServer::read_llogin(int fd)
{
debuglog(("Got command on llogin socket: %d\n", fd));
if (!latcp_circuits[fd]->do_command())
{
// Mark the FD for removal
deleted_session s(LLOGIN_SOCKET, 0, 0, fd);
dead_session_list.push_back(s);
}
}
void LATServer::delete_entry(deleted_session &dsl)
{
switch (dsl.get_type())
{
case INACTIVE:
break; // do nothing;
case LAT_SOCKET:
case LATCP_RENDEZVOUS:
case LLOGIN_RENDEZVOUS:
// These never get deleted
break;
case LATCP_SOCKET:
case LLOGIN_SOCKET:
remove_fd(dsl.get_fd());
close(dsl.get_fd());
delete latcp_circuits[dsl.get_fd()];
latcp_circuits.erase(dsl.get_fd());
break;
case LOCAL_PTY:
case DISABLED_PTY:
remove_fd(dsl.get_fd());
if (connections[dsl.get_conn()])
connections[dsl.get_conn()]->remove_session(dsl.get_id());
break;
default:
debuglog(("Unknown socket type: %d\n", dsl.get_type()));
break;
}
}
void LATServer::process_data(fdinfo &fdi)
{
switch (fdi.get_type())
{
case INACTIVE:
case DISABLED_PTY:
break; // do nothing;
case LOCAL_PTY:
fdi.get_localport()->do_read();
break;
case LAT_SOCKET:
read_lat(fdi.get_fd());
break;
case LATCP_RENDEZVOUS:
accept_latcp(fdi.get_fd());
break;
case LLOGIN_RENDEZVOUS:
accept_llogin(fdi.get_fd());
break;
case LATCP_SOCKET:
read_latcp(fdi.get_fd());
break;
case LLOGIN_SOCKET:
read_llogin(fdi.get_fd());
break;
}
}
// Send a circuit disconnect with error code
void LATServer::send_connect_error(int reason, LAT_Header *msg, int interface,
unsigned char *macaddr)
{
unsigned char buf[1600];
LAT_Header *header = (LAT_Header *)buf;
int ptr=sizeof(LAT_Header);
memset(buf, 0, sizeof(buf));
header->cmd = LAT_CCMD_DISCON;
header->num_slots = 0;
header->local_connid = 0; // Servers expect this to be clear
header->remote_connid = msg->local_connid;
header->sequence_number = msg->sequence_number;
header->ack_number = msg->ack_number;
buf[ptr++] = reason;
send_message(buf, ptr, interface, macaddr);
}
// Shutdown by latcp
void LATServer::Shutdown()
{
// Shutdown all the connections first
for (int i=1; i<MAX_CONNECTIONS; i++)
{
if (connections[i])
dead_connection_list.push_back(i);
}
// Exit the main loop.
do_shutdown = true;
}
// Return true if 'name' is a local service
bool LATServer::is_local_service(char *name)
{
// Look for it.
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), name);
if (sii != servicelist.end()) return true;
return false;
}
// Return the command info for a service
int LATServer::get_service_info(char *name, std::string &cmd, int &maxcon, int &curcon, uid_t &uid, gid_t &gid)
{
// Look for it.
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), name);
if (sii != servicelist.end())
{
cmd = sii->get_command();
maxcon = sii->get_max_connections();
curcon = sii->get_cur_connections();
uid = sii->get_uid();
gid = sii->get_gid();
return 0;
}
return -1;
}
// Add a new service from latcp
bool LATServer::add_service(char *name, char *ident, char *command, int max_conn,
uid_t uid, gid_t gid, int _rating, bool _static_rating)
{
// Look for it.
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), name);
if (sii != servicelist.end()) return false; // Already exists
// if rating is 0 then use the node default.
if (!_rating) _rating = rating;
servicelist.push_back(serviceinfo(name,
_rating,
_static_rating,
ident,
max_conn,
command,
uid, gid));
// Resend the announcement message.
send_service_announcement(-1);
return true;
}
// Change the rating of a service
bool LATServer::set_rating(char *name, int _rating, bool _static_rating)
{
// Look for it.
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), name);
if (sii == servicelist.end()) return false; // Not found it
sii->set_rating(_rating, _static_rating);
// Resend the announcement message.
send_service_announcement(-1);
return true;
}
// Change the ident of a service
bool LATServer::set_ident(char *name, char *ident)
{
// Look for it.
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), name);
if (sii == servicelist.end()) return false; // Not found it
debuglog(("Setting ident for %s to '%s'\n", name, ident));
sii->set_ident(ident);
// Resend the announcement message.
send_service_announcement(-1);
return true;
}
// Remove reverse-LAT port via latcp
bool LATServer::remove_port(char *name)
{
debuglog(("remove port %s\n", name));
// Search for it.
std::list<LocalPort>::iterator p(portlist.begin());
for (; p != portlist.end(); p++)
{
if (strcmp(p->get_devname().c_str(), name) == 0)
{
p->close_and_delete();
portlist.erase(p);
return true;
}
}
return false;
}
// Remove service via latcp
bool LATServer::remove_service(char *name)
{
// Only add the service if it does not already exist.
std::list<serviceinfo>::iterator sii;
sii = find(servicelist.begin(), servicelist.end(), name);
if (sii == servicelist.end()) return false; // Does not exist
servicelist.erase(sii);
// This is overkill but it gets rid of the service in the known
// services table.
LATServices::Instance()->remove_node(std::string((char*)get_local_node()));
// Resend the announcement message -- this will re-add our node
// services back into the known services list.
send_service_announcement(-1);
return true;
}
// Change the multicast timer
void LATServer::set_multicast(int newtime)
{
if (newtime)
{
multicast_timer = newtime;
alarm(newtime);
}
}
// Change the node name
void LATServer::set_nodename(unsigned char *name)
{
debuglog(("New node name is %s\n", name));
// Remove all existing node services
LATServices::Instance()->remove_node(std::string((char*)get_local_node()));
// Set the new name
strcpy((char *)local_name, (char *)name);
// Resend the announcement message -- this will re-add our node
// services back into the known services list.
send_service_announcement(-1);
}
// Start sending service announcements
void LATServer::unlock()
{
sig_blk_t _block(SIGALRM);
locked = false;
alarm_signal(SIGALRM);
}
// Generic make_connection code for llogin & port sessions.
int LATServer::make_connection(int fd, const char *service, const char *rnode, const char *port,
const char *localport, const char *password, bool queued)
{
unsigned char macaddr[6];
std::string servicenode;
int this_int;
char node[255];
// Take a local copy of the node so we can overwrite it.
strcpy(node, rnode);
// If no node was specified then use the highest rated one
if (node[0] == '\0')
{
debuglog(("make_connection : no node, use highest\n"));
if (!LATServices::Instance()->get_highest(std::string((char*)service),
servicenode, macaddr,
&this_int))
{
debuglog(("Can't find service %s\n", service));
return -2; // Never eard of it!
}
strcpy((char *)node, servicenode.c_str());
}
else
{
debuglog(("make_connection : node : %s\n", node));
// Try to find the node
if (!LATServices::Instance()->get_node(std::string((char*)service),
std::string((char*)node), macaddr,
&this_int))
{
debuglog(("Can't find node %s in service\n", node, service));
return -2;
}
}
// Look for a connection that's already in use for this node, unless we
// are queued in which case we need to allocate a new connection for
// initiating the connection and receiving status requests on.
int connid = -1;
if (!queued) connid = find_connection_by_node(node);
if (connid == -1)
{
// None: create a new one
connid = get_next_connection_number();
connections[connid] = new LATConnection(connid,
(char *)service,
(char *)port,
(char *)localport,
(char *)node,
queued,
false);
}
return connid;
}
int LATServer::make_llogin_connection(int fd, const char *service, const char *rnode, const char *port,
const char *localport, const char *password, bool queued)
{
int ret;
int connid;
connid = make_connection(fd, service, rnode, port, localport, password, queued);
if (connid < 0)
return connid;
debuglog(("lloginSession for %s has connid %d\n", service, connid));
ret = connections[connid]->create_llogin_session(fd, service, port, localport, password);
// Remove LLOGIN socket from the list as it's now been
// added as a PTY (honest!)
deleted_session s(LLOGIN_SOCKET, 0, 0, fd);
dead_session_list.push_back(s);
return ret;
}
// Called when activity is detected on a LocalPort - we connect it
// to the service.
int LATServer::make_port_connection(int fd, LocalPort *lport,
const char *service, const char *rnode,
const char *port,
const char *localport,
const char *password,
bool queued)
{
int ret;
int connid;
debuglog(("LATServer::make_port_connection : fd %d, lport '0x%X', service '%s', rnode '%s', port '%s', localport '%s', pwd '%s'\n",
fd, lport, service, rnode, port, localport, password));
connid = make_connection(fd, service, rnode, port, localport, password, queued);
if (connid < 0)
return connid;
debuglog(("localport for %s has connid %d\n", service, connid));
ret = connections[connid]->create_localport_session(fd, lport, service,
port, localport, password);
return ret;
}
// Called from latcp to create a /dev/lat/ port
// Here we simply add it to a lookaside list and wait
// for it to be activated by a user.
int LATServer::create_local_port(unsigned char *service,
unsigned char *portname,
unsigned char *devname,
unsigned char *remnode,
bool queued,
bool clean,
unsigned char *password)
{
debuglog(("Server::create_local_port: %s\n", devname));
// Don't create it if it already exists.
std::list<LocalPort>::iterator i(portlist.begin());
for (; i != portlist.end(); i++)
{
if (strcmp(i->get_devname().c_str(), (char *)devname) == 0)
{
return 1; // already in use
}
}
portlist.push_back(LocalPort(service, portname, devname, remnode, queued, clean, password));
// Find the actual port in the list and start it up, this is because
// the STL containers hold actual objects rather then pointers
std::list<LocalPort>::iterator p(portlist.begin());
for (; p != portlist.end(); p++)
{
if (strcmp(p->get_devname().c_str(), (char *)devname) == 0)
{
p->init_port();
break;
}
}
return 0;
}
// Make this as much like VMS LATCP SHOW NODE as possible.
bool LATServer::show_characteristics(bool verbose, std::ostringstream &output)
{
output <<std::endl;
output.setf(std::ios::left, std::ios::adjustfield);
output << "Node Name: ";
output.width(15);
output << get_local_node() << " " << " LAT Protocol Version: " << LAT_VERSION << "." << LAT_VERSION_ECO << std::endl;
output.setf(std::ios::right, std::ios::adjustfield);
output << "Node State: On " << " LATD Version: " << VERSION << std::endl;
output << "Node Ident: " << greeting << std::endl;
output << std::endl;
output << "Service Responder : " << (responder?"Enabled":"Disabled") << std::endl;
output << "Interfaces : ";
for (int i=0; i<num_interfaces; i++)
{
output << iface->ifname(interface_num[i]) << " ";
}
output << std::endl << std::endl;
output << "Circuit Timer (msec): " << std::setw(6) << circuit_timer*10 << " Keepalive Timer (sec): " << std::setw(6) << keepalive_timer << std::endl;
output << "Retransmit Limit: " << std::setw(6) << retransmit_limit << std::endl;
output << "Multicast Timer (sec):" << std::setw(6) << multicast_timer << std::endl;
output << std::endl;
// Show groups
output << "User Groups: ";
print_bitmap(output, true, user_groups);
output << "Service Groups: ";
print_bitmap(output, groups_set, groups);
output << std::endl;
// Show services we are accepting for.
output << "Service Name Status Rating Identification" << std::endl;
std::list<serviceinfo>::iterator i(servicelist.begin());
for (; i != servicelist.end(); i++)
{
output.width(15);
output.setf(std::ios::left, std::ios::adjustfield);
output << i->get_name().c_str();
output.setf(std::ios::right, std::ios::adjustfield);
output << "Enabled" << std::setw(6) << i->get_rating() <<
(i->get_static()?" ":" D ") << i->get_id() << std::endl;
}
output << std::endl << "Port Service Node Remote Port Queued" << std::endl;
// Show allocated ports
std::list<LocalPort>::iterator p(portlist.begin());
for (; p != portlist.end(); p++)
{
p->show_info(verbose, output);
}
// NUL-terminate it.
output << std::endl << std::ends;
return true;
}
bool LATServer::show_nodes(bool verbose, std::ostringstream &output)
{
return LATServices::Instance()->list_dummy_nodes(verbose, output);
}
// Return a number for a new connection
int LATServer::get_next_connection_number()
{
int i;
for (i=next_connection; i < MAX_CONNECTIONS; i++)
{
if (!connections[i])
{
next_connection = i+1;
return i;
}
}
// Didn't find a slot here - try from the start
for (i=1; i < next_connection; i++)
{
if (!connections[i])
{
next_connection = i+1;
return i;
}
}
return -1;
}
int LATServer::set_servergroups(unsigned char *bitmap)
{
// Assume all unset if this is the first mention of groups
if (!groups_set)
{
memset(groups, 0, 32);
user_groups[0] = 1; // But enable group 0
}
groups_set = true;
for (int i=0; i<32; i++)
{
groups[i] |= bitmap[i];
}
return true;
}
int LATServer::unset_servergroups(unsigned char *bitmap)
{
// Assume all set if this is the first mention of groups
if (!groups_set)
{
memset(groups, 0xFF, 32);
}
groups_set = true;
for (int i=0; i<32; i++)
{
groups[i] &= ~bitmap[i];
}
return true;
}
int LATServer::set_usergroups(unsigned char *bitmap)
{
for (int i=0; i<32; i++)
{
user_groups[i] |= bitmap[i];
}
return true;
}
int LATServer::unset_usergroups(unsigned char *bitmap)
{
for (int i=0; i<32; i++)
{
user_groups[i] &= ~bitmap[i];
}
return true;
}
// Look for a connection for this node name, if not found then
// return -1
int LATServer::find_connection_by_node(const char *node)
{
debuglog(("Looking for connection to node %s\n", node));
for (int i=1; i<MAX_CONNECTIONS; i++)
{
if (connections[i] &&
connections[i]->node_is(node) &&
connections[i]->num_clients() < LATConnection::MAX_SESSIONS-1)
{
debuglog(("Reusing connection for node %s\n", node));
return i;
}
}
return -1;
}
// Print a groups bitmap
// TODO: print x-y format like we accept in latcp.
void LATServer::print_bitmap(std::ostringstream &output, bool isset, unsigned char *bitmap)
{
if (!isset)
{
output << "0" << std::endl;
return;
}
bool printed = false;
for (int i=0; i<32; i++) // Show bytes
{
unsigned char thebyte = bitmap[i];
for (int j=0; j<8; j++) // Bits in the byte
{
if (thebyte&1)
{
if (printed) output << ",";
printed = true;
output << i*8+j;
}
thebyte = thebyte>>1;
}
}
output << std::endl;
}
LATServer *LATServer::instance = NULL;
unsigned char LATServer::greeting[255] = {'\0'};
|