bool PyAddress::Decode(PyRep *&in_object) { PyRep *base = in_object; in_object = NULL; if(!base->IsObject()) { codelog(NET__PACKET_ERROR, "Invalid element type, expected object"); PyDecRef(base); return false; } PyObject *obj = (PyObject *) base; //do we care about the object type? should be "macho.MachoAddress" if(!obj->arguments()->IsTuple()) { codelog(NET__PACKET_ERROR, "Invalid argument type, expected tuple"); PyDecRef(base); return false; } PyTuple *args = (PyTuple *) obj->arguments(); if(args->items.size() < 3) { codelog(NET__PACKET_ERROR, "Not enough elements in address tuple: %lu", args->items.size()); args->Dump(NET__PACKET_ERROR, " "); PyDecRef(base); return false; } //decode the address type. if(!args->items[0]->IsInt()) { codelog(NET__PACKET_ERROR, "Wrong type on address type element (0)"); args->items[0]->Dump(NET__PACKET_ERROR, " "); PyDecRef(base); return false; } PyInt *typei = (PyInt *) args->items[0]; switch(typei->value()) { case Any: { if(args->items.size() != 3) { codelog(NET__PACKET_ERROR, "Invalid number of elements in Any address tuple: %lu", args->items.size()); PyDecRef(base); return false; } type = Any; if(!_DecodeService(args->items[1]) || !_DecodeCallID(args->items[2])) { PyDecRef(base); return false; } break; } case Node: { if(args->items.size() != 4) { codelog(NET__PACKET_ERROR, "Invalid number of elements in Node address tuple: %lu", args->items.size()); PyDecRef(base); return false; } type = Node; if(!_DecodeTypeID(args->items[1]) || !_DecodeService(args->items[2]) || !_DecodeCallID(args->items[3])) { PyDecRef(base); return false; } break; } case Client: { if(args->items.size() != 4) { codelog(NET__PACKET_ERROR, "Invalid number of elements in Client address tuple: %lu", args->items.size()); PyDecRef(base); return false; } type = Client; if(!_DecodeTypeID(args->items[1]) || !_DecodeCallID(args->items[2]) || !_DecodeService(args->items[3])) { PyDecRef(base); return false; } break; } case Broadcast: { if(args->items.size() != 4) { codelog(NET__PACKET_ERROR, "Invalid number of elements in Broadcast address tuple: %lu", args->items.size()); PyDecRef(base); return false; } type = Broadcast; if(!args->items[1]->IsString()) { codelog(NET__PACKET_ERROR, "Invalid type %s for brodcastID", args->items[1]->TypeString()); PyDecRef(base); return false; } else if(!args->items[3]->IsString()) { codelog(NET__PACKET_ERROR, "Invalid type %s for idtype", args->items[3]->TypeString()); PyDecRef(base); return false; } PyString *bid = (PyString *) args->items[1]; PyString *idt = (PyString *) args->items[3]; service = bid->content(); bcast_idtype = idt->content(); //items[2] is either a list or a tuple. /* //PyList *nclist = (PyList *) args->items[2]; if(!nclist->items.empty()) { printf("Not decoding narrowcast list:"); nclist->Dump(NET__PACKET_ERROR, " "); }*/ break; } default: codelog(NET__PACKET_ERROR, "Unknown address type: %c", typei->value() ); PyDecRef(base); return false; } PyDecRef(base); return true; }
bool PyCachedObjectDecoder::Decode(PySubStream **in_ss) { PySubStream *ss = *in_ss; //consume *in_ss = NULL; PySafeDecRef( cache ); PySafeDecRef( objectID ); ss->DecodeData(); if(ss->decoded() == NULL) { sLog.Error("PyCachedObjectDecoder","Unable to decode initial stream for PycachedObject"); PyDecRef( ss ); return false; } if(!ss->decoded()->IsObject()) { sLog.Error("PyCachedObjectDecoder","Cache substream does not contain an object: %s", ss->decoded()->TypeString()); PyDecRef( ss ); return false; } PyObject *po = (PyObject *) ss->decoded(); //TODO: could check type string, dont care... (should be objectCaching.CachedObject) if(!po->arguments()->IsTuple()) { sLog.Error("PyCachedObjectDecoder","Cache object's args is not a tuple: %s", po->arguments()->TypeString()); PyDecRef( ss ); return false; } PyTuple *args = (PyTuple *) po->arguments(); if(args->items.size() != 7) { sLog.Error("PyCachedObjectDecoder","Cache object's args tuple has %lu elements instead of 7", args->items.size()); PyDecRef( ss ); return false; } if(!args->items[0]->IsTuple()) { sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not a Tuple: %s", 0, args->items[0]->TypeString()); PyDecRef( ss ); return false; } //ignore unknown [1] /*if(!args->items[1]->IsInt()) { _log(CLIENT__ERROR, "Cache object's arg %d is not a None: %s", 1, args->items[1]->TypeString()); PyDecRef( ss ); return false; }*/ if(!args->items[2]->IsInt()) { sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not an Integer: %s", 2, args->items[2]->TypeString()); PyDecRef( ss ); return false; } if(!args->items[3]->IsInt()) { sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not an Integer: %s", 3, args->items[3]->TypeString()); PyDecRef( ss ); return false; } if(!args->items[5]->IsInt()) { sLog.Error("PyCachedObjectDecoder","Cache object's arg %d is not a : %s", 5, args->items[5]->TypeString()); PyDecRef( ss ); return false; } PyTuple *objVt = (PyTuple *) args->items[0]; if(!objVt->items[0]->IsInt()) { sLog.Error("PyCachedObjectDecoder","Cache object's version tuple %d is not an Integer: %s", 0, objVt->items[0]->TypeString()); PyDecRef( ss ); return false; } if(!objVt->items[1]->IsInt()) { sLog.Error("PyCachedObjectDecoder","Cache object's version tuple %d is not an Integer: %s", 1, objVt->items[1]->TypeString()); PyDecRef( ss ); return false; } PyInt *nodeidr = (PyInt *) args->items[2]; PyInt *sharedr = (PyInt *) args->items[3]; PyInt *compressedr = (PyInt *) args->items[5]; PyInt *timer = (PyInt *) objVt->items[0]; PyInt *versionr = (PyInt *) objVt->items[1]; timestamp = timer->value(); version = versionr->value(); nodeID = nodeidr->value(); shared = ( sharedr->value() != 0 ); compressed = ( compressedr->value() != 0 ); //content (do this as the last thing, since its the heavy lifting): if(args->items[4]->IsSubStream()) { cache = (PySubStream *) args->items[4]; //take it args->items[4] = NULL; } else if(args->items[4]->IsBuffer()) { //this is a data buffer, likely compressed. PyBuffer* buf = args->items[4]->AsBuffer(); PyIncRef( buf ); cache = new PySubStream( buf ); } else if(args->items[4]->IsString()) { //this is a data buffer, likely compressed, not sure why it comes through as a string... PyString* str = args->items[4]->AsString(); cache = new PySubStream( new PyBuffer( *str ) ); } else { sLog.Error("PyCachedObjectMgr", "Cache object's arg %d is not a substream or buffer: %s", 4, args->items[4]->TypeString()); PyDecRef( ss ); return false; } objectID = args->items[6]; PyIncRef(objectID); PyDecRef( ss ); return true; }
bool PyPacket::Decode(PyRep **in_packet) { PyRep *packet = *in_packet; //consume *in_packet = NULL; PySafeDecRef(payload); PySafeDecRef(named_payload); if(packet->IsChecksumedStream()) { PyChecksumedStream* cs = packet->AsChecksumedStream(); //TODO: check cs->checksum packet = cs->stream(); PyIncRef( packet ); PyDecRef( cs ); } //Dragon nuance... it gets wrapped again if(packet->IsSubStream()) { PySubStream* ss = packet->AsSubStream(); ss->DecodeData(); if(ss->decoded() == NULL) { codelog(NET__PACKET_ERROR, "failed: unable to decode initial packet substream."); PyDecRef(packet); return false; } packet = ss->decoded(); PyIncRef( packet ); PyDecRef( ss ); } if(!packet->IsObject()) { codelog(NET__PACKET_ERROR, "failed: packet body is not an 'Object': %s", packet->TypeString()); PyDecRef(packet); return false; } PyObject *packeto = (PyObject *) packet; type_string = packeto->type()->content(); if(!packeto->arguments()->IsTuple()) { codelog(NET__PACKET_ERROR, "failed: packet body does not contain a tuple"); PyDecRef(packet); return false; } PyTuple *tuple = (PyTuple *) packeto->arguments(); if(tuple->items.size() != 7) { codelog(NET__PACKET_ERROR, "failed: packet body does not contain a tuple of length 7"); PyDecRef(packet); return false; } if(!tuple->items[0]->IsInt()) { codelog(NET__PACKET_ERROR, "failed: First main tuple element is not an integer"); PyDecRef(packet); return false; } PyInt *typer = (PyInt *) tuple->items[0]; switch(typer->value()) { case AUTHENTICATION_REQ: case AUTHENTICATION_RSP: case IDENTIFICATION_REQ: case IDENTIFICATION_RSP: case CALL_REQ: case CALL_RSP: case TRANSPORTCLOSED: case RESOLVE_REQ: case RESOLVE_RSP: case NOTIFICATION: case ERRORRESPONSE: case SESSIONCHANGENOTIFICATION: case SESSIONINITIALSTATENOTIFICATION: case PING_REQ: case PING_RSP: type = (MACHONETMSG_TYPE) typer->value(); break; default: codelog(NET__PACKET_ERROR, "failed: Unknown message type %"PRIu64, typer->value()); PyDecRef(packet); return false; break; } //source address if(!source.Decode(tuple->items[1])) { //error printed in decoder PyDecRef(packet); return false; } //dest address if(!dest.Decode(tuple->items[2])) { //error printed in decoder PyDecRef(packet); return false; } if(tuple->items[3]->IsInt()) { PyInt *i = (PyInt *) tuple->items[3]; userid = i->value(); } else if(tuple->items[3]->IsNone()) { userid = 0; } else { codelog(NET__PACKET_ERROR, "failed: User ID has invalid type"); PyDecRef(packet); return false; } //payload if(!(tuple->items[4]->IsBuffer() || tuple->items[4]->IsTuple())) { codelog(NET__PACKET_ERROR, "failed: Fifth main tuple element is not a tuple"); PyDecRef(packet); return false; } payload = (PyTuple *) tuple->items[4]; tuple->items[4] = NULL; //we keep this one //options dict if(tuple->items[5]->IsNone()) { named_payload = NULL; } else if(tuple->items[5]->IsDict()) { named_payload = (PyDict *) tuple->items[5]; tuple->items[5] = NULL; //we keep this too. } else { codelog(NET__PACKET_ERROR, "failed: Sixth main tuple element is not a dict"); PyDecRef(packet); return false; } PyDecRef(packet); return true; }