4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "chardev/char.h"
27 #include "io/channel-socket.h"
28 #include "io/channel-websock.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qapi/error.h"
33 #include "qapi/clone-visitor.h"
34 #include "qapi/qapi-visit-sockets.h"
35 #include "qemu/yank.h"
38 #include "chardev/char-io.h"
39 #include "chardev/char-socket.h"
41 static gboolean
socket_reconnect_timeout(gpointer opaque
);
42 static void tcp_chr_telnet_init(Chardev
*chr
);
44 static void tcp_chr_change_state(SocketChardev
*s
, TCPChardevState state
)
47 case TCP_CHARDEV_STATE_DISCONNECTED
:
49 case TCP_CHARDEV_STATE_CONNECTING
:
50 assert(s
->state
== TCP_CHARDEV_STATE_DISCONNECTED
);
52 case TCP_CHARDEV_STATE_CONNECTED
:
53 assert(s
->state
== TCP_CHARDEV_STATE_CONNECTING
);
59 static void tcp_chr_reconn_timer_cancel(SocketChardev
*s
)
61 if (s
->reconnect_timer
) {
62 g_source_destroy(s
->reconnect_timer
);
63 g_source_unref(s
->reconnect_timer
);
64 s
->reconnect_timer
= NULL
;
68 static void qemu_chr_socket_restart_timer(Chardev
*chr
)
70 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
73 assert(s
->state
== TCP_CHARDEV_STATE_DISCONNECTED
);
74 assert(!s
->reconnect_timer
);
75 name
= g_strdup_printf("chardev-socket-reconnect-%s", chr
->label
);
76 s
->reconnect_timer
= qemu_chr_timeout_add_ms(chr
,
78 socket_reconnect_timeout
,
80 g_source_set_name(s
->reconnect_timer
, name
);
84 static void check_report_connect_error(Chardev
*chr
,
87 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
89 if (!s
->connect_err_reported
) {
90 error_reportf_err(err
,
91 "Unable to connect character device %s: ",
93 s
->connect_err_reported
= true;
97 qemu_chr_socket_restart_timer(chr
);
100 static void tcp_chr_accept(QIONetListener
*listener
,
101 QIOChannelSocket
*cioc
,
104 static int tcp_chr_read_poll(void *opaque
);
105 static void tcp_chr_disconnect_locked(Chardev
*chr
);
107 /* Called with chr_write_lock held. */
108 static int tcp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
110 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
112 if (s
->state
== TCP_CHARDEV_STATE_CONNECTED
) {
113 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
115 s
->write_msgfds_num
);
117 /* free the written msgfds in any cases
118 * other than ret < 0 && errno == EAGAIN
120 if (!(ret
< 0 && EAGAIN
== errno
)
121 && s
->write_msgfds_num
) {
122 g_free(s
->write_msgfds
);
124 s
->write_msgfds_num
= 0;
127 if (ret
< 0 && errno
!= EAGAIN
) {
128 if (tcp_chr_read_poll(chr
) <= 0) {
129 /* Perform disconnect and return error. */
130 trace_chr_socket_poll_err(chr
, chr
->label
);
131 tcp_chr_disconnect_locked(chr
);
132 } /* else let the read handler finish it properly */
137 /* Indicate an error. */
143 static int tcp_chr_read_poll(void *opaque
)
145 Chardev
*chr
= CHARDEV(opaque
);
146 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
147 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
150 s
->max_size
= qemu_chr_be_can_write(chr
);
154 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
156 uint8_t *buf
, int *size
)
158 /* Handle any telnet or tn3270 client's basic IAC options.
159 * For telnet options, it satisfies char by char mode with no echo.
160 * For tn3270 options, it satisfies binary mode with EOR.
161 * All IAC options will be removed from the buf and the do_opt
162 * pointer will be used to track the state of the width of the
165 * RFC854: "All TELNET commands consist of at least a two byte sequence.
166 * The commands dealing with option negotiation are three byte sequences,
167 * the third byte being the code for the option referenced."
168 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
169 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
171 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
172 * session, and NOP and IP need to be done later.
178 for (i
= 0; i
< *size
; i
++) {
179 if (s
->do_telnetopt
> 1) {
180 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
181 /* Double IAC means send an IAC */
188 if ((unsigned char)buf
[i
] == IAC_BREAK
189 && s
->do_telnetopt
== 2) {
190 /* Handle IAC break commands by sending a serial break */
191 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
193 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_EOR
194 || (unsigned char)buf
[i
] == IAC_SB
195 || (unsigned char)buf
[i
] == IAC_SE
)
196 && s
->do_telnetopt
== 2) {
200 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_IP
201 || (unsigned char)buf
[i
] == IAC_NOP
)
202 && s
->do_telnetopt
== 2) {
203 /* TODO: IP and NOP need to be implemented later. */
208 if (s
->do_telnetopt
>= 4) {
212 if ((unsigned char)buf
[i
] == IAC
) {
225 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
227 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
229 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
231 assert(num
<= TCP_MAX_FDS
);
236 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
238 /* Close unused fds */
239 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
240 close(s
->read_msgfds
[i
]);
243 g_free(s
->read_msgfds
);
245 s
->read_msgfds_num
= 0;
251 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
253 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
255 /* clear old pending fd array */
256 g_free(s
->write_msgfds
);
257 s
->write_msgfds
= NULL
;
258 s
->write_msgfds_num
= 0;
260 if ((s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) ||
261 !qio_channel_has_feature(s
->ioc
,
262 QIO_CHANNEL_FEATURE_FD_PASS
)) {
267 s
->write_msgfds
= g_new(int, num
);
268 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
271 s
->write_msgfds_num
= num
;
276 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
278 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
279 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
283 size_t msgfds_num
= 0;
286 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
287 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
288 &msgfds
, &msgfds_num
,
291 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
297 /* close and clean read_msgfds */
298 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
299 close(s
->read_msgfds
[i
]);
302 if (s
->read_msgfds_num
) {
303 g_free(s
->read_msgfds
);
306 s
->read_msgfds
= msgfds
;
307 s
->read_msgfds_num
= msgfds_num
;
310 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
311 int fd
= s
->read_msgfds
[i
];
316 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
317 qemu_socket_set_block(fd
);
319 #ifndef MSG_CMSG_CLOEXEC
320 qemu_set_cloexec(fd
);
324 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
327 } else if (ret
== -1) {
328 trace_chr_socket_recv_err(chr
, chr
->label
, error_get_pretty(err
));
331 } else if (ret
== 0) {
332 trace_chr_socket_recv_eof(chr
, chr
->label
);
338 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
340 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
344 return qio_channel_create_watch(s
->ioc
, cond
);
347 static void remove_hup_source(SocketChardev
*s
)
349 if (s
->hup_source
!= NULL
) {
350 g_source_destroy(s
->hup_source
);
351 g_source_unref(s
->hup_source
);
352 s
->hup_source
= NULL
;
356 static void char_socket_yank_iochannel(void *opaque
)
358 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
360 qio_channel_shutdown(ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
363 static void tcp_chr_free_connection(Chardev
*chr
)
365 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
368 if (s
->read_msgfds_num
) {
369 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
370 close(s
->read_msgfds
[i
]);
372 g_free(s
->read_msgfds
);
373 s
->read_msgfds
= NULL
;
374 s
->read_msgfds_num
= 0;
377 remove_hup_source(s
);
379 tcp_set_msgfds(chr
, NULL
, 0);
380 remove_fd_in_watch(chr
);
381 if (s
->registered_yank
&&
382 (s
->state
== TCP_CHARDEV_STATE_CONNECTING
383 || s
->state
== TCP_CHARDEV_STATE_CONNECTED
)) {
384 yank_unregister_function(CHARDEV_YANK_INSTANCE(chr
->label
),
385 char_socket_yank_iochannel
,
386 QIO_CHANNEL(s
->sioc
));
390 qio_channel_close(s
->ioc
, NULL
);
392 object_unref(OBJECT(s
->sioc
));
394 object_unref(OBJECT(s
->ioc
));
396 g_free(chr
->filename
);
397 chr
->filename
= NULL
;
398 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_DISCONNECTED
);
401 static const char *qemu_chr_socket_protocol(SocketChardev
*s
)
406 return s
->is_websock
? "websocket" : "tcp";
409 static char *qemu_chr_socket_address(SocketChardev
*s
, const char *prefix
)
411 switch (s
->addr
->type
) {
412 case SOCKET_ADDRESS_TYPE_INET
:
413 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
414 qemu_chr_socket_protocol(s
),
415 s
->addr
->u
.inet
.host
,
416 s
->addr
->u
.inet
.port
,
417 s
->is_listen
? ",server=on" : "");
419 case SOCKET_ADDRESS_TYPE_UNIX
:
421 const char *tight
= "", *abstract
= "";
422 UnixSocketAddress
*sa
= &s
->addr
->u
.q_unix
;
425 if (sa
->has_abstract
&& sa
->abstract
) {
426 abstract
= ",abstract=on";
427 if (sa
->has_tight
&& sa
->tight
) {
433 return g_strdup_printf("%sunix:%s%s%s%s", prefix
, sa
->path
,
435 s
->is_listen
? ",server=on" : "");
438 case SOCKET_ADDRESS_TYPE_FD
:
439 return g_strdup_printf("%sfd:%s%s", prefix
, s
->addr
->u
.fd
.str
,
440 s
->is_listen
? ",server=on" : "");
442 case SOCKET_ADDRESS_TYPE_VSOCK
:
443 return g_strdup_printf("%svsock:%s:%s", prefix
,
444 s
->addr
->u
.vsock
.cid
,
445 s
->addr
->u
.vsock
.port
);
451 static void update_disconnected_filename(SocketChardev
*s
)
453 Chardev
*chr
= CHARDEV(s
);
455 g_free(chr
->filename
);
457 chr
->filename
= qemu_chr_socket_address(s
, "disconnected:");
459 chr
->filename
= g_strdup("disconnected:socket");
463 /* NB may be called even if tcp_chr_connect has not been
464 * reached, due to TLS or telnet initialization failure,
465 * so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
466 * This must be called with chr->chr_write_lock held.
468 static void tcp_chr_disconnect_locked(Chardev
*chr
)
470 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
471 bool emit_close
= s
->state
== TCP_CHARDEV_STATE_CONNECTED
;
473 trace_chr_socket_disconnect(chr
, chr
->label
);
474 tcp_chr_free_connection(chr
);
477 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
478 chr
, NULL
, chr
->gcontext
);
480 update_disconnected_filename(s
);
482 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
484 if (s
->reconnect_time_ms
&& !s
->reconnect_timer
) {
485 qemu_chr_socket_restart_timer(chr
);
489 static void tcp_chr_disconnect(Chardev
*chr
)
491 qemu_mutex_lock(&chr
->chr_write_lock
);
492 tcp_chr_disconnect_locked(chr
);
493 qemu_mutex_unlock(&chr
->chr_write_lock
);
496 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
498 Chardev
*chr
= CHARDEV(opaque
);
499 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
500 uint8_t buf
[CHR_READ_BUF_LEN
];
503 if ((s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) ||
508 if (len
> s
->max_size
) {
511 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
512 if (size
== 0 || (size
== -1 && errno
!= EAGAIN
)) {
513 /* connection closed */
514 tcp_chr_disconnect(chr
);
515 } else if (size
> 0) {
516 if (s
->do_telnetopt
) {
517 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
520 qemu_chr_be_write(chr
, buf
, size
);
527 static gboolean
tcp_chr_hup(QIOChannel
*channel
,
531 Chardev
*chr
= CHARDEV(opaque
);
532 trace_chr_socket_hangup(chr
, chr
->label
);
533 tcp_chr_disconnect(chr
);
534 return G_SOURCE_REMOVE
;
537 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
539 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
543 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
547 qio_channel_set_blocking(s
->ioc
, true, NULL
);
548 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
550 if (s
->state
!= TCP_CHARDEV_STATE_DISCONNECTED
) {
551 qio_channel_set_blocking(s
->ioc
, false, NULL
);
554 /* connection closed */
555 tcp_chr_disconnect(chr
);
562 static char *qemu_chr_compute_filename(SocketChardev
*s
)
564 struct sockaddr_storage
*ss
= &s
->sioc
->localAddr
;
565 struct sockaddr_storage
*ps
= &s
->sioc
->remoteAddr
;
566 socklen_t ss_len
= s
->sioc
->localAddrLen
;
567 socklen_t ps_len
= s
->sioc
->remoteAddrLen
;
568 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
569 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
570 const char *left
= "", *right
= "";
572 switch (ss
->ss_family
) {
574 return g_strdup_printf("unix:%s%s",
575 ((struct sockaddr_un
*)(ss
))->sun_path
,
576 s
->is_listen
? ",server=on" : "");
582 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
583 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
584 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
585 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
586 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
587 qemu_chr_socket_protocol(s
),
588 left
, shost
, right
, sserv
,
589 s
->is_listen
? ",server=on" : "",
590 left
, phost
, right
, pserv
);
593 return g_strdup_printf("unknown");
597 static void update_ioc_handlers(SocketChardev
*s
)
599 Chardev
*chr
= CHARDEV(s
);
601 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
605 remove_fd_in_watch(chr
);
606 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
611 remove_hup_source(s
);
612 s
->hup_source
= qio_channel_create_watch(s
->ioc
, G_IO_HUP
);
614 * poll() is liable to return POLLHUP even when there is
615 * still incoming data available to read on the FD. If
616 * we have the hup_source at the same priority as the
617 * main io_add_watch_poll GSource, then we might end up
618 * processing the POLLHUP event first, closing the FD,
619 * and as a result silently discard data we should have
622 * By setting the hup_source to G_PRIORITY_DEFAULT + 1,
623 * we ensure that io_add_watch_poll GSource will always
624 * be dispatched first, thus guaranteeing we will be
625 * able to process all incoming data before closing the
628 g_source_set_priority(s
->hup_source
, G_PRIORITY_DEFAULT
+ 1);
629 g_source_set_callback(s
->hup_source
, (GSourceFunc
)tcp_chr_hup
,
631 g_source_attach(s
->hup_source
, chr
->gcontext
);
634 static void tcp_chr_connect(void *opaque
)
636 Chardev
*chr
= CHARDEV(opaque
);
637 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
639 g_free(chr
->filename
);
640 chr
->filename
= qemu_chr_compute_filename(s
);
642 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTED
);
643 update_ioc_handlers(s
);
644 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
647 static void tcp_chr_telnet_destroy(SocketChardev
*s
)
649 if (s
->telnet_source
) {
650 g_source_destroy(s
->telnet_source
);
651 g_source_unref(s
->telnet_source
);
652 s
->telnet_source
= NULL
;
656 static void tcp_chr_update_read_handler(Chardev
*chr
)
658 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
660 if (s
->listener
&& s
->state
== TCP_CHARDEV_STATE_DISCONNECTED
) {
662 * It's possible that chardev context is changed in
663 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
664 * listener if there is.
666 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
667 chr
, NULL
, chr
->gcontext
);
670 if (s
->telnet_source
) {
671 tcp_chr_telnet_init(CHARDEV(s
));
674 update_ioc_handlers(s
);
677 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
678 GIOCondition cond G_GNUC_UNUSED
,
681 SocketChardev
*s
= user_data
;
682 Chardev
*chr
= CHARDEV(s
);
683 TCPChardevTelnetInit
*init
= s
->telnet_init
;
689 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, &err
);
691 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
694 trace_chr_socket_write_err(chr
, chr
->label
, error_get_pretty(err
));
696 tcp_chr_disconnect(chr
);
702 if (init
->buflen
== 0) {
703 tcp_chr_connect(chr
);
707 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
709 return G_SOURCE_CONTINUE
;
712 g_free(s
->telnet_init
);
713 s
->telnet_init
= NULL
;
714 g_source_unref(s
->telnet_source
);
715 s
->telnet_source
= NULL
;
716 return G_SOURCE_REMOVE
;
719 static void tcp_chr_telnet_init(Chardev
*chr
)
721 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
722 TCPChardevTelnetInit
*init
;
725 /* Destroy existing task */
726 tcp_chr_telnet_destroy(s
);
728 if (s
->telnet_init
) {
729 /* We are possibly during a handshake already */
733 s
->telnet_init
= g_new0(TCPChardevTelnetInit
, 1);
734 init
= s
->telnet_init
;
736 #define IACSET(x, a, b, c) \
745 /* Prep the telnet negotiation to put telnet in binary,
746 * no echo, single char mode */
747 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
748 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
749 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
750 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
753 /* Prep the TN3270 negotiation based on RFC1576 */
754 IACSET(init
->buf
, 0xff, 0xfd, 0x19); /* IAC DO EOR */
755 IACSET(init
->buf
, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
756 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
757 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
758 IACSET(init
->buf
, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
759 IACSET(init
->buf
, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
760 IACSET(init
->buf
, 0x01, 0xff, 0xf0); /* SEND IAC SE */
766 s
->telnet_source
= qio_channel_add_watch_source(s
->ioc
, G_IO_OUT
,
767 tcp_chr_telnet_init_io
,
773 static void tcp_chr_websock_handshake(QIOTask
*task
, gpointer user_data
)
775 Chardev
*chr
= user_data
;
776 SocketChardev
*s
= user_data
;
779 if (qio_task_propagate_error(task
, &err
)) {
780 trace_chr_socket_ws_handshake_err(chr
, chr
->label
,
781 error_get_pretty(err
));
783 tcp_chr_disconnect(chr
);
785 if (s
->do_telnetopt
) {
786 tcp_chr_telnet_init(chr
);
788 tcp_chr_connect(chr
);
794 static void tcp_chr_websock_init(Chardev
*chr
)
796 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
797 QIOChannelWebsock
*wioc
= NULL
;
800 wioc
= qio_channel_websock_new_server(s
->ioc
);
802 name
= g_strdup_printf("chardev-websocket-server-%s", chr
->label
);
803 qio_channel_set_name(QIO_CHANNEL(wioc
), name
);
805 object_unref(OBJECT(s
->ioc
));
806 s
->ioc
= QIO_CHANNEL(wioc
);
808 qio_channel_websock_handshake(wioc
, tcp_chr_websock_handshake
, chr
, NULL
);
812 static void tcp_chr_tls_handshake(QIOTask
*task
,
815 Chardev
*chr
= user_data
;
816 SocketChardev
*s
= user_data
;
819 if (qio_task_propagate_error(task
, &err
)) {
820 trace_chr_socket_tls_handshake_err(chr
, chr
->label
,
821 error_get_pretty(err
));
823 tcp_chr_disconnect(chr
);
826 tcp_chr_websock_init(chr
);
827 } else if (s
->do_telnetopt
) {
828 tcp_chr_telnet_init(chr
);
830 tcp_chr_connect(chr
);
836 static void tcp_chr_tls_init(Chardev
*chr
)
838 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
844 tioc
= qio_channel_tls_new_server(
845 s
->ioc
, s
->tls_creds
,
849 tioc
= qio_channel_tls_new_client(
850 s
->ioc
, s
->tls_creds
,
851 s
->addr
->u
.inet
.host
,
855 trace_chr_socket_tls_init_err(chr
, chr
->label
, error_get_pretty(err
));
857 tcp_chr_disconnect(chr
);
860 name
= g_strdup_printf("chardev-tls-%s-%s",
861 s
->is_listen
? "server" : "client",
863 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
865 object_unref(OBJECT(s
->ioc
));
866 s
->ioc
= QIO_CHANNEL(tioc
);
868 qio_channel_tls_handshake(tioc
,
869 tcp_chr_tls_handshake
,
876 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
877 QIOChannelSocket
*sioc
)
879 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
881 name
= g_strdup_printf("chardev-tcp-%s-%s",
882 s
->is_listen
? "server" : "client",
884 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
889 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
891 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
893 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTING
) {
897 s
->ioc
= QIO_CHANNEL(sioc
);
898 object_ref(OBJECT(sioc
));
900 object_ref(OBJECT(sioc
));
902 qio_channel_set_blocking(s
->ioc
, false, NULL
);
905 qio_channel_set_delay(s
->ioc
, false);
908 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
909 NULL
, chr
->gcontext
);
913 tcp_chr_tls_init(chr
);
914 } else if (s
->is_websock
) {
915 tcp_chr_websock_init(chr
);
916 } else if (s
->do_telnetopt
) {
917 tcp_chr_telnet_init(chr
);
919 tcp_chr_connect(chr
);
926 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
929 QIOChannelSocket
*sioc
;
930 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
932 if (s
->state
!= TCP_CHARDEV_STATE_DISCONNECTED
) {
936 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
940 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
941 tcp_chr_set_client_ioc_name(chr
, sioc
);
942 if (s
->registered_yank
) {
943 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
944 char_socket_yank_iochannel
,
947 ret
= tcp_chr_new_client(chr
, sioc
);
948 object_unref(OBJECT(sioc
));
952 static void tcp_chr_accept(QIONetListener
*listener
,
953 QIOChannelSocket
*cioc
,
956 Chardev
*chr
= CHARDEV(opaque
);
957 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
959 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
960 tcp_chr_set_client_ioc_name(chr
, cioc
);
961 if (s
->registered_yank
) {
962 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
963 char_socket_yank_iochannel
,
966 tcp_chr_new_client(chr
, cioc
);
970 static int tcp_chr_connect_client_sync(Chardev
*chr
, Error
**errp
)
972 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
973 QIOChannelSocket
*sioc
= qio_channel_socket_new();
974 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
975 tcp_chr_set_client_ioc_name(chr
, sioc
);
976 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
977 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_DISCONNECTED
);
978 object_unref(OBJECT(sioc
));
981 if (s
->registered_yank
) {
982 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
983 char_socket_yank_iochannel
,
986 tcp_chr_new_client(chr
, sioc
);
987 object_unref(OBJECT(sioc
));
992 static void tcp_chr_accept_server_sync(Chardev
*chr
)
994 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
995 QIOChannelSocket
*sioc
;
996 info_report("QEMU waiting for connection on: %s",
998 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
999 sioc
= qio_net_listener_wait_client(s
->listener
);
1000 tcp_chr_set_client_ioc_name(chr
, sioc
);
1001 if (s
->registered_yank
) {
1002 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
1003 char_socket_yank_iochannel
,
1006 tcp_chr_new_client(chr
, sioc
);
1007 object_unref(OBJECT(sioc
));
1011 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
1013 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1014 const char *opts
[] = { "telnet", "tn3270", "websock", "tls-creds" };
1015 bool optset
[] = { s
->is_telnet
, s
->is_tn3270
, s
->is_websock
, s
->tls_creds
};
1018 QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts
) != G_N_ELEMENTS(optset
));
1019 for (i
= 0; i
< G_N_ELEMENTS(opts
); i
++) {
1022 "'%s' option is incompatible with waiting for "
1023 "connection completion", opts
[i
]);
1028 tcp_chr_reconn_timer_cancel(s
);
1031 * We expect states to be as follows:
1034 * - wait -> CONNECTED
1035 * - nowait -> DISCONNECTED
1037 * - reconnect == 0 -> CONNECTED
1038 * - reconnect != 0 -> CONNECTING
1041 if (s
->state
== TCP_CHARDEV_STATE_CONNECTING
) {
1042 if (!s
->connect_task
) {
1044 "Unexpected 'connecting' state without connect task "
1045 "while waiting for connection completion");
1049 * tcp_chr_wait_connected should only ever be run from the
1050 * main loop thread associated with chr->gcontext, otherwise
1051 * qio_task_wait_thread has a dangerous race condition with
1052 * free'ing of the s->connect_task object.
1054 * Acquiring the main context doesn't 100% prove we're in
1055 * the main loop thread, but it does at least guarantee
1056 * that the main loop won't be executed by another thread
1057 * avoiding the race condition with the task idle callback.
1059 g_main_context_acquire(chr
->gcontext
);
1060 qio_task_wait_thread(s
->connect_task
);
1061 g_main_context_release(chr
->gcontext
);
1064 * The completion callback (qemu_chr_socket_connected) for
1065 * s->connect_task should have set this to NULL by the time
1066 * qio_task_wait_thread has returned.
1068 assert(!s
->connect_task
);
1071 * NB we are *not* guaranteed to have "s->state == ..CONNECTED"
1072 * at this point as this first connect may be failed, so
1073 * allow the next loop to run regardless.
1077 while (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
1079 tcp_chr_accept_server_sync(chr
);
1082 if (tcp_chr_connect_client_sync(chr
, &err
) < 0) {
1083 if (s
->reconnect_time_ms
) {
1085 g_usleep(s
->reconnect_time_ms
* 1000ULL);
1087 error_propagate(errp
, err
);
1097 static void char_socket_finalize(Object
*obj
)
1099 Chardev
*chr
= CHARDEV(obj
);
1100 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1102 tcp_chr_free_connection(chr
);
1103 tcp_chr_reconn_timer_cancel(s
);
1104 qapi_free_SocketAddress(s
->addr
);
1105 tcp_chr_telnet_destroy(s
);
1106 g_free(s
->telnet_init
);
1108 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
1109 NULL
, chr
->gcontext
);
1110 object_unref(OBJECT(s
->listener
));
1114 object_unref(OBJECT(s
->tls_creds
));
1116 g_free(s
->tls_authz
);
1117 if (s
->registered_yank
) {
1119 * In the chardev-change special-case, we shouldn't unregister the yank
1120 * instance, as it still may be needed.
1122 if (!chr
->handover_yank_instance
) {
1123 yank_unregister_instance(CHARDEV_YANK_INSTANCE(chr
->label
));
1127 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1130 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
1132 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
1133 Chardev
*chr
= CHARDEV(opaque
);
1134 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1137 s
->connect_task
= NULL
;
1139 if (qio_task_propagate_error(task
, &err
)) {
1140 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_DISCONNECTED
);
1141 if (s
->registered_yank
) {
1142 yank_unregister_function(CHARDEV_YANK_INSTANCE(chr
->label
),
1143 char_socket_yank_iochannel
,
1146 check_report_connect_error(chr
, err
);
1150 s
->connect_err_reported
= false;
1151 tcp_chr_new_client(chr
, sioc
);
1154 object_unref(OBJECT(sioc
));
1158 static void tcp_chr_connect_client_task(QIOTask
*task
,
1161 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
1162 SocketAddress
*addr
= opaque
;
1165 qio_channel_socket_connect_sync(ioc
, addr
, &err
);
1167 qio_task_set_error(task
, err
);
1171 static void tcp_chr_connect_client_async(Chardev
*chr
)
1173 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1174 QIOChannelSocket
*sioc
;
1176 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
1177 sioc
= qio_channel_socket_new();
1178 tcp_chr_set_client_ioc_name(chr
, sioc
);
1179 if (s
->registered_yank
) {
1180 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
1181 char_socket_yank_iochannel
,
1185 * Normally code would use the qio_channel_socket_connect_async
1186 * method which uses a QIOTask + qio_task_set_error internally
1187 * to avoid blocking. The tcp_chr_wait_connected method, however,
1188 * needs a way to synchronize with completion of the background
1189 * connect task which can't be done with the QIOChannelSocket
1190 * async APIs. Thus we must use QIOTask directly to implement
1191 * the non-blocking concept locally.
1193 s
->connect_task
= qio_task_new(OBJECT(sioc
),
1194 qemu_chr_socket_connected
,
1195 object_ref(OBJECT(chr
)),
1196 (GDestroyNotify
)object_unref
);
1197 qio_task_run_in_thread(s
->connect_task
,
1198 tcp_chr_connect_client_task
,
1204 static gboolean
socket_reconnect_timeout(gpointer opaque
)
1206 Chardev
*chr
= CHARDEV(opaque
);
1207 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
1209 qemu_mutex_lock(&chr
->chr_write_lock
);
1210 g_source_unref(s
->reconnect_timer
);
1211 s
->reconnect_timer
= NULL
;
1212 qemu_mutex_unlock(&chr
->chr_write_lock
);
1218 tcp_chr_connect_client_async(chr
);
1224 static int qmp_chardev_open_socket_server(Chardev
*chr
,
1226 bool is_waitconnect
,
1229 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1232 s
->do_telnetopt
= 1;
1234 s
->listener
= qio_net_listener_new();
1236 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
1237 qio_net_listener_set_name(s
->listener
, name
);
1240 if (s
->addr
->type
== SOCKET_ADDRESS_TYPE_FD
&& !*s
->addr
->u
.fd
.str
) {
1244 if (qio_net_listener_open_sync(s
->listener
, s
->addr
, 1, errp
) < 0) {
1245 object_unref(OBJECT(s
->listener
));
1250 qapi_free_SocketAddress(s
->addr
);
1251 s
->addr
= socket_local_address(s
->listener
->sioc
[0]->fd
, errp
);
1254 update_disconnected_filename(s
);
1256 if (is_waitconnect
) {
1257 tcp_chr_accept_server_sync(chr
);
1259 qio_net_listener_set_client_func_full(s
->listener
,
1269 static int qmp_chardev_open_socket_client(Chardev
*chr
,
1270 int64_t reconnect_ms
,
1273 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1275 if (reconnect_ms
> 0) {
1276 s
->reconnect_time_ms
= reconnect_ms
;
1277 tcp_chr_connect_client_async(chr
);
1280 return tcp_chr_connect_client_sync(chr
, errp
);
1285 static bool qmp_chardev_validate_socket(ChardevSocket
*sock
,
1286 SocketAddress
*addr
,
1289 /* Validate any options which have a dependency on address type */
1290 switch (addr
->type
) {
1291 case SOCKET_ADDRESS_TYPE_FD
:
1292 if (sock
->has_reconnect
) {
1294 "'reconnect' option is incompatible with "
1295 "'fd' address type");
1298 if (sock
->tls_creds
&&
1299 !(sock
->has_server
&& sock
->server
)) {
1301 "'tls_creds' option is incompatible with "
1302 "'fd' address type as client");
1307 case SOCKET_ADDRESS_TYPE_UNIX
:
1308 if (sock
->tls_creds
) {
1310 "'tls_creds' option is incompatible with "
1311 "'unix' address type");
1316 case SOCKET_ADDRESS_TYPE_INET
:
1319 case SOCKET_ADDRESS_TYPE_VSOCK
:
1320 if (sock
->tls_creds
) {
1322 "'tls_creds' option is incompatible with "
1323 "'vsock' address type");
1331 if (sock
->tls_authz
&& !sock
->tls_creds
) {
1332 error_setg(errp
, "'tls_authz' option requires 'tls_creds' option");
1336 /* Validate any options which have a dependency on client vs server */
1337 if (!sock
->has_server
|| sock
->server
) {
1338 if (sock
->has_reconnect
) {
1340 "'reconnect' option is incompatible with "
1341 "socket in server listen mode");
1345 if (sock
->has_websocket
&& sock
->websocket
) {
1346 error_setg(errp
, "%s", "Websocket client is not implemented");
1349 if (sock
->has_wait
) {
1350 error_setg(errp
, "%s",
1351 "'wait' option is incompatible with "
1352 "socket in client connect mode");
1357 if (sock
->has_reconnect_ms
&& sock
->has_reconnect
) {
1359 "'reconnect' and 'reconnect-ms' are mutually exclusive");
1367 static void qmp_chardev_open_socket(Chardev
*chr
,
1368 ChardevBackend
*backend
,
1372 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1373 ChardevSocket
*sock
= backend
->u
.socket
.data
;
1374 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
1375 bool is_listen
= sock
->has_server
? sock
->server
: true;
1376 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
1377 bool is_tn3270
= sock
->has_tn3270
? sock
->tn3270
: false;
1378 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
1379 bool is_websock
= sock
->has_websocket
? sock
->websocket
: false;
1380 int64_t reconnect_ms
= 0;
1381 SocketAddress
*addr
;
1383 s
->is_listen
= is_listen
;
1384 s
->is_telnet
= is_telnet
;
1385 s
->is_tn3270
= is_tn3270
;
1386 s
->is_websock
= is_websock
;
1387 s
->do_nodelay
= do_nodelay
;
1388 if (sock
->tls_creds
) {
1390 creds
= object_resolve_path_component(
1391 object_get_objects_root(), sock
->tls_creds
);
1393 error_setg(errp
, "No TLS credentials with id '%s'",
1397 s
->tls_creds
= (QCryptoTLSCreds
*)
1398 object_dynamic_cast(creds
,
1399 TYPE_QCRYPTO_TLS_CREDS
);
1400 if (!s
->tls_creds
) {
1401 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1405 object_ref(OBJECT(s
->tls_creds
));
1406 if (!qcrypto_tls_creds_check_endpoint(s
->tls_creds
,
1408 ? QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
1409 : QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
1414 s
->tls_authz
= g_strdup(sock
->tls_authz
);
1416 s
->addr
= addr
= socket_address_flatten(sock
->addr
);
1418 if (!qmp_chardev_validate_socket(sock
, addr
, errp
)) {
1422 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
1424 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1425 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
1426 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
1431 * In the chardev-change special-case, we shouldn't register a new yank
1432 * instance, as there already may be one.
1434 if (!chr
->handover_yank_instance
) {
1435 if (!yank_register_instance(CHARDEV_YANK_INSTANCE(chr
->label
), errp
)) {
1439 s
->registered_yank
= true;
1441 /* be isn't opened until we get a connection */
1444 update_disconnected_filename(s
);
1447 if (qmp_chardev_open_socket_server(chr
, is_telnet
|| is_tn3270
,
1448 is_waitconnect
, errp
) < 0) {
1452 if (sock
->has_reconnect
) {
1453 reconnect_ms
= sock
->reconnect
* 1000ULL;
1454 } else if (sock
->has_reconnect_ms
) {
1455 reconnect_ms
= sock
->reconnect_ms
;
1458 if (qmp_chardev_open_socket_client(chr
, reconnect_ms
, errp
) < 0) {
1464 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
1467 const char *path
= qemu_opt_get(opts
, "path");
1468 const char *host
= qemu_opt_get(opts
, "host");
1469 const char *port
= qemu_opt_get(opts
, "port");
1470 const char *fd
= qemu_opt_get(opts
, "fd");
1472 bool tight
= qemu_opt_get_bool(opts
, "tight", true);
1473 bool abstract
= qemu_opt_get_bool(opts
, "abstract", false);
1475 SocketAddressLegacy
*addr
;
1476 ChardevSocket
*sock
;
1478 if ((!!path
+ !!fd
+ !!host
) > 1) {
1480 "None or one of 'path', 'fd' or 'host' option required.");
1484 if (host
&& !port
) {
1485 error_setg(errp
, "chardev: socket: no port given");
1489 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
1490 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
1491 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
1493 if (qemu_opt_get(opts
, "delay") && qemu_opt_get(opts
, "nodelay")) {
1494 error_setg(errp
, "'delay' and 'nodelay' are mutually exclusive");
1498 qemu_opt_get(opts
, "delay") ||
1499 qemu_opt_get(opts
, "nodelay");
1501 !qemu_opt_get_bool(opts
, "delay", true) ||
1502 qemu_opt_get_bool(opts
, "nodelay", false);
1505 * We have different default to QMP for 'server', hence
1506 * we can't just check for existence of 'server'
1508 sock
->has_server
= true;
1509 sock
->server
= qemu_opt_get_bool(opts
, "server", false);
1510 sock
->has_telnet
= qemu_opt_get(opts
, "telnet");
1511 sock
->telnet
= qemu_opt_get_bool(opts
, "telnet", false);
1512 sock
->has_tn3270
= qemu_opt_get(opts
, "tn3270");
1513 sock
->tn3270
= qemu_opt_get_bool(opts
, "tn3270", false);
1514 sock
->has_websocket
= qemu_opt_get(opts
, "websocket");
1515 sock
->websocket
= qemu_opt_get_bool(opts
, "websocket", false);
1517 * We have different default to QMP for 'wait' when 'server'
1518 * is set, hence we can't just check for existence of 'wait'
1520 sock
->has_wait
= qemu_opt_find(opts
, "wait") || sock
->server
;
1521 sock
->wait
= qemu_opt_get_bool(opts
, "wait", true);
1522 sock
->has_reconnect
= qemu_opt_find(opts
, "reconnect");
1523 sock
->reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
1524 sock
->has_reconnect_ms
= qemu_opt_find(opts
, "reconnect-ms");
1525 sock
->reconnect_ms
= qemu_opt_get_number(opts
, "reconnect-ms", 0);
1527 sock
->tls_creds
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
1528 sock
->tls_authz
= g_strdup(qemu_opt_get(opts
, "tls-authz"));
1530 addr
= g_new0(SocketAddressLegacy
, 1);
1532 UnixSocketAddress
*q_unix
;
1533 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1534 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1535 q_unix
->path
= g_strdup(path
);
1537 q_unix
->has_tight
= true;
1538 q_unix
->tight
= tight
;
1539 q_unix
->has_abstract
= true;
1540 q_unix
->abstract
= abstract
;
1543 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1544 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
1545 *addr
->u
.inet
.data
= (InetSocketAddress
) {
1546 .host
= g_strdup(host
),
1547 .port
= g_strdup(port
),
1548 .has_to
= qemu_opt_get(opts
, "to"),
1549 .to
= qemu_opt_get_number(opts
, "to", 0),
1550 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
1551 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
1552 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
1553 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
1556 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1557 addr
->u
.fd
.data
= g_new(FdSocketAddress
, 1);
1558 addr
->u
.fd
.data
->str
= g_strdup(fd
);
1564 char_socket_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1565 void *opaque
, Error
**errp
)
1567 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1569 visit_type_SocketAddress(v
, name
, &s
->addr
, errp
);
1573 char_socket_get_connected(Object
*obj
, Error
**errp
)
1575 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1577 return s
->state
== TCP_CHARDEV_STATE_CONNECTED
;
1580 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
1582 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1584 cc
->supports_yank
= true;
1586 cc
->parse
= qemu_chr_parse_socket
;
1587 cc
->open
= qmp_chardev_open_socket
;
1588 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
1589 cc
->chr_write
= tcp_chr_write
;
1590 cc
->chr_sync_read
= tcp_chr_sync_read
;
1591 cc
->chr_disconnect
= tcp_chr_disconnect
;
1592 cc
->get_msgfds
= tcp_get_msgfds
;
1593 cc
->set_msgfds
= tcp_set_msgfds
;
1594 cc
->chr_add_client
= tcp_chr_add_client
;
1595 cc
->chr_add_watch
= tcp_chr_add_watch
;
1596 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1598 object_class_property_add(oc
, "addr", "SocketAddress",
1599 char_socket_get_addr
, NULL
,
1602 object_class_property_add_bool(oc
, "connected", char_socket_get_connected
,
1606 static const TypeInfo char_socket_type_info
= {
1607 .name
= TYPE_CHARDEV_SOCKET
,
1608 .parent
= TYPE_CHARDEV
,
1609 .instance_size
= sizeof(SocketChardev
),
1610 .instance_finalize
= char_socket_finalize
,
1611 .class_init
= char_socket_class_init
,
1614 static void register_types(void)
1616 type_register_static(&char_socket_type_info
);
1619 type_init(register_types
);