00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "DiscoveryUdpMessage.h"
00037 #include <core/Utilities.h>
00038 #include <string.h>
00039
00040
00041 using namespace std;
00042 using namespace wosh;
00043
00044
00045
00046
00047 long DiscoveryUdpFormat::isValidPacket( const char* data, int maxSize ) {
00048 if ( data == NULL) return 0;
00049 static const int len = strlen(_DiscoveryUdpFormat_PacketHeader);
00050 if ( maxSize < len ) return 0;
00051 if ( strncmp(_DiscoveryUdpFormat_PacketHeader, data, len) != 0 ) return 0;
00052 data += len;
00053 data += sizeof(char)*2;
00054 int32 proto = 0;
00055 memcpy( &proto, data, sizeof(int32) );
00056 return proto;
00057 }
00058
00059
00060
00061 int DiscoveryUdpFormat::writeHeader( char* out_data, int maxSize ) const {
00062 if ( out_data == NULL ) return 0;
00063 static const int len = strlen(_DiscoveryUdpFormat_PacketHeader);
00064 if ( maxSize < (int)(len+sizeof(char)*2+sizeof(int32)) ) return 0;
00065 strcpy(out_data, _DiscoveryUdpFormat_PacketHeader);
00066 out_data += len;
00067 memset( out_data, 0, sizeof(char)*2 );
00068 out_data += sizeof(char)*2;
00069 int32 proto = getProtocolVersion();
00070 memcpy( out_data, &proto, sizeof(int32) );
00071 return len + sizeof(char)*2 + sizeof(int32);
00072 }
00073
00074 int DiscoveryUdpFormat::headerSize() const {
00075 return strlen(_DiscoveryUdpFormat_PacketHeader) + sizeof(char)*2 + sizeof(int32);
00076 }
00077
00078
00079
00080 char* DiscoveryUdpFormat::putInt( long value, char* data, int maxSize ) {
00081 if ( maxSize < (int)sizeof(int32) ) return data;
00082 int32 tmpInteger = value;
00083 memcpy( data, &tmpInteger, sizeof(int32) );
00084 memset( data+sizeof(char)*2, 0, sizeof(char)*2 );
00085 return data + sizeof(char)*2 + sizeof(int32);
00086 }
00087
00088 const char* DiscoveryUdpFormat::popInt( long& value, const char* data, int maxSize ) {
00089 if ( maxSize < (int)sizeof(int32) ) return data;
00090 int32 tmpInteger = value;
00091 memcpy( &tmpInteger, data, sizeof(int32) );
00092 value = tmpInteger;
00093 return data + sizeof(char)*2 + sizeof(int32);
00094 }
00095
00096 char* DiscoveryUdpFormat::putString( const std::string& value, char* data, int maxSize ) {
00097 if ( maxSize < (int)value.size() ) return data;
00098 memcpy( data, value.c_str(), value.size() );
00099 memset( data+value.size()+sizeof(char), 0, sizeof(char)*2 );
00100 return data + value.size() + sizeof(char)*2;
00101 }
00102
00103 const char* DiscoveryUdpFormat::popString( std::string& value, const char* data, int maxSize ) {
00104 if ( maxSize < (int)sizeof(char)*2 ) return data;
00105 value.assign(data);
00106 return data + value.size() + sizeof(char)*2;
00107 }
00108
00109
00110
00111 bool DiscoveryUdpFormat_84::isRequest( const char* data, int maxSize ) {
00112 if ( data == NULL) return false;
00113 int offset = headerSize();
00114 if ( offset >= maxSize ) return false;
00115 if ( strcmp(data+offset, _DiscoveryUdpFormat_PacketRequest) == 0 ) return true;
00116 return false;
00117 }
00118
00119 WRESULT DiscoveryUdpFormat_84::encode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, char* out_data, int& out_size, int buffer_size ) {
00120 if ( out_data == NULL) return WRET_ERR_PARAM;
00121 char* outp = out_data + writeHeader(out_data, buffer_size);
00122
00123 outp = putString( d_data.getKernelName(), outp, buffer_size-(outp-out_data) );
00124 outp = putInt( d_data.getKernelState(), outp, buffer_size-(outp-out_data) );
00125 outp = putInt( d_info.getNotifyFrequency(), outp, buffer_size-(outp-out_data) );
00126 outp = putString( d_info.getLocalProtocol(), outp, buffer_size-(outp-out_data) );
00127
00128 string protocols; String::join(protocols, d_data.protocols, ";;");
00129 outp = putString( protocols, outp, buffer_size-(outp-out_data) );
00130
00131 string neighbours; String::join(neighbours, d_data.neighbours, ";");
00132 outp = putString( neighbours, outp, buffer_size-(outp-out_data) );
00133
00134 memset(outp, 0, sizeof(char)*2);
00135 out_size = outp - out_data + sizeof(char)*2;
00136 return WRET_OK;
00137 }
00138
00139 WRESULT DiscoveryUdpFormat_84::decode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, const char* data, int maxSize ) {
00140 if ( data == NULL) return WRET_ERR_PARAM;
00141 const char* inp = data + headerSize();
00142 string bufferStr; long bufferLong;
00143 bufferStr.clear();
00144 inp = popString(bufferStr, inp, maxSize-(inp-data) ); d_data.setKernelName(bufferStr);
00145 bufferStr.clear();
00146 inp = popInt(bufferLong, inp, maxSize-(inp-data) ); d_data.setKernelState(bufferLong);
00147 bufferStr.clear();
00148 inp = popInt(bufferLong, inp, maxSize-(inp-data) ); d_info.setNotifyFrequency(bufferLong);
00149 bufferStr.clear();
00150 inp = popString(bufferStr, inp, maxSize-(inp-data) ); d_info.setLocalProtocol(bufferStr);
00151 bufferStr.clear();
00152 d_data.protocols.clear();
00153 inp = popString(bufferStr, inp, maxSize-(inp-data) );
00154 wosh::String::split(d_data.protocols, bufferStr, ";;", _String_SPLIT_OPTION_NOT_EMPTY | _String_SPLIT_OPTION_TRIM | _String_SPLIT_OPTION_UNIQUE );
00155 bufferStr.clear();
00156 d_data.neighbours.clear();
00157 inp = popString(bufferStr, inp, maxSize-(inp-data) );
00158 wosh::String::split(d_data.neighbours, bufferStr, ";", _String_SPLIT_OPTION_NOT_EMPTY | _String_SPLIT_OPTION_TRIM | _String_SPLIT_OPTION_UNIQUE );
00159 return WRET_OK;
00160 }
00161
00162
00163
00164 bool DiscoveryUdpFormat_85::isRequest( const char* data, int maxSize ) {
00165 if ( data == NULL) return false;
00166 (void)maxSize;
00167 return false;
00168 }
00169
00170 WRESULT DiscoveryUdpFormat_85::encode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, char* out_data, int& out_size, int buffer_size ) {
00171 if ( out_data == NULL) return WRET_ERR_PARAM;
00172 (void)d_info; (void)d_data; (void)out_size; (void)buffer_size;
00173 return WRET_NOT_IMPLEMENTED;
00174 }
00175
00176 WRESULT DiscoveryUdpFormat_85::decode( wosh::WoshDiscoveryInfo& d_info, wosh::WoshDiscoveryData& d_data, const char* data, int maxSize ) {
00177 if ( data == NULL) return WRET_ERR_PARAM;
00178 (void)d_info; (void)d_data; (void)maxSize;
00179 return WRET_NOT_IMPLEMENTED;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 DiscoveryUdpMessageRequest::DiscoveryUdpMessageRequest() {
00191 this->buffer_size = strlen(_DiscoveryUdpFormat_PacketHeader);
00192 this->buffer = new char[this->buffer_size+1];
00193 memset(this->buffer, 0, this->buffer_size+1);
00194 memcpy(this->buffer, _DiscoveryUdpFormat_PacketHeader, this->buffer_size);
00195 }
00196
00197 DiscoveryUdpMessageRequest::~DiscoveryUdpMessageRequest() {
00198 delete this->buffer; this->buffer = NULL;
00199 }
00200
00201
00202