blob: 9382b3935ab2ae98a6de61bdc688e9b5a8a9a282 [file] [log] [blame]
Luke Huangf71a7222021-03-17 14:27:28 +08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// This file is autogenerated by:
18// cbindgen --config cbindgen.toml doh.rs >doh.h
19// Don't modify manually.
20
21#pragma once
22
Luke Huang10eed9d2021-09-29 12:11:30 +080023/* Generated with cbindgen:0.20.0 */
Luke Huangf71a7222021-03-17 14:27:28 +080024
25#include <stdint.h>
26#include <sys/types.h>
27
Luke Huangfe453402021-06-24 03:15:03 +080028/// The return code of doh_query means that there is no answer.
Matthew Maurer6b178422021-09-16 17:08:51 -070029static const ssize_t DOH_RESULT_INTERNAL_ERROR = -1;
Luke Huangfe453402021-06-24 03:15:03 +080030
31/// The return code of doh_query means that query can't be sent.
Matthew Maurer6b178422021-09-16 17:08:51 -070032static const ssize_t DOH_RESULT_CAN_NOT_SEND = -2;
Luke Huangfe453402021-06-24 03:15:03 +080033
34/// The return code of doh_query to indicate that the query timed out.
Matthew Maurer6b178422021-09-16 17:08:51 -070035static const ssize_t DOH_RESULT_TIMEOUT = -255;
Luke Huangfe453402021-06-24 03:15:03 +080036
Luke Huang53d3eb22021-07-19 15:08:56 +080037/// The error log level.
Matthew Maurer6b178422021-09-16 17:08:51 -070038static const uint32_t DOH_LOG_LEVEL_ERROR = 0;
Luke Huang53d3eb22021-07-19 15:08:56 +080039
40/// The warning log level.
Matthew Maurer6b178422021-09-16 17:08:51 -070041static const uint32_t DOH_LOG_LEVEL_WARN = 1;
Luke Huang53d3eb22021-07-19 15:08:56 +080042
43/// The info log level.
Matthew Maurer6b178422021-09-16 17:08:51 -070044static const uint32_t DOH_LOG_LEVEL_INFO = 2;
Luke Huang53d3eb22021-07-19 15:08:56 +080045
46/// The debug log level.
Matthew Maurer6b178422021-09-16 17:08:51 -070047static const uint32_t DOH_LOG_LEVEL_DEBUG = 3;
Luke Huang53d3eb22021-07-19 15:08:56 +080048
49/// The trace log level.
Matthew Maurer6b178422021-09-16 17:08:51 -070050static const uint32_t DOH_LOG_LEVEL_TRACE = 4;
Luke Huang53d3eb22021-07-19 15:08:56 +080051
Luke Huangfe453402021-06-24 03:15:03 +080052/// Context for a running DoH engine.
53struct DohDispatcher;
54
55using ValidationCallback = void (*)(uint32_t net_id, bool success, const char* ip_addr,
56 const char* host);
Luke Huangf71a7222021-03-17 14:27:28 +080057
Luke Huang10eed9d2021-09-29 12:11:30 +080058using TagSocketCallback = void (*)(int32_t sock);
59
Luke Huangf71a7222021-03-17 14:27:28 +080060extern "C" {
61
Luke Huang53d3eb22021-07-19 15:08:56 +080062/// Performs static initialization for android logger.
Matthew Maurer6b178422021-09-16 17:08:51 -070063/// If an invalid level is passed, defaults to logging errors only.
64/// If called more than once, it will have no effect on subsequent calls.
Luke Huang53d3eb22021-07-19 15:08:56 +080065void doh_init_logger(uint32_t level);
66
67/// Set the log level.
Matthew Maurer6b178422021-09-16 17:08:51 -070068/// If an invalid level is passed, defaults to logging errors only.
Luke Huang53d3eb22021-07-19 15:08:56 +080069void doh_set_log_level(uint32_t level);
70
71/// Performs the initialization for the DoH engine.
Luke Huangf71a7222021-03-17 14:27:28 +080072/// Creates and returns a DoH engine instance.
Luke Huang10eed9d2021-09-29 12:11:30 +080073DohDispatcher* doh_dispatcher_new(ValidationCallback validation_fn,
74 TagSocketCallback tag_socket_fn);
Luke Huangf71a7222021-03-17 14:27:28 +080075
Luke Huangfe453402021-06-24 03:15:03 +080076/// Deletes a DoH engine created by doh_dispatcher_new().
77/// # Safety
78/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
79/// and not yet deleted by `doh_dispatcher_delete()`.
80void doh_dispatcher_delete(DohDispatcher* doh);
Luke Huangf71a7222021-03-17 14:27:28 +080081
Luke Huangfe453402021-06-24 03:15:03 +080082/// Probes and stores the DoH server with the given configurations.
83/// Use the negative errno-style codes as the return value to represent the result.
84/// # Safety
85/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
86/// and not yet deleted by `doh_dispatcher_delete()`.
87/// `url`, `domain`, `ip_addr`, `cert_path` are null terminated strings.
88int32_t doh_net_new(DohDispatcher* doh, uint32_t net_id, const char* url, const char* domain,
89 const char* ip_addr, uint32_t sk_mark, const char* cert_path,
Mike Yu4c684972021-11-16 20:06:43 +080090 uint64_t probe_timeout_ms, uint64_t idle_timeout_ms);
Luke Huangfe453402021-06-24 03:15:03 +080091
92/// Sends a DNS query via the network associated to the given |net_id| and waits for the response.
93/// The return code should be either one of the public constant RESULT_* to indicate the error or
94/// the size of the answer.
95/// # Safety
96/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
97/// and not yet deleted by `doh_dispatcher_delete()`.
98/// `dns_query` must point to a buffer at least `dns_query_len` in size.
99/// `response` must point to a buffer at least `response_len` in size.
100ssize_t doh_query(DohDispatcher* doh, uint32_t net_id, uint8_t* dns_query, size_t dns_query_len,
101 uint8_t* response, size_t response_len, uint64_t timeout_ms);
102
103/// Clears the DoH servers associated with the given |netid|.
104/// # Safety
105/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
106/// and not yet deleted by `doh_dispatcher_delete()`.
107void doh_net_delete(DohDispatcher* doh, uint32_t net_id);
Luke Huangf71a7222021-03-17 14:27:28 +0800108
109} // extern "C"