1 /*	$OpenBSD: evutil.c,v 1.3 2010/07/12 18:03:38 nicm Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #ifdef WIN32
34 #include <winsock2.h>
35 #define WIN32_LEAN_AND_MEAN
36 #include <windows.h>
37 #undef WIN32_LEAN_AND_MEAN
38 #endif
39 
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50 #ifdef HAVE_STDLIB_H
51 #include <stdlib.h>
52 #endif
53 #include <errno.h>
54 #if defined WIN32 && !defined(HAVE_GETTIMEOFDAY_H)
55 #include <sys/timeb.h>
56 #endif
57 #include <stdio.h>
58 #include <signal.h>
59 
60 #include <sys/queue.h>
61 #include "event.h"
62 #include "event-internal.h"
63 #include "evutil.h"
64 #include "log.h"
65 
66 __RCSID("$MirOS: src/lib/libevent/evutil.c,v 1.2 2012/10/19 19:58:18 tg Exp $");
67 __RCSID(_EVENT_H_);
68 __RCSID(_EVUTIL_H_);
69 
70 int
evutil_socketpair(int family,int type,int protocol,int fd[2])71 evutil_socketpair(int family, int type, int protocol, int fd[2])
72 {
73 #ifndef WIN32
74 	return socketpair(family, type, protocol, fd);
75 #else
76 	/* This code is originally from Tor.  Used with permission. */
77 
78 	/* This socketpair does not work when localhost is down. So
79 	 * it's really not the same thing at all. But it's close enough
80 	 * for now, and really, when localhost is down sometimes, we
81 	 * have other problems too.
82 	 */
83 	int listener = -1;
84 	int connector = -1;
85 	int acceptor = -1;
86 	struct sockaddr_in listen_addr;
87 	struct sockaddr_in connect_addr;
88 	int size;
89 	int saved_errno = -1;
90 
91 	if (protocol
92 #ifdef AF_UNIX
93 		|| family != AF_UNIX
94 #endif
95 		) {
96 		EVUTIL_SET_SOCKET_ERROR(WSAEAFNOSUPPORT);
97 		return -1;
98 	}
99 	if (!fd) {
100 		EVUTIL_SET_SOCKET_ERROR(WSAEINVAL);
101 		return -1;
102 	}
103 
104 	listener = socket(AF_INET, type, 0);
105 	if (listener < 0)
106 		return -1;
107 	memset(&listen_addr, 0, sizeof(listen_addr));
108 	listen_addr.sin_family = AF_INET;
109 	listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
110 	listen_addr.sin_port = 0;	/* kernel chooses port.	 */
111 	if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
112 		== -1)
113 		goto tidy_up_and_fail;
114 	if (listen(listener, 1) == -1)
115 		goto tidy_up_and_fail;
116 
117 	connector = socket(AF_INET, type, 0);
118 	if (connector < 0)
119 		goto tidy_up_and_fail;
120 	/* We want to find out the port number to connect to.  */
121 	size = sizeof(connect_addr);
122 	if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
123 		goto tidy_up_and_fail;
124 	if (size != sizeof (connect_addr))
125 		goto abort_tidy_up_and_fail;
126 	if (connect(connector, (struct sockaddr *) &connect_addr,
127 				sizeof(connect_addr)) == -1)
128 		goto tidy_up_and_fail;
129 
130 	size = sizeof(listen_addr);
131 	acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
132 	if (acceptor < 0)
133 		goto tidy_up_and_fail;
134 	if (size != sizeof(listen_addr))
135 		goto abort_tidy_up_and_fail;
136 	EVUTIL_CLOSESOCKET(listener);
137 	/* Now check we are talking to ourself by matching port and host on the
138 	   two sockets.	 */
139 	if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
140 		goto tidy_up_and_fail;
141 	if (size != sizeof (connect_addr)
142 		|| listen_addr.sin_family != connect_addr.sin_family
143 		|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
144 		|| listen_addr.sin_port != connect_addr.sin_port)
145 		goto abort_tidy_up_and_fail;
146 	fd[0] = connector;
147 	fd[1] = acceptor;
148 
149 	return 0;
150 
151  abort_tidy_up_and_fail:
152 	saved_errno = WSAECONNABORTED;
153  tidy_up_and_fail:
154 	if (saved_errno < 0)
155 		saved_errno = WSAGetLastError();
156 	if (listener != -1)
157 		EVUTIL_CLOSESOCKET(listener);
158 	if (connector != -1)
159 		EVUTIL_CLOSESOCKET(connector);
160 	if (acceptor != -1)
161 		EVUTIL_CLOSESOCKET(acceptor);
162 
163 	EVUTIL_SET_SOCKET_ERROR(saved_errno);
164 	return -1;
165 #endif
166 }
167 
168 int
evutil_make_socket_nonblocking(int fd)169 evutil_make_socket_nonblocking(int fd)
170 {
171 #ifdef WIN32
172 	{
173 		unsigned long nonblocking = 1;
174 		ioctlsocket(fd, FIONBIO, (unsigned long*) &nonblocking);
175 	}
176 #else
177 	{
178 		int flags;
179 		if ((flags = fcntl(fd, F_GETFL, NULL)) < 0) {
180 			event_warn("fcntl(%d, F_GETFL)", fd);
181 			return -1;
182 		}
183 		if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
184 			event_warn("fcntl(%d, F_SETFL)", fd);
185 			return -1;
186 		}
187 	}
188 #endif
189 	return 0;
190 }
191 
192 ev_int64_t
evutil_strtoll(const char * s,char ** endptr,int base)193 evutil_strtoll(const char *s, char **endptr, int base)
194 {
195 #ifdef HAVE_STRTOLL
196 	return (ev_int64_t)strtoll(s, endptr, base);
197 #elif SIZEOF_LONG == 8
198 	return (ev_int64_t)strtol(s, endptr, base);
199 #elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
200 	/* XXXX on old versions of MS APIs, we only support base
201 	 * 10. */
202 	ev_int64_t r;
203 	if (base != 10)
204 		return 0;
205 	r = (ev_int64_t) _atoi64(s);
206 	while (isspace(*s))
207 		++s;
208 	while (isdigit(*s))
209 		++s;
210 	if (endptr)
211 		*endptr = (char*) s;
212 	return r;
213 #elif defined(WIN32)
214 	return (ev_int64_t) _strtoi64(s, endptr, base);
215 #else
216 #error "I don't know how to parse 64-bit integers."
217 #endif
218 }
219 
220 #ifndef HAVE_GETTIMEOFDAY
221 int
evutil_gettimeofday(struct timeval * tv,struct timezone * tz)222 evutil_gettimeofday(struct timeval *tv, struct timezone *tz)
223 {
224 	struct _timeb tb;
225 
226 	if(tv == NULL)
227 		return -1;
228 
229 	_ftime(&tb);
230 	tv->tv_sec = (long) tb.time;
231 	tv->tv_usec = ((int) tb.millitm) * 1000;
232 	return 0;
233 }
234 #endif
235 
236 int
evutil_snprintf(char * buf,size_t buflen,const char * format,...)237 evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
238 {
239 	int r;
240 	va_list ap;
241 	va_start(ap, format);
242 	r = evutil_vsnprintf(buf, buflen, format, ap);
243 	va_end(ap);
244 	return r;
245 }
246 
247 int
evutil_vsnprintf(char * buf,size_t buflen,const char * format,va_list ap)248 evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
249 {
250 #ifdef _MSC_VER
251 	int r = _vsnprintf(buf, buflen, format, ap);
252 	buf[buflen-1] = '\0';
253 	if (r >= 0)
254 		return r;
255 	else
256 		return _vscprintf(format, ap);
257 #else
258 	int r = vsnprintf(buf, buflen, format, ap);
259 	buf[buflen-1] = '\0';
260 	return r;
261 #endif
262 }
263 
264 static int
evutil_issetugid(void)265 evutil_issetugid(void)
266 {
267 #ifdef HAVE_ISSETUGID
268 	return issetugid();
269 #else
270 
271 #ifdef HAVE_GETEUID
272 	if (getuid() != geteuid())
273 		return 1;
274 #endif
275 #ifdef HAVE_GETEGID
276 	if (getgid() != getegid())
277 		return 1;
278 #endif
279 	return 0;
280 #endif
281 }
282 
283 const char *
evutil_getenv(const char * varname)284 evutil_getenv(const char *varname)
285 {
286 	if (evutil_issetugid())
287 		return NULL;
288 
289 	return getenv(varname);
290 }
291