Debian Codegraph @

src/win32_net.c

raw
1 /*
2 win32_net: Windows-specific network code
3
4 copyright 2009-2023 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Jonathan Yong (extracting out of httpget.c)
7 */
8
9 #include "win32_support.h"
10 #include "mpg123app.h"
11 #include "common/debug.h"
12
13 #ifdef DEBUG
14 #define msgme(x) win32_net_msg(x,__FILE__,__LINE__)
15 #define msgme1 win32_net_msg(1,__FILE__,__LINE__)
16 #define msgme_sock_err(x) if ((x)==SOCKET_ERROR) {msgme1;}
17 #else
18 #define msgme(x) x
19 #define msgme1 do{} while(0)
20 #define msgme_sock_err(x) x
21 #endif
22 struct ws_local
23 {
24 int inited;
25 SOCKET local_socket; /*stores last connet in win32_net_open_connection*/
26 WSADATA wsadata;
27 };
28
29 static struct ws_local ws;
30 #ifdef DEBUG
31 static void win32_net_msg (const int err, const char * const filedata, const int linedata)
32 {
33 char *errbuff;
34 int lc_err;
35 if (err)
36 {
37 lc_err = WSAGetLastError();
38 FormatMessage(
39 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
40 NULL,
41 lc_err,
42 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
43 (LPTSTR) &errbuff,
44 0,
45 NULL );
46 fprintf(stderr, "[%s:%d] [WSA2: %d] %s", filedata, linedata, lc_err, errbuff);
47 LocalFree (errbuff);
48 }
49 }
50 #endif
51
52 void win32_net_init (void)
53 {
54 ws.inited = 1;
55 switch ((WSAStartup(MAKEWORD(2,2), &ws.wsadata)))
56 {
57 case WSASYSNOTREADY: debug("WSAStartup failed with WSASYSNOTREADY"); break;
58 case WSAVERNOTSUPPORTED: debug("WSAStartup failed with WSAVERNOTSUPPORTED"); break;
59 case WSAEINPROGRESS: debug("WSAStartup failed with WSAEINPROGRESS"); break;
60 case WSAEPROCLIM: debug("WSAStartup failed with WSAEPROCLIM"); break;
61 case WSAEFAULT: debug("WSAStartup failed with WSAEFAULT"); break;
62 default:
63 break;
64 }
65 }
66
67 void win32_net_deinit (void)
68 {
69 debug("Begin winsock cleanup");
70 if (ws.inited)
71 {
72 if (ws.inited >= 2 && ws.local_socket != SOCKET_ERROR)
73 {
74 debug1("ws.local_socket = %" PRIuMAX, (uintmax_t)ws.local_socket);
75 msgme_sock_err(shutdown(ws.local_socket, SD_BOTH));
76 win32_net_close(ws.local_socket);
77 }
78 WSACleanup();
79 ws.inited = 0;
80 }
81 }
82
83 void win32_net_close (int sock)
84 {
85 msgme_sock_err(closesocket(ws.local_socket));
86 }
87
88 static void win32_net_nonblock(int sock)
89 {
90 u_long mode = 1;
91 msgme_sock_err(ioctlsocket(ws.local_socket, FIONBIO, &mode));
92 }
93
94 static void win32_net_block(int sock)
95 {
96 u_long mode = 0;
97 msgme_sock_err(ioctlsocket(ws.local_socket, FIONBIO, &mode));
98 }
99
100 mpg123_ssize_t win32_net_read (int fildes, void *buf, size_t nbyte)
101 {
102 debug1("Attempting to read %zu bytes from network.", nbyte);
103 mpg123_ssize_t ret;
104 msgme_sock_err(ret = (mpg123_ssize_t) recv(ws.local_socket, buf, nbyte, 0));
105 debug1("Read %" PRIiMAX " bytes from network.", (intmax_t)ret);
106
107 return ret;
108 }
109
110 /*
111 static int get_sock_ch (int sock)
112 {
113 char c;
114 int ret;
115 msgme_sock_err(ret = recv (ws.local_socket, &c, 1, 0));
116 if (ret == 1)
117 return (((int) c)&0xff);
118 return -1;
119 }
120 */
121
122 mpg123_ssize_t win32_net_write (int fildes, const void *buf, size_t nbyte)
123 {
124 debug1("Attempting to write %zu bytes to network.", nbyte);
125 mpg123_ssize_t ret;
126 msgme_sock_err((ret = (mpg123_ssize_t) send(ws.local_socket, buf, nbyte, 0)));
127 debug1("wrote %" PRIiMAX " bytes to network.", (intmax_t)ret);
128
129 return ret;
130 }
131
132 static int win32_net_timeout_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
133 {
134 debug("win32_net_timeout_connect ran");
135 if(param.timeout > 0)
136 {
137 int err;
138 win32_net_nonblock(ws.local_socket);
139 err = connect(ws.local_socket, serv_addr, addrlen);
140 if(err != SOCKET_ERROR)
141 {
142 debug("immediately successful");
143 win32_net_block(ws.local_socket);
144 return 0;
145 }
146 else if(WSAGetLastError() == WSAEWOULDBLOCK) /*WSAEINPROGRESS would not work here for some reason*/
147 {
148 struct timeval tv;
149 fd_set fds;
150 tv.tv_sec = param.timeout;
151 tv.tv_usec = 0;
152
153 debug("in progress, waiting...");
154
155 FD_ZERO(&fds);
156 FD_SET(ws.local_socket, &fds);
157 err = select(ws.local_socket+1, NULL, &fds, NULL, &tv);
158 if(err != SOCKET_ERROR)
159 {
160 socklen_t len = sizeof(err);
161 if((getsockopt(ws.local_socket, SOL_SOCKET, SO_ERROR, (char *)&err, &len) != SOCKET_ERROR)
162 && (err == 0) )
163 {
164 debug("non-blocking connect has been successful");
165 win32_net_block(ws.local_socket);
166 return 0;
167 }
168 else
169 {
170 //error1("connection error: %s", msgme(err));
171 return -1;
172 }
173 }
174 else if(err == 0)
175 {
176 error("connection timed out");
177 return -1;
178 }
179 else
180 {
181 /*error1("error from select(): %s", INT123_strerror(errno));*/
182 debug("error from select():");
183 msgme1;
184 return -1;
185 }
186 }
187 else
188 {
189 /*error1("connection failed: %s", INT123_strerror(errno));*/
190 debug("connection failed: ");
191 msgme1;
192 return err;
193 }
194 }
195 else
196 {
197 if(connect(ws.local_socket, serv_addr, addrlen) == SOCKET_ERROR)
198 {
199 /*error1("connection failed: %s", INT123_strerror(errno));*/
200 debug("connection failed");
201 msgme1;
202 return -1;
203 }
204 else {
205 debug("win32_net_timeout_connect succeed");
206 return 0; /* _good_ */
207 }
208 }
209 }
210
211 int win32_net_open_connection(mpg123_string *host, mpg123_string *port)
212 {
213 struct addrinfo hints;
214 struct addrinfo *addr, *addrlist;
215 SOCKET addrcount;
216 ws.local_socket = SOCKET_ERROR;
217
218 if(param.verbose>1) fprintf(stderr, "Note: Attempting new-style connection to %s\n", host->p);
219 memset(&hints, 0, sizeof(struct addrinfo));
220 hints.ai_family = AF_UNSPEC; /* We accept both IPv4 and IPv6 ... and perhaps IPv8;-) */
221 hints.ai_socktype = SOCK_STREAM;
222 hints.ai_protocol = IPPROTO_TCP;
223
224 debug2("Atempt resolve/connect to %s:%s", host->p, port->p);
225 msgme(addrcount = getaddrinfo(host->p, port->p, &hints, &addrlist));
226
227 if(addrcount == INVALID_SOCKET)
228 {
229 error3("Resolving %s:%s: %s", host->p, port->p, gai_strerror(addrcount));
230 return -1;
231 }
232
233 addr = addrlist;
234 while(addr != NULL)
235 {
236 ws.local_socket = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
237 if (ws.local_socket == INVALID_SOCKET)
238 {
239 msgme1;
240 }
241 else
242 {
243 if(win32_net_timeout_connect(ws.local_socket, addr->ai_addr, addr->ai_addrlen) == 0)
244 break;
245 debug("win32_net_timeout_connect error, closing socket");
246 win32_net_close(ws.local_socket);
247 ws.local_socket=SOCKET_ERROR;
248 }
249 addr=addr->ai_next;
250 }
251 if(ws.local_socket == SOCKET_ERROR) {error2("Cannot resolve/connect to %s:%s!", host->p, port->p);}
252 else
253 {
254 ws.inited = 2;
255 }
256
257 freeaddrinfo(addrlist);
258 return ws.local_socket == SOCKET_ERROR ? -1 : 1;
259 }
260
261 int win32_net_writestring (int fd, mpg123_string *string)
262 {
263 size_t result, bytes;
264 char *ptr = string->p;
265 bytes = string->fill ? string->fill-1 : 0;
266
267 while(bytes)
268 {
269 result = win32_net_write(ws.local_socket, ptr, bytes);
270 if(result < 0 && WSAGetLastError() != WSAEINTR)
271 {
272 perror ("writing http string");
273 return FALSE;
274 }
275 else if(result == 0)
276 {
277 error("write: socket closed unexpectedly");
278 return FALSE;
279 }
280 ptr += result;
281 bytes -= result;
282 }
283 return TRUE;
284 }