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
24 #include "qemu/osdep.h"
25 #include "sysemu/char.h"
26 #include "io/channel-socket.h"
27 #include "io/channel-tls.h"
28 #include "qemu/error-report.h"
29 #include "qapi/error.h"
30 #include "qapi/clone-visitor.h"
34 /***********************************************************/
37 #define TCP_MAX_FDS 16
41 QIOChannel
*ioc
; /* Client I/O channel */
42 QIOChannelSocket
*sioc
; /* Client master channel */
43 QIOChannelSocket
*listen_ioc
;
45 QCryptoTLSCreds
*tls_creds
;
52 size_t read_msgfds_num
;
54 size_t write_msgfds_num
;
60 guint reconnect_timer
;
61 int64_t reconnect_time
;
62 bool connect_err_reported
;
65 #define SOCKET_CHARDEV(obj) \
66 OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
68 static gboolean
socket_reconnect_timeout(gpointer opaque
);
70 static void qemu_chr_socket_restart_timer(Chardev
*chr
)
72 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
75 assert(s
->connected
== 0);
76 s
->reconnect_timer
= g_timeout_add_seconds(s
->reconnect_time
,
77 socket_reconnect_timeout
, chr
);
78 name
= g_strdup_printf("chardev-socket-reconnect-%s", chr
->label
);
79 g_source_set_name_by_id(s
->reconnect_timer
, name
);
83 static void check_report_connect_error(Chardev
*chr
,
86 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
88 if (!s
->connect_err_reported
) {
89 error_report("Unable to connect character device %s: %s",
90 chr
->label
, error_get_pretty(err
));
91 s
->connect_err_reported
= true;
93 qemu_chr_socket_restart_timer(chr
);
96 static gboolean
tcp_chr_accept(QIOChannel
*chan
,
100 /* Called with chr_write_lock held. */
101 static int tcp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
103 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
106 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
108 s
->write_msgfds_num
);
110 /* free the written msgfds, no matter what */
111 if (s
->write_msgfds_num
) {
112 g_free(s
->write_msgfds
);
114 s
->write_msgfds_num
= 0;
119 /* XXX: indicate an error ? */
124 static int tcp_chr_read_poll(void *opaque
)
126 Chardev
*chr
= CHARDEV(opaque
);
127 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
131 s
->max_size
= qemu_chr_be_can_write(chr
);
136 #define IAC_BREAK 243
137 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
139 uint8_t *buf
, int *size
)
141 /* Handle any telnet client's basic IAC options to satisfy char by
142 * char mode with no echo. All IAC options will be removed from
143 * the buf and the do_telnetopt variable will be used to track the
144 * state of the width of the IAC information.
146 * IAC commands come in sets of 3 bytes with the exception of the
147 * "IAC BREAK" command and the double IAC.
153 for (i
= 0; i
< *size
; i
++) {
154 if (s
->do_telnetopt
> 1) {
155 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
156 /* Double IAC means send an IAC */
163 if ((unsigned char)buf
[i
] == IAC_BREAK
164 && s
->do_telnetopt
== 2) {
165 /* Handle IAC break commands by sending a serial break */
166 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
171 if (s
->do_telnetopt
>= 4) {
175 if ((unsigned char)buf
[i
] == IAC
) {
188 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
190 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
192 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
194 assert(num
<= TCP_MAX_FDS
);
199 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
201 /* Close unused fds */
202 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
203 close(s
->read_msgfds
[i
]);
206 g_free(s
->read_msgfds
);
208 s
->read_msgfds_num
= 0;
214 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
216 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
218 /* clear old pending fd array */
219 g_free(s
->write_msgfds
);
220 s
->write_msgfds
= NULL
;
221 s
->write_msgfds_num
= 0;
224 !qio_channel_has_feature(s
->ioc
,
225 QIO_CHANNEL_FEATURE_FD_PASS
)) {
230 s
->write_msgfds
= g_new(int, num
);
231 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
234 s
->write_msgfds_num
= num
;
239 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
241 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
242 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
246 size_t msgfds_num
= 0;
248 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
249 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
250 &msgfds
, &msgfds_num
,
253 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
258 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
261 } else if (ret
== -1) {
266 /* close and clean read_msgfds */
267 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
268 close(s
->read_msgfds
[i
]);
271 if (s
->read_msgfds_num
) {
272 g_free(s
->read_msgfds
);
275 s
->read_msgfds
= msgfds
;
276 s
->read_msgfds_num
= msgfds_num
;
279 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
280 int fd
= s
->read_msgfds
[i
];
285 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
288 #ifndef MSG_CMSG_CLOEXEC
289 qemu_set_cloexec(fd
);
296 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
298 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
299 return qio_channel_create_watch(s
->ioc
, cond
);
302 static void tcp_chr_free_connection(Chardev
*chr
)
304 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
311 if (s
->read_msgfds_num
) {
312 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
313 close(s
->read_msgfds
[i
]);
315 g_free(s
->read_msgfds
);
316 s
->read_msgfds
= NULL
;
317 s
->read_msgfds_num
= 0;
320 tcp_set_msgfds(chr
, NULL
, 0);
321 remove_fd_in_watch(chr
);
322 object_unref(OBJECT(s
->sioc
));
324 object_unref(OBJECT(s
->ioc
));
326 g_free(chr
->filename
);
327 chr
->filename
= NULL
;
331 static char *SocketAddress_to_str(const char *prefix
, SocketAddress
*addr
,
332 bool is_listen
, bool is_telnet
)
334 switch (addr
->type
) {
335 case SOCKET_ADDRESS_KIND_INET
:
336 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
337 is_telnet
? "telnet" : "tcp",
338 addr
->u
.inet
.data
->host
,
339 addr
->u
.inet
.data
->port
,
340 is_listen
? ",server" : "");
342 case SOCKET_ADDRESS_KIND_UNIX
:
343 return g_strdup_printf("%sunix:%s%s", prefix
,
344 addr
->u
.q_unix
.data
->path
,
345 is_listen
? ",server" : "");
347 case SOCKET_ADDRESS_KIND_FD
:
348 return g_strdup_printf("%sfd:%s%s", prefix
, addr
->u
.fd
.data
->str
,
349 is_listen
? ",server" : "");
356 static void tcp_chr_disconnect(Chardev
*chr
)
358 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
364 tcp_chr_free_connection(chr
);
367 s
->listen_tag
= qio_channel_add_watch(
368 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, tcp_chr_accept
, chr
, NULL
);
370 chr
->filename
= SocketAddress_to_str("disconnected:", s
->addr
,
371 s
->is_listen
, s
->is_telnet
);
372 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
373 if (s
->reconnect_time
) {
374 qemu_chr_socket_restart_timer(chr
);
378 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
380 Chardev
*chr
= CHARDEV(opaque
);
381 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
382 uint8_t buf
[CHR_READ_BUF_LEN
];
385 if (!s
->connected
|| s
->max_size
<= 0) {
389 if (len
> s
->max_size
) {
392 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
393 if (size
== 0 || size
== -1) {
394 /* connection closed */
395 tcp_chr_disconnect(chr
);
396 } else if (size
> 0) {
397 if (s
->do_telnetopt
) {
398 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
401 qemu_chr_be_write(chr
, buf
, size
);
408 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
410 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
417 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
419 /* connection closed */
420 tcp_chr_disconnect(chr
);
426 static char *sockaddr_to_str(struct sockaddr_storage
*ss
, socklen_t ss_len
,
427 struct sockaddr_storage
*ps
, socklen_t ps_len
,
428 bool is_listen
, bool is_telnet
)
430 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
431 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
432 const char *left
= "", *right
= "";
434 switch (ss
->ss_family
) {
437 return g_strdup_printf("unix:%s%s",
438 ((struct sockaddr_un
*)(ss
))->sun_path
,
439 is_listen
? ",server" : "");
446 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
447 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
448 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
449 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
450 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
451 is_telnet
? "telnet" : "tcp",
452 left
, shost
, right
, sserv
,
453 is_listen
? ",server" : "",
454 left
, phost
, right
, pserv
);
457 return g_strdup_printf("unknown");
461 static void tcp_chr_connect(void *opaque
)
463 Chardev
*chr
= CHARDEV(opaque
);
464 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
466 g_free(chr
->filename
);
467 chr
->filename
= sockaddr_to_str(
468 &s
->sioc
->localAddr
, s
->sioc
->localAddrLen
,
469 &s
->sioc
->remoteAddr
, s
->sioc
->remoteAddrLen
,
470 s
->is_listen
, s
->is_telnet
);
474 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
479 qemu_chr_be_generic_open(chr
);
482 static void tcp_chr_update_read_handler(Chardev
*chr
,
483 GMainContext
*context
)
485 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
491 remove_fd_in_watch(chr
);
493 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
504 } TCPChardevTelnetInit
;
506 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
507 GIOCondition cond G_GNUC_UNUSED
,
510 TCPChardevTelnetInit
*init
= user_data
;
513 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
515 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
518 tcp_chr_disconnect(init
->chr
);
524 if (init
->buflen
== 0) {
525 tcp_chr_connect(init
->chr
);
529 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
534 static void tcp_chr_telnet_init(Chardev
*chr
)
536 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
537 TCPChardevTelnetInit
*init
= g_new0(TCPChardevTelnetInit
, 1);
543 #define IACSET(x, a, b, c) \
550 /* Prep the telnet negotion to put telnet in binary,
551 * no echo, single char mode */
552 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
553 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
554 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
555 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
559 qio_channel_add_watch(
561 tcp_chr_telnet_init_io
,
566 static void tcp_chr_tls_handshake(QIOTask
*task
,
569 Chardev
*chr
= user_data
;
570 SocketChardev
*s
= user_data
;
572 if (qio_task_propagate_error(task
, NULL
)) {
573 tcp_chr_disconnect(chr
);
575 if (s
->do_telnetopt
) {
576 tcp_chr_telnet_init(chr
);
578 tcp_chr_connect(chr
);
584 static void tcp_chr_tls_init(Chardev
*chr
)
586 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
592 tioc
= qio_channel_tls_new_server(
593 s
->ioc
, s
->tls_creds
,
594 NULL
, /* XXX Use an ACL */
597 tioc
= qio_channel_tls_new_client(
598 s
->ioc
, s
->tls_creds
,
599 s
->addr
->u
.inet
.data
->host
,
604 tcp_chr_disconnect(chr
);
607 name
= g_strdup_printf("chardev-tls-%s-%s",
608 s
->is_listen
? "server" : "client",
610 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
612 object_unref(OBJECT(s
->ioc
));
613 s
->ioc
= QIO_CHANNEL(tioc
);
615 qio_channel_tls_handshake(tioc
,
616 tcp_chr_tls_handshake
,
622 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
623 QIOChannelSocket
*sioc
)
625 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
627 name
= g_strdup_printf("chardev-tcp-%s-%s",
628 s
->is_listen
? "server" : "client",
630 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
635 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
637 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
639 if (s
->ioc
!= NULL
) {
643 s
->ioc
= QIO_CHANNEL(sioc
);
644 object_ref(OBJECT(sioc
));
646 object_ref(OBJECT(sioc
));
648 qio_channel_set_blocking(s
->ioc
, false, NULL
);
651 qio_channel_set_delay(s
->ioc
, false);
654 g_source_remove(s
->listen_tag
);
659 tcp_chr_tls_init(chr
);
661 if (s
->do_telnetopt
) {
662 tcp_chr_telnet_init(chr
);
664 tcp_chr_connect(chr
);
672 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
675 QIOChannelSocket
*sioc
;
677 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
681 tcp_chr_set_client_ioc_name(chr
, sioc
);
682 ret
= tcp_chr_new_client(chr
, sioc
);
683 object_unref(OBJECT(sioc
));
687 static gboolean
tcp_chr_accept(QIOChannel
*channel
,
691 Chardev
*chr
= CHARDEV(opaque
);
692 QIOChannelSocket
*sioc
;
694 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel
),
700 tcp_chr_new_client(chr
, sioc
);
702 object_unref(OBJECT(sioc
));
707 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
709 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
710 QIOChannelSocket
*sioc
;
712 /* It can't wait on s->connected, since it is set asynchronously
713 * in TLS and telnet cases, only wait for an accepted socket */
716 error_report("QEMU waiting for connection on: %s",
718 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), true, NULL
);
719 tcp_chr_accept(QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, chr
);
720 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), false, NULL
);
722 sioc
= qio_channel_socket_new();
723 tcp_chr_set_client_ioc_name(chr
, sioc
);
724 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
725 object_unref(OBJECT(sioc
));
728 tcp_chr_new_client(chr
, sioc
);
729 object_unref(OBJECT(sioc
));
736 static void char_socket_finalize(Object
*obj
)
738 Chardev
*chr
= CHARDEV(obj
);
739 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
741 tcp_chr_free_connection(chr
);
743 if (s
->reconnect_timer
) {
744 g_source_remove(s
->reconnect_timer
);
745 s
->reconnect_timer
= 0;
747 qapi_free_SocketAddress(s
->addr
);
749 g_source_remove(s
->listen_tag
);
753 object_unref(OBJECT(s
->listen_ioc
));
756 object_unref(OBJECT(s
->tls_creds
));
759 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
762 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
764 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
765 Chardev
*chr
= CHARDEV(opaque
);
766 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
769 if (qio_task_propagate_error(task
, &err
)) {
770 check_report_connect_error(chr
, err
);
775 s
->connect_err_reported
= false;
776 tcp_chr_new_client(chr
, sioc
);
779 object_unref(OBJECT(sioc
));
782 static gboolean
socket_reconnect_timeout(gpointer opaque
)
784 Chardev
*chr
= CHARDEV(opaque
);
785 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
786 QIOChannelSocket
*sioc
;
788 s
->reconnect_timer
= 0;
794 sioc
= qio_channel_socket_new();
795 tcp_chr_set_client_ioc_name(chr
, sioc
);
796 qio_channel_socket_connect_async(sioc
, s
->addr
,
797 qemu_chr_socket_connected
,
803 static void qmp_chardev_open_socket(Chardev
*chr
,
804 ChardevBackend
*backend
,
808 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
809 ChardevSocket
*sock
= backend
->u
.socket
.data
;
810 SocketAddress
*addr
= sock
->addr
;
811 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
812 bool is_listen
= sock
->has_server
? sock
->server
: true;
813 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
814 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
815 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
816 QIOChannelSocket
*sioc
= NULL
;
818 s
->is_unix
= addr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
819 s
->is_listen
= is_listen
;
820 s
->is_telnet
= is_telnet
;
821 s
->do_nodelay
= do_nodelay
;
822 if (sock
->tls_creds
) {
824 creds
= object_resolve_path_component(
825 object_get_objects_root(), sock
->tls_creds
);
827 error_setg(errp
, "No TLS credentials with id '%s'",
831 s
->tls_creds
= (QCryptoTLSCreds
*)
832 object_dynamic_cast(creds
,
833 TYPE_QCRYPTO_TLS_CREDS
);
835 error_setg(errp
, "Object with id '%s' is not TLS credentials",
839 object_ref(OBJECT(s
->tls_creds
));
841 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
842 error_setg(errp
, "%s",
843 "Expected TLS credentials for server endpoint");
847 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
848 error_setg(errp
, "%s",
849 "Expected TLS credentials for client endpoint");
855 s
->addr
= QAPI_CLONE(SocketAddress
, sock
->addr
);
857 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
859 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
862 /* be isn't opened until we get a connection */
865 chr
->filename
= SocketAddress_to_str("disconnected:",
866 addr
, is_listen
, is_telnet
);
872 } else if (reconnect
> 0) {
873 s
->reconnect_time
= reconnect
;
876 if (s
->reconnect_time
) {
877 sioc
= qio_channel_socket_new();
878 tcp_chr_set_client_ioc_name(chr
, sioc
);
879 qio_channel_socket_connect_async(sioc
, s
->addr
,
880 qemu_chr_socket_connected
,
885 sioc
= qio_channel_socket_new();
887 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
888 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
891 if (qio_channel_socket_listen_sync(sioc
, s
->addr
, errp
) < 0) {
894 s
->listen_ioc
= sioc
;
895 if (is_waitconnect
&&
896 qemu_chr_wait_connected(chr
, errp
) < 0) {
900 s
->listen_tag
= qio_channel_add_watch(
901 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
,
902 tcp_chr_accept
, chr
, NULL
);
904 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
913 object_unref(OBJECT(sioc
));
917 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
920 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
921 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
922 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
923 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
924 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
925 const char *path
= qemu_opt_get(opts
, "path");
926 const char *host
= qemu_opt_get(opts
, "host");
927 const char *port
= qemu_opt_get(opts
, "port");
928 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
932 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
935 error_setg(errp
, "chardev: socket: no host given");
939 error_setg(errp
, "chardev: socket: no port given");
944 error_setg(errp
, "TLS can only be used over TCP socket");
949 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
950 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
952 sock
->has_nodelay
= true;
953 sock
->nodelay
= do_nodelay
;
954 sock
->has_server
= true;
955 sock
->server
= is_listen
;
956 sock
->has_telnet
= true;
957 sock
->telnet
= is_telnet
;
958 sock
->has_wait
= true;
959 sock
->wait
= is_waitconnect
;
960 sock
->has_reconnect
= true;
961 sock
->reconnect
= reconnect
;
962 sock
->tls_creds
= g_strdup(tls_creds
);
964 addr
= g_new0(SocketAddress
, 1);
966 UnixSocketAddress
*q_unix
;
967 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
968 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
969 q_unix
->path
= g_strdup(path
);
971 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
972 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
973 *addr
->u
.inet
.data
= (InetSocketAddress
) {
974 .host
= g_strdup(host
),
975 .port
= g_strdup(port
),
976 .has_to
= qemu_opt_get(opts
, "to"),
977 .to
= qemu_opt_get_number(opts
, "to", 0),
978 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
979 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
980 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
981 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
987 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
989 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
991 cc
->parse
= qemu_chr_parse_socket
;
992 cc
->open
= qmp_chardev_open_socket
;
993 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
994 cc
->chr_write
= tcp_chr_write
;
995 cc
->chr_sync_read
= tcp_chr_sync_read
;
996 cc
->chr_disconnect
= tcp_chr_disconnect
;
997 cc
->get_msgfds
= tcp_get_msgfds
;
998 cc
->set_msgfds
= tcp_set_msgfds
;
999 cc
->chr_add_client
= tcp_chr_add_client
;
1000 cc
->chr_add_watch
= tcp_chr_add_watch
;
1001 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1004 static const TypeInfo char_socket_type_info
= {
1005 .name
= TYPE_CHARDEV_SOCKET
,
1006 .parent
= TYPE_CHARDEV
,
1007 .instance_size
= sizeof(SocketChardev
),
1008 .instance_finalize
= char_socket_finalize
,
1009 .class_init
= char_socket_class_init
,
1012 static void register_types(void)
1014 type_register_static(&char_socket_type_info
);
1017 type_init(register_types
);