1*c2c66affSColin Finck
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck * adns_unix_calls.c
4*c2c66affSColin Finck * - Simple implementation of requiered UNIX system calls and
5*c2c66affSColin Finck * library functions.
6*c2c66affSColin Finck */
7*c2c66affSColin Finck /*
8*c2c66affSColin Finck * This file is
9*c2c66affSColin Finck * Copyright (C) 2000, 2002 Jarle (jgaa) Aase <jgaa@jgaa.com>
10*c2c66affSColin Finck *
11*c2c66affSColin Finck * It is part of adns, which is
12*c2c66affSColin Finck * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
13*c2c66affSColin Finck * Copyright (C) 1999 Tony Finch <dot@dotat.at>
14*c2c66affSColin Finck *
15*c2c66affSColin Finck * This program is free software; you can redistribute it and/or modify
16*c2c66affSColin Finck * it under the terms of the GNU General Public License as published by
17*c2c66affSColin Finck * the Free Software Foundation; either version 2, or (at your option)
18*c2c66affSColin Finck * any later version.
19*c2c66affSColin Finck *
20*c2c66affSColin Finck * This program is distributed in the hope that it will be useful,
21*c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
22*c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23*c2c66affSColin Finck * GNU General Public License for more details.
24*c2c66affSColin Finck *
25*c2c66affSColin Finck * You should have received a copy of the GNU General Public License
26*c2c66affSColin Finck * along with this program; if not, write to the Free Software Foundation,
27*c2c66affSColin Finck * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28*c2c66affSColin Finck */
29*c2c66affSColin Finck
30*c2c66affSColin Finck
31*c2c66affSColin Finck #include "adns.h"
32*c2c66affSColin Finck
adns_writev(int FileDescriptor,const struct iovec * iov,int iovCount)33*c2c66affSColin Finck int adns_writev(int FileDescriptor, const struct iovec * iov, int iovCount)
34*c2c66affSColin Finck {
35*c2c66affSColin Finck size_t total_len = 0;
36*c2c66affSColin Finck int i = 0, r = 0;
37*c2c66affSColin Finck char *buf = NULL, *p = NULL;
38*c2c66affSColin Finck
39*c2c66affSColin Finck for(; i < iovCount; i++)
40*c2c66affSColin Finck total_len += iov[i].iov_len;
41*c2c66affSColin Finck
42*c2c66affSColin Finck p = buf = (char *)_alloca(total_len);
43*c2c66affSColin Finck
44*c2c66affSColin Finck for(; i < iovCount; i++)
45*c2c66affSColin Finck {
46*c2c66affSColin Finck memcpy(p, iov[i].iov_base, iov[i].iov_len);
47*c2c66affSColin Finck p += iov[i].iov_len;
48*c2c66affSColin Finck }
49*c2c66affSColin Finck
50*c2c66affSColin Finck ADNS_CLEAR_ERRNO
51*c2c66affSColin Finck r = send(FileDescriptor, buf, total_len, 0);
52*c2c66affSColin Finck ADNS_CAPTURE_ERRNO;
53*c2c66affSColin Finck return r;
54*c2c66affSColin Finck }
55*c2c66affSColin Finck
adns_inet_aton(const char * cp,struct in_addr * inp)56*c2c66affSColin Finck int adns_inet_aton(const char *cp, struct in_addr *inp)
57*c2c66affSColin Finck {
58*c2c66affSColin Finck if (!cp || !*cp || !inp)
59*c2c66affSColin Finck {
60*c2c66affSColin Finck errno = EINVAL;
61*c2c66affSColin Finck return -1;
62*c2c66affSColin Finck }
63*c2c66affSColin Finck
64*c2c66affSColin Finck if (!strcmp(cp, "255.255.255.255"))
65*c2c66affSColin Finck {
66*c2c66affSColin Finck // Special case
67*c2c66affSColin Finck inp->s_addr = INADDR_NONE;
68*c2c66affSColin Finck return 0;
69*c2c66affSColin Finck }
70*c2c66affSColin Finck
71*c2c66affSColin Finck inp->s_addr = inet_addr(cp);
72*c2c66affSColin Finck return (inp->s_addr == INADDR_NONE) ? -1 : 0;
73*c2c66affSColin Finck }
74*c2c66affSColin Finck
adns_getpid()75*c2c66affSColin Finck int adns_getpid()
76*c2c66affSColin Finck {
77*c2c66affSColin Finck return GetCurrentProcessId();
78*c2c66affSColin Finck }
79*c2c66affSColin Finck
gettimeofday(struct timeval * tv,struct timezone * tz)80*c2c66affSColin Finck int gettimeofday(struct timeval *tv, struct timezone *tz)
81*c2c66affSColin Finck {
82*c2c66affSColin Finck static __int64 Adjustment;
83*c2c66affSColin Finck __int64 now = 0;
84*c2c66affSColin Finck
85*c2c66affSColin Finck if (!Adjustment)
86*c2c66affSColin Finck {
87*c2c66affSColin Finck SYSTEMTIME st = {1970,1,0,1,0,0,0};
88*c2c66affSColin Finck SystemTimeToFileTime(&st, (LPFILETIME)&Adjustment);
89*c2c66affSColin Finck }
90*c2c66affSColin Finck
91*c2c66affSColin Finck if (tz)
92*c2c66affSColin Finck {
93*c2c66affSColin Finck errno = EINVAL;
94*c2c66affSColin Finck return -1;
95*c2c66affSColin Finck }
96*c2c66affSColin Finck
97*c2c66affSColin Finck GetSystemTimeAsFileTime((LPFILETIME)&now);
98*c2c66affSColin Finck now -= Adjustment;
99*c2c66affSColin Finck
100*c2c66affSColin Finck tv->tv_sec = (long)(now / 10000000);
101*c2c66affSColin Finck tv->tv_usec = (long)((now % 10000000) / 10);
102*c2c66affSColin Finck
103*c2c66affSColin Finck return 0;
104*c2c66affSColin Finck }
105*c2c66affSColin Finck
106*c2c66affSColin Finck /* Memory allocated in the DLL must be freed in the dll, so
107*c2c66affSColin Finck we provide memory manegement functions. */
108*c2c66affSColin Finck
109*c2c66affSColin Finck #ifdef ADNS_DLL
110*c2c66affSColin Finck
111*c2c66affSColin Finck #undef malloc
112*c2c66affSColin Finck #undef realloc
113*c2c66affSColin Finck #undef free
114*c2c66affSColin Finck
adns_malloc(const size_t bytes)115*c2c66affSColin Finck void *adns_malloc(const size_t bytes)
116*c2c66affSColin Finck {
117*c2c66affSColin Finck return malloc(bytes);
118*c2c66affSColin Finck }
119*c2c66affSColin Finck
adns_realloc(void * ptr,const size_t bytes)120*c2c66affSColin Finck void *adns_realloc(void *ptr, const size_t bytes)
121*c2c66affSColin Finck {
122*c2c66affSColin Finck return realloc(ptr, bytes);
123*c2c66affSColin Finck }
124*c2c66affSColin Finck
adns_free(void * ptr)125*c2c66affSColin Finck void adns_free(void *ptr)
126*c2c66affSColin Finck {
127*c2c66affSColin Finck free(ptr);
128*c2c66affSColin Finck }
129*c2c66affSColin Finck
130*c2c66affSColin Finck #endif /* ADNS_DLL */
131