]> git.proxmox.com Git - qemu.git/blame - qemu-sockets.c
sockets: Drop sockets_debug debug code
[qemu.git] / qemu-sockets.c
CommitLineData
305b0eb2
AL
1/*
2 * inet and unix socket functions for qemu
3 *
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
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; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
d247d25f
AL
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <ctype.h>
19#include <errno.h>
20#include <unistd.h>
21
22#include "qemu_socket.h"
47398b9c 23#include "qemu-common.h" /* for qemu_isdigit */
d247d25f
AL
24
25#ifndef AI_ADDRCONFIG
26# define AI_ADDRCONFIG 0
27#endif
28
d247d25f
AL
29static const int on=1, off=0;
30
2af2bf67 31/* used temporarely until all users are converted to QemuOpts */
c1390903 32static QemuOptsList dummy_opts = {
2af2bf67 33 .name = "dummy",
72cf2d4f 34 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
2af2bf67
GH
35 .desc = {
36 {
37 .name = "path",
38 .type = QEMU_OPT_STRING,
f4c94c7c
GH
39 },{
40 .name = "host",
41 .type = QEMU_OPT_STRING,
42 },{
43 .name = "port",
44 .type = QEMU_OPT_STRING,
45 },{
46 .name = "to",
47 .type = QEMU_OPT_NUMBER,
48 },{
49 .name = "ipv4",
50 .type = QEMU_OPT_BOOL,
51 },{
52 .name = "ipv6",
53 .type = QEMU_OPT_BOOL,
2af2bf67
GH
54 },
55 { /* end if list */ }
56 },
57};
58
d247d25f
AL
59static int inet_getport(struct addrinfo *e)
60{
61 struct sockaddr_in *i4;
62 struct sockaddr_in6 *i6;
63
64 switch (e->ai_family) {
65 case PF_INET6:
66 i6 = (void*)e->ai_addr;
67 return ntohs(i6->sin6_port);
68 case PF_INET:
69 i4 = (void*)e->ai_addr;
70 return ntohs(i4->sin_port);
71 default:
72 return 0;
73 }
74}
75
76static void inet_setport(struct addrinfo *e, int port)
77{
78 struct sockaddr_in *i4;
79 struct sockaddr_in6 *i6;
80
81 switch (e->ai_family) {
82 case PF_INET6:
83 i6 = (void*)e->ai_addr;
84 i6->sin6_port = htons(port);
85 break;
86 case PF_INET:
87 i4 = (void*)e->ai_addr;
88 i4->sin_port = htons(port);
89 break;
90 }
91}
92
c9c4b34e 93const char *inet_strfamily(int family)
d247d25f
AL
94{
95 switch (family) {
96 case PF_INET6: return "ipv6";
97 case PF_INET: return "ipv4";
98 case PF_UNIX: return "unix";
99 }
c445321e 100 return "unknown";
d247d25f
AL
101}
102
e5bc776f 103int inet_listen_opts(QemuOpts *opts, int port_offset)
d247d25f
AL
104{
105 struct addrinfo ai,*res,*e;
e5bc776f 106 const char *addr;
d247d25f
AL
107 char port[33];
108 char uaddr[INET6_ADDRSTRLEN+1];
109 char uport[33];
e5bc776f 110 int slisten,rc,to,try_next;
d247d25f
AL
111
112 memset(&ai,0, sizeof(ai));
113 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
114 ai.ai_family = PF_UNSPEC;
e5bc776f 115 ai.ai_socktype = SOCK_STREAM;
d247d25f 116
e23a22e6
JO
117 if ((qemu_opt_get(opts, "host") == NULL) ||
118 (qemu_opt_get(opts, "port") == NULL)) {
e5bc776f
GH
119 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
120 return -1;
d247d25f 121 }
e5bc776f
GH
122 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
123 addr = qemu_opt_get(opts, "host");
d247d25f 124
e5bc776f
GH
125 to = qemu_opt_get_number(opts, "to", 0);
126 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 127 ai.ai_family = PF_INET;
e5bc776f 128 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
129 ai.ai_family = PF_INET6;
130
131 /* lookup */
132 if (port_offset)
133 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
134 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
135 if (rc != 0) {
e5bc776f
GH
136 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
137 gai_strerror(rc));
d247d25f
AL
138 return -1;
139 }
d247d25f
AL
140
141 /* create socket + bind */
142 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 143 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
144 uaddr,INET6_ADDRSTRLEN,uport,32,
145 NI_NUMERICHOST | NI_NUMERICSERV);
40ff6d7e 146 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 147 if (slisten < 0) {
d247d25f
AL
148 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
149 inet_strfamily(e->ai_family), strerror(errno));
39b6efc8 150 continue;
151 }
d247d25f
AL
152
153 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
154#ifdef IPV6_V6ONLY
155 if (e->ai_family == PF_INET6) {
156 /* listen on both ipv4 and ipv6 */
39b6efc8 157 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
158 sizeof(off));
d247d25f
AL
159 }
160#endif
161
162 for (;;) {
163 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
d247d25f
AL
164 goto listen;
165 }
166 try_next = to && (inet_getport(e) <= to + port_offset);
136faa36 167 if (!try_next)
d247d25f
AL
168 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
169 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
170 strerror(errno));
171 if (try_next) {
172 inet_setport(e, inet_getport(e) + 1);
173 continue;
174 }
175 break;
176 }
177 closesocket(slisten);
178 }
179 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
180 freeaddrinfo(res);
181 return -1;
182
183listen:
184 if (listen(slisten,1) != 0) {
185 perror("listen");
186 closesocket(slisten);
39b6efc8 187 freeaddrinfo(res);
d247d25f
AL
188 return -1;
189 }
e5bc776f
GH
190 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
191 qemu_opt_set(opts, "host", uaddr);
192 qemu_opt_set(opts, "port", uport);
193 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
194 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
d247d25f
AL
195 freeaddrinfo(res);
196 return slisten;
197}
198
f4c94c7c 199int inet_connect_opts(QemuOpts *opts)
d247d25f
AL
200{
201 struct addrinfo ai,*res,*e;
f4c94c7c
GH
202 const char *addr;
203 const char *port;
d247d25f
AL
204 char uaddr[INET6_ADDRSTRLEN+1];
205 char uport[33];
206 int sock,rc;
207
208 memset(&ai,0, sizeof(ai));
209 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
210 ai.ai_family = PF_UNSPEC;
f4c94c7c 211 ai.ai_socktype = SOCK_STREAM;
d247d25f 212
f4c94c7c
GH
213 addr = qemu_opt_get(opts, "host");
214 port = qemu_opt_get(opts, "port");
215 if (addr == NULL || port == NULL) {
216 fprintf(stderr, "inet_connect: host and/or port not specified\n");
217 return -1;
d247d25f
AL
218 }
219
f4c94c7c 220 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 221 ai.ai_family = PF_INET;
f4c94c7c 222 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
223 ai.ai_family = PF_INET6;
224
225 /* lookup */
226 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
f4c94c7c
GH
227 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
228 gai_strerror(rc));
d247d25f
AL
229 return -1;
230 }
d247d25f
AL
231
232 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 233 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
234 uaddr,INET6_ADDRSTRLEN,uport,32,
235 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
d247d25f 236 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
39b6efc8 237 continue;
238 }
40ff6d7e 239 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 240 if (sock < 0) {
d247d25f 241 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
39b6efc8 242 inet_strfamily(e->ai_family), strerror(errno));
243 continue;
244 }
d247d25f
AL
245 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
246
39b6efc8 247 /* connect to peer */
248 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
136faa36 249 if (NULL == e->ai_next)
d247d25f
AL
250 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
251 inet_strfamily(e->ai_family),
252 e->ai_canonname, uaddr, uport, strerror(errno));
253 closesocket(sock);
39b6efc8 254 continue;
255 }
d247d25f 256 freeaddrinfo(res);
39b6efc8 257 return sock;
d247d25f
AL
258 }
259 freeaddrinfo(res);
260 return -1;
261}
262
7e1b35b4
GH
263int inet_dgram_opts(QemuOpts *opts)
264{
265 struct addrinfo ai, *peer = NULL, *local = NULL;
266 const char *addr;
267 const char *port;
268 char uaddr[INET6_ADDRSTRLEN+1];
269 char uport[33];
270 int sock = -1, rc;
271
272 /* lookup peer addr */
273 memset(&ai,0, sizeof(ai));
274 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
275 ai.ai_family = PF_UNSPEC;
276 ai.ai_socktype = SOCK_DGRAM;
277
278 addr = qemu_opt_get(opts, "host");
279 port = qemu_opt_get(opts, "port");
280 if (addr == NULL || strlen(addr) == 0) {
281 addr = "localhost";
282 }
283 if (port == NULL || strlen(port) == 0) {
284 fprintf(stderr, "inet_dgram: port not specified\n");
285 return -1;
286 }
287
288 if (qemu_opt_get_bool(opts, "ipv4", 0))
289 ai.ai_family = PF_INET;
290 if (qemu_opt_get_bool(opts, "ipv6", 0))
291 ai.ai_family = PF_INET6;
292
293 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
294 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
295 gai_strerror(rc));
296 return -1;
297 }
7e1b35b4
GH
298
299 /* lookup local addr */
300 memset(&ai,0, sizeof(ai));
301 ai.ai_flags = AI_PASSIVE;
302 ai.ai_family = peer->ai_family;
303 ai.ai_socktype = SOCK_DGRAM;
304
305 addr = qemu_opt_get(opts, "localaddr");
306 port = qemu_opt_get(opts, "localport");
307 if (addr == NULL || strlen(addr) == 0) {
308 addr = NULL;
309 }
310 if (!port || strlen(port) == 0)
311 port = "0";
312
313 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
314 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
315 gai_strerror(rc));
316 return -1;
317 }
7e1b35b4
GH
318
319 /* create socket */
40ff6d7e 320 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
7e1b35b4
GH
321 if (sock < 0) {
322 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
323 inet_strfamily(peer->ai_family), strerror(errno));
324 goto err;
325 }
326 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
327
328 /* bind socket */
329 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
330 uaddr,INET6_ADDRSTRLEN,uport,32,
331 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
332 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
333 goto err;
334 }
335 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
336 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
337 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
338 goto err;
339 }
340
341 /* connect to peer */
342 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
343 uaddr, INET6_ADDRSTRLEN, uport, 32,
344 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
345 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
346 goto err;
347 }
348 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
349 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
350 inet_strfamily(peer->ai_family),
351 peer->ai_canonname, uaddr, uport, strerror(errno));
352 goto err;
353 }
354
355 freeaddrinfo(local);
356 freeaddrinfo(peer);
357 return sock;
358
359err:
360 if (-1 != sock)
361 closesocket(sock);
362 if (local)
363 freeaddrinfo(local);
364 if (peer)
365 freeaddrinfo(peer);
366 return -1;
367}
368
f4c94c7c
GH
369/* compatibility wrapper */
370static int inet_parse(QemuOpts *opts, const char *str)
371{
372 const char *optstr, *h;
373 char addr[64];
374 char port[33];
375 int pos;
376
377 /* parse address */
378 if (str[0] == ':') {
379 /* no host given */
380 addr[0] = '\0';
381 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
382 fprintf(stderr, "%s: portonly parse error (%s)\n",
383 __FUNCTION__, str);
384 return -1;
385 }
386 } else if (str[0] == '[') {
387 /* IPv6 addr */
388 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
389 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
390 __FUNCTION__, str);
391 return -1;
392 }
2198a62e 393 qemu_opt_set(opts, "ipv6", "on");
f4c94c7c
GH
394 } else if (qemu_isdigit(str[0])) {
395 /* IPv4 addr */
396 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
397 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
398 __FUNCTION__, str);
399 return -1;
400 }
2198a62e 401 qemu_opt_set(opts, "ipv4", "on");
f4c94c7c
GH
402 } else {
403 /* hostname */
404 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
405 fprintf(stderr, "%s: hostname parse error (%s)\n",
406 __FUNCTION__, str);
407 return -1;
408 }
409 }
410 qemu_opt_set(opts, "host", addr);
411 qemu_opt_set(opts, "port", port);
412
413 /* parse options */
414 optstr = str + pos;
415 h = strstr(optstr, ",to=");
416 if (h)
417 qemu_opt_set(opts, "to", h+4);
418 if (strstr(optstr, ",ipv4"))
2198a62e 419 qemu_opt_set(opts, "ipv4", "on");
f4c94c7c 420 if (strstr(optstr, ",ipv6"))
2198a62e 421 qemu_opt_set(opts, "ipv6", "on");
f4c94c7c
GH
422 return 0;
423}
424
e5bc776f
GH
425int inet_listen(const char *str, char *ostr, int olen,
426 int socktype, int port_offset)
427{
428 QemuOpts *opts;
429 char *optstr;
430 int sock = -1;
431
432 opts = qemu_opts_create(&dummy_opts, NULL, 0);
433 if (inet_parse(opts, str) == 0) {
434 sock = inet_listen_opts(opts, port_offset);
435 if (sock != -1 && ostr) {
436 optstr = strchr(str, ',');
437 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
438 snprintf(ostr, olen, "[%s]:%s%s",
439 qemu_opt_get(opts, "host"),
440 qemu_opt_get(opts, "port"),
441 optstr ? optstr : "");
442 } else {
443 snprintf(ostr, olen, "%s:%s%s",
444 qemu_opt_get(opts, "host"),
445 qemu_opt_get(opts, "port"),
446 optstr ? optstr : "");
447 }
448 }
449 }
450 qemu_opts_del(opts);
451 return sock;
452}
453
f4c94c7c
GH
454int inet_connect(const char *str, int socktype)
455{
456 QemuOpts *opts;
457 int sock = -1;
458
459 opts = qemu_opts_create(&dummy_opts, NULL, 0);
460 if (inet_parse(opts, str) == 0)
461 sock = inet_connect_opts(opts);
462 qemu_opts_del(opts);
463 return sock;
464}
465
d247d25f
AL
466#ifndef _WIN32
467
62b6adfb 468int unix_listen_opts(QemuOpts *opts)
d247d25f
AL
469{
470 struct sockaddr_un un;
62b6adfb
GH
471 const char *path = qemu_opt_get(opts, "path");
472 int sock, fd;
d247d25f 473
40ff6d7e 474 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
d247d25f 475 if (sock < 0) {
39b6efc8 476 perror("socket(unix)");
477 return -1;
d247d25f
AL
478 }
479
d247d25f
AL
480 memset(&un, 0, sizeof(un));
481 un.sun_family = AF_UNIX;
482 if (path && strlen(path)) {
483 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
484 } else {
485 char *tmpdir = getenv("TMPDIR");
486 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
487 tmpdir ? tmpdir : "/tmp");
488 /*
489 * This dummy fd usage silences the mktemp() unsecure warning.
490 * Using mkstemp() doesn't make things more secure here
491 * though. bind() complains about existing files, so we have
492 * to unlink first and thus re-open the race window. The
493 * worst case possible is bind() failing, i.e. a DoS attack.
494 */
495 fd = mkstemp(un.sun_path); close(fd);
62b6adfb 496 qemu_opt_set(opts, "path", un.sun_path);
d247d25f 497 }
d247d25f
AL
498
499 unlink(un.sun_path);
500 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
501 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
502 goto err;
503 }
504 if (listen(sock, 1) < 0) {
505 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
506 goto err;
507 }
508
d247d25f
AL
509 return sock;
510
511err:
d247d25f
AL
512 closesocket(sock);
513 return -1;
514}
515
2af2bf67 516int unix_connect_opts(QemuOpts *opts)
d247d25f
AL
517{
518 struct sockaddr_un un;
2af2bf67 519 const char *path = qemu_opt_get(opts, "path");
d247d25f
AL
520 int sock;
521
2af2bf67
GH
522 if (NULL == path) {
523 fprintf(stderr, "unix connect: no path specified\n");
524 return -1;
525 }
526
40ff6d7e 527 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
d247d25f 528 if (sock < 0) {
39b6efc8 529 perror("socket(unix)");
530 return -1;
d247d25f
AL
531 }
532
533 memset(&un, 0, sizeof(un));
534 un.sun_family = AF_UNIX;
535 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
536 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
537 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
9d947472 538 close(sock);
d247d25f
AL
539 return -1;
540 }
541
d247d25f
AL
542 return sock;
543}
544
2af2bf67 545/* compatibility wrapper */
62b6adfb
GH
546int unix_listen(const char *str, char *ostr, int olen)
547{
548 QemuOpts *opts;
549 char *path, *optstr;
550 int sock, len;
551
552 opts = qemu_opts_create(&dummy_opts, NULL, 0);
553
554 optstr = strchr(str, ',');
555 if (optstr) {
556 len = optstr - str;
557 if (len) {
7267c094 558 path = g_malloc(len+1);
62b6adfb
GH
559 snprintf(path, len+1, "%.*s", len, str);
560 qemu_opt_set(opts, "path", path);
7267c094 561 g_free(path);
62b6adfb
GH
562 }
563 } else {
564 qemu_opt_set(opts, "path", str);
565 }
566
567 sock = unix_listen_opts(opts);
568
569 if (sock != -1 && ostr)
570 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
571 qemu_opts_del(opts);
572 return sock;
573}
574
2af2bf67
GH
575int unix_connect(const char *path)
576{
577 QemuOpts *opts;
578 int sock;
579
580 opts = qemu_opts_create(&dummy_opts, NULL, 0);
581 qemu_opt_set(opts, "path", path);
582 sock = unix_connect_opts(opts);
583 qemu_opts_del(opts);
584 return sock;
585}
586
d247d25f
AL
587#else
588
108af7b9
GH
589int unix_listen_opts(QemuOpts *opts)
590{
591 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 592 errno = ENOTSUP;
108af7b9
GH
593 return -1;
594}
595
596int unix_connect_opts(QemuOpts *opts)
597{
598 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 599 errno = ENOTSUP;
108af7b9
GH
600 return -1;
601}
602
d247d25f
AL
603int unix_listen(const char *path, char *ostr, int olen)
604{
605 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 606 errno = ENOTSUP;
d247d25f
AL
607 return -1;
608}
609
610int unix_connect(const char *path)
611{
612 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 613 errno = ENOTSUP;
d247d25f
AL
614 return -1;
615}
616
617#endif
0706a4dc
PB
618
619#ifdef _WIN32
620static void socket_cleanup(void)
621{
622 WSACleanup();
623}
624#endif
625
626int socket_init(void)
627{
628#ifdef _WIN32
629 WSADATA Data;
630 int ret, err;
631
632 ret = WSAStartup(MAKEWORD(2,2), &Data);
633 if (ret != 0) {
634 err = WSAGetLastError();
635 fprintf(stderr, "WSAStartup: %d\n", err);
636 return -1;
637 }
638 atexit(socket_cleanup);
639#endif
640 return 0;
641}