static ossl_inline void packet_forward(PACKET *pkt, size_t len)
{
    pkt->curr += len;
    pkt->remaining -= len;
}
['int DTLSv1_listen(SSL *s, BIO_ADDR *client)\n{\n    int next, n, ret = 0, clearpkt = 0;\n    unsigned char cookie[DTLS1_COOKIE_LENGTH];\n    unsigned char seq[SEQ_NUM_SIZE];\n    const unsigned char *data;\n    unsigned char *buf;\n    size_t fragoff, fraglen, msglen;\n    unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;\n    BIO *rbio, *wbio;\n    BUF_MEM *bufm;\n    BIO_ADDR *tmpclient = NULL;\n    PACKET pkt, msgpkt, msgpayload, session, cookiepkt;\n    if (!SSL_clear(s))\n        return -1;\n    ERR_clear_error();\n    rbio = SSL_get_rbio(s);\n    wbio = SSL_get_wbio(s);\n    if (!rbio || !wbio) {\n        SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET);\n        return -1;\n    }\n    BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n    if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {\n        SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);\n        return -1;\n    }\n    if (s->init_buf == NULL) {\n        if ((bufm = BUF_MEM_new()) == NULL) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n            return -1;\n        }\n        if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) {\n            BUF_MEM_free(bufm);\n            SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n            return -1;\n        }\n        s->init_buf = bufm;\n    }\n    buf = (unsigned char *)s->init_buf->data;\n    do {\n        clear_sys_error();\n        n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n        if (n <= 0) {\n            if (BIO_should_retry(rbio)) {\n                goto end;\n            }\n            return -1;\n        }\n        clearpkt = 1;\n        if (!PACKET_buf_init(&pkt, buf, n)) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n            return -1;\n        }\n        if (n < DTLS1_RT_HEADER_LENGTH) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL);\n            goto end;\n        }\n        if (s->msg_callback)\n            s->msg_callback(0, 0, SSL3_RT_HEADER, buf,\n                            DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n        if (!PACKET_get_1(&pkt, &rectype)\n            || !PACKET_get_1(&pkt, &versmajor)) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n            goto end;\n        }\n        if (rectype != SSL3_RT_HANDSHAKE) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n            goto end;\n        }\n        if (versmajor != DTLS1_VERSION_MAJOR) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);\n            goto end;\n        }\n        if (!PACKET_forward(&pkt, 1)\n            || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)\n            || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n            goto end;\n        }\n        if (seq[0] != 0 || seq[1] != 0) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n            goto end;\n        }\n        data = PACKET_data(&msgpkt);\n        if (!PACKET_get_1(&msgpkt, &msgtype)\n            || !PACKET_get_net_3_len(&msgpkt, &msglen)\n            || !PACKET_get_net_2(&msgpkt, &msgseq)\n            || !PACKET_get_net_3_len(&msgpkt, &fragoff)\n            || !PACKET_get_net_3_len(&msgpkt, &fraglen)\n            || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)\n            || PACKET_remaining(&msgpkt) != 0) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n            goto end;\n        }\n        if (msgtype != SSL3_MT_CLIENT_HELLO) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n            goto end;\n        }\n        if (msgseq > 2) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);\n            goto end;\n        }\n        if (fragoff != 0 || fraglen > msglen) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);\n            goto end;\n        }\n        if (s->msg_callback)\n            s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,\n                            fraglen + DTLS1_HM_HEADER_LENGTH, s,\n                            s->msg_callback_arg);\n        if (!PACKET_get_net_2(&msgpayload, &clientvers)) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n            goto end;\n        }\n        if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&\n            s->method->version != DTLS_ANY_VERSION) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);\n            goto end;\n        }\n        if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)\n            || !PACKET_get_length_prefixed_1(&msgpayload, &session)\n            || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {\n            SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n            goto end;\n        }\n        if (PACKET_remaining(&cookiepkt) == 0) {\n            next = LISTEN_SEND_VERIFY_REQUEST;\n        } else {\n            if (s->ctx->app_verify_cookie_cb == NULL) {\n                SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);\n                return -1;\n            }\n            if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),\n                    (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {\n                next = LISTEN_SEND_VERIFY_REQUEST;\n            } else {\n                next = LISTEN_SUCCESS;\n            }\n        }\n        if (next == LISTEN_SEND_VERIFY_REQUEST) {\n            WPACKET wpkt;\n            unsigned int version;\n            size_t wreclen;\n            BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n            BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n            BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n            if (s->ctx->app_gen_cookie_cb == NULL ||\n                s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||\n                cookielen > 255) {\n                SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);\n                return -1;\n            }\n            version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION\n                                                               : s->version;\n            if (!WPACKET_init(&wpkt, s->init_buf)\n                    || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)\n                    || !WPACKET_put_bytes_u16(&wpkt, version)\n                    || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)\n                    || !WPACKET_start_sub_packet_u16(&wpkt)\n                    || !WPACKET_put_bytes_u8(&wpkt,\n                                             DTLS1_MT_HELLO_VERIFY_REQUEST)\n                    || !WPACKET_put_bytes_u24(&wpkt, 0)\n                    || !WPACKET_put_bytes_u16(&wpkt, 0)\n                    || !WPACKET_put_bytes_u24(&wpkt, 0)\n                    || !WPACKET_start_sub_packet_u24(&wpkt)\n                    || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)\n                    || !WPACKET_close(&wpkt)\n                    || !WPACKET_close(&wpkt)\n                    || !WPACKET_get_total_written(&wpkt, &wreclen)\n                    || !WPACKET_finish(&wpkt)) {\n                SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n                WPACKET_cleanup(&wpkt);\n                return -1;\n            }\n            memcpy(&buf[DTLS1_RT_HEADER_LENGTH + 1],\n                   &buf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],\n                   3);\n            if (s->msg_callback)\n                s->msg_callback(1, 0, SSL3_RT_HEADER, buf,\n                                DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n            if ((tmpclient = BIO_ADDR_new()) == NULL) {\n                SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n                goto end;\n            }\n            if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {\n                (void)BIO_dgram_set_peer(wbio, tmpclient);\n            }\n            BIO_ADDR_free(tmpclient);\n            tmpclient = NULL;\n            if (BIO_write(wbio, buf, wreclen) < (int)wreclen) {\n                if (BIO_should_retry(wbio)) {\n                    goto end;\n                }\n                return -1;\n            }\n            if (BIO_flush(wbio) <= 0) {\n                if (BIO_should_retry(wbio)) {\n                    goto end;\n                }\n                return -1;\n            }\n        }\n    } while (next != LISTEN_SUCCESS);\n    s->d1->handshake_read_seq = 1;\n    s->d1->handshake_write_seq = 1;\n    s->d1->next_handshake_write_seq = 1;\n    DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);\n    SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);\n    ossl_statem_set_hello_verify_done(s);\n    if (BIO_dgram_get_peer(rbio, client) <= 0)\n        BIO_ADDR_clear(client);\n    ret = 1;\n    clearpkt = 0;\n end:\n    BIO_ADDR_free(tmpclient);\n    BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n    if (clearpkt) {\n        BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n    }\n    return ret;\n}', 'static ossl_inline int PACKET_get_sub_packet(PACKET *pkt,\n                                                    PACKET *subpkt, size_t len)\n{\n    if (!PACKET_peek_sub_packet(pkt, subpkt, len))\n        return 0;\n    packet_forward(pkt, len);\n    return 1;\n}', 'static ossl_inline int PACKET_peek_sub_packet(const PACKET *pkt,\n                                                     PACKET *subpkt, size_t len)\n{\n    if (PACKET_remaining(pkt) < len)\n        return 0;\n    return PACKET_buf_init(subpkt, pkt->curr, len);\n}', 'static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)\n{\n    if (!PACKET_peek_net_2(pkt, data))\n        return 0;\n    packet_forward(pkt, 2);\n    return 1;\n}', 'static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,\n                                                unsigned int *data)\n{\n    if (PACKET_remaining(pkt) < 2)\n        return 0;\n    *data = ((unsigned int)(*pkt->curr)) << 8;\n    *data |= *(pkt->curr + 1);\n    return 1;\n}', 'static ossl_inline int PACKET_forward(PACKET *pkt, size_t len)\n{\n    if (PACKET_remaining(pkt) < len)\n        return 0;\n    packet_forward(pkt, len);\n    return 1;\n}', 'static ossl_inline void packet_forward(PACKET *pkt, size_t len)\n{\n    pkt->curr += len;\n    pkt->remaining -= len;\n}']