utils.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "include/utils.h"
  2. #include "util.h"
  3. #include <arpa/inet.h>
  4. #include <ifaddrs.h>
  5. #include <netinet/in.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. namespace smartdns
  15. {
  16. TempFile::TempFile()
  17. {
  18. pattern_ = "/tmp/smartdns-test-tmp.XXXXXX";
  19. }
  20. TempFile::TempFile(const std::string &line)
  21. {
  22. pattern_ = "/tmp/smartdns-test-tmp.XXXXXX";
  23. }
  24. TempFile::~TempFile()
  25. {
  26. if (ofs_.is_open()) {
  27. ofs_.close();
  28. ofs_.clear();
  29. }
  30. if (path_.length() > 0) {
  31. unlink(path_.c_str());
  32. }
  33. }
  34. void TempFile::SetPattern(const std::string &pattern)
  35. {
  36. pattern_ = pattern;
  37. }
  38. bool TempFile::Write(const std::string &line)
  39. {
  40. if (ofs_.is_open() == false) {
  41. if (NewTempFile() == false) {
  42. return false;
  43. }
  44. }
  45. ofs_.write(line.data(), line.size());
  46. if (ofs_.fail()) {
  47. return false;
  48. }
  49. ofs_.flush();
  50. return true;
  51. }
  52. bool TempFile::NewTempFile()
  53. {
  54. char filename[128];
  55. strncpy(filename, "/tmp/smartdns-test-tmp.XXXXXX", sizeof(filename));
  56. int fd = mkstemp(filename);
  57. if (fd < 0) {
  58. return false;
  59. }
  60. Defer
  61. {
  62. close(fd);
  63. };
  64. std::ofstream ofs(filename);
  65. if (ofs.is_open() == false) {
  66. return false;
  67. }
  68. ofs_ = std::move(ofs);
  69. path_ = filename;
  70. return true;
  71. }
  72. std::string TempFile::GetPath()
  73. {
  74. if (ofs_.is_open() == false) {
  75. if (NewTempFile() == false) {
  76. return "";
  77. }
  78. }
  79. return path_;
  80. }
  81. Commander::Commander() {}
  82. Commander::~Commander()
  83. {
  84. Kill();
  85. }
  86. bool Commander::Run(const std::string &cmd)
  87. {
  88. std::vector<std::string> args;
  89. if (ParserArg(cmd, args) != 0) {
  90. return false;
  91. }
  92. return Run(args);
  93. }
  94. bool Commander::Run(const std::vector<std::string> &cmds)
  95. {
  96. pid_t pid;
  97. if (pid_ > 0) {
  98. return false;
  99. }
  100. pid = fork();
  101. if (pid < 0) {
  102. return false;
  103. }
  104. if (pid == 0) {
  105. char *argv[cmds.size() + 1];
  106. for (int i = 0; i < cmds.size(); i++) {
  107. argv[i] = (char *)cmds[i].c_str();
  108. }
  109. argv[cmds.size()] = nullptr;
  110. execvp(argv[0], argv);
  111. _exit(1);
  112. }
  113. pid_ = pid;
  114. return true;
  115. }
  116. void Commander::Kill()
  117. {
  118. if (pid_ <= 0) {
  119. return;
  120. }
  121. kill(pid_, SIGKILL);
  122. }
  123. void Commander::Terminate()
  124. {
  125. if (pid_ <= 0) {
  126. return;
  127. }
  128. kill(pid_, SIGTERM);
  129. }
  130. int Commander::ExitCode()
  131. {
  132. int wstatus = 0;
  133. if (exit_code_ >= 0) {
  134. return exit_code_;
  135. }
  136. if (pid_ <= 0) {
  137. return -1;
  138. }
  139. if (waitpid(pid_, &wstatus, 0) == -1) {
  140. return -1;
  141. }
  142. exit_code_ = WEXITSTATUS(wstatus);
  143. return exit_code_;
  144. }
  145. int Commander::GetPid()
  146. {
  147. return pid_;
  148. }
  149. bool IsCommandExists(const std::string &cmd)
  150. {
  151. char *copy_path = nullptr;
  152. char cmd_path[4096];
  153. const char *env_path = getenv("PATH");
  154. char *save_ptr = nullptr;
  155. if (env_path == nullptr) {
  156. env_path = "/bin:/usr/bin:/usr/local/bin";
  157. }
  158. copy_path = strdup(env_path);
  159. if (copy_path == nullptr) {
  160. return false;
  161. }
  162. Defer
  163. {
  164. free(copy_path);
  165. };
  166. for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(nullptr, ":", &save_ptr)) {
  167. snprintf(cmd_path, sizeof(cmd_path), "%s/%s", tok, cmd.c_str());
  168. if (access(cmd_path, X_OK) != 0) {
  169. continue;
  170. }
  171. return true;
  172. }
  173. return false;
  174. }
  175. std::string GenerateRandomString(int len)
  176. {
  177. std::string result;
  178. static const char alphanum[] = "0123456789"
  179. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  180. "abcdefghijklmnopqrstuvwxyz";
  181. result.resize(len);
  182. for (int i = 0; i < len; ++i) {
  183. result[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  184. }
  185. return result;
  186. }
  187. int ParserArg(const std::string &cmd, std::vector<std::string> &args)
  188. {
  189. std::string arg;
  190. char quoteChar = 0;
  191. for (char ch : cmd) {
  192. if (quoteChar == '\\') {
  193. arg.push_back(ch);
  194. quoteChar = 0;
  195. continue;
  196. }
  197. if (quoteChar && ch != quoteChar) {
  198. arg.push_back(ch);
  199. continue;
  200. }
  201. switch (ch) {
  202. case '\'':
  203. case '\"':
  204. case '\\':
  205. quoteChar = quoteChar ? 0 : ch;
  206. break;
  207. case ' ':
  208. case '\t':
  209. case '\n':
  210. if (!arg.empty()) {
  211. args.push_back(arg);
  212. arg.clear();
  213. }
  214. break;
  215. default:
  216. arg.push_back(ch);
  217. break;
  218. }
  219. }
  220. if (!arg.empty()) {
  221. args.push_back(arg);
  222. }
  223. return 0;
  224. }
  225. std::vector<std::string> GetAvailableIPAddresses()
  226. {
  227. std::vector<std::string> ipAddresses;
  228. struct ifaddrs *ifAddrStruct = nullptr;
  229. struct ifaddrs *ifa = nullptr;
  230. void *tmpAddrPtr = nullptr;
  231. getifaddrs(&ifAddrStruct);
  232. for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
  233. if (!ifa->ifa_addr) {
  234. continue;
  235. }
  236. if (ifa->ifa_addr->sa_family == AF_INET) { // IPv4 address
  237. tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
  238. char addressBuffer[INET_ADDRSTRLEN];
  239. inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
  240. std::string ipAddress(addressBuffer);
  241. if (!ipAddress.empty() && ipAddress.substr(0, 4) != "127.") {
  242. ipAddresses.push_back(ipAddress);
  243. }
  244. }
  245. }
  246. if (ifAddrStruct != nullptr) {
  247. freeifaddrs(ifAddrStruct);
  248. }
  249. return ipAddresses;
  250. }
  251. bool IsICMPAvailable()
  252. {
  253. int fd = -1;
  254. if (has_unprivileged_ping()) {
  255. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
  256. } else {
  257. fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  258. }
  259. if (fd < 0) {
  260. return false;
  261. }
  262. close(fd);
  263. return true;
  264. }
  265. } // namespace smartdns