main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright StrongLoop, Inc. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "defs.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h> /* getopt */
  27. #endif
  28. #define DEFAULT_BIND_HOST "127.0.0.1"
  29. #define DEFAULT_BIND_PORT 1080
  30. #define DEFAULT_IDLE_TIMEOUT (60 * 1000)
  31. static void parse_opts(server_config *cf, int argc, char **argv);
  32. static void usage(void);
  33. static const char *progname = __FILE__; /* Reset in main(). */
  34. int main(int argc, char **argv) {
  35. server_config config;
  36. int err;
  37. progname = argv[0];
  38. memset(&config, 0, sizeof(config));
  39. config.bind_host = DEFAULT_BIND_HOST;
  40. config.bind_port = DEFAULT_BIND_PORT;
  41. config.idle_timeout = DEFAULT_IDLE_TIMEOUT;
  42. parse_opts(&config, argc, argv);
  43. err = server_run(&config, uv_default_loop());
  44. if (err) {
  45. exit(1);
  46. }
  47. return 0;
  48. }
  49. const char *_getprogname(void) {
  50. return progname;
  51. }
  52. static void parse_opts(server_config *cf, int argc, char **argv) {
  53. int opt;
  54. while (-1 != (opt = getopt(argc, argv, "H:hp:"))) {
  55. switch (opt) {
  56. case 'H':
  57. cf->bind_host = optarg;
  58. break;
  59. case 'p':
  60. if (1 != sscanf(optarg, "%hu", &cf->bind_port)) {
  61. pr_err("bad port number: %s", optarg);
  62. usage();
  63. }
  64. break;
  65. default:
  66. usage();
  67. }
  68. }
  69. }
  70. static void usage(void) {
  71. printf("Usage:\n"
  72. "\n"
  73. " %s [-b <address> [-h] [-p <port>]\n"
  74. "\n"
  75. "Options:\n"
  76. "\n"
  77. " -b <hostname|address> Bind to this address or hostname.\n"
  78. " Default: \"127.0.0.1\"\n"
  79. " -h Show this help message.\n"
  80. " -p <port> Bind to this port number. Default: 1080\n"
  81. "",
  82. progname);
  83. exit(1);
  84. }