Nog een poging...

This commit is contained in:
Thulinma 2010-11-08 00:56:49 +01:00
parent 8c646c6391
commit 0e68f770e3
4 changed files with 92 additions and 88 deletions

View file

@ -108,18 +108,18 @@ void SendChunk(chunkpack ch){
} }
} }
if (ch.cs_id <= 63){ if (ch.cs_id <= 63){
tmp = chtype | ch.cs_id; fwrite(&tmp, 1, 1, CONN); tmp = chtype | ch.cs_id; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=1; snd_cnt+=1;
}else{ }else{
if (ch.cs_id <= 255+64){ if (ch.cs_id <= 255+64){
tmp = chtype | 0; fwrite(&tmp, 1, 1, CONN); tmp = chtype | 0; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ch.cs_id - 64; fwrite(&tmp, 1, 1, CONN); tmp = ch.cs_id - 64; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=2; snd_cnt+=2;
}else{ }else{
tmp = chtype | 1; fwrite(&tmp, 1, 1, CONN); tmp = chtype | 1; DDV_write(&tmp, 1, 1, CONN_fd);
tmpi = ch.cs_id - 64; tmpi = ch.cs_id - 64;
tmp = tmpi % 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi % 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi / 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi / 256; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=3; snd_cnt+=3;
} }
} }
@ -129,67 +129,67 @@ void SendChunk(chunkpack ch){
if (chtype == 0x00){ if (chtype == 0x00){
tmpi = ch.timestamp; tmpi = ch.timestamp;
if (tmpi >= 0x00ffffff){ntime = tmpi; tmpi = 0x00ffffff;} if (tmpi >= 0x00ffffff){ntime = tmpi; tmpi = 0x00ffffff;}
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, CONN); tmp = tmpi / (256*256); DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi / 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi / 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi % 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi % 256; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=3; snd_cnt+=3;
}else{ }else{
tmpi = ch.timestamp - prev.timestamp; tmpi = ch.timestamp - prev.timestamp;
if (tmpi >= 0x00ffffff){ntime = tmpi; tmpi = 0x00ffffff;} if (tmpi >= 0x00ffffff){ntime = tmpi; tmpi = 0x00ffffff;}
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, CONN); tmp = tmpi / (256*256); DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi / 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi / 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi % 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi % 256; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=3; snd_cnt+=3;
} }
if (chtype != 0x80){ if (chtype != 0x80){
//len //len
tmpi = ch.len; tmpi = ch.len;
tmp = tmpi / (256*256); fwrite(&tmp, 1, 1, CONN); tmp = tmpi / (256*256); DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi / 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi / 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi % 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi % 256; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=3; snd_cnt+=3;
//msg type id //msg type id
tmp = ch.msg_type_id; fwrite(&tmp, 1, 1, CONN); tmp = ch.msg_type_id; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=1; snd_cnt+=1;
if (chtype != 0x40){ if (chtype != 0x40){
//msg stream id //msg stream id
tmp = ch.msg_stream_id % 256; fwrite(&tmp, 1, 1, CONN); tmp = ch.msg_stream_id % 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ch.msg_stream_id / 256; fwrite(&tmp, 1, 1, CONN); tmp = ch.msg_stream_id / 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ch.msg_stream_id / (256*256); fwrite(&tmp, 1, 1, CONN); tmp = ch.msg_stream_id / (256*256); DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ch.msg_stream_id / (256*256*256); fwrite(&tmp, 1, 1, CONN); tmp = ch.msg_stream_id / (256*256*256); DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=4; snd_cnt+=4;
} }
} }
} }
//support for 0x00ffffff timestamps //support for 0x00ffffff timestamps
if (ntime){ if (ntime){
tmp = ntime / (256*256*256); fwrite(&tmp, 1, 1, CONN); tmp = ntime / (256*256*256); DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ntime / (256*256); fwrite(&tmp, 1, 1, CONN); tmp = ntime / (256*256); DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ntime / 256; fwrite(&tmp, 1, 1, CONN); tmp = ntime / 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ntime % 256; fwrite(&tmp, 1, 1, CONN); tmp = ntime % 256; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=4; snd_cnt+=4;
} }
ch.len_left = 0; ch.len_left = 0;
while (ch.len_left < ch.len){ while (ch.len_left < ch.len){
tmpi = ch.len - ch.len_left; tmpi = ch.len - ch.len_left;
if (tmpi > chunk_snd_max){tmpi = chunk_snd_max;} if (tmpi > chunk_snd_max){tmpi = chunk_snd_max;}
fwrite((ch.data + ch.len_left), 1, tmpi, CONN); DDV_write((ch.data + ch.len_left), 1, tmpi, CONN_fd);
snd_cnt+=tmpi; snd_cnt+=tmpi;
ch.len_left += tmpi; ch.len_left += tmpi;
if (ch.len_left < ch.len){ if (ch.len_left < ch.len){
if (ch.cs_id <= 63){ if (ch.cs_id <= 63){
tmp = 0xC0 + ch.cs_id; fwrite(&tmp, 1, 1, CONN); tmp = 0xC0 + ch.cs_id; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=1; snd_cnt+=1;
}else{ }else{
if (ch.cs_id <= 255+64){ if (ch.cs_id <= 255+64){
tmp = 0xC0; fwrite(&tmp, 1, 1, CONN); tmp = 0xC0; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = ch.cs_id - 64; fwrite(&tmp, 1, 1, CONN); tmp = ch.cs_id - 64; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=2; snd_cnt+=2;
}else{ }else{
tmp = 0xC1; fwrite(&tmp, 1, 1, CONN); tmp = 0xC1; DDV_write(&tmp, 1, 1, CONN_fd);
tmpi = ch.cs_id - 64; tmpi = ch.cs_id - 64;
tmp = tmpi % 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi % 256; DDV_write(&tmp, 1, 1, CONN_fd);
tmp = tmpi / 256; fwrite(&tmp, 1, 1, CONN); tmp = tmpi / 256; DDV_write(&tmp, 1, 1, CONN_fd);
snd_cnt+=4; snd_cnt+=4;
} }
} }
@ -310,19 +310,19 @@ struct chunkpack getChunk(){
gettimeofday(&lastrec, 0); gettimeofday(&lastrec, 0);
struct chunkpack ret; struct chunkpack ret;
unsigned char temp; unsigned char temp;
fread(&(ret.chunktype), 1, 1, CONN); DDV_read(&(ret.chunktype), 1, 1, CONN_fd);
rec_cnt++; rec_cnt++;
//read the chunkstream ID properly //read the chunkstream ID properly
switch (ret.chunktype & 0x3F){ switch (ret.chunktype & 0x3F){
case 0: case 0:
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
rec_cnt++; rec_cnt++;
ret.cs_id = temp + 64; ret.cs_id = temp + 64;
break; break;
case 1: case 1:
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.cs_id = temp + 64; ret.cs_id = temp + 64;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.cs_id += temp * 256; ret.cs_id += temp * 256;
rec_cnt+=2; rec_cnt+=2;
break; break;
@ -334,57 +334,57 @@ struct chunkpack getChunk(){
//process the rest of the header, for each chunk type //process the rest of the header, for each chunk type
switch (ret.chunktype & 0xC0){ switch (ret.chunktype & 0xC0){
case 0x00: case 0x00:
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp = temp*256*256; ret.timestamp = temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp*256; ret.timestamp += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp; ret.timestamp += temp;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.len = temp*256*256; ret.len = temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.len += temp*256; ret.len += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.len += temp; ret.len += temp;
ret.len_left = 0; ret.len_left = 0;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.msg_type_id = temp; ret.msg_type_id = temp;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.msg_stream_id = temp; ret.msg_stream_id = temp;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.msg_stream_id += temp*256; ret.msg_stream_id += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.msg_stream_id += temp*256*256; ret.msg_stream_id += temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.msg_stream_id += temp*256*256*256; ret.msg_stream_id += temp*256*256*256;
rec_cnt+=11; rec_cnt+=11;
break; break;
case 0x40: case 0x40:
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp = temp*256*256; ret.timestamp = temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp*256; ret.timestamp += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp; ret.timestamp += temp;
ret.timestamp += prev.timestamp; ret.timestamp += prev.timestamp;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.len = temp*256*256; ret.len = temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.len += temp*256; ret.len += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.len += temp; ret.len += temp;
ret.len_left = 0; ret.len_left = 0;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.msg_type_id = temp; ret.msg_type_id = temp;
ret.msg_stream_id = prev.msg_stream_id; ret.msg_stream_id = prev.msg_stream_id;
rec_cnt+=7; rec_cnt+=7;
break; break;
case 0x80: case 0x80:
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp = temp*256*256; ret.timestamp = temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp*256; ret.timestamp += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp; ret.timestamp += temp;
ret.timestamp += prev.timestamp; ret.timestamp += prev.timestamp;
ret.len = prev.len; ret.len = prev.len;
@ -414,20 +414,20 @@ struct chunkpack getChunk(){
} }
//read extended timestamp, if neccesary //read extended timestamp, if neccesary
if (ret.timestamp == 0x00ffffff){ if (ret.timestamp == 0x00ffffff){
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp = temp*256*256*256; ret.timestamp = temp*256*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp*256*256; ret.timestamp += temp*256*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp*256; ret.timestamp += temp*256;
fread(&temp, 1, 1, CONN); DDV_read(&temp, 1, 1, CONN_fd);
ret.timestamp += temp; ret.timestamp += temp;
rec_cnt+=4; rec_cnt+=4;
} }
//read data if length > 0, and allocate it //read data if length > 0, and allocate it
if (ret.real_len > 0){ if (ret.real_len > 0){
ret.data = (unsigned char*)malloc(ret.real_len); ret.data = (unsigned char*)malloc(ret.real_len);
fread(ret.data, 1, ret.real_len, CONN); DDV_read(ret.data, 1, ret.real_len, CONN_fd);
rec_cnt+=ret.real_len; rec_cnt+=ret.real_len;
}else{ }else{
ret.data = 0; ret.data = 0;
@ -493,7 +493,7 @@ chunkpack getWholeChunk(){
free(ret);//cleanup returned chunk free(ret);//cleanup returned chunk
return gwc_complete; return gwc_complete;
} }
if (feof(CONN) != 0){break;} if (feof(CONN_fd) != 0){break;}
counter++; counter++;
} }
gwc_complete.msg_type_id = 0; gwc_complete.msg_type_id = 0;

View file

@ -14,11 +14,11 @@ bool doHandshake(){
Handshake Client; Handshake Client;
Handshake Server; Handshake Server;
/** Read C0 **/ /** Read C0 **/
fread(&(Version), 1, 1, CONN); DDV_read(&(Version), 1, 1, CONN_fd);
/** Read C1 **/ /** Read C1 **/
fread(Client.Time, 1, 4, CONN); DDV_read(Client.Time, 1, 4, CONN_fd);
fread(Client.Zero, 1, 4, CONN); DDV_read(Client.Zero, 1, 4, CONN_fd);
fread(Client.Random, 1, 1528, CONN); DDV_read(Client.Random, 1, 1528, CONN_fd);
rec_cnt+=1537; rec_cnt+=1537;
/** Build S1 Packet **/ /** Build S1 Packet **/
Server.Time[0] = 0; Server.Time[1] = 0; Server.Time[2] = 0; Server.Time[3] = 0; Server.Time[0] = 0; Server.Time[1] = 0; Server.Time[2] = 0; Server.Time[3] = 0;
@ -27,25 +27,25 @@ bool doHandshake(){
Server.Random[i] = versionstring[i%13]; Server.Random[i] = versionstring[i%13];
} }
/** Send S0 **/ /** Send S0 **/
fwrite(&(Version), 1, 1, CONN); DDV_write(&(Version), 1, 1, CONN_fd);
/** Send S1 **/ /** Send S1 **/
fwrite(Server.Time, 1, 4, CONN); DDV_write(Server.Time, 1, 4, CONN_fd);
fwrite(Server.Zero, 1, 4, CONN); DDV_write(Server.Zero, 1, 4, CONN_fd);
fwrite(Server.Random, 1, 1528, CONN); DDV_write(Server.Random, 1, 1528, CONN_fd);
/** Flush output, just for certainty **/ /** Flush output, just for certainty **/
fflush(CONN); //fflush(CONN_fd);
snd_cnt+=1537; snd_cnt+=1537;
/** Send S2 **/ /** Send S2 **/
fwrite(Client.Time, 1, 4, CONN); DDV_write(Client.Time, 1, 4, CONN_fd);
fwrite(Client.Time, 1, 4, CONN); DDV_write(Client.Time, 1, 4, CONN_fd);
fwrite(Client.Random, 1, 1528, CONN); DDV_write(Client.Random, 1, 1528, CONN_fd);
snd_cnt+=1536; snd_cnt+=1536;
/** Flush, necessary in order to work **/ /** Flush, necessary in order to work **/
fflush(CONN); //fflush(CONN_fd);
/** Read and discard C2 **/ /** Read and discard C2 **/
fread(Client.Time, 1, 4, CONN); DDV_read(Client.Time, 1, 4, CONN_fd);
fread(Client.Zero, 1, 4, CONN); DDV_read(Client.Zero, 1, 4, CONN_fd);
fread(Client.Random, 1, 1528, CONN); DDV_read(Client.Random, 1, 1528, CONN_fd);
rec_cnt+=1536; rec_cnt+=1536;
return true; return true;
}//doHandshake }//doHandshake
@ -57,10 +57,10 @@ bool doHandshake(){
bool doHandshake(){ bool doHandshake(){
char Version; char Version;
/** Read C0 **/ /** Read C0 **/
fread(&Version, 1, 1, CONN); DDV_read(&Version, 1, 1, CONN_fd);
uint8_t Client[1536]; uint8_t Client[1536];
uint8_t Server[3072]; uint8_t Server[3072];
fread(&Client, 1, 1536, CONN); DDV_read(&Client, 1, 1536, CONN_fd);
rec_cnt+=1537; rec_cnt+=1537;
/** Build S1 Packet **/ /** Build S1 Packet **/
@ -123,13 +123,13 @@ bool doHandshake(){
delete[] pLastHash; delete[] pLastHash;
//***** DONE BUILDING THE RESPONSE ***// //***** DONE BUILDING THE RESPONSE ***//
/** Send response **/ /** Send response **/
fwrite(&Version, 1, 1, CONN); DDV_write(&Version, 1, 1, CONN_fd);
fwrite(&Server, 1, 3072, CONN); DDV_write(&Server, 1, 3072, CONN_fd);
snd_cnt+=3073; snd_cnt+=3073;
/** Flush, necessary in order to work **/ /** Flush, necessary in order to work **/
fflush(CONN); //fflush(CONN_fd);
/** Read and discard C2 **/ /** Read and discard C2 **/
fread(Client, 1, 1536, CONN); DDV_read(Client, 1, 1536, CONN_fd);
rec_cnt+=1536; rec_cnt+=1536;
return true; return true;
} }

View file

@ -17,7 +17,6 @@ bool stopparsing = false;
timeval lastrec; timeval lastrec;
int CONN_fd = 0; int CONN_fd = 0;
FILE * CONN = 0;
#include "parsechunks.cpp" //chunkstream parsing #include "parsechunks.cpp" //chunkstream parsing
#include "handshake.cpp" //handshaking #include "handshake.cpp" //handshaking
#include "../util/flv_sock.cpp" //FLV parsing with SocketW #include "../util/flv_sock.cpp" //FLV parsing with SocketW
@ -41,9 +40,6 @@ int main(){
return 0; return 0;
} }
CONN = fdopen(CONN_fd, "r+");
unsigned int ts; unsigned int ts;
unsigned int fts = 0; unsigned int fts = 0;
unsigned int ftst; unsigned int ftst;

View file

@ -34,3 +34,11 @@ int DDV_Listen(int port){
int DDV_Accept(int sock){ int DDV_Accept(int sock){
return accept(sock, 0, 0); return accept(sock, 0, 0);
} }
bool DDV_write(char * buffer, int width, int count, int sock){
return (send(sock, buffer, width*count, 0) == width*count);
}
bool DDV_read(char * buffer, int width, int count, int sock){
return (recv(sock, buffer, width*count, 0) == width*count);
}