]> git.sesse.net Git - vlc/blob - src/network/winsock.c
Win32: fix poll() return value
[vlc] / src / network / 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
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, 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 <errno.h>
27 #include <vlc_network.h>
28
29 typedef struct
30 {
31     int code;
32     const char *msg;
33 } wsaerrmsg_t;
34
35 static const wsaerrmsg_t wsaerrmsg[] =
36 {
37     { WSA_INVALID_HANDLE, "Specified event object handle is invalid" },
38     { WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available" },
39     { WSA_INVALID_PARAMETER, "One or more parameters are invalid" },
40     { WSA_OPERATION_ABORTED, "Overlapped operation aborted" },
41     { WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state" },
42     { WSA_IO_PENDING, "Overlapped operations will complete later" },
43     { WSAEINTR, "Interrupted function call" },
44     { WSAEBADF, "File handle is not valid" },
45     { WSAEACCES, "Access denied" },
46     { WSAEFAULT, "Invalid memory address" },
47     { WSAEINVAL, "Invalid argument" },
48     { WSAEMFILE, "Too many open sockets" },
49     { WSAEWOULDBLOCK, "Resource temporarily unavailable" },
50     { WSAEINPROGRESS, "Operation now in progress" },
51     { WSAEALREADY, "Operation already in progress" },
52     { WSAENOTSOCK, "Non-socket handle specified" },
53     { WSAEDESTADDRREQ, "Missing destination address" },
54     { WSAEMSGSIZE, "Message too long" },
55     { WSAEPROTOTYPE, "Protocol wrong type for socket", },
56     { WSAENOPROTOOPT, "Option not supported by protocol" },
57     { WSAEPROTONOSUPPORT, "Protocol not supported" },
58     { WSAESOCKTNOSUPPORT, "Socket type not supported" },
59     { WSAEOPNOTSUPP, "Operation not supported" },
60     { WSAEPFNOSUPPORT, "Protocol family not supported" },
61     { WSAEAFNOSUPPORT, "Address family not supported by protocol family" },
62     { WSAEADDRINUSE, "Address already in use" },
63     { WSAEADDRNOTAVAIL, "Cannot assign requested address" },
64     { WSAENETDOWN, "Network is down" },
65     { WSAENETUNREACH, "Network unreachable" },
66     { WSAENETRESET, "Network dropped connection on reset" },
67     { WSAECONNABORTED, "Software caused connection abort" },
68     { WSAECONNRESET, "Connection reset by peer" },
69     { WSAENOBUFS, "No buffer space available (not enough memory)" },
70     { WSAEISCONN, "Socket is already connected" },
71     { WSAENOTCONN, "Socket is not connected" },
72     { WSAESHUTDOWN, "Cannot send after socket shutdown" },
73     { WSAETOOMANYREFS, "Too many references" },
74     { WSAETIMEDOUT, "Connection timed out" },
75     { WSAECONNREFUSED, "Connection refused by peer" },
76     { WSAELOOP, "Cannot translate name" },
77     { WSAENAMETOOLONG, "Name too long" },
78     { WSAEHOSTDOWN, "Remote host is down" },
79     { WSAEHOSTUNREACH, "No route to host (unreachable)" },
80     { WSAENOTEMPTY, "Directory not empty" },
81     { WSAEPROCLIM, "Too many processes" },
82     { WSAEUSERS, "User quota exceeded" },
83     { WSAEDQUOT, "Disk quota exceeded" },
84     { WSAESTALE, "Stale file handle reference" },
85     { WSAEREMOTE, "Item is remote", },
86     { WSASYSNOTREADY, "Network subsystem is unavailable (network stack not ready)" },
87     { WSAVERNOTSUPPORTED, "Winsock.dll version out of range (network stack version not supported" },
88     { WSANOTINITIALISED, "Network not initialized" },
89     { WSAEDISCON, "Graceful shutdown in progress" },
90     { WSAENOMORE, "No more results" },
91     { WSAECANCELLED, "Call has been cancelled" },
92     { WSAEINVALIDPROCTABLE, "Procedure call table is invalid" },
93     { WSAEINVALIDPROVIDER, "Service provider is invalid" },
94     { WSAEPROVIDERFAILEDINIT, "Service provider failed to initialize" },
95     { WSASYSCALLFAILURE, "System call failure" },
96     { WSASERVICE_NOT_FOUND, "Service not found" },
97     { WSATYPE_NOT_FOUND, "Class type not found" },
98     { WSA_E_NO_MORE, "No more results" },
99     { WSA_E_CANCELLED, "Call was cancelled" },
100     { WSAEREFUSED, "Database query was refused" },
101     { WSAHOST_NOT_FOUND, "Host not found" },
102     { WSATRY_AGAIN, "Nonauthoritative host not found (temporary hostname error)" },
103     { WSANO_RECOVERY, "Non-recoverable hostname error" },
104     { WSANO_DATA, "Valid name, no data record of requested type" },
105     { WSA_QOS_RECEIVERS, "QOS receivers" },
106     { WSA_QOS_SENDERS, "QOS senders" },
107     { WSA_QOS_NO_SENDERS, "No QOS senders" },
108     { WSA_QOS_NO_RECEIVERS, "QOS no receivers" },
109     { WSA_QOS_REQUEST_CONFIRMED, "QOS request confirmed" },
110     { WSA_QOS_ADMISSION_FAILURE, "QOS admission error" },
111     { WSA_QOS_POLICY_FAILURE, "QOS policy failure" },
112     { WSA_QOS_BAD_STYLE, "QOS bad style" },
113     { WSA_QOS_BAD_OBJECT, "QOS bad object" },
114     { WSA_QOS_TRAFFIC_CTRL_ERROR, "QOS traffic control error" },
115     { WSA_QOS_GENERIC_ERROR, "QOS generic error" },
116     { WSA_QOS_ESERVICETYPE, "QOS service type error" },
117     { WSA_QOS_EFLOWSPEC, "QOS flowspec error" },
118     { WSA_QOS_EPROVSPECBUF, "Invalid QOS provider buffer" },
119     { WSA_QOS_EFILTERSTYLE, "Invalid QOS filter style" },
120     { WSA_QOS_EFILTERTYPE, "Invalid QOS filter type" },
121     { WSA_QOS_EFILTERCOUNT, "Incorrect QOS filter count" },
122     { WSA_QOS_EOBJLENGTH, "Invalid QOS object length" },
123     { WSA_QOS_EFLOWCOUNT, "Incorrect QOS flow count" },
124     { WSA_QOS_EUNKNOWNPSOBJ, "Unrecognized QOS object" },
125     { WSA_QOS_EPOLICYOBJ, "Invalid QOS policy object" },
126     { WSA_QOS_EFLOWDESC, "Invalid QOS flow descriptor" },
127     { WSA_QOS_EPSFLOWSPEC, "Invalid QOS provider-specific flowspec" },
128     { WSA_QOS_EPSFILTERSPEC, "Invalid QOS provider-specific filterspec" },
129     { WSA_QOS_ESDMODEOBJ, "Invalid QOS shape discard mode object" },
130     { WSA_QOS_ESHAPERATEOBJ, "Invalid QOS shaping rate object" },
131     { WSA_QOS_RESERVED_PETYPE, "Reserved policy QOS element type" },
132     { 0, NULL }
133     /* Winsock2 error codes are missing, they "never" occur */
134 };
135
136
137 const char *net_strerror( int value )
138 {
139     /* There doesn't seem to be any portable error message generation for
140      * Winsock errors. Some old versions had s_error, but it appears to be
141      * gone, and is not documented.
142      */
143     for( const wsaerrmsg_t *e = wsaerrmsg; e->msg != NULL; e++ )
144         if( e->code == value )
145             return e->msg;
146
147     /* Remember to update src/misc/messages.c if you change this one */
148     return "Unknown network stack error";
149 }
150
151 ssize_t vlc_sendmsg (int s, struct msghdr *hdr, int flags)
152 {
153     /* WSASendMsg would be more straightforward, and would support ancilliary
154      * data, but it's not yet in mingw32. */
155     if ((hdr->msg_iovlen > 100) || (hdr->msg_controllen > 0))
156     {
157         errno = EINVAL;
158         return -1;
159     }
160
161     WSABUF buf[hdr->msg_iovlen];
162     for (size_t i = 0; i < sizeof (buf) / sizeof (buf[0]); i++)
163         buf[i].buf = hdr->msg_iov[i].iov_base,
164         buf[i].len = hdr->msg_iov[i].iov_len;
165
166     DWORD sent;
167     if (WSASendTo (s, buf, sizeof (buf) / sizeof (buf[0]), &sent, flags,
168                    hdr->msg_name, hdr->msg_namelen, NULL, NULL) == 0)
169         return sent;
170     return -1;
171 }
172
173 ssize_t vlc_recvmsg (int s, struct msghdr *hdr, int flags)
174 {
175     /* WSARecvMsg would be more straightforward, and would support ancilliary
176      * data, but it's not yet in mingw32. */
177     if (hdr->msg_iovlen > 100)
178     {
179         errno = EINVAL;
180         return -1;
181     }
182
183     WSABUF buf[hdr->msg_iovlen];
184     for (size_t i = 0; i < sizeof (buf) / sizeof (buf[0]); i++)
185         buf[i].buf = hdr->msg_iov[i].iov_base,
186         buf[i].len = hdr->msg_iov[i].iov_len;
187
188     DWORD recvd, dwFlags = flags;
189     INT fromlen = hdr->msg_namelen;
190     hdr->msg_controllen = 0;
191     hdr->msg_flags = 0;
192
193     int ret = WSARecvFrom (s, buf, sizeof (buf) / sizeof (buf[0]), &recvd,
194                            &dwFlags, hdr->msg_name, &fromlen, NULL, NULL);
195     hdr->msg_namelen = fromlen;
196     hdr->msg_flags = dwFlags;
197     if (ret == 0)
198         return recvd;
199
200 #ifdef MSG_TRUNC
201     if (WSAGetLastError() == WSAEMSGSIZE)
202     {
203         hdr->msg_flags |= MSG_TRUNC;
204         return recvd;
205     }
206 #else
207 # warning Out-of-date Winsock header files!
208 #endif
209     return -1;
210 }