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-tls.h"
29 #include "io/channel-websock.h"
30 #include "io/net-listener.h"
31 #include "qemu/error-report.h"
32 #include "qemu/option.h"
33 #include "qapi/error.h"
34 #include "qapi/clone-visitor.h"
35 #include "qapi/qapi-visit-sockets.h"
37 #include "chardev/char-io.h"
39 /***********************************************************/
42 #define TCP_MAX_FDS 16
47 } TCPChardevTelnetInit
;
51 QIOChannel
*ioc
; /* Client I/O channel */
52 QIOChannelSocket
*sioc
; /* Client master channel */
53 QIONetListener
*listener
;
55 QCryptoTLSCreds
*tls_creds
;
61 size_t read_msgfds_num
;
63 size_t write_msgfds_num
;
69 GSource
*telnet_source
;
70 TCPChardevTelnetInit
*telnet_init
;
74 GSource
*reconnect_timer
;
75 int64_t reconnect_time
;
76 bool connect_err_reported
;
79 #define SOCKET_CHARDEV(obj) \
80 OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
82 static gboolean
socket_reconnect_timeout(gpointer opaque
);
83 static void tcp_chr_telnet_init(Chardev
*chr
);
85 static void tcp_chr_reconn_timer_cancel(SocketChardev
*s
)
87 if (s
->reconnect_timer
) {
88 g_source_destroy(s
->reconnect_timer
);
89 g_source_unref(s
->reconnect_timer
);
90 s
->reconnect_timer
= NULL
;
94 static void qemu_chr_socket_restart_timer(Chardev
*chr
)
96 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
99 assert(s
->connected
== 0);
100 name
= g_strdup_printf("chardev-socket-reconnect-%s", chr
->label
);
101 s
->reconnect_timer
= qemu_chr_timeout_add_ms(chr
,
102 s
->reconnect_time
* 1000,
103 socket_reconnect_timeout
,
105 g_source_set_name(s
->reconnect_timer
, name
);
109 static void check_report_connect_error(Chardev
*chr
,
112 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
114 if (!s
->connect_err_reported
) {
115 error_report("Unable to connect character device %s: %s",
116 chr
->label
, error_get_pretty(err
));
117 s
->connect_err_reported
= true;
119 qemu_chr_socket_restart_timer(chr
);
122 static void tcp_chr_accept(QIONetListener
*listener
,
123 QIOChannelSocket
*cioc
,
126 static int tcp_chr_read_poll(void *opaque
);
127 static void tcp_chr_disconnect(Chardev
*chr
);
129 /* Called with chr_write_lock held. */
130 static int tcp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
132 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
135 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
137 s
->write_msgfds_num
);
139 /* free the written msgfds in any cases
140 * other than ret < 0 && errno == EAGAIN
142 if (!(ret
< 0 && EAGAIN
== errno
)
143 && s
->write_msgfds_num
) {
144 g_free(s
->write_msgfds
);
146 s
->write_msgfds_num
= 0;
149 if (ret
< 0 && errno
!= EAGAIN
) {
150 if (tcp_chr_read_poll(chr
) <= 0) {
151 tcp_chr_disconnect(chr
);
153 } /* else let the read handler finish it properly */
158 /* XXX: indicate an error ? */
163 static int tcp_chr_read_poll(void *opaque
)
165 Chardev
*chr
= CHARDEV(opaque
);
166 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
170 s
->max_size
= qemu_chr_be_can_write(chr
);
174 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
176 uint8_t *buf
, int *size
)
178 /* Handle any telnet or tn3270 client's basic IAC options.
179 * For telnet options, it satisfies char by char mode with no echo.
180 * For tn3270 options, it satisfies binary mode with EOR.
181 * All IAC options will be removed from the buf and the do_opt
182 * pointer will be used to track the state of the width of the
185 * RFC854: "All TELNET commands consist of at least a two byte sequence.
186 * The commands dealing with option negotiation are three byte sequences,
187 * the third byte being the code for the option referenced."
188 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
189 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
191 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
192 * session, and NOP and IP need to be done later.
198 for (i
= 0; i
< *size
; i
++) {
199 if (s
->do_telnetopt
> 1) {
200 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
201 /* Double IAC means send an IAC */
208 if ((unsigned char)buf
[i
] == IAC_BREAK
209 && s
->do_telnetopt
== 2) {
210 /* Handle IAC break commands by sending a serial break */
211 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
213 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_EOR
214 || (unsigned char)buf
[i
] == IAC_SB
215 || (unsigned char)buf
[i
] == IAC_SE
)
216 && s
->do_telnetopt
== 2) {
220 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_IP
221 || (unsigned char)buf
[i
] == IAC_NOP
)
222 && s
->do_telnetopt
== 2) {
223 /* TODO: IP and NOP need to be implemented later. */
228 if (s
->do_telnetopt
>= 4) {
232 if ((unsigned char)buf
[i
] == IAC
) {
245 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
247 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
249 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
251 assert(num
<= TCP_MAX_FDS
);
256 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
258 /* Close unused fds */
259 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
260 close(s
->read_msgfds
[i
]);
263 g_free(s
->read_msgfds
);
265 s
->read_msgfds_num
= 0;
271 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
273 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
275 /* clear old pending fd array */
276 g_free(s
->write_msgfds
);
277 s
->write_msgfds
= NULL
;
278 s
->write_msgfds_num
= 0;
281 !qio_channel_has_feature(s
->ioc
,
282 QIO_CHANNEL_FEATURE_FD_PASS
)) {
287 s
->write_msgfds
= g_new(int, num
);
288 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
291 s
->write_msgfds_num
= num
;
296 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
298 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
299 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
303 size_t msgfds_num
= 0;
305 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
306 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
307 &msgfds
, &msgfds_num
,
310 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
315 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
318 } else if (ret
== -1) {
323 /* close and clean read_msgfds */
324 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
325 close(s
->read_msgfds
[i
]);
328 if (s
->read_msgfds_num
) {
329 g_free(s
->read_msgfds
);
332 s
->read_msgfds
= msgfds
;
333 s
->read_msgfds_num
= msgfds_num
;
336 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
337 int fd
= s
->read_msgfds
[i
];
342 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
345 #ifndef MSG_CMSG_CLOEXEC
346 qemu_set_cloexec(fd
);
353 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
355 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
356 return qio_channel_create_watch(s
->ioc
, cond
);
359 static void remove_hup_source(SocketChardev
*s
)
361 if (s
->hup_source
!= NULL
) {
362 g_source_destroy(s
->hup_source
);
363 g_source_unref(s
->hup_source
);
364 s
->hup_source
= NULL
;
368 static void tcp_chr_free_connection(Chardev
*chr
)
370 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
373 if (s
->read_msgfds_num
) {
374 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
375 close(s
->read_msgfds
[i
]);
377 g_free(s
->read_msgfds
);
378 s
->read_msgfds
= NULL
;
379 s
->read_msgfds_num
= 0;
382 remove_hup_source(s
);
384 tcp_set_msgfds(chr
, NULL
, 0);
385 remove_fd_in_watch(chr
);
386 object_unref(OBJECT(s
->sioc
));
388 object_unref(OBJECT(s
->ioc
));
390 g_free(chr
->filename
);
391 chr
->filename
= NULL
;
395 static const char *qemu_chr_socket_protocol(SocketChardev
*s
)
400 return s
->is_websock
? "websocket" : "tcp";
403 static char *qemu_chr_socket_address(SocketChardev
*s
, const char *prefix
)
405 switch (s
->addr
->type
) {
406 case SOCKET_ADDRESS_TYPE_INET
:
407 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
408 qemu_chr_socket_protocol(s
),
409 s
->addr
->u
.inet
.host
,
410 s
->addr
->u
.inet
.port
,
411 s
->is_listen
? ",server" : "");
413 case SOCKET_ADDRESS_TYPE_UNIX
:
414 return g_strdup_printf("%sunix:%s%s", prefix
,
415 s
->addr
->u
.q_unix
.path
,
416 s
->is_listen
? ",server" : "");
418 case SOCKET_ADDRESS_TYPE_FD
:
419 return g_strdup_printf("%sfd:%s%s", prefix
, s
->addr
->u
.fd
.str
,
420 s
->is_listen
? ",server" : "");
422 case SOCKET_ADDRESS_TYPE_VSOCK
:
423 return g_strdup_printf("%svsock:%s:%s", prefix
,
424 s
->addr
->u
.vsock
.cid
,
425 s
->addr
->u
.vsock
.port
);
431 static void update_disconnected_filename(SocketChardev
*s
)
433 Chardev
*chr
= CHARDEV(s
);
435 g_free(chr
->filename
);
437 chr
->filename
= qemu_chr_socket_address(s
, "disconnected:");
439 chr
->filename
= g_strdup("disconnected:socket");
443 /* NB may be called even if tcp_chr_connect has not been
444 * reached, due to TLS or telnet initialization failure,
445 * so can *not* assume s->connected == true
447 static void tcp_chr_disconnect(Chardev
*chr
)
449 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
450 bool emit_close
= s
->connected
;
452 tcp_chr_free_connection(chr
);
455 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
456 chr
, NULL
, chr
->gcontext
);
458 update_disconnected_filename(s
);
460 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
462 if (s
->reconnect_time
) {
463 qemu_chr_socket_restart_timer(chr
);
467 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
469 Chardev
*chr
= CHARDEV(opaque
);
470 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
471 uint8_t buf
[CHR_READ_BUF_LEN
];
474 if (!s
->connected
|| s
->max_size
<= 0) {
478 if (len
> s
->max_size
) {
481 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
482 if (size
== 0 || (size
== -1 && errno
!= EAGAIN
)) {
483 /* connection closed */
484 tcp_chr_disconnect(chr
);
485 } else if (size
> 0) {
486 if (s
->do_telnetopt
) {
487 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
490 qemu_chr_be_write(chr
, buf
, size
);
497 static gboolean
tcp_chr_hup(QIOChannel
*channel
,
501 Chardev
*chr
= CHARDEV(opaque
);
502 tcp_chr_disconnect(chr
);
503 return G_SOURCE_REMOVE
;
506 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
508 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
515 qio_channel_set_blocking(s
->ioc
, true, NULL
);
516 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
517 qio_channel_set_blocking(s
->ioc
, false, NULL
);
519 /* connection closed */
520 tcp_chr_disconnect(chr
);
526 static char *qemu_chr_compute_filename(SocketChardev
*s
)
528 struct sockaddr_storage
*ss
= &s
->sioc
->localAddr
;
529 struct sockaddr_storage
*ps
= &s
->sioc
->remoteAddr
;
530 socklen_t ss_len
= s
->sioc
->localAddrLen
;
531 socklen_t ps_len
= s
->sioc
->remoteAddrLen
;
532 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
533 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
534 const char *left
= "", *right
= "";
536 switch (ss
->ss_family
) {
539 return g_strdup_printf("unix:%s%s",
540 ((struct sockaddr_un
*)(ss
))->sun_path
,
541 s
->is_listen
? ",server" : "");
548 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
549 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
550 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
551 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
552 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
553 qemu_chr_socket_protocol(s
),
554 left
, shost
, right
, sserv
,
555 s
->is_listen
? ",server" : "",
556 left
, phost
, right
, pserv
);
559 return g_strdup_printf("unknown");
563 static void update_ioc_handlers(SocketChardev
*s
)
565 Chardev
*chr
= CHARDEV(s
);
571 remove_fd_in_watch(chr
);
572 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
577 remove_hup_source(s
);
578 s
->hup_source
= qio_channel_create_watch(s
->ioc
, G_IO_HUP
);
579 g_source_set_callback(s
->hup_source
, (GSourceFunc
)tcp_chr_hup
,
581 g_source_attach(s
->hup_source
, chr
->gcontext
);
584 static void tcp_chr_connect(void *opaque
)
586 Chardev
*chr
= CHARDEV(opaque
);
587 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
589 g_free(chr
->filename
);
590 chr
->filename
= qemu_chr_compute_filename(s
);
593 update_ioc_handlers(s
);
594 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
597 static void tcp_chr_telnet_destroy(SocketChardev
*s
)
599 if (s
->telnet_source
) {
600 g_source_destroy(s
->telnet_source
);
601 g_source_unref(s
->telnet_source
);
602 s
->telnet_source
= NULL
;
606 static void tcp_chr_update_read_handler(Chardev
*chr
)
608 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
612 * It's possible that chardev context is changed in
613 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
614 * listener if there is.
616 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
617 chr
, NULL
, chr
->gcontext
);
620 if (s
->telnet_source
) {
621 tcp_chr_telnet_init(CHARDEV(s
));
624 update_ioc_handlers(s
);
627 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
628 GIOCondition cond G_GNUC_UNUSED
,
631 SocketChardev
*s
= user_data
;
632 Chardev
*chr
= CHARDEV(s
);
633 TCPChardevTelnetInit
*init
= s
->telnet_init
;
638 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
640 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
643 tcp_chr_disconnect(chr
);
649 if (init
->buflen
== 0) {
650 tcp_chr_connect(chr
);
654 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
656 return G_SOURCE_CONTINUE
;
659 g_free(s
->telnet_init
);
660 s
->telnet_init
= NULL
;
661 g_source_unref(s
->telnet_source
);
662 s
->telnet_source
= NULL
;
663 return G_SOURCE_REMOVE
;
666 static void tcp_chr_telnet_init(Chardev
*chr
)
668 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
669 TCPChardevTelnetInit
*init
;
672 /* Destroy existing task */
673 tcp_chr_telnet_destroy(s
);
675 if (s
->telnet_init
) {
676 /* We are possibly during a handshake already */
680 s
->telnet_init
= g_new0(TCPChardevTelnetInit
, 1);
681 init
= s
->telnet_init
;
683 #define IACSET(x, a, b, c) \
692 /* Prep the telnet negotion to put telnet in binary,
693 * no echo, single char mode */
694 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
695 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
696 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
697 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
700 /* Prep the TN3270 negotion based on RFC1576 */
701 IACSET(init
->buf
, 0xff, 0xfd, 0x19); /* IAC DO EOR */
702 IACSET(init
->buf
, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
703 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
704 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
705 IACSET(init
->buf
, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
706 IACSET(init
->buf
, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
707 IACSET(init
->buf
, 0x01, 0xff, 0xf0); /* SEND IAC SE */
713 s
->telnet_source
= qio_channel_add_watch_source(s
->ioc
, G_IO_OUT
,
714 tcp_chr_telnet_init_io
,
720 static void tcp_chr_websock_handshake(QIOTask
*task
, gpointer user_data
)
722 Chardev
*chr
= user_data
;
723 SocketChardev
*s
= user_data
;
725 if (qio_task_propagate_error(task
, NULL
)) {
726 tcp_chr_disconnect(chr
);
728 if (s
->do_telnetopt
) {
729 tcp_chr_telnet_init(chr
);
731 tcp_chr_connect(chr
);
737 static void tcp_chr_websock_init(Chardev
*chr
)
739 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
740 QIOChannelWebsock
*wioc
= NULL
;
743 wioc
= qio_channel_websock_new_server(s
->ioc
);
745 name
= g_strdup_printf("chardev-websocket-server-%s", chr
->label
);
746 qio_channel_set_name(QIO_CHANNEL(wioc
), name
);
748 object_unref(OBJECT(s
->ioc
));
749 s
->ioc
= QIO_CHANNEL(wioc
);
751 qio_channel_websock_handshake(wioc
, tcp_chr_websock_handshake
, chr
, NULL
);
755 static void tcp_chr_tls_handshake(QIOTask
*task
,
758 Chardev
*chr
= user_data
;
759 SocketChardev
*s
= user_data
;
761 if (qio_task_propagate_error(task
, NULL
)) {
762 tcp_chr_disconnect(chr
);
765 tcp_chr_websock_init(chr
);
766 } else if (s
->do_telnetopt
) {
767 tcp_chr_telnet_init(chr
);
769 tcp_chr_connect(chr
);
775 static void tcp_chr_tls_init(Chardev
*chr
)
777 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
783 tioc
= qio_channel_tls_new_server(
784 s
->ioc
, s
->tls_creds
,
785 NULL
, /* XXX Use an ACL */
788 tioc
= qio_channel_tls_new_client(
789 s
->ioc
, s
->tls_creds
,
790 s
->addr
->u
.inet
.host
,
795 tcp_chr_disconnect(chr
);
798 name
= g_strdup_printf("chardev-tls-%s-%s",
799 s
->is_listen
? "server" : "client",
801 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
803 object_unref(OBJECT(s
->ioc
));
804 s
->ioc
= QIO_CHANNEL(tioc
);
806 qio_channel_tls_handshake(tioc
,
807 tcp_chr_tls_handshake
,
814 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
815 QIOChannelSocket
*sioc
)
817 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
819 name
= g_strdup_printf("chardev-tcp-%s-%s",
820 s
->is_listen
? "server" : "client",
822 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
827 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
829 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
831 if (s
->ioc
!= NULL
) {
835 s
->ioc
= QIO_CHANNEL(sioc
);
836 object_ref(OBJECT(sioc
));
838 object_ref(OBJECT(sioc
));
840 qio_channel_set_blocking(s
->ioc
, false, NULL
);
843 qio_channel_set_delay(s
->ioc
, false);
846 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
847 NULL
, chr
->gcontext
);
851 tcp_chr_tls_init(chr
);
852 } else if (s
->is_websock
) {
853 tcp_chr_websock_init(chr
);
854 } else if (s
->do_telnetopt
) {
855 tcp_chr_telnet_init(chr
);
857 tcp_chr_connect(chr
);
864 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
867 QIOChannelSocket
*sioc
;
869 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
873 tcp_chr_set_client_ioc_name(chr
, sioc
);
874 ret
= tcp_chr_new_client(chr
, sioc
);
875 object_unref(OBJECT(sioc
));
879 static void tcp_chr_accept(QIONetListener
*listener
,
880 QIOChannelSocket
*cioc
,
883 Chardev
*chr
= CHARDEV(opaque
);
885 tcp_chr_set_client_ioc_name(chr
, cioc
);
886 tcp_chr_new_client(chr
, cioc
);
889 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
891 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
892 QIOChannelSocket
*sioc
;
894 /* It can't wait on s->connected, since it is set asynchronously
895 * in TLS and telnet cases, only wait for an accepted socket */
898 info_report("QEMU waiting for connection on: %s",
900 sioc
= qio_net_listener_wait_client(s
->listener
);
901 tcp_chr_set_client_ioc_name(chr
, sioc
);
902 tcp_chr_new_client(chr
, sioc
);
903 object_unref(OBJECT(sioc
));
905 sioc
= qio_channel_socket_new();
906 tcp_chr_set_client_ioc_name(chr
, sioc
);
907 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
908 object_unref(OBJECT(sioc
));
911 tcp_chr_new_client(chr
, sioc
);
912 object_unref(OBJECT(sioc
));
919 static void char_socket_finalize(Object
*obj
)
921 Chardev
*chr
= CHARDEV(obj
);
922 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
924 tcp_chr_free_connection(chr
);
925 tcp_chr_reconn_timer_cancel(s
);
926 qapi_free_SocketAddress(s
->addr
);
927 tcp_chr_telnet_destroy(s
);
928 g_free(s
->telnet_init
);
930 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
931 NULL
, chr
->gcontext
);
932 object_unref(OBJECT(s
->listener
));
935 object_unref(OBJECT(s
->tls_creds
));
938 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
941 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
943 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
944 Chardev
*chr
= CHARDEV(opaque
);
945 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
948 if (qio_task_propagate_error(task
, &err
)) {
949 check_report_connect_error(chr
, err
);
954 s
->connect_err_reported
= false;
955 tcp_chr_new_client(chr
, sioc
);
958 object_unref(OBJECT(sioc
));
961 static void tcp_chr_connect_async(Chardev
*chr
)
963 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
964 QIOChannelSocket
*sioc
;
966 sioc
= qio_channel_socket_new();
967 tcp_chr_set_client_ioc_name(chr
, sioc
);
968 qio_channel_socket_connect_async(sioc
, s
->addr
,
969 qemu_chr_socket_connected
,
970 chr
, NULL
, chr
->gcontext
);
973 static gboolean
socket_reconnect_timeout(gpointer opaque
)
975 Chardev
*chr
= CHARDEV(opaque
);
976 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
978 g_source_unref(s
->reconnect_timer
);
979 s
->reconnect_timer
= NULL
;
985 tcp_chr_connect_async(chr
);
991 static bool qmp_chardev_validate_socket(ChardevSocket
*sock
,
995 /* Validate any options which have a dependency on address type */
996 switch (addr
->type
) {
997 case SOCKET_ADDRESS_TYPE_FD
:
998 if (sock
->has_reconnect
) {
1000 "'reconnect' option is incompatible with "
1001 "'fd' address type");
1004 if (sock
->has_tls_creds
&&
1005 !(sock
->has_server
&& sock
->server
)) {
1007 "'tls_creds' option is incompatible with "
1008 "'fd' address type as client");
1013 case SOCKET_ADDRESS_TYPE_UNIX
:
1014 if (sock
->has_tls_creds
) {
1016 "'tls_creds' option is incompatible with "
1017 "'unix' address type");
1022 case SOCKET_ADDRESS_TYPE_INET
:
1025 case SOCKET_ADDRESS_TYPE_VSOCK
:
1026 if (sock
->has_tls_creds
) {
1028 "'tls_creds' option is incompatible with "
1029 "'vsock' address type");
1037 /* Validate any options which have a dependancy on client vs server */
1038 if (!sock
->has_server
|| sock
->server
) {
1039 if (sock
->has_reconnect
) {
1041 "'reconnect' option is incompatible with "
1042 "socket in server listen mode");
1046 if (sock
->has_websocket
&& sock
->websocket
) {
1047 error_setg(errp
, "%s", "Websocket client is not implemented");
1050 if (sock
->has_wait
) {
1051 error_setg(errp
, "%s",
1052 "'wait' option is incompatible with "
1053 "socket in client connect mode");
1062 static void qmp_chardev_open_socket(Chardev
*chr
,
1063 ChardevBackend
*backend
,
1067 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1068 ChardevSocket
*sock
= backend
->u
.socket
.data
;
1069 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
1070 bool is_listen
= sock
->has_server
? sock
->server
: true;
1071 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
1072 bool is_tn3270
= sock
->has_tn3270
? sock
->tn3270
: false;
1073 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
1074 bool is_websock
= sock
->has_websocket
? sock
->websocket
: false;
1075 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
1076 QIOChannelSocket
*sioc
= NULL
;
1077 SocketAddress
*addr
;
1079 s
->is_listen
= is_listen
;
1080 s
->is_telnet
= is_telnet
;
1081 s
->is_tn3270
= is_tn3270
;
1082 s
->is_websock
= is_websock
;
1083 s
->do_nodelay
= do_nodelay
;
1084 if (sock
->tls_creds
) {
1086 creds
= object_resolve_path_component(
1087 object_get_objects_root(), sock
->tls_creds
);
1089 error_setg(errp
, "No TLS credentials with id '%s'",
1093 s
->tls_creds
= (QCryptoTLSCreds
*)
1094 object_dynamic_cast(creds
,
1095 TYPE_QCRYPTO_TLS_CREDS
);
1096 if (!s
->tls_creds
) {
1097 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1101 object_ref(OBJECT(s
->tls_creds
));
1103 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
1104 error_setg(errp
, "%s",
1105 "Expected TLS credentials for server endpoint");
1109 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
1110 error_setg(errp
, "%s",
1111 "Expected TLS credentials for client endpoint");
1117 s
->addr
= addr
= socket_address_flatten(sock
->addr
);
1119 if (!qmp_chardev_validate_socket(sock
, addr
, errp
)) {
1123 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
1124 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1125 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
1126 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
1129 /* be isn't opened until we get a connection */
1132 update_disconnected_filename(s
);
1135 if (is_telnet
|| is_tn3270
) {
1136 s
->do_telnetopt
= 1;
1138 } else if (reconnect
> 0) {
1139 s
->reconnect_time
= reconnect
;
1142 if (s
->reconnect_time
) {
1143 tcp_chr_connect_async(chr
);
1147 s
->listener
= qio_net_listener_new();
1149 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
1150 qio_net_listener_set_name(s
->listener
, name
);
1153 if (qio_net_listener_open_sync(s
->listener
, s
->addr
, errp
) < 0) {
1154 object_unref(OBJECT(s
->listener
));
1159 qapi_free_SocketAddress(s
->addr
);
1160 s
->addr
= socket_local_address(s
->listener
->sioc
[0]->fd
, errp
);
1161 update_disconnected_filename(s
);
1163 if (is_waitconnect
&&
1164 qemu_chr_wait_connected(chr
, errp
) < 0) {
1168 qio_net_listener_set_client_func_full(s
->listener
,
1173 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
1182 object_unref(OBJECT(sioc
));
1186 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
1189 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
1190 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
1191 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
1192 bool is_tn3270
= qemu_opt_get_bool(opts
, "tn3270", false);
1193 bool is_websock
= qemu_opt_get_bool(opts
, "websocket", false);
1194 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
1195 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
1196 const char *path
= qemu_opt_get(opts
, "path");
1197 const char *host
= qemu_opt_get(opts
, "host");
1198 const char *port
= qemu_opt_get(opts
, "port");
1199 const char *fd
= qemu_opt_get(opts
, "fd");
1200 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
1201 SocketAddressLegacy
*addr
;
1202 ChardevSocket
*sock
;
1204 if ((!!path
+ !!fd
+ !!host
) != 1) {
1206 "Exactly one of 'path', 'fd' or 'host' required");
1210 if (host
&& !port
) {
1211 error_setg(errp
, "chardev: socket: no port given");
1215 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
1216 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
1217 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
1219 sock
->has_nodelay
= true;
1220 sock
->nodelay
= do_nodelay
;
1221 sock
->has_server
= true;
1222 sock
->server
= is_listen
;
1223 sock
->has_telnet
= true;
1224 sock
->telnet
= is_telnet
;
1225 sock
->has_tn3270
= true;
1226 sock
->tn3270
= is_tn3270
;
1227 sock
->has_websocket
= true;
1228 sock
->websocket
= is_websock
;
1230 * We have different default to QMP for 'wait' when 'server'
1231 * is set, hence we can't just check for existence of 'wait'
1233 sock
->has_wait
= qemu_opt_find(opts
, "wait") || is_listen
;
1234 sock
->wait
= is_waitconnect
;
1235 sock
->has_reconnect
= qemu_opt_find(opts
, "reconnect");
1236 sock
->reconnect
= reconnect
;
1237 sock
->has_tls_creds
= tls_creds
;
1238 sock
->tls_creds
= g_strdup(tls_creds
);
1240 addr
= g_new0(SocketAddressLegacy
, 1);
1242 UnixSocketAddress
*q_unix
;
1243 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_UNIX
;
1244 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1245 q_unix
->path
= g_strdup(path
);
1247 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_INET
;
1248 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
1249 *addr
->u
.inet
.data
= (InetSocketAddress
) {
1250 .host
= g_strdup(host
),
1251 .port
= g_strdup(port
),
1252 .has_to
= qemu_opt_get(opts
, "to"),
1253 .to
= qemu_opt_get_number(opts
, "to", 0),
1254 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
1255 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
1256 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
1257 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
1260 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_FD
;
1261 addr
->u
.fd
.data
= g_new(String
, 1);
1262 addr
->u
.fd
.data
->str
= g_strdup(fd
);
1264 g_assert_not_reached();
1270 char_socket_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1271 void *opaque
, Error
**errp
)
1273 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1275 visit_type_SocketAddress(v
, name
, &s
->addr
, errp
);
1279 char_socket_get_connected(Object
*obj
, Error
**errp
)
1281 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1283 return s
->connected
;
1286 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
1288 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1290 cc
->parse
= qemu_chr_parse_socket
;
1291 cc
->open
= qmp_chardev_open_socket
;
1292 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
1293 cc
->chr_write
= tcp_chr_write
;
1294 cc
->chr_sync_read
= tcp_chr_sync_read
;
1295 cc
->chr_disconnect
= tcp_chr_disconnect
;
1296 cc
->get_msgfds
= tcp_get_msgfds
;
1297 cc
->set_msgfds
= tcp_set_msgfds
;
1298 cc
->chr_add_client
= tcp_chr_add_client
;
1299 cc
->chr_add_watch
= tcp_chr_add_watch
;
1300 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1302 object_class_property_add(oc
, "addr", "SocketAddress",
1303 char_socket_get_addr
, NULL
,
1304 NULL
, NULL
, &error_abort
);
1306 object_class_property_add_bool(oc
, "connected", char_socket_get_connected
,
1307 NULL
, &error_abort
);
1310 static const TypeInfo char_socket_type_info
= {
1311 .name
= TYPE_CHARDEV_SOCKET
,
1312 .parent
= TYPE_CHARDEV
,
1313 .instance_size
= sizeof(SocketChardev
),
1314 .instance_finalize
= char_socket_finalize
,
1315 .class_init
= char_socket_class_init
,
1318 static void register_types(void)
1320 type_register_static(&char_socket_type_info
);
1323 type_init(register_types
);