// File: /workspace/github_cpp_makefile_star2_O3/xocoder[P]m_kcptun/src/kcp_remote.cpp
// Function: remote_udpin_callback(chann_msg_t*)
static void
_remote_udpin_callback(chann_msg_t *e) {
   tun_remote_t *tun = (tun_remote_t*)e->opaque;   

   switch (e->event) {

      case CHANN_EVENT_RECV: {
         long ret = mnet_chann_recv(e->n, tun->buf, MKCP_BUF_SIZE);

         if (ret >= MKCP_OVERHEAD) {
            const int rs_offset = tun->rt ? sizeof(uint16_t) : 0;
            uint8_t *data = tun->buf + rs_offset;
            int data_len = tun->rt ? rskcp_dec_info(tun->rt, ret) : (ret - XOR64_CHECKSUM_SIZE);

            if ( tun->rt ) {
               ret = rskcp_decode(tun->rt, tun->buf, ret, &tun->buf[data_len]);
               data_len = *((uint16_t*)tun->buf); // restore data_len
            } else {
               ret = xor64_checksum_check(data, data_len, &tun->buf[data_len]);
            }

            if ( ret ) {
               if ( tun->conf->crypto ) {
                  data_len = rc4_decrypt((const char*)data, data_len,
                                         (char*)tun->buf, MKCP_BUF_SIZE,
                                         tun->ukey, (tun->ti>>20));
                  if (data_len < 0) {
                     return;
                  }
                  data = tun->buf;
               }

               if (data_len >= MKCP_OVERHEAD) {
                  proto_t pr;
                  const uint8_t *proto_data = &data[MKCP_OVERHEAD];

                  if (proto_probe(proto_data, data_len - MKCP_OVERHEAD, &pr) &&
                      pr.ptype == PROTO_TYPE_CTRL &&
                      pr.u.cmd == PROTO_CMD_RESET &&
                      tun->kcpconv != ikcp_getconv(data))
                  {
                     _remote_kcpin_reset(tun, ikcp_getconv(data));
                  }

                  ikcp_input(tun->kcpin, (const char*)data, data_len);
                  break;
               }
            }
         }

         chann_addr_t addr;
         mnet_chann_addr(e->n, &addr);
         cout << "invalid udp packet from "
              << addr.ip << ":" << addr.port
              << " ret: " << ret << endl;
         break;
      }

      case CHANN_EVENT_DISCONNECT:  {
         cout << "remote udp disconnect !" << endl;
         mnet_chann_close(e->n);
         break;
      }

      default: {
         break;
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl (%rdi), %eax
cmpl $0x5, %eax
je 0x8944
cmpl $0x1, %eax
jne 0x8b10
movq 0x18(%rbx), %r15
movq 0x8(%rbx), %rdi
leaq 0x98(%r15), %r14
movq %r14, %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x4bf4
movl %eax, %r13d
cmpl $0x18, %eax
jl 0x8a61
movq 0x90(%r15), %rdi
xorl %eax, %eax
testq %rdi, %rdi
setne %al
leaq (%r14,%rax,2), %rax
movq %rax, -0x30(%rbp)
je 0x899d
movl %r13d, %esi
callq 0x922e
movl %eax, %r12d
movq 0x90(%r15), %rdi
testq %rdi, %rdi
je 0x89a6
movslq %r12d, %rcx
addq %r14, %rcx
movq %r14, %rsi
movl %r13d, %edx
callq 0x9307
movl %eax, %r13d
movzwl (%r14), %r12d
jmp 0x89bc
movq 0x7685(%rip), %r14      # 0xffd0
leaq 0x36ef(%rip), %rsi      # 0xc041
movl $0x17, %edx
movq %r14, %rdi
callq 0x22e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21d0
movq 0x8(%rbx), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x45ce
addl $-0x8, %r13d
movl %r13d, %r12d
jmp 0x89aa
movq -0x30(%rbp), %rax
movslq %r12d, %rsi
leaq (%r14,%rsi), %rdx
movq %rax, %rdi
callq 0x5bfd
movl %eax, %r13d
testl %r13d, %r13d
je 0x89fa
movq 0x78(%r15), %rax
cmpl $0x0, 0x148(%rax)
je 0x89ff
movq 0x60(%r15), %r8
movq 0x68(%r15), %r9
shrq $0x14, %r9
movq -0x30(%rbp), %rdi
movl %r12d, %esi
movq %r14, %rdx
movl $0x10000, %ecx          # imm = 0x10000
callq 0x599a
movl %eax, %r12d
testl %eax, %eax
jns 0x8a03
jmp 0x8b10
xorl %r13d, %r13d
jmp 0x8a61
movq -0x30(%rbp), %r14
cmpl $0x18, %r12d
jl 0x8a61
leaq 0x18(%r14), %rdi
leal -0x18(%r12), %esi
leaq -0x48(%rbp), %rdx
callq 0x708c
testl %eax, %eax
je 0x8a4d
cmpl $0x1, -0x48(%rbp)
jne 0x8a4d
cmpb $0x4, -0x40(%rbp)
jne 0x8a4d
movl 0x50(%r15), %ebx
movq %r14, %rdi
callq 0x3ebd
cmpl %eax, %ebx
je 0x8a4d
movq %r14, %rdi
callq 0x3ebd
movq %r15, %rdi
movl %eax, %esi
callq 0x8c5b
movq 0x48(%r15), %rdi
movl %r12d, %edx
movq %r14, %rsi
callq 0x30c4
jmp 0x8b10
movslq %r13d, %r14
movq 0x8(%rbx), %rdi
leaq -0x48(%rbp), %rbx
movq %rbx, %rsi
callq 0x5082
movq 0x7555(%rip), %r15      # 0xffd0
leaq 0x359f(%rip), %rsi      # 0xc021
movl $0x18, %edx
movq %r15, %rdi
callq 0x22e0
movq %rbx, %rdi
callq 0x2100
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x22e0
leaq 0x30e5(%rip), %rsi      # 0xbb91
movl $0x1, %edx
movq %r15, %rdi
callq 0x22e0
movl 0x10(%rbx), %esi
movq %r15, %rdi
callq 0x2400
movq %rax, %rbx
leaq 0x356c(%rip), %rsi      # 0xc03a
movl $0x6, %edx
movq %rax, %rdi
callq 0x22e0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2260
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21d0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq