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>
61 evutil_socketpair(
int family,
int type,
int protocol,
int fd[2])
64 return socketpair(family, type, protocol, fd);
76 struct sockaddr_in listen_addr;
77 struct sockaddr_in connect_addr;
86 EVUTIL_SET_SOCKET_ERROR(WSAEAFNOSUPPORT);
90 EVUTIL_SET_SOCKET_ERROR(WSAEINVAL);
94 listener = socket(AF_INET, type, 0);
97 memset(&listen_addr, 0,
sizeof(listen_addr));
98 listen_addr.sin_family = AF_INET;
99 listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
100 listen_addr.sin_port = 0;
101 if (bind(listener, (
struct sockaddr *) &listen_addr,
sizeof (listen_addr))
103 goto tidy_up_and_fail;
104 if (listen(listener, 1) == -1)
105 goto tidy_up_and_fail;
107 connector = socket(AF_INET, type, 0);
109 goto tidy_up_and_fail;
111 size =
sizeof(connect_addr);
112 if (getsockname(listener, (
struct sockaddr *) &connect_addr, &size) == -1)
113 goto tidy_up_and_fail;
114 if (size !=
sizeof (connect_addr))
115 goto abort_tidy_up_and_fail;
116 if (connect(connector, (
struct sockaddr *) &connect_addr,
117 sizeof(connect_addr)) == -1)
118 goto tidy_up_and_fail;
120 size =
sizeof(listen_addr);
121 acceptor = accept(listener, (
struct sockaddr *) &listen_addr, &size);
123 goto tidy_up_and_fail;
124 if (size !=
sizeof(listen_addr))
125 goto abort_tidy_up_and_fail;
126 EVUTIL_CLOSESOCKET(listener);
129 if (getsockname(connector, (
struct sockaddr *) &connect_addr, &size) == -1)
130 goto tidy_up_and_fail;
131 if (size !=
sizeof (connect_addr)
132 || listen_addr.sin_family != connect_addr.sin_family
133 || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
134 || listen_addr.sin_port != connect_addr.sin_port)
135 goto abort_tidy_up_and_fail;
141 abort_tidy_up_and_fail:
142 saved_errno = WSAECONNABORTED;
145 saved_errno = WSAGetLastError();
147 EVUTIL_CLOSESOCKET(listener);
149 EVUTIL_CLOSESOCKET(connector);
151 EVUTIL_CLOSESOCKET(acceptor);
153 EVUTIL_SET_SOCKET_ERROR(saved_errno);
159 evutil_make_socket_nonblocking(
int fd)
163 unsigned long nonblocking = 1;
164 ioctlsocket(fd, FIONBIO, (
unsigned long*) &nonblocking);
167 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
168 event_warn(
"fcntl(O_NONBLOCK)");
176 evutil_strtoll(
const char *s,
char **endptr,
int base)
179 return (ev_int64_t)strtoll(s, endptr, base);
180 #elif SIZEOF_LONG == 8
181 return (ev_int64_t)strtol(s, endptr, base);
182 #elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300
188 r = (ev_int64_t) _atoi64(s);
197 return (ev_int64_t) _strtoi64(s, endptr, base);
199 #error "I don't know how to parse 64-bit integers."
203 #ifndef HAVE_GETTIMEOFDAY
213 tv->tv_sec = (long) tb.time;
214 tv->tv_usec = ((
int) tb.millitm) * 1000;
220 evutil_snprintf(
char *
buf,
size_t buflen,
const char *format, ...)
224 va_start(ap, format);
225 r = evutil_vsnprintf(buf, buflen, format, ap);
231 evutil_vsnprintf(
char *
buf,
size_t buflen,
const char *format, va_list ap)
234 int r = _vsnprintf(buf, buflen, format, ap);
235 buf[buflen-1] =
'\0';
239 return _vscprintf(format, ap);
241 int r = vsnprintf(buf, buflen, format, ap);
242 buf[buflen-1] =
'\0';