mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-04 00:03:54 -06:00
util: add socket_set_fast_reuse function which will replace setting SO_REUSEADDR
If a socket is closed it remains in TIME_WAIT state for some time. On operating systems using BSD sockets the endpoint of the socket may not be reused while in this state unless SO_REUSEADDR was set on the socket. On windows on the other hand the default behaviour is to allow reuse (i.e. identical to SO_REUSEADDR on other operating systems) and setting SO_REUSEADDR on a socket allows it to be bound to a endpoint even if the endpoint is already used by another socket independently of the other sockets state. This can even result in undefined behaviour. Many sockets used by QEMU should not block the use of their endpoint after being closed while they are still in TIME_WAIT state. Currently QEMU sets SO_REUSEADDR for such sockets, which can lead to problems on Windows. This patch introduces the function socket_set_fast_reuse that should be used instead of setting SO_REUSEADDR when fast socket reuse is desired and behaves correctly on all operating systems. As a failure of this function can only be caused by bad QEMU internal errors, an assertion handles these situations. The return value is still passed on, to minimize changes in client code and prevent unused variable warnings if NDEBUG is defined. Signed-off-by: Sebastian Ottlik <ottlik@fzi.de> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
a684f3cf9b
commit
606600a176
3 changed files with 23 additions and 0 deletions
|
@ -157,6 +157,18 @@ void qemu_set_nonblock(int fd)
|
|||
fcntl(fd, F_SETFL, f | O_NONBLOCK);
|
||||
}
|
||||
|
||||
int socket_set_fast_reuse(int fd)
|
||||
{
|
||||
int val = 1, ret;
|
||||
|
||||
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const char *)&val, sizeof(val));
|
||||
|
||||
assert(ret == 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void qemu_set_cloexec(int fd)
|
||||
{
|
||||
int f;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue