cmdline.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * \file cmdline.c
  3. *
  4. * \brief Commandline utilities.
  5. *
  6. * Frontend to QA, proving an easy command line inteface according with the
  7. * POSIX standard.
  8. */
  9. #define _GNU_SOURCE
  10. #include <getopt.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include "qa/qa.h"
  16. /**
  17. * \brief Prints the usage message, then exit.
  18. *
  19. * Prints in POSIX format the various options for using qa.
  20. *
  21. */
  22. void usage(void)
  23. {
  24. static const char* help_message = "%s usage: %s"
  25. " [-r HOST:port | -f X509 | -R RSA]"
  26. " [-a ATTACK]"
  27. " \n";
  28. fprintf(stderr, help_message,
  29. program_invocation_short_name,
  30. program_invocation_name);
  31. }
  32. void conflicting_args(void)
  33. {
  34. fprintf(stderr, "Conflicting arguments.\n");
  35. usage();
  36. exit(EXIT_FAILURE);
  37. }
  38. int main(int argc, char** argv)
  39. {
  40. char opt;
  41. int option_index;
  42. struct option long_options[] = {
  43. {"help", no_argument, NULL, 'h'},
  44. {"remote", required_argument, NULL, 'r'},
  45. {"file", required_argument, NULL, 'f'},
  46. {"rsa", required_argument, NULL, 'R'},
  47. {0, 0, 0, 0}
  48. };
  49. static const char* short_options = "hr:f:a:R:";
  50. struct qa_conf conf = {
  51. .src_type = NONE,
  52. .attacks = NULL,
  53. };
  54. while ((opt=getopt_long(argc, argv,
  55. short_options, long_options,
  56. &option_index)) != -1)
  57. switch (opt) {
  58. case 'h':
  59. usage();
  60. exit(EXIT_SUCCESS);
  61. break;
  62. case 'f':
  63. if (conf.src_type != NONE) conflicting_args();
  64. conf.src_type = LOCAL_X509;
  65. conf.src = optarg;
  66. break;
  67. case 'r':
  68. if (conf.src_type != NONE) conflicting_args();
  69. conf.src_type = REMOTE;
  70. conf.src = optarg;
  71. break;
  72. case 'R':
  73. if (conf.src_type != NONE) conflicting_args();
  74. conf.src_type = LOCAL_RSA;
  75. conf.src = optarg;
  76. break;
  77. case 'a':
  78. conf.attacks = optarg;
  79. break;
  80. case '?':
  81. default:
  82. usage();
  83. exit(EXIT_FAILURE);
  84. }
  85. if (conf.src_type == NONE) {
  86. conf.src_type = LOCAL_RSA;
  87. if (optind == argc)
  88. conf.src = "-";
  89. else if (optind == argc-1)
  90. conf.src = argv[optind];
  91. else if (optind == argc-2 && !strcmp(argv[optind], "--"))
  92. conf.src = argv[optind+1];
  93. else {
  94. usage();
  95. exit(EXIT_FAILURE);
  96. }
  97. }
  98. return qa_init(&conf);
  99. }