// File: /workspace/github_cpp_makefile_star2_O3/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 0xb423
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, 0x154023(%rip) # imm = 0xAFC8
movl $0x0, 0x334e89(%rip)    # 0x33ff40
movl %edi, %eax
imulq $0x2260, %rax, %r13     # imm = 0x2260
leaq 0x303219(%rip), %r15    # 0x30e2e0
leaq (%r15,%r13), %r14
addq $0x10d, %r14            # imm = 0x10D
leaq 0xfe8b4(%rip), %rdi     # 0x10998d
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 0xb14d
cmpb $0x5b, -0x280(%rbp)
jne 0xb14d
movq %rax, %r14
leaq 0x1004a8(%rip), %rdi    # 0x10b5c5
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 0xb171
movq %rbx, %rdi
movl $0x3b, %esi
callq 0x9100
testq %rax, %rax
je 0xb171
movl $0x1, 0x334dd7(%rip)    # 0x33ff40
movl $0x1, %r12d
jmp 0xb18a
movq %rbx, %rdi
movl $0x3a, %esi
callq 0x9100
movl %r12d, 0x334dbb(%rip)   # 0x33ff40
testq %rax, %rax
je 0xb1a8
movb $0x0, (%rax)
cmpb $0x0, 0x1(%rax)
je 0xb1a8
incq %rax
movq %rax, %rdi
callq 0x95e0
movl %eax, %edx
movl %eax, 0x153f2a(%rip)    # 0x15f0d0
jmp 0xb1ae
movl 0x153f22(%rip), %edx    # 0x15f0d0
testl %edx, %edx
jns 0xb1c1
movl $0xafc8, 0x153f14(%rip) # imm = 0xAFC8
movl $0xafc8, %edx           # imm = 0xAFC8
addq %r13, %r15
leaq 0x118d89(%rip), %rsi    # 0x123f54
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x9060
movl 0x334d61(%rip), %ecx    # 0x33ff40
leaq 0xfe7bd(%rip), %rdi     # 0x1099a3
leaq -0x280(%rbp), %r14
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9030
xorl %eax, %eax
cmpl $0x0, 0x334d3d(%rip)    # 0x33ff40
setne %al
leal 0x2(,%rax,8), %eax
movl %eax, 0x334cf9(%rip)    # 0x33ff0c
movl $0x1, 0x334cf3(%rip)    # 0x33ff10
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x334cde(%rip), %rdx    # 0x33ff08
leaq 0x334d07(%rip), %rcx    # 0x33ff38
callq 0x9490
movl %eax, %r13d
testl %eax, %eax
sete %al
testl %r12d, %r12d
setne %cl
orb %al, %cl
jne 0xb295
leaq 0x10038d(%rip), %rdi    # 0x10b5dc
callq 0x95d0
xorl %eax, %eax
cmpl $0x0, 0x334ce3(%rip)    # 0x33ff40
sete %al
movl %eax, 0x334cda(%rip)    # 0x33ff40
leal 0x2(,%rax,8), %eax
movl %eax, 0x334c99(%rip)    # 0x33ff0c
movq %r14, %rdi
movq %rbx, %rsi
leaq 0x334c88(%rip), %rdx    # 0x33ff08
leaq 0x334cb1(%rip), %rcx    # 0x33ff38
callq 0x9490
movl %eax, %r13d
movl $0x1, %r12d
testl %r13d, %r13d
jne 0xb372
movq 0x334c93(%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 0xb2d5
testl %r12d, %r12d
je 0xb35e
movl $0x4e20, %edi           # imm = 0x4E20
leaq 0xfe72a(%rip), %rsi     # 0x1099fa
callq 0xb424
cmpl $0x0, 0x334c64(%rip)    # 0x33ff40
leaq 0xfe74e(%rip), %rsi     # 0x109a31
je 0xb2ec
leaq 0xfe740(%rip), %rsi     # 0x109a2c
leaq 0xfe726(%rip), %rdi     # 0x109a19
xorl %eax, %eax
callq 0x9030
leaq 0x100311(%rip), %rdi    # 0x10b612
callq 0x95d0
movl 0x8(%r15), %edi
movq 0x334c27(%rip), %rax    # 0x33ff38
movq 0x18(%rax), %rsi
movl 0x10(%rax), %edx
callq 0x92c0
testl %eax, %eax
jns 0xb3c6
testl %r12d, %r12d
jne 0xb3b5
xorl %eax, %eax
cmpl $0x0, 0x334c09(%rip)    # 0x33ff40
sete %al
movl %eax, 0x334c00(%rip)    # 0x33ff40
movl 0x8(%r15), %eax
movl %eax, %edi
callq 0x9760
movl $0xffffffff, 0x8(%r15)  # imm = 0xFFFFFFFF
movl $0x1, %r12d
jmp 0xb1fa
xorl %ecx, %ecx
cmpl $0x0, 0x334bd9(%rip)    # 0x33ff40
sete %cl
movl %ecx, 0x334bd0(%rip)    # 0x33ff40
jmp 0xb344
movl %r13d, %edi
callq 0x9500
leaq 0xfe63d(%rip), %rdi     # 0x1099be
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 0xb412
leaq 0xfe67a(%rip), %rsi     # 0x109a36
movl $0x4e22, %edi           # imm = 0x4E22
callq 0xb424
leaq 0x100267(%rip), %rdi    # 0x10b634
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 0x100232(%rip), %rdi    # 0x10b63f
callq 0x95d0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq