Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket_remove_frame() ToDo: Frame >= 32768 i.e len ==127 #47

Open
farizadp opened this issue Nov 30, 2018 · 1 comment
Open

websocket_remove_frame() ToDo: Frame >= 32768 i.e len ==127 #47

farizadp opened this issue Nov 30, 2018 · 1 comment

Comments

@farizadp
Copy link

farizadp commented Nov 30, 2018

@pcbreflux
Any idea how this ToDo should be implemented? And what is the reason behind this ToDo ?

int websocket_remove_frame(Network *n, unsigned char *framebuffer,
                           unsigned char *data, int framlen) {

  int bytepos = 0;
  int opcode;
  uint8_t mask_flag = 1;
  int mask_key[4] = {0x00, 0x00, 0x00, 0x00};
  int len;

  opcode = framebuffer[bytepos] & 0x0F;
  bytepos++;
  len = framebuffer[bytepos] & 0x7F;
  mask_flag = framebuffer[bytepos] >> 7;
  bytepos++;
  if (len == 126) {
    len = framebuffer[bytepos] << 8;
    bytepos++;
    len += framebuffer[bytepos];
    bytepos++;
  }
  if (len == 127) {
    len = 0; // ToDo : Frame >= 32768 i.e. len == 127
    goto exit;
  }

  if (len > framlen) {
    len = framlen;
  }
  if (mask_flag == 1) {
    mask_key[0] = framebuffer[bytepos];
    bytepos++;
    mask_key[1] = framebuffer[bytepos];
    bytepos++;
    mask_key[2] = framebuffer[bytepos];
    bytepos++;
    mask_key[3] = framebuffer[bytepos];
    bytepos++;
  }
  for (int i = 0; i < len; i++) {
    if (mask_flag == 1) {
      data[i] = framebuffer[bytepos + i] ^
                mask_key[i % 4]; // Bitwise XOR at index modulo 4
    } else {
      data[i] = framebuffer[bytepos + i];
    }
  }
  if (opcode == WEBSOCKET_CONNCLOSE || opcode == WEBSOCKET_PING) {
    if (opcode == WEBSOCKET_PING) {
      opcode = WEBSOCKET_PONG;
    }
    ESP_LOGD(TAG, "opcode %d websocket_mbedtls_write mqtt want %d", opcode,
             len);
    int framelen = websocket_create_frame(opcode, n->ws_sendbuf, data, len, 0);

    for (int i = 0; i < framelen; i++) {
      ESP_LOGD(TAG, "opcode websocket_mbedtls_write: %d %02X [%c]", i + 1,
               n->ws_sendbuf[i], n->ws_sendbuf[i]);
    }
    int ret = mbedtls_ssl_write(&n->ssl, n->ws_sendbuf, framelen);
    ESP_LOGD(TAG, "opcode mbedtls_ssl_write websocket %d from %d", ret, len);
  }

exit:
  return len;
}

I am running into an issue where my application flow goes to this ToDo. And this causes issue

@farizadp
Copy link
Author

can anyone check my pull request to fix this?
#48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant