File: mock_dns_resolver.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (70 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "mock_dns_resolver.h"

#include <aws/io/host_resolver.h>

int mock_dns_resolver_init(struct mock_dns_resolver *resolver, size_t max_resolves, struct aws_allocator *allocator) {
    resolver->index = 0;
    resolver->max_resolves = max_resolves;
    resolver->resolve_count = 0;
    return aws_array_list_init_dynamic(&resolver->address_list, allocator, 2, sizeof(struct aws_array_list));
}

void mock_dns_resolver_clean_up(struct mock_dns_resolver *resolver) {
    for (size_t i = 0; i < aws_array_list_length(&resolver->address_list); ++i) {
        struct aws_array_list *temp = NULL;
        aws_array_list_get_at_ptr(&resolver->address_list, (void **)&temp, i);

        for (size_t j = 0; j < aws_array_list_length(temp); ++j) {
            struct aws_host_address *temp_address = NULL;
            aws_array_list_get_at_ptr(temp, (void **)&temp_address, j);
            aws_host_address_clean_up(temp_address);
        }
        aws_array_list_clean_up(temp);
    }

    aws_array_list_clean_up(&resolver->address_list);
}

int mock_dns_resolver_append_address_list(struct mock_dns_resolver *resolver, struct aws_array_list *addresses) {
    return aws_array_list_push_back(&resolver->address_list, addresses);
}

int mock_dns_resolve(
    struct aws_allocator *allocator,
    const struct aws_string *host_name,
    struct aws_array_list *output_addresses,
    void *user_data) {

    (void)allocator;
    (void)host_name;
    struct mock_dns_resolver *mock_resolver = user_data;

    if (mock_resolver->resolve_count == mock_resolver->max_resolves) {
        return aws_raise_error(AWS_IO_DNS_QUERY_FAILED);
    }

    struct aws_array_list *iteration_list = NULL;
    if (aws_array_list_get_at_ptr(&mock_resolver->address_list, (void **)&iteration_list, mock_resolver->index)) {
        return aws_raise_error(AWS_ERROR_UNKNOWN);
    }
    mock_resolver->index = (mock_resolver->index + 1) % aws_array_list_length(&mock_resolver->address_list);
    mock_resolver->resolve_count += 1;

    if (aws_array_list_length(iteration_list) == 0) {
        return aws_raise_error(AWS_IO_DNS_QUERY_FAILED);
    }

    for (size_t i = 0; i < aws_array_list_length(iteration_list); ++i) {
        struct aws_host_address *temp_address = NULL;
        aws_array_list_get_at_ptr(iteration_list, (void **)&temp_address, i);
        struct aws_host_address address_cpy;
        aws_host_address_copy(temp_address, &address_cpy);
        aws_array_list_push_back(output_addresses, &address_cpy);
    }

    return AWS_OP_SUCCESS;
}