Пример #1
0
int main(int argc, char *argv[])
{
  struct relay *relays;
  int relay_count, is_server;
  int i;
  fd_set readfds;
  int max = 0;
  int ok;

  parse_args(argc, argv, &relays, &relay_count, &is_server);

  for (i = 0; i < relay_count; i++) {
    if (is_server) {
      setup_server_listen(&relays[i]);
    }
    else {
      setup_tcp_client(&relays[i]);
    }
    setup_udp_recv(&relays[i]);
    setup_udp_send(&relays[i]);
  }

  if (is_server) {
    await_incoming_connections(relays, relay_count);
  }

  do {
    FD_ZERO(&readfds);
    for (i = 0; i < relay_count; i++) {
      FD_SET(relays[i].tcp_sock, &readfds);
      SET_MAX(relays[i].tcp_sock);
      FD_SET(relays[i].udp_recv_sock, &readfds);
      SET_MAX(relays[i].udp_recv_sock);
    }

    if (select(max, &readfds, NULL, NULL, NULL) < 0) {
      if (errno != EINTR) {
        perror("main loop: select");
        exit(1);
      }
    }

    ok = 0;
    for (i = 0; i < relay_count; i++) {
      if (FD_ISSET(relays[i].tcp_sock, &readfds)) {
        ok += tcp_to_udp(&relays[i]);
      }
      if (FD_ISSET(relays[i].udp_recv_sock, &readfds)) {
        ok += udp_to_tcp(&relays[i]);
      }
    }
  } while (ok == 0);

  exit(0);
} /* main */
Пример #2
0
void receive(void)
{
	struct net_context *udp_recv4 = { 0 };
	struct net_context *udp_recv6 = { 0 };
	struct net_context *tcp_recv4 = { 0 };
	struct net_context *tcp_recv6 = { 0 };

	if (!get_context(&udp_recv4, &udp_recv6,
			 &tcp_recv4, &tcp_recv6)) {
		NET_ERR("Cannot get network contexts");
		return;
	}

	NET_INFO("Starting to wait");

#if defined(CONFIG_NET_TCP)
	setup_tcp_accept(tcp_recv4, tcp_recv6);
#endif

#if defined(CONFIG_NET_UDP)
	setup_udp_recv(udp_recv4, udp_recv6);
#endif

	k_sem_take(&quit_lock, K_FOREVER);

	NET_INFO("Stopping...");

#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_UDP)
	net_context_put(udp_recv6);
#endif

#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_UDP)
	net_context_put(udp_recv4);
#endif

#if defined(CONFIG_NET_IPV6) && defined(CONFIG_NET_TCP)
	net_context_put(tcp_recv6);
#endif

#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_TCP)
	net_context_put(tcp_recv4);
#endif
}
Пример #3
0
static void listen(void)
{
	struct net_context *udp_recv6 = { 0 };
	struct net_context *tcp_recv6 = { 0 };
	struct net_context *mcast_recv6 = { 0 };

	if (!get_context(&udp_recv6, &tcp_recv6, &mcast_recv6)) {
		printk("Cannot get network contexts");
		return;
	}

	printk("Starting to wait");

	setup_tcp_accept(tcp_recv6);
	setup_udp_recv(udp_recv6);

	k_sem_take(&quit_lock, K_FOREVER);

	printk("Stopping...");

	net_context_put(udp_recv6);
	net_context_put(mcast_recv6);
	net_context_put(tcp_recv6);
}