static int dtls_get_reassembled_message(SSL *s, int *errtype, size_t *len)
{
    unsigned char wire[DTLS1_HM_HEADER_LENGTH];
    size_t mlen, frag_off, frag_len;
    int i, ret, recvd_type;
    struct hm_header_st msg_hdr;
    size_t readbytes;
    *errtype = 0;
 redo:
    ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
    if (ret < 0) {
        return 0;
    }
    if (ret > 0) {
        s->init_num = frag_len;
        *len = frag_len;
        return 1;
    }
    i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,
                                  DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
    if (i <= 0) {
        s->rwstate = SSL_READING;
        *len = 0;
        return 0;
    }
    if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
        if (wire[0] != SSL3_MT_CCS) {
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
                     SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
                     SSL_R_BAD_CHANGE_CIPHER_SPEC);
            goto f_err;
        }
        memcpy(s->init_buf->data, wire, readbytes);
        s->init_num = readbytes - 1;
        s->init_msg = s->init_buf->data + 1;
        s->s3->tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
        s->s3->tmp.message_size = readbytes - 1;
        *len = readbytes - 1;
        return 1;
    }
    if (readbytes != DTLS1_HM_HEADER_LENGTH) {
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
                 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
        goto f_err;
    }
    dtls1_get_message_header(wire, &msg_hdr);
    mlen = msg_hdr.msg_len;
    frag_off = msg_hdr.frag_off;
    frag_len = msg_hdr.frag_len;
    if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
                 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);
        goto f_err;
    }
    if (msg_hdr.seq != s->d1->handshake_read_seq) {
        *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
        return 0;
    }
    if (frag_len && frag_len < mlen) {
        *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
        return 0;
    }
    if (!s->server
            && s->d1->r_msg_hdr.frag_off == 0
            && s->statem.hand_state != TLS_ST_OK
            && wire[0] == SSL3_MT_HELLO_REQUEST) {
        if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
            if (s->msg_callback)
                s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
                                wire, DTLS1_HM_HEADER_LENGTH, s,
                                s->msg_callback_arg);
            s->init_num = 0;
            goto redo;
        } else {
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
                     SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
                     SSL_R_UNEXPECTED_MESSAGE);
            goto f_err;
        }
    }
    if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
        goto f_err;
    }
    if (frag_len > 0) {
        unsigned char *p =
            (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
        i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
                                      &p[frag_off], frag_len, 0, &readbytes);
        if (i <= 0) {
            s->rwstate = SSL_READING;
            *len = 0;
            return 0;
        }
    } else {
        readbytes = 0;
    }
    if (readbytes != frag_len) {
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
                 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);
        goto f_err;
    }
    *len = s->init_num = frag_len;
    return 1;
 f_err:
    s->init_num = 0;
    *len = 0;
    return 0;
}
['static int dtls_get_reassembled_message(SSL *s, int *errtype, size_t *len)\n{\n    unsigned char wire[DTLS1_HM_HEADER_LENGTH];\n    size_t mlen, frag_off, frag_len;\n    int i, ret, recvd_type;\n    struct hm_header_st msg_hdr;\n    size_t readbytes;\n    *errtype = 0;\n redo:\n    ret = dtls1_retrieve_buffered_fragment(s, &frag_len);\n    if (ret < 0) {\n        return 0;\n    }\n    if (ret > 0) {\n        s->init_num = frag_len;\n        *len = frag_len;\n        return 1;\n    }\n    i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,\n                                  DTLS1_HM_HEADER_LENGTH, 0, &readbytes);\n    if (i <= 0) {\n        s->rwstate = SSL_READING;\n        *len = 0;\n        return 0;\n    }\n    if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {\n        if (wire[0] != SSL3_MT_CCS) {\n            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,\n                     SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,\n                     SSL_R_BAD_CHANGE_CIPHER_SPEC);\n            goto f_err;\n        }\n        memcpy(s->init_buf->data, wire, readbytes);\n        s->init_num = readbytes - 1;\n        s->init_msg = s->init_buf->data + 1;\n        s->s3->tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;\n        s->s3->tmp.message_size = readbytes - 1;\n        *len = readbytes - 1;\n        return 1;\n    }\n    if (readbytes != DTLS1_HM_HEADER_LENGTH) {\n        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,\n                 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);\n        goto f_err;\n    }\n    dtls1_get_message_header(wire, &msg_hdr);\n    mlen = msg_hdr.msg_len;\n    frag_off = msg_hdr.frag_off;\n    frag_len = msg_hdr.frag_len;\n    if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {\n        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,\n                 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);\n        goto f_err;\n    }\n    if (msg_hdr.seq != s->d1->handshake_read_seq) {\n        *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);\n        return 0;\n    }\n    if (frag_len && frag_len < mlen) {\n        *errtype = dtls1_reassemble_fragment(s, &msg_hdr);\n        return 0;\n    }\n    if (!s->server\n            && s->d1->r_msg_hdr.frag_off == 0\n            && s->statem.hand_state != TLS_ST_OK\n            && wire[0] == SSL3_MT_HELLO_REQUEST) {\n        if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {\n            if (s->msg_callback)\n                s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,\n                                wire, DTLS1_HM_HEADER_LENGTH, s,\n                                s->msg_callback_arg);\n            s->init_num = 0;\n            goto redo;\n        } else {\n            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,\n                     SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,\n                     SSL_R_UNEXPECTED_MESSAGE);\n            goto f_err;\n        }\n    }\n    if (!dtls1_preprocess_fragment(s, &msg_hdr)) {\n        goto f_err;\n    }\n    if (frag_len > 0) {\n        unsigned char *p =\n            (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;\n        i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,\n                                      &p[frag_off], frag_len, 0, &readbytes);\n        if (i <= 0) {\n            s->rwstate = SSL_READING;\n            *len = 0;\n            return 0;\n        }\n    } else {\n        readbytes = 0;\n    }\n    if (readbytes != frag_len) {\n        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,\n                 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);\n        goto f_err;\n    }\n    *len = s->init_num = frag_len;\n    return 1;\n f_err:\n    s->init_num = 0;\n    *len = 0;\n    return 0;\n}', 'static int dtls1_retrieve_buffered_fragment(SSL *s, size_t *len)\n{\n    pitem *item;\n    hm_fragment *frag;\n    int ret;\n    do {\n        item = pqueue_peek(s->d1->buffered_messages);\n        if (item == NULL)\n            return 0;\n        frag = (hm_fragment *)item->data;\n        if (frag->msg_header.seq < s->d1->handshake_read_seq) {\n            pqueue_pop(s->d1->buffered_messages);\n            dtls1_hm_fragment_free(frag);\n            pitem_free(item);\n            item = NULL;\n            frag = NULL;\n        }\n    } while (item == NULL);\n    if (frag->reassembly != NULL)\n        return 0;\n    if (s->d1->handshake_read_seq == frag->msg_header.seq) {\n        size_t frag_len = frag->msg_header.frag_len;\n        pqueue_pop(s->d1->buffered_messages);\n        ret = dtls1_preprocess_fragment(s, &frag->msg_header);\n        if (ret) {\n            unsigned char *p =\n                (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;\n            memcpy(&p[frag->msg_header.frag_off], frag->fragment,\n                   frag->msg_header.frag_len);\n        }\n        dtls1_hm_fragment_free(frag);\n        pitem_free(item);\n        if (ret) {\n            *len = frag_len;\n            return 1;\n        }\n        s->init_num = 0;\n        return -1;\n    } else {\n        return 0;\n    }\n}']