| 6 |
| * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 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. |
| 15 | * |
| 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. |
| 26 | */ |
| 27 |
| 28 | #include "config.h" |
| 29 |
| 30 | #ifdef WIN32 |
| 31 | #include <winsock2.h> |
| 32 | #define WIN32_LEAN_AND_MEAN |
| 33 | #include <windows.h> |
| 34 | #undef WIN32_LEAN_AND_MEAN |
| 35 | #endif |
| 36 |
| 37 | #include <sys/types.h> |
| 38 | #ifdef HAVE_SYS_SOCKET_H |
| 39 | #include <sys/socket.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_UNISTD_H |
| 42 | #include <unistd.h> |
| 43 | #endif |
| 44 | #ifdef HAVE_FCNTL_H |
| 45 | #include <fcntl.h> |
| 46 | #endif |
| 47 | #ifdef HAVE_STDLIB_H |
| 48 | #include <stdlib.h> |
| 49 | #endif |
| 50 | #include <errno.h> |
| 51 | #if defined WIN32 && !defined(HAVE_GETTIMEOFDAY_H) |
| 52 | #include <sys/timeb.h> |
| 53 | #endif |
| 54 | #include <stdio.h> |
| 55 | #include <signal.h> |
| 56 |
| 57 | #include <sys/queue.h> |
| 58 | #include "event.h" |
| 59 | #include "event-internal.h" |
| 60 | #include "evutil.h" |
| 61 | #include "log.h" |
| 62 |
| 63 | int |
| 64 | evutil_socketpair(int family, int type, int protocol, int fd[2]) |
| 65 | { |
| 66 | #ifndef WIN32 |
| 67 | return socketpair(family, type, protocol, fd); |
| 68 | #else |
| 69 | /* This code is originally from Tor. Used with permission. */ |
| 70 |
| 71 | /* This socketpair does not work when localhost is down. So |
| 72 | * it's really not the same thing at all. But it's close enough |
| 73 | * for now, and really, when localhost is down sometimes, we |
| 74 | * have other problems too. |
| 75 | */ |
| 76 | int listener = -1; |
| 77 | int connector = -1; |
| 78 | int acceptor = -1; |
| 79 | struct sockaddr_in listen_addr; |
| 80 | struct sockaddr_in connect_addr; |
| 81 | int size; |
| 82 | int saved_errno = -1; |
| 83 |
| 84 | if (protocol |
| 85 | #ifdef AF_UNIX |
| 86 | || family != AF_UNIX |
| 87 | #endif |
| 88 | ) { |
| 89 | EVUTIL_SET_SOCKET_ERROR(WSAEAFNOSUPPORT); |
| 90 | return -1; |
| 91 | } |
| 92 | if (!fd) { |
| 93 | EVUTIL_SET_SOCKET_ERROR(WSAEINVAL); |
| 94 | return -1; |
| 95 | } |
| 96 |
| 97 | listener = socket(AF_INET, type, 0); |
| 98 | if (listener < 0) |
| 99 | return -1; |
| 100 | memset(&listen_addr, 0, sizeof(listen_addr)); |
| 101 | listen_addr.sin_family = AF_INET; |
| 102 | listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 103 | listen_addr.sin_port = 0; /* kernel chooses port. */ |
| 104 | if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr)) |
| 105 | == -1) |
| 106 | goto tidy_up_and_fail; |
| 107 | if (listen(listener, 1) == -1) |
| 108 | goto tidy_up_and_fail; |
| 109 |
| 110 | connector = socket(AF_INET, type, 0); |
| 111 | if (connector < 0) |
| 112 | goto tidy_up_and_fail; |
| 113 | /* We want to find out the port number to connect to. */ |
| 114 | size = sizeof(connect_addr); |
| 115 | if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1) |
| 116 | goto tidy_up_and_fail; |
| 117 | if (size != sizeof (connect_addr)) |
| 118 | goto abort_tidy_up_and_fail; |
| 119 | if (connect(connector, (struct sockaddr *) &connect_addr, |
| 120 | sizeof(connect_addr)) == -1) |
| 121 | goto tidy_up_and_fail; |
| 122 |
| 123 | size = sizeof(listen_addr); |
| 124 | acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size); |
| 125 | if (acceptor < 0) |
| 126 | goto tidy_up_and_fail; |
| 127 | if (size != sizeof(listen_addr)) |
| 128 | goto abort_tidy_up_and_fail; |
| 129 | EVUTIL_CLOSESOCKET(listener); |
| 130 | /* Now check we are talking to ourself by matching port and host on the |
| 131 | two sockets. */ |
| 132 | if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1) |
| 133 | goto tidy_up_and_fail; |
| 134 | if (size != sizeof (connect_addr) |
| 135 | || listen_addr.sin_family != connect_addr.sin_family |
| 136 | || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr |
| 137 | || listen_addr.sin_port != connect_addr.sin_port) |
| 138 | goto abort_tidy_up_and_fail; |
| 139 | fd[0] = connector; |
| 140 | fd[1] = acceptor; |
| 141 |
| 142 | return 0; |
| 143 |
| 144 | abort_tidy_up_and_fail: |
| 145 | saved_errno = WSAECONNABORTED; |
| 146 | tidy_up_and_fail: |
| 147 | if (saved_errno < 0) |
| 148 | saved_errno = WSAGetLastError(); |
| 149 | if (listener != -1) |
| 150 | EVUTIL_CLOSESOCKET(listener); |
| 151 | if (connector != -1) |
| 152 | EVUTIL_CLOSESOCKET(connector); |
| 153 | if (acceptor != -1) |
| 154 | EVUTIL_CLOSESOCKET(acceptor); |
| 155 |
| 156 | EVUTIL_SET_SOCKET_ERROR(saved_errno); |
| 157 | return -1; |
| 158 | #endif |
| 159 | } |
| 160 |
| 161 | int |
| 162 | evutil_make_socket_nonblocking(int fd) |
| 163 | { |
| 164 | #ifdef WIN32 |
| 165 | { |
| 166 | unsigned long nonblocking = 1; |
| 167 | ioctlsocket(fd, FIONBIO, (unsigned long*) &nonblocking); |
| 168 | } |
| 169 | #else |
| 170 | { |
| 171 | int flags; |
| 172 | if ((flags = fcntl(fd, F_GETFL, NULL)) < 0) { |
| 173 | event_warn("fcntl(%d, F_GETFL)", fd); |
| 174 | return -1; |
| 175 | } |
| 176 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { |
| 177 | event_warn("fcntl(%d, F_SETFL)", fd); |
| 178 | return -1; |
| 179 | } |
| 180 | } |
| 181 | #endif |
| 182 | return 0; |
| 183 | } |
| 184 |
| 185 | ev_int64_t |
| 186 | evutil_strtoll(const char *s, char **endptr, int base) |
| 187 | { |
| 188 | #ifdef HAVE_STRTOLL |
| 189 | return (ev_int64_t)strtoll(s, endptr, base); |
| 190 | #elif SIZEOF_LONG == 8 |
| 191 | return (ev_int64_t)strtol(s, endptr, base); |
| 192 | #elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300 |
| 193 | /* XXXX on old versions of MS APIs, we only support base |
| 194 | * 10. */ |
| 195 | ev_int64_t r; |
| 196 | if (base != 10) |
| 197 | return 0; |
| 198 | r = (ev_int64_t) _atoi64(s); |
| 199 | while (isspace(*s)) |
| 200 | ++s; |
| 201 | while (isdigit(*s)) |
| 202 | ++s; |
| 203 | if (endptr) |
| 204 | *endptr = (char*) s; |
| 205 | return r; |
| 206 | #elif defined(WIN32) |
| 207 | return (ev_int64_t) _strtoi64(s, endptr, base); |
| 208 | #else |
| 209 | #error "I don't know how to parse 64-bit integers." |
| 210 | #endif |
| 211 | } |
| 212 |
| 213 | #ifndef _EVENT_HAVE_GETTIMEOFDAY |
| 214 | int |
| 215 | evutil_gettimeofday(struct timeval *tv, struct timezone *tz) |
| 216 | { |
| 217 | struct _timeb tb; |
| 218 |
| 219 | if(tv == NULL) |
| 220 | return -1; |
| 221 |
| 222 | _ftime(&tb); |
| 223 | tv->tv_sec = (long) tb.time; |
| 224 | tv->tv_usec = ((int) tb.millitm) * 1000; |
| 225 | return 0; |
| 226 | } |
| 227 | #endif |
| 228 |
| 229 | int |
| 230 | evutil_snprintf(char *buf, size_t buflen, const char *format, ...) |
| 231 | { |
| 232 | int r; |
| 233 | va_list ap; |
| 234 | va_start(ap, format); |
| 235 | r = evutil_vsnprintf(buf, buflen, format, ap); |
| 236 | va_end(ap); |
| 237 | return r; |
| 238 | } |
| 239 |
| 240 | int |
| 241 | evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap) |
| 242 | { |
| 243 | #ifdef _MSC_VER |
| 244 | int r = _vsnprintf(buf, buflen, format, ap); |
| 245 | buf[buflen-1] = '\0'; |
| 246 | if (r >= 0) |
| 247 | return r; |
| 248 | else |
| 249 | return _vscprintf(format, ap); |
| 250 | #else |
| 251 | int r = vsnprintf(buf, buflen, format, ap); |
| 252 | buf[buflen-1] = '\0'; |
| 253 | return r; |
| 254 | #endif |
| 255 | } |
| 256 |
| 257 | static int |
| 258 | evutil_issetugid(void) |
| 259 | { |
| 260 | #ifdef _EVENT_HAVE_ISSETUGID |
| 261 | return issetugid(); |
| 262 | #else |
| 263 |
| 264 | #ifdef _EVENT_HAVE_GETEUID |
| 265 | if (getuid() != geteuid()) |
| 266 | return 1; |
| 267 | #endif |
| 268 | #ifdef _EVENT_HAVE_GETEGID |
| 269 | if (getgid() != getegid()) |
| 270 | return 1; |
| 271 | #endif |
| 272 | return 0; |
| 273 | #endif |
| 274 | } |
| 275 |
| 276 | const char * |
| 277 | evutil_getenv(const char *varname) |
| 278 | { |
| 279 | if (evutil_issetugid()) |
| 280 | return NULL; |
| 281 |
| 282 | return getenv(varname); |
| 283 | } |
| 284 |