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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
%% options
copyright owner = Dirk Krause
copyright year = 2016-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% module
#include "dk4conf.h"
#include "dk4types.h"
#include "dk4mem.h"
#include "dk4sock.h"
#include "dk4mai8dus.h"
#include "dk4maowd.h"
#include "dk4mao8d.h"
#include "dk4rec24.h"
#include "dk4unused.h"
#if DK4_HAVE_ASSERT_H
#ifndef ASSERT_H_INCLUDED
#include <assert.h>
#define ASSERT_H_INCLUDED 1
#endif
#endif
$!trace-include
#if DK4_ON_WINDOWS
/** Attempt to convert an IPv4 or IPv6 address from text to address.
@param dptr Destination variable.
@param str Source text.
@return 1 on success, 0 on error.
*/
static
int
dk4socket_wc_find_address(dk4_sockaddr_storage_t *dptr, const wchar_t *str)
{
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
struct sockaddr_in6 *in6;
#endif
struct sockaddr_in *in4;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
IN6_ADDR addr6;
#endif
IN_ADDR addr4;
int back = 0;
int res;
$? "+ dk4socket_wc_find_address"
res = dk4socket_wc_inet_pton(&addr4, sizeof(addr4), AF_INET, str, NULL);
if (DK4_SOCKET_RESULT_SUCCESS == res) {
in4 = (struct sockaddr_in *)dptr;
in4->sin_family = AF_INET;
DK4_MEMCPY(&(in4->sin_addr), &addr4, sizeof(addr4));
back = 1;
} else {
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
res = dk4socket_wc_inet_pton(&addr6, sizeof(addr6), AF_INET6, str, NULL);
if (DK4_SOCKET_RESULT_SUCCESS == res) {
in6 = (struct sockaddr_in6 *)dptr;
in6->sin6_family = AF_INET6;
DK4_MEMCPY(&(in6->sin6_addr), &addr6, sizeof(addr6));
back = 1;
}
#endif
}
$? "- dk4socket_wc_find_address %d", back
return back;
}
/** Find address for one host name.
@param dptr Address of destination data structure.
@param remhost Remote host name.
@param remport Remote port number in host representation.
@param ip4 Flag: Prefer IPv4 over IPv6 addresses.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
*/
static
int
dk4socket_wc_find_host(
dk4_sockaddr_storage_t *dptr,
const wchar_t *remhost,
unsigned short remport,
int ip4,
#if (!(DK4_HAVE_GETADDRINFO))
dk4_er_t *erp
#else
dk4_er_t * DK4_ARG_UNUSED(erp)
#endif
)
{
#if DK4_HAVE_GETADDRINFO
/* +++ getaddrinfo +++ */
wchar_t buf[16*sizeof(unsigned short)];
ADDRINFOW hints;
ADDRINFOW *results = NULL;
ADDRINFOW *rptr = NULL;
int back = 0;
int res = 0;
DK4_UNUSED_ARG(erp)
$? "+ dk4socket_wc_find_host (getaddrinfo) \"%!ws\"", TR_LSTR(remhost)
#if DK4_USE_ASSERT
assert(NULL != dptr);
assert(NULL != remhost);
assert(0 < remport);
#endif
res = dk4ma_write_wc_decimal_unsigned(
buf, DK4_SIZEOF(buf,wchar_t), (dk4_um_t)remport, 0, NULL
);
if (0 != res) {
DK4_MEMRES(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_NUMERICSERV;
res = GetAddrInfoW(remhost, buf, &hints, &results);
if ((0 == res) && (NULL != results)) { $? ". GetAddrInfoW"
if (0 != ip4) {
rptr = results;
while ((NULL != rptr) && (0 == back)) {
if ((0 < rptr->ai_addrlen) && (NULL != rptr->ai_addr)) {
if (AF_INET == rptr->ai_addr->sa_family) {
if (sizeof(dk4_sockaddr_storage_t) >= rptr->ai_addrlen) {
DK4_MEMCPY(dptr, rptr->ai_addr, rptr->ai_addrlen);
back = 1; $? ". found"
}
#if TRACE_DEBUG
else { $? "! address too large"
}
#endif
}
#if TRACE_DEBUG
else { $? "! not IPv4"
}
#endif
}
#if TRACE_DEBUG
else { $? "! no address data"
}
#endif
rptr = rptr->ai_next;
}
if (0 == back) {
rptr = results;
while ((NULL != rptr) && (0 == back)) {
if ((0 < rptr->ai_addrlen) && (NULL != rptr->ai_addr)) {
if (sizeof(dk4_sockaddr_storage_t) >= rptr->ai_addrlen) {
DK4_MEMCPY(dptr, rptr->ai_addr, rptr->ai_addrlen);
back = 1; $? "- found"
}
#if TRACE_DEBUG
else { $? "! address to large"
}
#endif
}
#if TRACE_DEBUG
else { $? "! no address data"
}
#endif
rptr = rptr->ai_next;
}
}
} else { $? ". any address fam"
rptr = results;
while ((NULL != rptr) && (0 == back)) {
if ((0 < rptr->ai_addrlen) && (NULL != rptr->ai_addr)) {
if (sizeof(dk4_sockaddr_storage_t) >= rptr->ai_addrlen) {
DK4_MEMCPY(dptr, rptr->ai_addr, rptr->ai_addrlen);
back = 1; $? ". found"
}
#if TRACE_DEBUG
else { $? "! addr too large"
}
#endif
}
#if TRACE_DEBUG
else { $? "! no address data"
}
#endif
rptr = rptr->ai_next;
}
}
} else { $? "! GetAddrInfoW"
}
if (NULL != results) { FreeAddrInfoW(results); }
}
#if TRACE_DEBUG
else { $? "! failed to convert port number to text"
}
#endif
$? "- dk4socket_wc_find_host %d", back
return back;
/* --- getaddrinfo --- */
#else
/* +++ ! getaddrinfo +++ */
#if DK4_HAVE_GETHOSTBYNAME
/* +++ gethostbyname +++ */
char hona[1040];
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
struct sockaddr_in6 *sa6;
#endif
struct sockaddr_in *sa4;
struct hostent *hep = NULL;
int back = 0;
$? "+ dk4socket_wc_find_host (gethostbyname) \"%s\"", TR_LSTR(remhost)
#if DK4_USE_ASSERT
assert(NULL != dptr);
assert(NULL != remhost);
assert(0 < remport);
#endif
if (0 != dk4recode_wchar_t_to_char(hona, sizeof(hona), remhost, erp)) {
hep = gethostbyname(hona);
if (NULL != hep) { $? ". hep"
if (NULL != hep->h_addr_list) { $? ". hep->h_addr_list"
if (NULL != *(hep->h_addr_list)) { $? ". *(hep->h_addr_list)"
switch (hep->h_addrtype) {
case AF_INET : { $? ". IPv4"
sa4 = (struct sockaddr_in *)dptr;
DK4_MEMRES(sa4, sizeof(struct sockaddr_in));
sa4->sin_family = AF_INET;
sa4->sin_port = dk4socket_htons(remport);
DK4_MEMCPY(&(sa4->sin_addr),*(hep->h_addr_list),sizeof(IN_ADDR));
back = 1; $? ". found"
} break;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
case AF_INET6 : { $? ". IPv6"
sa6 = (struct sockaddr_in6 *)dptr;
DK4_MEMRES(sa6, sizeof(struct sockaddr_in6));
sa6->sin6_family = AF_INET6;
sa6->sin_port = dk4socket_htons(remport);
DK4_MEMCPY(&(sa6->sin6_addr),*(hep->h_addr_list),sizeof(IN6_ADDR));
back = 1; $? ". found"
} break;
#endif
#if TRACE_DEBUG
default : { $? "! address type"
} break;
#endif
}
}
#if TRACE_DEBUG
else { $? "! *(hep->h_addr_list)"
}
#endif
}
#if TRACE_DEBUG
else { $? "! hep->h_addr_list"
}
#endif
}
#if TRACE_DEBUG
else { $? "! not found"
}
#endif
}
#if TRACE_DEBUG
else { $? "! recode"
}
#endif
$? "- dk4socket_wc_find_host %d", back
return back;
/* --- gethostbyname --- */
#else
/* +++ ! gethostbyname +++ */
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
return 0; $? "! not supported"
/* --- ! gethostbyname --- */
#endif
/* --- ! getaddrinfo --- */
#endif
}
#endif
int
dk4socket_wc_udp_addresses_numeric(
#if DK4_ON_WINDOWS
dk4_sockaddr_storage_t *array,
const wchar_t *remhost,
unsigned short remport,
unsigned short locport,
int ip4,
#else
dk4_sockaddr_storage_t * DK4_ARG_UNUSED(array),
const wchar_t * DK4_ARG_UNUSED(remhost),
unsigned short DK4_ARG_UNUSED(remport),
unsigned short DK4_ARG_UNUSED(locport),
int DK4_ARG_UNUSED(ip4),
#endif
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
int back = DK4_SOCKET_RESULT_FAILED;
$? "+ dk4socket_wc_udp_addresses_numeric DK4_ON_WINDOWS"
#if DK4_USE_ASSERT
assert(NULL != array);
assert(NULL != remhost);
assert(0 < remport);
assert(0 < locport);
#endif
if ((NULL != array) && (NULL != remhost) && (0 < remport) && (0 < locport)) {
DK4_MEMRES(array, (2*sizeof(dk4_sockaddr_storage_t)));
if (0 != dk4socket_wc_find_address(array, remhost)) {
$? ". address pair found"
back = dk4socket_complete_addresses(array, remport, locport);
$? ". back = %d", back
}
if (DK4_SOCKET_RESULT_FAILED == back) { $? ". no pair"
if (0 != dk4socket_wc_find_host(array, remhost, remport, ip4, erp)) {
back = dk4socket_complete_addresses(array, remport, locport);
if (DK4_SOCKET_RESULT_FAILED == back) { $? "! complete"
dk4error_set_simple_error_code(erp, DK4_E_BUG);
}
} else { $? "! not found"
dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
}
}
} else { $? "! arguments"
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
$? "- dk4socket_wc_udp_addresses_numeric DK4_ON_WINDOWS %d", back
return back;
#else
DK4_UNUSED_ARG(array)
DK4_UNUSED_ARG(remhost)
DK4_UNUSED_ARG(remport)
DK4_UNUSED_ARG(locport)
DK4_UNUSED_ARG(ip4)
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
return 0;
#endif
}
int
dk4socket_udp_addresses_numeric(
dk4_sockaddr_storage_t *array,
const dkChar *remhost,
unsigned short remport,
unsigned short locport,
int ip4,
dk4_er_t *erp
)
{
#if DK4_USE_ASSERT
assert(NULL != array);
assert(NULL != remhost);
assert(0 < remport);
assert(0 < locport);
#endif
#if DK4_CHAR_SIZE > 1
return (
dk4socket_wc_udp_addresses_numeric(
array, remhost, remport, locport, ip4, erp
)
);
#else
return (
dk4socket_c8_udp_addresses_numeric(
array, remhost, remport, locport, ip4, erp
)
);
#endif
}
|