PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/source/misc.c

https://github.com/struct/QueMod
C | 124 lines | 92 code | 26 blank | 6 comment | 26 complexity | b0dca0c37c15b07ea48ece71eb2eb2ff MD5 | raw file
  1. #include "include/quemod.h"
  2. #include "include/plugins.h"
  3. void help()
  4. {
  5. fprintf(stdout, "QueMod Fuzzer %d.%d\n\n"
  6. "\t-h\tHelp\n"
  7. "\t-u\tComma separated list of plugins to load\n"
  8. "\t-p\tPlugin directory\n"
  9. "\t-w\tWrite out the packets to the current directory as numbered files\n"
  10. "\t-d\tWrite out packet headers (default is OFF)\n"
  11. "\t-v\tIP Version (4 or 6 (experimental))\n",
  12. VERSION_MAJOR, VERSION_MINOR);
  13. #ifdef DEBUG
  14. fprintf(stdout, "\n[!] QueMod Debug messages turned on\n");
  15. #endif
  16. fprintf(stdout, "\n");
  17. exit(0);
  18. }
  19. void cleanup()
  20. {
  21. /* Call the cleanup hooks in each plugin */
  22. cleanup_plugins();
  23. /* Release each global vector */
  24. free_plugin_vectors();
  25. }
  26. void *x_malloc(unsigned int size)
  27. {
  28. void *ptr;
  29. if(size < 0)
  30. {
  31. error("x_malloc() cannot allocate a negative size");
  32. return NULL;
  33. }
  34. ptr = malloc(size);
  35. if(ptr != NULL)
  36. {
  37. memset(ptr, 0x0, size);
  38. return ptr;
  39. }
  40. if(ptr == NULL || ptr == 0)
  41. {
  42. error("Out Of Memory");
  43. return NULL;
  44. }
  45. error("x_malloc() died somehow");
  46. return NULL;
  47. }
  48. /* Ctrl+C will get you here */
  49. void sig_handler(int signum)
  50. {
  51. nfq_destroy_queue(qh);
  52. nfq_close(nfqh);
  53. cleanup();
  54. exit(ERR);
  55. }
  56. void error(char *str)
  57. {
  58. fprintf(stderr, "%s\n", str);
  59. }
  60. void setup_signal_handlers()
  61. {
  62. #ifdef __linux__
  63. if(signal(SIGINT, sig_handler) == SIG_IGN)
  64. {
  65. signal(SIGINT, SIG_IGN);
  66. }
  67. if(signal(SIGHUP, sig_handler) == SIG_IGN)
  68. {
  69. signal(SIGINT, SIG_IGN);
  70. }
  71. if(signal(SIGTERM, sig_handler) == SIG_IGN)
  72. {
  73. signal(SIGINT, SIG_IGN);
  74. }
  75. #endif
  76. }
  77. /* strlcpy/strlcat copied from OpenBSD - Licensed under BSD */
  78. size_t strlcpy(char *dst, const char *src, size_t siz)
  79. {
  80. register char *d = dst;
  81. register const char *s = src;
  82. register size_t n = siz;
  83. /* Copy as many bytes as will fit */
  84. if (n != 0 && --n != 0)
  85. {
  86. do {
  87. if ((*d++ = *s++) == 0)
  88. break;
  89. } while (--n != 0);
  90. }
  91. /* Not enough room in dst, add NUL and traverse rest of src */
  92. if (n == 0) {
  93. if (siz != 0)
  94. *d = '\0'; /* NUL-terminate dst */
  95. while (*s++);
  96. }
  97. return(s - src - 1); /* count does not include NUL */
  98. }