// File: /workspace/github_cpp_makefile_star2_O1/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 0x8a71
cmpl $0x1, %eax
jne 0x8c67
movq 0x18(%rbx), %r15
movq 0x8(%rbx), %rdi
leaq 0x98(%r15), %r14
movq %r14, %rsi
movl $0x10000, %edx          # imm = 0x10000
callq 0x4c53
movl %eax, %r13d
cmpl $0x18, %eax
jl 0x8bb8
movq %r15, -0x38(%rbp)
movq 0x90(%r15), %rdi
xorl %r15d, %r15d
testq %rdi, %rdi
setne %r15b
je 0x8aca
movl %r13d, %esi
callq 0x9356
movl %eax, %r12d
jmp 0x8ad1
movq 0x7558(%rip), %r14      # 0xffd0
leaq 0x35c2(%rip), %rsi      # 0xc041
movl $0x17, %edx
movq %r14, %rdi
callq 0x22d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21c0
movq 0x8(%rbx), %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x463a
movl %r13d, %r12d
addl $-0x8, %r12d
leaq (%r14,%r15,2), %rax
movq -0x38(%rbp), %r15
movq 0x90(%r15), %rdi
movslq %r12d, %rcx
addq %r14, %rcx
testq %rdi, %rdi
movq %rax, -0x30(%rbp)
je 0x8b03
movq %r14, %rsi
movl %r13d, %edx
callq 0x9431
movl %eax, %r13d
movzwl (%r14), %r12d
jmp 0x8b14
movq %rax, %rdi
movl %r12d, %esi
movq %rcx, %rdx
callq 0x5c85
movl %eax, %r13d
testl %r13d, %r13d
je 0x8bb8
movq 0x78(%r15), %rax
cmpl $0x0, 0x148(%rax)
je 0x8b56
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 0x5a22
movl %eax, %r12d
testl %eax, %eax
jns 0x8b5a
jmp 0x8c67
movq -0x30(%rbp), %r14
cmpl $0x18, %r12d
jl 0x8bb8
leaq 0x18(%r14), %rdi
leal -0x18(%r12), %esi
leaq -0x50(%rbp), %rdx
callq 0x7154
testl %eax, %eax
je 0x8ba4
cmpl $0x1, -0x50(%rbp)
jne 0x8ba4
cmpb $0x4, -0x48(%rbp)
jne 0x8ba4
movl 0x50(%r15), %ebx
movq %r14, %rdi
callq 0x3f01
cmpl %eax, %ebx
je 0x8ba4
movq %r14, %rdi
callq 0x3f01
movq %r15, %rdi
movl %eax, %esi
callq 0x8db2
movq 0x48(%r15), %rdi
movl %r12d, %edx
movq %r14, %rsi
callq 0x307e
jmp 0x8c67
movslq %r13d, %r14
movq 0x8(%rbx), %rdi
leaq -0x50(%rbp), %rbx
movq %rbx, %rsi
callq 0x50ef
movq 0x73fe(%rip), %r15      # 0xffd0
leaq 0x3448(%rip), %rsi      # 0xc021
movl $0x18, %edx
movq %r15, %rdi
callq 0x22d0
movq %rbx, %rdi
callq 0x2100
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x22d0
leaq 0x2f8e(%rip), %rsi      # 0xbb91
movl $0x1, %edx
movq %r15, %rdi
callq 0x22d0
movl 0x10(%rbx), %esi
movq %r15, %rdi
callq 0x23f0
movq %rax, %rbx
leaq 0x3415(%rip), %rsi      # 0xc03a
movl $0x6, %edx
movq %rax, %rdi
callq 0x22d0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x21c0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq