args.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * args.c
  3. * Routines for processing command-line arguments.
  4. *
  5. * Matej Pfajfar <mp292@cam.ac.uk>
  6. */
  7. /*
  8. * Changes :
  9. * $Log$
  10. * Revision 1.1 2002/06/26 22:45:50 arma
  11. * Initial revision
  12. *
  13. * Revision 1.3 2002/01/26 22:08:40 mp292
  14. * Reviewed according to Secure-Programs-HOWTO.
  15. *
  16. * Revision 1.2 2001/12/14 11:26:23 badbytes
  17. * Tested
  18. *
  19. * Revision 1.1 2001/12/13 15:15:10 badbytes
  20. * Started coding the onion proxy.
  21. *
  22. */
  23. #include <unistd.h>
  24. #include <ctype.h>
  25. #include "../common/log.h"
  26. #include "args.h"
  27. /* prints help on using op */
  28. void print_usage()
  29. {
  30. char *program = "op";
  31. printf("\n%s - Onion Proxy for Onion Routing.\nUsage : %s -f config -p port [-l loglevel -h]\n-h : display this help\n-f config : config file\n-p port : port number which %s should bind to\n-l loglevel : logging threshold; one of alert|crit|err|warning|notice|info|debug\n\n", program,program,program);
  32. }
  33. /* get command-line arguments */
  34. int getargs(int argc, char *argv[], char *args, unsigned short *p, char **conf_filename, int *loglevel)
  35. {
  36. char c; /* next option character */
  37. char *errtest = NULL; /* for detecting strtoul() errors */
  38. int gotf=0; int gotp=0;
  39. if ((!args) || (!conf_filename) || (!loglevel)) /* invalid parameters */
  40. return -1;
  41. while ((c = getopt(argc,argv,args)) != -1)
  42. {
  43. switch(c)
  44. {
  45. case 'f': /* config file */
  46. *conf_filename = optarg;
  47. gotf=1;
  48. break;
  49. case 'p':
  50. *p = (u_short)strtoul(optarg,&errtest,0);
  51. if (errtest == optarg) /* error */
  52. {
  53. log(LOG_ERR,"Error : -p must be followed by an unsigned positive integer value. See help (-h).");
  54. return -1;
  55. }
  56. gotp=1;
  57. break;
  58. case 'h':
  59. print_usage();
  60. exit(0);
  61. case 'l':
  62. if (!strcmp(optarg,"emerg"))
  63. *loglevel = LOG_EMERG;
  64. else if (!strcmp(optarg,"alert"))
  65. *loglevel = LOG_ALERT;
  66. else if (!strcmp(optarg,"crit"))
  67. *loglevel = LOG_CRIT;
  68. else if (!strcmp(optarg,"err"))
  69. *loglevel = LOG_ERR;
  70. else if (!strcmp(optarg,"warning"))
  71. *loglevel = LOG_WARNING;
  72. else if (!strcmp(optarg,"notice"))
  73. *loglevel = LOG_NOTICE;
  74. else if (!strcmp(optarg,"info"))
  75. *loglevel = LOG_INFO;
  76. else if (!strcmp(optarg,"debug"))
  77. *loglevel = LOG_DEBUG;
  78. else
  79. {
  80. log(LOG_ERR,"Error : argument to -l must be one of alert|crit|err|warning|notice|info|debug.");
  81. print_usage();
  82. return -1;
  83. }
  84. break;
  85. case '?':
  86. if (isprint(c))
  87. log(LOG_ERR,"Missing argument or unknown option '-%c'. See help (-h).",optopt);
  88. else
  89. log(LOG_ERR,"Unknown option character 'x%x'. See help (-h).",optopt);
  90. print_usage();
  91. return -1;
  92. break;
  93. default:
  94. return -1;
  95. }
  96. }
  97. /* the -f option is mandatory */
  98. if (!gotf)
  99. {
  100. log(LOG_ERR,"You must specify a config file with the -f option. See help (-h).");
  101. return -1;
  102. }
  103. /* the -p option is mandatory */
  104. if (!gotp)
  105. {
  106. log(LOG_ERR,"You must specify a port with the -p option. See help (-h).");
  107. return -1;
  108. }
  109. return 0;
  110. }