static void
task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct sockaddr_in v4;

  ls = open_listen_socket ();
  lsock = GNUNET_CONNECTION_create_from_existing (ls);
  GNUNET_assert (lsock != NULL);

#if HAVE_SOCKADDR_IN_SIN_LEN
  v4.sin_len = sizeof (v4);
#endif
  v4.sin_family = AF_INET;
  v4.sin_port = htons (PORT);
  v4.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  csock =
      GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
                                              (const struct sockaddr *) &v4,
                                              sizeof (v4));
  GNUNET_assert (csock != NULL);
  GNUNET_assert (NULL !=
                 GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
                                                          GNUNET_TIME_UNIT_SECONDS,
                                                          &make_hello, NULL));
  GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept,
                                 cls);
}
Beispiel #2
0
static void
task_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{

  ls = open_listen_socket ();
  lsock = GNUNET_CONNECTION_create_from_existing (ls);
  GNUNET_assert (lsock != NULL);
  csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
  GNUNET_assert (csock != NULL);
  GNUNET_assert (NULL !=
                 GNUNET_CONNECTION_notify_transmit_ready (csock, 1024,
                                                          GNUNET_TIME_UNIT_SECONDS,
                                                          &send_kilo, cls));
}
Beispiel #3
0
static void
task (void *cls)
{
  ls = open_listen_socket ();
  lsock = GNUNET_CONNECTION_create_from_existing (ls);
  GNUNET_assert (lsock != NULL);
  csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
  GNUNET_assert (csock != NULL);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n");
  GNUNET_assert (NULL !=
                 GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
                                                          GNUNET_TIME_UNIT_SECONDS,
                                                          &make_hello, NULL));
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n");
  GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept,
                                 cls);
}
Beispiel #4
0
int epoll_loop() {
#define MAX_EVENTS 10
    int epollfd, nfds, n, listen_sock, conn_sock;
    struct epoll_event ev, events[MAX_EVENTS];

    epollfd = epoll_create1(EPOLL_CLOEXEC);
    if (epollfd == -1) {
        return -1;
    }

    listen_sock = open_listen_socket();
    ev.events = EPOLLIN;
    ev.data.fd = listen_sock;
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) {
        perror("epoll_ctl: listen_sock");
        //exit(EXIT_FAILURE);
    }

    for (;;) {
        nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
        if (nfds == -1) {
        }

        for (n = 0; n < nfds; ++n) {
            if (events[n].data.fd == listen_sock) {
                conn_sock = accept_connect(listen_sock);
                if (setnonblocking(conn_sock) < 0) {
                    perror("setnonblocking");
                }
                ev.events = EPOLLIN | EPOLLET;
                ev.data.fd = conn_sock;
                if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) {

                }
            } else {
                int fd = events[n].data.fd;
                char buf[255];
                int len = recv(fd, buf, 255, MSG_DONTWAIT);
                buf[len] = '\0';
                printf("%s\n", buf);
            }
        }
    }
    return 0;
}
int main(int argc, char *argv[])
{
    int res = 0;

    /* Parse command line parameters */
    if (0 != (res = parse_peer_params(argc, argv, &proc_id, &fname))) {
        dbg_err("parse_args() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Parse the DME simulation config file.
     */
    if (0 != (res = parse_file(fname, proc_id, &nodes, &nodes_count))) {
        dbg_err("parse_file() returned nonzero status:%d", res);
        goto end;
    }

    /* Initialize and allocate any structures/vars used by the generic algorithm */

    /* struct genericstruct = calloc(...) */

    /*
     * Init connections (open listening socket)
     */
    if (0 != (res = open_listen_socket(proc_id, nodes, nodes_count))) {
        dbg_err("open_listen_socket() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Register signals (for I/O, alarms, etc.)
     */
    if (0 != (res = init_handlers(nodes[proc_id].sock_fd))) {
        dbg_err("init_handlers() returned nonzero status");
        goto end;
    }

    /* Register event handlers that will be called automatically on event occurrence */
    register_event_handler(DME_EV_SUP_MSG_IN, handle_supervisor_msg);
    register_event_handler(DME_EV_PEER_MSG_IN, handle_peer_msg);
    register_event_handler(DME_EV_WANT_CRITICAL_REG, process_ev_want_cr);
    register_event_handler(DME_EV_ENTERED_CRITICAL_REG, process_ev_entered_cr);
    register_event_handler(DME_EV_EXITED_CRITICAL_REG, process_ev_exited_cr);

    /*
     * Main loop: just sit here and wait for events (triggered by the supervisor).
     * All work is done in interrupt handlers mapped to registered functions.
     */
    wait_events();

end:
    /*
     * Do cleanup (deallocating dynamic structures)
     */
    deinit_handlers();

    /* Close our listening socket */
    if (nodes && nodes[proc_id].sock_fd > 0) {
        close(nodes[proc_id].sock_fd);
    }

    /* Free allocated structures */
    safe_free(nodes);

    return res;
}
Beispiel #6
0
static int32 main_loop()
{
	int32 		rc = 0;
	int32 		conn_fd = -1;
	int32 		listen_fd = -1;
	fd_set		rfds;
	socklen_t	addrlen = 0;
	usockaddr	usa;
	pthread_t	tid_ipmi_cb;
	struct sigaction	sa;
	struct http_request *request = NULL;
	int32 port  = 0;

	/* ingore sigpipe */
	sa.sa_handler = SIG_IGN;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGPIPE, &sa, 0);
	
	rest_register_handlers();

	port = rmm_cfg_get_port(RESTD_PORT);
	listen_fd = open_listen_socket(port);
	if (listen_fd == -1) {
		HTTPD_ERR("Failed to open HTTP server!\n");
		return -1;
	}

	fprintf(stderr, "Mini-HTTP Server is Running ...\n");

	/* Create IPMI callback thread. */
	if (pthread_create(&tid_ipmi_cb, NULL, ipmi_cb_thread, NULL) != 0) {
		close(listen_fd);
		HTTPD_ERR("Failed to create ipmi callback thread!\n");
		return -1;
	}
	for (;;) {
			FD_ZERO(&rfds);
			FD_SET(listen_fd, &rfds);
			rc = select(listen_fd + 1, &rfds, NULL, NULL, NULL);
			if (rc <= 0)
				continue;
	
			addrlen = sizeof(usa);
			conn_fd = accept(listen_fd, &usa.sa, &addrlen);
			if (conn_fd < 0)
				continue;
			HTTPD_INFO("restd accepted a new connection: fd = %d\n", conn_fd);
	
			request = malloc(sizeof(struct http_request));
			if (NULL == request) {
				HTTPD_ERR("Malloc error: %s\n", strerror(errno));
				return -1;
			}
			memset(request, 0, sizeof(struct http_request));
			request->fd = conn_fd;
			request->from = usa;
	
			if (pthread_create(&request->tid_rest_req, NULL, http_process, (void *)request) != 0) {
				HTTPD_ERR("Failed to create rest_req_thread!\n");
				return -1;
			}
		}

	return 0;
}
Beispiel #7
0
int main(int argc, char **argv) {
    int listen_sd;
    int sd;
    int c;
    pthread_t thread_id;

    cfg.listen_host = "127.0.0.1";
    cfg.listen_port = 8443;
    cfg.ca_cert_file = DEFAULT_CA_CERT_FILE;
    cfg.ca_key_file = DEFAULT_CA_KEY_FILE;
    cfg.cert_cache_path = DEFAULT_CERT_CACHE_PATH;
    cfg.client_priority = DEFAULT_CLIENT_PRIORITY;
    cfg.accounts_inline = NULL;

    int detect_only = 0;
    char *detect_host = NULL;

    while ((c = getopt(argc, argv, "a:C:D:K:m:l:p:h")) != -1) {
        switch (c) {
        case 'a':
            cfg.accounts_inline = optarg;
            break;
        case 'l':
            cfg.listen_host = optarg;
            break;
        case 'p':
            cfg.listen_port = atoi(optarg);
            break;
        case 'C':
            cfg.ca_cert_file = optarg;
            break;
        case 'K':
            cfg.ca_key_file = optarg;
            break;
        case 'm':
            cfg.cert_cache_path = optarg;
            break;
        case 'D':
            detect_only = 1;
            detect_host = optarg;
            break;
        case '?':
            if (isprint(optopt))
                fprintf(stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf(stderr,
                        "Unknown option character `\\x%x'.\n",
                        optopt);
        case 'h':
        default:
            print_usage(argv[0]);
            return 1;
        }
    }

    init_gnutls(&cfg);
    load_ca_cert_and_key(&global_ca, cfg.ca_cert_file, cfg.ca_key_file);
    srand(time(NULL));

    if (detect_only) {
        fprintf(stderr, "Detecting whether %s:443 supports TLS-SRP...\n", detect_host);
        return site_uses_tls_srp(detect_host, 443) != 1; /* returncode 0 is success */
    }

    listen_sd = open_listen_socket(cfg.listen_host, cfg.listen_port);
    if (listen_sd == -1)
        exit(1);

    for (;;) {
        sd = do_accept(listen_sd);
        pthread_create(&thread_id, NULL, connection_thread, (void*)(long)sd);
    }

    close(listen_sd);
    global_deinit();

    return 0;
}
int main(int argc, char *argv[])
{
    int res = 0;
    
    if (0 != (res = parse_sup_params(argc, argv, &fname, &logfname,
                                     &concurency_ratio, &max_concurrent_proc,
                                     &election_interval, &number_of_runs))) {
        dbg_err("parse_args() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Parse the file fname
     */
    if (0 != (res = parse_file(fname, proc_id, &nodes, &nodes_count))) {
        dbg_err("parse_file() returned nonzero status:%d", res);
        goto end;
    }
    dbg_msg("nodes has %d elements", nodes_count);

    /* compute the number of maximum concurrent processes (nearest integer) */
    if (max_concurrent_proc != 0) {
        /* The '-c' option was specified on the command line */
        fixed_concurent_num = TRUE;
    } else {
        /* Compute based on concurrency ratio */
        max_concurrent_proc = (nodes_count * concurency_ratio + 50) / 100;
        fixed_concurent_num = FALSE;
    }

    if (max_concurrent_proc < 2) {
        dbg_err("concurrency ratio or number set too low. At least 2 processes must be concurrent.");
        goto end;
    } else if (max_concurrent_proc > nodes_count) {
        dbg_err("concurrency number is greater than total number of processes. Setting it to maximum.");
        max_concurrent_proc = nodes_count;
    }

    dbg_msg("max_concurrent_proc=%d", max_concurrent_proc);

    randomizer_init();
    
    /* Allocate the statistics collection storage and open the log file */
    synchro_delays = calloc(nodes_count, sizeof(timespec_t));
    response_times = calloc(nodes_count, sizeof(timespec_t));

    if (NULL == (log_fh = fopen(logfname, "w"))) {
        dbg_err("Could not open log file %s for writing", logfname);
        res = ERR_BADFILE;
        goto end;
    }


    /*
     * Init connections (open listening socket)
     */
    if (0 != (res = open_listen_socket(proc_id, nodes, nodes_count))) {
        dbg_err("open_listen_socket() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Register signals (for I/O, alarms, etc.)
     */
    if (0 != (res = init_handlers(nodes[proc_id].sock_fd))) {
        dbg_err("init_handlers() returned nonzero status");
        goto end;
    }
    
    register_event_handler(DME_SEV_PERIODIC_WORK, do_work);
    register_event_handler(DME_SEV_SYNCRO, syncro);
    register_event_handler(DME_SEV_ENDSIMULATION, end_simulation);
    register_event_handler(DME_SEV_MSG_IN, process_messages);

    /* wait for peers processes to init, then kick start the supervisor */
    sleep(1);
    
    /* Start working; mark time */
    clock_gettime(CLOCK_REALTIME, &tstamp_supervisor_start);

    handle_event(DME_SEV_SYNCRO, &tstamp_supervisor_start);
    sleep(1);

    handle_event(DME_SEV_PERIODIC_WORK, NULL);

    /*
     * Main loop: just sit here and wait for interrupts.
     * All work is done in interrupt handlers mapped to registered functions.
     */
    wait_events();
    
end:
    /*
     * Do cleanup (deallocating dynamic structures)
     */
    deinit_handlers();

    /* Close our listening socket */
    if (nodes && nodes[proc_id].sock_fd > 0) {
        close(nodes[proc_id].sock_fd);
    }

    if (log_fh) {
        fclose(log_fh);
    }

    safe_free(nodes);
    safe_free(synchro_delays);
    safe_free(response_times);
    
    return res;
}