// File: xocoder[P]m_kcptun/src/kcp_local.cpp
// Function: local_network_init(tun_local_t*)
static int
_local_network_init(tun_local_t *tun) {
   if (tun && !tun->is_init) {
      mnet_init();

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

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

      // tcp listen
      chann_t *tcp = mnet_chann_open(CHANN_TYPE_STREAM);
      if (tcp == NULL) {
         cerr << "Fail to create listen tcp !" << endl;
         return 0;
      }

      mnet_chann_set_cb(tcp, _local_tcpin_listen, tun);
      if (mnet_chann_listen(tcp, tun->conf->src_ip[0], tun->conf->src_port[0], 1) <= 0) {
         cerr << "Fail to listen tcpin !" << endl;
         return 0;
      }

      // udp
      if ( !_local_udpout_create(tun) ) {
         return 0;
      }
      
      // kcp
      if ( !_local_kcpout_create(tun) ) {
         return 0;
      }

      // when setup kcp, reset remote kcp
      {
         unsigned char buf[16] = {0};
         if ( proto_mark_cmd(buf, 0, PROTO_CMD_RESET) ) {
            ikcp_send(tun->kcpout, (const char*)buf, 16);
         }
      }

      // setup timer
      tun->tmr = tmr_create_lst();
      tun->tm = tmr_add(tun->tmr, tun->ti, 10000, 1, tun, _local_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 $0x40, %rsp
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0xd60a
movq -0x10(%rbp), %rax
cmpl $0x0, 0x70(%rax)
jne 0xd60a
callq 0x6750
callq 0xb350
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
cmpl $0x0, 0x148(%rax)
je 0xd447
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
addq $0x158, %rax            # imm = 0x158
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rdi
addq $0x158, %rdi            # imm = 0x158
callq 0x3150
movq -0x38(%rbp), %rdi
movl %eax, %esi
callq 0x9130
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
callq 0x9a50
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x68(%rax)
movl $0x1, %edi
callq 0x71b0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xd48a
movq 0xab8d(%rip), %rdi      # 0x17ff0
leaq 0x6ad1(%rip), %rsi      # 0x13f3b
callq 0x32f0
movq %rax, %rdi
movq 0xab47(%rip), %rsi      # 0x17fc0
callq 0x3340
movl $0x0, -0x4(%rbp)
jmp 0xd611
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0x467(%rip), %rsi       # 0xd900
callq 0x7950
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rsi
addq $0x4, %rsi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movl 0x84(%rax), %edx
movl $0x1, %ecx
callq 0x7890
cmpl $0x0, %eax
jg 0xd4f9
movq 0xab1e(%rip), %rdi      # 0x17ff0
leaq 0x6a7e(%rip), %rsi      # 0x13f57
callq 0x32f0
movq %rax, %rdi
movq 0xaad8(%rip), %rsi      # 0x17fc0
callq 0x3340
movl $0x0, -0x4(%rbp)
jmp 0xd611
movq -0x10(%rbp), %rdi
callq 0xda30
cmpl $0x0, %eax
jne 0xd513
movl $0x0, -0x4(%rbp)
jmp 0xd611
movq -0x10(%rbp), %rdi
callq 0xdb70
cmpl $0x0, %eax
jne 0xd52d
movl $0x0, -0x4(%rbp)
jmp 0xd611
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31c0
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xbca0
cmpl $0x0, %eax
je 0xd568
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rdi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0x4420
callq 0xac80
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 0x76d(%rip), %r9        # 0xdd10
callq 0xb080
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
cmpl $0x0, 0x14c(%rax)
je 0xd5f6
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 0xe530
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x90(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x70(%rax)
movl $0x1, -0x4(%rbp)
jmp 0xd611
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)