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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/net/nameresolvers.h"
#include "gate/results.h"
#include "gate/net/sockets.h"
#include "gate/regexpressions.h"
#define GATE_NET_NAMERSOLVERS_ADDRINFO_IMPL 1
gate_bool_t gate_net_is_ipv4_address(gate_string_t const* ipv4text)
{
static gate_string_t const ipv4_pattern = GATE_STRING_INIT_STATIC("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$");
static gate_regex_pattern_t rx;
static gate_bool_t is_init = false;
gate_result_t result;
gate_size_t dummy;
if (!is_init)
{
result = gate_regex_init(&rx, &ipv4_pattern);
if (GATE_FAILED(result))
{
gate_socket_ip4address_t ip4;
result = gate_socket_parse_ip4(ipv4text, &ip4);
return GATE_SUCCEEDED(result);
}
else
{
is_init = true;
}
}
result = gate_regex_match(&rx, ipv4text, &dummy, &dummy);
return GATE_SUCCEEDED(result);
}
gate_bool_t gate_net_is_ipv6_address(gate_string_t const* ipv6text)
{
gate_socket_ip6address_t ip6;
gate_result_t result = gate_socket_parse_ip6(ipv6text, &ip6);
return GATE_SUCCEEDED(result);
}
#if defined(GATE_NET_NAMERSOLVERS_ADDRINFO_IMPL)
static gate_bool_t network_init();
static gate_bool_t network_uninit();
#include "gate/platforms.h"
#include "gate/net/platform/socket_api.h"
static gate_net_platform_socket_api_t api;
static gate_bool_t network_init()
{
gate_result_t result = gate_socket_init();
if (GATE_FAILED(result))
{
return false;
}
result = gate_socket_api_load(&api);
if (GATE_FAILED(result))
{
return false;
}
result = gate_socket_session_init();
if (GATE_FAILED(result))
{
return false;
}
return true;
}
static gate_bool_t network_uninit()
{
gate_result_t result = gate_socket_session_uninit();
return GATE_SUCCEEDED(result);
}
gate_result_t gate_net_resolve_host(gate_string_t const* hostname,
gate_int16_t family,
gate_socket_endpoint_t* endpoints,
gate_size_t* endpoints_length)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_result_t result;
struct addrinfo hints;
struct addrinfo* ptr_infos = NULL;
struct addrinfo const* ptr_info;
char host[512];
int errcode;<--- The scope of the variable 'errcode' can be reduced. [+]The scope of the variable 'errcode' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t ep_used = 0;
gate_bool_t net_uninit_required = false;
do
{
if (!endpoints || !endpoints_length)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
net_uninit_required = network_init();
if (!net_uninit_required)
{
ret = GATE_RESULT_NOTAVAILABLE;
break;
}
gate_string_to_buffer(hostname, host, sizeof(host));
gate_mem_clear(&hints, sizeof(hints));
hints.ai_family = PF_UNSPEC;
switch (family)
{
case GATE_SOCKET_FAMILY_INET4:
{
hints.ai_family = AF_INET;
break;
}
case GATE_SOCKET_FAMILY_INET6:
{
hints.ai_family = AF_INET6;
break;
}
}
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_CANONNAME;
errcode = api.sock_getaddrinfo(host, NULL, &hints, &ptr_infos);
if (errcode != 0)
{
ret = GATE_RESULT_FAILED;
break;
}
ret = GATE_RESULT_OK;
for (ptr_info = ptr_infos; ptr_info != NULL; ptr_info = ptr_info->ai_next)
{
if (ep_used >= *endpoints_length)
{
/* there are more results than space available */
ret = GATE_RESULT_OK_PARTIAL;
break;
}
result = gate_socket_load_endpoint(ptr_info->ai_addr, endpoints);
if (GATE_SUCCEEDED(result))
{
++endpoints;
++ep_used;
}
}
*endpoints_length = ep_used;
} while (0);
if (ptr_infos != NULL)
{
api.sock_freeaddrinfo(ptr_infos);
}
if (net_uninit_required)
{
network_uninit();
}
return ret;
}
#endif
|