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