summaryrefslogtreecommitdiff
path: root/bsd.h
blob: cd0cdc3602a1bdac0107ccb08bdb061cf0a497a6 (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
#ifndef LIBPORT_H
#define LIBPORT_H

#define setproctitle(fmt, args...)

#define __dead2
#define __printflike(x,y)
#define __FBSDID(x)

#include <sys/cdefs.h>
#include <sys/queue.h>


typedef char *                  __va_list;
#if !defined(__GNUC_VA_LIST)
#define __GNUC_VA_LIST
typedef __va_list               __gnuc_va_list; /* compatibility w/GNU headers*/
#endif


/*
 * Missing BSD sys/queue.h definitions
 */

/*
 * Singly-linked Tail queue declarations.
 */
#define	STAILQ_HEAD(name, type)						\
struct name {								\
	struct type *stqh_first;/* first element */			\
	struct type **stqh_last;/* addr of last next element */		\
}

#define	STAILQ_HEAD_INITIALIZER(head)					\
	{ NULL, &(head).stqh_first }

#define	STAILQ_ENTRY(type)						\
struct {								\
	struct type *stqe_next;	/* next element */			\
}

/*
 * Singly-linked Tail queue functions.
 */
#define	STAILQ_CONCAT(head1, head2) do {				\
	if (!STAILQ_EMPTY((head2))) {					\
		*(head1)->stqh_last = (head2)->stqh_first;		\
		(head1)->stqh_last = (head2)->stqh_last;		\
		STAILQ_INIT((head2));					\
	}								\
} while (0)

#define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)

#define	STAILQ_FIRST(head)	((head)->stqh_first)

#define	STAILQ_FOREACH(var, head, field)				\
	for((var) = STAILQ_FIRST((head));				\
	   (var);							\
	   (var) = STAILQ_NEXT((var), field))

#define	STAILQ_INIT(head) do {						\
	STAILQ_FIRST((head)) = NULL;					\
	(head)->stqh_last = &STAILQ_FIRST((head));			\
} while (0)

#define	STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {		\
	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
	STAILQ_NEXT((tqelm), field) = (elm);				\
} while (0)

#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
	if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL)	\
		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
	STAILQ_FIRST((head)) = (elm);					\
} while (0)

#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
	STAILQ_NEXT((elm), field) = NULL;				\
	*(head)->stqh_last = (elm);					\
	(head)->stqh_last = &STAILQ_NEXT((elm), field);			\
} while (0)

#define	STAILQ_LAST(head, type, field)					\
	(STAILQ_EMPTY((head)) ?						\
		NULL :							\
	        ((struct type *)					\
		((char *)((head)->stqh_last) - __offsetof(struct type, field))))

#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)

#define	STAILQ_REMOVE(head, elm, type, field) do {			\
	if (STAILQ_FIRST((head)) == (elm)) {				\
		STAILQ_REMOVE_HEAD((head), field);			\
	}								\
	else {								\
		struct type *curelm = STAILQ_FIRST((head));		\
		while (STAILQ_NEXT(curelm, field) != (elm))		\
			curelm = STAILQ_NEXT(curelm, field);		\
		if ((STAILQ_NEXT(curelm, field) =			\
		     STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
			(head)->stqh_last = &STAILQ_NEXT((curelm), field);\
	}								\
} while (0)

#define	STAILQ_REMOVE_HEAD(head, field) do {				\
	if ((STAILQ_FIRST((head)) =					\
	     STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)		\
		(head)->stqh_last = &STAILQ_FIRST((head));		\
} while (0)

#define	STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {			\
	if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL)	\
		(head)->stqh_last = &STAILQ_FIRST((head));		\
} while (0)

/*
 * List declarations.
 */
#define	LIST_HEAD(name, type)						\
struct name {								\
	struct type *lh_first;	/* first element */			\
}

#define	LIST_HEAD_INITIALIZER(head)					\
	{ NULL }

#define	LIST_ENTRY(type)						\
struct {								\
	struct type *le_next;	/* next element */			\
	struct type **le_prev;	/* address of previous next element */	\
}

/*
 * List functions.
 */

#define	LIST_EMPTY(head)	((head)->lh_first == NULL)

#define	LIST_FIRST(head)	((head)->lh_first)

#define	LIST_FOREACH(var, head, field)					\
	for ((var) = LIST_FIRST((head));				\
	    (var);							\
	    (var) = LIST_NEXT((var), field))

#define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
	(elm)->field.le_prev = (listelm)->field.le_prev;		\
	LIST_NEXT((elm), field) = (listelm);				\
	*(listelm)->field.le_prev = (elm);				\
	(listelm)->field.le_prev = &LIST_NEXT((elm), field);		\
} while (0)

#define	LIST_NEXT(elm, field)	((elm)->field.le_next)

#define	TAILQ_FIRST(head)	((head)->tqh_first)

#define	TAILQ_FOREACH(var, head, field)					\
	for ((var) = TAILQ_FIRST((head));				\
	    (var);							\
	    (var) = TAILQ_NEXT((var), field))

#define	TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)

#define	TAILQ_HEAD_INITIALIZER(head)					\
	{ NULL, &(head).tqh_first }


#include <stdio.h>
#include <sys/types.h>
#include <string.h>

#define ICMP_TRACEROUTE         30              /* traceroute */
#define ICMP_DATACONVERR        31              /* data conversion error */
#define ICMP_MOBILE_REDIRECT    32              /* mobile host redirect */
#define ICMP_IPV6_WHEREAREYOU   33              /* IPv6 where-are-you */
#define ICMP_IPV6_IAMHERE       34              /* IPv6 i-am-here */
#define ICMP_MOBILE_REGREQUEST  35              /* mobile registration req */
#define ICMP_MOBILE_REGREPLY    36              /* mobile registration reply */
#define ICMP_SKIP               39              /* SKIP */
#define ICMP_PHOTURIS           40              /* Photuris */
#define MLD_LISTENER_QUERY              130     /* multicast listener query */
#define MLD_LISTENER_REPORT             131     /* multicast listener report */
#define MLD_LISTENER_DONE               132     /* multicast listener done */
#define ICMP6_ROUTER_RENUMBERING     138     /* router renumbering */
#define ICMP6_WRUREQUEST             139     /* who are you request */
#define ICMP6_WRUREPLY                       140     /* who are you reply */
#define ICMP6_FQDN_QUERY                139     /* FQDN query */
#define ICMP6_FQDN_REPLY                140     /* FQDN reply */
#define ICMP6_NI_QUERY                  139     /* node information request */
#define ICMP6_NI_REPLY                  140     /* node information reply */
#define MLD_MTRACE_RESP                 200     /* mtrace resp (to sender) */
#define MLD_MTRACE                      201     /* mtrace messages */
#define         ICMP_ROUTERADVERT_NORMAL                0       /* normal advertisement */
#define         ICMP_ROUTERADVERT_NOROUTE_COMMON        16      /* selective routing */
#define         ICMP_PHOTURIS_UNKNOWN_INDEX     1       /* unknown sec index */
#define         ICMP_PHOTURIS_AUTH_FAILED       2       /* auth failed */
#define         ICMP_PHOTURIS_DECRYPT_FAILED    3       /* decrypt failed */
#define ICMP6_DST_UNREACH_BEYONDSCOPE   2       /* beyond scope of source address */
#define ND_REDIRECT_ONLINK      0       /* redirect to an on-link node */
#define ND_REDIRECT_ROUTER      1       /* redirect to a better router */


#define GID_MAX         UINT_MAX        /* max value for a gid_t */
#define UID_MAX         UINT_MAX        /* max value for a uid_t */

#define SIZE_T_MAX      __SIZE_T_MAX    /* max value for a size_t */

// This depends on the arch, so this must be ported in other manner
#define       __SIZE_T_MAX    __UINT_MAX      /* max value for a size_t */
#define       __UINT_MAX      0xffffffffU     /* max value for an unsigned int */

#define    ICMP_ALTHOSTADDR        6               /* alternate host address */

#define       HTONL(x)        (x) = htonl((__uint32_t)(x))

#if _BYTE_ORDER == _LITTLE_ENDIAN

#define be64toh(x)      bswap64((x))
#else /* _BYTE_ORDER != _LITTLE_ENDIAN */

#define be64toh(x)      ((uint64_t)(x))
#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */

#define bswap64(x)      __bswap64(x)

extern time_t time (time_t *__timer) __THROW;


static __inline __uint64_t
__bswap64(__uint64_t _x)
{
        return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
            ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
            ((_x << 24) & ((__uint64_t)0xff << 40)) |
            ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
}


size_t strlcpy(char *dst, const char *src, size_t siz);
size_t strlcat(char *dst, const char *src, size_t siz);
char * fgetln(FILE *fp, size_t *lenp);

u_int32_t arc4random(void);


struct __sbuf {
        unsigned char *_base;
        int     _size;
};


// Directly from FreeBSD stdio.h

//#define    __SMOD  0x2000          /* true => fgetln modified _p text */
//#define    __SLBF  0x0001          /* line buffered */
//#define    __SWR   0x0008          /* OK to write */
//#define    __SEOF  0x0020          /* found EOF */
//#define    __SRD   0x0004          /* OK to read */
//#define    __SRW   0x0010          /* open for reading & writing */
//#define    __SERR  0x0040          /* found error */
//#define    __SNBF  0x0002          /* unbuffered */
//#define    __SIGN  0x8000          /* ignore this file in _fwalk */

#endif