static void
socket_set_nosigpipe (const struct GNUNET_NETWORK_Handle *h)
{
- int value = 1;
+ int abs_value = 1;
if (0 !=
- setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof (value)))
+ setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE, &abs_value, sizeof (abs_value)))
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
}
#endif
if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof (value)))
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
#else
- const char * value = "1";
- if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, value, sizeof (value)))
+ const char * abs_value = "1";
+ if (0 != setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY, abs_value, sizeof (abs_value)))
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
#endif
}
#ifdef IPPROTO_IPV6
const int on = 1;
if (desc->af == AF_INET6)
- setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on));
+ if (0 != setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)))
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
#if 0
/* is this needed or desired? or done elsewhere? */
- setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
+ if (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "setsockopt");
#endif
#endif
#endif
return GNUNET_NO;
}
+
/**
* Read data from a connected socket (always non-blocking).
* @param desc socket
#endif
}
+
+/**
+ * Return file descriptor for this network handle
+ *
+ * @param desc wrapper to process
+ * @return POSIX file descriptor
+ */
int
GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
{
return desc->fd;
}
+
/**
* Copy a native fd set
*
void GNUNET_NETWORK_fdset_set_native (struct GNUNET_NETWORK_FDSet *to,
int nfd)
{
+ GNUNET_assert(nfd >= 0);
FD_SET (nfd, &to->sds);
to->nsds = GNUNET_MAX (nfd + 1, to->nsds);
}
const struct GNUNET_DISK_FileHandle *h)
{
#ifdef MINGW
- HANDLE hw;
- GNUNET_DISK_internal_file_handle_ (h, &hw, sizeof (HANDLE));
GNUNET_CONTAINER_slist_add (fds->handles,
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
- &hw, sizeof (HANDLE));
+ h, sizeof (struct GNUNET_DISK_FileHandle));
#else
int fd;
}
-#if MINGW
-/**
- * Add a W32 file handle to the fd set
- * @param fds fd set
- * @param h the file handle to add
- */
-void
-GNUNET_NETWORK_fdset_handle_set_native_w32_handle (struct GNUNET_NETWORK_FDSet *fds,
- HANDLE h)
-{
- GNUNET_CONTAINER_slist_add (fds->handles,
- GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
- &h, sizeof (HANDLE));
-}
-#endif
-
-
/**
* Check if a file handle is part of an fd set
* @param fds fd set
{
#ifdef MINGW
- return GNUNET_CONTAINER_slist_contains (fds->handles, &h->h,
- sizeof (HANDLE));
+ return GNUNET_CONTAINER_slist_contains (fds->handles, h,
+ sizeof (struct GNUNET_DISK_FileHandle));
#else
return FD_ISSET (h->fd, &fds->sds);
#endif
GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,
const struct GNUNET_NETWORK_FDSet *fds2)
{
+#ifndef MINGW
int nfds;
nfds = fds1->nsds;
if (FD_ISSET (nfds, &fds1->sds) && FD_ISSET (nfds, &fds2->sds))
return GNUNET_YES;
}
-#ifdef MINGW
- {
- struct GNUNET_CONTAINER_SList_Iterator *it;
+#else
+ struct GNUNET_CONTAINER_SList_Iterator *it;
+ struct GNUNET_DISK_FileHandle *h;
+ int i;
+ int j;
- for (it = GNUNET_CONTAINER_slist_begin (fds1->handles);
- GNUNET_CONTAINER_slist_end (it) != GNUNET_YES;
- GNUNET_CONTAINER_slist_next (it))
- {
- HANDLE *h;
-
- h = (HANDLE *) GNUNET_CONTAINER_slist_get ((const struct GNUNET_CONTAINER_SList_Iterator *)it, NULL);
- if (GNUNET_CONTAINER_slist_contains
- (fds2->handles, h, sizeof (HANDLE)))
- {
- GNUNET_CONTAINER_slist_iter_destroy (it);
- return GNUNET_YES;
- }
- }
- GNUNET_CONTAINER_slist_iter_destroy (it);
+ /*This code is somewhat hacky, we are not supposed to know what's
+ inside of fd_set; also the O(n^2) is really bad... */
+
+ for (i = 0; i < fds1->sds.fd_count; i++)
+ {
+ for (j = 0; j < fds2->sds.fd_count; j++)
+ {
+ if (fds1->sds.fd_array[i] == fds2->sds.fd_array[j])
+ return GNUNET_YES;
+ }
}
+ it = GNUNET_CONTAINER_slist_begin (fds1->handles);
+ while (GNUNET_CONTAINER_slist_end (it) != GNUNET_YES)
+ {
+ h = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (it, NULL);
+ if (GNUNET_CONTAINER_slist_contains
+ (fds2->handles, h, sizeof (struct GNUNET_DISK_FileHandle)))
+ {
+ GNUNET_CONTAINER_slist_iter_destroy (it);
+ return GNUNET_YES;
+ }
+ GNUNET_CONTAINER_slist_next (it);
+ }
+ GNUNET_CONTAINER_slist_iter_destroy (it);
#endif
return GNUNET_NO;
}
}
struct timeval tv;
- tv.tv_sec = timeout.value / GNUNET_TIME_UNIT_SECONDS.value;
+ tv.tv_sec = timeout.rel_value / GNUNET_TIME_UNIT_SECONDS.rel_value;
tv.tv_usec =
- 1000 * (timeout.value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.value));
- if ((nfds == 0) && (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
+ 1000 * (timeout.rel_value - (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value));
+ if ((nfds == 0) && (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
#ifdef MINGW
&& handles == 0
#endif
(rfds != NULL) ? &rfds->sds : NULL,
(wfds != NULL) ? &wfds->sds : NULL,
(efds != NULL) ? &efds->sds : NULL,
- (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
+ (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
? NULL : &tv);
#else
#define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
/* calculate how long we need to wait in milliseconds */
- if (timeout.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
+ if (timeout.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
ms_total = INFINITE;
else
- ms_total = timeout.value / GNUNET_TIME_UNIT_MILLISECONDS.value;
+ ms_total = timeout.rel_value / GNUNET_TIME_UNIT_MILLISECONDS.rel_value;
/* select() may be used as a portable way to sleep */
if (!(rfds || wfds || efds))
GNUNET_CONTAINER_slist_next (i))
{
- HANDLE h;
+ struct GNUNET_DISK_FileHandle *fh;
DWORD dwBytes;
- h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
- if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
+ fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
+ if (fh->type == GNUNET_PIPE)
{
- retcode = -1;
- SetErrnoFromWinError (GetLastError ());
-
-#if DEBUG_NETWORK
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
- "PeekNamedPipe");
+ if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
+ {
+ DWORD error_code = GetLastError ();
+ switch (error_code)
+ {
+ case ERROR_BROKEN_PIPE:
+ GNUNET_CONTAINER_slist_add (handles_read,
+ GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+ fh, sizeof (struct GNUNET_DISK_FileHandle));
+ retcode++;
+ break;
+ default:
+ retcode = -1;
+ SetErrnoFromWinError (error_code);
+
+ #if DEBUG_NETWORK
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+ "PeekNamedPipe");
+
+ #endif
+ goto select_loop_end;
+ }
+ }
+ else if (dwBytes)
-#endif
- goto select_loop_end;
+ {
+ GNUNET_CONTAINER_slist_add (handles_read,
+ GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+ fh, sizeof (struct GNUNET_DISK_FileHandle));
+ retcode++;
+ }
}
- else if (dwBytes)
-
+ else
{
+ /* Should we wait for more bytes to read here (in case of previous EOF)? */
GNUNET_CONTAINER_slist_add (handles_read,
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
- &h, sizeof (HANDLE));
- retcode++;
+ fh, sizeof (struct GNUNET_DISK_FileHandle));
}
}
GNUNET_CONTAINER_slist_iter_destroy (i);
GNUNET_CONTAINER_slist_next (i))
{
- HANDLE h;
+ struct GNUNET_DISK_FileHandle *fh;
DWORD dwBytes;
- h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
- if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
+ fh = (struct GNUNET_DISK_FileHandle *) GNUNET_CONTAINER_slist_get (i, NULL);
+ if (fh->type == GNUNET_PIPE)
{
- GNUNET_CONTAINER_slist_add (handles_except,
- GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
- &h, sizeof (HANDLE));
- retcode++;
+ if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
+
+ {
+ GNUNET_CONTAINER_slist_add (handles_except,
+ GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+ fh, sizeof (struct GNUNET_DISK_FileHandle));
+ retcode++;
+ }
}
}
GNUNET_CONTAINER_slist_iter_destroy (i);