2
0

util.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016-2020 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * nmrpflash is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "util.h"
  20. using namespace std;
  21. namespace nmrpflash {
  22. namespace {
  23. uint32_t prefix_to_netmask(int prefix)
  24. {
  25. return 0xffffffff << (32 - prefix);
  26. }
  27. }
  28. const mac_addr mac_addr::none;
  29. const mac_addr mac_addr::broadcast({ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });
  30. mac_addr::mac_addr(const string& str)
  31. {
  32. unsigned mac[6];
  33. int n = sscanf(str.c_str(),
  34. "%02x:%02x:%02x:%02x:%02x:%02x",
  35. &mac[0], &mac[1], &mac[2],
  36. &mac[3], &mac[4], &mac[5]);
  37. if (n != 6) {
  38. throw invalid_argument("Invalid MAC address: " + str);
  39. }
  40. init(mac);
  41. }
  42. string mac_addr::to_string(char delim) const
  43. {
  44. string ret;
  45. for (int i = 0; i < sizeof(m_mac); ++i) {
  46. if (i) {
  47. ret += delim;
  48. }
  49. ret += (boost::format("%02x") % int(m_mac[i])).str();
  50. }
  51. return ret;
  52. }
  53. ip_addr::ip_addr(const std::string& ip)
  54. {
  55. auto pos = ip.find('/');
  56. if (pos != string::npos && (pos + 1) < ip.size()) {
  57. prefix(stoi(ip.substr(pos + 1)));
  58. }
  59. in_addr addr;
  60. if (!inet_aton(ip.substr(0, pos).c_str(), &addr)) {
  61. throw invalid_argument("Invalid IP address: " + ip);
  62. }
  63. m_ip = addr.s_addr;
  64. }
  65. ip_addr ip_addr::address() const
  66. {
  67. return { m_ip, 0 };
  68. }
  69. ip_addr ip_addr::netmask() const
  70. {
  71. return { prefix_to_netmask(m_prefix), 0 };
  72. }
  73. ip_addr ip_addr::broadcast() const
  74. {
  75. if (m_prefix) {
  76. uint32_t mask = prefix_to_netmask(m_prefix);
  77. return { (m_ip & mask) | ~mask, 0 };
  78. } else {
  79. return { 0, 0 };
  80. }
  81. }
  82. ostream& operator<<(ostream& os, const ip_addr& ip)
  83. {
  84. os << inet_ntoa(in_addr { ip.m_ip });
  85. if (ip.m_prefix) {
  86. os << '/' << ip.m_prefix;
  87. }
  88. return os;
  89. }
  90. void ip_addr::prefix(int pfx)
  91. {
  92. if (pfx < 0 || pfx > 31) {
  93. throw invalid_argument("IP prefix length out of range: " + to_string(pfx));
  94. }
  95. m_prefix = pfx;
  96. }
  97. #if BOOST_OS_WINDOWS
  98. wstring quote(const wstring& str)
  99. {
  100. // https://docs.microsoft.com/en-gb/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way
  101. if (!str.empty() && str.find_first_of(L" \t\n\v\"") == wstring::npos) {
  102. return str;
  103. } else {
  104. wstring quoted = L"\"";
  105. for (auto it = str.begin();; ++it) {
  106. unsigned backslashes = 0;
  107. for (; it != str.end() && *it == L'\\'; ++it) {
  108. ++backslashes;
  109. }
  110. if (it == str.end()) {
  111. quoted.append(backslashes * 2, L'\\');
  112. } else if (it == L'"') {
  113. quoted.append(backslashes * 2 + 1, L'\\');
  114. quoted.append(*it);
  115. } else {
  116. quoted.append(backslashes, L'\\');
  117. quoted.append(*it);
  118. }
  119. }
  120. return quoted + L'"';
  121. }
  122. }
  123. #else
  124. string quote(const string& str)
  125. {
  126. auto pos = str.find('\'');
  127. if (pos == string::npos) {
  128. return "'" + str + "'";
  129. } else {
  130. string quoted = str;
  131. do {
  132. quoted.replace(pos, 1, "'\\''");
  133. pos = quoted.find('\'', pos + 4);
  134. } while (pos != string::npos);
  135. return "'" + quoted + "'";
  136. }
  137. }
  138. #endif
  139. int run(const cmdfmt& cmd, bool throw_on_error)
  140. {
  141. #if BOOST_OS_WINDOWS
  142. PROCESS_INFORMATION pi;
  143. STARTUPINFO si;
  144. memset(&si, 0, sizeof(si));
  145. si.cb = sizeof(si);
  146. DWORD ret = CreateProcessW(
  147. nullptr,
  148. cmd.str(),
  149. nullptr,
  150. nullptr,
  151. FALSE,
  152. 0,
  153. nullptr,
  154. nullptr,
  155. &si,
  156. &pi);
  157. if (!ret) {
  158. throw winapi_error("CreateProcessW");
  159. }
  160. DWORD wait = WaitForSingleObject(pi.hProcess);
  161. DWORD ret = -1;
  162. if (wait == WAIT_OBJECT_0) {
  163. if (!GetExitCodeProcess(pi.hProcess, &ret)) {
  164. ret = -1;
  165. }
  166. }
  167. CloseHandle(pi.hProcess);
  168. CloseHandle(pi.hThread);
  169. if (wait == WAIT_FAILED) {
  170. throw winapi_error("WaitForSingleObject");
  171. }
  172. #else
  173. int ret = system(cmd.str().c_str());
  174. if (ret == -1 || ret == 127) {
  175. throw errno_error("system");
  176. }
  177. #endif
  178. if (ret != 0 && throw_on_error) {
  179. throw runtime_error("command failed with exit status " + to_string(ret));
  180. }
  181. return ret;
  182. }
  183. bool select_readfd(int fd, unsigned timeout)
  184. {
  185. return true;
  186. }
  187. void log::w(const string& msg)
  188. {
  189. cerr << msg << endl;
  190. }
  191. }