]> git.sesse.net Git - vlc/blob - src/win32/winsock.c
udp: fix locking (fixes #14234) and cancellation
[vlc] / src / win32 / winsock.c
1 /*****************************************************************************
2  * winsock.c: POSIX replacements for Winsock
3  *****************************************************************************
4  * Copyright © 2006-2008 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_network.h>
27
28 #if 0
29 ssize_t vlc_sendmsg (int s, struct msghdr *hdr, int flags)
30 {
31     /* WSASendMsg would be more straightforward, and would support ancilliary
32      * data, but it's not yet in mingw32. */
33     if ((hdr->msg_iovlen > 100) || (hdr->msg_controllen > 0))
34     {
35         errno = EINVAL;
36         return -1;
37     }
38
39     WSABUF buf[hdr->msg_iovlen];
40     for (size_t i = 0; i < sizeof (buf) / sizeof (buf[0]); i++)
41         buf[i].buf = hdr->msg_iov[i].iov_base,
42         buf[i].len = hdr->msg_iov[i].iov_len;
43
44     DWORD sent;
45     if (WSASendTo (s, buf, sizeof (buf) / sizeof (buf[0]), &sent, flags,
46                    hdr->msg_name, hdr->msg_namelen, NULL, NULL) == 0)
47         return sent;
48     return -1;
49 }
50
51 ssize_t vlc_recvmsg (int s, struct msghdr *hdr, int flags)
52 {
53     /* WSARecvMsg would be more straightforward, and would support ancilliary
54      * data, but it's not yet in mingw32. */
55     if (hdr->msg_iovlen > 100)
56     {
57         errno = EINVAL;
58         return -1;
59     }
60
61     WSABUF buf[hdr->msg_iovlen];
62     for (size_t i = 0; i < sizeof (buf) / sizeof (buf[0]); i++)
63         buf[i].buf = hdr->msg_iov[i].iov_base,
64         buf[i].len = hdr->msg_iov[i].iov_len;
65
66     DWORD recvd, dwFlags = flags;
67     INT fromlen = hdr->msg_namelen;
68     hdr->msg_controllen = 0;
69     hdr->msg_flags = 0;
70
71     int ret = WSARecvFrom (s, buf, sizeof (buf) / sizeof (buf[0]), &recvd,
72                            &dwFlags, hdr->msg_name, &fromlen, NULL, NULL);
73     hdr->msg_namelen = fromlen;
74     hdr->msg_flags = dwFlags;
75     if (ret == 0)
76         return recvd;
77
78 #ifdef MSG_TRUNC
79     if (WSAGetLastError() == WSAEMSGSIZE)
80     {
81         hdr->msg_flags |= MSG_TRUNC;
82         return recvd;
83     }
84 #else
85 # warning Out-of-date Winsock header files!
86 #endif
87     return -1;
88 }
89 #endif