summaryrefslogtreecommitdiff
path: root/support/dktools/dk4sock02.ctr
blob: 8e39a7dc7b4b6ce01a6c0d65f579c7b46cc961dc (plain)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd

%%	module

/**	@file	dk4sock01.c	Conversion between host and net representation.
*/

#include "dk4types.h"
#include "dk4error.h"
#include "dk4sock.h"

#if DK4_HAVE_STRINGS_H
#ifndef	STRINGS_H_INCLUDED
#include <strings.h>
#define	STRINGS_H_INCLUDED 1
#endif
#endif

#if (!DK4_ON_WINDOWS) \
&& (DK4_HAVE_HTONS || DK4_HAVE_NTOHS || DK4_HAVE_HTONL || DK4_HAVE_NTOHL)

#if DK4_HAVE_ARPA_INET_H
#ifndef ARPA_INET_H_INCLUDED
#include <arpa/inet.h>
#define	ARPA_INET_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_NETINET_IN_H
#ifndef NETINET_IN_H_INCLUDED
#include <netinet/in.h>
#define	NETINET_IN_H_INCLUDED 1
#endif
#endif

#endif


#if DK4_ON_WINDOWS || (!DK4_HAVE_HTONS) || (!DK4_HAVE_NTOHS)
#if !DK4_WORDS_BIGENDIAN

static
uint16_t
dk4socket_short_swap(uint16_t u)
{
  return (
    ((u >> 8) & 0x00FFU)
    | ((u << 8) & 0xFF00U)
  );
}

#endif
#endif

#if DK4_ON_WINDOWS || (!DK4_HAVE_HTONL) || (!DK4_HAVE_NTOHL)
#if !DK4_WORDS_BIGENDIAN

static
uint32_t
dk4socket_long_swap(uint32_t u)
{
  return (
    (  (u >> 24) & 0x000000FFUL)
    | ((u >>  8) & 0x0000FF00UL)
    | ((u <<  8) & 0x00FF0000UL)
    | ((u << 24) & 0xFF000000UL)
  );
}

#endif
#endif

uint16_t
dk4socket_htons(uint16_t u)
{
#if DK4_HAVE_HTONS && (!DK4_ON_WINDOWS)
  return (htons(u));
#else
#if DK4_WORDS_BIGENDIAN
  return u;
#else
  return (dk4socket_short_swap(u));
#endif
#endif
}



uint16_t
dk4socket_ntohs(uint16_t u)
{
#if DK4_HAVE_NTOHS && (!DK4_ON_WINDOWS)
  return (ntohs(u));
#else
#if DK4_WORDS_BIGENDIAN
  return u;
#else
  return (dk4socket_short_swap(u));
#endif
#endif
}

uint32_t
dk4socket_htonl(uint32_t u)
{
#if DK4_HAVE_HTONL && (!DK4_ON_WINDOWS)
  return (htonl(u));
#else
#if DK4_WORDS_BIGENDIAN
  return u;
#else
  return (dk4socket_long_swap(u));
#endif
#endif
}

uint32_t
dk4socket_ntohl(uint32_t u)
{
#if DK4_HAVE_NTOHL && (!DK4_ON_WINDOWS)
  return (ntohl(u));
#else
#if DK4_WORDS_BIGENDIAN
  return u;
#else
  return (dk4socket_long_swap(u));
#endif
#endif
}

void
dk4socket_swap_bytes_if_not_bigendian(void *buf, size_t sz)
{
#if !DK4_WORDS_BIGENDIAN
  unsigned char	*ucp;		/* Buffer address converted ty bytes pointer */
  size_t	 szh;		/* Half of the size sz */
  size_t	 i;		/* Current byte index, 0 to szh-1 */
  unsigned char	 u;		/* Byte variable used to swap bytes */

  if ((NULL != buf) && (0 < sz)) {
    ucp = (unsigned char *)buf;
    szh = sz / 2;
    for (i = 0; i < szh; i++) {
      u = ucp[i];
      ucp[i] = ucp[sz - 1 - i];
      ucp[sz - 1 - i] = u;
    }
  }
#endif
}



int
dk4socket_mask_bits(unsigned long *dptr, size_t sz, dk4_er_t *erp)
{
  unsigned long		res	= 0UL;
  unsigned long		val	= 0x80000000UL;
  int			back	= DK4_SOCKET_RESULT_FAILED;
  if (NULL != dptr) {
    if ((0 < sz) && (33 > sz)) {
      while (0 < sz--) {
        res |= val;
        val =  val / 2UL;
      }
      *dptr = res;
      back = DK4_SOCKET_RESULT_SUCCESS;
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



#if DK4_HAVE_STRUCT_SOCKADDR_IN6

int
dk4socket_mask6_bits(struct in6_addr *inp, size_t sz, dk4_er_t *erp)
{
  int			 back	= DK4_SOCKET_RESULT_FAILED;
  size_t		 i	= 0;
  size_t		 j	= 0;
  unsigned char		 rchr	= 0x00;
  unsigned char		 vchr	= 0x80;

  if (NULL != inp) {
    for (i = 0; i < 16; i++) { inp->s6_addr[i] = 0xFF; }
    if ((0 < sz) && (129 > sz)) {
      for (i = 0; i < 16; i++) {
        rchr = 0x00;
	vchr = 0x80;
	for (j = 0; j < 8; j++) {
	  if (0 < sz) {
	    rchr |= vchr;
	    sz--;
	  }
	  vchr = vchr / 2;
	}
	inp->s6_addr[i] = rchr;
      }
      back = DK4_SOCKET_RESULT_SUCCESS;
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}

#endif



void
dk4socket_set_one_bytes(void *ptr, size_t nbytes)
{
  unsigned char	*cptr;
  cptr = ptr;
  while (0 < nbytes--) { *(cptr++) = 0x01; }
}



#if DK4_HAVE_STRUCT_SOCKADDR_UN

size_t
dk4socket_unix_length(const struct sockaddr_un *sock)
{
  size_t	back = 0;
  if (NULL != sock) {
#ifdef SUN_LEN
    back = SUN_LEN(sock);
#else
    back = sizeof(struct sockaddr_un)
           - sizeof(sock->sun_path)
	   + strlen(sock->sun_path);
#endif
  }
  return back;
}

#endif


int
dk4socket_pf_for_af(int af)
{
  int		 back = 0;
  switch (af) {

#if defined(AF_UNSPEC) && defined(PF_UNSPEC)
    case AF_UNSPEC : { back = PF_UNSPEC ; } break;
#endif
#if defined(AF_UNIX) && defined(PF_UNIX)
    case AF_UNIX : { back = PF_UNIX ; } break;
#endif
#if defined(AF_LOCAL) && defined(PF_LOCAL) && \
	(!(defined(AF_UNIX) && defined(PF_UNIX)))
    case AF_LOCAL : { back = PF_LOCAL ; } break;
#endif
#if defined(AF_FILE) && defined(PF_FILE) && \
	(!(defined(AF_UNIX) && defined(PF_UNIX))) && \
	(!(defined(AF_LOCAL) && defined(PF_LOCAL)))
    case AF_FILE : { back = PF_FILE ; } break;
#endif
#if defined(AF_INET) && defined(PF_INET)
    case AF_INET : { back = PF_INET ; } break;
#endif
#if defined(AF_AX25) && defined(PF_AX25)
    case AF_AX25 : { back = PF_AX25 ; } break;
#endif
#if defined(AF_IPX) && defined(PF_IPX)
    case AF_IPX : { back = PF_IPX ; } break;
#endif
#if defined(AF_APPLETALK) && defined(PF_APPLETALK)
    case AF_APPLETALK : { back = PF_APPLETALK ; } break;
#endif
#if defined(AF_NETROM) && defined(PF_NETROM)
    case AF_NETROM : { back = PF_NETROM ; } break;
#endif
#if defined(AF_BRIDGE) && defined(PF_BRIDGE)
    case AF_BRIDGE : { back = PF_BRIDGE ; } break;
#endif
#if defined(AF_ATMPVC) && defined(PF_ATMPVC)
    case AF_ATMPVC : { back = PF_ATMPVC ; } break;
#endif
#if defined(AF_X25) && defined(PF_X25)
    case AF_X25 : { back = PF_X25 ; } break;
#endif
#if defined(AF_INET6) && defined(PF_INET6)
    case AF_INET6 : { back = PF_INET6 ; } break;
#endif
#if defined(AF_ROSE) && defined(PF_ROSE)
    case AF_ROSE : { back = PF_ROSE ; } break;
#endif
#if defined(AF_DECnet) && defined(PF_DECnet)
    case AF_DECnet : { back = PF_DECnet ; } break;
#endif
#if defined(AF_NETBEUI) && defined(PF_NETBEUI)
    case AF_NETBEUI : { back = PF_NETBEUI ; } break;
#endif
#if defined(AF_SECURITY) && defined(PF_SECURITY)
    case AF_SECURITY : { back = PF_SECURITY ; } break;
#endif
#if defined(AF_KEY) && defined(PF_KEY)
    case AF_KEY : { back = PF_KEY ; } break;
#endif
#if defined(AF_NETLINK) && defined(PF_NETLINK)
    case AF_NETLINK : { back = PF_NETLINK ; } break;
#endif
#if defined(AF_ROUTE) && defined(PF_ROUTE) && \
	(!(defined(AF_NETLINK) && defined(PF_NETLINK)))
    case AF_ROUTE : { back = PF_ROUTE ; } break;
#endif
#if defined(AF_PACKET) && defined(PF_PACKET)
    case AF_PACKET : { back = PF_PACKET ; } break;
#endif
#if defined(AF_ASH) && defined(PF_ASH)
    case AF_ASH : { back = PF_ASH ; } break;
#endif
#if defined(AF_ECONET) && defined(PF_ECONET)
    case AF_ECONET : { back = PF_ECONET ; } break;
#endif
#if defined(AF_ATMSVC) && defined(PF_ATMSVC)
    case AF_ATMSVC : { back = PF_ATMSVC ; } break;
#endif
#if defined(AF_RDS) && defined(PF_RDS)
    case AF_RDS : { back = PF_RDS ; } break;
#endif
#if defined(AF_SNA) && defined(PF_SNA)
    case AF_SNA : { back = PF_SNA ; } break;
#endif
#if defined(AF_IRDA) && defined(PF_IRDA)
    case AF_IRDA : { back = PF_IRDA ; } break;
#endif
#if defined(AF_PPPOX) && defined(PF_PPPOX)
    case AF_PPPOX : { back = PF_PPPOX ; } break;
#endif
#if defined(AF_WANPIPE) && defined(PF_WANPIPE)
    case AF_WANPIPE : { back = PF_WANPIPE ; } break;
#endif
#if defined(AF_LLC) && defined(PF_LLC)
    case AF_LLC : { back = PF_LLC ; } break;
#endif
#if defined(AF_CAN) && defined(PF_CAN)
    case AF_CAN : { back = PF_CAN ; } break;
#endif
#if defined(AF_TIPC) && defined(PF_TIPC)
    case AF_TIPC : { back = PF_TIPC ; } break;
#endif
#if defined(AF_BLUETOOTH) && defined(PF_BLUETOOTH)
    case AF_BLUETOOTH : { back = PF_BLUETOOTH ; } break;
#endif
#if defined(AF_IUCV) && defined(PF_IUCV)
    case AF_IUCV : { back = PF_IUCV ; } break;
#endif
#if defined(AF_RXRPC) && defined(PF_RXRPC)
    case AF_RXRPC : { back = PF_RXRPC ; } break;
#endif
#if defined(AF_ISDN) && defined(PF_ISDN)
    case AF_ISDN : { back = PF_ISDN ; } break;
#endif
#if defined(AF_PHONET) && defined(PF_PHONET)
    case AF_PHONET : { back = PF_PHONET ; } break;
#endif
#if defined(AF_IEEE802154) && defined(PF_IEEE802154)
    case AF_IEEE802154 : { back = PF_IEEE802154 ; } break;
#endif
#if defined(AF_CAIF) && defined(PF_CAIF)
    case AF_CAIF : { back = PF_CAIF ; } break;
#endif
#if defined(AF_ALG) && defined(PF_ALG)
    case AF_ALG : { back = PF_ALG ; } break;
#endif
#if defined(AF_NFC) && defined(PF_NFC)
    case AF_NFC : { back = PF_NFC ; } break;
#endif
#if defined(AF_VSOCK) && defined(PF_VSOCK)
    case AF_VSOCK : { back = PF_VSOCK ; } break;
#endif
  }
  return back;
}



int
dk4socket_correct_addr_lgt(const struct sockaddr *soa, size_t *plgt)
{
  int		 back	= 0;
  if ((NULL != soa) && (NULL != plgt)) {
    back = 1;
    switch (soa->sa_family) {
      case AF_INET : {
	if (sizeof(struct sockaddr_in) <= *plgt) {
	  *plgt = sizeof(struct sockaddr_in);
	} else {
	  back = 0;
	}
      } break;
#if DK4_HAVE_STRUCT_SOCKADDR_IN6
      case AF_INET6 : {
        if (sizeof(struct sockaddr_in6) <= *plgt) {
	  *plgt = sizeof(struct sockaddr_in6);
	} else {
	  back = 0;
	}
      } break;
#endif
    }
  }
  return back;
}