#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
-#define DEBUG_W32_CYCLES GNUNET_EXTRA_LOGGING
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
{
#ifndef MINGW
int fd;
-
#else
SOCKET fd;
#endif
#ifdef MINGW
DWORD error = 0;
-#if DEBUG_NETWORK
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing 0x%x\n",
- desc->fd);
-#endif
SetLastError (0);
ret = closesocket (desc->fd);
error = WSAGetLastError ();
SetErrnoFromWinsockError (error);
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Closed 0x%x, closesocket() returned %d, GLE is %u\n", desc->fd, ret,
error);
-#endif
#else
ret = close (desc->fd);
#endif
* @return NULL on error (including not supported on target platform)
*/
struct GNUNET_NETWORK_Handle *
-GNUNET_NETWORK_socket_box_native (int fd)
+GNUNET_NETWORK_socket_box_native (SOCKTYPE fd)
{
+ struct GNUNET_NETWORK_Handle *ret;
#if MINGW
- return NULL;
+ unsigned long i;
+ DWORD d;
+ /* FIXME: Find a better call to check that FD is valid */
+ if (WSAIoctl (fd, FIONBIO, (void *) &i, sizeof (i), NULL, 0, &d, NULL, NULL) != 0)
+ return NULL; /* invalid FD */
+ ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
+ ret->fd = fd;
+ ret->af = AF_UNSPEC;
+ return ret;
#else
- struct GNUNET_NETWORK_Handle *ret;
-
if (fcntl (fd, F_GETFD) < 0)
return NULL; /* invalid FD */
ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle));
if (fh->type == GNUNET_PIPE)
{
/* Read zero bytes to check the status of the pipe */
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading 0 bytes from the pipe 0x%x\n",
fh->h);
-#endif
if (!ReadFile (fh->h, NULL, 0, NULL, fh->oOverlapRead))
{
DWORD error_code = GetLastError ();
if (error_code == ERROR_IO_PENDING)
{
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the pipe's 0x%x overlapped event to the array as %d\n",
fh->h, nhandles);
-#endif
handle_array[nhandles++] = fh->oOverlapRead->hEvent;
readArray[readPipes++] = fh;
}
- /*
- * else
- * {
- * SetErrnoFromWinError (error_code);
- * }
- */
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Read failed, adding the read ready event to the array as %d\n", nhandles);
+ handle_array[nhandles++] = hEventReadReady;
+ readArray[readPipes++] = fh;
+ }
}
else
{
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the read ready event to the array as %d\n", nhandles);
-#endif
handle_array[nhandles++] = hEventReadReady;
readArray[readPipes++] = fh;
}
}
if (wfds && write_handles)
{
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the write ready event to the array as %d\n", nhandles);
-#endif
handle_array[nhandles++] = hEventPipeWrite;
writePipePos = nhandles;
}
{
if (rfds)
{
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the socket read event to the array as %d\n", nhandles);
-#endif
handle_array[nhandles++] = hEventRead;
nSockEvents++;
for (i = 0; i < rfds->sds.fd_count; i++)
{
int wakeup = 0;
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the socket write event to the array as %d\n", nhandles);
-#endif
handle_array[nhandles++] = hEventWrite;
nSockEvents++;
for (i = 0; i < wfds->sds.fd_count; i++)
status = send (wfds->sds.fd_array[i], NULL, 0, 0);
error = GetLastError ();
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"pre-send to the socket %d returned %d (%u)\n", i, status, error);
-#endif
if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN))
wakeup = 1;
WSAEventSelect (wfds->sds.fd_array[i], hEventWrite,
}
if (efds)
{
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the socket error event to the array as %d\n", nhandles);
-#endif
handle_array[nhandles++] = hEventException;
nSockEvents++;
for (i = 0; i < efds->sds.fd_count; i++)
#endif
returnedpos = returncode - WAIT_OBJECT_0;
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "return pos is : %d\n", returnedpos);
-#endif
/* FIXME: THIS LINE IS WRONG !! We should add to handles only handles that fired the events, not all ! */
/*
retcode = select (nfds, &aread, &awrite, &aexcept, &tvslice);
if (retcode == -1)
retcode = 0;
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Select retcode : %d\n", retcode);
-#endif
}
/* FIXME: <= writePipePos? Really? */
if ((writePipePos != -1) && (returnedpos <= writePipePos))
{
GNUNET_CONTAINER_slist_append (handles_write, wfds->handles);
retcode += write_handles;
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Added write pipe\n");
-#endif
}
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "ReadPipes is : %d\n", readPipes);
-#endif
/* We have some pipes ready for read. */
/* FIXME: it is supposed to work !! Only choose the Pipes who fired the event, but it is not working */
bret =
PeekNamedPipe (readArray[i]->h, NULL, 0, NULL, &waitstatus, NULL);
error = GetLastError ();
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Peek at read pipe %d (0x%x) returned %d (%d bytes available) GLE %u\n",
i, readArray[i]->h, bret, waitstatus, error);
-#endif
if (bret == 0)
{
if (error != ERROR_BROKEN_PIPE)
readArray[i],
sizeof (struct GNUNET_DISK_FileHandle));
retcode++;
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Added read Pipe 0x%x (0x%x)\n",
readArray[i], readArray[i]->h);
-#endif
}
}
waitstatus = WaitForSingleObject (hEventWrite, 0);
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Wait for the write event returned %d\n",
waitstatus);
-#endif
if (waitstatus == WAIT_OBJECT_0)
{
for (i = 0; i < wfds->sds.fd_count; i++)
status = send (wfds->sds.fd_array[i], NULL, 0, 0);
error = GetLastError ();
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG,
"send to the socket %d returned %d (%u)\n", i, status, error);
-#endif
if (status == 0 || (error != WSAEWOULDBLOCK && error != WSAENOTCONN) ||
(status == -1 && gso_result == 0 && error == WSAENOTCONN &&
so_error == WSAECONNREFUSED))
CancelIo (fh->h);
}
}
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing rfds\n");
-#endif
GNUNET_NETWORK_fdset_zero (rfds);
if (retcode != -1 && nhandles && (returnedpos < nhandles))
GNUNET_NETWORK_fdset_copy_native (rfds, &aread, retcode);
WSAEventSelect (wfds->sds.fd_array[i], hEventWrite, 0);
nsock++;
}
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing wfds\n");
-#endif
GNUNET_NETWORK_fdset_zero (wfds);
if (retcode != -1 && nhandles && (returnedpos < nhandles))
GNUNET_NETWORK_fdset_copy_native (wfds, &awrite, retcode);
WSAEventSelect (efds->sds.fd_array[i], hEventException, 0);
nsock++;
}
-#if DEBUG_NETWORK
LOG (GNUNET_ERROR_TYPE_DEBUG, "Zeroing efds\n");
-#endif
GNUNET_NETWORK_fdset_zero (efds);
if (retcode != -1 && nhandles && (returnedpos < nhandles))
GNUNET_NETWORK_fdset_copy_native (efds, &aexcept, retcode);