Exemplo n.º 1
0
bool PyCallStream::Decode(const std::string &type, PyTuple *&in_payload) {
    PyTuple *payload = in_payload;   //consume
    in_payload = NULL;

    PySafeDecRef(arg_tuple);
    PySafeDecRef(arg_dict);
    arg_tuple = NULL;
    arg_dict = NULL;

    if(type != "macho.CallReq") {
        codelog(NET__PACKET_ERROR, "failed: packet payload has unknown string type '%s'", type.c_str());
        PyDecRef(payload);
        return false;
    }

    if (payload->items.size() != 1) {
        codelog(NET__PACKET_ERROR, "invalid tuple length %lu", payload->items.size());
        PyDecRef(payload);
        return false;
    }
    if (!payload->items[0]->IsTuple()) {
        codelog(NET__PACKET_ERROR, "non tuple payload[0]");
        PyDecRef(payload);
        return false;
    }

    PyTuple *payload2 = (PyTuple *) payload->items[0];
    if(payload2->items.size() != 2) {
        codelog(NET__PACKET_ERROR, "invalid tuple2 length %lu", payload2->items.size());
        PyDecRef(payload);
        return false;
    }

    //decode inner payload tuple
    //ignore tuple 0, it should be an int, dont know what it is
    if(!payload2->items[1]->IsSubStream()) {
        codelog(NET__PACKET_ERROR, "non-substream type");
        PyDecRef(payload);
        return false;
    }
    PySubStream *ss = (PySubStream *) payload2->items[1];

    ss->DecodeData();
    if(ss->decoded() == NULL) {
        codelog(NET__PACKET_ERROR, "Unable to decode call stream");
        PyDecRef(payload);
        return false;
    }

    if(!ss->decoded()->IsTuple()) {
        codelog(NET__PACKET_ERROR, "packet body does not contain a tuple");
        PyDecRef(payload);
        return false;
    }

    PyTuple *maint = (PyTuple *) ss->decoded();
    if(maint->items.size() != 4) {
        codelog(NET__PACKET_ERROR, "packet body has %lu elements, expected %d", maint->items.size(), 4);
        PyDecRef(payload);
        return false;
    }

    //parse first tuple element, unknown
    if(maint->items[0]->IsInt()) {
        PyInt *tuple0 = (PyInt *) maint->items[0];
        remoteObject = tuple0->value();
        remoteObjectStr = "";
    } else if(maint->items[0]->IsString()) {
        PyString *tuple0 = (PyString *) maint->items[0];
        remoteObject = 0;
        remoteObjectStr = tuple0->content();
    } else {
        codelog(NET__PACKET_ERROR, "tuple[0] has invalid type %s", maint->items[0]->TypeString());
        codelog(NET__PACKET_ERROR, " in:");
        payload->Dump(NET__PACKET_ERROR, "    ");
        PyDecRef(payload);
        return false;
    }

    //parse tuple[1]: method name
    if(maint->items[1]->IsString()) {
        PyString *i = (PyString *) maint->items[1];
        method = i->content();
    } else {
        codelog(NET__PACKET_ERROR, "tuple[1] has non-string type");
        maint->items[1]->Dump(NET__PACKET_ERROR, " --> ");
        codelog(NET__PACKET_ERROR, " in:");
        payload->Dump(NET__PACKET_ERROR, "    ");
        PyDecRef(payload);
        return false;
    }

    //grab argument list.
    if(!maint->items[2]->IsTuple()) {
        codelog(NET__PACKET_ERROR, "argument list has non-tuple type");
        maint->items[2]->Dump(NET__PACKET_ERROR, " --> ");
        codelog(NET__PACKET_ERROR, "in:");
        payload->Dump(NET__PACKET_ERROR, "    ");
        PyDecRef(payload);
        return false;
    }
    arg_tuple = (PyTuple *) maint->items[2];
    maint->items[2] = NULL; //we keep this one

    //options dict
    if(maint->items[3]->IsNone()) {
        arg_dict = NULL;
    } else if(maint->items[3]->IsDict()) {
        arg_dict = (PyDict *) maint->items[3];
        maint->items[3] = NULL; //we keep this too.
    } else {
        codelog(NET__PACKET_ERROR, "tuple[3] has non-dict type");
        maint->items[3]->Dump(NET__PACKET_ERROR, " --> ");
        codelog(NET__PACKET_ERROR, "in:");
        payload->Dump(NET__PACKET_ERROR, "    ");
        PyDecRef(payload);
        return false;
    }

    PyDecRef(payload);
    return true;
}
Exemplo n.º 2
0
bool EVENotificationStream::Decode(const std::string &pkt_type, const std::string &notify_type, PyTuple *&in_payload) {
    PyTuple *payload = in_payload;   //consume
    in_payload = NULL;

    PySafeDecRef(args);
    args = NULL;

    if(pkt_type != "macho.Notification") {
        codelog(NET__PACKET_ERROR, "notification payload has unknown string type %s", pkt_type.c_str());
        PyDecRef(payload);
        return false;
    }

    //decode payload tuple
    if(payload->items.size() != 2) {
        codelog(NET__PACKET_ERROR, "invalid tuple length %lu", payload->items.size());
        PyDecRef(payload);
        return false;
    }
    if(!payload->items[0]->IsTuple()) {
        codelog(NET__PACKET_ERROR, "non-tuple payload[0]");
        PyDecRef(payload);
        return false;
    }
    PyTuple *payload2 = (PyTuple *) payload->items[0];
    if(payload2->items.size() != 2) {
        codelog(NET__PACKET_ERROR, "invalid tuple2 length %lu", payload2->items.size());
        PyDecRef(payload);
        return false;
    }

    //decode inner payload tuple
    //ignore tuple 0, it should be an int, dont know what it is
    if(!payload2->items[1]->IsSubStream()) {
        codelog(NET__PACKET_ERROR, "non-substream type");
        PyDecRef(payload);
        return false;
    }
    PySubStream *ss = (PySubStream *) payload2->items[1];

    ss->DecodeData();
    if(ss->decoded() == NULL) {
        codelog(NET__PACKET_ERROR, "Unable to decode call stream");
        PyDecRef(payload);
        return false;
    }

    if(!ss->decoded()->IsTuple()) {
        codelog(NET__PACKET_ERROR, "packet body does not contain a tuple");
        PyDecRef(payload);
        return false;
    }

    PyTuple *robjt = (PyTuple *) ss->decoded();
    if(robjt->items.size() != 2) {
        codelog(NET__PACKET_ERROR, "packet body has %lu elements, expected %d", robjt->items.size(), 2);
        PyDecRef(payload);
        return false;
    }

    //parse first tuple element, remote object
    if(robjt->items[0]->IsInt()) {
        PyInt *tuple0 = (PyInt *) robjt->items[0];
        remoteObject = tuple0->value();
        remoteObjectStr = "";
    } else if(robjt->items[0]->IsString()) {
        PyString *tuple0 = (PyString *) robjt->items[0];
        remoteObject = 0;
        remoteObjectStr = tuple0->content();
    } else {
        codelog(NET__PACKET_ERROR, "main tuple[0] has invalid type %s", robjt->items[0]->TypeString());
        _log(NET__PACKET_ERROR, " in:");
        payload->Dump( NET__PACKET_ERROR, "" );
        PyDecRef(payload);
        return false;
    }

    if(!robjt->items[1]->IsTuple()) {
        codelog(NET__PACKET_ERROR, "main tuple[1] has non-tuple type %s", robjt->items[0]->TypeString());
        _log(NET__PACKET_ERROR, " it is:");
        payload->Dump( NET__PACKET_ERROR, "" );
        PyDecRef(payload);
        return false;
    }



    PyTuple *subt = (PyTuple *) robjt->items[1];
    if(subt->items.size() != 2) {
        codelog(NET__PACKET_ERROR, "packet body has %lu elements, expected %d", subt->items.size(), 2);
        PyDecRef(payload);
        return false;
    }

    //parse first tuple element, remote object
    if(subt->items[0]->IsInt()) {
        //PyInt *tuple0 = (PyInt *) maint->items[0];
        //no idea what this is.
    } else {
        codelog(NET__PACKET_ERROR, "sub tuple[0] has invalid type %s", subt->items[0]->TypeString());
        _log(NET__PACKET_ERROR, " in:");
        payload->Dump( NET__PACKET_ERROR, "" );
        PyDecRef(payload);
        return false;
    }



    if(!subt->items[1]->IsTuple()) {
        codelog(NET__PACKET_ERROR, "subt tuple[1] has non-tuple type %s", robjt->items[0]->TypeString());
        _log(NET__PACKET_ERROR, " it is:");
        payload->Dump( NET__PACKET_ERROR, "" );
        PyDecRef(payload);
        return false;
    }

    args = (PyTuple *) subt->items[1];
    subt->items[1] = NULL;

    notifyType = notify_type;

    PyDecRef(payload);
    return true;
}
Exemplo n.º 3
0
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;
}