// File: /workspace/github_cpp_makefile_star2_O1/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 0xb424
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, 0x154014(%rip) # imm = 0xAFC8
movl $0x0, 0x334e7a(%rip)    # 0x33ff40
movl %edi, %eax
imulq $0x2260, %rax, %r13     # imm = 0x2260
leaq 0x30320a(%rip), %r15    # 0x30e2e0
leaq (%r15,%r13), %r14
addq $0x10d, %r14            # imm = 0x10D
leaq 0xfe895(%rip), %rdi     # 0x10997d
xorl %r12d, %r12d
movq %r14, %rdx
xorl %eax, %eax
callq 0x9030
leaq -0x280(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9340
movq %rbx, %rdi
movl $0x5d, %esi
callq 0x9100
testq %rax, %rax
je 0xb15c
cmpb $0x5b, -0x280(%rbp)
jne 0xb15c
movq %rax, %r14
leaq 0x10048c(%rip), %rdi    # 0x10b5b8
callq 0x95d0
movb $0x0, (%r14)
leaq -0x27f(%rbp), %rsi
leaq -0x280(%rbp), %rdi
movq %r14, %rdx
subq %rdi, %rdx
callq 0x9650
incq %r14
movl $0x1, %r12d
movq %r14, %rbx
jmp 0xb176
movq %rbx, %rdi
movl $0x3b, %esi
callq 0x9100
testq %rax, %rax
je 0xb176
movl $0x1, %r12d
jmp 0xb183
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x9100
movl %r12d, 0x334db6(%rip)   # 0x33ff40
testq %rax, %rax
je 0xb1a9
movb $0x0, (%rax)
cmpb $0x0, 0x1(%rax)
je 0xb1a9
incq %rax
movq %rax, %rdi
callq 0x95e0
movl %eax, 0x153f27(%rip)    # 0x15f0d0
cmpl $0x0, 0x153f20(%rip)    # 0x15f0d0
jns 0xb1bc
movl $0xafc8, 0x153f14(%rip) # imm = 0xAFC8
addq %r13, %r15
movl 0x153f0b(%rip), %edx    # 0x15f0d0
leaq 0x118ca8(%rip), %rsi    # 0x123e74
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movl 0x334d60(%rip), %ecx    # 0x33ff40
leaq 0xfe7ac(%rip), %rdi     # 0x109993
leaq -0x280(%rbp), %r14
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9030
xorl %eax, %eax
cmpl $0x0, 0x334d3c(%rip)    # 0x33ff40
setne %al
leal 0x2(,%rax,8), %eax
movl %eax, 0x334cf8(%rip)    # 0x33ff0c
movl $0x1, 0x334cf2(%rip)    # 0x33ff10
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x334cdd(%rip), %rdx    # 0x33ff08
leaq 0x334d06(%rip), %rcx    # 0x33ff38
callq 0x9490
movl %eax, %r13d
testl %eax, %eax
sete %al
testl %r12d, %r12d
setne %cl
orb %al, %cl
jne 0xb296
leaq 0x10037f(%rip), %rdi    # 0x10b5cf
callq 0x95d0
xorl %eax, %eax
cmpl $0x0, 0x334ce2(%rip)    # 0x33ff40
sete %al
movl %eax, 0x334cd9(%rip)    # 0x33ff40
leal 0x2(,%rax,8), %eax
movl %eax, 0x334c98(%rip)    # 0x33ff0c
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x334c87(%rip), %rdx    # 0x33ff08
leaq 0x334cb0(%rip), %rcx    # 0x33ff38
callq 0x9490
movl %eax, %r13d
movl $0x1, %r12d
testl %r13d, %r13d
jne 0xb373
movq 0x334c92(%rip), %rax    # 0x33ff38
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x90a0
movl %eax, 0x8(%r15)
testl %eax, %eax
jns 0xb2d6
testl %r12d, %r12d
je 0xb35f
movl $0x4e20, %edi           # imm = 0x4E20
leaq 0xfe719(%rip), %rsi     # 0x1099ea
callq 0xb425
cmpl $0x0, 0x334c63(%rip)    # 0x33ff40
leaq 0xfe73d(%rip), %rsi     # 0x109a21
je 0xb2ed
leaq 0xfe72f(%rip), %rsi     # 0x109a1c
leaq 0xfe715(%rip), %rdi     # 0x109a09
xorl %eax, %eax
callq 0x9030
leaq 0x100303(%rip), %rdi    # 0x10b605
callq 0x95d0
movl 0x8(%r15), %edi
movq 0x334c26(%rip), %rax    # 0x33ff38
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x92c0
testl %eax, %eax
jns 0xb3c7
testl %r12d, %r12d
jne 0xb3b6
xorl %eax, %eax
cmpl $0x0, 0x334c08(%rip)    # 0x33ff40
sete %al
movl %eax, 0x334bff(%rip)    # 0x33ff40
movl 0x8(%r15), %eax
movl %eax, %edi
callq 0x9760
movl $0xffffffff, 0x8(%r15)  # imm = 0xFFFFFFFF
movl $0x1, %r12d
jmp 0xb1fb
xorl %ecx, %ecx
cmpl $0x0, 0x334bd8(%rip)    # 0x33ff40
sete %cl
movl %ecx, 0x334bcf(%rip)    # 0x33ff40
jmp 0xb345
movl %r13d, %edi
callq 0x9500
leaq 0xfe62c(%rip), %rdi     # 0x1099ae
leaq -0x280(%rbp), %rsi
leaq -0x80(%rbp), %rdx
movl %r13d, %ecx
movq %rax, %r8
xorl %eax, %eax
callq 0x9030
movl $0x0, 0x2234(%r15)
movq $0x0, (%r15)
orb $0x4, 0x2251(%r15)
jmp 0xb413
leaq 0xfe669(%rip), %rsi     # 0x109a26
movl $0x4e22, %edi           # imm = 0x4E22
callq 0xb425
leaq 0x100259(%rip), %rdi    # 0x10b627
callq 0x95d0
movl 0x8(%r15), %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x91e0
movl 0x8(%r15), %edi
orl $0x800, %eax            # imm = 0x800
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x91e0
movl $0x3, 0x222c(%r15)
leaq 0x100224(%rip), %rdi    # 0x10b632
callq 0x95d0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq