tun: remove tun_finalize()
PATCH 1/1 tun: remove tun_finalize()
From: Lev Stipakov <[email protected]...>
tun_finalize() is essentially subset of socket_finalize() apart from:
- using WSAFoo() functions instead of Foo()
- "from" address is not returned
There is no clear official statement that one can use non-WSA API on handles, so let's be on a safe side and use both.
Introduce sockethandle_t abstraction, which represents socket and handle. Add SocketHandle* macros which call proper API depends on underlying type in abstraction.
Rename socket_finalize() to sockethandle_finalize(), take sockethandle_t and new macros into use and kick tun_finalize().
--- src/openvpn/forward.c | 3 +- src/openvpn/socket.c | 37 ++++++++--------- src/openvpn/socket.h | 31 ++++++++++++-- src/openvpn/tun.c | 94 +++++++++---------------------------------- src/openvpn/tun.h | 34 +--------------- 5 files changed, 65 insertions(+), 134 deletions(-)
src/openvpn/forward.c +2 -1
@@ -1115,7 +1115,8 @@ read_incoming_tun(struct context *c) }else { -read_tun_buffered (c->c1.tuntap, &c->c2.buf); +sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand }; +sockethandle_finalize (sh, &c->c1.tuntap->reads, &c->c2.buf,NULL ); }#else /* ifdef _WIN32 */ ASSERT (buf_init (&c->c2.buf,FRAME_HEADROOM (&c->c2.frame)));
src/openvpn/socket.c +16 -21
@@ -3198,7 +3198,8 @@ link_socket_read_tcp(struct link_socket *sock,if (!sock->stream_buf.residual_fully_formed) {#ifdef _WIN32 - len =socket_finalize (sock->sd, &sock->reads, buf,NULL ); +sockethandle_t sh = { .s = sock->sd }; + len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else struct buffer frag;stream_buf_get_next (&sock->stream_buf, &frag);
@@ -3664,10 +3665,10 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin }int -socket_finalize (SOCKET s, -struct overlapped_io *io, -struct buffer *buf, -struct link_socket_actual *from) +sockethandle_finalize (sockethandle_t sh, +struct overlapped_io *io, +struct buffer *buf, +struct link_socket_actual *from) {int ret =-1 ;BOOL status;
@@ -3675,13 +3676,7 @@ socket_finalize(SOCKET s,switch (io->iostate) {case IOSTATE_QUEUED : - status =WSAGetOverlappedResult ( - s, - &io->overlapped, - &io->size, - FALSE, - &io->flags - ); + status =SocketHandleGetOverlappedResult (sh, &io);if (status) {/* successful return for a queued operation */
@@ -3693,18 +3688,18 @@ socket_finalize(SOCKET s, io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion success [%d]" , ret); }else {/* error during a queued operation */ ret =-1 ; -if (WSAGetLastError () !=WSA_IO_INCOMPLETE ) +if (SocketHandleGetLastError (sh) !=ERROR_IO_INCOMPLETE ) {/* if no error (i.e. just not finished yet), then DON'T execute this code */ io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion error" ); } }break ;
@@ -3715,9 +3710,9 @@ socket_finalize(SOCKET s,if (io->status) {/* error return for a non-queued operation */ -WSASetLastError (io->status); +SocketHandleSetLastError (sh, io->status); ret =-1 ; -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion non-queued error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion non-queued error" ); }else {
@@ -3727,14 +3722,14 @@ socket_finalize(SOCKET s, *buf = io->buf; } ret = io->size; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion non-queued success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion non-queued success [%d]" , ret); }break ;case IOSTATE_INITIAL :/* were we called without proper queueing? */ -WSASetLastError (WSAEINVAL ); +SocketHandleSetInvalError (sh); ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion BAD STATE" ); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion BAD STATE" );break ;default :
@@ -3742,7 +3737,7 @@ socket_finalize(SOCKET s, }/* return from address if requested */ -if (from) +if (!sh.is_handle && from) {if (ret >=0 && io->addr_defined) {
src/openvpn/socket.h +27 -4
@@ -262,12 +262,33 @@ int socket_send_queue(struct link_socket *sock,struct buffer *buf, const struct link_socket_actual *to); -int socket_finalize ( -SOCKET s, +typedef struct { +union { +SOCKET s; +HANDLE h; + }; +bool is_handle; +}sockethandle_t ; + +int sockethandle_finalize ( +sockethandle_t sh,struct overlapped_io *io,struct buffer *buf,struct link_socket_actual *from); +#define SocketHandleGetOverlappedResult (sh, io) (sh.is_handle ? \ + GetOverlappedResult(sh.h, io->overlapped, io->size, FALSE) : \ + WSAGetOverlappedResult(sh.s, io->overlapped, io->size, FALSE, io->flags)) + +#define SocketHandleGetLastError (sh) (sh.is_handle ? \ + GetLastError() : WSAGetLastError()) + +#define SocketHandleSetLastError (sh, ...) (sh.is_handle ? \ + SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__)) + +#define SocketHandleSetInvalError (sh) (sh.is_handle ? \ + SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL)) +#else /* ifdef _WIN32 */ #define openvpn_close_socket (s) close(s)
@@ -1020,7 +1041,8 @@ link_socket_read_udp_win32(struct link_socket *sock,struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .s = sock->sd }; +return sockethandle_finalize (sh, &sock->reads, buf, from); }#else /* ifdef _WIN32 */
@@ -1078,9 +1100,10 @@ link_socket_write_win32(struct link_socket *sock, {int err =0 ;int status =0 ; +sockethandle_t sh = { .s = sock->sd };if (overlapped_io_active (&sock->writes)) { - status =socket_finalize (sock->sd, &sock->writes,NULL ,NULL ); + status =sockethandle_finalize (sh, &sock->writes,NULL ,NULL );if (status <0 ) { err =WSAGetLastError ();
src/openvpn/tun.c +18 -76
105 lines not on this page · raw message
src/openvpn/tun.h +2 -32
@@ -437,8 +437,6 @@ int tun_read_queue(struct tuntap *tt, int maxsize);int tun_write_queue (struct tuntap *tt,struct buffer *buf); -int tun_finalize (HANDLE h,struct overlapped_io *io,struct buffer *buf); -static inline bool tuntap_stop (int status) {
@@ -466,36 +464,8 @@ tuntap_abort(int status)return false; } -static inline int -tun_write_win32 (struct tuntap *tt,struct buffer *buf) -{ -int err =0 ; -int status =0 ; -if (overlapped_io_active (&tt->writes)) - { - status =tun_finalize (tt->hand, &tt->writes,NULL ); -if (status <0 ) - { - err =GetLastError (); - } - } -tun_write_queue (tt, buf); -if (status <0 ) - { -SetLastError (err); -return status; - } -else - { -return BLEN (buf); - } -} - -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);static inline ULONG wintun_ring_packet_align (ULONG size)
Signature · 1 line
2.23.0.windows.1
PATCH v2 1/1 tun: remove tun_finalize()
In reply to Antonio QuartulliFrom: Lev Stipakov <[email protected]...>
tun_finalize() is essentially subset of socket_finalize() apart from:
- using WSAFoo() functions instead of Foo()
- "from" address is not returned
There is no clear official statement that one can use non-WSA API on handles, so let's be on a safe side and use both.
Introduce sockethandle_t abstraction, which represents socket and handle. Add SocketHandle* macros which call proper API depends on underlying type in abstraction.
Rename socket_finalize() to sockethandle_finalize(), take sockethandle_t and new macros into use and kick tun_finalize().
---
v2: explicitly initialize .is_handle to false for readablity
src/openvpn/forward.c | 3 +- src/openvpn/socket.c | 37 ++++++++--------- src/openvpn/socket.h | 31 ++++++++++++-- src/openvpn/tun.c | 94 +++++++++---------------------------------- src/openvpn/tun.h | 34 +--------------- 5 files changed, 65 insertions(+), 134 deletions(-)
src/openvpn/forward.c +2 -1
@@ -1115,7 +1115,8 @@ read_incoming_tun(struct context *c) }else { -read_tun_buffered (c->c1.tuntap, &c->c2.buf); +sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand }; +sockethandle_finalize (sh, &c->c1.tuntap->reads, &c->c2.buf,NULL ); }#else /* ifdef _WIN32 */ ASSERT (buf_init (&c->c2.buf,FRAME_HEADROOM (&c->c2.frame)));
src/openvpn/socket.c +16 -21
@@ -3198,7 +3198,8 @@ link_socket_read_tcp(struct link_socket *sock,if (!sock->stream_buf.residual_fully_formed) {#ifdef _WIN32 - len =socket_finalize (sock->sd, &sock->reads, buf,NULL ); +sockethandle_t sh = { .is_handle = false, .s = sock->sd }; + len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else struct buffer frag;stream_buf_get_next (&sock->stream_buf, &frag);
@@ -3664,10 +3665,10 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin }int -socket_finalize (SOCKET s, -struct overlapped_io *io, -struct buffer *buf, -struct link_socket_actual *from) +sockethandle_finalize (sockethandle_t sh, +struct overlapped_io *io, +struct buffer *buf, +struct link_socket_actual *from) {int ret =-1 ;BOOL status;
@@ -3675,13 +3676,7 @@ socket_finalize(SOCKET s,switch (io->iostate) {case IOSTATE_QUEUED : - status =WSAGetOverlappedResult ( - s, - &io->overlapped, - &io->size, - FALSE, - &io->flags - ); + status =SocketHandleGetOverlappedResult (sh, &io);if (status) {/* successful return for a queued operation */
@@ -3693,18 +3688,18 @@ socket_finalize(SOCKET s, io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion success [%d]" , ret); }else {/* error during a queued operation */ ret =-1 ; -if (WSAGetLastError () !=WSA_IO_INCOMPLETE ) +if (SocketHandleGetLastError (sh) !=ERROR_IO_INCOMPLETE ) {/* if no error (i.e. just not finished yet), then DON'T execute this code */ io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion error" ); } }break ;
@@ -3715,9 +3710,9 @@ socket_finalize(SOCKET s,if (io->status) {/* error return for a non-queued operation */ -WSASetLastError (io->status); +SocketHandleSetLastError (sh, io->status); ret =-1 ; -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion non-queued error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion non-queued error" ); }else {
@@ -3727,14 +3722,14 @@ socket_finalize(SOCKET s, *buf = io->buf; } ret = io->size; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion non-queued success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion non-queued success [%d]" , ret); }break ;case IOSTATE_INITIAL :/* were we called without proper queueing? */ -WSASetLastError (WSAEINVAL ); +SocketHandleSetInvalError (sh); ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion BAD STATE" ); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion BAD STATE" );break ;default :
@@ -3742,7 +3737,7 @@ socket_finalize(SOCKET s, }/* return from address if requested */ -if (from) +if (!sh.is_handle && from) {if (ret >=0 && io->addr_defined) {
src/openvpn/socket.h +27 -4
@@ -262,12 +262,33 @@ int socket_send_queue(struct link_socket *sock,struct buffer *buf, const struct link_socket_actual *to); -int socket_finalize ( -SOCKET s, +typedef struct { +union { +SOCKET s; +HANDLE h; + }; +bool is_handle; +}sockethandle_t ; + +int sockethandle_finalize ( +sockethandle_t sh,struct overlapped_io *io,struct buffer *buf,struct link_socket_actual *from); +#define SocketHandleGetOverlappedResult (sh, io) (sh.is_handle ? \ + GetOverlappedResult(sh.h, io->overlapped, io->size, FALSE) : \ + WSAGetOverlappedResult(sh.s, io->overlapped, io->size, FALSE, io->flags)) + +#define SocketHandleGetLastError (sh) (sh.is_handle ? \ + GetLastError() : WSAGetLastError()) + +#define SocketHandleSetLastError (sh, ...) (sh.is_handle ? \ + SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__)) + +#define SocketHandleSetInvalError (sh) (sh.is_handle ? \ + SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL)) +#else /* ifdef _WIN32 */ #define openvpn_close_socket (s) close(s)
@@ -1020,7 +1041,8 @@ link_socket_read_udp_win32(struct link_socket *sock,struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .is_handle = false, .s = sock->sd }; +return sockethandle_finalize (sh, &sock->reads, buf, from); }#else /* ifdef _WIN32 */
@@ -1078,9 +1100,10 @@ link_socket_write_win32(struct link_socket *sock, {int err =0 ;int status =0 ; +sockethandle_t sh = { .is_handle = false, .s = sock->sd };if (overlapped_io_active (&sock->writes)) { - status =socket_finalize (sock->sd, &sock->writes,NULL ,NULL ); + status =sockethandle_finalize (sh, &sock->writes,NULL ,NULL );if (status <0 ) { err =WSAGetLastError ();
src/openvpn/tun.c +18 -76
105 lines not on this page · raw message
src/openvpn/tun.h +2 -32
@@ -437,8 +437,6 @@ int tun_read_queue(struct tuntap *tt, int maxsize);int tun_write_queue (struct tuntap *tt,struct buffer *buf); -int tun_finalize (HANDLE h,struct overlapped_io *io,struct buffer *buf); -static inline bool tuntap_stop (int status) {
@@ -466,36 +464,8 @@ tuntap_abort(int status)return false; } -static inline int -tun_write_win32 (struct tuntap *tt,struct buffer *buf) -{ -int err =0 ; -int status =0 ; -if (overlapped_io_active (&tt->writes)) - { - status =tun_finalize (tt->hand, &tt->writes,NULL ); -if (status <0 ) - { - err =GetLastError (); - } - } -tun_write_queue (tt, buf); -if (status <0 ) - { -SetLastError (err); -return status; - } -else - { -return BLEN (buf); - } -} - -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);static inline ULONG wintun_ring_packet_align (ULONG size)
Signature · 1 line
2.23.0.windows.1
Quoted 5 lines
src/openvpn/socket.h +3
-0+#define SocketHandleGetOverlappedResult (sh, io) (sh.is_handle ? \ + GetOverlappedResult(sh.h, io->overlapped, io->size, FALSE) : \ + WSAGetOverlappedResult(sh.s, io->overlapped, io->size, FALSE, io->flags))
src/openvpn/socket.h int socket_send_queue(struct link_socket *sock, · line 291
Quoted 10 lines
src/openvpn/socket.h +10
-0+ +#define SocketHandleGetLastError (sh) (sh.is_handle ? \ + GetLastError() : WSAGetLastError()) + +#define SocketHandleSetLastError (sh, ...) (sh.is_handle ? \ + SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__)) + +#define SocketHandleSetInvalError (sh) (sh.is_handle ? \ + SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL)) +
I would prefer small static inline functions here. With modern compilers they get inlined and are nicer than these macros.
src/openvpn/socket.h link_socket_read_udp_win32(struct link_socket *sock, · line 1044
Quoted 9 lines
#else /* ifdef _WIN32 */ #define openvpn_close_socket(s) close(s)src/openvpn/socket.h +1
-1@@ -1020,7 +1041,8 @@ link_socket_read_udp_win32(struct link_socket *sock,struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .s = sock->sd };
It would be good to explicitly have is_handle = false here. Otherwise we rely on implicit zero initialisation, which I am not sure is guaranteed and it also is better for documentation.
Arne
Hi,
Quoted 15 lines · Arne Schwabe
Quoted 5 lines
+#define
SocketHandleGetOverlappedResult(sh, io)(sh.is_handle ? \
+GetOverlappedResult(sh.h, io->overlapped, io->size, FALSE): \
+WSAGetOverlappedResult(sh.s, io->overlapped, io->size, FALSE, io->flags))src/openvpn/socket.h int socket_send_queue(struct link_socket *sock, · line 291
Quoted 10 lines
src/openvpn/socket.h +10
-0+ +#define SocketHandleGetLastError (sh) (sh.is_handle ? \ + GetLastError() : WSAGetLastError()) + +#define SocketHandleSetLastError (sh, ...) (sh.is_handle ? \ + SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__)) + +#define SocketHandleSetInvalError (sh) (sh.is_handle ? \ + SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL)) +
I think macros are just way simpler at handling varargs, as you just use ... and __VA_ARGS__ in the code.
Quoted 5 lines
I would prefer small static inline functions here. With modern compilers they get inlined and are nicer than these macros.
Quoted 11 lines
src/openvpn/socket.h link_socket_read_udp_win32(struct link_socket · line 1045
Quoted 9 lines
#else /* ifdef
_WIN32*/
#defineopenvpn_close_socket(s)close(s)src/openvpn/socket.h +1
-1@@ -1020,7 +1041,8 @@ link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .s = sock->sd };It would be good to explicitly have
is_handle= false here. Otherwise we rely on implicit zero initialisation, which I am not sure is guaranteed and it also is better for documentation.
I agree here - especially on the documentation bit.
Cheers,
Quoted 9 lines
Arne
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Signature · 1 line
Antonio Quartulli
Hi Selva,
we were hoping to hear your opinion on this :-)
We spent quite some time figuring out if we have to use both the non-WSA and the WSA variant of the API in our code, and it seems we have to.
(not because using one variant only would not work, but rather because the spec demands using WSA with sockets and non-WSA with file handles)
What's your take?
Assuming you agree with us, does this patch look reasonable to you?
Thanks a lot!
Best Regards,
Quoted 372 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
tun_finalize()is essentially subset ofsocket_finalize()apart from:- using
WSAFoo()functions instead ofFoo()- "from" address is not returned
There is no clear official statement that one can use non-WSA API on handles, so let's be on a safe side and use both.
Introduce
sockethandle_tabstraction, which represents socket and handle. Add SocketHandle* macros which call proper API depends on underlying type in abstraction.Rename
socket_finalize()tosockethandle_finalize(), takesockethandle_tand new macros into use and kicktun_finalize().Signed-off-by Lev Stipakov <[email protected]...>---
v2: explicitly initialize .is_handle to false for readablitysrc/openvpn/forward.c | 3 +- src/openvpn/socket.c | 37 ++++++++--------- src/openvpn/socket.h | 31 ++++++++++++-- src/openvpn/tun.c | 94 +++++++++---------------------------------- src/openvpn/tun.h | 34 +--------------- 5 files changed, 65 insertions(+), 134 deletions(-)src/openvpn/forward.c +2
-1@@ -1115,7 +1115,8 @@ read_incoming_tun(struct context *c) }else { -read_tun_buffered (c->c1.tuntap, &c->c2.buf); +sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand }; +sockethandle_finalize (sh, &c->c1.tuntap->reads, &c->c2.buf,NULL ); }#else /* ifdef _WIN32 */ ASSERT (buf_init (&c->c2.buf,FRAME_HEADROOM (&c->c2.frame)));src/openvpn/socket.c +16
-21@@ -3198,7 +3198,8 @@ link_socket_read_tcp(struct link_socket *sock,if (!sock->stream_buf.residual_fully_formed) {#ifdef _WIN32 - len =socket_finalize (sock->sd, &sock->reads, buf,NULL ); +sockethandle_t sh = { .is_handle = false, .s = sock->sd }; + len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else struct buffer frag;stream_buf_get_next (&sock->stream_buf, &frag);@@ -3664,10 +3665,10 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin }int -socket_finalize (SOCKET s, -struct overlapped_io *io, -struct buffer *buf, -struct link_socket_actual *from) +sockethandle_finalize (sockethandle_t sh, +struct overlapped_io *io, +struct buffer *buf, +struct link_socket_actual *from) {int ret =-1 ;BOOL status;@@ -3675,13 +3676,7 @@ socket_finalize(SOCKET s,switch (io->iostate) {case IOSTATE_QUEUED : - status =WSAGetOverlappedResult ( - s, - &io->overlapped, - &io->size, - FALSE, - &io->flags - ); + status =SocketHandleGetOverlappedResult (sh, &io);if (status) {/* successful return for a queued operation */ @@ -3693,18 +3688,18 @@ socket_finalize(SOCKET s, io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion success [%d]" , ret); }else {/* error during a queued operation */ ret =-1 ; -if (WSAGetLastError () !=WSA_IO_INCOMPLETE ) +if (SocketHandleGetLastError (sh) !=ERROR_IO_INCOMPLETE ) {/* if no error (i.e. just not finished yet), then DON'T execute this code */ io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion error" ); } }break ;@@ -3715,9 +3710,9 @@ socket_finalize(SOCKET s,if (io->status) {/* error return for a non-queued operation */ -WSASetLastError (io->status); +SocketHandleSetLastError (sh, io->status); ret =-1 ; -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion non-queued error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion non-queued error" ); }else {@@ -3727,14 +3722,14 @@ socket_finalize(SOCKET s, *buf = io->buf; } ret = io->size; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion non-queued success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion non-queued success [%d]" , ret); }break ;case IOSTATE_INITIAL :/* were we called without proper queueing? */ -WSASetLastError (WSAEINVAL ); +SocketHandleSetInvalError (sh); ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion BAD STATE" ); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion BAD STATE" );break ;default :@@ -3742,7 +3737,7 @@ socket_finalize(SOCKET s, }/* return from address if requested */ -if (from) +if (!sh.is_handle && from) {if (ret >=0 && io->addr_defined) {src/openvpn/socket.h +27
-4@@ -262,12 +262,33 @@ int socket_send_queue(struct link_socket *sock,struct buffer *buf, const struct link_socket_actual *to); -int socket_finalize ( -SOCKET s, +typedef struct { +union { +SOCKET s; +HANDLE h; + }; +bool is_handle; +}sockethandle_t ; + +int sockethandle_finalize ( +sockethandle_t sh,struct overlapped_io *io,struct buffer *buf,struct link_socket_actual *from); +#define SocketHandleGetOverlappedResult (sh, io) (sh.is_handle ? \ + GetOverlappedResult(sh.h, io->overlapped, io->size, FALSE) : \ + WSAGetOverlappedResult(sh.s, io->overlapped, io->size, FALSE, io->flags)) + +#define SocketHandleGetLastError (sh) (sh.is_handle ? \ + GetLastError() : WSAGetLastError()) + +#define SocketHandleSetLastError (sh, ...) (sh.is_handle ? \ + SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__)) + +#define SocketHandleSetInvalError (sh) (sh.is_handle ? \ + SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL)) +#else /* ifdef _WIN32 */ #define openvpn_close_socket (s) close(s)@@ -1020,7 +1041,8 @@ link_socket_read_udp_win32(struct link_socket *sock,struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .is_handle = false, .s = sock->sd }; +return sockethandle_finalize (sh, &sock->reads, buf, from); }#else /* ifdef _WIN32 */ @@ -1078,9 +1100,10 @@ link_socket_write_win32(struct link_socket *sock, {int err =0 ;int status =0 ; +sockethandle_t sh = { .is_handle = false, .s = sock->sd };if (overlapped_io_active (&sock->writes)) { - status =socket_finalize (sock->sd, &sock->writes,NULL ,NULL ); + status =sockethandle_finalize (sh, &sock->writes,NULL ,NULL );if (status <0 ) { err =WSAGetLastError ();src/openvpn/tun.c +18
-76@@ -3561,87 +3561,29 @@ tun_write_queue(struct tuntap *tt, struct buffer *buf) }int -tun_finalize ( -HANDLE h, -struct overlapped_io *io, -struct buffer *buf) +tun_write_win32 (struct tuntap *tt,struct buffer *buf) { -int ret =-1 ; -BOOL status; - -switch (io->iostate) +int err =0 ; +int status =0 ; +if (overlapped_io_active (&tt->writes)) { -case IOSTATE_QUEUED : - status =GetOverlappedResult ( - h, - &io->overlapped, - &io->size, - FALSE - ); -if (status) - { -/* successful return for a queued operation */ -if (buf) - { - *buf = io->buf; - } - ret = io->size; - io->iostate =IOSTATE_INITIAL ; -ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: TAP Completion success [%d]" , ret); - } -else - { -/* error during a queued operation */ - ret =-1 ; -if (GetLastError () !=ERROR_IO_INCOMPLETE ) - { -/* if no error (i.e. just not finished yet), -* then DON'T execute this code */ - io->iostate =IOSTATE_INITIAL ; -ASSERT (ResetEvent (io->overlapped.hEvent)); -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: TAP Completion error" ); - } - } -break ; - -case IOSTATE_IMMEDIATE_RETURN : - io->iostate =IOSTATE_INITIAL ; -ASSERT (ResetEvent (io->overlapped.hEvent)); -if (io->status) - { -/* error return for a non-queued operation */ -SetLastError (io->status); - ret =-1 ; -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: TAP Completion non-queued error" ); - } -else - { -/* successful return for a non-queued operation */ -if (buf) - { - *buf = io->buf; - } - ret = io->size; -dmsg (D_WIN32_IO ,"WIN32 I/O: TAP Completion non-queued success [%d]" , ret); - } -break ; - -case IOSTATE_INITIAL :/* were we called without proper queueing? */ -SetLastError (ERROR_INVALID_FUNCTION ); - ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: TAP Completion BAD STATE" ); -break ; - -default : -ASSERT (0 ); +sockethandle_t sh = { .is_handle = true, .h = tt->hand }; + status =sockethandle_finalize (sh, &tt->writes,NULL ,NULL ); +if (status <0 ) + { + err =GetLastError (); + } } - -if (buf) +tun_write_queue (tt, buf); +if (status <0 ) { - buf->len = ret; +SetLastError (err); +return status; + } +else + { +return BLEN (buf); } -return ret; } static const struct device_instance_id_interface *src/openvpn/tun.h +2
-32@@ -437,8 +437,6 @@ int tun_read_queue(struct tuntap *tt, int maxsize);int tun_write_queue (struct tuntap *tt,struct buffer *buf); -int tun_finalize (HANDLE h,struct overlapped_io *io,struct buffer *buf); -static inline bool tuntap_stop (int status) {@@ -466,36 +464,8 @@ tuntap_abort(int status)return false; } -static inline int -tun_write_win32 (struct tuntap *tt,struct buffer *buf) -{ -int err =0 ; -int status =0 ; -if (overlapped_io_active (&tt->writes)) - { - status =tun_finalize (tt->hand, &tt->writes,NULL ); -if (status <0 ) - { - err =GetLastError (); - } - } -tun_write_queue (tt, buf); -if (status <0 ) - { -SetLastError (err); -return status; - } -else - { -return BLEN (buf); - } -} - -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);static inline ULONG wintun_ring_packet_align (ULONG size)
Signature · 1 line
Antonio Quartulli
Hi,
Quoted 5 lines · Antonio Quartulli
Hi Selva,
we were hoping to hear your opinion on this :-)
Quoted 8 lines
We spent quite some time figuring out if we have to use both the non-WSA and the WSA variant of the API in our code, and it seems we have to.
(not because using one variant only would not work, but rather because the spec demands using WSA with sockets and non-WSA with file handles)
What's your take?
Assuming you agree with us, does this patch look reasonable to you?
As far as I know, the reason for identical sounding functions with and without WSA prefix is mostly historical. e.g., WSAGet/SetLastError() and Get/SetLastError() are likely identical functions internally. Tests show error code set by one is overwritten by the other etc.
That said, it's better to stick to the docs and use WSA functions with sockets as opposed to file handles. In any case we have to distinguish the two for GetOverlappedResult() as the WSA version also returns an additional flag.
The way I see it, the patch is eliminating a chunk of nearly identical code, and use of an ambivalent struct holding socket/file-handle and a few macros is a small price to pay for that.
So, looks reasonable to me assuming any effect on performance is negligible.
A minor thing:
+
+#define SocketHandleSetLastError(sh, ...) (sh.is_handle ? \
+ SetLastError(__VA_ARGS__) : WSASetLastError(__VA_ARGS__))
Why __VA_ARGS__?
As SetLastError() takes a single argument, wouldn't
#define SocketHandleSetLastError(sh, err) (sh.is_handle ? SetLastError(err) : WSASetLastError(err))
be better?
Selva
PATCH v3 1/1 tun: remove tun_finalize()
From: Lev Stipakov <[email protected]...>
tun_finalize() is essentially subset of socket_finalize() apart from:
- using WSAFoo() functions instead of Foo()
- "from" address is not returned
There is no clear official statement that one can use non-WSA API on handles, so let's be on a safe side and use both.
Introduce sockethandle_t abstraction, which represents socket and handle. Add SocketHandle* routines which call proper API depends on underlying type in abstraction.
Rename socket_finalize() to sockethandle_finalize(), take sockethandle_t and new routines into use and kick tun_finalize().
---
v3: replace macros with inline functions
v2: explicitly initialize .is_handle to false for readablity
src/openvpn/forward.c | 3 +- src/openvpn/socket.c | 37 ++++++++--------- src/openvpn/socket.h | 49 ++++++++++++++++++---- src/openvpn/tun.c | 94 +++++++++---------------------------------- src/openvpn/tun.h | 34 +--------------- 5 files changed, 80 insertions(+), 137 deletions(-)
src/openvpn/forward.c +2 -1
@@ -1115,7 +1115,8 @@ read_incoming_tun(struct context *c) }else { -read_tun_buffered (c->c1.tuntap, &c->c2.buf); +sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand }; +sockethandle_finalize (sh, &c->c1.tuntap->reads, &c->c2.buf,NULL ); }#else /* ifdef _WIN32 */ ASSERT (buf_init (&c->c2.buf,FRAME_HEADROOM (&c->c2.frame)));
src/openvpn/socket.c +16 -21
@@ -3198,7 +3198,8 @@ link_socket_read_tcp(struct link_socket *sock,if (!sock->stream_buf.residual_fully_formed) {#ifdef _WIN32 - len =socket_finalize (sock->sd, &sock->reads, buf,NULL ); +sockethandle_t sh = { .s = sock->sd }; + len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else struct buffer frag;stream_buf_get_next (&sock->stream_buf, &frag);
@@ -3664,10 +3665,10 @@ socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct lin }int -socket_finalize (SOCKET s, -struct overlapped_io *io, -struct buffer *buf, -struct link_socket_actual *from) +sockethandle_finalize (sockethandle_t sh, +struct overlapped_io *io, +struct buffer *buf, +struct link_socket_actual *from) {int ret =-1 ;BOOL status;
@@ -3675,13 +3676,7 @@ socket_finalize(SOCKET s,switch (io->iostate) {case IOSTATE_QUEUED : - status =WSAGetOverlappedResult ( - s, - &io->overlapped, - &io->size, - FALSE, - &io->flags - ); + status =SocketHandleGetOverlappedResult (sh, io);if (status) {/* successful return for a queued operation */
@@ -3693,18 +3688,18 @@ socket_finalize(SOCKET s, io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion success [%d]" , ret); }else {/* error during a queued operation */ ret =-1 ; -if (WSAGetLastError () !=WSA_IO_INCOMPLETE ) +if (SocketHandleGetLastError (sh) !=ERROR_IO_INCOMPLETE ) {/* if no error (i.e. just not finished yet), then DON'T execute this code */ io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion error" ); } }break ;
@@ -3715,9 +3710,9 @@ socket_finalize(SOCKET s,if (io->status) {/* error return for a non-queued operation */ -WSASetLastError (io->status); +SocketHandleSetLastError (sh, io->status); ret =-1 ; -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Socket Completion non-queued error" ); +msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: Completion non-queued error" ); }else {
@@ -3727,14 +3722,14 @@ socket_finalize(SOCKET s, *buf = io->buf; } ret = io->size; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion non-queued success [%d]" , ret); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion non-queued success [%d]" , ret); }break ;case IOSTATE_INITIAL :/* were we called without proper queueing? */ -WSASetLastError (WSAEINVAL ); +SocketHandleSetInvalError (sh); ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion BAD STATE" ); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion BAD STATE" );break ;default :
@@ -3742,7 +3737,7 @@ socket_finalize(SOCKET s, }/* return from address if requested */ -if (from) +if (!sh.is_handle && from) {if (ret >=0 && io->addr_defined) {
src/openvpn/socket.h +42 -7
69 lines not on this page · raw message
src/openvpn/tun.c +18 -76
105 lines not on this page · raw message
src/openvpn/tun.h +2 -32
@@ -437,8 +437,6 @@ int tun_read_queue(struct tuntap *tt, int maxsize);int tun_write_queue (struct tuntap *tt,struct buffer *buf); -int tun_finalize (HANDLE h,struct overlapped_io *io,struct buffer *buf); -static inline bool tuntap_stop (int status) {
@@ -466,36 +464,8 @@ tuntap_abort(int status)return false; } -static inline int -tun_write_win32 (struct tuntap *tt,struct buffer *buf) -{ -int err =0 ; -int status =0 ; -if (overlapped_io_active (&tt->writes)) - { - status =tun_finalize (tt->hand, &tt->writes,NULL ); -if (status <0 ) - { - err =GetLastError (); - } - } -tun_write_queue (tt, buf); -if (status <0 ) - { -SetLastError (err); -return status; - } -else - { -return BLEN (buf); - } -} - -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);static inline ULONG wintun_ring_packet_align (ULONG size)
Signature · 1 line
2.23.0.windows.1
Hi,
On 17/01/2022 10:49, Lev Stipakov wrote:
[cut]
src/openvpn/tun.h tuntap_abort(int status) · line 468
Quoted 8 lines
src/openvpn/tun.h +2
-6- -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);
for prototypes we put the return type on the same line as the function name (unless we have line length issues, but not in this case).
Cheers,
Signature · 1 line
Antonio Quartulli
This is probably something committer (looks at cron2) could fix on the fly, unless there are more issues which would require v4?
ma 17. tammik. 2022 klo 11.53 Antonio Quartulli ([email protected]...) kirjoitti:
Quoted 22 lines
Hi,
On 17/01/2022 10:49, Lev Stipakov wrote:
[cut]src/openvpn/tun.h tuntap_abort(int status) · line 468
Quoted 8 lines
src/openvpn/tun.h +2
-6- -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);for prototypes we put the return type on the same line as the function name (unless we have line length issues, but not in this case).
Cheers,
--
Antonio Quartulli
Signature · 1 line
-Lev
Hi,
Quoted 2 lines · Lev Stipakov
This is probably something committer (looks at cron2) could fix on the fly, unless there are more issues which would require v4?
If that's the only thing, I can fix that on the fly.
gert
Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted
it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh Mistress
Gert Doering - Munich, Germany [email protected]...Gentle nudge.
ma 17. tammik. 2022 klo 13.42 Gert Doering ([email protected]...) kirjoitti:
Quoted 17 lines
Hi,
Quoted 2 lines · Lev Stipakov
This is probably something committer (looks at cron2) could fix on the fly, unless there are more issues which would require v4?
If that's the only thing, I can fix that on the fly.
gert
--
"If was one thing all people took for granted, was conviction that if you
feed honest figures into a computer, honest figures come out. Never doubted it myself till I met a computer with a sense of humor."
Robert A. Heinlein, The Moon is a Harsh MistressGert Doering - Munich, Germany [email protected]...
Signature · 1 line
-Lev
Hi,
Quoted 98 lines · Lev Stipakov
From: Lev Stipakov <[email protected]...>
tun_finalize()is essentially subset ofsocket_finalize()apart from:- using
WSAFoo()functions instead ofFoo()- "from" address is not returned
There is no clear official statement that one can use non-WSA API on handles, so let's be on a safe side and use both.
Introduce
sockethandle_tabstraction, which represents socket and handle. Add SocketHandle* routines which call proper API depends on underlying type in abstraction.Rename
socket_finalize()tosockethandle_finalize(), takesockethandle_tand new routines into use and kicktun_finalize().Signed-off-by Lev Stipakov <[email protected]...>---
v3: replace macros with inline functionsv2: explicitly initialize .is_handle to false for readablity
src/openvpn/forward.c | 3 +- src/openvpn/socket.c | 37 ++++++++--------- src/openvpn/socket.h | 49 ++++++++++++++++++---- src/openvpn/tun.c | 94 +++++++++---------------------------------- src/openvpn/tun.h | 34 +--------------- 5 files changed, 80 insertions(+), 137 deletions(-)src/openvpn/forward.c +2
-1@@ -1115,7 +1115,8 @@ read_incoming_tun(struct context *c) }else { -read_tun_buffered (c->c1.tuntap, &c->c2.buf); +sockethandle_t sh = { .is_handle = true, .h = c->c1.tuntap->hand }; +sockethandle_finalize (sh, &c->c1.tuntap->reads, &c->c2.buf,NULL ); }#else /* ifdef _WIN32 */ ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));src/openvpn/socket.c +2
-1@@ -3198,7 +3198,8 @@ link_socket_read_tcp(struct link_socket *sock,if (!sock->stream_buf.residual_fully_formed) {#ifdef _WIN32 - len =socket_finalize (sock->sd, &sock->reads, buf,NULL ); +sockethandle_t sh = { .s = sock->sd }; + len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else struct buffer frag;stream_buf_get_next (&sock->stream_buf, &frag);@@ -3664,10 +3665,10 @@ socket_send_queue(struct link_socket *sock, structbuffer *buf, const struct lin
}int
src/openvpn/socket.c +5
-12-socket_finalize(SOCKET s, - struct overlapped_io *io, - struct buffer *buf, - struct link_socket_actual *from) +sockethandle_finalize(sockethandle_t sh, + struct overlapped_io *io, + struct buffer *buf, + struct link_socket_actual *from) { int ret = -1;BOOL status;@@ -3675,13 +3676,7 @@ socket_finalize(SOCKET s,switch (io->iostate) {case IOSTATE_QUEUED : - status =WSAGetOverlappedResult ( - s, - &io->overlapped, - &io->size, - FALSE, - &io->flags - ); + status =SocketHandleGetOverlappedResult (sh, io);if (status) {/* successful return for a queued operation */ @@ -3693,18 +3688,18 @@ socket_finalize(SOCKET s, io->iostate =IOSTATE_INITIAL ;ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion success [%d]", ret);
src/openvpn/socket.c socket_finalize(SOCKET s, · line 3700
Quoted 10 lines
src/openvpn/socket.c +2
-1+dmsg (D_WIN32_IO ,"WIN32 I/O: Completion success [%d]" , ret); }else {/* error during a queued operation */ ret =-1 ; -if (WSAGetLastError () !=WSA_IO_INCOMPLETE ) +if (SocketHandleGetLastError (sh) !=ERROR_IO_INCOMPLETE )
WSA_IO_INCOMPLETE is the same as ERROR_IO_INCOMPLETE, so using them interchangeably looks ok.
{Quoted 92 lines
src/openvpn/socket.c +0
-0/* if no error (i.e. just not finished yet), then DON'T execute this code */
io->iostate = IOSTATE_INITIAL; ASSERT(ResetEvent(io->overlapped.hEvent)); - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Socket Completion error"); + msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion error"); } } break;src/openvpn/socket.c +2
-2@@ -3715,9 +3710,9 @@ socket_finalize(SOCKET s, if (io->status) { /* error return for a non-queued operation */ - WSASetLastError(io->status); + SocketHandleSetLastError(sh, io->status); ret = -1; - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Socket Completion non-queued error"); + msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion non-queued error"); }else {src/openvpn/socket.c +3
-3@@ -3727,14 +3722,14 @@ socket_finalize(SOCKET s, *buf = io->buf; } ret = io->size; - dmsg(D_WIN32_IO, "WIN32 I/O: Socket Completion non-queued success [%d]", ret); + dmsg(D_WIN32_IO, "WIN32 I/O: Completion non-queued success [%d]", ret); }break ;case IOSTATE_INITIAL :/* were we called without proper queueing? */ -WSASetLastError (WSAEINVAL ); +SocketHandleSetInvalError (sh); ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: Socket Completion BAD STATE" ); +dmsg (D_WIN32_IO ,"WIN32 I/O: Completion BAD STATE" );break; default:src/openvpn/socket.c +1
-1@@ -3742,7 +3737,7 @@ socket_finalize(SOCKET s, }/* return from address if requested */ -if (from) +if (!sh.is_handle && from) {if (ret >=0 && io->addr_defined) {src/openvpn/socket.h +22
-5@@ -262,11 +262,44 @@ int socket_send_queue(struct link_socket *sock,struct buffer *buf, const struct link_socket_actual *to); -int socket_finalize ( -SOCKET s, -struct overlapped_io *io, -struct buffer *buf, -struct link_socket_actual *from); +typedef struct { +union { +SOCKET s; +HANDLE h; + }; +bool is_handle; +}sockethandle_t ; + +int sockethandle_finalize (sockethandle_t sh, +struct overlapped_io *io, +struct buffer *buf, +struct link_socket_actual *from); + +static inline BOOL +SocketHandleGetOverlappedResult (sockethandle_t sh,struct overlapped_io *io) +{ +return sh.is_handle ? +GetOverlappedResult (sh.h, &io->overlapped, &io->size, FALSE) : +WSAGetOverlappedResult (sh.s, &io->overlapped, &io->size, FALSE, &io->flags); +} + +static inline int
src/openvpn/socket.h int socket_send_queue(struct link_socket *sock, · line 298
Quoted 10 lines
src/openvpn/socket.h +9
-0+SocketHandleGetLastError(sockethandle_t sh) +{ +return sh.is_handle ? (int )GetLastError () :WSAGetLastError (); +} + +inline static void +SocketHandleSetLastError (sockethandle_t sh,DWORD err) +{ + sh.is_handle ?SetLastError (err) :WSASetLastError (err);
To be consistent, one could add an explicit cast here too and avoid a potential compiler warning:
WSASetLastError((int) err);
Quoted 9 lines
src/openvpn/socket.h +6
-0+} + +static inline void +SocketHandleSetInvalError (sockethandle_t sh) +{ + sh.is_handle ? SetLastError(ERROR_INVALID_FUNCTION) :WSASetLastError(WSAEINVAL);
+}
I'm not sure whether ERROR_INVALID_FUNCTION is the appropriate error code here. WSAEINVAL is more like invalid-argument (so ERROR_BAD_ARGUMENTS?). But this agrees with the original, so let it be.
Quoted 181 lines
#else /* ifdef
_WIN32*/src/openvpn/socket.h +4
-2@@ -1020,7 +1053,8 @@ link_socket_read_udp_win32(struct link_socket *sock,struct buffer *buf, struct link_socket_actual *from) { - return socket_finalize(sock->sd, &sock->reads, buf, from); + sockethandle_t sh = { .s = sock->sd }; +return sockethandle_finalize (sh, &sock->reads, buf, from); }#else /* ifdef _WIN32 */ @@ -1078,9 +1112,10 @@ link_socket_write_win32(struct link_socket *sock, {int err =0 ;int status =0 ; +sockethandle_t sh = { .s = sock->sd };if (overlapped_io_active (&sock->writes)) { - status =socket_finalize (sock->sd, &sock->writes,NULL ,NULL ); + status =sockethandle_finalize (sh, &sock->writes,NULL ,NULL );if (status <0 ) { err =WSAGetLastError ();src/openvpn/tun.c +18
-76@@ -3561,87 +3561,29 @@ tun_write_queue(struct tuntap *tt, struct buffer*buf) } int -tun_finalize ( -HANDLE h, -struct overlapped_io *io, -struct buffer *buf) +tun_write_win32(struct tuntap *tt, struct buffer *buf) { -int ret =-1 ; -BOOL status; - -switch (io->iostate) +int err = 0; +int status = 0; +if (overlapped_io_active(&tt->writes)) { -case IOSTATE_QUEUED : - status =GetOverlappedResult ( - h, - &io->overlapped, - &io->size, - FALSE - ); -if (status) - { -/* successful return for a queued operation */ -if (buf) - { - *buf = io->buf; - } - ret = io->size; - io->iostate =IOSTATE_INITIAL ; -ASSERT (ResetEvent (io->overlapped.hEvent)); -dmsg (D_WIN32_IO ,"WIN32 I/O: TAP Completion success [%d]", ret); - } -else - { -/* error during a queued operation */ - ret =-1 ; -if (GetLastError () !=ERROR_IO_INCOMPLETE ) - { -/* if no error (i.e. just not finished yet), -* then DON'T execute this code */ - io->iostate =IOSTATE_INITIAL ; -ASSERT (ResetEvent (io->overlapped.hEvent)); -msg (D_WIN32_IO |M_ERRNO ,"WIN32 I/O: TAP Completion error"); - } - } -break ; - -case IOSTATE_IMMEDIATE_RETURN : - io->iostate =IOSTATE_INITIAL ; -ASSERT (ResetEvent (io->overlapped.hEvent)); -if (io->status) - { -/* error return for a non-queued operation */ -SetLastError (io->status); - ret =-1 ; - msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: TAP Completionnon-queued error"); - } - else - { - /* successful return for a non-queued operation */ - if (buf) - { - *buf = io->buf; - } - ret = io->size; - dmsg(D_WIN32_IO, "WIN32 I/O: TAP Completion non-queuedsuccess [%d]", ret); - } -break ; - -case IOSTATE_INITIAL :/* were we called without proper queueing? */ -SetLastError (ERROR_INVALID_FUNCTION ); - ret =-1 ; -dmsg (D_WIN32_IO ,"WIN32 I/O: TAP Completion BAD STATE" ); -break ; - -default : -ASSERT (0 ); +sockethandle_t sh = { .is_handle = true, .h = tt->hand }; + status =sockethandle_finalize (sh, &tt->writes,NULL ,NULL ); +if (status <0 ) + { + err =GetLastError (); + } } - -if (buf) +tun_write_queue (tt, buf); +if (status <0 ) { - buf->len = ret; +SetLastError (err); +return status; + } +else + { +return BLEN (buf); } -return ret; } static const struct device_instance_id_interface *src/openvpn/tun.h +0
-25@@ -437,8 +437,6 @@ int tun_read_queue(struct tuntap *tt, int maxsize);int tun_write_queue (struct tuntap *tt,struct buffer *buf); -int tun_finalize (HANDLE h,struct overlapped_io *io,struct buffer *buf); -static inline bool tuntap_stop (int status) {@@ -466,36 +464,8 @@ tuntap_abort(int status)return false; } -static inline int -tun_write_win32 (struct tuntap *tt,struct buffer *buf) -{ -int err =0 ; -int status =0 ; -if (overlapped_io_active (&tt->writes)) - { - status =tun_finalize (tt->hand, &tt->writes,NULL ); -if (status <0 ) - { - err =GetLastError (); - } - } -tun_write_queue (tt, buf); -if (status <0 ) - { -SetLastError (err); -return status; - } -else - { -return BLEN (buf); - }
src/openvpn/tun.h tuntap_abort(int status) · line 469
Quoted 10 lines
src/openvpn/tun.h +2
-7-} - -static inline int -read_tun_buffered (struct tuntap *tt,struct buffer *buf) -{ -return tun_finalize (tt->hand, &tt->reads, buf); -} +int +tun_write_win32 (struct tuntap *tt,struct buffer *buf);
Antonio wanted this to be in one line though we are not terribly consistent about this.
src/openvpn/tun.h tuntap_abort(int status) · line 471
Quoted 3 lines
src/openvpn/tun.h +0
-0static inline ULONG wintun_ring_packet_align (ULONG size)
In spite of those nits meant to annoy the author, I think this looks good.
Test compiled with MinGW. Not runtime tested.
Adjusted the prototype to be in-one-line, as instructed. And have ignored the other nits :-)
Your patch has been applied to the master branch.
commit bcf04b0b8e3c0f142ab0ec97627ea140c80c7962
Author: Lev Stipakov Date: Mon Jan 17 11:49:17 2022 +0200
tun: remove tun_finalize()
Signed-off-by: Lev Stipakov <[email protected]...>
Acked-by: Selva Nair <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg23555.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering