dhcp_message::dhcp_message(message_type type) { memset(this, 0, sizeof(*this)); options_magic = htonl(OPTION_MAGIC); uint8* next = PrepareMessage(type); FinishOptions(next); }
void NullUnit::GuiReporter::ReportCaseFail(const std::string& suite_name, const std::string& case_name, const std::string& reason, const std::string& filename, int lineNumber) { std::string preparedMessage(reason); PrepareMessage(preparedMessage); stream << "CASE_FAIL" << delim << suite_name << delim << case_name << delim << preparedMessage << delim << filename << delim << lineNumber << std::endl; }
int LeaveRoom() { char msg[1000]; if(strcmp(g_currroom, "anteroom")==0) { return Disconnect(); } PrepareMessage(msg,"!leave","",g_username); SendMessage(msg); return 1; }
int Disconnect() { char msg[1000]; PrepareMessage(msg,"!bye","",g_username); SendMessage(msg); ClearGlobals(); g_doListen = 0; printf("DISCONNECTED!\n"); return 1; }
int EnterRoom(char *command, char *content) { if(strcmp(g_currroom, "anteroom") != 0) //it is not default room so u can not move to other { printf("You need to leave current room to be able to enter other!\n"); return -1; } char msg[MAX_MSG_SIZE]; PrepareMessage(msg, command, content, g_username); SendMessage(msg); return 1; }
void CBotChat::ReplyToMessage(CClient *pFrom, const char *msg) { if (m_flChatTime > g_pServer->GetTime()) return; // we already have something to say // generate a reply for this message char *p = g_pServer->BotManager()->chat.GenerateReply(m_pBot, pFrom, msg); // if we have found a reply if (p) { PrepareMessage(p); // reply to the message } }
int ConnectToServer(char *content) { //parsing address, port, username char currChar = content[0]; int charCount = 1; char username[31]; int i = 0; while(currChar!='@') { username[i] = currChar; currChar = content[charCount++]; i++; } username[i] = '\0'; i=0; char ip[45]; //45 max of ip address length currChar = content[charCount++]; while(currChar != ':' && currChar!='\n' && currChar!='\0') { ip[i] = currChar; currChar = content[charCount++]; i++; } ip[i]='\0'; char port[4]; if(currChar!=':') //use default port { g_port = 8888; } else { //charCount++; for (int j = 0; j < 4; ++j) { port[j] = content[charCount++]; } g_port = atoi(port); } printf("%s : %s : %i\n",username,ip,g_port); strcpy(g_serverip, ip); g_serverFd = connect_socket(g_serverip, (uint16_t) g_port); //make connection with server char retMsg[MAX_MSG_SIZE]; strcpy(g_username, username); PrepareMessage(retMsg, "!connect", content, g_username); SendMessage(retMsg); strcat(g_currroom, "anteroom"); //START LISTENING THREAD struct ListeningThreadArg *targ; if ((targ = (struct ListeningThreadArg *) calloc (1, sizeof(struct ListeningThreadArg))) == NULL) ERR("calloc"); g_doListen = 1; targ->serverFd = &g_serverFd; pthread_t thread; if (pthread_create(&thread, NULL, listeningThreadHandler, (void *) targ) != 0) ERR("pthread_create"); if (pthread_detach(thread) != 0) ERR("pthread_detach"); return 0; }
int GetRooms(char *command) { char msg[MAX_MSG_SIZE]; PrepareMessage(msg, command, "", g_username); SendMessage(msg); return 1; }
int OpenRoom(char *command, char *content) { char msg[1000]; PrepareMessage(msg, "!open", content, g_username); SendMessage(msg); return 1; }
int CloseRoom(char *content) { char msg[1000]; PrepareMessage(msg, "!close", content, g_username); SendMessage(msg); return 1; }
int SendMessageToRoom(char *msgContent) { char msg[1000]; PrepareMessage(msg, "!send", msgContent, g_username); SendMessage(msg); return 1; }
int CheckFiles() { char msg[1000]; PrepareMessage(msg,"!files","",g_username); SendMessage(msg); return 1; }
int UploadFile(char *filename) { char msg[1000]; PrepareMessage(msg, "!push",filename, g_username); SendMessage(msg); return 1; }
OP_STATUS OpScopeTPReader::ParseStream() { OP_ASSERT(enabled); while (true) { switch (state) { case Message: { if (version == 0) { state = STP0_Message; continue; } else state = Prefix; message_version = 1; } case Prefix: { if (incoming->Length() < 4) return STPStatus::ERR_NOT_ENOUGH_DATA; char buffer[4]; // "STP" + ver // ARRAY OK 2009-05-05 jhoff incoming->Extract(0, 4, buffer); if (buffer[0] != 'S' || buffer[1] != 'T' || buffer[2] != 'P') { return OpStatus::ERR_PARSING_FAILED; } message.Clear(); incoming->Consume(4); unsigned int incoming_version = buffer[3]; // Check if we support the message if (incoming_version == 0) { // Support for STP/0 message wrapped in a STP/1 message message_version = 0; } else if (incoming_version > 1) { // TODO: Report it somewhere/somehow? state = IgnoreMessageSize; continue; } message.SetVersion((OpScopeTPMessage::MessageVersion)incoming_version); state = MessageSize; } case MessageSize: { unsigned int field_size; // FIXME: Field size is 64 bit BOOL3 result = ParseVarInt32(field_size); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; OP_ASSERT(field_size < (1u << 31)); protobuf_limit = field_size; has_service_field = has_command_field = has_type_field = has_payload_field = FALSE; if (message_version == 0) { // Support for STP/0 message wrapped in a STP/1 message // The exception is the size entry for STP/0 which is skipped body_size = field_size; OP_ASSERT((body_size % 2) == 0); stp0_size = body_size / 2; state = STP0_Init; continue; } state = STPType; } case STPType: { if (incoming->Length() < 1) return STPStatus::ERR_NOT_ENOUGH_DATA; unsigned int type = incoming->Extract1(0); protobuf_limit -= 1; incoming->Consume(1); if (type == OpScopeTPMessage::STP_Call) message.SetTransportType(OpScopeTPMessage::STP_Call); else if (type == OpScopeTPMessage::STP_Response) message.SetTransportType(OpScopeTPMessage::STP_Response); else if (type == OpScopeTPMessage::STP_Event) message.SetTransportType(OpScopeTPMessage::STP_Event); else if (type == OpScopeTPMessage::STP_Error) message.SetTransportType(OpScopeTPMessage::STP_Error); else { state = IgnoreFields; continue; } state = Field; } case Field: { if (protobuf_limit == 0) // Did we reach the end of the field data? { protobuf_limit = -1; // Remove limit state = MessageDone; continue; } unsigned int field; BOOL3 result = ParseVarInt32(field); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; int type = OpProtobufWireFormat::DecodeFieldType32(field); int number = OpProtobufWireFormat::DecodeFieldNumber32(field); if (number == OpScopeTPMessage::Field_ServiceName) { if (type == OpProtobufWireFormat::LengthDelimited) state = Service; } else if (number == OpScopeTPMessage::Field_CommandID) { if (type == OpProtobufWireFormat::VarInt) state = Command; } else if (number == OpScopeTPMessage::Field_Type) { if (type == OpProtobufWireFormat::VarInt) state = Type; } else if (number == OpScopeTPMessage::Field_Status) { if (type == OpProtobufWireFormat::VarInt) state = Status; } else if (number == OpScopeTPMessage::Field_Tag) { if (type == OpProtobufWireFormat::VarInt) state = Tag; } else if (number == OpScopeTPMessage::Field_Payload) { if (type == OpProtobufWireFormat::LengthDelimited) state = ChunkSize; payload.Clear(); } if (state == Field) { if (type == OpProtobufWireFormat::VarInt) state = IgnoreVarint; else if (type == OpProtobufWireFormat::Fixed32) state = IgnoreFixed32; else if (type == OpProtobufWireFormat::Fixed64) state = IgnoreFixed64; else if (type == OpProtobufWireFormat::LengthDelimited) state = IgnoreLengthDelimited; else return OpStatus::ERR_PARSING_FAILED; } continue; } case Service: { BOOL3 result = ParseVarInt32(string_length); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; message.ServiceName().Empty(); state = ServiceString; } case ServiceString: { BOOL3 result = ParseString(message.ServiceName(), string_length); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; has_service_field = TRUE; state = Field; continue; } case Command: { unsigned command_id; BOOL3 result = ParseVarInt32(command_id); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; message.SetCommandID(command_id); has_command_field = TRUE; state = Field; continue; } case Type: { unsigned int type; BOOL3 result = ParseVarInt32(type); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; if (type > OpScopeTPHeader::MessageTypeMax) return OpStatus::ERR; payload_format = static_cast<OpScopeTPMessage::MessageType>(type); has_type_field = TRUE; state = Field; continue; } case Status: { unsigned int status; BOOL3 result = ParseVarInt32(status); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; message.SetStatus(static_cast<OpScopeTPMessage::MessageStatus>(status)); state = Field; continue; } case Tag: { unsigned int tag; BOOL3 result = ParseVarInt32(tag); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; message.SetTag(tag); state = Field; continue; } case ChunkSize: { BOOL3 result = ParseVarInt32(chunk_size); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; if (chunk_size == 0) { chunk_size = 0; state = Field; has_payload_field = TRUE; continue; } body_size = chunk_size; state = ChunkData; } case ChunkData: { if (incoming->Length() < chunk_size) return STPStatus::ERR_NOT_ENOUGH_DATA; RETURN_IF_ERROR(OpScopeCopy(*incoming, payload, chunk_size)); incoming->Consume(chunk_size); protobuf_limit -= chunk_size; chunk_size = 0; has_payload_field = TRUE; state = Field; continue; } case MessageDone: { // TODO: Check for missing required fields if (!has_service_field || !has_command_field || !has_type_field || !has_payload_field) { // Missing fields, ignore this message // TODO: Should report about bad message to a callback. state = Message; continue; } RETURN_IF_ERROR(message.SetData(payload, payload_format)); OP_ASSERT(message.Type() != OpScopeTPHeader::None); OP_ASSERT(message.Data() != NULL); PrepareMessage(message); OnMessageParsed(message); RETURN_IF_ERROR(message.Free()); state = Message; return OpStatus::OK; } case STP0_Message: { if (version > 0) { state = Message; continue; } message.Clear(); state = STP0_Size; message_version = 0; } case STP0_Size: { BOOL3 result = ParseSTP0Size(stp0_size); if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; else if (result == NO) return STPStatus::ERR_PARSING_FAILED; body_size = stp0_size*2; state = STP0_Init; } case STP0_Init: { OP_DELETEA(stp0_incoming); stp0_incoming = OP_NEWA(uni_char, stp0_size); RETURN_OOM_IF_NULL(stp0_incoming); message.SetVersion(OpScopeTPMessage::Version_0); message.SetTransportType(OpScopeTPMessage::STP_Call); message.SetServiceName(UNI_L("")); message.SetCommandID(0); message.SetStatus(OpScopeTPMessage::OK); RETURN_IF_ERROR(message.Free()); message.SetTag(0); payload.Clear(); state = STP0_Data; } case STP0_Data: { OP_ASSERT(stp0_incoming != NULL); if (incoming->Length() < body_size) return STPStatus::ERR_NOT_ENOUGH_DATA; incoming->Extract(0, body_size, reinterpret_cast<char *>(stp0_incoming)); incoming->Consume(body_size); #ifndef OPERA_BIG_ENDIAN OpProtobufUtils::ByteSwap(stp0_incoming, stp0_size); #endif // OPERA_BIG_ENDIAN // Now find the service name and separate it from the data that follows const uni_char *stp0_data = NULL; for (unsigned int i = 0; i < stp0_size; ++i) { if (stp0_incoming[i] == ' ') { stp0_incoming[i] = '\0'; stp0_data = stp0_incoming + i + 1; break; } } OP_STATUS status = OpStatus::OK; if (stp0_data != NULL) { unsigned int data_size = stp0_size - (stp0_data - stp0_incoming); RETURN_IF_ERROR(message.SetServiceName(stp0_incoming)); // Check if it contains an STP/x header, if so decode extra headers. if (stp0_incoming[0] != '*' && data_size >= 4 && uni_strncmp(stp0_data, UNI_L("STP/"), 4) == 0) { status = ParseExtendedStp0(message, stp0_data, data_size); } else { RETURN_IF_ERROR(payload.AppendBytes(reinterpret_cast<const char *>(stp0_data), data_size*2)); // Sniff information if (stp0_incoming[0] == '*') payload_format = OpScopeTPMessage::JSON; // FIXME: The JSON type is just a temporary solution as the Scope type is removed else if (data_size > 0) { if (uni_strncmp(stp0_data, "<?xml", 5) == 0) payload_format = OpScopeTPMessage::XML; else if (stp0_data[0] == '[') payload_format = OpScopeTPMessage::JSON; } } } OP_DELETEA(stp0_incoming); stp0_incoming = NULL; chunk_size = stp0_size = 0; state = version == 0 ? STP0_Message : Message; if (stp0_data != NULL && OpStatus::IsSuccess(status)) { RETURN_IF_ERROR(message.SetData(payload, payload_format)); payload.Clear(); PrepareMessage(message); OnMessageParsed(message); RETURN_IF_ERROR(message.Free()); } if (stp0_data == NULL) return OpStatus::ERR_PARSING_FAILED; return OpStatus::OK; } case IgnoreVarint: { unsigned int tmp; BOOL3 result = ParseVarInt32(tmp); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; state = Field; continue; } case IgnoreFixed32: { unsigned int tmp; BOOL3 result = ParseFixed32(tmp); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; state = Field; continue; } case IgnoreFixed64: { UINT64 tmp; BOOL3 result = ParseFixed64(tmp); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; state = Field; continue; } case IgnoreLengthDelimited: { BOOL3 result = ParseVarInt32(ignore_size); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; state = IgnoreLengthDelimitedData; } case IgnoreLengthDelimitedData: { if (protobuf_limit >= 0) { OP_ASSERT(protobuf_limit >= (int)ignore_size); if (protobuf_limit < (int)ignore_size) // Cannot skip length-delimited data as it is crosses the limit return OpStatus::ERR_PARSING_FAILED; } if (incoming->Length() < ignore_size) return STPStatus::ERR_NOT_ENOUGH_DATA; incoming->Consume(ignore_size); if (protobuf_limit > 0) protobuf_limit -= ignore_size; state = Field; continue; } case IgnoreMessageSize: { // TODO: Should consume data as they arrive, better for iterative parsing unsigned int field_size; // FIXME: Field size is 64 bit BOOL3 result = ParseVarInt32(field_size); if (result == NO) return OpStatus::ERR_PARSING_FAILED; if (result == MAYBE) return STPStatus::ERR_NOT_ENOUGH_DATA; OP_ASSERT(field_size < (1u << 31)); protobuf_limit = field_size; state = IgnoreFields; } case IgnoreFields: { // TODO: Should consume data as they arrive, better for iterative parsing if ((int)incoming->Length() < protobuf_limit) return STPStatus::ERR_NOT_ENOUGH_DATA; incoming->Consume(protobuf_limit); protobuf_limit = -1; state = Message; continue; } default: OP_ASSERT(!"Invalid parser state"); } break; } return OpStatus::OK; }