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