2 * Copyright (C) 2009 Luigi Rizzo, Marta Carbone, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $Id: bsd_compat.c 4508 2009-12-15 21:54:14Z luigi $
29 * kernel variables and functions that are not available in linux.
32 #include <sys/cdefs.h>
33 #include <asm/div64.h> /* do_div on 2.4 */
34 #include <linux/random.h> /* get_random_bytes on 2.4 */
37 * gettimeofday would be in sys/time.h but it is not
38 * visible if _KERNEL is defined
40 int gettimeofday(struct timeval *, struct timezone *);
42 int ticks; /* kernel ticks counter */
43 int hz = 1000; /* default clock time */
44 long tick = 1000; /* XXX is this 100000/hz ? */
46 time_t time_uptime = 0;
47 struct timeval boottime;
51 u_long in_ifaddrhmask; /* mask for hash table */
52 struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
54 u_int rt_numfibs = RT_NUMFIBS;
58 * We make pfil_head_get return a non-null pointer, which is then ignored
59 * in our 'add-hook' routines.
62 typedef int (pfil_hook_t)
63 (void *, struct mbuf **, struct ifnet *, int, struct inpcb *);
66 pfil_head_get(int proto, u_long flags)
69 return (struct pfil_head *)&dummy;
73 pfil_add_hook(pfil_hook_t *func, void *arg, int dir, struct pfil_head *h)
79 pfil_remove_hook(pfil_hook_t *func, void *arg, int dir, struct pfil_head *h)
84 /* define empty body for kernel function */
86 priv_check(struct thread *td, int priv)
92 securelevel_ge(struct ucred *cr, int level)
98 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
104 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
110 ether_demux(struct ifnet *ifp, struct mbuf *m)
116 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
122 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum)
128 icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
134 in_cksum_skip(struct mbuf *m, int len, int skip)
140 in_cksum_hdr(struct ip *ip)
146 ip_reass(struct mbuf *clone)
150 #ifdef INP_LOCK_ASSERT
151 #undef INP_LOCK_ASSERT
152 #define INP_LOCK_ASSERT(a)
156 jailed(struct ucred *cred)
162 * Return 1 if an internet address is for a ``local'' host
163 * (one to which we have a connection). If subnetsarelocal
164 * is true, this includes other subnets of the local net.
165 * Otherwise, it includes only the directly-connected (sub)nets.
168 in_localaddr(struct in_addr in)
174 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
176 size_t valsize = sopt->sopt_valsize;
179 sopt->sopt_valsize = valsize = len;
180 bcopy(buf, sopt->sopt_val, valsize);
185 * copy data from userland to kernel
188 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
190 size_t valsize = sopt->sopt_valsize;
192 if (valsize < minlen)
195 sopt->sopt_valsize = valsize = len;
196 bcopy(sopt->sopt_val, buf, valsize);
201 getmicrouptime(struct timeval *tv)
210 #include <arpa/inet.h>
213 inet_ntoa_r(struct in_addr ina, char *buf)
217 unsigned char *ucp = (unsigned char *)&ina;
219 sprintf(buf, "%d.%d.%d.%d",
229 inet_ntoa(struct in_addr ina)
232 return inet_ntoa_r(ina, buf);
242 get_random_bytes(&r, sizeof(r));
243 return r & 0x7fffffff;
249 * do_div really does a u64 / u32 bit division.
250 * we save the sign and convert to uint befor calling.
251 * We are safe just because we always call it with small operands.
254 div64(int64_t a, int64_t b)
261 int sign = ((a>0)?1:-1) * ((b>0)?1:-1);
271 * compact version of fnmatch.
274 fnmatch(const char *pattern, const char *string, int flags)
278 if (!string || !pattern)
279 return 1; /* no match */
280 while ( (s = *string++) ) {
282 if (p == '\0') /* pattern is over, no match */
284 if (p == '*') /* wildcard, match */
286 if (p == '.' || p == s) /* char match, continue */
288 return 1; /* no match */
290 /* end of string, make sure the pattern is over too */
291 if (*pattern == '\0' || *pattern == '*')
293 return 1; /* no match */
298 * as good as anywhere, place here the missing calls
304 void *_ret = ExAllocatePoolWithTag(0, size, 'wfpi');
306 memset(_ret, 0, size);
311 panic(const char *fmt, ...)
319 extern int _vsnprintf(char *buf, int buf_size, char * fmt, va_list ap);
322 * Windows' _snprintf doesn't terminate buffer with zero if size > buf_size
325 snprintf(char *buf, int buf_size, char *fmt, ...)
329 if (_vsnprintf(buf, buf_size, fmt, ap) < 0)
330 buf[buf_size - 1] = '\0';