3 * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #define WIN32_LEAN_AND_MEAN
36 #undef WIN32_LEAN_AND_MEAN
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
53 #if defined WIN32 && !defined(HAVE_GETTIMEOFDAY_H)
54 #include <sys/timeb.h>
62 evutil_socketpair(int family
, int type
, int protocol
, int fd
[2])
65 return socketpair(family
, type
, protocol
, fd
);
67 /* This code is originally from Tor. Used with permission. */
69 /* This socketpair does not work when localhost is down. So
70 * it's really not the same thing at all. But it's close enough
71 * for now, and really, when localhost is down sometimes, we
72 * have other problems too.
77 struct sockaddr_in listen_addr
;
78 struct sockaddr_in connect_addr
;
87 EVUTIL_SET_SOCKET_ERROR(WSAEAFNOSUPPORT
);
91 EVUTIL_SET_SOCKET_ERROR(WSAEINVAL
);
95 listener
= socket(AF_INET
, type
, 0);
98 memset(&listen_addr
, 0, sizeof(listen_addr
));
99 listen_addr
.sin_family
= AF_INET
;
100 listen_addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
101 listen_addr
.sin_port
= 0; /* kernel chooses port. */
102 if (bind(listener
, (struct sockaddr
*) &listen_addr
, sizeof (listen_addr
))
104 goto tidy_up_and_fail
;
105 if (listen(listener
, 1) == -1)
106 goto tidy_up_and_fail
;
108 connector
= socket(AF_INET
, type
, 0);
110 goto tidy_up_and_fail
;
111 /* We want to find out the port number to connect to. */
112 size
= sizeof(connect_addr
);
113 if (getsockname(listener
, (struct sockaddr
*) &connect_addr
, &size
) == -1)
114 goto tidy_up_and_fail
;
115 if (size
!= sizeof (connect_addr
))
116 goto abort_tidy_up_and_fail
;
117 if (connect(connector
, (struct sockaddr
*) &connect_addr
,
118 sizeof(connect_addr
)) == -1)
119 goto tidy_up_and_fail
;
121 size
= sizeof(listen_addr
);
122 acceptor
= accept(listener
, (struct sockaddr
*) &listen_addr
, &size
);
124 goto tidy_up_and_fail
;
125 if (size
!= sizeof(listen_addr
))
126 goto abort_tidy_up_and_fail
;
127 EVUTIL_CLOSESOCKET(listener
);
128 /* Now check we are talking to ourself by matching port and host on the
130 if (getsockname(connector
, (struct sockaddr
*) &connect_addr
, &size
) == -1)
131 goto tidy_up_and_fail
;
132 if (size
!= sizeof (connect_addr
)
133 || listen_addr
.sin_family
!= connect_addr
.sin_family
134 || listen_addr
.sin_addr
.s_addr
!= connect_addr
.sin_addr
.s_addr
135 || listen_addr
.sin_port
!= connect_addr
.sin_port
)
136 goto abort_tidy_up_and_fail
;
142 abort_tidy_up_and_fail
:
143 saved_errno
= WSAECONNABORTED
;
146 saved_errno
= WSAGetLastError();
148 EVUTIL_CLOSESOCKET(listener
);
150 EVUTIL_CLOSESOCKET(connector
);
152 EVUTIL_CLOSESOCKET(acceptor
);
154 EVUTIL_SET_SOCKET_ERROR(saved_errno
);
160 evutil_make_socket_nonblocking(int fd
)
164 unsigned long nonblocking
= 1;
165 ioctlsocket(fd
, FIONBIO
, (unsigned long*) &nonblocking
);
168 if (fcntl(fd
, F_SETFL
, O_NONBLOCK
) == -1) {
169 event_warn("fcntl(O_NONBLOCK)");
177 evutil_strtoll(const char *s
, char **endptr
, int base
)
180 return (ev_int64_t
)strtoll(s
, endptr
, base
);
181 #elif SIZEOF_LONG == 8
182 return (ev_int64_t
)strtol(s
, endptr
, base
);
183 #elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
184 /* XXXX on old versions of MS APIs, we only support base
189 r
= (ev_int64_t
) _atoi64(s
);
198 return (ev_int64_t
) _strtoi64(s
, endptr
, base
);
200 #error "I don't know how to parse 64-bit integers."
204 #ifndef _EVENT_HAVE_GETTIMEOFDAY
206 evutil_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
214 tv
->tv_sec
= (long) tb
.time
;
215 tv
->tv_usec
= ((int) tb
.millitm
) * 1000;
221 evutil_snprintf(char *buf
, size_t buflen
, const char *format
, ...)
225 va_start(ap
, format
);
226 r
= evutil_vsnprintf(buf
, buflen
, format
, ap
);
232 evutil_vsnprintf(char *buf
, size_t buflen
, const char *format
, va_list ap
)
235 int r
= _vsnprintf(buf
, buflen
, format
, ap
);
236 buf
[buflen
-1] = '\0';
240 return _vscprintf(format
, ap
);
242 int r
= vsnprintf(buf
, buflen
, format
, ap
);
243 buf
[buflen
-1] = '\0';