2 * inet and unix socket functions for qemu
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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.
25 #include "monitor/monitor.h"
26 #include "qemu/sockets.h"
27 #include "qemu-common.h" /* for qemu_isdigit */
28 #include "qemu/main-loop.h"
31 # define AI_ADDRCONFIG 0
34 static const int on
=1, off
=0;
36 /* used temporarily until all users are converted to QemuOpts */
37 QemuOptsList socket_optslist
= {
39 .head
= QTAILQ_HEAD_INITIALIZER(socket_optslist
.head
),
43 .type
= QEMU_OPT_STRING
,
46 .type
= QEMU_OPT_STRING
,
49 .type
= QEMU_OPT_STRING
,
52 .type
= QEMU_OPT_NUMBER
,
55 .type
= QEMU_OPT_BOOL
,
58 .type
= QEMU_OPT_BOOL
,
64 static int inet_getport(struct addrinfo
*e
)
66 struct sockaddr_in
*i4
;
67 struct sockaddr_in6
*i6
;
69 switch (e
->ai_family
) {
71 i6
= (void*)e
->ai_addr
;
72 return ntohs(i6
->sin6_port
);
74 i4
= (void*)e
->ai_addr
;
75 return ntohs(i4
->sin_port
);
81 static void inet_setport(struct addrinfo
*e
, int port
)
83 struct sockaddr_in
*i4
;
84 struct sockaddr_in6
*i6
;
86 switch (e
->ai_family
) {
88 i6
= (void*)e
->ai_addr
;
89 i6
->sin6_port
= htons(port
);
92 i4
= (void*)e
->ai_addr
;
93 i4
->sin_port
= htons(port
);
98 const char *inet_strfamily(int family
)
101 case PF_INET6
: return "ipv6";
102 case PF_INET
: return "ipv4";
103 case PF_UNIX
: return "unix";
108 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
110 struct addrinfo ai
,*res
,*e
;
113 char uaddr
[INET6_ADDRSTRLEN
+1];
115 int slisten
, rc
, to
, port_min
, port_max
, p
;
117 memset(&ai
,0, sizeof(ai
));
118 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
119 ai
.ai_family
= PF_UNSPEC
;
120 ai
.ai_socktype
= SOCK_STREAM
;
122 if ((qemu_opt_get(opts
, "host") == NULL
) ||
123 (qemu_opt_get(opts
, "port") == NULL
)) {
124 error_setg(errp
, "host and/or port not specified");
127 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
128 addr
= qemu_opt_get(opts
, "host");
130 to
= qemu_opt_get_number(opts
, "to", 0);
131 if (qemu_opt_get_bool(opts
, "ipv4", 0))
132 ai
.ai_family
= PF_INET
;
133 if (qemu_opt_get_bool(opts
, "ipv6", 0))
134 ai
.ai_family
= PF_INET6
;
138 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
139 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
141 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
146 /* create socket + bind */
147 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
148 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
149 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
150 NI_NUMERICHOST
| NI_NUMERICSERV
);
151 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
154 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
159 qemu_setsockopt(slisten
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
161 if (e
->ai_family
== PF_INET6
) {
162 /* listen on both ipv4 and ipv6 */
163 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
168 port_min
= inet_getport(e
);
169 port_max
= to
? to
+ port_offset
: port_min
;
170 for (p
= port_min
; p
<= port_max
; p
++) {
172 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
177 error_set_errno(errp
, errno
, QERR_SOCKET_BIND_FAILED
);
181 closesocket(slisten
);
187 if (listen(slisten
,1) != 0) {
188 error_set_errno(errp
, errno
, QERR_SOCKET_LISTEN_FAILED
);
189 closesocket(slisten
);
193 snprintf(uport
, sizeof(uport
), "%d", inet_getport(e
) - port_offset
);
194 qemu_opt_set(opts
, "host", uaddr
);
195 qemu_opt_set(opts
, "port", uport
);
196 qemu_opt_set(opts
, "ipv6", (e
->ai_family
== PF_INET6
) ? "on" : "off");
197 qemu_opt_set(opts
, "ipv4", (e
->ai_family
!= PF_INET6
) ? "on" : "off");
203 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
204 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
206 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
207 ((rc) == -EINPROGRESS)
210 /* Struct to store connect state for non blocking connect */
211 typedef struct ConnectState
{
213 struct addrinfo
*addr_list
;
214 struct addrinfo
*current_addr
;
215 NonBlockingConnectHandler
*callback
;
219 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
220 ConnectState
*connect_state
, Error
**errp
);
222 static void wait_for_connect(void *opaque
)
224 ConnectState
*s
= opaque
;
226 socklen_t valsize
= sizeof(val
);
229 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
232 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
233 } while (rc
== -1 && socket_error() == EINTR
);
235 /* update rc to contain error */
246 /* try to connect to the next address on the list */
247 if (s
->current_addr
) {
248 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
249 s
->current_addr
= s
->current_addr
->ai_next
;
250 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
251 /* connect in progress */
257 freeaddrinfo(s
->addr_list
);
261 s
->callback(s
->fd
, s
->opaque
);
266 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
267 ConnectState
*connect_state
, Error
**errp
)
271 *in_progress
= false;
273 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
275 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
278 qemu_setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
279 if (connect_state
!= NULL
) {
280 qemu_set_nonblock(sock
);
282 /* connect to peer */
285 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
286 rc
= -socket_error();
288 } while (rc
== -EINTR
);
290 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
291 connect_state
->fd
= sock
;
292 qemu_set_fd_handler2(sock
, NULL
, NULL
, wait_for_connect
,
296 error_set_errno(errp
, errno
, QERR_SOCKET_CONNECT_FAILED
);
303 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
305 struct addrinfo ai
, *res
;
310 memset(&ai
, 0, sizeof(ai
));
312 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
313 ai
.ai_family
= PF_UNSPEC
;
314 ai
.ai_socktype
= SOCK_STREAM
;
316 addr
= qemu_opt_get(opts
, "host");
317 port
= qemu_opt_get(opts
, "port");
318 if (addr
== NULL
|| port
== NULL
) {
319 error_setg(errp
, "host and/or port not specified");
323 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
324 ai
.ai_family
= PF_INET
;
326 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
327 ai
.ai_family
= PF_INET6
;
331 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
333 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
341 * Create a socket and connect it to an address.
343 * @opts: QEMU options, recognized parameters strings "host" and "port",
344 * bools "ipv4" and "ipv6".
345 * @errp: set on error
346 * @callback: callback function for non-blocking connect
347 * @opaque: opaque for callback function
349 * Returns: -1 on error, file descriptor on success.
351 * If @callback is non-null, the connect is non-blocking. If this
352 * function succeeds, callback will be called when the connection
353 * completes, with the file descriptor on success, or -1 on error.
355 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
,
356 NonBlockingConnectHandler
*callback
, void *opaque
)
358 struct addrinfo
*res
, *e
;
361 ConnectState
*connect_state
= NULL
;
363 res
= inet_parse_connect_opts(opts
, errp
);
368 if (callback
!= NULL
) {
369 connect_state
= g_malloc0(sizeof(*connect_state
));
370 connect_state
->addr_list
= res
;
371 connect_state
->callback
= callback
;
372 connect_state
->opaque
= opaque
;
375 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
376 if (error_is_set(errp
)) {
380 if (connect_state
!= NULL
) {
381 connect_state
->current_addr
= e
;
383 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, errp
);
386 } else if (sock
>= 0) {
387 /* non blocking socket immediate success, call callback */
388 if (callback
!= NULL
) {
389 callback(sock
, opaque
);
394 g_free(connect_state
);
399 int inet_dgram_opts(QemuOpts
*opts
, Error
**errp
)
401 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
406 /* lookup peer addr */
407 memset(&ai
,0, sizeof(ai
));
408 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
409 ai
.ai_family
= PF_UNSPEC
;
410 ai
.ai_socktype
= SOCK_DGRAM
;
412 addr
= qemu_opt_get(opts
, "host");
413 port
= qemu_opt_get(opts
, "port");
414 if (addr
== NULL
|| strlen(addr
) == 0) {
417 if (port
== NULL
|| strlen(port
) == 0) {
418 error_setg(errp
, "remote port not specified");
422 if (qemu_opt_get_bool(opts
, "ipv4", 0))
423 ai
.ai_family
= PF_INET
;
424 if (qemu_opt_get_bool(opts
, "ipv6", 0))
425 ai
.ai_family
= PF_INET6
;
427 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
428 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
433 /* lookup local addr */
434 memset(&ai
,0, sizeof(ai
));
435 ai
.ai_flags
= AI_PASSIVE
;
436 ai
.ai_family
= peer
->ai_family
;
437 ai
.ai_socktype
= SOCK_DGRAM
;
439 addr
= qemu_opt_get(opts
, "localaddr");
440 port
= qemu_opt_get(opts
, "localport");
441 if (addr
== NULL
|| strlen(addr
) == 0) {
444 if (!port
|| strlen(port
) == 0)
447 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
448 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
454 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
456 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
459 qemu_setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
462 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
463 error_set_errno(errp
, errno
, QERR_SOCKET_BIND_FAILED
);
467 /* connect to peer */
468 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
469 error_set_errno(errp
, errno
, QERR_SOCKET_CONNECT_FAILED
);
487 /* compatibility wrapper */
488 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
490 InetSocketAddress
*addr
;
491 const char *optstr
, *h
;
497 addr
= g_new0(InetSocketAddress
, 1);
503 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
504 error_setg(errp
, "error parsing port in address '%s'", str
);
507 } else if (str
[0] == '[') {
509 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
510 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
513 addr
->ipv6
= addr
->has_ipv6
= true;
514 } else if (qemu_isdigit(str
[0])) {
516 if (2 != sscanf(str
, "%64[0-9.]:%32[^,]%n", host
, port
, &pos
)) {
517 error_setg(errp
, "error parsing IPv4 address '%s'", str
);
520 addr
->ipv4
= addr
->has_ipv4
= true;
523 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
524 error_setg(errp
, "error parsing address '%s'", str
);
529 addr
->host
= g_strdup(host
);
530 addr
->port
= g_strdup(port
);
534 h
= strstr(optstr
, ",to=");
537 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
538 (h
[pos
] != '\0' && h
[pos
] != ',')) {
539 error_setg(errp
, "error parsing to= argument");
545 if (strstr(optstr
, ",ipv4")) {
546 addr
->ipv4
= addr
->has_ipv4
= true;
548 if (strstr(optstr
, ",ipv6")) {
549 addr
->ipv6
= addr
->has_ipv6
= true;
554 qapi_free_InetSocketAddress(addr
);
558 static void inet_addr_to_opts(QemuOpts
*opts
, const InetSocketAddress
*addr
)
560 bool ipv4
= addr
->ipv4
|| !addr
->has_ipv4
;
561 bool ipv6
= addr
->ipv6
|| !addr
->has_ipv6
;
563 if (!ipv4
|| !ipv6
) {
564 qemu_opt_set_bool(opts
, "ipv4", ipv4
);
565 qemu_opt_set_bool(opts
, "ipv6", ipv6
);
569 snprintf(to
, sizeof(to
), "%d", addr
->to
);
570 qemu_opt_set(opts
, "to", to
);
572 qemu_opt_set(opts
, "host", addr
->host
);
573 qemu_opt_set(opts
, "port", addr
->port
);
576 int inet_listen(const char *str
, char *ostr
, int olen
,
577 int socktype
, int port_offset
, Error
**errp
)
582 InetSocketAddress
*addr
;
584 addr
= inet_parse(str
, errp
);
586 opts
= qemu_opts_create_nofail(&socket_optslist
);
587 inet_addr_to_opts(opts
, addr
);
588 qapi_free_InetSocketAddress(addr
);
589 sock
= inet_listen_opts(opts
, port_offset
, errp
);
590 if (sock
!= -1 && ostr
) {
591 optstr
= strchr(str
, ',');
592 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
593 snprintf(ostr
, olen
, "[%s]:%s%s",
594 qemu_opt_get(opts
, "host"),
595 qemu_opt_get(opts
, "port"),
596 optstr
? optstr
: "");
598 snprintf(ostr
, olen
, "%s:%s%s",
599 qemu_opt_get(opts
, "host"),
600 qemu_opt_get(opts
, "port"),
601 optstr
? optstr
: "");
610 * Create a blocking socket and connect it to an address.
612 * @str: address string
613 * @errp: set in case of an error
615 * Returns -1 in case of error, file descriptor on success
617 int inet_connect(const char *str
, Error
**errp
)
621 InetSocketAddress
*addr
;
623 addr
= inet_parse(str
, errp
);
625 opts
= qemu_opts_create_nofail(&socket_optslist
);
626 inet_addr_to_opts(opts
, addr
);
627 qapi_free_InetSocketAddress(addr
);
628 sock
= inet_connect_opts(opts
, errp
, NULL
, NULL
);
635 * Create a non-blocking socket and connect it to an address.
636 * Calls the callback function with fd in case of success or -1 in case of
639 * @str: address string
640 * @callback: callback function that is called when connect completes,
642 * @opaque: opaque for callback function
643 * @errp: set in case of an error
645 * Returns: -1 on immediate error, file descriptor on success.
647 int inet_nonblocking_connect(const char *str
,
648 NonBlockingConnectHandler
*callback
,
649 void *opaque
, Error
**errp
)
653 InetSocketAddress
*addr
;
655 g_assert(callback
!= NULL
);
657 addr
= inet_parse(str
, errp
);
659 opts
= qemu_opts_create_nofail(&socket_optslist
);
660 inet_addr_to_opts(opts
, addr
);
661 qapi_free_InetSocketAddress(addr
);
662 sock
= inet_connect_opts(opts
, errp
, callback
, opaque
);
670 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
672 struct sockaddr_un un
;
673 const char *path
= qemu_opt_get(opts
, "path");
676 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
678 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
682 memset(&un
, 0, sizeof(un
));
683 un
.sun_family
= AF_UNIX
;
684 if (path
&& strlen(path
)) {
685 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
687 char *tmpdir
= getenv("TMPDIR");
688 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
689 tmpdir
? tmpdir
: "/tmp");
691 * This dummy fd usage silences the mktemp() unsecure warning.
692 * Using mkstemp() doesn't make things more secure here
693 * though. bind() complains about existing files, so we have
694 * to unlink first and thus re-open the race window. The
695 * worst case possible is bind() failing, i.e. a DoS attack.
697 fd
= mkstemp(un
.sun_path
); close(fd
);
698 qemu_opt_set(opts
, "path", un
.sun_path
);
702 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
703 error_set_errno(errp
, errno
, QERR_SOCKET_BIND_FAILED
);
706 if (listen(sock
, 1) < 0) {
707 error_set_errno(errp
, errno
, QERR_SOCKET_LISTEN_FAILED
);
718 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
719 NonBlockingConnectHandler
*callback
, void *opaque
)
721 struct sockaddr_un un
;
722 const char *path
= qemu_opt_get(opts
, "path");
723 ConnectState
*connect_state
= NULL
;
727 error_setg(errp
, "unix connect: no path specified");
731 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
733 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
736 if (callback
!= NULL
) {
737 connect_state
= g_malloc0(sizeof(*connect_state
));
738 connect_state
->callback
= callback
;
739 connect_state
->opaque
= opaque
;
740 qemu_set_nonblock(sock
);
743 memset(&un
, 0, sizeof(un
));
744 un
.sun_family
= AF_UNIX
;
745 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
747 /* connect to peer */
750 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
751 rc
= -socket_error();
753 } while (rc
== -EINTR
);
755 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
756 connect_state
->fd
= sock
;
757 qemu_set_fd_handler2(sock
, NULL
, NULL
, wait_for_connect
,
760 } else if (rc
>= 0) {
761 /* non blocking socket immediate success, call callback */
762 if (callback
!= NULL
) {
763 callback(sock
, opaque
);
768 error_set_errno(errp
, -rc
, QERR_SOCKET_CONNECT_FAILED
);
773 g_free(connect_state
);
779 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
781 error_setg(errp
, "unix sockets are not available on windows");
786 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
787 NonBlockingConnectHandler
*callback
, void *opaque
)
789 error_setg(errp
, "unix sockets are not available on windows");
795 /* compatibility wrapper */
796 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
802 opts
= qemu_opts_create_nofail(&socket_optslist
);
804 optstr
= strchr(str
, ',');
808 path
= g_malloc(len
+1);
809 snprintf(path
, len
+1, "%.*s", len
, str
);
810 qemu_opt_set(opts
, "path", path
);
814 qemu_opt_set(opts
, "path", str
);
817 sock
= unix_listen_opts(opts
, errp
);
819 if (sock
!= -1 && ostr
)
820 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
825 int unix_connect(const char *path
, Error
**errp
)
830 opts
= qemu_opts_create_nofail(&socket_optslist
);
831 qemu_opt_set(opts
, "path", path
);
832 sock
= unix_connect_opts(opts
, errp
, NULL
, NULL
);
838 int unix_nonblocking_connect(const char *path
,
839 NonBlockingConnectHandler
*callback
,
840 void *opaque
, Error
**errp
)
845 g_assert(callback
!= NULL
);
847 opts
= qemu_opts_create_nofail(&socket_optslist
);
848 qemu_opt_set(opts
, "path", path
);
849 sock
= unix_connect_opts(opts
, errp
, callback
, opaque
);
854 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
856 SocketAddress
*addr
= NULL
;
858 addr
= g_new(SocketAddress
, 1);
859 if (strstart(str
, "unix:", NULL
)) {
860 if (str
[5] == '\0') {
861 error_setg(errp
, "invalid Unix socket address");
864 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
865 addr
->q_unix
= g_new(UnixSocketAddress
, 1);
866 addr
->q_unix
->path
= g_strdup(str
+ 5);
868 } else if (strstart(str
, "fd:", NULL
)) {
869 if (str
[3] == '\0') {
870 error_setg(errp
, "invalid file descriptor address");
873 addr
->kind
= SOCKET_ADDRESS_KIND_FD
;
874 addr
->fd
= g_new(String
, 1);
875 addr
->fd
->str
= g_strdup(str
+ 3);
878 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
879 addr
->inet
= g_new(InetSocketAddress
, 1);
880 addr
->inet
= inet_parse(str
, errp
);
881 if (addr
->inet
== NULL
) {
888 qapi_free_SocketAddress(addr
);
892 int socket_connect(SocketAddress
*addr
, Error
**errp
,
893 NonBlockingConnectHandler
*callback
, void *opaque
)
898 opts
= qemu_opts_create_nofail(&socket_optslist
);
899 switch (addr
->kind
) {
900 case SOCKET_ADDRESS_KIND_INET
:
901 inet_addr_to_opts(opts
, addr
->inet
);
902 fd
= inet_connect_opts(opts
, errp
, callback
, opaque
);
905 case SOCKET_ADDRESS_KIND_UNIX
:
906 qemu_opt_set(opts
, "path", addr
->q_unix
->path
);
907 fd
= unix_connect_opts(opts
, errp
, callback
, opaque
);
910 case SOCKET_ADDRESS_KIND_FD
:
911 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
913 qemu_set_nonblock(fd
);
914 callback(fd
, opaque
);
925 int socket_listen(SocketAddress
*addr
, Error
**errp
)
930 opts
= qemu_opts_create_nofail(&socket_optslist
);
931 switch (addr
->kind
) {
932 case SOCKET_ADDRESS_KIND_INET
:
933 inet_addr_to_opts(opts
, addr
->inet
);
934 fd
= inet_listen_opts(opts
, 0, errp
);
937 case SOCKET_ADDRESS_KIND_UNIX
:
938 qemu_opt_set(opts
, "path", addr
->q_unix
->path
);
939 fd
= unix_listen_opts(opts
, errp
);
942 case SOCKET_ADDRESS_KIND_FD
:
943 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
953 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
958 opts
= qemu_opts_create_nofail(&socket_optslist
);
959 switch (remote
->kind
) {
960 case SOCKET_ADDRESS_KIND_INET
:
961 qemu_opt_set(opts
, "host", remote
->inet
->host
);
962 qemu_opt_set(opts
, "port", remote
->inet
->port
);
964 qemu_opt_set(opts
, "localaddr", local
->inet
->host
);
965 qemu_opt_set(opts
, "localport", local
->inet
->port
);
967 fd
= inet_dgram_opts(opts
, errp
);
971 error_setg(errp
, "socket type unsupported for datagram");
979 static void socket_cleanup(void)
985 int socket_init(void)
991 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
993 err
= WSAGetLastError();
994 fprintf(stderr
, "WSAStartup: %d\n", err
);
997 atexit(socket_cleanup
);