// File: 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:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movl %edi, -0x4(%rbp)
cmpl $0x13, -0x4(%rbp)
jb 0xba09
jmp 0xc039
movl $0xafc8, 0x1b26ad(%rip) # imm = 0xAFC8
movl $0x0, 0x393703(%rip)    # 0x39f120
movl -0x4(%rbp), %esi
movl -0x4(%rbp), %eax
leaq 0x361a96(%rip), %rdx    # 0x36d4c0
imulq $0x2260, %rax, %rax     # imm = 0x2260
addq %rax, %rdx
addq $0x10d, %rdx            # imm = 0x10D
leaq 0x163c97(%rip), %rdi    # 0x16f6d9
movb $0x0, %al
callq 0x9030
leaq -0x210(%rbp), %rdi
movl -0x4(%rbp), %eax
leaq 0x361a66(%rip), %rsi    # 0x36d4c0
imulq $0x2260, %rax, %rax     # imm = 0x2260
addq %rax, %rsi
addq $0x10d, %rsi            # imm = 0x10D
callq 0x9350
leaq -0x210(%rbp), %rdi
leaq 0x1638ff(%rip), %rsi    # 0x16f37d
callq 0x9090
movq %rax, -0x270(%rbp)
cmpq $0x0, -0x270(%rbp)
je 0xbb18
movsbl -0x210(%rbp), %eax
cmpl $0x5b, %eax
jne 0xbb18
leaq 0x163c44(%rip), %rdi    # 0x16f6ef
movb $0x0, %al
callq 0x9030
movq -0x270(%rbp), %rax
movb $0x0, (%rax)
leaq -0x210(%rbp), %rdi
leaq -0x210(%rbp), %rsi
addq $0x1, %rsi
movq -0x270(%rbp), %rdx
leaq -0x210(%rbp), %rax
subq %rax, %rdx
callq 0x9630
movq -0x270(%rbp), %rdi
addq $0x1, %rdi
leaq 0x17d751(%rip), %rsi    # 0x189247
callq 0x9090
movq %rax, -0x268(%rbp)
movl $0x1, 0x393614(%rip)    # 0x39f120
movl $0x1, -0x27c(%rbp)
jmp 0xbb82
leaq -0x210(%rbp), %rdi
leaq 0x17810e(%rip), %rsi    # 0x183c34
callq 0x9090
movq %rax, -0x268(%rbp)
cmpq $0x0, -0x268(%rbp)
je 0xbb52
movl $0x1, 0x3935da(%rip)    # 0x39f120
movl $0x1, -0x27c(%rbp)
jmp 0xbb80
leaq -0x210(%rbp), %rdi
leaq 0x17d6e7(%rip), %rsi    # 0x189247
callq 0x9090
movq %rax, -0x268(%rbp)
movl $0x0, 0x3935aa(%rip)    # 0x39f120
movl $0x0, -0x27c(%rbp)
jmp 0xbb82
cmpq $0x0, -0x268(%rbp)
je 0xbbbb
movq -0x268(%rbp), %rax
movb $0x0, (%rax)
movq -0x268(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0xbbb9
movq -0x268(%rbp), %rdi
addq $0x1, %rdi
callq 0x95c0
movl %eax, 0x1b2507(%rip)    # 0x1be0c0
jmp 0xbbbb
cmpl $0x0, 0x1b24fe(%rip)    # 0x1be0c0
jge 0xbbce
movl $0xafc8, 0x1b24f2(%rip) # imm = 0xAFC8
leaq -0x260(%rbp), %rdi
movl 0x1b24e5(%rip), %edx    # 0x1be0c0
leaq 0x17b65f(%rip), %rsi    # 0x187241
movb $0x0, %al
callq 0x9060
leaq -0x210(%rbp), %rsi
leaq -0x260(%rbp), %rdx
movl 0x393523(%rip), %ecx    # 0x39f120
leaq 0x163b03(%rip), %rdi    # 0x16f707
movb $0x0, %al
callq 0x9030
cmpl $0x0, 0x39350e(%rip)    # 0x39f120
je 0xbc20
movl $0xa, 0x3934ce(%rip)    # 0x39f0ec
jmp 0xbc2a
movl $0x2, 0x3934c2(%rip)    # 0x39f0ec
movl $0x1, 0x3934bc(%rip)    # 0x39f0f0
leaq -0x210(%rbp), %rdi
leaq -0x260(%rbp), %rsi
leaq 0x39349f(%rip), %rdx    # 0x39f0e8
leaq 0x3934c8(%rip), %rcx    # 0x39f118
callq 0x9490
movl %eax, -0x278(%rbp)
cmpl $0x0, -0x278(%rbp)
je 0xbce2
cmpl $0x0, -0x27c(%rbp)
jne 0xbce2
leaq 0x163aae(%rip), %rdi    # 0x16f722
movb $0x0, %al
callq 0x9030
cmpl $0x0, 0x39349e(%rip)    # 0x39f120
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x39348e(%rip)    # 0x39f120
movl $0x1, -0x27c(%rbp)
cmpl $0x0, 0x39347d(%rip)    # 0x39f120
je 0xbcb1
movl $0xa, 0x39343d(%rip)    # 0x39f0ec
jmp 0xbcbb
movl $0x2, 0x393431(%rip)    # 0x39f0ec
leaq -0x210(%rbp), %rdi
leaq -0x260(%rbp), %rsi
leaq 0x393418(%rip), %rdx    # 0x39f0e8
leaq 0x393441(%rip), %rcx    # 0x39f118
callq 0x9490
movl %eax, -0x278(%rbp)
cmpl $0x0, -0x278(%rbp)
je 0xbdcb
leaq -0x210(%rbp), %rax
movq %rax, -0x290(%rbp)
leaq -0x260(%rbp), %rax
movq %rax, -0x288(%rbp)
movl -0x278(%rbp), %eax
movl %eax, -0x280(%rbp)
movl -0x278(%rbp), %edi
callq 0x9500
movq -0x290(%rbp), %rsi
movq -0x288(%rbp), %rdx
movl -0x280(%rbp), %ecx
movq %rax, %r8
leaq 0x163a19(%rip), %rdi    # 0x16f759
movb $0x0, %al
callq 0x9030
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x36176d(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x2234(%rax)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x36174d(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361730(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x0, (%rax)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361714(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movzbl 0x2251(%rax), %ecx
orl $0x4, %ecx
movb %cl, 0x2251(%rax)
jmp 0xc039
movq 0x393346(%rip), %rax    # 0x39f118
movl 0x4(%rax), %edi
movq 0x39333c(%rip), %rax    # 0x39f118
movl 0x8(%rax), %esi
movq 0x393332(%rip), %rax    # 0x39f118
movl 0xc(%rax), %edx
callq 0x90a0
movl %eax, %ecx
movl -0x4(%rbp), %eax
movl %eax, %edx
leaq 0x3616c4(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rdx, %rdx     # imm = 0x2260
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x3616ab(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jge 0xbea0
cmpl $0x0, -0x27c(%rbp)
jne 0xbe8f
cmpl $0x0, 0x3932eb(%rip)    # 0x39f120
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x3932db(%rip)    # 0x39f120
movl $0x1, -0x27c(%rbp)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361665(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
callq 0x9740
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361647(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
jmp 0xbc0b
movl $0x4e20, %edi           # imm = 0x4E20
leaq 0x1638fa(%rip), %rsi    # 0x16f795
callq 0xc050
cmpl $0x0, 0x393279(%rip)    # 0x39f120
je 0xbeb9
leaq 0x163917(%rip), %rax    # 0x16f7c7
movq %rax, -0x298(%rbp)
jmp 0xbec9
leaq 0x16390c(%rip), %rax    # 0x16f7cc
movq %rax, -0x298(%rbp)
jmp 0xbec9
movq -0x298(%rbp), %rsi
leaq 0x1638dd(%rip), %rdi    # 0x16f7b4
movb $0x0, %al
callq 0x9030
leaq 0x1638ec(%rip), %rdi    # 0x16f7d1
movb $0x0, %al
callq 0x9030
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x3615c8(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
movq 0x39320c(%rip), %rax    # 0x39f118
movq 0x18(%rax), %rsi
movq 0x393201(%rip), %rax    # 0x39f118
movl 0x10(%rax), %edx
callq 0x92d0
cmpl $0x0, %eax
jge 0xbf9f
cmpl $0x0, -0x27c(%rbp)
jne 0xbf8e
cmpl $0x0, 0x3931ec(%rip)    # 0x39f120
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x3931dc(%rip)    # 0x39f120
movl $0x1, -0x27c(%rbp)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361566(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
callq 0x9740
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361548(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
jmp 0xbc0b
movl $0x4e22, %edi           # imm = 0x4E22
leaq 0x16385a(%rip), %rsi    # 0x16f7f4
callq 0xc050
leaq 0x16386d(%rip), %rdi    # 0x16f813
movb $0x0, %al
callq 0x9030
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x361507(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x91e0
movl %eax, -0x274(%rbp)
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x3614da(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl 0x8(%rax), %edi
movl -0x274(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x91e0
movl -0x4(%rbp), %eax
movl %eax, %ecx
leaq 0x3614a9(%rip), %rax    # 0x36d4c0
imulq $0x2260, %rcx, %rcx     # imm = 0x2260
addq %rcx, %rax
movl $0x3, 0x222c(%rax)
leaq 0x1637ed(%rip), %rdi    # 0x16f81f
movb $0x0, %al
callq 0x9030
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
nopw %cs:(%rax,%rax)