bool dissect_rpc_packet(tvbuff_t *tvb, guint *offset, proto_tree *hadoop_tree, string& packetName, string& displayName) { guint len = tvb_get_ntohl(tvb, *offset); *offset += 4; const guint8* buf1 = tvb_get_ptr(tvb, *offset, len); *offset += len; len = tvb_get_ntohl(tvb, *offset); *offset += 4; const guint8* buf = tvb_get_ptr(tvb, *offset, len); map<string, Handles*>::iterator it = g_mapHandles.find( packetName ); if( it == g_mapHandles.end() ) { return false; // bug } DynamicMessageFactory factory; Handles* handles = it->second; const Message *message = NULL; message = factory.GetPrototype(handles->descriptor); Message *messagePacket = message->New(); if (messagePacket->ParseFromArray(buf, len)) { dissect_protobuf_message(messagePacket, tvb, offset, hadoop_tree, displayName, true); return true; } return false; }
bool dissect_rpcBody(tvbuff_t *tvb, guint* offset, proto_tree *tree, string& rpcMethodParam) { uint32 rpcParamLen = 0; if (!read_varint32(tvb, offset, &rpcParamLen)) { return false; } if (0 == rpcParamLen) { return true; } const guint8* rpcParamBuf = tvb_get_ptr(tvb, *offset, rpcParamLen); map<string, Handles*>::iterator itParam = g_mapHandles.find( rpcMethodParam ); if( itParam == g_mapHandles.end() ) { return false; // bug } DynamicMessageFactory factory; Handles* handles = itParam->second; const Message *message = NULL; message = factory.GetPrototype(handles->descriptor); Message *rpcParamMessage = message->New(); if (rpcParamMessage->ParseFromArray(rpcParamBuf, rpcParamLen)) { dissect_protobuf_message(rpcParamMessage, tvb, offset, tree, rpcMethodParam, true); return true; } }
bool dissect_reqheader(tvbuff_t *tvb, guint* offset, proto_tree *tree, string& rpcMethodName) { uint32 reqHeaderLen = 0; if (!read_varint32(tvb, offset, &reqHeaderLen)) { return false; } const guint8* reqHeaderBuf = tvb_get_ptr(tvb, *offset, reqHeaderLen); map<string, Handles*>::iterator itRequest = g_mapHandles.find( "hadoop.common.RequestHeaderProto" ); if( itRequest == g_mapHandles.end() ) { return false; // bug } DynamicMessageFactory factory; Handles* handles = itRequest->second; const Message *message = NULL; message = factory.GetPrototype(handles->descriptor); Message *reqHeaderMessage = message->New(); if (reqHeaderMessage->ParseFromArray(reqHeaderBuf, reqHeaderLen)) { dissect_protobuf_message(reqHeaderMessage, tvb, offset, tree, string("RequestHeaderProto"), true); const FieldDescriptor *field = NULL; field = reqHeaderMessage->GetDescriptor()->FindFieldByName("methodName"); const Reflection* reflection = reqHeaderMessage->GetReflection(); rpcMethodName = reflection->GetString(*reqHeaderMessage, field); return true; } }
int main() { DiskSourceTree sourceTree; sourceTree.MapPath("", "./"); Importer importer(&sourceTree, NULL); importer.Import("Foo.proto"); const Descriptor *descriptor = importer.pool()->FindMessageTypeByName("Pair"); cout << descriptor->DebugString(); DynamicMessageFactory factory; const Message *message = factory.GetPrototype(descriptor); Message *pair = message->New(); const Reflection *reflection = pair->GetReflection(); const FieldDescriptor *field = NULL; field = descriptor->FindFieldByName("key"); reflection->SetString(pair, field, "my key"); field = descriptor->FindFieldByName("value"); reflection->SetUInt32(pair, field, 1111); cout << pair->DebugString(); delete pair; return 0; }
const Message* ProtoBuf::GetMessageProtoType(const string& name, Importer* ipt) { const Descriptor* desc = NULL; desc = DescriptorPool::generated_pool()->FindMessageTypeByName(name); if (desc) { return MessageFactory::generated_factory()->GetPrototype(desc); } desc = ipt->pool()->FindMessageTypeByName(name); if (desc) { DynamicMessageFactory* df = new DynamicMessageFactory; return df->GetPrototype(desc); } return NULL; }
bool protobuf_get_message(const string msgName, tvbuff_t *tvb, guint* offset, bool bVarintLen, guint16 lenByte, Message **messagePacket) { // get message handles map<string, Handles*>::iterator it = g_mapHandles.find( msgName ); if( it == g_mapHandles.end() ) { return false; // bug } Handles* handles = it->second; // get message len guint len = 0; if (bVarintLen) { if (!read_varint32(tvb, offset, &len)) { return false; } } else { if (lenByte == 4) { len = tvb_get_ntohl(tvb, *offset); *offset += 4; } else { len = tvb_get_ntohs(tvb, *offset); *offset += 2; } } if (len > tvb_reported_length(tvb)) { return false; } // get message buffer const guint8* buf = tvb_get_ptr(tvb, *offset, len); DynamicMessageFactory *factory = new DynamicMessageFactory(); const Message *message = NULL; message = factory->GetPrototype(handles->descriptor); *messagePacket = message->New(); return (*messagePacket)->ParseFromArray(buf, len); }
Type* Protobuf::GetType(const Descriptor* descriptor) { TypeMap::iterator it = mTypeMap.find(descriptor); if (it != mTypeMap.end()) return it->second; Type* result = mTypeMap[descriptor] = new Type(this, factory.GetPrototype(descriptor)->New(), TypeTemplate->GetFunction()->NewInstance(), handles.size()); handles.push_back(result); Handle<Array> types = handle_->GetInternalField(1).As<Array>(); types->Set(types->Length(), result->handle_); return result; }
bool dissect_rpcheader(tvbuff_t *tvb, guint* offset, proto_tree *tree, bool* bRequest, int *callId) { uint32 rpcHeaderLen = 0; if (!read_varint32(tvb, offset, &rpcHeaderLen)) { return false; } const guint8* rpcHeaderBuf = tvb_get_ptr(tvb, *offset, rpcHeaderLen); *bRequest = true; // first find hadoop common rpcheader map<string, Handles*>::iterator itRequest = g_mapHandles.find( "hadoop.common.RpcRequestHeaderProto" ); if( itRequest == g_mapHandles.end() ) { return false; // bug } DynamicMessageFactory factory; Handles* handles = itRequest->second; const Message *message = NULL; message = factory.GetPrototype(handles->descriptor); Message *reqMessage = message->New(); if (reqMessage->ParseFromArray(rpcHeaderBuf,rpcHeaderLen)) { dissect_protobuf_message(reqMessage, tvb, offset, tree, string("RpcRequestHeaderProto"), true); *bRequest = true; // get callid for response const FieldDescriptor *field = NULL; field = reqMessage->GetDescriptor()->FindFieldByName("callId"); const Reflection* reflection = reqMessage->GetReflection(); *callId = reflection->GetInt32(*reqMessage, field); return true; } else { // maybe response map<string, Handles*>::iterator itResponse = g_mapHandles.find( "hadoop.common.RpcResponseHeaderProto" ); if( itResponse == g_mapHandles.end() ) { return false; // bug } handles = itResponse->second; message = factory.GetPrototype(handles->descriptor); Message *responseMessage = message->New(); if (responseMessage->ParseFromArray(rpcHeaderBuf,rpcHeaderLen)) { dissect_protobuf_message(responseMessage, tvb, offset, tree, string("RpcResponseHeaderProto"), true); *bRequest = false; // get callid const FieldDescriptor *field = NULL; field = responseMessage->GetDescriptor()->FindFieldByName("callId"); const Reflection* reflection = responseMessage->GetReflection(); *callId = reflection->GetUInt32(*responseMessage, field); return true; } return false; // not rpcheader } }