// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heather.cpp
// Function: init_tcpip(unsigned int)
void init_tcpip(unsigned port)
{
static int portno = 45000;
static int use_v6 = 0;
char ip_string[512];
char port_string[80];
char *s,*eb;
int x;
int gai_err;
int af_given;

   // establish a TCPIP connection to a remote device server
   if(port >= NUM_COM_PORTS) return;
portno = 45000;
use_v6 = 0;

printf("init tcpip[%d] to %s\n", port, com[port].IP_addr);  // zork - show_debug_info
   strcpy(ip_string, com[port].IP_addr);

   //see if this is a bracketed IPv6 address
   eb = strstr(ip_string, "]");
   if(eb && (ip_string[0]=='[')) { //bracketed IPv6 address.
printf("bracketed IPv6 address\n");      // zork - show_debug_info
      //Remove the brackets and leave a zero terminated address.
      *eb=0;
      memmove(ip_string,ip_string+1,eb-ip_string);
      //check for the : after the ]
      s = strstr(eb+1, ":");
      use_v6 = 1;
      af_given = 1;
   } else { // see if IPv6 is forced by the ';' portnumber separator
      s = strstr(ip_string, ";");
      if(s) {  // IPv6 address detected
         use_v6 = 1;
         af_given = 1;
      }
      else {   // no ';' - assume IPv4 (we try that first)
         s = strstr(ip_string, ":");
         use_v6 = 0; // This sets the default address space.
         af_given = 0;
      }
   }


   if(s) {  // we have a port number string
      *s = 0;                         // chop off the port number string
      if(*(s+1)) portno = atoi(s+1);  // get the port number as an integer
   }
   if(portno < 0) portno = 45000;
   sprintf(port_string, "%d", portno);
printf("ip:%s  port:%s  use_v6:%d\n", ip_string, port_string, use_v6); // zork - show_debug_info

   retry_resolve:
   if(use_v6) hints.ai_family = AF_INET6;
   else       hints.ai_family = AF_INET;
   hints.ai_socktype = SOCK_STREAM;

   //see if we can get an address in the chosen family
   gai_err = getaddrinfo(ip_string, port_string, &hints, &res);
   if(gai_err && !af_given) {
        //we could not get an address in the default address space.
        //try the other address space
printf("No address in the chosen ai_family, trying the other.\n");  // zork - show_debug_info
        use_v6 = !use_v6;
        af_given = 1;
        if(use_v6) hints.ai_family = AF_INET6;
        else       hints.ai_family = AF_INET;
        gai_err = getaddrinfo(ip_string, port_string, &hints, &res);
   }

   if(gai_err) {
printf("ERROR, could not get IP addr info:%s  port:%s. err=%d (%s)\n", ip_string, port_string, gai_err, gai_strerror(gai_err));
      com[port].process_com = 0;
      com[port].usb_port = 0;
      com[port].com_port = 0;
      com[port].com_error |= INIT_ERR;
      return;
   }

   com[port].com_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
   if(com[port].com_fd < 0) {
      if(!af_given) { // The local machine can not handle this address family. Try the other address family.
        use_v6 = !use_v6;
        af_given = 1;
        close(com[port].com_fd);
        com[port].com_fd = (-1);
        goto retry_resolve;
      }
      error_exit(20000, "ERROR could not open IP socket");
   }
printf("%s socket opened!\n", use_v6?"IPv6":"IPv4");
printf("Attempting to connect() to socket\n");

   if(connect(com[port].com_fd, res->ai_addr, res->ai_addrlen) < 0) {
      if(!af_given) {
        // we can not connect using that address family. Try the other.
        // this happens when the machine has link local IPv6 or IPv4 only, and we are trying
        // to reach a remote IPv6/IPv4 address. In this case we get an immediate error,
        // and can retry using the other protocol if the other address is dual-stacked.
        use_v6 = !use_v6;
        af_given = 1;
        close(com[port].com_fd);
        com[port].com_fd = (-1);
        goto retry_resolve;
      }

      error_exit(20002, "ERROR connecting to IP socket.");
   }

printf("connected!\n");
   x = fcntl(com[port].com_fd, F_GETFL, 0);
   fcntl(com[port].com_fd, F_SETFL, x | O_NONBLOCK);
   com[port].port_used = 3;  // port open for TCP/IP
printf("set ip connection to non-blocking\n");
}

// Assembly:
cmpl $0x12, %edi
ja 0xb28f
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movl %edi, %esi
movl $0xafc8, 0x13e195(%rip) # imm = 0xAFC8
andl $0x0, 0x31effe(%rip)    # 0x329f40
movl %edi, %eax
imulq $0x2260, %rax, %r13     # imm = 0x2260
leaq 0x2ed38e(%rip), %r15    # 0x2f82e0
leaq (%r15,%r13), %r14
addq $0x10d, %r14            # imm = 0x10D
leaq 0xe8a79(%rip), %rdi     # 0xf39dd
xorl %r12d, %r12d
movq %r14, %rdx
xorl %eax, %eax
callq 0x9030
leaq -0x280(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9330
pushq $0x5d
popq %rsi
movq %rbx, %rdi
callq 0x9100
testq %rax, %rax
je 0xafd4
cmpb $0x5b, -0x280(%rbp)
jne 0xafd4
movq %rax, %r14
leaq 0xea69a(%rip), %rdi     # 0xf5640
callq 0x95d0
movb $0x0, (%r14)
leaq -0x27f(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movq %r14, %rdx
subq %rdi, %rdx
callq 0x9650
incq %r14
pushq $0x1
popq %r12
movq %r14, %rbx
jmp 0xaff4
pushq $0x3b
popq %rsi
movq %rbx, %rdi
callq 0x9100
testq %rax, %rax
je 0xaff4
pushq $0x1
popq %r12
movl $0x1, 0x31ef4e(%rip)    # 0x329f40
jmp 0xb00b
pushq $0x3a
popq %rsi
movq %rbx, %rdi
callq 0x9100
movl %r12d, 0x31ef3a(%rip)   # 0x329f40
testq %rax, %rax
je 0xb029
movb $0x0, (%rax)
cmpb $0x0, 0x1(%rax)
je 0xb029
incq %rax
movq %rax, %rdi
callq 0x95e0
movl %eax, %edx
movl %eax, 0x13e0a9(%rip)    # 0x1490d0
jmp 0xb02f
movl 0x13e0a1(%rip), %edx    # 0x1490d0
testl %edx, %edx
jns 0xb042
movl $0xafc8, 0x13e093(%rip) # imm = 0xAFC8
movl $0xafc8, %edx           # imm = 0xAFC8
addq %r13, %r15
leaq 0x102e50(%rip), %rsi    # 0x10de9c
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movl 0x31eee0(%rip), %ecx    # 0x329f40
leaq 0xe898c(%rip), %rdi     # 0xf39f3
leaq -0x280(%rbp), %r14
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9030
xorl %eax, %eax
cmpl $0x0, 0x31eebc(%rip)    # 0x329f40
setne %al
leal 0x2(,%rax,8), %eax
movl %eax, 0x31ee78(%rip)    # 0x329f0c
movl $0x1, 0x31ee72(%rip)    # 0x329f10
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x31ee5d(%rip), %rdx    # 0x329f08
leaq 0x31ee86(%rip), %rcx    # 0x329f38
callq 0x9490
movl %eax, %r13d
testl %eax, %eax
sete %al
testl %r12d, %r12d
setne %cl
orb %al, %cl
jne 0xb114
leaq 0xea587(%rip), %rdi     # 0xf5657
callq 0x95d0
xorl %eax, %eax
cmpl $0x0, 0x31ee62(%rip)    # 0x329f40
sete %al
movl %eax, 0x31ee59(%rip)    # 0x329f40
leal 0x2(,%rax,8), %eax
movl %eax, 0x31ee18(%rip)    # 0x329f0c
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x31ee07(%rip), %rdx    # 0x329f08
leaq 0x31ee30(%rip), %rcx    # 0x329f38
callq 0x9490
movl %eax, %r13d
pushq $0x1
popq %r12
testl %r13d, %r13d
jne 0xb1e8
movq 0x31ee14(%rip), %rax    # 0x329f38
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x90a0
movl %eax, 0x8(%r15)
testl %eax, %eax
jns 0xb154
testl %r12d, %r12d
je 0xb1d4
movl $0x4e20, %edi           # imm = 0x4E20
leaq 0xe88fb(%rip), %rsi     # 0xf3a4a
callq 0xb290
cmpl $0x0, 0x31ede5(%rip)    # 0x329f40
leaq 0xe891f(%rip), %rsi     # 0xf3a81
je 0xb16b
leaq 0xe8911(%rip), %rsi     # 0xf3a7c
leaq 0xe88f7(%rip), %rdi     # 0xf3a69
xorl %eax, %eax
callq 0x9030
leaq 0xea50d(%rip), %rdi     # 0xf568d
callq 0x95d0
movl 0x8(%r15), %edi
movq 0x31eda8(%rip), %rax    # 0x329f38
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x92b0
testl %eax, %eax
jns 0xb236
testl %r12d, %r12d
jne 0xb225
xorl %eax, %eax
cmpl $0x0, 0x31ed8e(%rip)    # 0x329f40
sete %al
movl %eax, 0x31ed85(%rip)    # 0x329f40
movl 0x8(%r15), %eax
movl %eax, %edi
callq 0x9760
orl $-0x1, 0x8(%r15)
pushq $0x1
popq %r12
jmp 0xb07b
xorl %ecx, %ecx
cmpl $0x0, 0x31ed63(%rip)    # 0x329f40
sete %cl
movl %ecx, 0x31ed5a(%rip)    # 0x329f40
jmp 0xb1bf
movl %r13d, %edi
callq 0x9500
leaq 0xe8817(%rip), %rdi     # 0xf3a0e
leaq -0x280(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movl %r13d, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x9030
andl $0x0, 0x2234(%r15)
andq $0x0, (%r15)
orb $0x4, 0x2251(%r15)
jmp 0xb27e
leaq 0xe885a(%rip), %rsi     # 0xf3a86
movl $0x4e22, %edi           # imm = 0x4E22
callq 0xb290
leaq 0xea472(%rip), %rdi     # 0xf56af
callq 0x95d0
movl 0x8(%r15), %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x91d0
movl 0x8(%r15), %edi
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x91d0
movl $0x3, 0x222c(%r15)
leaq 0xea441(%rip), %rdi     # 0xf56ba
callq 0x95d0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq