Beispiel #1
0
/* JS signature: listen(addr, [options]) */
SJ_PRIVATE enum v7_err Http_Server_listen(struct v7 *v7, v7_val_t *res) {
  enum v7_err rcode = V7_OK;
  char buf[50], *p = buf;
  const char *ca_cert = NULL, *cert = NULL;
  v7_val_t this_obj = v7_get_this(v7);
  v7_val_t arg0 = v7_arg(v7, 0);
  v7_val_t opts = v7_arg(v7, 1);

  if (!v7_is_number(arg0) && !v7_is_string(arg0)) {
    rcode = v7_throwf(v7, "TypeError", "Function expected");
    goto clean;
  }

  if (!v7_is_undefined(opts) && !v7_is_object(opts)) {
    rcode = v7_throwf(v7, "TypeError", "Options must be an object");
    goto clean;
  }

  if (!v7_is_undefined(opts)) {
    v7_val_t ca_cert_v = v7_get(v7, opts, "ssl_ca_cert", ~0);
    v7_val_t cert_v = v7_get(v7, opts, "ssl_cert", ~0);
    if (!v7_is_undefined(ca_cert_v) && !v7_is_string(ca_cert_v)) {
      rcode = v7_throwf(v7, "TypeError", "ca_cert must be a string");
      goto clean;
    }

    if (!v7_is_undefined(cert_v) && !v7_is_string(cert_v)) {
      rcode = v7_throwf(v7, "TypeError", "cert must be a string");
      goto clean;
    }

    if (!v7_is_undefined(ca_cert_v)) {
      ca_cert = v7_to_cstring(v7, &ca_cert_v);
    }

    if (!v7_is_undefined(cert_v)) {
      cert = v7_to_cstring(v7, &cert_v);
    }
  }

  p = v7_stringify(v7, arg0, buf, sizeof(buf), 0);
  rcode = start_http_server(v7, p, this_obj, ca_cert, cert);
  if (rcode != V7_OK) {
    goto clean;
  }

  *res = this_obj;

clean:
  if (p != buf) {
    free(p);
  }
  return rcode;
}
Beispiel #2
0
int broker_start_server(json_t *config) {
    json_incref(config);

    const char *httpHost = NULL;
    char httpPort[8];
    memset(httpPort, 0, sizeof(httpPort));
    {
        json_t *http = json_object_get(config, "http");
        if (http) {
            json_t *enabled = json_object_get(http, "enabled");
            if (!(enabled && json_boolean_value(enabled))) {
                json_decref(config);
                return 0;
            }
            httpHost = json_string_value(json_object_get(http, "host"));

            json_t *jsonPort = json_object_get(http, "port");
            if (jsonPort) {
                json_int_t p = json_integer_value(jsonPort);
                int len = snprintf(httpPort, sizeof(httpPort) - 1,
                                   "%" JSON_INTEGER_FORMAT, p);
                httpPort[len] = '\0';
            }
        }
    }

    int httpActive = 0;
    Server httpServer;
    uv_poll_t httpPoll;
    if (httpHost && httpPort[0] != '\0') {
        mbedtls_net_init(&httpServer.srv);
        httpServer.data_ready = broker_on_data_callback;

        httpActive = start_http_server(&httpServer, httpHost, httpPort,
                                       mainLoop, &httpPoll);
    }

    uv_signal_t sigInt;
    uv_signal_init(mainLoop, &sigInt);
    uv_signal_start(&sigInt, stop_server_handler, SIGINT);

    uv_signal_t sigTerm;
    uv_signal_init(mainLoop, &sigTerm);
    uv_signal_start(&sigTerm, stop_server_handler, SIGTERM);

//    upstream_connect_conn(&loop, "http://10.0.1.158:8080/conn", "dartbroker", "cbroker");

    if (httpActive) {
        uv_run(mainLoop, UV_RUN_DEFAULT);
    }

    uv_signal_stop(&sigInt);
    uv_signal_stop(&sigTerm);

    if (httpActive) {
        uv_poll_stop(&httpPoll);
    }
    uv_loop_close(mainLoop);
#if defined(__unix__) || defined(__APPLE__)
    if (mainLoop && mainLoop->watchers) {
        uv__free(mainLoop->watchers);
    }
#endif

    json_decref(config);
    return 0;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
    int opt;
    str destination_hostname = {.length = 0, .s = NULL};
    uint16_t server_port = 8080, destination_port = COAP_DEFAULT_PORT;
    char *endptr;
    struct stat s;

    while((opt = getopt(argc, argv, "D:P:p:f:h")) != EOF) {
        switch(opt) {
            case 'D':
                destination_hostname.s = (unsigned char *)optarg;
                destination_hostname.length = strlen(optarg);
                resolve_address(&destination_hostname, (struct sockaddr *)&destination);
                break;
            case 'P':
                destination_port = (uint16_t)strtoul(optarg, &endptr, 10);
                if(*endptr != '\0') {
                    fprintf(stderr, "error: invalid port number: %s\n", optarg);
                    return EXIT_FAILURE;
                }
                break;
            case 'p':
                server_port = (uint16_t)strtoul(optarg, &endptr, 10);
                if(*endptr != '\0') {
                    fprintf(stderr, "error: invalid port number: %s\n", optarg);
                    return EXIT_FAILURE;
                }
                break;
            case 'f':
                if(stat(optarg, &s) == -1) {
                    if(ENOENT == errno) {
                        /* does not exist */
                        fprintf(stderr, "error: %s: %s\n", optarg, strerror(errno));
                        return EXIT_FAILURE;
                    }
                    else {
                        perror("stat");
                        return EXIT_FAILURE;
                    }
                } else {
                    if(S_ISDIR(s.st_mode)) {
                        /* it's a dir */
                        strncpy(static_files_path, optarg, 63);
                        static_files_path[63] = '\0';
                        fprintf(stderr, "Will serve static files of directory '%s'\n", static_files_path);
                    }
                    else {
                        /* exists but is no dir */
                        fprintf(stderr, "error: %s is not a directory\n", optarg);
                        return EXIT_FAILURE;
                    }
                }
                break;
            case 'h':
                fprintf(stderr, "usage: %s -D coap_host [-P coap_port] [-p HTTP_server_port] [-f static_files_dir]\n",
                        basename(argv[0]));
                return EXIT_SUCCESS;
            default:
                return EXIT_FAILURE;
        }
    }

    if(destination_hostname.s == NULL) {
        fprintf(stderr, "error: please specify the target coap host of the proxy with the -D option\n");
        return EXIT_FAILURE;
    }

    destination.sin_port = htons(destination_port);

    // Register the clean function for when the program exists
    if(atexit(cleanup) != 0) {
        perror("atexit");
        return EXIT_FAILURE;
    }
    // Also call it where a SIGINT is received
    struct sigaction action;
    memset(&action, 0, sizeof(action));
    action.sa_handler = &signal_handler;
    if(sigaction(SIGINT, &action, &old_action) != 0) {
        perror("sigaction");
        return EXIT_FAILURE;
    }

    start_http_server(server_port);
    if(http_daemon == NULL) {
        fprintf(stderr, "error: HTTP server failed to start: %s\n", strerror(errno));
        return EXIT_FAILURE;
    }

    fprintf(stderr, "HTTP server is listening on port %u (using libmicrohttpd %s)\n", server_port, MHD_get_version());

    // Create the CoAP context
    coap_set_log_level(LOG_DEBUG);
    coap_context = coap_create_context("0.0.0.0", NULL);
    coap_register_response_handler(coap_context, coap_response_handler);

    // Now let microhttpd accept HTTP requests and wait for a signal
    pause();

    return EXIT_SUCCESS;
}
Beispiel #4
0
int main(int argc, char *argv[])  {
  struct afb_hsrv *hsrv;
  struct afb_config *config;
  struct sd_event *eventloop;

  LOGAUTH("afb-daemon");

  // ------------- Build session handler & init config -------
  config = calloc (1, sizeof (struct afb_config));

  on_exit(closeSession, config);
  parse_arguments(argc, argv, config);

  // ------------------ sanity check ----------------------------------------
  if (config->httpdPort <= 0) {
     ERROR("no port is defined");
     exit (1);
  }

  if (config->ldpaths) {
    if (afb_api_so_add_pathset(config->ldpaths) < 0) {
      ERROR("initialisation of plugins within %s failed", config->ldpaths);
      exit(1);
    }
  }

  start_items(config->items);
  config->items = NULL;

  ctxStoreInit(CTX_NBCLIENTS, config->cntxTimeout, config->token, afb_apis_count());
  if (!afb_hreq_init_cookie(config->httpdPort, config->rootapi, DEFLT_CNTX_TIMEOUT)) {
     ERROR("initialisation of cookies failed");
     exit (1);
  }

  if (afb_sig_handler_init() < 0) {
     ERROR("main fail to initialise signal handlers");
     return 1;
  }

  // let's run this program with a low priority
  nice (20);

  // ------------------ Finaly Process Commands -----------------------------
  // let's not take the risk to run as ROOT
  //if (getuid() == 0)  goto errorNoRoot;

  DEBUG("Init config done");

  // --------- run -----------
  if (config->background) {
      // --------- in background mode -----------
      INFO("entering background mode");
      daemonize(config);
  } else {
      // ---- in foreground mode --------------------
      INFO("entering foreground mode");
  }

   hsrv = start_http_server(config);
   if (hsrv == NULL)
	exit(1);

   if (config->readyfd != 0) {
		static const char readystr[] = "READY=1";
		write(config->readyfd, readystr, sizeof(readystr) - 1);
		close(config->readyfd);
  }

   // infinite loop
  eventloop = afb_common_get_event_loop();
  for(;;)
    sd_event_run(eventloop, 30000000);

  WARNING("hoops returned from infinite loop [report bug]");

  return 0;
}