X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwebsock.c;h=0bbd56ac6471c3cb44de90593a029df04de589cf;hb=a63851bb4726c89d9a3c5755d78d1c4bbc3f3b2e;hp=9e79559b5a27ab450a1280fd1515bad1fff731a8;hpb=8dff3447cea6df8ab727d9615cd07b3774ccffa0;p=src%2Fapp-framework-binder.git diff --git a/src/websock.c b/src/websock.c index 9e79559b..0bbd56ac 100644 --- a/src/websock.c +++ b/src/websock.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 iot.bzh + * Copyright (C) 2016, 2017 "IoT.bzh" * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,7 +45,7 @@ #define FRAME_SET_RSV3(BYTE) (((BYTE) & 0x01) << 4) #define FRAME_SET_OPCODE(BYTE) ((BYTE) & 0x0F) #define FRAME_SET_MASK(BYTE) (((BYTE) & 0x01) << 7) -#define FRAME_SET_LENGTH(X64, IDX) (unsigned char)(((X64) >> ((IDX)*8)) & 0xFF) +#define FRAME_SET_LENGTH(X64, IDX) (unsigned char)((sizeof(X64)) <= (IDX) ? 0 : (((X64) >> ((IDX)*8)) & 0xFF)) #define OPCODE_CONTINUATION 0x0 #define OPCODE_TEXT 0x1 @@ -303,7 +303,7 @@ static int check_control_header(struct websock *ws) return 1; } -int websock_dispatch(struct websock *ws) +int websock_dispatch(struct websock *ws, int loop) { uint16_t code; loop: @@ -312,6 +312,7 @@ loop: ws->lenhead = 0; ws->szhead = 2; ws->state = STATE_START; + /*@fallthrough@*/ case STATE_START: /* read the header */ @@ -342,12 +343,15 @@ loop: switch (FRAME_GET_PAYLOAD_LEN(ws->header[1])) { case 127: ws->szhead += 6; + /*@fallthrough@*/ case 126: ws->szhead += 2; + /*@fallthrough@*/ default: ws->szhead += 4 * FRAME_GET_MASK(ws->header[1]); } ws->state = STATE_LENGTH; + /*@fallthrough@*/ case STATE_LENGTH: /* continue to read the header */ @@ -418,16 +422,22 @@ loop: ws->itf->on_continue(ws->closure, FRAME_GET_FIN(ws->header[0]), (size_t) ws->length); + if (!loop) + return 0; break; case OPCODE_TEXT: ws->itf->on_text(ws->closure, FRAME_GET_FIN(ws->header[0]), (size_t) ws->length); + if (!loop) + return 0; break; case OPCODE_BINARY: ws->itf->on_binary(ws->closure, FRAME_GET_FIN(ws->header[0]), (size_t) ws->length); + if (!loop) + return 0; break; case OPCODE_CLOSE: if (ws->length == 0) @@ -447,6 +457,8 @@ loop: websock_pong(ws, NULL, 0); } ws->state = STATE_INIT; + if (!loop) + return 0; break; case OPCODE_PONG: if (ws->itf->on_pong) @@ -454,6 +466,8 @@ loop: else websock_drop(ws); ws->state = STATE_INIT; + if (!loop) + return 0; break; default: goto protocol_error; @@ -537,7 +551,7 @@ int websock_drop(struct websock *ws) char buffer[8000]; while (ws->length) - if (ws_read(ws, buffer, sizeof buffer) < 0) + if (websock_read(ws, buffer, sizeof buffer) < 0) return -1; return 0; }