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/main-loop.h"
30 # define AI_ADDRCONFIG 0
33 /* used temporarily until all users are converted to QemuOpts */
34 QemuOptsList socket_optslist
= {
36 .head
= QTAILQ_HEAD_INITIALIZER(socket_optslist
.head
),
40 .type
= QEMU_OPT_STRING
,
43 .type
= QEMU_OPT_STRING
,
46 .type
= QEMU_OPT_STRING
,
49 .type
= QEMU_OPT_NUMBER
,
52 .type
= QEMU_OPT_BOOL
,
55 .type
= QEMU_OPT_BOOL
,
61 static int inet_getport(struct addrinfo
*e
)
63 struct sockaddr_in
*i4
;
64 struct sockaddr_in6
*i6
;
66 switch (e
->ai_family
) {
68 i6
= (void*)e
->ai_addr
;
69 return ntohs(i6
->sin6_port
);
71 i4
= (void*)e
->ai_addr
;
72 return ntohs(i4
->sin_port
);
78 static void inet_setport(struct addrinfo
*e
, int port
)
80 struct sockaddr_in
*i4
;
81 struct sockaddr_in6
*i6
;
83 switch (e
->ai_family
) {
85 i6
= (void*)e
->ai_addr
;
86 i6
->sin6_port
= htons(port
);
89 i4
= (void*)e
->ai_addr
;
90 i4
->sin_port
= htons(port
);
95 NetworkAddressFamily
inet_netfamily(int family
)
98 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
99 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
100 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
102 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
105 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
107 struct addrinfo ai
,*res
,*e
;
110 char uaddr
[INET6_ADDRSTRLEN
+1];
112 int slisten
, rc
, to
, port_min
, port_max
, p
;
114 memset(&ai
,0, sizeof(ai
));
115 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
116 ai
.ai_family
= PF_UNSPEC
;
117 ai
.ai_socktype
= SOCK_STREAM
;
119 if ((qemu_opt_get(opts
, "host") == NULL
) ||
120 (qemu_opt_get(opts
, "port") == NULL
)) {
121 error_setg(errp
, "host and/or port not specified");
124 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
125 addr
= qemu_opt_get(opts
, "host");
127 to
= qemu_opt_get_number(opts
, "to", 0);
128 if (qemu_opt_get_bool(opts
, "ipv4", 0))
129 ai
.ai_family
= PF_INET
;
130 if (qemu_opt_get_bool(opts
, "ipv6", 0))
131 ai
.ai_family
= PF_INET6
;
135 unsigned long long baseport
;
136 if (parse_uint_full(port
, &baseport
, 10) < 0) {
137 error_setg(errp
, "can't convert to a number: %s", port
);
140 if (baseport
> 65535 ||
141 baseport
+ port_offset
> 65535) {
142 error_setg(errp
, "port %s out of range", port
);
145 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
147 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
149 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
154 /* create socket + bind */
155 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
156 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
157 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
158 NI_NUMERICHOST
| NI_NUMERICSERV
);
159 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
162 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
167 socket_set_fast_reuse(slisten
);
169 if (e
->ai_family
== PF_INET6
) {
170 /* listen on both ipv4 and ipv6 */
172 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
177 port_min
= inet_getport(e
);
178 port_max
= to
? to
+ port_offset
: port_min
;
179 for (p
= port_min
; p
<= port_max
; p
++) {
181 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
186 error_set_errno(errp
, errno
, QERR_SOCKET_BIND_FAILED
);
190 closesocket(slisten
);
196 if (listen(slisten
,1) != 0) {
197 error_set_errno(errp
, errno
, QERR_SOCKET_LISTEN_FAILED
);
198 closesocket(slisten
);
202 snprintf(uport
, sizeof(uport
), "%d", inet_getport(e
) - port_offset
);
203 qemu_opt_set(opts
, "host", uaddr
);
204 qemu_opt_set(opts
, "port", uport
);
205 qemu_opt_set(opts
, "ipv6", (e
->ai_family
== PF_INET6
) ? "on" : "off");
206 qemu_opt_set(opts
, "ipv4", (e
->ai_family
!= PF_INET6
) ? "on" : "off");
212 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
213 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
215 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
216 ((rc) == -EINPROGRESS)
219 /* Struct to store connect state for non blocking connect */
220 typedef struct ConnectState
{
222 struct addrinfo
*addr_list
;
223 struct addrinfo
*current_addr
;
224 NonBlockingConnectHandler
*callback
;
228 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
229 ConnectState
*connect_state
, Error
**errp
);
231 static void wait_for_connect(void *opaque
)
233 ConnectState
*s
= opaque
;
235 socklen_t valsize
= sizeof(val
);
238 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
241 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
242 } while (rc
== -1 && socket_error() == EINTR
);
244 /* update rc to contain error */
255 /* try to connect to the next address on the list */
256 if (s
->current_addr
) {
257 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
258 s
->current_addr
= s
->current_addr
->ai_next
;
259 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
260 /* connect in progress */
266 freeaddrinfo(s
->addr_list
);
270 s
->callback(s
->fd
, s
->opaque
);
275 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
276 ConnectState
*connect_state
, Error
**errp
)
280 *in_progress
= false;
282 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
284 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
287 socket_set_fast_reuse(sock
);
288 if (connect_state
!= NULL
) {
289 qemu_set_nonblock(sock
);
291 /* connect to peer */
294 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
295 rc
= -socket_error();
297 } while (rc
== -EINTR
);
299 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
300 connect_state
->fd
= sock
;
301 qemu_set_fd_handler2(sock
, NULL
, NULL
, wait_for_connect
,
305 error_set_errno(errp
, errno
, QERR_SOCKET_CONNECT_FAILED
);
312 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
314 struct addrinfo ai
, *res
;
319 memset(&ai
, 0, sizeof(ai
));
321 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
322 ai
.ai_family
= PF_UNSPEC
;
323 ai
.ai_socktype
= SOCK_STREAM
;
325 addr
= qemu_opt_get(opts
, "host");
326 port
= qemu_opt_get(opts
, "port");
327 if (addr
== NULL
|| port
== NULL
) {
328 error_setg(errp
, "host and/or port not specified");
332 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
333 ai
.ai_family
= PF_INET
;
335 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
336 ai
.ai_family
= PF_INET6
;
340 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
342 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
350 * Create a socket and connect it to an address.
352 * @opts: QEMU options, recognized parameters strings "host" and "port",
353 * bools "ipv4" and "ipv6".
354 * @errp: set on error
355 * @callback: callback function for non-blocking connect
356 * @opaque: opaque for callback function
358 * Returns: -1 on error, file descriptor on success.
360 * If @callback is non-null, the connect is non-blocking. If this
361 * function succeeds, callback will be called when the connection
362 * completes, with the file descriptor on success, or -1 on error.
364 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
,
365 NonBlockingConnectHandler
*callback
, void *opaque
)
367 Error
*local_err
= NULL
;
368 struct addrinfo
*res
, *e
;
371 ConnectState
*connect_state
= NULL
;
373 res
= inet_parse_connect_opts(opts
, errp
);
378 if (callback
!= NULL
) {
379 connect_state
= g_malloc0(sizeof(*connect_state
));
380 connect_state
->addr_list
= res
;
381 connect_state
->callback
= callback
;
382 connect_state
->opaque
= opaque
;
385 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
386 error_free(local_err
);
388 if (connect_state
!= NULL
) {
389 connect_state
->current_addr
= e
;
391 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
398 error_propagate(errp
, local_err
);
399 } else if (in_progress
) {
400 /* wait_for_connect() will do the rest */
404 callback(sock
, opaque
);
407 g_free(connect_state
);
412 int inet_dgram_opts(QemuOpts
*opts
, Error
**errp
)
414 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
419 /* lookup peer addr */
420 memset(&ai
,0, sizeof(ai
));
421 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
422 ai
.ai_family
= PF_UNSPEC
;
423 ai
.ai_socktype
= SOCK_DGRAM
;
425 addr
= qemu_opt_get(opts
, "host");
426 port
= qemu_opt_get(opts
, "port");
427 if (addr
== NULL
|| strlen(addr
) == 0) {
430 if (port
== NULL
|| strlen(port
) == 0) {
431 error_setg(errp
, "remote port not specified");
435 if (qemu_opt_get_bool(opts
, "ipv4", 0))
436 ai
.ai_family
= PF_INET
;
437 if (qemu_opt_get_bool(opts
, "ipv6", 0))
438 ai
.ai_family
= PF_INET6
;
440 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
441 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
446 /* lookup local addr */
447 memset(&ai
,0, sizeof(ai
));
448 ai
.ai_flags
= AI_PASSIVE
;
449 ai
.ai_family
= peer
->ai_family
;
450 ai
.ai_socktype
= SOCK_DGRAM
;
452 addr
= qemu_opt_get(opts
, "localaddr");
453 port
= qemu_opt_get(opts
, "localport");
454 if (addr
== NULL
|| strlen(addr
) == 0) {
457 if (!port
|| strlen(port
) == 0)
460 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
461 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
467 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
469 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
472 socket_set_fast_reuse(sock
);
475 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
476 error_set_errno(errp
, errno
, QERR_SOCKET_BIND_FAILED
);
480 /* connect to peer */
481 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
482 error_set_errno(errp
, errno
, QERR_SOCKET_CONNECT_FAILED
);
500 /* compatibility wrapper */
501 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
503 InetSocketAddress
*addr
;
504 const char *optstr
, *h
;
510 addr
= g_new0(InetSocketAddress
, 1);
516 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
517 error_setg(errp
, "error parsing port in address '%s'", str
);
520 } else if (str
[0] == '[') {
522 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
523 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
526 addr
->ipv6
= addr
->has_ipv6
= true;
528 /* hostname or IPv4 addr */
529 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
530 error_setg(errp
, "error parsing address '%s'", str
);
533 if (host
[strspn(host
, "0123456789.")] == '\0') {
534 addr
->ipv4
= addr
->has_ipv4
= true;
538 addr
->host
= g_strdup(host
);
539 addr
->port
= g_strdup(port
);
543 h
= strstr(optstr
, ",to=");
546 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
547 (h
[pos
] != '\0' && h
[pos
] != ',')) {
548 error_setg(errp
, "error parsing to= argument");
554 if (strstr(optstr
, ",ipv4")) {
555 addr
->ipv4
= addr
->has_ipv4
= true;
557 if (strstr(optstr
, ",ipv6")) {
558 addr
->ipv6
= addr
->has_ipv6
= true;
563 qapi_free_InetSocketAddress(addr
);
567 static void inet_addr_to_opts(QemuOpts
*opts
, const InetSocketAddress
*addr
)
569 bool ipv4
= addr
->ipv4
|| !addr
->has_ipv4
;
570 bool ipv6
= addr
->ipv6
|| !addr
->has_ipv6
;
572 if (!ipv4
|| !ipv6
) {
573 qemu_opt_set_bool(opts
, "ipv4", ipv4
);
574 qemu_opt_set_bool(opts
, "ipv6", ipv6
);
578 snprintf(to
, sizeof(to
), "%d", addr
->to
);
579 qemu_opt_set(opts
, "to", to
);
581 qemu_opt_set(opts
, "host", addr
->host
);
582 qemu_opt_set(opts
, "port", addr
->port
);
585 int inet_listen(const char *str
, char *ostr
, int olen
,
586 int socktype
, int port_offset
, Error
**errp
)
591 InetSocketAddress
*addr
;
593 addr
= inet_parse(str
, errp
);
595 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
596 inet_addr_to_opts(opts
, addr
);
597 qapi_free_InetSocketAddress(addr
);
598 sock
= inet_listen_opts(opts
, port_offset
, errp
);
599 if (sock
!= -1 && ostr
) {
600 optstr
= strchr(str
, ',');
601 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
602 snprintf(ostr
, olen
, "[%s]:%s%s",
603 qemu_opt_get(opts
, "host"),
604 qemu_opt_get(opts
, "port"),
605 optstr
? optstr
: "");
607 snprintf(ostr
, olen
, "%s:%s%s",
608 qemu_opt_get(opts
, "host"),
609 qemu_opt_get(opts
, "port"),
610 optstr
? optstr
: "");
619 * Create a blocking socket and connect it to an address.
621 * @str: address string
622 * @errp: set in case of an error
624 * Returns -1 in case of error, file descriptor on success
626 int inet_connect(const char *str
, Error
**errp
)
630 InetSocketAddress
*addr
;
632 addr
= inet_parse(str
, errp
);
634 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
635 inet_addr_to_opts(opts
, addr
);
636 qapi_free_InetSocketAddress(addr
);
637 sock
= inet_connect_opts(opts
, errp
, NULL
, NULL
);
644 * Create a non-blocking socket and connect it to an address.
645 * Calls the callback function with fd in case of success or -1 in case of
648 * @str: address string
649 * @callback: callback function that is called when connect completes,
651 * @opaque: opaque for callback function
652 * @errp: set in case of an error
654 * Returns: -1 on immediate error, file descriptor on success.
656 int inet_nonblocking_connect(const char *str
,
657 NonBlockingConnectHandler
*callback
,
658 void *opaque
, Error
**errp
)
662 InetSocketAddress
*addr
;
664 g_assert(callback
!= NULL
);
666 addr
= inet_parse(str
, errp
);
668 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
669 inet_addr_to_opts(opts
, addr
);
670 qapi_free_InetSocketAddress(addr
);
671 sock
= inet_connect_opts(opts
, errp
, callback
, opaque
);
679 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
681 struct sockaddr_un un
;
682 const char *path
= qemu_opt_get(opts
, "path");
685 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
687 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
691 memset(&un
, 0, sizeof(un
));
692 un
.sun_family
= AF_UNIX
;
693 if (path
&& strlen(path
)) {
694 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
696 char *tmpdir
= getenv("TMPDIR");
697 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
698 tmpdir
? tmpdir
: "/tmp");
700 * This dummy fd usage silences the mktemp() unsecure warning.
701 * Using mkstemp() doesn't make things more secure here
702 * though. bind() complains about existing files, so we have
703 * to unlink first and thus re-open the race window. The
704 * worst case possible is bind() failing, i.e. a DoS attack.
706 fd
= mkstemp(un
.sun_path
); close(fd
);
707 qemu_opt_set(opts
, "path", un
.sun_path
);
711 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
712 error_set_errno(errp
, errno
, QERR_SOCKET_BIND_FAILED
);
715 if (listen(sock
, 1) < 0) {
716 error_set_errno(errp
, errno
, QERR_SOCKET_LISTEN_FAILED
);
727 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
728 NonBlockingConnectHandler
*callback
, void *opaque
)
730 struct sockaddr_un un
;
731 const char *path
= qemu_opt_get(opts
, "path");
732 ConnectState
*connect_state
= NULL
;
736 error_setg(errp
, "unix connect: no path specified");
740 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
742 error_set_errno(errp
, errno
, QERR_SOCKET_CREATE_FAILED
);
745 if (callback
!= NULL
) {
746 connect_state
= g_malloc0(sizeof(*connect_state
));
747 connect_state
->callback
= callback
;
748 connect_state
->opaque
= opaque
;
749 qemu_set_nonblock(sock
);
752 memset(&un
, 0, sizeof(un
));
753 un
.sun_family
= AF_UNIX
;
754 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
756 /* connect to peer */
759 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
760 rc
= -socket_error();
762 } while (rc
== -EINTR
);
764 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
765 connect_state
->fd
= sock
;
766 qemu_set_fd_handler2(sock
, NULL
, NULL
, wait_for_connect
,
769 } else if (rc
>= 0) {
770 /* non blocking socket immediate success, call callback */
771 if (callback
!= NULL
) {
772 callback(sock
, opaque
);
777 error_set_errno(errp
, -rc
, QERR_SOCKET_CONNECT_FAILED
);
782 g_free(connect_state
);
788 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
790 error_setg(errp
, "unix sockets are not available on windows");
795 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
796 NonBlockingConnectHandler
*callback
, void *opaque
)
798 error_setg(errp
, "unix sockets are not available on windows");
804 /* compatibility wrapper */
805 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
811 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
813 optstr
= strchr(str
, ',');
817 path
= g_malloc(len
+1);
818 snprintf(path
, len
+1, "%.*s", len
, str
);
819 qemu_opt_set(opts
, "path", path
);
823 qemu_opt_set(opts
, "path", str
);
826 sock
= unix_listen_opts(opts
, errp
);
828 if (sock
!= -1 && ostr
)
829 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
834 int unix_connect(const char *path
, Error
**errp
)
839 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
840 qemu_opt_set(opts
, "path", path
);
841 sock
= unix_connect_opts(opts
, errp
, NULL
, NULL
);
847 int unix_nonblocking_connect(const char *path
,
848 NonBlockingConnectHandler
*callback
,
849 void *opaque
, Error
**errp
)
854 g_assert(callback
!= NULL
);
856 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
857 qemu_opt_set(opts
, "path", path
);
858 sock
= unix_connect_opts(opts
, errp
, callback
, opaque
);
863 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
867 addr
= g_new0(SocketAddress
, 1);
868 if (strstart(str
, "unix:", NULL
)) {
869 if (str
[5] == '\0') {
870 error_setg(errp
, "invalid Unix socket address");
873 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
874 addr
->q_unix
= g_new(UnixSocketAddress
, 1);
875 addr
->q_unix
->path
= g_strdup(str
+ 5);
877 } else if (strstart(str
, "fd:", NULL
)) {
878 if (str
[3] == '\0') {
879 error_setg(errp
, "invalid file descriptor address");
882 addr
->kind
= SOCKET_ADDRESS_KIND_FD
;
883 addr
->fd
= g_new(String
, 1);
884 addr
->fd
->str
= g_strdup(str
+ 3);
887 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
888 addr
->inet
= inet_parse(str
, errp
);
889 if (addr
->inet
== NULL
) {
896 qapi_free_SocketAddress(addr
);
900 int socket_connect(SocketAddress
*addr
, Error
**errp
,
901 NonBlockingConnectHandler
*callback
, void *opaque
)
906 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
907 switch (addr
->kind
) {
908 case SOCKET_ADDRESS_KIND_INET
:
909 inet_addr_to_opts(opts
, addr
->inet
);
910 fd
= inet_connect_opts(opts
, errp
, callback
, opaque
);
913 case SOCKET_ADDRESS_KIND_UNIX
:
914 qemu_opt_set(opts
, "path", addr
->q_unix
->path
);
915 fd
= unix_connect_opts(opts
, errp
, callback
, opaque
);
918 case SOCKET_ADDRESS_KIND_FD
:
919 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
920 if (fd
>= 0 && callback
) {
921 qemu_set_nonblock(fd
);
922 callback(fd
, opaque
);
933 int socket_listen(SocketAddress
*addr
, Error
**errp
)
938 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
939 switch (addr
->kind
) {
940 case SOCKET_ADDRESS_KIND_INET
:
941 inet_addr_to_opts(opts
, addr
->inet
);
942 fd
= inet_listen_opts(opts
, 0, errp
);
945 case SOCKET_ADDRESS_KIND_UNIX
:
946 qemu_opt_set(opts
, "path", addr
->q_unix
->path
);
947 fd
= unix_listen_opts(opts
, errp
);
950 case SOCKET_ADDRESS_KIND_FD
:
951 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
961 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
966 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
967 switch (remote
->kind
) {
968 case SOCKET_ADDRESS_KIND_INET
:
969 qemu_opt_set(opts
, "host", remote
->inet
->host
);
970 qemu_opt_set(opts
, "port", remote
->inet
->port
);
972 qemu_opt_set(opts
, "localaddr", local
->inet
->host
);
973 qemu_opt_set(opts
, "localport", local
->inet
->port
);
975 fd
= inet_dgram_opts(opts
, errp
);
979 error_setg(errp
, "socket type unsupported for datagram");