Пример #1
0
Файл: main.c Проект: zaius/smart
int main(int argc, char **argv) {
	// Seed the random number generator
	srandom(time(NULL));

	// Set up signal handlers
	signal(SIGINT, sigterm);

	serial_init();
	tunnel_init();

	// Start the main loop
	while (quit == FALSE) {
		int result = 0;

		result = poll(fds, 2, 2500);

		if (result < 0) {
			// error in poll, warn
			warn("Poll Error");
			continue;
		}

		// timed out, continue the loop
		if (result == 0) {
			printf("timeout\n");
			continue;
		}

		if (fds[SERIAL_INDEX].revents != 0) {
			serial_event();
		}

		if (fds[TUNNEL_INDEX].revents != 0) {
			tunnel_event();
		}
	}
	// We must have received a signal, tidy up and exit
	printf("Exiting...\n");
	close(serial);
	close(tunnel);

	return 0;
}
Пример #2
0
void tunnel_loop_epoll() {

    struct epoll_event events[MAX_EPOLL_EVENTS];
    int timeout;
    nabto_stamp_t ne;
    nabto_stamp_t now;
    int nfds;
    int i;

    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed to initialize unabto"));
    }

    if (!init_tunnel_module()) {
        NABTO_LOG_FATAL(("Cannot initialize tunnel module"));
        return;
    }

    unabto_time_auto_update(false);
    // time is updated here and after the select since that's the only blocking point.
    unabto_time_update_stamp();

    while (true) {
        int i;
        unabto_next_event(&ne);
        now = nabtoGetStamp();
        timeout = nabtoStampDiff2ms(nabtoStampDiff(&ne, &now));
        if (timeout < 0) {
            timeout = 0;
        }
        
        nfds = epoll_wait(unabto_epoll_fd, events, MAX_EPOLL_EVENTS, timeout);
        unabto_time_update_stamp();
        
        for (i = 0; i < nfds; i++) {
            
            unabto_epoll_event_handler* handler = (unabto_epoll_event_handler*)events[i].data.ptr;

            if (handler->epollEventType == UNABTO_EPOLL_TYPE_UDP) {
                unabto_epoll_event_handler_udp* udpHandler = (unabto_epoll_event_handler_udp*)handler;
                bool status;
                do {
                    status = unabto_read_socket(udpHandler->fd);
                } while (status);
            }
#if NABTO_ENABLE_TCP_FALLBACK
            if (handler->epollEventType == UNABTO_EPOLL_TYPE_TCP_FALLBACK) {

                nabto_connect* con = (nabto_connect*)handler;
                if (events[i].events & EPOLLIN) {
                    unabto_tcp_fallback_read_ready(con);
                }
                if (events[i].events & EPOLLOUT) {
                    unabto_tcp_fallback_write_ready(con);
                }
            }
#endif
            if (handler->epollEventType == UNABTO_EPOLL_TYPE_TCP_TUNNEL) {
                tunnel* tunnelPtr = (tunnel*)handler;
                if (tunnelPtr->sock != INVALID_SOCKET) {
                    tunnel_event(tunnelPtr, TUNNEL_EVENT_SOURCE_TCP_READ);
                }
                if (tunnelPtr->sock != INVALID_SOCKET) {
                    tunnel_event(tunnelPtr, TUNNEL_EVENT_SOURCE_TCP_WRITE);
                }
            }
        }

        unabto_time_event();
    }
    deinit_tunnel_module();
    unabto_close();
}
Пример #3
0
int main(int argc, char **argv)
{
	int ret;
	const char *chan_name;
	tunnel_t *tun;
	HANDLE h;
	time_t now;

	if (argc > 2)
		usage(argv[0]);

	chan_name = (argc == 2 ? argv[1] : RDP2TCP_CHAN_NAME);

	setup();

	do {
		if (channel_init(chan_name))
			break;

		ret = ping(&now);

		// I/O loop
		while (ret >= 0) {

			switch (event_wait(&tun, &h)) {

				case EVT_CHAN_WRITE: // virtual channel outgoing data
					debug(0, "EVT_CHAN_WRITE");
					ret = channel_write_event();
					if (!ret)
						last_ping = now;
					break;

				case EVT_CHAN_READ: // virtual channel incoming data
					debug(0, "EVT_CHAN_READ");
					ret = channel_read_event();
					if (ret >= 0)
						ping(&now);
					break;

				case EVT_TUNNEL: // tcp tunnel incoming/outgoing data
					debug(0, "EVT_TUNNEL");
					ret = tunnel_event(tun, h);
					break;

				case EVT_PING: // ping delay
					if (channel_is_connected()) {
						debug(0, "EVT_PING");
						ret = ping(&now);
					} else {
						debug(0, "channel still disconnected");
					}
					break;

				default:
					ret = -1;

			}

		}

		channel_kill();
		Sleep(1000);

	} while (1);

	bye();
	return 0;
}