// File: xocoder[P]m_kcptun/src/kcp_remote.cpp
// Function: remote_network_init(tun_remote_t*)
static int
_remote_network_init(tun_remote_t *tun) {
   if (tun && !tun->is_init) {
      mnet_init();

      // tcp list
      tun->session_lst = skt_create();

      // crytpo
      if (tun->conf->crypto) {
         tun->ukey = rc4_hash_key((const char*)tun->conf->key, strlen(tun->conf->key));
         tun->ti = mtime_current();
      }

      // udp
      for (int i=0; i<tun->conf->dest_count; i++) {
         tun->udpin[i] = mnet_chann_open(CHANN_TYPE_DGRAM);
         if (tun->udpin[i] == NULL) {
            cerr << "Fail to create udp in !" << endl;
            return 0;
         }

         mnet_chann_set_cb(tun->udpin[i], _remote_udpin_callback, tun);
         if (mnet_chann_listen(tun->udpin[i], tun->conf->src_ip[i], tun->conf->src_port[i], 1) <= 0) {
            cerr << "Fail to connect to udp in !" << endl;
            return 0;
         }
         mnet_chann_set_bufsize(tun->udpin[i], 2048*1024);
      }

      // kcp
      if ( !_remote_kcpin_create(tun) ) {
         return 0;
      }

      // setup timer
      tun->tmr = tmr_create_lst();
      tun->tm = tmr_add(tun->tmr, tun->ti, 10000, 1, tun, _remote_tmr_callback);

      if (tun->conf->rs_data) {
         tun->rt = rskcp_create(tun->conf->rs_data, tun->conf->rs_parity);
      }
      
      tun->is_init = 1;
      return 1;
   }
   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xd7df
movq -0x10(%rbp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xd7df
callq 0x6740
callq 0xb340
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
cmpl $0x0, 0x148(%rax)
je 0xd5f7
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
addq $0x158, %rax            # imm = 0x158
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x3140
movq -0x20(%rbp), %rdi
movl %eax, %esi
callq 0x9120
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
callq 0x9a40
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x78(%rcx), %rcx
cmpl 0x94(%rcx), %eax
jge 0xd723
movl $0x2, %edi
callq 0x71a0
movq %rax, %rdx
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
cmpq $0x0, 0x8(%rax,%rcx,8)
jne 0xd66d
movq 0xb9aa(%rip), %rdi      # 0x18ff0
leaq 0x792b(%rip), %rsi      # 0x14f78
callq 0x32d0
movq %rax, %rdi
movq 0xb964(%rip), %rsi      # 0x18fc0
callq 0x3320
movl $0x0, -0x4(%rbp)
jmp 0xd7e6
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rdx
leaq 0x43b(%rip), %rsi       # 0xdac0
callq 0x7940
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rsi
addq $0x4, %rsi
movslq -0x14(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x14(%rbp), %rcx
movl 0x84(%rax,%rcx,4), %edx
movl $0x1, %ecx
callq 0x7880
cmpl $0x0, %eax
jg 0xd6fe
movq 0xb919(%rip), %rdi      # 0x18ff0
leaq 0x78b2(%rip), %rsi      # 0x14f90
callq 0x32d0
movq %rax, %rdi
movq 0xb8d3(%rip), %rsi      # 0x18fc0
callq 0x3320
movl $0x0, -0x4(%rbp)
jmp 0xd7e6
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
movl $0x200000, %esi         # imm = 0x200000
callq 0x8100
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xd5fe
movq -0x10(%rbp), %rdi
callq 0xddd0
cmpl $0x0, %eax
jne 0xd73d
movl $0x0, -0x4(%rbp)
jmp 0xd7e6
callq 0xac70
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rsi
movq -0x10(%rbp), %r8
movl $0x2710, %edx           # imm = 0x2710
movl $0x1, %ecx
leaq 0x7c8(%rip), %r9        # 0xdf40
callq 0xb070
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
cmpl $0x0, 0x14c(%rax)
je 0xd7cb
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x14c(%rax), %edi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x150(%rax), %esi
callq 0xe6c0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x70(%rax)
movl $0x1, -0x4(%rbp)
jmp 0xd7e6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nop