diff --git a/func_replacements.js b/func_replacements.js index aa3784b..b0cc7a9 100644 --- a/func_replacements.js +++ b/func_replacements.js @@ -1,4 +1,4 @@ -const u16_swap = (x) => ((x & 0xff00) >> 8) | ((x & 0x00ff) << 8); +export const u16_swap = (x) => ((x & 0xff00) >> 8) | ((x & 0x00ff) << 8); const swap_endianness_u16 = (ptr) => { const bytes = ptr.readU16(); @@ -42,7 +42,7 @@ const compare_buf = (a, b, len) => { } }; -const Commands = { +export const Commands = { Close: 0xf1f0, LanSearchExt: 0xf132, LanSearch: 0xf130, @@ -54,16 +54,47 @@ const Commands = { LstReq: 0xf167, DrwAck: 0xf1d1, Drw: 0xf1d0, + + // From CSession_CtrlPkt_Proc, incomplete + PunchTo: 0xf140, + PunchPkt: 0xf141, + HelloAck: 0xf101, + RlyTo: 0xf102, + DevLgnAck: 0xf111, + P2PReqAck: 0xf121, + ListenReqAck: 0xf169, + RlyHelloAck: 0xf170, // always + RlyHelloAck2: 0xf171, // if len >1?? }; -const CommandsByValue = Object.keys(Commands).reduce((acc, cur) => { +export const CommandsByValue = Object.keys(Commands).reduce((acc, cur) => { acc[Commands[cur]] = cur; return acc; }, {}); -const pack_P2pHdr = (in_buf, out_buf) => { +export const reply_Drw = (inbuf, outbuf) => { + /* + -> cmd = inbuf+10 + -> chan = inbuf+9 + -> param2 = lock ? 0xd1 : 0xd2 (never lock branch) => 0xd2 + -> Send_Pkt_DrwAck(10,0xd2,channel,1,&cmd_,sock_fd,ipaddr_); + -> create_DrwAck(bare_drwack, 0xd2, chan, halflen??, inbuf) + -> pack_P2pHdr(bare_drwack, pkt_hdr) + -> pack_DrwAck(bare_drwack+4, len = swapped(bare_drwack[1]), pkt_hdr + hdr_len) + -> send_udp(pkt_hdr, max(hdr_len, 0x20)??) + */ + const chan = inbuf.add(9).readU16(); + const cmd = inbuf.add(10).readU16(); + const param2 = 0xd2; // ? + const half_len = 1; //FIXME + const bare_ack = Memory.alloc(half_len * 2 + 8); + create_DrwAck(bare_ack, param2, chan, half_len, inbuf); + const hdr_len = pack_P2pHdr(bare_ack, outbuf); + pack_DrwAck(bare_ack + 4, half_len * 2 + 4, outbuf.add(hdr_len)); +}; +const pack_P2pHdr = (inbuf, out_buf) => { // shitty memcpy - out_buf.writeByteArray(in_buf.readByteArray(4)); + out_buf.writeByteArray(inbuf.readByteArray(4)); return 4; }; @@ -158,31 +189,21 @@ const create_LstReq = (outbuf, inbuf) => { return LISTREQ_SIZE + 4; // this is actually wrong (and unused) in the code -- it is 0x1c }; -const dbg_create_DrwAck = (og_func) => { - const create_DrwAck = (outbuf, idk_param2, channel, half_len, inbuf) => { - // console.log(`2: ${idk_param2} 3: ${channel}, halflen: ${half_len}`); - // param2 is 0xd1 if "unlocked" and 0xd2 if "locked"? - const copyLen = half_len * 2; - writeCommand2(Commands.DrwAck, outbuf); - outbuf.add(2).writeU16(u16_swap(copyLen + 4)); - outbuf.add(8).writeU8(idk_param2); - outbuf.add(9).writeU8(channel); - outbuf.add(10).writeU16(u16_swap(half_len)); - outbuf.add(12).writeByteArray(inbuf.readByteArray(copyLen)); - return half_len * 2 + 8; // this is wrong in the code as well - should be +12 - }; - return create_DrwAck; +const create_DrwAck = (outbuf, idk_param2, channel, half_len, inbuf) => { + console.log(`DrwAck: 2: ${idk_param2} 3: ${channel}, halflen: ${half_len}`); + // param2 is 0xd1 if "unlocked" and 0xd2 if "locked"? + const copyLen = half_len * 2; + writeCommand2(Commands.DrwAck, outbuf); + outbuf.add(2).writeU16(u16_swap(copyLen + 4)); + outbuf.add(8).writeU8(idk_param2); + outbuf.add(9).writeU8(channel); + outbuf.add(10).writeU16(u16_swap(half_len)); + outbuf.add(12).writeByteArray(inbuf.readByteArray(copyLen)); + return half_len * 2 + 8; }; const dbg_create_Drw = (og_func) => { - const create_Drw = ( - outbuf, - idk_param2, - idk_param3, - idk_param4, - copy_len, - inbuf, - ) => { + const create_Drw = (outbuf, idk_param2, idk_param3, idk_param4, copy_len, inbuf) => { /// idk_param4 goes up by 0x100 per call // console.log( @@ -219,14 +240,10 @@ const dbg_create_Drw = (og_func) => { const dbg_pack_ClntPkt = (og_func) => { const pack_ClntPkt = (addr_fam, inbuf, outbuf) => { const cmd = u16_swap(inbuf.readU16()); - console.log( - `pack_ClntPkt: cmd 0x${cmd.toString(16)} = ${ - CommandsByValue[cmd] - }; fam ${addr_fam}; inbuf =>`, - ); - //console.log(inbuf.readByteArray(16)); + console.log(`pack_ClntPkt: cmd 0x${cmd.toString(16)} = ${CommandsByValue[cmd]}; fam ${addr_fam}; inbuf =>`); + // console.log(inbuf.readByteArray(16)); - //const ret = og_func(addr_fam, inbuf, outbuf); + // const ret = og_func(addr_fam, inbuf, outbuf); // const new_outbuf = Memory.alloc(ret); const hdrLen = pack_P2pHdr(inbuf, outbuf); @@ -235,10 +252,8 @@ const dbg_pack_ClntPkt = (og_func) => { [Commands.LanSearchExt]: () => hdrLen, [Commands.Close]: () => hdrLen, [Commands.Hello]: () => hdrLen, - [Commands.P2pReq]: () => - hdrLen + pack_P2pReq4(inbuf.add(8), outbuf.add(hdrLen)), - [Commands.LstReq]: () => - hdrLen + pack_P2pId(inbuf.add(8), outbuf.add(hdrLen)), + [Commands.P2pReq]: () => hdrLen + pack_P2pReq4(inbuf.add(8), outbuf.add(hdrLen)), + [Commands.LstReq]: () => hdrLen + pack_P2pId(inbuf.add(8), outbuf.add(hdrLen)), [Commands.DrwAck]: () => { const pkt_size = u16_swap(inbuf.add(2).readU16()); return hdrLen + pack_DrwAck(inbuf.add(8), pkt_size, outbuf.add(hdrLen)); @@ -249,20 +264,20 @@ const dbg_pack_ClntPkt = (og_func) => { }, }; /* - // No PackClnt? - P2PAlive: 0xf1e0, - P2PAliveAck: 0xf1e1, + // No PackClnt? + P2PAlive: 0xf1e0, + P2PAliveAck: 0xf1e1, - P2pRdy: 0xf142, // idk?? - */ + P2pRdy: 0xf142, // idk?? + */ const fn = packFn[cmd]; if (fn == undefined) { console.error(`IDK how to handle ${cmd}`); } else { const my_ret = fn(); - //compare_buf(outbuf, new_outbuf, ret); - //console.log(new_outbuf.readByteArray(ret)); + // compare_buf(outbuf, new_outbuf, ret); + // console.log(new_outbuf.readByteArray(ret)); return my_ret; } return -1; @@ -277,9 +292,7 @@ const pack_Drw = (inbuf, m_pkt_size, outbuf) => { outbuf.writeU8(inbuf.readU8()); outbuf.add(1).writeU8(inbuf.add(1).readU8()); outbuf.add(2).writeU16(inbuf.add(2).readU16()); - outbuf - .add(4) - .writeByteArray(inbuf.add(4).readByteArray(m_pkt_size - DRW_HDR_SIZE)); + outbuf.add(4).writeByteArray(inbuf.add(4).readByteArray(m_pkt_size - DRW_HDR_SIZE)); return m_pkt_size; }; @@ -291,9 +304,7 @@ const pack_DrwAck = (inbuf, m_pkt_size, outbuf) => { outbuf.writeU8(inbuf.readU8()); outbuf.add(1).writeU8(inbuf.add(1).readU8()); outbuf.add(2).writeU16(inbuf.add(2).readU16()); - outbuf - .add(4) - .writeByteArray(inbuf.add(4).readByteArray(m_pkt_size - DRW_ACK_HDR_SIZE)); + outbuf.add(4).writeByteArray(inbuf.add(4).readByteArray(m_pkt_size - DRW_ACK_HDR_SIZE)); return m_pkt_size; }; @@ -329,14 +340,9 @@ export const replace_func = (stub, ret, args) => { replacement_func = stub; } - console.log( - `Replacing ${name_in_elf}, signature "${ret} ${name_in_elf}(${args})"`, - ); + console.log(`Replacing ${name_in_elf}, signature "${ret} ${name_in_elf}(${args})"`); - Interceptor.replace( - symbol_addr, - new NativeCallback(replacement_func, ret, args), - ); + Interceptor.replace(symbol_addr, new NativeCallback(replacement_func, ret, args)); }; /* Send_Pkt_LanSearch = @@ -349,6 +355,9 @@ export const replace_func = (stub, ret, args) => { * pack_ClntPkt * UdpPktSend */ + +// CSession_DataPkt_Proc(struct, *cmd) == Drw_Deal +// CSession_CtrlPkt_Proc(struct, *cmd) == control? export const replaceFunctions = () => { const replacements = [ [create_P2pAlive, "uint8", ["pointer"]], @@ -357,16 +366,8 @@ export const replaceFunctions = () => { [create_LanSearchExt, "uint8", ["pointer"]], [create_Hello, "uint8", ["pointer"]], [create_Close, "uint8", ["pointer"]], - [ - dbg_create_Drw, - "uint", - ["pointer", "uint64", "uint8", "uint16", "uint32", "pointer"], - ], - [ - dbg_create_DrwAck, - "uint", - ["pointer", "uint8", "uint8", "uint16", "pointer"], - ], + [dbg_create_Drw, "uint", ["pointer", "uint64", "uint8", "uint16", "uint32", "pointer"]], + [create_DrwAck, "uint", ["pointer", "uint8", "uint8", "uint16", "pointer"]], [create_P2pReq, "uint8", ["pointer", "pointer", "pointer", "uint"]], [create_LstReq, "uint8", ["pointer", "pointer"]], [create_P2pRdy, "uint8", ["pointer", "pointer"]],