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 static int tcp_chr_read_poll(void *opaque
);
101 static void tcp_chr_disconnect(Chardev
*chr
);
103 /* Called with chr_write_lock held. */
104 static int tcp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
106 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
109 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
111 s
->write_msgfds_num
);
113 /* free the written msgfds, no matter what */
114 if (s
->write_msgfds_num
) {
115 g_free(s
->write_msgfds
);
117 s
->write_msgfds_num
= 0;
120 if (ret
< 0 && errno
!= EAGAIN
) {
121 if (tcp_chr_read_poll(chr
) <= 0) {
122 tcp_chr_disconnect(chr
);
124 } /* else let the read handler finish it properly */
129 /* XXX: indicate an error ? */
134 static int tcp_chr_read_poll(void *opaque
)
136 Chardev
*chr
= CHARDEV(opaque
);
137 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
141 s
->max_size
= qemu_chr_be_can_write(chr
);
146 #define IAC_BREAK 243
147 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
149 uint8_t *buf
, int *size
)
151 /* Handle any telnet client's basic IAC options to satisfy char by
152 * char mode with no echo. All IAC options will be removed from
153 * the buf and the do_telnetopt variable will be used to track the
154 * state of the width of the IAC information.
156 * IAC commands come in sets of 3 bytes with the exception of the
157 * "IAC BREAK" command and the double IAC.
163 for (i
= 0; i
< *size
; i
++) {
164 if (s
->do_telnetopt
> 1) {
165 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
166 /* Double IAC means send an IAC */
173 if ((unsigned char)buf
[i
] == IAC_BREAK
174 && s
->do_telnetopt
== 2) {
175 /* Handle IAC break commands by sending a serial break */
176 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
181 if (s
->do_telnetopt
>= 4) {
185 if ((unsigned char)buf
[i
] == IAC
) {
198 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
200 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
202 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
204 assert(num
<= TCP_MAX_FDS
);
209 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
211 /* Close unused fds */
212 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
213 close(s
->read_msgfds
[i
]);
216 g_free(s
->read_msgfds
);
218 s
->read_msgfds_num
= 0;
224 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
226 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
228 /* clear old pending fd array */
229 g_free(s
->write_msgfds
);
230 s
->write_msgfds
= NULL
;
231 s
->write_msgfds_num
= 0;
234 !qio_channel_has_feature(s
->ioc
,
235 QIO_CHANNEL_FEATURE_FD_PASS
)) {
240 s
->write_msgfds
= g_new(int, num
);
241 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
244 s
->write_msgfds_num
= num
;
249 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
251 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
252 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
256 size_t msgfds_num
= 0;
258 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
259 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
260 &msgfds
, &msgfds_num
,
263 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
268 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
271 } else if (ret
== -1) {
276 /* close and clean read_msgfds */
277 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
278 close(s
->read_msgfds
[i
]);
281 if (s
->read_msgfds_num
) {
282 g_free(s
->read_msgfds
);
285 s
->read_msgfds
= msgfds
;
286 s
->read_msgfds_num
= msgfds_num
;
289 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
290 int fd
= s
->read_msgfds
[i
];
295 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
298 #ifndef MSG_CMSG_CLOEXEC
299 qemu_set_cloexec(fd
);
306 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
308 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
309 return qio_channel_create_watch(s
->ioc
, cond
);
312 static void tcp_chr_free_connection(Chardev
*chr
)
314 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
321 if (s
->read_msgfds_num
) {
322 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
323 close(s
->read_msgfds
[i
]);
325 g_free(s
->read_msgfds
);
326 s
->read_msgfds
= NULL
;
327 s
->read_msgfds_num
= 0;
330 tcp_set_msgfds(chr
, NULL
, 0);
331 remove_fd_in_watch(chr
);
332 object_unref(OBJECT(s
->sioc
));
334 object_unref(OBJECT(s
->ioc
));
336 g_free(chr
->filename
);
337 chr
->filename
= NULL
;
341 static char *SocketAddress_to_str(const char *prefix
, SocketAddress
*addr
,
342 bool is_listen
, bool is_telnet
)
344 switch (addr
->type
) {
345 case SOCKET_ADDRESS_KIND_INET
:
346 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
347 is_telnet
? "telnet" : "tcp",
348 addr
->u
.inet
.data
->host
,
349 addr
->u
.inet
.data
->port
,
350 is_listen
? ",server" : "");
352 case SOCKET_ADDRESS_KIND_UNIX
:
353 return g_strdup_printf("%sunix:%s%s", prefix
,
354 addr
->u
.q_unix
.data
->path
,
355 is_listen
? ",server" : "");
357 case SOCKET_ADDRESS_KIND_FD
:
358 return g_strdup_printf("%sfd:%s%s", prefix
, addr
->u
.fd
.data
->str
,
359 is_listen
? ",server" : "");
366 static void tcp_chr_disconnect(Chardev
*chr
)
368 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
374 tcp_chr_free_connection(chr
);
377 s
->listen_tag
= qio_channel_add_watch(
378 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, tcp_chr_accept
, chr
, NULL
);
380 chr
->filename
= SocketAddress_to_str("disconnected:", s
->addr
,
381 s
->is_listen
, s
->is_telnet
);
382 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
383 if (s
->reconnect_time
) {
384 qemu_chr_socket_restart_timer(chr
);
388 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
390 Chardev
*chr
= CHARDEV(opaque
);
391 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
392 uint8_t buf
[CHR_READ_BUF_LEN
];
395 if (!s
->connected
|| s
->max_size
<= 0) {
399 if (len
> s
->max_size
) {
402 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
403 if (size
== 0 || size
== -1) {
404 /* connection closed */
405 tcp_chr_disconnect(chr
);
406 } else if (size
> 0) {
407 if (s
->do_telnetopt
) {
408 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
411 qemu_chr_be_write(chr
, buf
, size
);
418 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
420 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
427 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
429 /* connection closed */
430 tcp_chr_disconnect(chr
);
436 static char *sockaddr_to_str(struct sockaddr_storage
*ss
, socklen_t ss_len
,
437 struct sockaddr_storage
*ps
, socklen_t ps_len
,
438 bool is_listen
, bool is_telnet
)
440 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
441 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
442 const char *left
= "", *right
= "";
444 switch (ss
->ss_family
) {
447 return g_strdup_printf("unix:%s%s",
448 ((struct sockaddr_un
*)(ss
))->sun_path
,
449 is_listen
? ",server" : "");
456 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
457 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
458 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
459 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
460 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
461 is_telnet
? "telnet" : "tcp",
462 left
, shost
, right
, sserv
,
463 is_listen
? ",server" : "",
464 left
, phost
, right
, pserv
);
467 return g_strdup_printf("unknown");
471 static void tcp_chr_connect(void *opaque
)
473 Chardev
*chr
= CHARDEV(opaque
);
474 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
476 g_free(chr
->filename
);
477 chr
->filename
= sockaddr_to_str(
478 &s
->sioc
->localAddr
, s
->sioc
->localAddrLen
,
479 &s
->sioc
->remoteAddr
, s
->sioc
->remoteAddrLen
,
480 s
->is_listen
, s
->is_telnet
);
484 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
489 qemu_chr_be_generic_open(chr
);
492 static void tcp_chr_update_read_handler(Chardev
*chr
,
493 GMainContext
*context
)
495 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
501 remove_fd_in_watch(chr
);
503 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
514 } TCPChardevTelnetInit
;
516 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
517 GIOCondition cond G_GNUC_UNUSED
,
520 TCPChardevTelnetInit
*init
= user_data
;
523 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
525 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
528 tcp_chr_disconnect(init
->chr
);
534 if (init
->buflen
== 0) {
535 tcp_chr_connect(init
->chr
);
539 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
544 static void tcp_chr_telnet_init(Chardev
*chr
)
546 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
547 TCPChardevTelnetInit
*init
= g_new0(TCPChardevTelnetInit
, 1);
553 #define IACSET(x, a, b, c) \
560 /* Prep the telnet negotion to put telnet in binary,
561 * no echo, single char mode */
562 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
563 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
564 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
565 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
569 qio_channel_add_watch(
571 tcp_chr_telnet_init_io
,
576 static void tcp_chr_tls_handshake(QIOTask
*task
,
579 Chardev
*chr
= user_data
;
580 SocketChardev
*s
= user_data
;
582 if (qio_task_propagate_error(task
, NULL
)) {
583 tcp_chr_disconnect(chr
);
585 if (s
->do_telnetopt
) {
586 tcp_chr_telnet_init(chr
);
588 tcp_chr_connect(chr
);
594 static void tcp_chr_tls_init(Chardev
*chr
)
596 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
602 tioc
= qio_channel_tls_new_server(
603 s
->ioc
, s
->tls_creds
,
604 NULL
, /* XXX Use an ACL */
607 tioc
= qio_channel_tls_new_client(
608 s
->ioc
, s
->tls_creds
,
609 s
->addr
->u
.inet
.data
->host
,
614 tcp_chr_disconnect(chr
);
617 name
= g_strdup_printf("chardev-tls-%s-%s",
618 s
->is_listen
? "server" : "client",
620 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
622 object_unref(OBJECT(s
->ioc
));
623 s
->ioc
= QIO_CHANNEL(tioc
);
625 qio_channel_tls_handshake(tioc
,
626 tcp_chr_tls_handshake
,
632 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
633 QIOChannelSocket
*sioc
)
635 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
637 name
= g_strdup_printf("chardev-tcp-%s-%s",
638 s
->is_listen
? "server" : "client",
640 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
645 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
647 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
649 if (s
->ioc
!= NULL
) {
653 s
->ioc
= QIO_CHANNEL(sioc
);
654 object_ref(OBJECT(sioc
));
656 object_ref(OBJECT(sioc
));
658 qio_channel_set_blocking(s
->ioc
, false, NULL
);
661 qio_channel_set_delay(s
->ioc
, false);
664 g_source_remove(s
->listen_tag
);
669 tcp_chr_tls_init(chr
);
671 if (s
->do_telnetopt
) {
672 tcp_chr_telnet_init(chr
);
674 tcp_chr_connect(chr
);
682 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
685 QIOChannelSocket
*sioc
;
687 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
691 tcp_chr_set_client_ioc_name(chr
, sioc
);
692 ret
= tcp_chr_new_client(chr
, sioc
);
693 object_unref(OBJECT(sioc
));
697 static gboolean
tcp_chr_accept(QIOChannel
*channel
,
701 Chardev
*chr
= CHARDEV(opaque
);
702 QIOChannelSocket
*sioc
;
704 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel
),
710 tcp_chr_new_client(chr
, sioc
);
712 object_unref(OBJECT(sioc
));
717 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
719 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
720 QIOChannelSocket
*sioc
;
722 /* It can't wait on s->connected, since it is set asynchronously
723 * in TLS and telnet cases, only wait for an accepted socket */
726 error_report("QEMU waiting for connection on: %s",
728 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), true, NULL
);
729 tcp_chr_accept(QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
, chr
);
730 qio_channel_set_blocking(QIO_CHANNEL(s
->listen_ioc
), false, NULL
);
732 sioc
= qio_channel_socket_new();
733 tcp_chr_set_client_ioc_name(chr
, sioc
);
734 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
735 object_unref(OBJECT(sioc
));
738 tcp_chr_new_client(chr
, sioc
);
739 object_unref(OBJECT(sioc
));
746 static void char_socket_finalize(Object
*obj
)
748 Chardev
*chr
= CHARDEV(obj
);
749 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
751 tcp_chr_free_connection(chr
);
753 if (s
->reconnect_timer
) {
754 g_source_remove(s
->reconnect_timer
);
755 s
->reconnect_timer
= 0;
757 qapi_free_SocketAddress(s
->addr
);
759 g_source_remove(s
->listen_tag
);
763 object_unref(OBJECT(s
->listen_ioc
));
766 object_unref(OBJECT(s
->tls_creds
));
769 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
772 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
774 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
775 Chardev
*chr
= CHARDEV(opaque
);
776 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
779 if (qio_task_propagate_error(task
, &err
)) {
780 check_report_connect_error(chr
, err
);
785 s
->connect_err_reported
= false;
786 tcp_chr_new_client(chr
, sioc
);
789 object_unref(OBJECT(sioc
));
792 static gboolean
socket_reconnect_timeout(gpointer opaque
)
794 Chardev
*chr
= CHARDEV(opaque
);
795 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
796 QIOChannelSocket
*sioc
;
798 s
->reconnect_timer
= 0;
804 sioc
= qio_channel_socket_new();
805 tcp_chr_set_client_ioc_name(chr
, sioc
);
806 qio_channel_socket_connect_async(sioc
, s
->addr
,
807 qemu_chr_socket_connected
,
813 static void qmp_chardev_open_socket(Chardev
*chr
,
814 ChardevBackend
*backend
,
818 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
819 ChardevSocket
*sock
= backend
->u
.socket
.data
;
820 SocketAddress
*addr
= sock
->addr
;
821 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
822 bool is_listen
= sock
->has_server
? sock
->server
: true;
823 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
824 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
825 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
826 QIOChannelSocket
*sioc
= NULL
;
828 s
->is_unix
= addr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
829 s
->is_listen
= is_listen
;
830 s
->is_telnet
= is_telnet
;
831 s
->do_nodelay
= do_nodelay
;
832 if (sock
->tls_creds
) {
834 creds
= object_resolve_path_component(
835 object_get_objects_root(), sock
->tls_creds
);
837 error_setg(errp
, "No TLS credentials with id '%s'",
841 s
->tls_creds
= (QCryptoTLSCreds
*)
842 object_dynamic_cast(creds
,
843 TYPE_QCRYPTO_TLS_CREDS
);
845 error_setg(errp
, "Object with id '%s' is not TLS credentials",
849 object_ref(OBJECT(s
->tls_creds
));
851 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
852 error_setg(errp
, "%s",
853 "Expected TLS credentials for server endpoint");
857 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
858 error_setg(errp
, "%s",
859 "Expected TLS credentials for client endpoint");
865 s
->addr
= QAPI_CLONE(SocketAddress
, sock
->addr
);
867 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
869 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
872 /* be isn't opened until we get a connection */
875 chr
->filename
= SocketAddress_to_str("disconnected:",
876 addr
, is_listen
, is_telnet
);
882 } else if (reconnect
> 0) {
883 s
->reconnect_time
= reconnect
;
886 if (s
->reconnect_time
) {
887 sioc
= qio_channel_socket_new();
888 tcp_chr_set_client_ioc_name(chr
, sioc
);
889 qio_channel_socket_connect_async(sioc
, s
->addr
,
890 qemu_chr_socket_connected
,
895 sioc
= qio_channel_socket_new();
897 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
898 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
901 if (qio_channel_socket_listen_sync(sioc
, s
->addr
, errp
) < 0) {
904 s
->listen_ioc
= sioc
;
905 if (is_waitconnect
&&
906 qemu_chr_wait_connected(chr
, errp
) < 0) {
910 s
->listen_tag
= qio_channel_add_watch(
911 QIO_CHANNEL(s
->listen_ioc
), G_IO_IN
,
912 tcp_chr_accept
, chr
, NULL
);
914 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
923 object_unref(OBJECT(sioc
));
927 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
930 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
931 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
932 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
933 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
934 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
935 const char *path
= qemu_opt_get(opts
, "path");
936 const char *host
= qemu_opt_get(opts
, "host");
937 const char *port
= qemu_opt_get(opts
, "port");
938 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
942 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
945 error_setg(errp
, "chardev: socket: no host given");
949 error_setg(errp
, "chardev: socket: no port given");
954 error_setg(errp
, "TLS can only be used over TCP socket");
959 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
960 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
962 sock
->has_nodelay
= true;
963 sock
->nodelay
= do_nodelay
;
964 sock
->has_server
= true;
965 sock
->server
= is_listen
;
966 sock
->has_telnet
= true;
967 sock
->telnet
= is_telnet
;
968 sock
->has_wait
= true;
969 sock
->wait
= is_waitconnect
;
970 sock
->has_reconnect
= true;
971 sock
->reconnect
= reconnect
;
972 sock
->tls_creds
= g_strdup(tls_creds
);
974 addr
= g_new0(SocketAddress
, 1);
976 UnixSocketAddress
*q_unix
;
977 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
978 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
979 q_unix
->path
= g_strdup(path
);
981 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
982 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
983 *addr
->u
.inet
.data
= (InetSocketAddress
) {
984 .host
= g_strdup(host
),
985 .port
= g_strdup(port
),
986 .has_to
= qemu_opt_get(opts
, "to"),
987 .to
= qemu_opt_get_number(opts
, "to", 0),
988 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
989 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
990 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
991 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
997 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
999 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1001 cc
->parse
= qemu_chr_parse_socket
;
1002 cc
->open
= qmp_chardev_open_socket
;
1003 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
1004 cc
->chr_write
= tcp_chr_write
;
1005 cc
->chr_sync_read
= tcp_chr_sync_read
;
1006 cc
->chr_disconnect
= tcp_chr_disconnect
;
1007 cc
->get_msgfds
= tcp_get_msgfds
;
1008 cc
->set_msgfds
= tcp_set_msgfds
;
1009 cc
->chr_add_client
= tcp_chr_add_client
;
1010 cc
->chr_add_watch
= tcp_chr_add_watch
;
1011 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1014 static const TypeInfo char_socket_type_info
= {
1015 .name
= TYPE_CHARDEV_SOCKET
,
1016 .parent
= TYPE_CHARDEV
,
1017 .instance_size
= sizeof(SocketChardev
),
1018 .instance_finalize
= char_socket_finalize
,
1019 .class_init
= char_socket_class_init
,
1022 static void register_types(void)
1024 type_register_static(&char_socket_type_info
);
1027 type_init(register_types
);