Ejemplo n.º 1
0
void create_tunnel(struct am_device *device, uint16_t src_port, uint16_t dst_port)
{
  struct mobiletunnel t;
  int reuse = 1;
  struct sockaddr_in addr;
  socklen_t addr_size;

  // initialize tunnel server socket
  init_tunnel(&t);
  t.device = device;
  t.src_port = src_port;
  t.dst_port = dst_port;

  t.sock = socket(AF_INET, SOCK_STREAM, 0);
  ASSERT_OR_EXIT(t.sock != -1, &t, "!socket\n");

  setsockopt(t.sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));

  // specify tunnel server address (localhost) and port
  addr_size = sizeof(addr);
  memset(&addr, 0, addr_size);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  addr.sin_port = htons(src_port);

  // bind server socket to the address we specified
  if ((bind(t.sock, (struct sockaddr *)&addr, addr_size) != 0) ||
      (listen(t.sock, 10) != 0))
  {
    ASSERT_OR_EXIT(0, &t, "Failed to bind to port %d!\n", src_port);
  }

  // if user passes port 0 as local port, we want to print out the real port.
  if ((src_port == 0) &&
      (getsockname(t.sock, (struct sockaddr *)&addr, &addr_size) == 0))
  {
    src_port = ntohs(addr.sin_port);
  }

  FLPRINT("Tunneling from local port %u to device port %u...\n", src_port, dst_port);

  // register termination function
  tunnel = &t;
  signal(SIGHUP, terminate_tunnel_at_exit);
  signal(SIGINT, terminate_tunnel_at_exit);
  signal(SIGQUIT, terminate_tunnel_at_exit);
  signal(SIGTERM, terminate_tunnel_at_exit);

  // accept connections and tunnel them
  tunnel_loop(&t);
}
/**
 * This is a simple example of a custom tunnel implementation.
 */
int main() {
    nabto_main_setup* nms = unabto_init_context();

    // Setup device id.
    nms->id = "deviceid";
    
    // Setup encryption.
    nms->cryptoSuite = CRYPT_W_AES_CBC_HMAC_SHA256;
    nms->secureAttach = true;
    nms->secureData = true;
    // Copy the preshared key into unabto
    // memcpy(nms->presharedKey, key,16);
    
    // Loop forever.
    tunnel_loop();
}
void* main_routine(void* args) {

    nabto_main_setup* nms;
    const char* id = (const char*)args;

    nms = unabto_init_context();
    nms->id = id;
    nms->localPort = 0;
    nms->secureAttach = true;
    nms->secureData = true;
    nms->localPort = 0;
    nms->cryptoSuite = CRYPT_W_AES_CBC_HMAC_SHA256;
    nms->controllerArg.addr = bsip;
    if (random_local_address) {
        nms->ipAddress = 0x7f000001 + (rand() % 1000);
    }

    tunnel_loop();

    free(args);
    return 0;
    
}
int main(int argc, char** argv)
{
#if USE_TEST_WEBSERVER
#ifdef WIN32
    HANDLE testWebserverThread;
#else
    pthread_t testWebserverThread;
#endif
#endif

    nabto_main_setup* nms = unabto_init_context();
    if (!tunnel_parse_args(argc, argv, nms)) {
        NABTO_LOG_FATAL(("failed to parse commandline args"));
    }

#if USE_TEST_WEBSERVER
    if (testWebserver) {
#ifdef WIN32
        testWebserverThread = CreateThread(NULL, 0, test_webserver, (void*)testWebserverPortStr, NULL, NULL);
#else
        pthread_create(&testWebserverThread, NULL, test_webserver, (void*)testWebserverPortStr);
#endif
    }
#endif

#if HANDLE_SIGNALS
#ifdef WIN32
    signal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
#endif
    signal(SIGTERM, handle_signal);
    signal(SIGINT, handle_signal);
#endif
        
    tunnel_loop();
    return 0;
}