Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

snf_sync.cpp 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // snf_sync.cpp
  2. // Copyright (C) 2006 - 2020 ARM Research Labs, LLC.
  3. // See www.armresearch.com for the copyright terms.
  4. // See snf_sync.hpp for details.
  5. #include "snf_sync.hpp"
  6. namespace cd = codedweller;
  7. void snf_sync::construct() { // Encapsulate initial construction.
  8. ClientGBUAlertInitializer.link(ClientGBUAlertHandler); // Link the alert configurators.
  9. ServerGBUAlertInitializer.link(ServerGBUAlertHandler);
  10. SNFWasParsed.setup(ReadWasGood); // Link our configurator to that flag.
  11. SetupReader(); // Configure our reader.
  12. reset(); // and initialize our data.
  13. }
  14. snf_sync::snf_sync() : // Construcing a blank snf_sync.
  15. Reader("snf"), // The Reader looks for "snf"
  16. ReadWasGood(false) { // There has been no good read yet.
  17. construct(); // Internal wiring & initialization.
  18. }
  19. snf_sync::snf_sync(const char* bfr, int len) : // Constructing with a full buffer.
  20. Reader("snf"), // Start with our blank construction.
  21. ReadWasGood(false) {
  22. construct(); // Internal wiring & initialization.
  23. cd::ConfigurationData Data(bfr, len); // Then build ConfigurationData from
  24. Reader.interpret(Data); // the buffer and interpret it.
  25. }
  26. snf_sync::snf_sync(std::string& input) : // Constructing with a string.
  27. Reader("snf"), // Start with our blank construction.
  28. ReadWasGood(false) {
  29. construct(); // Internal wiring & initialization.
  30. cd::ConfigurationData Data(input.c_str(), input.length()); // Then build ConfigurationData from
  31. Reader.interpret(Data); // the string and interpret it.
  32. }
  33. void snf_sync::SetupReader() { // Configure the reader to recognize
  34. Reader // the snf_sync protocol.
  35. .atEndCall(SNFWasParsed) // Set flag to true when successful.
  36. .Element("<!-- SNFWasParsed -->", ReadWasGood, false).End() // Trick using impossible element name.
  37. .Element("sync")
  38. .Element("challenge")
  39. .Attribute("text", snf_sync_challenge_txt, "")
  40. .End("challenge")
  41. .Element("response")
  42. .Attribute("nodeid", snf_sync_response_nodeid, "")
  43. .Attribute("text", snf_sync_response_text, "")
  44. .End("response")
  45. .Element("error")
  46. .Attribute("message", snf_sync_error_message, "")
  47. .Attribute("code", snf_sync_error_code, 0)
  48. .End("error")
  49. .Element("rulebase")
  50. .Attribute("utc", snf_sync_rulebase_utc, "")
  51. .End("rulebase")
  52. .Element("client")
  53. .atStartCall(ClientGBUAlertInitializer)
  54. .Element("gbu")
  55. .atEndCall(ClientGBUAlertHandler)
  56. .Attribute("time", ClientGBUAlertHandler.Alert_time, "")
  57. .Attribute("ip", ClientGBUAlertHandler.Alert_ip, "")
  58. .Attribute("t", ClientGBUAlertHandler.Alert_t, "Ignore")
  59. .Attribute("b", ClientGBUAlertHandler.Alert_b, 0)
  60. .Attribute("g", ClientGBUAlertHandler.Alert_g, 0)
  61. .End("gbu")
  62. .End("client")
  63. .Element("server")
  64. .atStartCall(ServerGBUAlertInitializer)
  65. .Element("gbu")
  66. .atEndCall(ServerGBUAlertHandler)
  67. .Attribute("time", ServerGBUAlertHandler.Alert_time, "")
  68. .Attribute("ip", ServerGBUAlertHandler.Alert_ip, "")
  69. .Attribute("t", ServerGBUAlertHandler.Alert_t, "Ignore")
  70. .Attribute("b", ServerGBUAlertHandler.Alert_b, 0)
  71. .Attribute("g", ServerGBUAlertHandler.Alert_g, 0)
  72. .End("gbu")
  73. .Element("resync")
  74. .Attribute("secs", snf_sync_server_resync_secs, -1)
  75. .End("resync")
  76. .End("server")
  77. .End("sync")
  78. .End("snf");
  79. }
  80. void snf_sync::reset() { // Reset the reader for new data.
  81. ReadWasGood = false; // There has been no read yet.
  82. Reader.initialize(); // Initialize to the defaults.
  83. };
  84. bool snf_sync::read(const char* bfr, int len) { // To read from a buffer we
  85. cd::ConfigurationData Data(bfr, len); // construct ConfigurationData from
  86. Reader.interpret(Data); // the buffer and interpret it.
  87. return good(); // Return true if it looked good.
  88. }
  89. bool snf_sync::read(std::string& input) { // To read from a string we
  90. return read(input.c_str(), input.length()); // get the strings buffer and hand off
  91. } // to our buffer read()
  92. bool snf_sync::good() { // True if the Reader finished the
  93. return (true == ReadWasGood); // snf element successfully.
  94. }
  95. bool snf_sync::bad() { // False if the Reader finished the
  96. return (false == ReadWasGood); // snf element successfully.
  97. }
  98. void GBUAlertHandler::operator()(
  99. cd::ConfigurationElement& E, cd::ConfigurationData& D) { // Add an alert.
  100. GBUdbAlert NewAlert; // Create an alert object.
  101. cd::SocketAddress IPAddress; // Grab one of these for a converter.
  102. IPAddress.setAddress(const_cast<char*>(Alert_ip.c_str())); // Conver the IP address to an int.
  103. NewAlert.IP = IPAddress.getAddress(); // Put the IP into it's place.
  104. NewAlert.R.Bad(Alert_b); // Set the bad count on the record.
  105. NewAlert.R.Good(Alert_g); // Set the good count on the record.
  106. strncpy(NewAlert.UTC, Alert_time.c_str(), UTCBufferSize-1); // Copy the timestamp.
  107. switch(Alert_t.at(0)) { // Use the first byte to set the flag.
  108. case 'U': { // U means Ugly.
  109. NewAlert.R.Flag(Ugly);
  110. break;
  111. }
  112. case 'I': { // I means Ignore.
  113. NewAlert.R.Flag(Ignore);
  114. break;
  115. }
  116. case 'G': { // G means Good.
  117. NewAlert.R.Flag(Good);
  118. break;
  119. }
  120. case 'B': { // B means Bad.
  121. NewAlert.R.Flag(Bad);
  122. break;
  123. }
  124. }
  125. AlertList.push_back(NewAlert); // Push back the new alert.
  126. }
  127. void GBUAlertHandler::reset() { // To reset the handler,
  128. Alert_time = ""; // clear all of the input strings
  129. Alert_ip = ""; // to the empty string and all of
  130. Alert_t = ""; // the input counts to zero.
  131. Alert_b = 0;
  132. Alert_g = 0;
  133. AlertList.clear(); // Clear out the list.
  134. }