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
376
|
/*
WARNING: This file was generated by dkct.
Changes you make here will be lost if dkct is run again!
You should modify the original source and run dkct on it.
Original source: dk4sock04.ctr
*/
/** @file dk4sock04.c The dk4sock04 module.
*/
#line 6 "dk4sock04.ctr"
#include "dk4sock.h"
#include "dk4mem.h"
#include "dk4strw.h"
#include "dk4strd.h"
#include "dk4maowd.h"
#include "dk4maowh.h"
#include "dk4rec23.h"
#line 18 "dk4sock04.ctr"
/** Constant texts used by the module, not localized.
*/
static const wchar_t * const dk4socket_wc_kw[] = {
/* 0 */
L"@",
/* 1 */
L".",
/* 2 */
L":",
NULL
#line 38 "dk4sock04.ctr"
};
int
dk4socket_wc_inet_ntop(
wchar_t *dptr,
size_t dsz,
int afam,
const void *src,
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
#if DK4_HAVE_INET_NTOP
/* +++++ +Windows +inet_ntop */
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
struct in6_addr in6;
#endif
struct in_addr in4;
int back = DK4_SOCKET_RESULT_FAILED;
if ((NULL != dptr) && (0 < dsz)) { *dptr = L'\0'; }
if ((NULL != dptr) && (NULL != src) && (0 < dsz)) {
dk4socket_error_reset();
switch (afam) {
case AF_INET : {
DK4_MEMCPY(&in4, src, sizeof(in4));
if (NULL != InetNtopW(afam, (void *)(&in4), dptr, dsz)) {
back = DK4_SOCKET_RESULT_SUCCESS;
} else {
dk4socket_error_report(erp, DK4_E_SOCKET_INET_NTOP);
}
} break;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
case AF_INET6 : {
DK4_MEMCPY(&in6, src, sizeof(in6));
if (NULL != InetNtopW(afam, (void *)(&in6), dptr, dsz)) {
back = DK4_SOCKET_RESULT_SUCCESS;
} else {
dk4socket_error_report(erp, DK4_E_SOCKET_INET_NTOP);
}
} break;
#endif
default : {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} break;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
/* ----- +Windows +inet_ntop */
#else
/* +++++ +Windows -inet_ntop */
wchar_t buf[16*sizeof(dk4_um_t)];
const struct in_addr *inp4;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
const struct in6_addr *inp6;
#endif
unsigned long ul;
unsigned long u1;
int back = DK4_SOCKET_RESULT_FAILED;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
int i;
#endif
int res;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
unsigned short us;
#endif
if ((NULL != dptr) && (0 < dsz)) { *dptr = L'\0'; }
if ((NULL != dptr) && (NULL != src) && (0 < dsz)) {
switch (afam) {
case AF_INET : {
inp4 = (const struct in_addr *)src;
ul = inp4->s_addr;
ul = dk4socket_htonl(ul);
u1 = ((ul >> 24) & 0x000000FFUL);
res = dk4ma_write_wc_decimal_unsigned(
buf, sizeof(buf), (dk4_um_t)u1, 0, erp
);
if (0 != res) {
if (0 != dk4strw_cpy_s(dptr, dsz, buf, erp)) {
u1 = ((ul >> 16) & 0x000000FFUL);
res = dk4ma_write_wc_decimal_unsigned(
buf, sizeof(buf), (dk4_um_t)u1, 0, erp
);
if (0 != res) {
if (0 != dk4strw_cat_s(dptr, dsz, dk4socket_wc_kw[1], erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, buf, erp)) {
u1 = ((ul >> 8) & 0x000000FFUL);
res = dk4ma_write_wc_decimal_unsigned(
buf, sizeof(buf), (dk4_um_t)u1, 0, erp
);
if (0 != res) {
if (0 != dk4strw_cat_s(dptr,dsz,dk4socket_wc_kw[1],erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, buf, erp)) {
u1 = (ul & 0x000000FFUL);
res = dk4ma_write_wc_decimal_unsigned(
buf, sizeof(buf), (dk4_um_t)u1, 0, erp
);
if (0 != res) {
if (
0 != dk4strw_cat_s(dptr,dsz,dk4socket_wc_kw[1],erp)
)
{
if (0 != dk4strw_cat_s(dptr, dsz, buf, erp)) {
back = DK4_SOCKET_RESULT_SUCCESS;
}
}
}
}
}
}
}
}
}
}
}
} break;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
case AF_INET6 : {
inp6 = (const struct in6_addr *)src;
back = DK4_SOCKET_RESULT_SUCCESS;
for (i = 0; i < 8; i++) {
#if DK4_ON_WINDOWS
us = inp6->u.Word[i];
#else
us = inp6->s6_addr16[i];
#endif
us = dk4socket_htons(us);
res = dk4ma_write_wc_hex_unsigned(
buf, sizeof(buf), (dk4_um_t)us, 0, erp
);
if (0 != res) {
if (0 == i) {
if (0 == dk4strw_cpy_s(dptr, dsz, buf, erp)) {
back = DK4_SOCKET_RESULT_FAILED;
i = 8;
}
} else {
if (0 != dk4strw_cat_s(dptr, dsz, dk4socket_wc_kw[2], erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, buf, erp)) {
back = DK4_SOCKET_RESULT_FAILED;
i = 8;
}
} else {
back = DK4_SOCKET_RESULT_FAILED;
i = 8;
}
}
} else {
back = DK4_SOCKET_RESULT_FAILED;
i = 8;
}
}
} break;
#endif
default : {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} break;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
/* ----- +Windows -inet_ntop */
#endif
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
return DK4_SOCKET_RESULT_FAILED;
#endif
}
int
dk4socket_wc_addr_port_to_text(
wchar_t *dptr,
size_t dsz,
int afam,
const struct sockaddr *saptr,
size_t sasz,
dk4_er_t *erp
)
{
#if DK4_ON_WINDOWS
#if DK4_HAVE_GETNAMEINFO
/* +++++ +getnameinfo */
wchar_t hbuf[64];
wchar_t pbuf[64];
const DWORD szh = DK4_SIZEOF(hbuf,wchar_t);
const DWORD szp = DK4_SIZEOF(pbuf,wchar_t);
int back = DK4_SOCKET_RESULT_FAILED;
int res;
if ((NULL != dptr) && (0 < dsz)) { *dptr = '\0'; }
if ((NULL != dptr) && (NULL != saptr) && (0 < dsz) && (0 < sasz)) {
res = NI_NUMERICHOST | NI_NUMERICSERV;
dk4socket_error_reset();
#if DK4_HAVE_SOCKLEN_T
res = GetNameInfoW(saptr, (socklen_t)sasz, hbuf, szh, pbuf, szp, res);
#else
res = GetNameInfoW(saptr, sasz, hbuf, szh, pbuf, szp, res);
#endif
if (0 == res) {
if (0 != dk4strw_cpy_s(dptr, dsz, pbuf, erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, dk4socket_wc_kw[0], erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, hbuf, erp)) {
back = DK4_SOCKET_RESULT_SUCCESS;
}
}
}
} else {
dk4error_set_idetails(erp, DK4_E_SOCKET_GETADDRINFO, res);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
/* ----- +getnameinfo */
#else
/* +++++ -getnameinfo */
wchar_t hbuf[64];
wchar_t pbuf[64];
struct sockaddr_in *psoin4;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
struct sockaddr_in6 *psoin6;
#endif
int back = DK4_SOCKET_RESULT_FAILED;
int hpah = 0;
int res;
unsigned short us;
if ((NULL != dptr) && (0 < dsz)) { *dptr = L'\0'; }
if ((NULL != dptr) && (NULL != saptr) && (0 < dsz) && (0 < sasz)) {
switch (afam) {
case AF_INET : {
psoin4 = (struct sockaddr_in *)saptr;
us = psoin4->sin_port;
us = dk4socket_ntohs(us);
res = dk4ma_write_wc_decimal_unsigned(
pbuf, sizeof(pbuf), (dk4_um_t)us, 0, erp
);
if (0 != res) {
res = dk4socket_wc_inet_ntop(
hbuf, sizeof(hbuf), AF_INET, &(psoin4->sin_addr), erp
);
if (DK4_SOCKET_RESULT_SUCCESS == res) {
hpah = 1;
}
}
} break;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
case AF_INET6 : {
psoin6 = (struct sockaddr_in6 *)saptr;
us = psoin6->sin6_port;
us = dk4socket_ntohs(us);
res = dk4ma_write_wc_decimal_unsigned(
pbuf, sizeof(pbuf), (dk4_um_t)us, 0, erp
);
if (0 != res) {
res = dk4socket_wc_inet_ntop(
hbuf, sizeof(hbuf), AF_INET6, &(psoin6->sin6_addr), erp
);
if (DK4_SOCKET_RESULT_SUCCESS == res) {
hpah = 1;
}
}
} break;
#endif
default : {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} break;
}
if (0 != hpah) {
if (0 != dk4strw_cpy_s(dptr, dsz, pbuf, erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, dk4socket_wc_kw[0], erp)) {
if (0 != dk4strw_cat_s(dptr, dsz, hbuf, erp)) {
back = DK4_SOCKET_RESULT_SUCCESS;
}
}
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
/* ----- -getnameinfo */
#endif
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
return DK4_SOCKET_RESULT_FAILED;
#endif
}
int
dk4socket_wc_sockaddr_to_text(
wchar_t *dptr,
size_t dsz,
const dk4_sockaddr_storage_t *saptr,
dk4_er_t *erp
)
{
const struct sockaddr *soa;
int back = DK4_SOCKET_RESULT_FAILED;
if ((NULL != dptr) && (NULL != saptr) && (0 < dsz)) {
soa = (const struct sockaddr *)saptr;
switch (soa->sa_family) {
case AF_INET : {
back = dk4socket_wc_addr_port_to_text(
dptr, dsz, AF_INET, soa, sizeof(struct sockaddr_in), erp
);
} break;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
case AF_INET6 : {
back = dk4socket_wc_addr_port_to_text(
dptr, dsz, AF_INET6, soa, sizeof(struct sockaddr_in6), erp
);
} break;
#endif
default : {
dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
} break;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
|