* get your errors from WSAGetLastError, not errno. (If you supply a
* socket of -1, we check WSAGetLastError, but don't correct
* WSAEWOULDBLOCKs.)
+ *
+ * The upshot of all of this is that when a socket call fails, you
+ * should call tor_socket_errno <em>at most once</em> on the failing
+ * socket to get the error.
*/
#ifdef MS_WINDOWS
int tor_socket_errno(int sock)
// log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
read_result = recv(s, buf->mem+buf->datalen, at_most, 0);
if (read_result < 0) {
- if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */
+ int e = tor_socket_errno(s);
+ if(!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
return -1;
}
return 0; /* would block. */
write_result = send(s, buf->mem, *buf_flushlen, 0);
if (write_result < 0) {
- if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */
+ int e = tor_socket_errno(s);
+ if(!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
return -1;
}
log_fn(LOG_DEBUG,"write() would block, returning.");
log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port);
if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
- if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) {
+ int e = tor_socket_errno(s);
+ if(!ERRNO_IS_CONN_EINPROGRESS(e)) {
/* yuck. kill it. */
log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,
- tor_socket_strerror(tor_socket_errno(s)));
+ tor_socket_strerror(e));
tor_close_socket(s);
return -1;
} else {
/* let catch() handle things like ^c, and otherwise don't worry about it */
if (poll_result < 0) {
+ int e = tor_socket_errno(-1);
/* let the program survive things like ^z */
- if(tor_socket_errno(-1) != EINTR) {
+ if(e != EINTR) {
log_fn(LOG_ERR,"poll failed: %s [%d]",
- tor_socket_strerror(tor_socket_errno(-1)),
- tor_socket_errno(-1));
+ tor_socket_strerror(e), e);
return -1;
} else {
log_fn(LOG_DEBUG,"poll interrupted.");