FTL_last-dns_replacement.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --- a/args.c 2019-05-25 21:37:26.000000000 +0200
  2. +++ b/args.c 2020-04-24 00:32:27.000000000 +0200
  3. @@ -17,20 +17,88 @@
  4. int argc_dnsmasq = 0;
  5. const char** argv_dnsmasq = NULL;
  6. +static inline bool strEndsWith(const char *input, const char *end){
  7. + return strcmp(input + strlen(input) - strlen(end), end) == 0;
  8. +}
  9. +
  10. void parse_args(int argc, char* argv[])
  11. {
  12. - int i;
  13. -
  14. // Regardless of any arguments, we always pass "-k" (nofork) to dnsmasq
  15. argc_dnsmasq = 2;
  16. argv_dnsmasq = calloc(argc_dnsmasq, sizeof(char*));
  17. argv_dnsmasq[0] = "";
  18. argv_dnsmasq[1] = "-k";
  19. - // start from 1, as argv[0] is the executable name "pihole-FTL"
  20. - for(i=1; i < argc; i++)
  21. + bool consume_for_dnsmasq = false;
  22. + // If the binary name is "dnsmasq" (e.g., symlink /usr/bin/dnsmasq -> /usr/bin/pihole-FTL),
  23. + // we operate in drop-in mode and consume all arguments for the embedded dnsmasq core
  24. + if(strEndsWith(argv[0], "dnsmasq"))
  25. + consume_for_dnsmasq = true;
  26. +
  27. + // start from 1, as argv[0] is the executable name
  28. + for(int i = 1; i < argc; i++)
  29. {
  30. bool ok = false;
  31. +
  32. + // Implement dnsmasq's test function, no need to prepare the entire FTL
  33. + // environment (initialize shared memory, lead queries from long-term
  34. + // database, ...) when the task is a simple (dnsmasq) syntax check
  35. + if(strcmp(argv[i], "dnsmasq-test") == 0 ||
  36. + strcmp(argv[i], "--test") == 0)
  37. + {
  38. + const char *arg[2];
  39. + arg[0] = "";
  40. + arg[1] = "--test";
  41. + main_dnsmasq(2, arg);
  42. + ok = true;
  43. + }
  44. +
  45. + // If we find "--" we collect everything behind that for dnsmasq
  46. + if(strcmp(argv[i], "--") == 0)
  47. + {
  48. + // Remember that the rest is for dnsmasq ...
  49. + consume_for_dnsmasq = true;
  50. +
  51. + // ... and skip the current argument ("--")
  52. + continue;
  53. + }
  54. +
  55. + // If consume_for_dnsmasq is true, we collect all remaining options for
  56. + // dnsmasq
  57. + if(consume_for_dnsmasq)
  58. + {
  59. + argc_dnsmasq = argc - i + 2;
  60. + if(argv_dnsmasq != NULL)
  61. + free(argv_dnsmasq);
  62. +
  63. + argv_dnsmasq = calloc(argc_dnsmasq, sizeof(const char*));
  64. + argv_dnsmasq[0] = "";
  65. +
  66. + if(debug)
  67. + argv_dnsmasq[1] = "-d";
  68. + else
  69. + argv_dnsmasq[1] = "-k";
  70. +
  71. + if(debug)
  72. + {
  73. + printf("dnsmasq options: [0]: %s\n", argv_dnsmasq[0]);
  74. + printf("dnsmasq options: [1]: %s\n", argv_dnsmasq[1]);
  75. + }
  76. +
  77. + int j = 2;
  78. + while(i < argc)
  79. + {
  80. + argv_dnsmasq[j++] = strdup(argv[i++]);
  81. + if(debug)
  82. + printf("dnsmasq options: [%i]: %s\n", j-1, argv_dnsmasq[j-1]);
  83. + }
  84. +
  85. + // Return early: We have consumes all available command line arguments
  86. + return;
  87. + }
  88. +
  89. + // What follows beyond this point are FTL internal command line arguments
  90. +
  91. if(strcmp(argv[i], "d") == 0 ||
  92. strcmp(argv[i], "debug") == 0)
  93. {
  94. @@ -97,35 +165,6 @@
  95. ok = true;
  96. }
  97. - // Implement dnsmasq's test function
  98. - if(strcmp(argv[i], "dnsmasq-test") == 0)
  99. - {
  100. - const char *arg[2];
  101. - arg[0] = "";
  102. - arg[1] = "--test";
  103. - main_dnsmasq(2, arg);
  104. - ok = true;
  105. - }
  106. -
  107. - // If we find "--" we collect everything behind that for dnsmasq
  108. - if(strcmp(argv[i], "--") == 0)
  109. - {
  110. - int j;
  111. - argc_dnsmasq = argc - i + 1;
  112. - if(argv_dnsmasq != NULL) free(argv_dnsmasq);
  113. - argv_dnsmasq = calloc(argc_dnsmasq + 2,sizeof(const char*));
  114. - argv_dnsmasq[0] = "";
  115. - if(debug) argv_dnsmasq[1] = "-d";
  116. - else argv_dnsmasq[1] = "-k";
  117. -
  118. - for(j=2; j < argc_dnsmasq; j++)
  119. - {
  120. - argv_dnsmasq[j] = strdup(argv[i+j-1]);
  121. - if(debug) logg("dnsmasq options: [%i]: %s",j,argv_dnsmasq[j]);
  122. - }
  123. - return;
  124. - }
  125. -
  126. // List of implemented arguments
  127. if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "help") == 0 || strcmp(argv[i], "--help") == 0)
  128. {