1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-14 20:31:56 +00:00

for bug #235, move functions of block and digest to struct.

This commit is contained in:
winlin 2014-11-29 13:08:43 +08:00
parent ff5cdb1cc1
commit 5d883e2e29
2 changed files with 176 additions and 161 deletions

View file

@ -312,31 +312,17 @@ namespace _srs_internal
return stream.read_4bytes();
}
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int srs_key_block_get_offset(key_block* key)
{
int max_offset_size = 764 - 128 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&key->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// create new key block data.
// if created, user must free it by srs_key_block_free
void srs_key_block_init(key_block* key)
void key_block::init()
{
key_block* key = this;
key->offset = (int32_t)rand();
key->random0 = NULL;
key->random1 = NULL;
int offset = srs_key_block_get_offset(key);
int offset = key->offsets();
srs_assert(offset >= 0);
key->random0_size = offset;
@ -356,11 +342,31 @@ namespace _srs_internal
}
}
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int key_block::offsets()
{
key_block* key = this;
int max_offset_size = 764 - 128 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&key->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// parse key block from c1s1.
// if created, user must free it by srs_key_block_free
// @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
int srs_key_block_parse(key_block* key, char* c1s1_key_bytes)
int key_block::parse(char* c1s1_key_bytes)
{
key_block* key = this;
int ret = ERROR_SUCCESS;
char* pp = c1s1_key_bytes + 764;
@ -371,7 +377,7 @@ namespace _srs_internal
key->random0 = NULL;
key->random1 = NULL;
int offset = srs_key_block_get_offset(key);
int offset = key->offsets();
srs_assert(offset >= 0);
pp = c1s1_key_bytes;
@ -396,8 +402,10 @@ namespace _srs_internal
// free the block data create by
// srs_key_block_init or srs_key_block_parse
void srs_key_block_free(key_block* key)
void key_block::free()
{
key_block* key = this;
if (key->random0) {
srs_freep(key->random0);
}
@ -406,31 +414,17 @@ namespace _srs_internal
}
}
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int srs_digest_block_get_offset(digest_block* digest)
{
int max_offset_size = 764 - 32 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&digest->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// create new digest block data.
// if created, user must free it by srs_digest_block_free
void srs_digest_block_init(digest_block* digest)
void digest_block::init()
{
digest_block* digest = this;
digest->offset = (int32_t)rand();
digest->random0 = NULL;
digest->random1 = NULL;
int offset = srs_digest_block_get_offset(digest);
int offset = digest->offsets();
srs_assert(offset >= 0);
digest->random0_size = offset;
@ -449,12 +443,32 @@ namespace _srs_internal
snprintf(digest->random1, digest->random1_size, "%s", RTMP_SIG_SRS_HANDSHAKE);
}
}
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int digest_block::offsets()
{
digest_block* digest = this;
int max_offset_size = 764 - 32 - 4;
int offset = 0;
u_int8_t* pp = (u_int8_t*)&digest->offset;
offset += *pp++;
offset += *pp++;
offset += *pp++;
offset += *pp++;
return offset % max_offset_size;
}
// parse digest block from c1s1.
// if created, user must free it by srs_digest_block_free
// @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
int srs_digest_block_parse(digest_block* digest, char* c1s1_digest_bytes)
int digest_block::parse(char* c1s1_digest_bytes)
{
digest_block* digest = this;
int ret = ERROR_SUCCESS;
char* pp = c1s1_digest_bytes;
@ -465,7 +479,7 @@ namespace _srs_internal
digest->random0 = NULL;
digest->random1 = NULL;
int offset = srs_digest_block_get_offset(digest);
int offset = digest->offsets();
srs_assert(offset >= 0);
digest->random0_size = offset;
@ -489,8 +503,10 @@ namespace _srs_internal
// free the block data create by
// srs_digest_block_init or srs_digest_block_parse
void srs_digest_block_free(digest_block* digest)
void digest_block::free()
{
digest_block* digest = this;
if (digest->random0) {
srs_freep(digest->random0);
}
@ -792,21 +808,21 @@ namespace _srs_internal
version = __srs_stream_read_4bytes(_c1s1 + 4); // client c1 version
if (_schema == srs_schema0) {
if ((ret = srs_key_block_parse(&block0.key, _c1s1 + 8)) != ERROR_SUCCESS) {
if ((ret = block0.key.parse(_c1s1 + 8)) != ERROR_SUCCESS) {
srs_error("parse the c1 key failed. ret=%d", ret);
return ret;
}
if ((ret = srs_digest_block_parse(&block1.digest, _c1s1 + 8 + 764)) != ERROR_SUCCESS) {
if ((ret = block1.digest.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) {
srs_error("parse the c1 digest failed. ret=%d", ret);
return ret;
}
srs_verbose("parse c1 key-digest success");
} else if (_schema == srs_schema1) {
if ((ret = srs_digest_block_parse(&block0.digest, _c1s1 + 8)) != ERROR_SUCCESS) {
if ((ret = block0.digest.parse(_c1s1 + 8)) != ERROR_SUCCESS) {
srs_error("parse the c1 key failed. ret=%d", ret);
return ret;
}
if ((ret = srs_key_block_parse(&block1.key, _c1s1 + 8 + 764)) != ERROR_SUCCESS) {
if ((ret = block1.key.parse(_c1s1 + 8 + 764)) != ERROR_SUCCESS) {
srs_error("parse the c1 digest failed. ret=%d", ret);
return ret;
}
@ -840,11 +856,11 @@ namespace _srs_internal
// generate signature by schema
if (_schema == srs_schema0) {
srs_key_block_init(&block0.key);
srs_digest_block_init(&block1.digest);
block0.key.init();
block1.digest.init();
} else {
srs_digest_block_init(&block0.digest);
srs_key_block_init(&block1.key);
block0.digest.init();
block1.key.init();
}
schema = _schema;
@ -941,8 +957,8 @@ namespace _srs_internal
}
if (schema == srs_schema0) {
srs_key_block_init(&block0.key);
srs_digest_block_init(&block1.digest);
block0.key.init();
block1.digest.init();
// directly generate the public key.
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
@ -953,8 +969,8 @@ namespace _srs_internal
}
srs_assert(pkey_size == 128);
} else {
srs_digest_block_init(&block0.digest);
srs_key_block_init(&block1.key);
block0.digest.init();
block1.key.init();
// directly generate the public key.
// @see: https://github.com/winlinvip/simple-rtmp-server/issues/148
@ -1048,11 +1064,11 @@ namespace _srs_internal
}
if (schema == srs_schema0) {
srs_key_block_free(&block0.key);
srs_digest_block_free(&block1.digest);
block0.key.free();
block1.digest.free();
} else {
srs_digest_block_free(&block0.digest);
srs_key_block_free(&block1.key);
block0.digest.free();
block1.key.free();
}
}
}

View file

@ -41,78 +41,6 @@ class SrsHandshakeBytes;
namespace _srs_internal
{
/**
* the schema type.
*/
enum srs_schema_type
{
srs_schema_invalid = 2,
/**
* key-digest sequence
*/
srs_schema0 = 0,
/**
* digest-key sequence
* @remark, FMS requires the schema1(digest-key), or connect failed.
*/
//
srs_schema1 = 1,
};
/**
* 764bytes key structure
* random-data: (offset)bytes
* key-data: 128bytes
* random-data: (764-offset-128-4)bytes
* offset: 4bytes
* @see also: http://blog.csdn.net/win_lin/article/details/13006803
*/
class key_block
{
public:
// (offset)bytes
char* random0;
int random0_size;
// 128bytes
char key[128];
// (764-offset-128-4)bytes
char* random1;
int random1_size;
// 4bytes
int32_t offset;
};
/**
* 764bytes digest structure
* offset: 4bytes
* random-data: (offset)bytes
* digest-data: 32bytes
* random-data: (764-4-offset-32)bytes
* @see also: http://blog.csdn.net/win_lin/article/details/13006803
*/
class digest_block
{
public:
// 4bytes
int32_t offset;
// (offset)bytes
char* random0;
int random0_size;
// 32bytes
char digest[32];
// (764-4-offset-32)bytes
char* random1;
int random1_size;
};
// the digest key generate size.
#define __SRS_OpensslHashSize 512
extern u_int8_t SrsGenuineFMSKey[];
@ -159,40 +87,111 @@ namespace _srs_internal
private:
virtual int do_initialize();
};
/**
* the schema type.
*/
enum srs_schema_type
{
srs_schema_invalid = 2,
/**
* key-digest sequence
*/
srs_schema0 = 0,
/**
* digest-key sequence
* @remark, FMS requires the schema1(digest-key), or connect failed.
*/
//
srs_schema1 = 1,
};
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int srs_key_block_get_offset(key_block* key);
/**
* 764bytes key structure
* random-data: (offset)bytes
* key-data: 128bytes
* random-data: (764-offset-128-4)bytes
* offset: 4bytes
* @see also: http://blog.csdn.net/win_lin/article/details/13006803
*/
class key_block
{
public:
// (offset)bytes
char* random0;
int random0_size;
// 128bytes
char key[128];
// (764-offset-128-4)bytes
char* random1;
int random1_size;
// 4bytes
int32_t offset;
public:
// create new key block data.
// if created, user must free it by srs_key_block_free
void init();
// calc the offset of key,
// the key->offset cannot be used as the offset of key.
int offsets();
// parse key block from c1s1.
// if created, user must free it by srs_key_block_free
// @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
int parse(char* c1s1_key_bytes);
// free the block data create by
// srs_key_block_init or srs_key_block_parse
void free();
};
// create new key block data.
// if created, user must free it by srs_key_block_free
void srs_key_block_init(key_block* key);
/**
* 764bytes digest structure
* offset: 4bytes
* random-data: (offset)bytes
* digest-data: 32bytes
* random-data: (764-4-offset-32)bytes
* @see also: http://blog.csdn.net/win_lin/article/details/13006803
*/
class digest_block
{
public:
// 4bytes
int32_t offset;
// (offset)bytes
char* random0;
int random0_size;
// 32bytes
char digest[32];
// (764-4-offset-32)bytes
char* random1;
int random1_size;
public:
// create new digest block data.
// if created, user must free it by srs_digest_block_free
void init();
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int offsets();
// parse key block from c1s1.
// if created, user must free it by srs_key_block_free
// @c1s1_key_bytes the key start bytes, maybe c1s1 or c1s1+764
int srs_key_block_parse(key_block* key, char* c1s1_key_bytes);
// free the block data create by
// srs_key_block_init or srs_key_block_parse
void srs_key_block_free(key_block* key);
// calc the offset of digest,
// the key->offset cannot be used as the offset of digest.
int srs_digest_block_get_offset(digest_block* digest);
// create new digest block data.
// if created, user must free it by srs_digest_block_free
void srs_digest_block_init(digest_block* digest);
// parse digest block from c1s1.
// if created, user must free it by srs_digest_block_free
// @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
int srs_digest_block_parse(digest_block* digest, char* c1s1_digest_bytes);
// free the block data create by
// srs_digest_block_init or srs_digest_block_parse
void srs_digest_block_free(digest_block* digest);
// parse digest block from c1s1.
// if created, user must free it by srs_digest_block_free
// @c1s1_digest_bytes the digest start bytes, maybe c1s1 or c1s1+764
int parse(char* c1s1_digest_bytes);
// free the block data create by
// srs_digest_block_init or srs_digest_block_parse
void free();
};
/**
* copy whole c1s1 to bytes.