2 * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #define WIN32_LEAN_AND_MEAN
35 #undef WIN32_LEAN_AND_MEAN
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
52 #if defined WIN32 && !defined(HAVE_GETTIMEOFDAY_H)
53 #include <sys/timeb.h>
58 #include <sys/queue.h>
60 #include "event-internal.h"
65 evutil_socketpair(int family, int type, int protocol, int fd[2])
68 return socketpair(family, type, protocol, fd);
70 /* This code is originally from Tor. Used with permission. */
72 /* This socketpair does not work when localhost is down. So
73 * it's really not the same thing at all. But it's close enough
74 * for now, and really, when localhost is down sometimes, we
75 * have other problems too.
80 struct sockaddr_in listen_addr;
81 struct sockaddr_in connect_addr;
90 EVUTIL_SET_SOCKET_ERROR(WSAEAFNOSUPPORT);
94 EVUTIL_SET_SOCKET_ERROR(WSAEINVAL);
98 listener = socket(AF_INET, type, 0);
101 memset(&listen_addr, 0, sizeof(listen_addr));
102 listen_addr.sin_family = AF_INET;
103 listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
104 listen_addr.sin_port = 0; /* kernel chooses port. */
105 if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
107 goto tidy_up_and_fail;
108 if (listen(listener, 1) == -1)
109 goto tidy_up_and_fail;
111 connector = socket(AF_INET, type, 0);
113 goto tidy_up_and_fail;
114 /* We want to find out the port number to connect to. */
115 size = sizeof(connect_addr);
116 if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
117 goto tidy_up_and_fail;
118 if (size != sizeof (connect_addr))
119 goto abort_tidy_up_and_fail;
120 if (connect(connector, (struct sockaddr *) &connect_addr,
121 sizeof(connect_addr)) == -1)
122 goto tidy_up_and_fail;
124 size = sizeof(listen_addr);
125 acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
127 goto tidy_up_and_fail;
128 if (size != sizeof(listen_addr))
129 goto abort_tidy_up_and_fail;
130 EVUTIL_CLOSESOCKET(listener);
131 /* Now check we are talking to ourself by matching port and host on the
133 if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
134 goto tidy_up_and_fail;
135 if (size != sizeof (connect_addr)
136 || listen_addr.sin_family != connect_addr.sin_family
137 || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
138 || listen_addr.sin_port != connect_addr.sin_port)
139 goto abort_tidy_up_and_fail;
145 abort_tidy_up_and_fail:
146 saved_errno = WSAECONNABORTED;
149 saved_errno = WSAGetLastError();
151 EVUTIL_CLOSESOCKET(listener);
153 EVUTIL_CLOSESOCKET(connector);
155 EVUTIL_CLOSESOCKET(acceptor);
157 EVUTIL_SET_SOCKET_ERROR(saved_errno);
163 evutil_make_socket_nonblocking(int fd)
167 unsigned long nonblocking = 1;
168 ioctlsocket(fd, FIONBIO, (unsigned long*) &nonblocking);
171 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
172 event_warn("fcntl(O_NONBLOCK)");
180 evutil_strtoll(const char *s, char **endptr, int base)
183 return (ev_int64_t)strtoll(s, endptr, base);
184 #elif SIZEOF_LONG == 8
185 return (ev_int64_t)strtol(s, endptr, base);
186 #elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
187 /* XXXX on old versions of MS APIs, we only support base
192 r = (ev_int64_t) _atoi64(s);
201 return (ev_int64_t) _strtoi64(s, endptr, base);
203 #error "I don't know how to parse 64-bit integers."
207 #ifndef _EVENT_HAVE_GETTIMEOFDAY
209 evutil_gettimeofday(struct timeval *tv, struct timezone *tz)
217 tv->tv_sec = (long) tb.time;
218 tv->tv_usec = ((int) tb.millitm) * 1000;
224 evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
228 va_start(ap, format);
229 r = evutil_vsnprintf(buf, buflen, format, ap);
235 evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
238 int r = _vsnprintf(buf, buflen, format, ap);
239 buf[buflen-1] = '\0';
243 return _vscprintf(format, ap);
245 int r = vsnprintf(buf, buflen, format, ap);
246 buf[buflen-1] = '\0';
252 evutil_issetugid(void)
254 #ifdef _EVENT_HAVE_ISSETUGID
258 #ifdef _EVENT_HAVE_GETEUID
259 if (getuid() != geteuid())
262 #ifdef _EVENT_HAVE_GETEGID
263 if (getgid() != getegid())
271 evutil_getenv(const char *varname)
273 if (evutil_issetugid())
276 return getenv(varname);