//
// sub_618F58 from 0x618f58 to 0x619472 (1306 bytes)
//
// 3 xrefs:
// > sub_61A2BC @ 0x61a358
// > sub_61ABE8 @ 0x61b36a
// > sub_61ABE8 @ 0x61ba3a
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
int __fastcall sub_618F58(int a1, int a2)
{
char *v2; // r4
int v4; // r6
const char *v5; // r6
size_t v6; // r0
size_t v7; // r0
int result; // r0
int v9; // r3
char *v10; // r11
const char *v11; // r1
size_t v12; // r2
char *v13; // r0
const char *v14; // r6
unsigned int v15; // r9
unsigned int v16; // r0
char *v17; // r0
unsigned int v18; // r0
unsigned int v19; // r0
char *v20; // r11
int v21; // r0
int v22; // r0
char *v23; // r1
char *v24; // r2
socklen_t v25; // r3
char v26; // r6
int v27; // r8
char *v28; // r0
int v29; // r4
socklen_t v30; // r2
unsigned int v31; // r11
bool v32; // zf
int *v33; // r0
unsigned __int16 v34; // r2
int v35; // r1
int v36; // r3
int v37; // r0
int v38; // r0
int v39; // r0
_DWORD *v40; // r0
int v41; // r0
_DWORD *v42; // r0
const char *v43; // r0
int v44; // r3
int v45; // r11
int v46; // r0
int v47; // r0
char *v48; // r3
const char *v49; // r1
size_t v50; // r2
_DWORD *v51; // r0
int v52; // r0
const char *v53; // r0
int v54; // r6
_DWORD *v55; // r0
int v56; // r0
unsigned int v57; // r0
int v58; // r3
int v59; // r3
int v60; // r1
int v61; // r1
int v62; // r1
unsigned __int8 *v63; // r4
char *v64; // r6
int v65; // r3
int v66; // t1
char *v67; // r0
int v68; // r4
const char *v69; // r0
int v70; // r0
int v71; // r3
int v72; // r4
const char *v73; // r0
size_t v74; // [sp+Ch] [bp-14h]
unsigned int v75; // [sp+10h] [bp-10h]
int fd; // [sp+20h] [bp+0h] BYREF
socklen_t v78; // [sp+24h] [bp+4h] BYREF
char *v79; // [sp+28h] [bp+8h] BYREF
struct sockaddr buf[8]; // [sp+2Ch] [bp+Ch] BYREF
_DWORD v81[64]; // [sp+ACh] [bp+8Ch] BYREF
char v82[1024]; // [sp+1ACh] [bp+18Ch] BYREF
char v83[1032]; // [sp+5ACh] [bp+58Ch] BYREF
v2 = nullptr;
v4 = *(_DWORD *)a1;
v74 = *(_DWORD *)a1;
memset(v81, 0, sizeof(v81));
fd = -1;
v5 = *(const char **)(v4 + 1008);
v79 = nullptr;
if ( !v5 )
{
v2 = nullptr;
v15 = 0;
v75 = 0;
goto LABEL_19;
}
v6 = strlen(v5);
if ( v6 <= 1 )
{
v15 = 0;
v75 = 0;
goto LABEL_19;
}
if ( v6 <= 0x2D )
v7 = 47;
else
v7 = v6 + 1;
v2 = (char *)off_ABAEBC(v7, 1u);
result = 27;
if ( !v2 )
return result;
v9 = *(unsigned __int8 *)v5;
if ( v9 == 91 )
{
v48 = strchr(v5, 93);
if ( !v48 )
goto LABEL_16;
v49 = v5 + 1;
v50 = v48 - (v5 + 1);
v5 = v48;
strncpy(v2, v49, v50);
}
else if ( v9 != 58 )
{
v10 = strchr(v5, 58);
if ( !v10 || inet_pton(10, v5, buf) == 1 )
{
strcpy(v2, v5);
goto LABEL_16;
}
v11 = v5;
v12 = v10 - v5;
v5 = v10;
strncpy(v2, v11, v12);
}
v13 = strchr(v5, 58);
v14 = v13;
v15 = (unsigned int)v13;
if ( !v13 )
goto LABEL_17;
v16 = strtoul(v13 + 1, nullptr, 10);
v15 = sub_63B7CC(v16);
v17 = strchr(v14, 45);
if ( !v17 )
goto LABEL_17;
v18 = strtoul(v17 + 1, nullptr, 10);
v19 = sub_63B7CC(v18);
if ( v15 <= v19 )
{
v75 = v15;
v15 = v19;
goto LABEL_18;
}
LABEL_16:
v15 = 0;
LABEL_17:
v75 = v15;
LABEL_18:
if ( *v2 )
{
v44 = *(_DWORD *)(a1 + 76);
v45 = *(_DWORD *)(v44 + 4);
v46 = sub_6238E4(*(_DWORD *)(v44 + 24));
v47 = sub_6239F4(v45, v46, *(_DWORD *)(a1 + 136), v2, v83, 1025);
if ( v47 == 1 )
return 30;
if ( v47 )
{
if ( v47 != 2 )
goto LABEL_19;
v20 = v83;
}
else
{
v20 = v2;
}
v26 = 1;
goto LABEL_27;
}
LABEL_19:
v20 = (char *)buf;
v21 = *(_DWORD *)(a1 + 416);
v78 = 128;
v22 = getsockname(v21, buf, &v78);
v26 = v22;
if ( v22 )
{
v51 = (_DWORD *)_errno();
v52 = sub_6325A4(a1, *v51);
sub_6180F8(v74, "getsockname() failed: %s", v52);
goto LABEL_51;
}
if ( buf[0].sa_family == 10 )
{
v24 = v83;
v23 = &buf[0].sa_data[6];
v25 = 1025;
}
else
{
v20 = v83;
}
if ( buf[0].sa_family == 10 )
{
v20 = v24;
}
else
{
v23 = &buf[0].sa_data[2];
v25 = 1025;
v24 = v20;
}
inet_ntop(buf[0].sa_family, v23, v24, v25);
LABEL_27:
sub_610224((int *)a1, v20, 0, (const char **)&v79);
if ( !v79 || (v27 = *(_DWORD *)v79, sub_610524(v74, v79), !v27) )
{
sub_6180F8(v74, "failed to resolve the address provided to PORT: %s", v20);
LABEL_51:
off_ABAEB0(v2);
return 30;
}
v28 = v2;
v29 = v27;
off_ABAEB0(v28);
fd = -1;
while ( sub_62E01C(a1, v29, 0, &fd) )
{
v42 = (_DWORD *)_errno();
v29 = *(_DWORD *)(v29 + 28);
if ( !v29 )
{
v43 = (const char *)sub_6325A4(a1, *v42);
sub_6180F8(v74, "socket failure: %s", v43);
return 30;
}
}
memcpy(buf, *(const void **)(v29 + 24), *(_DWORD *)(v29 + 16));
v30 = *(_DWORD *)(v29 + 16);
v78 = v30;
if ( v75 > v15 )
{
LABEL_65:
sub_6180F8(v74, "bind() failed, we ran out of ports!");
sub_62DFD4(a1, fd);
return 30;
}
v31 = v75;
while ( 1 )
{
*(_WORD *)buf[0].sa_data = __rev16(v31);
if ( !bind(fd, buf, v30) )
break;
v33 = (int *)_errno();
v34 = v31 + 1;
v35 = *v33;
if ( *v33 == 99 )
v36 = v26 & 1;
else
v36 = 0;
if ( v36 )
{
v37 = sub_6325A4(a1, 99);
sub_61807C(v74, "bind(port=%hu) on non-local address failed: %s\n", v31, v37);
v38 = *(_DWORD *)(a1 + 416);
v78 = 128;
v39 = getsockname(v38, buf, &v78);
v31 = v75;
v26 = v39;
if ( v39 )
goto LABEL_45;
}
else
{
v32 = v35 == 13;
if ( v35 != 13 )
v32 = v35 == 98;
if ( !v32 )
{
v53 = (const char *)sub_6325A4(a1, v35);
sub_6180F8(v74, "bind(port=%hu) failed: %s", v31, v53);
sub_62DFD4(a1, fd);
return 30;
}
v31 = v34;
if ( v34 > v15 )
goto LABEL_65;
}
v30 = v78;
}
v78 = 128;
if ( getsockname(fd, buf, &v78) )
{
LABEL_45:
v40 = (_DWORD *)_errno();
v41 = sub_6325A4(a1, *v40);
sub_6180F8(v74, "getsockname() failed: %s", v41);
LABEL_46:
sub_62DFD4(a1, fd);
return 30;
}
v54 = listen(fd, 1);
if ( v54 )
{
v55 = (_DWORD *)_errno();
v56 = sub_6325A4(a1, *v55);
sub_6180F8(v74, "socket failure: %s", v56);
goto LABEL_46;
}
sub_60FFC0(v29, (char *)v81, 256);
v57 = *(unsigned __int8 *)(a1 + 633);
if ( !*(_BYTE *)(a1 + 633) )
{
v57 = *(unsigned __int8 *)(a1 + 623);
if ( *(_BYTE *)(a1 + 623) )
*(_BYTE *)(a1 + 633) = 1;
}
v58 = a2;
if ( a2 == 2 )
{
v54 = 2;
goto LABEL_83;
}
if ( buf[0].sa_family != 2 )
{
v59 = a2;
while ( 1 )
{
if ( (v59 == 0) <= v57 )
{
if ( v59 == 1 )
{
v54 = 2;
goto LABEL_83;
}
if ( buf[0].sa_family == 10 )
{
v60 = (unsigned __int16)__rev16(*(unsigned __int16 *)buf[0].sa_data);
if ( !v59 )
{
v70 = a1 + 1088;
v71 = 2;
goto LABEL_99;
}
}
}
if ( ++v59 == 2 )
goto LABEL_82;
}
}
while ( 1 )
{
if ( (v58 == 0) > v57 )
goto LABEL_89;
v60 = (unsigned __int16)__rev16(*(unsigned __int16 *)buf[0].sa_data);
if ( !v58 )
break;
if ( v58 == 1 )
{
v63 = (unsigned __int8 *)v81;
v64 = v82;
do
{
v66 = *v63++;
v65 = v66;
if ( !v66 )
break;
if ( v65 == 46 )
LOBYTE(v65) = 44;
*v64++ = v65;
}
while ( v63 );
v67 = v64;
*v64 = 0;
v54 = 1;
sub_624640(v67, 20, ",%d,%d", BYTE1(v60), (unsigned __int8)v60);
v68 = sub_63A7E4(a1 + 1088, "%s %s", "PORT", v82);
if ( v68 )
{
v69 = (const char *)sub_6320C0();
sub_6180F8(v74, "Failure sending PORT command: %s", v69);
sub_62DFD4(a1, fd);
*(_DWORD *)(a1 + 1192) = 0;
return v68;
}
goto LABEL_83;
}
LABEL_89:
if ( ++v58 == 2 )
{
LABEL_82:
v54 = 2;
goto LABEL_83;
}
}
v70 = a1 + 1088;
v71 = 1;
LABEL_99:
v72 = sub_63A7E4(v70, "%s |%d|%s|%hu|", "EPRT", v71, (const char *)v81, v60);
if ( v72 )
{
v73 = (const char *)sub_6320C0();
sub_6180F8(v74, "Failure sending EPRT command: %s", v73);
sub_62DFD4(a1, fd);
*(_DWORD *)(a1 + 1180) = 1;
*(_DWORD *)(a1 + 1192) = 0;
return v72;
}
LABEL_83:
v61 = *(_DWORD *)(a1 + 420);
*(_DWORD *)(a1 + 1180) = v54;
if ( v61 != -1 )
sub_62DFD4(a1, v61);
v62 = fd;
*(_DWORD *)(a1 + 1256) = 0;
*(_DWORD *)(a1 + 420) = v62;
*(_BYTE *)(a1 + 626) = 1;
*(_DWORD *)(a1 + 1192) = 28;
return 0;
}