/src/h2o/include/h2o/hostinfo.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #ifndef h2o__hostinfo_h |
23 | | #define h2o__hostinfo_h |
24 | | |
25 | | #include <arpa/inet.h> |
26 | | #include <netdb.h> |
27 | | #include <netinet/in.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | #include <sys/socket.h> |
31 | | #include <sys/types.h> |
32 | | #include "h2o/multithread.h" |
33 | | |
34 | | /* generic errors (https://tools.ietf.org/html/rfc8499#section-3) */ |
35 | | extern const char h2o_hostinfo_error_nxdomain[]; |
36 | | extern const char h2o_hostinfo_error_nodata[]; |
37 | | extern const char h2o_hostinfo_error_refused[]; |
38 | | extern const char h2o_hostinfo_error_servfail[]; |
39 | | |
40 | | /* errors specfic to getaddrinfo */ |
41 | | extern const char h2o_hostinfo_error_gai_addrfamily[]; |
42 | | extern const char h2o_hostinfo_error_gai_badflags[]; |
43 | | extern const char h2o_hostinfo_error_gai_family[]; |
44 | | extern const char h2o_hostinfo_error_gai_memory[]; |
45 | | extern const char h2o_hostinfo_error_gai_service[]; |
46 | | extern const char h2o_hostinfo_error_gai_socktype[]; |
47 | | extern const char h2o_hostinfo_error_gai_system[]; |
48 | | extern const char h2o_hostinfo_error_gai_other[]; |
49 | | |
50 | | typedef struct st_h2o_hostinfo_getaddr_req_t h2o_hostinfo_getaddr_req_t; |
51 | | |
52 | | typedef void (*h2o_hostinfo_getaddr_cb)(h2o_hostinfo_getaddr_req_t *req, const char *errstr, struct addrinfo *res, void *cbdata); |
53 | | |
54 | | extern size_t h2o_hostinfo_max_threads; |
55 | | |
56 | | /** |
57 | | * dispatches a (possibly) asynchronous hostname lookup |
58 | | */ |
59 | | h2o_hostinfo_getaddr_req_t *h2o_hostinfo_getaddr(h2o_multithread_receiver_t *receiver, h2o_iovec_t name, h2o_iovec_t serv, |
60 | | int family, int socktype, int protocol, int flags, h2o_hostinfo_getaddr_cb cb, |
61 | | void *cbdata); |
62 | | /** |
63 | | * cancels the request |
64 | | */ |
65 | | void h2o_hostinfo_getaddr_cancel(h2o_hostinfo_getaddr_req_t *req); |
66 | | |
67 | | /** |
68 | | * function that receives and dispatches the responses |
69 | | */ |
70 | | void h2o_hostinfo_getaddr_receiver(h2o_multithread_receiver_t *receiver, h2o_linklist_t *messages); |
71 | | |
72 | | /** |
73 | | * select one entry at random from the response |
74 | | */ |
75 | | static struct addrinfo *h2o_hostinfo_select_one(struct addrinfo *res); |
76 | | |
77 | | /** |
78 | | * equiv. to inet_pton(AF_INET4) |
79 | | */ |
80 | | int h2o_hostinfo_aton(h2o_iovec_t host, struct in_addr *addr); |
81 | | |
82 | | /* inline defs */ |
83 | | |
84 | | inline struct addrinfo *h2o_hostinfo_select_one(struct addrinfo *res) |
85 | 0 | { |
86 | 0 | if (res->ai_next == NULL) |
87 | 0 | return res; |
88 | 0 |
|
89 | 0 | /* count the number of candidates */ |
90 | 0 | size_t i = 0; |
91 | 0 | struct addrinfo *ai = res; |
92 | 0 | do { |
93 | 0 | ++i; |
94 | 0 | } while ((ai = ai->ai_next) != NULL); |
95 | 0 |
|
96 | 0 | /* choose one, distributed by rand() :-p */ |
97 | 0 | i = rand() % i; |
98 | 0 | for (ai = res; i != 0; ai = ai->ai_next, --i) |
99 | 0 | ; |
100 | 0 | return ai; |
101 | 0 | } |
102 | | |
103 | | #endif |