无法在C中取消屏蔽Websocket数据

时间:2016-01-25 15:53:51

标签: c websocket

我正在开发一个Websocket服务器。我有一个函数来处理websocket数据的取消屏蔽。

我有一些调试代码处于活动状态。通过套接字发送“?jog,0,10,10 \ n”时调试代码的输出是:

databefore:▒▒cy▒▒\ðOI▒▒SU▒▒i
dataafter:▒{U

代码如下:

void sock_unmasking_data(struct sock_data *sockdat, int dataCount) {
    unsigned int ix;
    unsigned char mask[4];
    int rc;
    log_printf(3,"databefore: %s",sockdat->buffer);
    dataCount = ((unsigned char) sockdat->buffer[1]) & 0x7f;
    mask[0] = sockdat->buffer[2];
    mask[1] = sockdat->buffer[3];
    mask[2] = sockdat->buffer[4];
    mask[3] = sockdat->buffer[5];
    if(dataCount <= 126) { // Unmask the payload for a 126 byte message or smaller                  
        for (ix = 0; ix < dataCount; ix++) {
            sockdat->buffer[6 + ix] ^= mask[ix % 4];
        }
        rc = asprintf(&sockdat->dataUnmasked, "%.*s", dataCount, sockdat->buffer + 6); 
    } else if(dataCount == 127) { // Unmask the payload for a 127 byte message or larger
        for (ix = 0; ix < dataCount; ix++) {
            sockdat->buffer[8 + ix] ^= mask[ix % 4];
        }
        rc = asprintf(&sockdat->dataUnmasked, "%.*s", dataCount, sockdat->buffer + 8);
    }
    log_printf(3,"dataafter: %s",sockdat->dataUnmasked);
}

数据全部在全局结构中处理。 dataUnmasked是此函数中的数据输出,&amp; sockdat-&gt;缓冲区是输入。

此功能的输出应为:

databefore:▒▒cy▒▒\ðOI▒▒SU▒▒i
dataafter:?jog,0,10,10 \ n

非常欢迎任何帮助。

0 个答案:

没有答案