bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: winsock: needs access to SOCKET in application code


From: Simon Josefsson
Subject: Re: winsock: needs access to SOCKET in application code
Date: Tue, 14 Oct 2008 16:25:57 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux)

Paolo Bonzini <address@hidden> writes:

>> I need to pass a SOCKET handle to GnuTLS, instead of the FD handle that
>> winsock wrappers created.
>
> You can also install your own GnuTLS transport handlers using read and
> write.  It would probably be less code and less complication than
> toggling between descriptors and sockets.

The _gl_fd_to_socket call is pretty small, and would only be needed at
one place, so I'm not sure replacing the transport handlers will be
simpler.

>>> SOCKET _gl_fd_to_socket (int fd)
>>> {
>>>   return FD_TO_SOCKET(fd);
>>> }
>>> ...
>>> This approach is unclean: the code is not written in portable POSIX
>>> style.
>> 
>> Indeed. You could have completely POSIX code in gsasl if the library's
>> API offered each socket related API in a variant that takes a SOCKET
>> argument and another variablt which takes an 'fd' argument.
>> 
>> But if you were only the maintainer of gsasl, and the library was not
>> under your control, this cleaner approach is not possible, and you need
>> to adapt gsasl to the data types supported by the library. So, yes, I agree,
>> gnulib should export this _gl_fd_to_socket function (without leading
>> underscore, once it's supported).
>
> I agree, but it needs to be thought out...  Should it be supported for
> POSIX sockets too?  In that case, you don't have SOCKET at all, so what
> should the prototype be?

Maybe something for the 'sockets' module, it is rather Windows specific?
How about the patch below?

>>> int _gl_socket_to_fd (SOCKET fh)
>>> {
>>>   return SOCKET_TO_FD(fh);
>>> }
>> 
>> This would be a problem, however. If you have a library which creates
>> SOCKET objects, then they need to be non-overlapping in order to work
>> with ReadFile/WriteFile, as Paolo said. How could that be guaranteed?
>
> Do you need this or is it only for symmetry?

I don't think I needed it.

/Simon

diff --git a/lib/sockets.c b/lib/sockets.c
index 658119e..c0bc223 100644
--- a/lib/sockets.c
+++ b/lib/sockets.c
@@ -55,3 +55,12 @@ gl_sockets_cleanup (void)
 
   return 0;
 }
+
+int gl_sockets_from_fd (int fd)
+{
+#if WINDOWS_SOCKETS
+  return _get_osfhandle (fd);
+#else
+  return fd;
+#endif
+}
diff --git a/lib/sockets.h b/lib/sockets.h
index 3ab16a0..f5a2f99 100644
--- a/lib/sockets.h
+++ b/lib/sockets.h
@@ -29,4 +29,6 @@
 int gl_sockets_startup (int version);
 int gl_sockets_cleanup (void);
 
+int gl_sockets_from_fd (int fd);
+
 #endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]