简体   繁体   中英

openssl ssl_read sends read binary data into char*

i am trying to write Apple Push Notification Server provider (APNS provider) in c, using openssl.

So far i have been able to succesfuly send a notification via ssl_write, however sometimes it can happen, that the message i sent gets rejected (for whatever reason, bad token, etc...). When that happens and i try to ssl_write, i get -1 bytes written and APNS is then supposed to send back error message and close the connection.

The error comes in as binary and consists of 6 bytes as described in apple documentation here: Apple Documenatation

const int written= SSL_write(this->_ssl, buffer, bufferSize);   

int want =SSL_want(this->_ssl);

if(want == 2 ){ //2 means that ssl wants to read 
char bufferRead[6];        
SSL_read(this->_ssl,bufferRead,6);

//Some code that transfers bufferRead into a ASCII format

}

I want to ask you, how do i convert this binary bufferRead into something that i can read and store in lets say char* or std::string. The output when converted should look something like this "881234" ...
I appreciate any help i can get.

EDIT solution by Eran:

   unsigned char command = bufferRead[0]; 
   unsigned char status = bufferRead[1]; 
   unsigned char c2 = bufferRead[2];
   unsigned char c3 = bufferRead[3];
   unsigned char c4 = bufferRead[4];
   unsigned char c5 = bufferRead[5];

int comInt = command;
int statusInt = status;

 int id = (c5 << 24) +
          (c4 << 16) +
          (c3 << 8) +
          (c2);

You should parse the buffer into 3 parameters. It has been a while since I programmed in c, but I think you need something like this:

char command = bufferRead[0]; // should contain 8
char status  = bufferRead[1]; // the type of the error - the most common being 8 (InvalidToken)
int id = (bufferRead[2] << 24) + 
         (bufferRead[3] << 16) + 
         (bufferRead[4] << 8) + 
         (bufferRead[5]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM