int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
                       size_t *written)
{
    int i;
    SSL3_BUFFER *wb = s->rlayer.wbuf;
    size_t currbuf = 0;
    size_t tmpwrit = 0;
    if ((s->rlayer.wpend_tot > len)
        || ((s->rlayer.wpend_buf != buf) &&
            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
        || (s->rlayer.wpend_type != type)) {
        SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
        return -1;
    }
    for (;;) {
        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
            && currbuf < s->rlayer.numwpipes - 1) {
            currbuf++;
            continue;
        }
        clear_sys_error();
        if (s->wbio != NULL) {
            s->rwstate = SSL_WRITING;
            i = BIO_write(s->wbio, (char *)
                          &(SSL3_BUFFER_get_buf(&wb[currbuf])
                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),
                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
            if (i >= 0)
                tmpwrit = i;
        } else {
            SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
            i = -1;
        }
        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
            SSL3_BUFFER_set_left(&wb[currbuf], 0);
            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
            if (currbuf + 1 < s->rlayer.numwpipes)
                continue;
            s->rwstate = SSL_NOTHING;
            *written = s->rlayer.wpend_ret;
            return 1;
        } else if (i <= 0) {
            if (SSL_IS_DTLS(s)) {
                SSL3_BUFFER_set_left(&wb[currbuf], 0);
            }
            return (i);
        }
        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);
    }
}
['int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,\n                     size_t len, int peek, size_t *readbytes)\n{\n    int al, i, j, iret;\n    size_t n;\n    SSL3_RECORD *rr;\n    void (*cb) (const SSL *ssl, int type2, int val) = NULL;\n    if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {\n        if (!ssl3_setup_buffers(s))\n            return (-1);\n    }\n    if ((type && (type != SSL3_RT_APPLICATION_DATA) &&\n         (type != SSL3_RT_HANDSHAKE)) ||\n        (peek && (type != SSL3_RT_APPLICATION_DATA))) {\n        SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n        return -1;\n    }\n    if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))\n    {\n        i = s->handshake_func(s);\n        if (i < 0)\n            return i;\n        if (i == 0) {\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n            return -1;\n        }\n    }\n start:\n    s->rwstate = SSL_NOTHING;\n    rr = s->rlayer.rrec;\n    if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {\n        pitem *item;\n        item = pqueue_pop(s->rlayer.d->buffered_app_data.q);\n        if (item) {\n#ifndef OPENSSL_NO_SCTP\n            if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n                DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;\n                BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,\n                         sizeof(rdata->recordinfo), &rdata->recordinfo);\n            }\n#endif\n            dtls1_copy_record(s, item);\n            OPENSSL_free(item->data);\n            pitem_free(item);\n        }\n    }\n    if (dtls1_handle_timeout(s) > 0)\n        goto start;\n    if ((SSL3_RECORD_get_length(rr) == 0)\n        || (s->rlayer.rstate == SSL_ST_READ_BODY)) {\n        iret = dtls1_get_record(s);\n        if (iret <= 0) {\n            iret = dtls1_read_failed(s, iret);\n            if (iret <= 0)\n                return iret;\n            else\n                goto start;\n        }\n    }\n    if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT\n            && SSL3_RECORD_get_length(rr) != 0)\n        s->rlayer.alert_count = 0;\n    if (s->s3->change_cipher_spec\n        && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {\n        if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),\n                                SSL3_RECORD_get_seq_num(rr)) < 0) {\n            SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n            return -1;\n        }\n        SSL3_RECORD_set_length(rr, 0);\n        goto start;\n    }\n    if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {\n        SSL3_RECORD_set_length(rr, 0);\n        s->rwstate = SSL_NOTHING;\n        return 0;\n    }\n    if (type == SSL3_RECORD_get_type(rr)\n        || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC\n            && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {\n        if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&\n            (s->enc_read_ctx == NULL)) {\n            al = SSL_AD_UNEXPECTED_MESSAGE;\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);\n            goto f_err;\n        }\n        if (recvd_type != NULL)\n            *recvd_type = SSL3_RECORD_get_type(rr);\n        if (len == 0)\n            return 0;\n        if (len > SSL3_RECORD_get_length(rr))\n            n = SSL3_RECORD_get_length(rr);\n        else\n            n = len;\n        memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);\n        if (!peek) {\n            SSL3_RECORD_sub_length(rr, n);\n            SSL3_RECORD_add_off(rr, n);\n            if (SSL3_RECORD_get_length(rr) == 0) {\n                s->rlayer.rstate = SSL_ST_READ_HEADER;\n                SSL3_RECORD_set_off(rr, 0);\n            }\n        }\n#ifndef OPENSSL_NO_SCTP\n        if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n            s->d1->shutdown_received\n            && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n            s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n            return 0;\n        }\n#endif\n        *readbytes = n;\n        return 1;\n    }\n    if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {\n        unsigned int alert_level, alert_descr;\n        unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)\n                                     + SSL3_RECORD_get_off(rr);\n        PACKET alert;\n        if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))\n                || !PACKET_get_1(&alert, &alert_level)\n                || !PACKET_get_1(&alert, &alert_descr)\n                || PACKET_remaining(&alert) != 0) {\n            al = SSL_AD_UNEXPECTED_MESSAGE;\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_INVALID_ALERT);\n            goto f_err;\n        }\n        if (s->msg_callback)\n            s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,\n                            s->msg_callback_arg);\n        if (s->info_callback != NULL)\n            cb = s->info_callback;\n        else if (s->ctx->info_callback != NULL)\n            cb = s->ctx->info_callback;\n        if (cb != NULL) {\n            j = (alert_level << 8) | alert_descr;\n            cb(s, SSL_CB_READ_ALERT, j);\n        }\n        if (alert_level == SSL3_AL_WARNING) {\n            s->s3->warn_alert = alert_descr;\n            s->rlayer.alert_count++;\n            if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {\n                al = SSL_AD_UNEXPECTED_MESSAGE;\n                SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);\n                goto f_err;\n            }\n            if (alert_descr == SSL_AD_CLOSE_NOTIFY) {\n#ifndef OPENSSL_NO_SCTP\n                if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&\n                    BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {\n                    s->d1->shutdown_received = 1;\n                    s->rwstate = SSL_READING;\n                    BIO_clear_retry_flags(SSL_get_rbio(s));\n                    BIO_set_retry_read(SSL_get_rbio(s));\n                    return -1;\n                }\n#endif\n                s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n                return 0;\n            }\n        } else if (alert_level == SSL3_AL_FATAL) {\n            char tmp[16];\n            s->rwstate = SSL_NOTHING;\n            s->s3->fatal_alert = alert_descr;\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);\n            BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);\n            ERR_add_error_data(2, "SSL alert number ", tmp);\n            s->shutdown |= SSL_RECEIVED_SHUTDOWN;\n            SSL_CTX_remove_session(s->session_ctx, s->session);\n            return 0;\n        } else {\n            al = SSL_AD_ILLEGAL_PARAMETER;\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);\n            goto f_err;\n        }\n        goto start;\n    }\n    if (s->shutdown & SSL_SENT_SHUTDOWN) {\n        s->rwstate = SSL_NOTHING;\n        SSL3_RECORD_set_length(rr, 0);\n        return 0;\n    }\n    if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {\n        SSL3_RECORD_set_length(rr, 0);\n        goto start;\n    }\n    if ((SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) &&\n            !ossl_statem_get_in_handshake(s)) {\n        struct hm_header_st msg_hdr;\n        if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch\n                || SSL3_RECORD_get_length(rr) < DTLS1_HM_HEADER_LENGTH) {\n            SSL3_RECORD_set_length(rr, 0);\n            goto start;\n        }\n        dtls1_get_message_header(rr->data, &msg_hdr);\n        if (msg_hdr.type == SSL3_MT_FINISHED) {\n            if (dtls1_check_timeout_num(s) < 0)\n                return -1;\n            dtls1_retransmit_buffered_messages(s);\n            SSL3_RECORD_set_length(rr, 0);\n            goto start;\n        }\n        if (!ossl_assert(SSL_is_init_finished(s))) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        ossl_statem_set_in_init(s, 1);\n        i = s->handshake_func(s);\n        if (i < 0)\n            return i;\n        if (i == 0) {\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);\n            return -1;\n        }\n        if (!(s->mode & SSL_MODE_AUTO_RETRY)) {\n            if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {\n                BIO *bio;\n                s->rwstate = SSL_READING;\n                bio = SSL_get_rbio(s);\n                BIO_clear_retry_flags(bio);\n                BIO_set_retry_read(bio);\n                return -1;\n            }\n        }\n        goto start;\n    }\n    switch (SSL3_RECORD_get_type(rr)) {\n    default:\n        al = SSL_AD_UNEXPECTED_MESSAGE;\n        SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n        goto f_err;\n    case SSL3_RT_CHANGE_CIPHER_SPEC:\n    case SSL3_RT_ALERT:\n    case SSL3_RT_HANDSHAKE:\n        al = SSL_AD_UNEXPECTED_MESSAGE;\n        SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);\n        goto f_err;\n    case SSL3_RT_APPLICATION_DATA:\n        if (s->s3->in_read_app_data &&\n            (s->s3->total_renegotiations != 0) &&\n            ossl_statem_app_data_allowed(s)) {\n            s->s3->in_read_app_data = 2;\n            return -1;\n        } else {\n            al = SSL_AD_UNEXPECTED_MESSAGE;\n            SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);\n            goto f_err;\n        }\n    }\n f_err:\n    ssl3_send_alert(s, SSL3_AL_FATAL, al);\n    return -1;\n}', 'int dtls1_handle_timeout(SSL *s)\n{\n    if (!dtls1_is_timer_expired(s)) {\n        return 0;\n    }\n    dtls1_double_timeout(s);\n    if (dtls1_check_timeout_num(s) < 0)\n        return -1;\n    s->d1->timeout.read_timeouts++;\n    if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {\n        s->d1->timeout.read_timeouts = 1;\n    }\n    dtls1_start_timer(s);\n    return dtls1_retransmit_buffered_messages(s);\n}', 'int dtls1_get_record(SSL *s)\n{\n    int ssl_major, ssl_minor;\n    int rret;\n    size_t more, n;\n    SSL3_RECORD *rr;\n    unsigned char *p = NULL;\n    unsigned short version;\n    DTLS1_BITMAP *bitmap;\n    unsigned int is_next_epoch;\n    rr = RECORD_LAYER_get_rrec(&s->rlayer);\n again:\n    if (!dtls1_process_buffered_records(s))\n        return -1;\n    if (dtls1_get_processed_record(s))\n        return 1;\n    if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||\n        (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {\n        rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,\n                           SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);\n        if (rret <= 0)\n            return rret;\n        if (RECORD_LAYER_get_packet_length(&s->rlayer) !=\n            DTLS1_RT_HEADER_LENGTH) {\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n        RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);\n        p = RECORD_LAYER_get_packet(&s->rlayer);\n        if (s->msg_callback)\n            s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,\n                            s, s->msg_callback_arg);\n        rr->type = *(p++);\n        ssl_major = *(p++);\n        ssl_minor = *(p++);\n        version = (ssl_major << 8) | ssl_minor;\n        n2s(p, rr->epoch);\n        memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);\n        p += 6;\n        n2s(p, rr->length);\n        if (!s->first_packet) {\n            if (version != s->version) {\n                rr->length = 0;\n                RECORD_LAYER_reset_packet_length(&s->rlayer);\n                goto again;\n            }\n        }\n        if ((version & 0xff00) != (s->version & 0xff00)) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n        if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n    }\n    if (rr->length >\n        RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {\n        more = rr->length;\n        rret = ssl3_read_n(s, more, more, 1, 1, &n);\n        if (rret <= 0 || n != more) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n    }\n    RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);\n    bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n    if (bitmap == NULL) {\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto again;\n    }\n#ifndef OPENSSL_NO_SCTP\n    if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n#endif\n        if (!dtls1_record_replay_check(s, bitmap)) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n#ifndef OPENSSL_NO_SCTP\n    }\n#endif\n    if (rr->length == 0)\n        goto again;\n    if (is_next_epoch) {\n        if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {\n            if (dtls1_buffer_record\n                (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),\n                 rr->seq_num) < 0)\n                return -1;\n        }\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto again;\n    }\n    if (!dtls1_process_record(s, bitmap)) {\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto again;\n    }\n    return (1);\n}', 'int dtls1_process_buffered_records(SSL *s)\n{\n    pitem *item;\n    SSL3_BUFFER *rb;\n    SSL3_RECORD *rr;\n    DTLS1_BITMAP *bitmap;\n    unsigned int is_next_epoch;\n    int replayok = 1;\n    item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);\n    if (item) {\n        if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)\n            return 1;\n        rr = RECORD_LAYER_get_rrec(&s->rlayer);\n        rb = RECORD_LAYER_get_rbuf(&s->rlayer);\n        if (SSL3_BUFFER_get_left(rb) > 0) {\n            return 1;\n        }\n        while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {\n            dtls1_get_unprocessed_record(s);\n            bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n            if (bitmap == NULL) {\n                 SSLerr(SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,\n                        ERR_R_INTERNAL_ERROR);\n                 return 0;\n            }\n#ifndef OPENSSL_NO_SCTP\n            if (!BIO_dgram_is_sctp(SSL_get_rbio(s)))\n#endif\n            {\n                replayok = dtls1_record_replay_check(s, bitmap);\n            }\n            if (!replayok || !dtls1_process_record(s, bitmap)) {\n                rr->length = 0;\n                RECORD_LAYER_reset_packet_length(&s->rlayer);\n                continue;\n            }\n            if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),\n                    SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0)\n                return 0;\n        }\n    }\n    s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;\n    s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;\n    return 1;\n}', 'int dtls1_read_failed(SSL *s, int code)\n{\n    if (code > 0) {\n        SSLerr(SSL_F_DTLS1_READ_FAILED, ERR_R_INTERNAL_ERROR);\n        return 1;\n    }\n    if (!dtls1_is_timer_expired(s)) {\n        return code;\n    }\n    if (!SSL_in_init(s))\n    {\n        BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);\n        return code;\n    }\n    return dtls1_handle_timeout(s);\n}', 'int dtls1_retransmit_buffered_messages(SSL *s)\n{\n    pqueue *sent = s->d1->sent_messages;\n    piterator iter;\n    pitem *item;\n    hm_fragment *frag;\n    int found = 0;\n    iter = pqueue_iterator(sent);\n    for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {\n        frag = (hm_fragment *)item->data;\n        if (dtls1_retransmit_message(s, (unsigned short)\n                                     dtls1_get_queue_priority\n                                     (frag->msg_header.seq,\n                                      frag->msg_header.is_ccs), &found) <= 0)\n            return -1;\n    }\n    return 1;\n}', 'int dtls1_retransmit_message(SSL *s, unsigned short seq, int *found)\n{\n    int ret;\n    pitem *item;\n    hm_fragment *frag;\n    unsigned long header_length;\n    unsigned char seq64be[8];\n    struct dtls1_retransmit_state saved_state;\n    memset(seq64be, 0, sizeof(seq64be));\n    seq64be[6] = (unsigned char)(seq >> 8);\n    seq64be[7] = (unsigned char)seq;\n    item = pqueue_find(s->d1->sent_messages, seq64be);\n    if (item == NULL) {\n        SSLerr(SSL_F_DTLS1_RETRANSMIT_MESSAGE, ERR_R_INTERNAL_ERROR);\n        *found = 0;\n        return 0;\n    }\n    *found = 1;\n    frag = (hm_fragment *)item->data;\n    if (frag->msg_header.is_ccs)\n        header_length = DTLS1_CCS_HEADER_LENGTH;\n    else\n        header_length = DTLS1_HM_HEADER_LENGTH;\n    memcpy(s->init_buf->data, frag->fragment,\n           frag->msg_header.msg_len + header_length);\n    s->init_num = frag->msg_header.msg_len + header_length;\n    dtls1_set_message_header_int(s, frag->msg_header.type,\n                                 frag->msg_header.msg_len,\n                                 frag->msg_header.seq, 0,\n                                 frag->msg_header.frag_len);\n    saved_state.enc_write_ctx = s->enc_write_ctx;\n    saved_state.write_hash = s->write_hash;\n    saved_state.compress = s->compress;\n    saved_state.session = s->session;\n    saved_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);\n    s->d1->retransmitting = 1;\n    s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;\n    s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;\n    s->compress = frag->msg_header.saved_retransmit_state.compress;\n    s->session = frag->msg_header.saved_retransmit_state.session;\n    DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer,\n                                        frag->msg_header.\n                                        saved_retransmit_state.epoch);\n    ret = dtls1_do_write(s, frag->msg_header.is_ccs ?\n                         SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);\n    s->enc_write_ctx = saved_state.enc_write_ctx;\n    s->write_hash = saved_state.write_hash;\n    s->compress = saved_state.compress;\n    s->session = saved_state.session;\n    DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer, saved_state.epoch);\n    s->d1->retransmitting = 0;\n    (void)BIO_flush(s->wbio);\n    return ret;\n}', 'int dtls1_do_write(SSL *s, int type)\n{\n    int ret;\n    size_t written;\n    size_t curr_mtu;\n    int retry = 1;\n    size_t len, frag_off, mac_size, blocksize, used_len;\n    if (!dtls1_query_mtu(s))\n        return -1;\n    if (s->d1->mtu < dtls1_min_mtu(s))\n        return -1;\n    if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {\n        if (!ossl_assert(s->init_num ==\n                         s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))\n            return -1;\n    }\n    if (s->write_hash) {\n        if (s->enc_write_ctx\n            && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &\n                EVP_CIPH_FLAG_AEAD_CIPHER) != 0)\n            mac_size = 0;\n        else\n            mac_size = EVP_MD_CTX_size(s->write_hash);\n    } else\n        mac_size = 0;\n    if (s->enc_write_ctx &&\n        (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))\n        blocksize = 2 * EVP_CIPHER_CTX_block_size(s->enc_write_ctx);\n    else\n        blocksize = 0;\n    frag_off = 0;\n    s->rwstate = SSL_NOTHING;\n    while (s->init_num > 0) {\n        if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {\n            if (frag_off > 0) {\n                if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {\n                    return -1;\n                }\n                s->init_off -= DTLS1_HM_HEADER_LENGTH;\n                s->init_num += DTLS1_HM_HEADER_LENGTH;\n            } else {\n                frag_off = s->d1->w_msg_hdr.frag_off;\n            }\n        }\n        used_len = BIO_wpending(s->wbio) + DTLS1_RT_HEADER_LENGTH\n            + mac_size + blocksize;\n        if (s->d1->mtu > used_len)\n            curr_mtu = s->d1->mtu - used_len;\n        else\n            curr_mtu = 0;\n        if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {\n            ret = BIO_flush(s->wbio);\n            if (ret <= 0) {\n                s->rwstate = SSL_WRITING;\n                return ret;\n            }\n            used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;\n            if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {\n                curr_mtu = s->d1->mtu - used_len;\n            } else {\n                return -1;\n            }\n        }\n        if (((unsigned int)s->init_num) > curr_mtu)\n            len = curr_mtu;\n        else\n            len = s->init_num;\n        if (len > s->max_send_fragment)\n            len = s->max_send_fragment;\n        if (type == SSL3_RT_HANDSHAKE) {\n            if (len < DTLS1_HM_HEADER_LENGTH) {\n                return -1;\n            }\n            dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);\n            dtls1_write_message_header(s,\n                                       (unsigned char *)&s->init_buf->\n                                       data[s->init_off]);\n        }\n        ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,\n                                &written);\n        if (ret < 0) {\n            if (retry && BIO_ctrl(SSL_get_wbio(s),\n                                  BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {\n                if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {\n                    if (!dtls1_query_mtu(s))\n                        return -1;\n                    retry = 0;\n                } else\n                    return -1;\n            } else {\n                return -1;\n            }\n        } else {\n            if (!ossl_assert(len == written))\n                return -1;\n            if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {\n                unsigned char *p =\n                    (unsigned char *)&s->init_buf->data[s->init_off];\n                const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;\n                size_t xlen;\n                if (frag_off == 0 && s->version != DTLS1_BAD_VER) {\n                    *p++ = msg_hdr->type;\n                    l2n3(msg_hdr->msg_len, p);\n                    s2n(msg_hdr->seq, p);\n                    l2n3(0, p);\n                    l2n3(msg_hdr->msg_len, p);\n                    p -= DTLS1_HM_HEADER_LENGTH;\n                    xlen = written;\n                } else {\n                    p += DTLS1_HM_HEADER_LENGTH;\n                    xlen = written - DTLS1_HM_HEADER_LENGTH;\n                }\n                if (!ssl3_finish_mac(s, p, xlen))\n                    return -1;\n            }\n            if (written == s->init_num) {\n                if (s->msg_callback)\n                    s->msg_callback(1, s->version, type, s->init_buf->data,\n                                    (size_t)(s->init_off + s->init_num), s,\n                                    s->msg_callback_arg);\n                s->init_off = 0;\n                s->init_num = 0;\n                return 1;\n            }\n            s->init_off += written;\n            s->init_num -= written;\n            written -= DTLS1_HM_HEADER_LENGTH;\n            frag_off += written;\n            dtls1_fix_message_header(s, frag_off, 0);\n        }\n    }\n    return 0;\n}', 'int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,\n                      size_t *written)\n{\n    int i;\n    if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {\n        SSLerr(SSL_F_DTLS1_WRITE_BYTES, ERR_R_INTERNAL_ERROR);\n        return -1;\n    }\n    s->rwstate = SSL_NOTHING;\n    i = do_dtls1_write(s, type, buf, len, 0, written);\n    return i;\n}', 'int do_dtls1_write(SSL *s, int type, const unsigned char *buf,\n                   size_t len, int create_empty_fragment, size_t *written)\n{\n    unsigned char *p, *pseq;\n    int i, mac_size, clear = 0;\n    size_t prefix_len = 0;\n    int eivlen;\n    SSL3_RECORD wr;\n    SSL3_BUFFER *wb;\n    SSL_SESSION *sess;\n    wb = &s->rlayer.wbuf[0];\n    if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {\n        SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);\n        return 0;\n    }\n    if (s->s3->alert_dispatch) {\n        i = s->method->ssl_dispatch_alert(s);\n        if (i <= 0)\n            return i;\n    }\n    if (len == 0 && !create_empty_fragment)\n        return 0;\n    if (len > s->max_send_fragment) {\n        SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);\n        return 0;\n    }\n    sess = s->session;\n    if ((sess == NULL) ||\n        (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))\n        clear = 1;\n    if (clear)\n        mac_size = 0;\n    else {\n        mac_size = EVP_MD_CTX_size(s->write_hash);\n        if (mac_size < 0)\n            goto err;\n    }\n    p = SSL3_BUFFER_get_buf(wb) + prefix_len;\n    *(p++) = type & 0xff;\n    SSL3_RECORD_set_type(&wr, type);\n    if (s->method->version == DTLS_ANY_VERSION &&\n        s->max_proto_version != DTLS1_BAD_VER) {\n        *(p++) = DTLS1_VERSION >> 8;\n        *(p++) = DTLS1_VERSION & 0xff;\n    } else {\n        *(p++) = s->version >> 8;\n        *(p++) = s->version & 0xff;\n    }\n    pseq = p;\n    p += 10;\n    if (s->enc_write_ctx) {\n        int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);\n        if (mode == EVP_CIPH_CBC_MODE) {\n            eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);\n            if (eivlen <= 1)\n                eivlen = 0;\n        }\n        else if (mode == EVP_CIPH_GCM_MODE)\n            eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;\n        else if (mode == EVP_CIPH_CCM_MODE)\n            eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;\n        else\n            eivlen = 0;\n    } else\n        eivlen = 0;\n    SSL3_RECORD_set_data(&wr, p + eivlen);\n    SSL3_RECORD_set_length(&wr, len);\n    SSL3_RECORD_set_input(&wr, (unsigned char *)buf);\n    if (s->compress != NULL) {\n        if (!ssl3_do_compress(s, &wr)) {\n            SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);\n            goto err;\n        }\n    } else {\n        memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),\n               SSL3_RECORD_get_length(&wr));\n        SSL3_RECORD_reset_input(&wr);\n    }\n    if (!SSL_WRITE_ETM(s) && mac_size != 0) {\n        if (!s->method->ssl3_enc->mac(s, &wr,\n                                      &(p[SSL3_RECORD_get_length(&wr) + eivlen]),\n                                      1))\n            goto err;\n        SSL3_RECORD_add_length(&wr, mac_size);\n    }\n    SSL3_RECORD_set_data(&wr, p);\n    SSL3_RECORD_reset_input(&wr);\n    if (eivlen)\n        SSL3_RECORD_add_length(&wr, eivlen);\n    if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)\n        goto err;\n    if (SSL_WRITE_ETM(s) && mac_size != 0) {\n        if (!s->method->ssl3_enc->mac(s, &wr,\n                                      &(p[SSL3_RECORD_get_length(&wr)]), 1))\n            goto err;\n        SSL3_RECORD_add_length(&wr, mac_size);\n    }\n    s2n(s->rlayer.d->w_epoch, pseq);\n    memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);\n    pseq += 6;\n    s2n(SSL3_RECORD_get_length(&wr), pseq);\n    if (s->msg_callback)\n        s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,\n                        DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n    SSL3_RECORD_set_type(&wr, type);\n    SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);\n    ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));\n    if (create_empty_fragment) {\n        *written = wr.length;\n        return 1;\n    }\n    SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));\n    SSL3_BUFFER_set_offset(wb, 0);\n    s->rlayer.wpend_tot = len;\n    s->rlayer.wpend_buf = buf;\n    s->rlayer.wpend_type = type;\n    s->rlayer.wpend_ret = len;\n    return ssl3_write_pending(s, type, buf, len, written);\n err:\n    return -1;\n}', 'int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,\n                       size_t *written)\n{\n    int i;\n    SSL3_BUFFER *wb = s->rlayer.wbuf;\n    size_t currbuf = 0;\n    size_t tmpwrit = 0;\n    if ((s->rlayer.wpend_tot > len)\n        || ((s->rlayer.wpend_buf != buf) &&\n            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))\n        || (s->rlayer.wpend_type != type)) {\n        SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);\n        return -1;\n    }\n    for (;;) {\n        if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0\n            && currbuf < s->rlayer.numwpipes - 1) {\n            currbuf++;\n            continue;\n        }\n        clear_sys_error();\n        if (s->wbio != NULL) {\n            s->rwstate = SSL_WRITING;\n            i = BIO_write(s->wbio, (char *)\n                          &(SSL3_BUFFER_get_buf(&wb[currbuf])\n                            [SSL3_BUFFER_get_offset(&wb[currbuf])]),\n                          (unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));\n            if (i >= 0)\n                tmpwrit = i;\n        } else {\n            SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);\n            i = -1;\n        }\n        if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {\n            SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n            if (currbuf + 1 < s->rlayer.numwpipes)\n                continue;\n            s->rwstate = SSL_NOTHING;\n            *written = s->rlayer.wpend_ret;\n            return 1;\n        } else if (i <= 0) {\n            if (SSL_IS_DTLS(s)) {\n                SSL3_BUFFER_set_left(&wb[currbuf], 0);\n            }\n            return (i);\n        }\n        SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);\n        SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);\n    }\n}']