*/
guint32 _wapi_shm_mapped_segments;
+guint32 _wapi_fd_offset_table_size;
+gpointer *_wapi_fd_offset_table=NULL;
+
static void shared_init (void)
{
struct sockaddr_un shared_socket_address;
_wapi_shared_data=g_new0 (struct _WapiHandleShared_list *, 1);
_wapi_private_data=g_new0 (struct _WapiHandlePrivate_list *, 1);
+
+ /* Build this array at runtime, because I'm guessing that
+ * getdtablesize() won't be a fixed value on all systems (or
+ * why would it be a function rather than a constant define?)
+ */
+ _wapi_fd_offset_table_size=getdtablesize ();
+ _wapi_fd_offset_table=g_new0 (gpointer, _wapi_fd_offset_table_size);
attach_again:
for(j=idx; j<_WAPI_HANDLES_PER_SEGMENT; j++) {
struct _WapiHandleShared *shared;
- /* Make sure we dont try and assign handle 0 */
- if (i==0 && j==0) {
+ /* Make sure we dont try and assign the
+ * handles that would clash with fds
+ */
+ if (i==0 && j < _wapi_fd_offset_table_size) {
+ j = _wapi_fd_offset_table_size;
continue;
}
int thr_ret;
mono_once (&shared_init_once, shared_init);
-
+
again:
if(shared==TRUE) {
new.type=WapiHandleRequestType_New;
pthread_cleanup_pop (0);
}
+ /* Make sure we left the space for fd mappings */
+ g_assert (handle_idx >= _wapi_fd_offset_table_size);
+
if(handle_idx==0) {
g_warning (G_GNUC_PRETTY_FUNCTION ": Ran out of handles!");
guint32 idx;
guint32 segment;
+ g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+
_wapi_handle_segment (handle, &segment, &idx);
_wapi_handle_ensure_mapped (segment);
void _wapi_handle_ref (gpointer handle)
{
+ g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+
if(shared==TRUE) {
WapiHandleRequest req={0};
WapiHandleResponse resp={0};
gboolean destroy = FALSE;
int thr_ret;
+ g_assert (GPOINTER_TO_UINT (handle) >= _wapi_fd_offset_table_size);
+
_wapi_handle_segment (handle, &segment, &idx);
if(shared==TRUE) {
guint32 idx, segment;
WapiHandleType type;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
guint32 idx, segment;
WapiHandleType type;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
guint32 idx, segment;
WapiHandleType type;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
guint32 idx, segment;
WapiHandleType type;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
guint32 idx, segment;
WapiHandleType type;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
guint32 idx, segment;
WapiHandleType type;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
type=_wapi_handle_get_shared_segment (segment)->handles[idx].type;
*/
gboolean CloseHandle(gpointer handle)
{
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_unref (handle);
return(TRUE);
again:
for(i=0; i<numhandles; i++) {
guint32 idx, segment;
+ gpointer handle = handles[i];
- _wapi_handle_segment (handles[i], &segment, &idx);
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
+ _wapi_handle_segment (handle, &segment, &idx);
#ifdef DEBUG
g_message (G_GNUC_PRETTY_FUNCTION ": attempting to lock %p",
- handles[i]);
+ handle);
#endif
ret=mono_mutex_trylock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex);
struct timespec sleepytime;
#ifdef DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION ": attempt failed for %p", handles[i]);
+ g_message (G_GNUC_PRETTY_FUNCTION ": attempt failed for %p", handle);
#endif
while(i--) {
- _wapi_handle_segment (handles[i], &segment, &idx);
+ _wapi_handle_segment (handle, &segment, &idx);
thr_ret = mono_mutex_unlock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex);
g_assert (thr_ret == 0);
}
for(i=0; i<numhandles; i++) {
guint32 idx, segment;
-
- _wapi_handle_ref (handles[i]);
+ gpointer handle = handles[i];
+
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
+ _wapi_handle_ref (handle);
- _wapi_handle_segment (handles[i], &segment, &idx);
+ _wapi_handle_segment (handle, &segment, &idx);
#ifdef DEBUG
g_message (G_GNUC_PRETTY_FUNCTION ": Checking handle %p",
- handles[i]);
+ handle);
#endif
- if(((_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_OWN)==TRUE) &&
- (_wapi_handle_ops_isowned (handles[i])==TRUE)) ||
+ if(((_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_OWN)==TRUE) &&
+ (_wapi_handle_ops_isowned (handle)==TRUE)) ||
(_wapi_handle_get_shared_segment (segment)->handles[idx].signalled==TRUE)) {
count++;
#ifdef DEBUG
g_message (G_GNUC_PRETTY_FUNCTION
- ": Handle %p signalled", handles[i]);
+ ": Handle %p signalled", handle);
#endif
if(*lowest>i) {
*lowest=i;
for(i=0; i<numhandles; i++) {
guint32 idx, segment;
-
- _wapi_handle_segment (handles[i], &segment, &idx);
+ gpointer handle = handles[i];
+
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
+ _wapi_handle_segment (handle, &segment, &idx);
#ifdef DEBUG
g_message (G_GNUC_PRETTY_FUNCTION ": unlocking handle %p",
- handles[i]);
+ handle);
#endif
thr_ret = mono_mutex_unlock (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_mutex);
g_assert (thr_ret == 0);
- _wapi_handle_unref (handles[i]);
+ _wapi_handle_unref (handle);
}
}
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
guint32 idx, segment;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
return(mono_cond_wait (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_cond,
struct timespec fake_timeout;
int ret;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
_wapi_calc_timeout (&fake_timeout, 100);
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
guint32 idx, segment;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
return(mono_cond_timedwait (&_wapi_handle_get_shared_segment (segment)->handles[idx].signal_cond,
struct timespec fake_timeout;
int ret;
+ if (GPOINTER_TO_UINT (handle) < _wapi_fd_offset_table_size) {
+ handle = _wapi_handle_fd_offset_to_handle (handle);
+ }
+
_wapi_handle_segment (handle, &segment, &idx);
_wapi_calc_timeout (&fake_timeout, 100);
handle, file_private_handle->fd);
#endif
+ /* Blank out the mapping, to make catching errors easier */
+ _wapi_handle_fd_offset_store (file_private_handle->fd, NULL);
+
close(file_private_handle->fd);
}
console_private_handle->fd);
#endif
+ /* Blank out the mapping, to make catching errors easier */
+ _wapi_handle_fd_offset_store (console_private_handle->fd, NULL);
+
close(console_private_handle->fd);
}
pipe_private_handle->fd);
#endif
+ /* Blank out the mapping, to make catching errors easier */
+ _wapi_handle_fd_offset_store (pipe_private_handle->fd, NULL);
+
close(pipe_private_handle->fd);
}
close (fd);
goto cleanup;
}
- cf_ret = handle;
+
+ _wapi_handle_fd_offset_store (fd, handle);
+ cf_ret = GINT_TO_POINTER (fd);
file_private_handle->fd=fd;
file_private_handle->assigned=TRUE;
": error looking up console handle %p", handle);
goto cleanup;
}
- ret = handle;
+
+ _wapi_handle_fd_offset_store (fd, handle);
+ ret = GINT_TO_POINTER (fd);
file_private_handle->fd=fd;
file_private_handle->assigned=TRUE;
}
/* Add a reference to this handle */
- _wapi_handle_ref (handle);
+ _wapi_handle_ref (_wapi_handle_fd_offset_to_handle (handle));
return(handle);
}
* read due to an attempt to read past the end of the file), %FALSE on
* error.
*/
-gboolean ReadFile(gpointer handle, gpointer buffer, guint32 numbytes,
+gboolean ReadFile(gpointer fd_handle, gpointer buffer, guint32 numbytes,
guint32 *bytesread, WapiOverlapped *overlapped)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].readfile==NULL) {
*
* Return value: %TRUE if the write succeeds, %FALSE on error.
*/
-gboolean WriteFile(gpointer handle, gconstpointer buffer, guint32 numbytes,
+gboolean WriteFile(gpointer fd_handle, gconstpointer buffer, guint32 numbytes,
guint32 *byteswritten, WapiOverlapped *overlapped)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].writefile==NULL) {
*
* Return value: %TRUE on success, %FALSE otherwise.
*/
-gboolean FlushFileBuffers(gpointer handle)
+gboolean FlushFileBuffers(gpointer fd_handle)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].flushfile==NULL) {
*
* Return value: %TRUE on success, %FALSE otherwise.
*/
-gboolean SetEndOfFile(gpointer handle)
+gboolean SetEndOfFile(gpointer fd_handle)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].setendoffile==NULL) {
* If @highmovedistance is not %NULL, the high 32 bits of the new file
* pointer are stored there. On failure, %INVALID_SET_FILE_POINTER.
*/
-guint32 SetFilePointer(gpointer handle, gint32 movedistance,
+guint32 SetFilePointer(gpointer fd_handle, gint32 movedistance,
gint32 *highmovedistance, WapiSeekMethod method)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].seek==NULL) {
* %FILE_TYPE_CHAR - @handle is a character device, such as a console.
* %FILE_TYPE_PIPE - @handle is a named or anonymous pipe.
*/
-WapiFileType GetFileType(gpointer handle)
+WapiFileType GetFileType(gpointer fd_handle)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].getfiletype==NULL) {
* @highsize is non-%NULL then the high 32 bits of the file size are
* stored here. On failure %INVALID_FILE_SIZE is returned.
*/
-guint32 GetFileSize(gpointer handle, guint32 *highsize)
+guint32 GetFileSize(gpointer fd_handle, guint32 *highsize)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].getfilesize==NULL) {
*
* Return value: %TRUE on success, %FALSE otherwise.
*/
-gboolean GetFileTime(gpointer handle, WapiFileTime *create_time,
+gboolean GetFileTime(gpointer fd_handle, WapiFileTime *create_time,
WapiFileTime *last_access, WapiFileTime *last_write)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].getfiletime==NULL) {
*
* Return value: %TRUE on success, %FALSE otherwise.
*/
-gboolean SetFileTime(gpointer handle, const WapiFileTime *create_time,
+gboolean SetFileTime(gpointer fd_handle, const WapiFileTime *create_time,
const WapiFileTime *last_access,
const WapiFileTime *last_write)
{
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
WapiHandleType type=_wapi_handle_type (handle);
if(io_ops[type].setfiletime==NULL) {
return result;
}
-int _wapi_file_handle_to_fd (gpointer handle)
+/* When we're confident there are no more bugs in the fd->handle
+ * mapping, this can be replaced as a no-op: GPOINTER_TO_INT(fd_handle) == fd
+ */
+int _wapi_file_handle_to_fd (gpointer fd_handle)
{
struct _WapiHandlePrivate_file *file_private_handle;
gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
#ifdef DEBUG
g_message (G_GNUC_PRETTY_FUNCTION ": looking up fd for %p", handle);
file_private_handle->fd);
#endif
+ g_assert (file_private_handle->fd == GPOINTER_TO_INT (fd_handle));
+
return(file_private_handle->fd);
}
pipe_read_private_handle->assigned=TRUE;
pipe_read_handle->fileaccess=GENERIC_READ;
- *readpipe=read_handle;
+ _wapi_handle_fd_offset_store (filedes[0], read_handle);
+ *readpipe=GINT_TO_POINTER (filedes[0]);
pipe_write_private_handle->fd=filedes[1];
pipe_write_private_handle->assigned=TRUE;
pipe_write_handle->fileaccess=GENERIC_WRITE;
- *writepipe=write_handle;
+ _wapi_handle_fd_offset_store (filedes[1], write_handle);
+ *writepipe=GINT_TO_POINTER (filedes[1]);
#ifdef DEBUG
- g_message (G_GNUC_PRETTY_FUNCTION
- ": Returning pipe: read handle %p, write handle %p",
- read_handle, write_handle);
+ g_message (G_GNUC_PRETTY_FUNCTION ": Returning pipe: read handle %p (fd %d), write handle %p (fd %d)", read_handle, filedes[0], write_handle, filedes[1]);
#endif
write_cleanup:
}
gboolean
-_wapi_io_add_callback (gpointer handle,
+_wapi_io_add_callback (gpointer fd_handle,
WapiOverlappedCB callback,
guint64 flags G_GNUC_UNUSED)
{
gboolean ok;
int thr_ret;
gboolean ret = FALSE;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
(gpointer *) &file_handle,
return(TRUE);
}
-gboolean LockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
+gboolean LockFile (gpointer fd_handle, guint32 offset_low, guint32 offset_high,
guint32 length_low, guint32 length_high)
{
struct _WapiHandle_file *file_handle;
struct _WapiHandlePrivate_file *file_private_handle;
gboolean ok;
off_t offset, length;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
(gpointer *)&file_handle,
length));
}
-gboolean UnlockFile (gpointer handle, guint32 offset_low, guint32 offset_high,
- guint32 length_low, guint32 length_high)
+gboolean UnlockFile (gpointer fd_handle, guint32 offset_low,
+ guint32 offset_high, guint32 length_low,
+ guint32 length_high)
{
struct _WapiHandle_file *file_handle;
struct _WapiHandlePrivate_file *file_private_handle;
gboolean ok;
off_t offset, length;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
(gpointer *)&file_handle,
g_ptr_array_remove_fast(sockets, GUINT_TO_POINTER (handle));
+ /* Blank out the mapping, to make catching errors easier */
+ _wapi_handle_fd_offset_store (socket_private_handle->fd, NULL);
+
do {
ret=close(socket_private_handle->fd);
}
return(err);
}
-int closesocket(guint32 handle)
+int closesocket(guint32 fd_handle)
{
- _wapi_handle_unref (GUINT_TO_POINTER (handle));
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd_handle));
+
+ _wapi_handle_unref (handle);
return(0);
}
-guint32 _wapi_accept(guint32 handle, struct sockaddr *addr,
- socklen_t *addrlen)
+guint32 _wapi_accept(guint32 fd, struct sockaddr *addr, socklen_t *addrlen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
struct _WapiHandlePrivate_socket *new_socket_private_handle;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
gpointer new_handle;
gboolean ok;
- int fd;
+ int new_fd;
int thr_ret;
guint32 ret = INVALID_SOCKET;
return(INVALID_SOCKET);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(INVALID_SOCKET);
}
do {
- fd=accept(socket_private_handle->fd, addr, addrlen);
+ new_fd=accept(fd, addr, addrlen);
}
- while (fd==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
+ while (new_fd==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
- if(fd==-1) {
+ if(new_fd==-1) {
gint errnum = errno;
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION ": accept error: %s",
if(new_handle==_WAPI_HANDLE_INVALID) {
g_warning (G_GNUC_PRETTY_FUNCTION
": error creating socket handle");
+ WSASetLastError (ERROR_GEN_FAILURE);
return(INVALID_SOCKET);
}
(gpointer *)&new_socket_private_handle);
if(ok==FALSE) {
g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ ": error looking up new socket handle %p",
+ new_handle);
goto cleanup;
}
- ret = GPOINTER_TO_UINT (new_handle);
+
+ _wapi_handle_fd_offset_store (new_fd, new_handle);
+ ret = new_fd;
- new_socket_private_handle->fd=fd;
+ new_socket_private_handle->fd=new_fd;
#ifdef DEBUG
g_message(G_GNUC_PRETTY_FUNCTION
return(ret);
}
-int _wapi_bind(guint32 handle, struct sockaddr *my_addr, socklen_t addrlen)
+int _wapi_bind(guint32 fd, struct sockaddr *my_addr, socklen_t addrlen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
WSASetLastError(WSANOTINITIALISED);
return(SOCKET_ERROR);
}
-
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=bind(socket_private_handle->fd, my_addr, addrlen);
+ ret=bind(fd, my_addr, addrlen);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
return(ret);
}
-int _wapi_connect(guint32 handle, const struct sockaddr *serv_addr,
+int _wapi_connect(guint32 fd, const struct sockaddr *serv_addr,
socklen_t addrlen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
gint errnum;
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
do {
- ret=connect(socket_private_handle->fd, serv_addr, addrlen);
+ ret=connect(fd, serv_addr, addrlen);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
errnum = errno;
- ret=setsockopt (socket_private_handle->fd, SOL_SOCKET,
- SO_BROADCAST, &true, sizeof(true));
+ ret=setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &true,
+ sizeof(true));
if(ret==0) {
do {
- ret=connect (socket_private_handle->fd, serv_addr, addrlen);
+ ret=connect (fd, serv_addr, addrlen);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
}
return(ret);
}
-int _wapi_getpeername(guint32 handle, struct sockaddr *name,
- socklen_t *namelen)
+int _wapi_getpeername(guint32 fd, struct sockaddr *name, socklen_t *namelen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=getpeername(socket_private_handle->fd, name, namelen);
+ ret=getpeername(fd, name, namelen);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
return(ret);
}
-int _wapi_getsockname(guint32 handle, struct sockaddr *name,
- socklen_t *namelen)
+int _wapi_getsockname(guint32 fd, struct sockaddr *name, socklen_t *namelen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=getsockname(socket_private_handle->fd, name, namelen);
+ ret=getsockname(fd, name, namelen);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
return(ret);
}
-int _wapi_getsockopt(guint32 handle, int level, int optname, void *optval,
+int _wapi_getsockopt(guint32 fd, int level, int optname, void *optval,
socklen_t *optlen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=getsockopt(socket_private_handle->fd, level, optname, optval,
- optlen);
+ ret=getsockopt(fd, level, optname, optval, optlen);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
return(ret);
}
-int _wapi_listen(guint32 handle, int backlog)
+int _wapi_listen(guint32 fd, int backlog)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=listen(socket_private_handle->fd, backlog);
+ ret=listen(fd, backlog);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
return(0);
}
-int _wapi_recv(guint32 handle, void *buf, size_t len, int recv_flags)
+int _wapi_recv(guint32 fd, void *buf, size_t len, int recv_flags)
{
- return(_wapi_recvfrom(handle, buf, len, recv_flags, NULL, 0));
+ return(_wapi_recvfrom(fd, buf, len, recv_flags, NULL, 0));
}
-int _wapi_recvfrom(guint32 handle, void *buf, size_t len, int recv_flags,
+int _wapi_recvfrom(guint32 fd, void *buf, size_t len, int recv_flags,
struct sockaddr *from, socklen_t *fromlen)
{
#ifndef HAVE_MSG_NOSIGNAL
void (*old_sigpipe)(int); // old SIGPIPE handler
#endif
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
#ifdef HAVE_MSG_NOSIGNAL
do {
- ret=recvfrom(socket_private_handle->fd, buf, len, recv_flags | MSG_NOSIGNAL, from,
+ ret=recvfrom(fd, buf, len, recv_flags | MSG_NOSIGNAL, from,
fromlen);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
#else
old_sigpipe = signal(SIGPIPE, SIG_IGN);
do {
- ret=recvfrom(socket_private_handle->fd, buf, len, recv_flags, from,
- fromlen);
+ ret=recvfrom(fd, buf, len, recv_flags, from, fromlen);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
signal(SIGPIPE, old_sigpipe);
return(ret);
}
-int _wapi_send(guint32 handle, const void *msg, size_t len, int send_flags)
+int _wapi_send(guint32 fd, const void *msg, size_t len, int send_flags)
{
#ifndef HAVE_MSG_NOSIGNAL
void (*old_sigpipe)(int); // old SIGPIPE handler
#endif
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
#ifdef HAVE_MSG_NOSIGNAL
do {
- ret=send(socket_private_handle->fd, msg, len, send_flags | MSG_NOSIGNAL);
+ ret=send(fd, msg, len, send_flags | MSG_NOSIGNAL);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
#else
old_sigpipe = signal(SIGPIPE, SIG_IGN);
do {
- ret=send(socket_private_handle->fd, msg, len, send_flags);
+ ret=send(fd, msg, len, send_flags);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
signal(SIGPIPE, old_sigpipe);
return(ret);
}
-int _wapi_sendto(guint32 handle, const void *msg, size_t len, int send_flags,
+int _wapi_sendto(guint32 fd, const void *msg, size_t len, int send_flags,
const struct sockaddr *to, socklen_t tolen)
{
#ifndef HAVE_MSG_NOSIGNAL
void (*old_sigpipe)(int); // old SIGPIPE handler
#endif
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
#ifdef HAVE_MSG_NOSIGNAL
do {
- ret=sendto(socket_private_handle->fd, msg, len, send_flags | MSG_NOSIGNAL, to, tolen);
+ ret=sendto(fd, msg, len, send_flags | MSG_NOSIGNAL, to, tolen);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
#else
old_sigpipe = signal(SIGPIPE, SIG_IGN);
do {
- ret=sendto(socket_private_handle->fd, msg, len, send_flags, to, tolen);
+ ret=sendto(fd, msg, len, send_flags, to, tolen);
}
while (ret==-1 && errno==EINTR && !_wapi_thread_cur_apc_pending());
signal(SIGPIPE, old_sigpipe);
return(ret);
}
-int _wapi_setsockopt(guint32 handle, int level, int optname,
+int _wapi_setsockopt(guint32 fd, int level, int optname,
const void *optval, socklen_t optlen)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=setsockopt(socket_private_handle->fd, level, optname, optval,
- optlen);
+ ret=setsockopt(fd, level, optname, optval, optlen);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
return(ret);
}
-int _wapi_shutdown(guint32 handle, int how)
+int _wapi_shutdown(guint32 fd, int how)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
- ret=shutdown(socket_private_handle->fd, how);
+ ret=shutdown(fd, how);
if(ret==-1) {
gint errnum = errno;
#ifdef DEBUG
": error looking up socket handle %p", handle);
goto cleanup;
}
- ret = GPOINTER_TO_UINT (handle);
+
+ _wapi_handle_fd_offset_store (fd, handle);
+ ret = fd;
socket_private_handle->fd=fd;
}
int
-WSAIoctl (guint32 handle, gint32 command,
- gchar *input, gint i_len,
- gchar *output, gint o_len, glong *written,
- void *unused1, void *unused2)
+WSAIoctl (guint32 fd, gint32 command,
+ gchar *input, gint i_len,
+ gchar *output, gint o_len, glong *written,
+ void *unused1, void *unused2)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
gchar *buffer = NULL;
return(SOCKET_ERROR);
}
- ok = _wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
-
- if (ok == FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError (WSAENOTSOCK);
return SOCKET_ERROR;
}
if (i_len > 0)
buffer = g_memdup (input, i_len);
- ret = ioctl (socket_private_handle->fd, command, buffer);
+ ret = ioctl (fd, command, buffer);
if (ret == -1) {
gint errnum = errno;
#ifdef DEBUG
return 0;
}
-int ioctlsocket(guint32 handle, gint32 command, gpointer arg)
+int ioctlsocket(guint32 fd, gint32 command, gpointer arg)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
int ret;
if(startup_count==0) {
return(SOCKET_ERROR);
}
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(SOCKET_ERROR);
}
* connect to return EINPROGRESS, but the ioctl doesn't seem to)
*/
if(command==FIONBIO) {
- ret=fcntl(socket_private_handle->fd, F_GETFL, 0);
+ ret=fcntl(fd, F_GETFL, 0);
if(ret!=-1) {
if(*(gboolean *)arg) {
ret &= ~O_NONBLOCK;
} else {
ret |= O_NONBLOCK;
}
- ret=fcntl(socket_private_handle->fd, F_SETFL, ret);
+ ret=fcntl(fd, F_SETFL, ret);
}
} else
#endif /* O_NONBLOCK */
{
- ret=ioctl(socket_private_handle->fd, command, arg);
+ ret=ioctl(fd, command, arg);
}
if(ret==-1) {
gint errnum = errno;
return(ret);
}
-void _wapi_FD_CLR(guint32 handle, fd_set *set)
+void _wapi_FD_CLR(guint32 fd, fd_set *set)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return;
}
- FD_CLR(socket_private_handle->fd, set);
+ FD_CLR(fd, set);
}
-int _wapi_FD_ISSET(guint32 handle, fd_set *set)
+int _wapi_FD_ISSET(guint32 fd, fd_set *set)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return(0);
}
- return(FD_ISSET(socket_private_handle->fd, set));
+ return(FD_ISSET(fd, set));
}
-void _wapi_FD_SET(guint32 handle, fd_set *set)
+void _wapi_FD_SET(guint32 fd, fd_set *set)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (GUINT_TO_POINTER (fd));
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if(ok==FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError(WSAENOTSOCK);
return;
}
- FD_SET(socket_private_handle->fd, set);
+ FD_SET(fd, set);
}
#ifdef USE_AIO
}
static gboolean
-do_aio_call (gboolean is_read, gpointer handle, gpointer buffer,
+do_aio_call (gboolean is_read, gpointer fd_handle, gpointer buffer,
guint32 numbytes, guint32 *out_bytes,
gpointer ares,
SocketAsyncCB callback)
{
- struct _WapiHandlePrivate_socket *socket_private_handle;
- gboolean ok;
- int fd;
+ gpointer handle = _wapi_handle_fd_offset_to_handle (fd_handle);
+ int fd = GPOINTER_TO_UINT (fd_handle);
struct aiocb *aio;
int result;
notifier_data_t *ndata;
- ok=_wapi_lookup_handle (GUINT_TO_POINTER (handle), WAPI_HANDLE_SOCKET,
- NULL, (gpointer *)&socket_private_handle);
- if (ok == FALSE) {
- g_warning (G_GNUC_PRETTY_FUNCTION
- ": error looking up socket handle 0x%x", (guint) handle);
+ if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
WSASetLastError (WSAENOTSOCK);
return FALSE;
}
- fd = socket_private_handle->fd;
-
ndata = g_new0 (notifier_data_t, 1);
aio = g_new0 (struct aiocb, 1);
ndata->ares = ares;