int main(int argc, char **argv) { mesh_t meshA = mesh_new(3); fail_unless(meshA); lob_t secretsA = mesh_generate(meshA); fail_unless(secretsA); mesh_t meshB = mesh_new(3); fail_unless(meshB); lob_t secretsB = mesh_generate(meshB); fail_unless(secretsB); net_loopback_t pair = net_loopback_new(meshA,meshB); fail_unless(pair); link_t linkAB = link_get(meshA, meshB->id->hashname); link_t linkBA = link_get(meshB, meshA->id->hashname); fail_unless(linkAB); fail_unless(linkBA); fail_unless(link_sync(linkAB)); fail_unless(link_ready(linkAB)); fail_unless(link_ready(linkBA)); return 0; }
void recv_data(int len) { static i32 i; static u32 buffer[4]; const __builtin_quad *b = (__builtin_quad *)buffer; i32 tout; while (len >= 4) { tout = 0x1000; buffer[i] = (u32)(RECEIVE_NETWORK_B() & 0xFF) << 24 | (u32)(RECEIVE_NETWORK_B() & 0xFF) << 16 | (u32)(RECEIVE_NETWORK_B() & 0xFF) << 8 | (u32)(RECEIVE_NETWORK_B() & 0xFF); i = (i + 1) % 4; if (i == 0) { while (!link_ready() && --tout) { asm("nop;nop;nop;nop;;"); } if (tout) { link_send(b); } } if (tout) { len -= 4; } else { len = 0; } } }
// process an incoming handshake link_t link_handshake(link_t link, lob_t inner, lob_t outer, pipe_t pipe) { link_t ready; uint32_t out; if(!link || !inner || !outer) return LOG("bad args"); if(!link->key && link_key(link->mesh,inner) != link) return LOG("invalid/mismatch handshake key"); out = exchange3_out(link->x,0); ready = link_ready(link); // if bad at, always send current handshake if(exchange3_in(link->x, lob_get_int(inner,"at")) < out) { LOG("old/bad at: %s (%d,%d,%d)",lob_json(inner),lob_get_int(inner,"at"),exchange3_in(link->x,0),exchange3_out(link->x,0)); if(pipe) pipe->send(pipe,exchange3_handshake(link->x),link); return NULL; } // trust/add this pipe if(pipe) link_pipe(link,pipe); // try to sync ephemeral key if(!exchange3_sync(link->x,outer)) return LOG("sync failed"); // we may need to re-sync if(out != exchange3_out(link->x,0)) link_sync(link); // notify of ready state change if(!ready && link_ready(link)) { LOG("link ready"); mesh_link(link->mesh, link); } return link; }