You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snf_sync.cpp 8.8KB

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