blob: 4d82d7a26c27853c0c0a5a83752a2615df4e46bd [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.
29static const ssize_t RESULT_INTERNAL_ERROR = -1;
30
31/// The return code of doh_query means that query can't be sent.
32static const ssize_t RESULT_CAN_NOT_SEND = -2;
33
34/// The return code of doh_query to indicate that the query timed out.
35static const ssize_t RESULT_TIMEOUT = -255;
36
Luke Huang53d3eb22021-07-19 15:08:56 +080037/// The error log level.
38static const uint32_t LOG_LEVEL_ERROR = 0;
39
40/// The warning log level.
41static const uint32_t LOG_LEVEL_WARN = 1;
42
43/// The info log level.
44static const uint32_t LOG_LEVEL_INFO = 2;
45
46/// The debug log level.
47static const uint32_t LOG_LEVEL_DEBUG = 3;
48
49/// The trace log level.
50static const uint32_t LOG_LEVEL_TRACE = 4;
51
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.
63void doh_init_logger(uint32_t level);
64
65/// Set the log level.
66void doh_set_log_level(uint32_t level);
67
68/// Performs the initialization for the DoH engine.
Luke Huangf71a7222021-03-17 14:27:28 +080069/// Creates and returns a DoH engine instance.
Luke Huang10eed9d2021-09-29 12:11:30 +080070DohDispatcher* doh_dispatcher_new(ValidationCallback validation_fn,
71 TagSocketCallback tag_socket_fn);
Luke Huangf71a7222021-03-17 14:27:28 +080072
Luke Huangfe453402021-06-24 03:15:03 +080073/// Deletes a DoH engine created by doh_dispatcher_new().
74/// # Safety
75/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
76/// and not yet deleted by `doh_dispatcher_delete()`.
77void doh_dispatcher_delete(DohDispatcher* doh);
Luke Huangf71a7222021-03-17 14:27:28 +080078
Luke Huangfe453402021-06-24 03:15:03 +080079/// Probes and stores the DoH server with the given configurations.
80/// Use the negative errno-style codes as the return value to represent the result.
81/// # Safety
82/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
83/// and not yet deleted by `doh_dispatcher_delete()`.
84/// `url`, `domain`, `ip_addr`, `cert_path` are null terminated strings.
85int32_t doh_net_new(DohDispatcher* doh, uint32_t net_id, const char* url, const char* domain,
86 const char* ip_addr, uint32_t sk_mark, const char* cert_path,
87 uint64_t timeout_ms);
88
89/// Sends a DNS query via the network associated to the given |net_id| and waits for the response.
90/// The return code should be either one of the public constant RESULT_* to indicate the error or
91/// the size of the answer.
92/// # Safety
93/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
94/// and not yet deleted by `doh_dispatcher_delete()`.
95/// `dns_query` must point to a buffer at least `dns_query_len` in size.
96/// `response` must point to a buffer at least `response_len` in size.
97ssize_t doh_query(DohDispatcher* doh, uint32_t net_id, uint8_t* dns_query, size_t dns_query_len,
98 uint8_t* response, size_t response_len, uint64_t timeout_ms);
99
100/// Clears the DoH servers associated with the given |netid|.
101/// # Safety
102/// `doh` must be a non-null pointer previously created by `doh_dispatcher_new()`
103/// and not yet deleted by `doh_dispatcher_delete()`.
104void doh_net_delete(DohDispatcher* doh, uint32_t net_id);
Luke Huangf71a7222021-03-17 14:27:28 +0800105
106} // extern "C"