Skip to content

Instantly share code, notes, and snippets.

@shuffle2
Last active June 4, 2017 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuffle2/4541e4e96ba3e04a34bd36b0e07d3013 to your computer and use it in GitHub Desktop.
Save shuffle2/4541e4e96ba3e04a34bd36b0e07d3013 to your computer and use it in GitHub Desktop.
IOS56 SO socket
int __fastcall so_set_fd_entry(sock_fd_node *node, int fd)
{
int proto; // r5@3
unsigned __int8 *v5; // r7@3
int v6; // r8@3
__int16 v7; // r2@3
int v9; // r4@15
int v10; // r3@15
int v11; // [sp+14h] [bp-4h]@0
if ( !node )
sub_13B5ACC0("IPSocket.c", 904, "assert node");
proto = 0;
v5 = 0;
v6 = irq_disable();
v7 = node->ref;
if ( node->ref <= 0 )
{
sub_13B5ACC0("IPSocket.c", 908, "assert 0 < node->ref");
v7 = node->ref;
}
node->ref = v7 - 1;
if ( v7 == 1 && node->info )
{
v5 = (unsigned __int8 *)node->info;
node->info = (void *)(signed __int16)(v7 - 1);
proto = (unsigned __int8)node->proto;
node->proto = v7 - 1;
if ( node->ref )
goto LABEL_8;
}
else if ( node->ref )
{
goto LABEL_8;
}
mtx_reset(&node->mtx0);
mtx_reset(&node->mtx1);
LABEL_8:
node->fd = fd;
*((_DWORD *)sub_13B5ADE8() + 2) = fd;
irq_restore(v6);
if ( v5 )
{
if ( proto == 6 )
{
sub_13B4EF78((int)v5);
}
else if ( proto == 17 )
{
v9 = sub_13B467E8(v5);
so_free(5, *(void **)(v9 + 188), *(_DWORD *)(v9 + 192));
so_free(4, *(void **)(v9 + 204), *(_DWORD *)(v9 + 208));
v10 = v5[8];
if ( v10 == 2 )
{
so_free(3, v5, 796);
}
else if ( v10 == 23 )
{
so_free(3, v5, 324);
}
}
else
{
sub_13B5ACC0("IPSocket.c", 943, "PutNode: unknown proto");
}
}
return v11;
}
signed int __fastcall so_socket(int pf, int type, int protocol)
{
_DWORD *v3; // r11@1
_DWORD *v4; // r9@1
signed int v7; // r0@8
sock_fd_node *v8; // r3@8
int v9; // r7@8
int fd; // r5@8
sock_fd_node *node; // r4@10
void *v12; // r7@23
int v13; // r0@23
void *v14; // r8@23
int v15; // r0@32
int v16; // r0@49
int v18; // r1@20
signed int v19; // r1@29
void *v20; // r1@58
signed int v21; // r2@58
int v22; // [sp+0h] [bp-28h]@26
v3 = 0;
v4 = 0;
if ( dword_13B6CDF8 != 1 )
return -39;
if ( pf != 2 && pf != 23 )
return -5;
if ( protocol )
return -68;
so_fd_get_obj(0xFFFFFFFF, 0);
v7 = irq_disable();
v8 = sock_fd_table;
v9 = v7;
fd = 0;
while ( 1 )
{
node = v8;
if ( !v8->ref )
break;
++fd;
++v8;
if ( fd > 23 )
goto LABEL_14;
}
if ( v8->info )
sub_13B5ACC0("IPSocket.c", 1937, "assert node->info == NULL");
node->ref = 2;
LABEL_14:
irq_restore(v9);
if ( fd == 24 )
return -33;
if ( type != 1 )
{
if ( type != 2 )
{
so_set_fd_entry(node, -69);
so_set_fd_entry(node, -69);
return -69;
}
if ( pf == 2 )
{
v18 = 796;
}
else
{
if ( pf != 23 )
goto LABEL_23;
v18 = 324;
}
v4 = (_DWORD *)so_alloc(3, v18);
LABEL_23:
v12 = (void *)so_alloc(4, 0x1000);
v13 = so_alloc(5, 0x2000);
v14 = (void *)v13;
if ( !v4 || !v12 || !v13 || (v22 = sub_13B46A90(pf, v4), v22 < 0) )
{
LABEL_46:
if ( pf == 2 )
{
so_free(3, v4, 796);
}
else if ( pf == 23 )
{
so_free(3, v4, 324);
}
so_free(4, v12, 0x1000);
v16 = 5;
goto LABEL_50;
}
sub_13B469B8(v4, v14, 0x2000);
sub_13B46A44(v4, v12, 0x1000);
goto LABEL_37;
}
if ( pf == 2 )
{
v19 = 226;
}
else
{
if ( pf != 23 )
goto LABEL_32;
v19 = 236;
}
v3 = (_DWORD *)so_alloc(0, 4 * v19);
LABEL_32:
v12 = (void *)so_alloc(1, 0x1000);
v15 = so_alloc(2, 0x2000);
v14 = (void *)v15;
if ( !v3 )
goto LABEL_55;
if ( !v12 )
goto LABEL_55;
if ( !v15 )
goto LABEL_55;
v22 = sub_13B4CF84(pf, v3);
if ( v22 < 0 )
goto LABEL_55;
sub_13B4CDF4((int)v3, (int)v12, 0x1000);
sub_13B4CE64((int)v3, (int)v14, 0x2000);
sub_13B4EAF0((int)v3, dword_13B6D254, dword_13B6D258);
sub_13B4EB2C((int)v3, dword_13B6D25C, dword_13B6D260);
LABEL_37:
if ( v22 < 0 )
{
if ( type != 1 )
{
if ( type != 2 )
{
LABEL_51:
so_set_fd_entry(node, -49);
so_set_fd_entry(node, -49);
return -49;
}
goto LABEL_46;
}
LABEL_55:
if ( pf == 2 )
{
v20 = v3;
v21 = 226;
}
else
{
if ( pf != 23 )
{
LABEL_57:
so_free(1, v12, 0x1000);
v16 = 2;
LABEL_50:
so_free(v16, v14, 0x2000);
goto LABEL_51;
}
v20 = v3;
v21 = 236;
}
so_free(0, v20, 4 * v21);
goto LABEL_57;
}
node->field_1 = 0;
mtx_init(&node->mtx0);
mtx_init(&node->mtx1);
if ( type == 1 )
{
*(_DWORD *)(sub_13B4C804((int)v3) + 832) = node;
node->proto = 6;
node->info = v3;
}
else if ( type == 2 )
{
node->proto = 17;
node->info = v4;
}
so_set_fd_entry(node, fd);
return fd;
}
// ppc code
int __fastcall _SOCreateSocket(int pf, int type, int protocol)
{
int v4; // r29@1
int *v5; // r3@3
int *v6; // r30@3
int result; // r3@6
int v8; // r0@6
int back_chain; // [sp+0h] [-30h]@1
long v12; // [sp+10h] [-20h]@0
v4 = SOiPrepare((long *)_FUNCTION___58);
if ( !v4 )
{
if ( pf != 23 )
{
v5 = (int *)SOiAlloc(0xCu, 32);
v6 = v5;
if ( v5 )
{
*v5 = pf;
v5[1] = type;
v5[2] = protocol;
IOS_Ioctl(v12, 15, v5, 0xCu, 0, 0);
SOiFree(0xCu, v6, 32);
}
}
v4 = SOiConclude((int)_FUNCTION___58);
}
result = v4;
v8 = *(_DWORD *)(back_chain + 4);
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment