/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef H3_SERVER_H
#define H3_SERVER_H

#include <httpd.h>

#include <apr_pools.h>

#include "h3.h"
#include "h3_config.h"

/**
 * ap_child_init hook: bring up the QUIC listener in the first available
 * child process. Idempotent across children: subsequent children detect the
 * port is already owned and skip socket creation.
 * @param pchild The child process pool.
 * @param s      The server_rec for the listener's vhost.
 */
void h3_child_init(apr_pool_t* pchild, server_rec* s);

/**
 * Bind the QUIC socket in the parent while it is still privileged (Linux), and
 * keep it across graceful restarts for the children to inherit.
 * @param s Main server.
 */
void h3_server_post_config(server_rec* s);

/**
 * The QUIC token key. The parent makes it once and keeps it across restarts,
 * so a token from one child is good in every other child.
 * @return 32 bytes, or NULL if no key could be made.
 */
const unsigned char* h3_server_secret(server_rec* s);

/**
 * @return The number of QUIC sockets the parent holds (Linux), at least 1.
 */
int h3_server_buckets(server_rec* s);

/**
 * @return The bucket index of this child when an eBPF program routes the
 *         QUIC packets by connection ID, else -1.
 */
int h3_server_route_bucket(server_rec* s);

/**
 * Child-stopping hook: stop retrying for the QUIC port, then either drain the
 * live connections (graceful) or tear the listener down at once (immediate).
 * No-op if the child never owned the listener.
 * @param pool     The pool used for any teardown allocations.
 * @param graceful Non-zero for a graceful stop, zero for immediate.
 */
void h3_c1_child_stopping(apr_pool_t* pool, int graceful);

#if H3_DEVEL

/**
 * Child-stopped hook: tear down the QUIC listener, join worker threads, and
 * release the SSL context. Runs once the MPM has finished waiting for the
 * connections we noted, so anything still alive here gets cut short.
 * No-op if the child never owned the listener or already tore it down.
 * @param pool     The pool used for any teardown allocations.
 * @param graceful Non-zero for a graceful stop, zero for immediate.
 */
void h3_c1_child_stopped(apr_pool_t* pool, int graceful);

#endif

#endif /* H3_SERVER_H */
