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/net-listener.h"
30 #include "qemu/error-report.h"
31 #include "qemu/option.h"
32 #include "qapi/error.h"
33 #include "qapi/clone-visitor.h"
34 #include "qapi/qapi-visit-sockets.h"
36 #include "chardev/char-io.h"
38 /***********************************************************/
41 #define TCP_MAX_FDS 16
46 } TCPChardevTelnetInit
;
50 QIOChannel
*ioc
; /* Client I/O channel */
51 QIOChannelSocket
*sioc
; /* Client master channel */
52 QIONetListener
*listener
;
54 QCryptoTLSCreds
*tls_creds
;
60 size_t read_msgfds_num
;
62 size_t write_msgfds_num
;
68 GSource
*telnet_source
;
69 TCPChardevTelnetInit
*telnet_init
;
71 GSource
*reconnect_timer
;
72 int64_t reconnect_time
;
73 bool connect_err_reported
;
76 #define SOCKET_CHARDEV(obj) \
77 OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
79 static gboolean
socket_reconnect_timeout(gpointer opaque
);
80 static void tcp_chr_telnet_init(Chardev
*chr
);
82 static void tcp_chr_reconn_timer_cancel(SocketChardev
*s
)
84 if (s
->reconnect_timer
) {
85 g_source_destroy(s
->reconnect_timer
);
86 g_source_unref(s
->reconnect_timer
);
87 s
->reconnect_timer
= NULL
;
91 static void qemu_chr_socket_restart_timer(Chardev
*chr
)
93 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
96 assert(s
->connected
== 0);
97 name
= g_strdup_printf("chardev-socket-reconnect-%s", chr
->label
);
98 s
->reconnect_timer
= qemu_chr_timeout_add_ms(chr
,
99 s
->reconnect_time
* 1000,
100 socket_reconnect_timeout
,
102 g_source_set_name(s
->reconnect_timer
, name
);
106 static void check_report_connect_error(Chardev
*chr
,
109 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
111 if (!s
->connect_err_reported
) {
112 error_report("Unable to connect character device %s: %s",
113 chr
->label
, error_get_pretty(err
));
114 s
->connect_err_reported
= true;
116 qemu_chr_socket_restart_timer(chr
);
119 static void tcp_chr_accept(QIONetListener
*listener
,
120 QIOChannelSocket
*cioc
,
123 static int tcp_chr_read_poll(void *opaque
);
124 static void tcp_chr_disconnect(Chardev
*chr
);
126 /* Called with chr_write_lock held. */
127 static int tcp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
129 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
132 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
134 s
->write_msgfds_num
);
136 /* free the written msgfds in any cases
137 * other than ret < 0 && errno == EAGAIN
139 if (!(ret
< 0 && EAGAIN
== errno
)
140 && s
->write_msgfds_num
) {
141 g_free(s
->write_msgfds
);
143 s
->write_msgfds_num
= 0;
146 if (ret
< 0 && errno
!= EAGAIN
) {
147 if (tcp_chr_read_poll(chr
) <= 0) {
148 tcp_chr_disconnect(chr
);
150 } /* else let the read handler finish it properly */
155 /* XXX: indicate an error ? */
160 static int tcp_chr_read_poll(void *opaque
)
162 Chardev
*chr
= CHARDEV(opaque
);
163 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
167 s
->max_size
= qemu_chr_be_can_write(chr
);
171 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
173 uint8_t *buf
, int *size
)
175 /* Handle any telnet or tn3270 client's basic IAC options.
176 * For telnet options, it satisfies char by char mode with no echo.
177 * For tn3270 options, it satisfies binary mode with EOR.
178 * All IAC options will be removed from the buf and the do_opt
179 * pointer will be used to track the state of the width of the
182 * RFC854: "All TELNET commands consist of at least a two byte sequence.
183 * The commands dealing with option negotiation are three byte sequences,
184 * the third byte being the code for the option referenced."
185 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
186 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
188 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
189 * session, and NOP and IP need to be done later.
195 for (i
= 0; i
< *size
; i
++) {
196 if (s
->do_telnetopt
> 1) {
197 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
198 /* Double IAC means send an IAC */
205 if ((unsigned char)buf
[i
] == IAC_BREAK
206 && s
->do_telnetopt
== 2) {
207 /* Handle IAC break commands by sending a serial break */
208 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
210 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_EOR
211 || (unsigned char)buf
[i
] == IAC_SB
212 || (unsigned char)buf
[i
] == IAC_SE
)
213 && s
->do_telnetopt
== 2) {
217 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_IP
218 || (unsigned char)buf
[i
] == IAC_NOP
)
219 && s
->do_telnetopt
== 2) {
220 /* TODO: IP and NOP need to be implemented later. */
225 if (s
->do_telnetopt
>= 4) {
229 if ((unsigned char)buf
[i
] == IAC
) {
242 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
244 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
246 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
248 assert(num
<= TCP_MAX_FDS
);
253 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
255 /* Close unused fds */
256 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
257 close(s
->read_msgfds
[i
]);
260 g_free(s
->read_msgfds
);
262 s
->read_msgfds_num
= 0;
268 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
270 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
272 /* clear old pending fd array */
273 g_free(s
->write_msgfds
);
274 s
->write_msgfds
= NULL
;
275 s
->write_msgfds_num
= 0;
278 !qio_channel_has_feature(s
->ioc
,
279 QIO_CHANNEL_FEATURE_FD_PASS
)) {
284 s
->write_msgfds
= g_new(int, num
);
285 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
288 s
->write_msgfds_num
= num
;
293 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
295 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
296 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
300 size_t msgfds_num
= 0;
302 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
303 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
304 &msgfds
, &msgfds_num
,
307 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
312 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
315 } else if (ret
== -1) {
320 /* close and clean read_msgfds */
321 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
322 close(s
->read_msgfds
[i
]);
325 if (s
->read_msgfds_num
) {
326 g_free(s
->read_msgfds
);
329 s
->read_msgfds
= msgfds
;
330 s
->read_msgfds_num
= msgfds_num
;
333 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
334 int fd
= s
->read_msgfds
[i
];
339 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
342 #ifndef MSG_CMSG_CLOEXEC
343 qemu_set_cloexec(fd
);
350 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
352 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
353 return qio_channel_create_watch(s
->ioc
, cond
);
356 static void remove_hup_source(SocketChardev
*s
)
358 if (s
->hup_source
!= NULL
) {
359 g_source_destroy(s
->hup_source
);
360 g_source_unref(s
->hup_source
);
361 s
->hup_source
= NULL
;
365 static void tcp_chr_free_connection(Chardev
*chr
)
367 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
370 if (s
->read_msgfds_num
) {
371 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
372 close(s
->read_msgfds
[i
]);
374 g_free(s
->read_msgfds
);
375 s
->read_msgfds
= NULL
;
376 s
->read_msgfds_num
= 0;
379 remove_hup_source(s
);
381 tcp_set_msgfds(chr
, NULL
, 0);
382 remove_fd_in_watch(chr
);
383 object_unref(OBJECT(s
->sioc
));
385 object_unref(OBJECT(s
->ioc
));
387 g_free(chr
->filename
);
388 chr
->filename
= NULL
;
392 static char *SocketAddress_to_str(const char *prefix
, SocketAddress
*addr
,
393 bool is_listen
, bool is_telnet
)
395 switch (addr
->type
) {
396 case SOCKET_ADDRESS_TYPE_INET
:
397 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
398 is_telnet
? "telnet" : "tcp",
401 is_listen
? ",server" : "");
403 case SOCKET_ADDRESS_TYPE_UNIX
:
404 return g_strdup_printf("%sunix:%s%s", prefix
,
406 is_listen
? ",server" : "");
408 case SOCKET_ADDRESS_TYPE_FD
:
409 return g_strdup_printf("%sfd:%s%s", prefix
, addr
->u
.fd
.str
,
410 is_listen
? ",server" : "");
412 case SOCKET_ADDRESS_TYPE_VSOCK
:
413 return g_strdup_printf("%svsock:%s:%s", prefix
,
421 static void update_disconnected_filename(SocketChardev
*s
)
423 Chardev
*chr
= CHARDEV(s
);
425 g_free(chr
->filename
);
427 chr
->filename
= SocketAddress_to_str("disconnected:", s
->addr
,
428 s
->is_listen
, s
->is_telnet
);
430 chr
->filename
= g_strdup("disconnected:socket");
434 /* NB may be called even if tcp_chr_connect has not been
435 * reached, due to TLS or telnet initialization failure,
436 * so can *not* assume s->connected == true
438 static void tcp_chr_disconnect(Chardev
*chr
)
440 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
441 bool emit_close
= s
->connected
;
443 tcp_chr_free_connection(chr
);
446 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
447 chr
, NULL
, chr
->gcontext
);
449 update_disconnected_filename(s
);
451 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
453 if (s
->reconnect_time
) {
454 qemu_chr_socket_restart_timer(chr
);
458 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
460 Chardev
*chr
= CHARDEV(opaque
);
461 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
462 uint8_t buf
[CHR_READ_BUF_LEN
];
465 if (!s
->connected
|| s
->max_size
<= 0) {
469 if (len
> s
->max_size
) {
472 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
473 if (size
== 0 || (size
== -1 && errno
!= EAGAIN
)) {
474 /* connection closed */
475 tcp_chr_disconnect(chr
);
476 } else if (size
> 0) {
477 if (s
->do_telnetopt
) {
478 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
481 qemu_chr_be_write(chr
, buf
, size
);
488 static gboolean
tcp_chr_hup(QIOChannel
*channel
,
492 Chardev
*chr
= CHARDEV(opaque
);
493 tcp_chr_disconnect(chr
);
494 return G_SOURCE_REMOVE
;
497 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
499 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
506 qio_channel_set_blocking(s
->ioc
, true, NULL
);
507 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
508 qio_channel_set_blocking(s
->ioc
, false, NULL
);
510 /* connection closed */
511 tcp_chr_disconnect(chr
);
517 static char *sockaddr_to_str(struct sockaddr_storage
*ss
, socklen_t ss_len
,
518 struct sockaddr_storage
*ps
, socklen_t ps_len
,
519 bool is_listen
, bool is_telnet
)
521 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
522 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
523 const char *left
= "", *right
= "";
525 switch (ss
->ss_family
) {
528 return g_strdup_printf("unix:%s%s",
529 ((struct sockaddr_un
*)(ss
))->sun_path
,
530 is_listen
? ",server" : "");
537 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
538 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
539 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
540 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
541 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
542 is_telnet
? "telnet" : "tcp",
543 left
, shost
, right
, sserv
,
544 is_listen
? ",server" : "",
545 left
, phost
, right
, pserv
);
548 return g_strdup_printf("unknown");
552 static void update_ioc_handlers(SocketChardev
*s
)
554 Chardev
*chr
= CHARDEV(s
);
560 remove_fd_in_watch(chr
);
561 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
566 remove_hup_source(s
);
567 s
->hup_source
= qio_channel_create_watch(s
->ioc
, G_IO_HUP
);
568 g_source_set_callback(s
->hup_source
, (GSourceFunc
)tcp_chr_hup
,
570 g_source_attach(s
->hup_source
, chr
->gcontext
);
573 static void tcp_chr_connect(void *opaque
)
575 Chardev
*chr
= CHARDEV(opaque
);
576 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
578 g_free(chr
->filename
);
579 chr
->filename
= sockaddr_to_str(
580 &s
->sioc
->localAddr
, s
->sioc
->localAddrLen
,
581 &s
->sioc
->remoteAddr
, s
->sioc
->remoteAddrLen
,
582 s
->is_listen
, s
->is_telnet
);
585 update_ioc_handlers(s
);
586 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
589 static void tcp_chr_telnet_destroy(SocketChardev
*s
)
591 if (s
->telnet_source
) {
592 g_source_destroy(s
->telnet_source
);
593 g_source_unref(s
->telnet_source
);
594 s
->telnet_source
= NULL
;
598 static void tcp_chr_update_read_handler(Chardev
*chr
)
600 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
604 * It's possible that chardev context is changed in
605 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
606 * listener if there is.
608 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
609 chr
, NULL
, chr
->gcontext
);
612 if (s
->telnet_source
) {
613 tcp_chr_telnet_init(CHARDEV(s
));
616 update_ioc_handlers(s
);
619 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
620 GIOCondition cond G_GNUC_UNUSED
,
623 SocketChardev
*s
= user_data
;
624 Chardev
*chr
= CHARDEV(s
);
625 TCPChardevTelnetInit
*init
= s
->telnet_init
;
630 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
632 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
635 tcp_chr_disconnect(chr
);
641 if (init
->buflen
== 0) {
642 tcp_chr_connect(chr
);
646 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
648 return G_SOURCE_CONTINUE
;
651 g_free(s
->telnet_init
);
652 s
->telnet_init
= NULL
;
653 g_source_unref(s
->telnet_source
);
654 s
->telnet_source
= NULL
;
655 return G_SOURCE_REMOVE
;
658 static void tcp_chr_telnet_init(Chardev
*chr
)
660 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
661 TCPChardevTelnetInit
*init
;
664 /* Destroy existing task */
665 tcp_chr_telnet_destroy(s
);
667 if (s
->telnet_init
) {
668 /* We are possibly during a handshake already */
672 s
->telnet_init
= g_new0(TCPChardevTelnetInit
, 1);
673 init
= s
->telnet_init
;
675 #define IACSET(x, a, b, c) \
684 /* Prep the telnet negotion to put telnet in binary,
685 * no echo, single char mode */
686 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
687 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
688 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
689 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
692 /* Prep the TN3270 negotion based on RFC1576 */
693 IACSET(init
->buf
, 0xff, 0xfd, 0x19); /* IAC DO EOR */
694 IACSET(init
->buf
, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
695 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
696 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
697 IACSET(init
->buf
, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
698 IACSET(init
->buf
, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
699 IACSET(init
->buf
, 0x01, 0xff, 0xf0); /* SEND IAC SE */
705 s
->telnet_source
= qio_channel_add_watch_source(s
->ioc
, G_IO_OUT
,
706 tcp_chr_telnet_init_io
,
712 static void tcp_chr_tls_handshake(QIOTask
*task
,
715 Chardev
*chr
= user_data
;
716 SocketChardev
*s
= user_data
;
718 if (qio_task_propagate_error(task
, NULL
)) {
719 tcp_chr_disconnect(chr
);
721 if (s
->do_telnetopt
) {
722 tcp_chr_telnet_init(chr
);
724 tcp_chr_connect(chr
);
730 static void tcp_chr_tls_init(Chardev
*chr
)
732 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
738 tioc
= qio_channel_tls_new_server(
739 s
->ioc
, s
->tls_creds
,
740 NULL
, /* XXX Use an ACL */
743 tioc
= qio_channel_tls_new_client(
744 s
->ioc
, s
->tls_creds
,
745 s
->addr
->u
.inet
.host
,
750 tcp_chr_disconnect(chr
);
753 name
= g_strdup_printf("chardev-tls-%s-%s",
754 s
->is_listen
? "server" : "client",
756 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
758 object_unref(OBJECT(s
->ioc
));
759 s
->ioc
= QIO_CHANNEL(tioc
);
761 qio_channel_tls_handshake(tioc
,
762 tcp_chr_tls_handshake
,
769 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
770 QIOChannelSocket
*sioc
)
772 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
774 name
= g_strdup_printf("chardev-tcp-%s-%s",
775 s
->is_listen
? "server" : "client",
777 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
782 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
784 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
786 if (s
->ioc
!= NULL
) {
790 s
->ioc
= QIO_CHANNEL(sioc
);
791 object_ref(OBJECT(sioc
));
793 object_ref(OBJECT(sioc
));
795 qio_channel_set_blocking(s
->ioc
, false, NULL
);
798 qio_channel_set_delay(s
->ioc
, false);
801 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
802 NULL
, chr
->gcontext
);
806 tcp_chr_tls_init(chr
);
808 if (s
->do_telnetopt
) {
809 tcp_chr_telnet_init(chr
);
811 tcp_chr_connect(chr
);
819 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
822 QIOChannelSocket
*sioc
;
824 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
828 tcp_chr_set_client_ioc_name(chr
, sioc
);
829 ret
= tcp_chr_new_client(chr
, sioc
);
830 object_unref(OBJECT(sioc
));
834 static void tcp_chr_accept(QIONetListener
*listener
,
835 QIOChannelSocket
*cioc
,
838 Chardev
*chr
= CHARDEV(opaque
);
840 tcp_chr_set_client_ioc_name(chr
, cioc
);
841 tcp_chr_new_client(chr
, cioc
);
844 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
846 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
847 QIOChannelSocket
*sioc
;
849 /* It can't wait on s->connected, since it is set asynchronously
850 * in TLS and telnet cases, only wait for an accepted socket */
853 info_report("QEMU waiting for connection on: %s",
855 sioc
= qio_net_listener_wait_client(s
->listener
);
856 tcp_chr_set_client_ioc_name(chr
, sioc
);
857 tcp_chr_new_client(chr
, sioc
);
858 object_unref(OBJECT(sioc
));
860 sioc
= qio_channel_socket_new();
861 tcp_chr_set_client_ioc_name(chr
, sioc
);
862 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
863 object_unref(OBJECT(sioc
));
866 tcp_chr_new_client(chr
, sioc
);
867 object_unref(OBJECT(sioc
));
874 static void char_socket_finalize(Object
*obj
)
876 Chardev
*chr
= CHARDEV(obj
);
877 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
879 tcp_chr_free_connection(chr
);
880 tcp_chr_reconn_timer_cancel(s
);
881 qapi_free_SocketAddress(s
->addr
);
882 tcp_chr_telnet_destroy(s
);
883 g_free(s
->telnet_init
);
885 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
886 NULL
, chr
->gcontext
);
887 object_unref(OBJECT(s
->listener
));
890 object_unref(OBJECT(s
->tls_creds
));
893 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
896 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
898 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
899 Chardev
*chr
= CHARDEV(opaque
);
900 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
903 if (qio_task_propagate_error(task
, &err
)) {
904 check_report_connect_error(chr
, err
);
909 s
->connect_err_reported
= false;
910 tcp_chr_new_client(chr
, sioc
);
913 object_unref(OBJECT(sioc
));
916 static void tcp_chr_connect_async(Chardev
*chr
)
918 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
919 QIOChannelSocket
*sioc
;
921 sioc
= qio_channel_socket_new();
922 tcp_chr_set_client_ioc_name(chr
, sioc
);
923 qio_channel_socket_connect_async(sioc
, s
->addr
,
924 qemu_chr_socket_connected
,
925 chr
, NULL
, chr
->gcontext
);
928 static gboolean
socket_reconnect_timeout(gpointer opaque
)
930 Chardev
*chr
= CHARDEV(opaque
);
931 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
933 g_source_unref(s
->reconnect_timer
);
934 s
->reconnect_timer
= NULL
;
940 tcp_chr_connect_async(chr
);
945 static void qmp_chardev_open_socket(Chardev
*chr
,
946 ChardevBackend
*backend
,
950 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
951 ChardevSocket
*sock
= backend
->u
.socket
.data
;
952 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
953 bool is_listen
= sock
->has_server
? sock
->server
: true;
954 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
955 bool is_tn3270
= sock
->has_tn3270
? sock
->tn3270
: false;
956 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
957 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
958 QIOChannelSocket
*sioc
= NULL
;
961 s
->is_listen
= is_listen
;
962 s
->is_telnet
= is_telnet
;
963 s
->is_tn3270
= is_tn3270
;
964 s
->do_nodelay
= do_nodelay
;
965 if (sock
->tls_creds
) {
967 creds
= object_resolve_path_component(
968 object_get_objects_root(), sock
->tls_creds
);
970 error_setg(errp
, "No TLS credentials with id '%s'",
974 s
->tls_creds
= (QCryptoTLSCreds
*)
975 object_dynamic_cast(creds
,
976 TYPE_QCRYPTO_TLS_CREDS
);
978 error_setg(errp
, "Object with id '%s' is not TLS credentials",
982 object_ref(OBJECT(s
->tls_creds
));
984 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
985 error_setg(errp
, "%s",
986 "Expected TLS credentials for server endpoint");
990 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
991 error_setg(errp
, "%s",
992 "Expected TLS credentials for client endpoint");
998 s
->addr
= addr
= socket_address_flatten(sock
->addr
);
1000 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
1001 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1002 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
1003 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
1006 /* be isn't opened until we get a connection */
1009 update_disconnected_filename(s
);
1012 if (is_telnet
|| is_tn3270
) {
1013 s
->do_telnetopt
= 1;
1015 } else if (reconnect
> 0) {
1016 s
->reconnect_time
= reconnect
;
1019 if (s
->reconnect_time
) {
1020 tcp_chr_connect_async(chr
);
1024 s
->listener
= qio_net_listener_new();
1026 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
1027 qio_net_listener_set_name(s
->listener
, name
);
1030 if (qio_net_listener_open_sync(s
->listener
, s
->addr
, errp
) < 0) {
1031 object_unref(OBJECT(s
->listener
));
1036 qapi_free_SocketAddress(s
->addr
);
1037 s
->addr
= socket_local_address(s
->listener
->sioc
[0]->fd
, errp
);
1038 update_disconnected_filename(s
);
1040 if (is_waitconnect
&&
1041 qemu_chr_wait_connected(chr
, errp
) < 0) {
1045 qio_net_listener_set_client_func_full(s
->listener
,
1050 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
1059 object_unref(OBJECT(sioc
));
1063 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
1066 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
1067 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
1068 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
1069 bool is_tn3270
= qemu_opt_get_bool(opts
, "tn3270", false);
1070 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
1071 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
1072 const char *path
= qemu_opt_get(opts
, "path");
1073 const char *host
= qemu_opt_get(opts
, "host");
1074 const char *port
= qemu_opt_get(opts
, "port");
1075 const char *fd
= qemu_opt_get(opts
, "fd");
1076 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
1077 SocketAddressLegacy
*addr
;
1078 ChardevSocket
*sock
;
1080 if ((!!path
+ !!fd
+ !!host
) != 1) {
1082 "Exactly one of 'path', 'fd' or 'host' required");
1086 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
1089 error_setg(errp
, "TLS can only be used over TCP socket");
1094 error_setg(errp
, "chardev: socket: no port given");
1098 /* We don't know what host to validate against when in client mode */
1099 if (tls_creds
&& !is_listen
) {
1100 error_setg(errp
, "TLS can not be used with pre-opened client FD");
1104 g_assert_not_reached();
1107 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
1108 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
1110 sock
->has_nodelay
= true;
1111 sock
->nodelay
= do_nodelay
;
1112 sock
->has_server
= true;
1113 sock
->server
= is_listen
;
1114 sock
->has_telnet
= true;
1115 sock
->telnet
= is_telnet
;
1116 sock
->has_tn3270
= true;
1117 sock
->tn3270
= is_tn3270
;
1118 sock
->has_wait
= true;
1119 sock
->wait
= is_waitconnect
;
1120 sock
->has_reconnect
= true;
1121 sock
->reconnect
= reconnect
;
1122 sock
->tls_creds
= g_strdup(tls_creds
);
1124 addr
= g_new0(SocketAddressLegacy
, 1);
1126 UnixSocketAddress
*q_unix
;
1127 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_UNIX
;
1128 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1129 q_unix
->path
= g_strdup(path
);
1131 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_INET
;
1132 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
1133 *addr
->u
.inet
.data
= (InetSocketAddress
) {
1134 .host
= g_strdup(host
),
1135 .port
= g_strdup(port
),
1136 .has_to
= qemu_opt_get(opts
, "to"),
1137 .to
= qemu_opt_get_number(opts
, "to", 0),
1138 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
1139 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
1140 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
1141 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
1144 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_FD
;
1145 addr
->u
.fd
.data
= g_new(String
, 1);
1146 addr
->u
.fd
.data
->str
= g_strdup(fd
);
1148 g_assert_not_reached();
1154 char_socket_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1155 void *opaque
, Error
**errp
)
1157 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1159 visit_type_SocketAddress(v
, name
, &s
->addr
, errp
);
1163 char_socket_get_connected(Object
*obj
, Error
**errp
)
1165 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1167 return s
->connected
;
1170 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
1172 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1174 cc
->parse
= qemu_chr_parse_socket
;
1175 cc
->open
= qmp_chardev_open_socket
;
1176 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
1177 cc
->chr_write
= tcp_chr_write
;
1178 cc
->chr_sync_read
= tcp_chr_sync_read
;
1179 cc
->chr_disconnect
= tcp_chr_disconnect
;
1180 cc
->get_msgfds
= tcp_get_msgfds
;
1181 cc
->set_msgfds
= tcp_set_msgfds
;
1182 cc
->chr_add_client
= tcp_chr_add_client
;
1183 cc
->chr_add_watch
= tcp_chr_add_watch
;
1184 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1186 object_class_property_add(oc
, "addr", "SocketAddress",
1187 char_socket_get_addr
, NULL
,
1188 NULL
, NULL
, &error_abort
);
1190 object_class_property_add_bool(oc
, "connected", char_socket_get_connected
,
1191 NULL
, &error_abort
);
1194 static const TypeInfo char_socket_type_info
= {
1195 .name
= TYPE_CHARDEV_SOCKET
,
1196 .parent
= TYPE_CHARDEV
,
1197 .instance_size
= sizeof(SocketChardev
),
1198 .instance_finalize
= char_socket_finalize
,
1199 .class_init
= char_socket_class_init
,
1202 static void register_types(void)
1204 type_register_static(&char_socket_type_info
);
1207 type_init(register_types
);