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 "chardev/char.h"
26 #include "io/channel-socket.h"
27 #include "io/channel-tls.h"
28 #include "io/net-listener.h"
29 #include "qemu/error-report.h"
30 #include "qapi/error.h"
31 #include "qapi/clone-visitor.h"
33 #include "chardev/char-io.h"
35 /***********************************************************/
38 #define TCP_MAX_FDS 16
42 QIOChannel
*ioc
; /* Client I/O channel */
43 QIOChannelSocket
*sioc
; /* Client master channel */
44 QIONetListener
*listener
;
45 QCryptoTLSCreds
*tls_creds
;
51 size_t read_msgfds_num
;
53 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 void tcp_chr_accept(QIONetListener
*listener
,
97 QIOChannelSocket
*cioc
,
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
);
145 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
147 uint8_t *buf
, int *size
)
149 /* Handle any telnet or tn3270 client's basic IAC options.
150 * For telnet options, it satisfies char by char mode with no echo.
151 * For tn3270 options, it satisfies binary mode with EOR.
152 * All IAC options will be removed from the buf and the do_opt
153 * pointer will be used to track the state of the width of the
156 * RFC854: "All TELNET commands consist of at least a two byte sequence.
157 * The commands dealing with option negotiation are three byte sequences,
158 * the third byte being the code for the option referenced."
159 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
160 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
162 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
163 * session, and NOP and IP need to be done later.
169 for (i
= 0; i
< *size
; i
++) {
170 if (s
->do_telnetopt
> 1) {
171 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
172 /* Double IAC means send an IAC */
179 if ((unsigned char)buf
[i
] == IAC_BREAK
180 && s
->do_telnetopt
== 2) {
181 /* Handle IAC break commands by sending a serial break */
182 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
184 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_EOR
185 || (unsigned char)buf
[i
] == IAC_SB
186 || (unsigned char)buf
[i
] == IAC_SE
)
187 && s
->do_telnetopt
== 2) {
191 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_IP
192 || (unsigned char)buf
[i
] == IAC_NOP
)
193 && s
->do_telnetopt
== 2) {
194 /* TODO: IP and NOP need to be implemented later. */
199 if (s
->do_telnetopt
>= 4) {
203 if ((unsigned char)buf
[i
] == IAC
) {
216 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
218 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
220 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
222 assert(num
<= TCP_MAX_FDS
);
227 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
229 /* Close unused fds */
230 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
231 close(s
->read_msgfds
[i
]);
234 g_free(s
->read_msgfds
);
236 s
->read_msgfds_num
= 0;
242 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
244 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
246 /* clear old pending fd array */
247 g_free(s
->write_msgfds
);
248 s
->write_msgfds
= NULL
;
249 s
->write_msgfds_num
= 0;
252 !qio_channel_has_feature(s
->ioc
,
253 QIO_CHANNEL_FEATURE_FD_PASS
)) {
258 s
->write_msgfds
= g_new(int, num
);
259 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
262 s
->write_msgfds_num
= num
;
267 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
269 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
270 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
274 size_t msgfds_num
= 0;
276 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
277 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
278 &msgfds
, &msgfds_num
,
281 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
286 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
289 } else if (ret
== -1) {
294 /* close and clean read_msgfds */
295 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
296 close(s
->read_msgfds
[i
]);
299 if (s
->read_msgfds_num
) {
300 g_free(s
->read_msgfds
);
303 s
->read_msgfds
= msgfds
;
304 s
->read_msgfds_num
= msgfds_num
;
307 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
308 int fd
= s
->read_msgfds
[i
];
313 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
316 #ifndef MSG_CMSG_CLOEXEC
317 qemu_set_cloexec(fd
);
324 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
326 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
327 return qio_channel_create_watch(s
->ioc
, cond
);
330 static void tcp_chr_free_connection(Chardev
*chr
)
332 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
335 if (s
->read_msgfds_num
) {
336 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
337 close(s
->read_msgfds
[i
]);
339 g_free(s
->read_msgfds
);
340 s
->read_msgfds
= NULL
;
341 s
->read_msgfds_num
= 0;
344 tcp_set_msgfds(chr
, NULL
, 0);
345 remove_fd_in_watch(chr
);
346 object_unref(OBJECT(s
->sioc
));
348 object_unref(OBJECT(s
->ioc
));
350 g_free(chr
->filename
);
351 chr
->filename
= NULL
;
355 static char *SocketAddress_to_str(const char *prefix
, SocketAddress
*addr
,
356 bool is_listen
, bool is_telnet
)
358 switch (addr
->type
) {
359 case SOCKET_ADDRESS_TYPE_INET
:
360 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
361 is_telnet
? "telnet" : "tcp",
364 is_listen
? ",server" : "");
366 case SOCKET_ADDRESS_TYPE_UNIX
:
367 return g_strdup_printf("%sunix:%s%s", prefix
,
369 is_listen
? ",server" : "");
371 case SOCKET_ADDRESS_TYPE_FD
:
372 return g_strdup_printf("%sfd:%s%s", prefix
, addr
->u
.fd
.str
,
373 is_listen
? ",server" : "");
375 case SOCKET_ADDRESS_TYPE_VSOCK
:
376 return g_strdup_printf("%svsock:%s:%s", prefix
,
384 static void update_disconnected_filename(SocketChardev
*s
)
386 Chardev
*chr
= CHARDEV(s
);
388 g_free(chr
->filename
);
389 chr
->filename
= SocketAddress_to_str("disconnected:", s
->addr
,
390 s
->is_listen
, s
->is_telnet
);
393 /* NB may be called even if tcp_chr_connect has not been
394 * reached, due to TLS or telnet initialization failure,
395 * so can *not* assume s->connected == true
397 static void tcp_chr_disconnect(Chardev
*chr
)
399 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
400 bool emit_close
= s
->connected
;
402 tcp_chr_free_connection(chr
);
405 qio_net_listener_set_client_func(s
->listener
, tcp_chr_accept
,
408 update_disconnected_filename(s
);
410 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
412 if (s
->reconnect_time
) {
413 qemu_chr_socket_restart_timer(chr
);
417 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
419 Chardev
*chr
= CHARDEV(opaque
);
420 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
421 uint8_t buf
[CHR_READ_BUF_LEN
];
424 if (!s
->connected
|| s
->max_size
<= 0) {
428 if (len
> s
->max_size
) {
431 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
432 if (size
== 0 || size
== -1) {
433 /* connection closed */
434 tcp_chr_disconnect(chr
);
435 } else if (size
> 0) {
436 if (s
->do_telnetopt
) {
437 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
440 qemu_chr_be_write(chr
, buf
, size
);
447 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
449 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
456 qio_channel_set_blocking(s
->ioc
, true, NULL
);
457 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
458 qio_channel_set_blocking(s
->ioc
, false, NULL
);
460 /* connection closed */
461 tcp_chr_disconnect(chr
);
467 static char *sockaddr_to_str(struct sockaddr_storage
*ss
, socklen_t ss_len
,
468 struct sockaddr_storage
*ps
, socklen_t ps_len
,
469 bool is_listen
, bool is_telnet
)
471 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
472 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
473 const char *left
= "", *right
= "";
475 switch (ss
->ss_family
) {
478 return g_strdup_printf("unix:%s%s",
479 ((struct sockaddr_un
*)(ss
))->sun_path
,
480 is_listen
? ",server" : "");
487 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
488 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
489 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
490 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
491 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
492 is_telnet
? "telnet" : "tcp",
493 left
, shost
, right
, sserv
,
494 is_listen
? ",server" : "",
495 left
, phost
, right
, pserv
);
498 return g_strdup_printf("unknown");
502 static void tcp_chr_connect(void *opaque
)
504 Chardev
*chr
= CHARDEV(opaque
);
505 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
507 g_free(chr
->filename
);
508 chr
->filename
= sockaddr_to_str(
509 &s
->sioc
->localAddr
, s
->sioc
->localAddrLen
,
510 &s
->sioc
->remoteAddr
, s
->sioc
->remoteAddrLen
,
511 s
->is_listen
, s
->is_telnet
);
515 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
520 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
523 static void tcp_chr_update_read_handler(Chardev
*chr
)
525 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
531 remove_fd_in_watch(chr
);
533 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
544 } TCPChardevTelnetInit
;
546 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
547 GIOCondition cond G_GNUC_UNUSED
,
550 TCPChardevTelnetInit
*init
= user_data
;
553 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
555 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
558 tcp_chr_disconnect(init
->chr
);
564 if (init
->buflen
== 0) {
565 tcp_chr_connect(init
->chr
);
569 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
574 static void tcp_chr_telnet_init(Chardev
*chr
)
576 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
577 TCPChardevTelnetInit
*init
= g_new0(TCPChardevTelnetInit
, 1);
580 #define IACSET(x, a, b, c) \
590 /* Prep the telnet negotion to put telnet in binary,
591 * no echo, single char mode */
592 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
593 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
594 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
595 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
598 /* Prep the TN3270 negotion based on RFC1576 */
599 IACSET(init
->buf
, 0xff, 0xfd, 0x19); /* IAC DO EOR */
600 IACSET(init
->buf
, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
601 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
602 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
603 IACSET(init
->buf
, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
604 IACSET(init
->buf
, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
605 IACSET(init
->buf
, 0x01, 0xff, 0xf0); /* SEND IAC SE */
610 qio_channel_add_watch(
612 tcp_chr_telnet_init_io
,
617 static void tcp_chr_tls_handshake(QIOTask
*task
,
620 Chardev
*chr
= user_data
;
621 SocketChardev
*s
= user_data
;
623 if (qio_task_propagate_error(task
, NULL
)) {
624 tcp_chr_disconnect(chr
);
626 /* tn3270 does not support TLS yet */
627 if (s
->do_telnetopt
&& !s
->is_tn3270
) {
628 tcp_chr_telnet_init(chr
);
630 tcp_chr_connect(chr
);
636 static void tcp_chr_tls_init(Chardev
*chr
)
638 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
644 tioc
= qio_channel_tls_new_server(
645 s
->ioc
, s
->tls_creds
,
646 NULL
, /* XXX Use an ACL */
649 tioc
= qio_channel_tls_new_client(
650 s
->ioc
, s
->tls_creds
,
651 s
->addr
->u
.inet
.host
,
656 tcp_chr_disconnect(chr
);
659 name
= g_strdup_printf("chardev-tls-%s-%s",
660 s
->is_listen
? "server" : "client",
662 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
664 object_unref(OBJECT(s
->ioc
));
665 s
->ioc
= QIO_CHANNEL(tioc
);
667 qio_channel_tls_handshake(tioc
,
668 tcp_chr_tls_handshake
,
674 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
675 QIOChannelSocket
*sioc
)
677 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
679 name
= g_strdup_printf("chardev-tcp-%s-%s",
680 s
->is_listen
? "server" : "client",
682 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
687 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
689 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
691 if (s
->ioc
!= NULL
) {
695 s
->ioc
= QIO_CHANNEL(sioc
);
696 object_ref(OBJECT(sioc
));
698 object_ref(OBJECT(sioc
));
700 qio_channel_set_blocking(s
->ioc
, false, NULL
);
703 qio_channel_set_delay(s
->ioc
, false);
706 qio_net_listener_set_client_func(s
->listener
, NULL
, NULL
, NULL
);
710 tcp_chr_tls_init(chr
);
712 if (s
->do_telnetopt
) {
713 tcp_chr_telnet_init(chr
);
715 tcp_chr_connect(chr
);
723 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
726 QIOChannelSocket
*sioc
;
728 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
732 tcp_chr_set_client_ioc_name(chr
, sioc
);
733 ret
= tcp_chr_new_client(chr
, sioc
);
734 object_unref(OBJECT(sioc
));
738 static void tcp_chr_accept(QIONetListener
*listener
,
739 QIOChannelSocket
*cioc
,
742 Chardev
*chr
= CHARDEV(opaque
);
744 tcp_chr_set_client_ioc_name(chr
, cioc
);
745 tcp_chr_new_client(chr
, cioc
);
748 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
750 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
751 QIOChannelSocket
*sioc
;
753 /* It can't wait on s->connected, since it is set asynchronously
754 * in TLS and telnet cases, only wait for an accepted socket */
757 info_report("QEMU waiting for connection on: %s",
759 sioc
= qio_net_listener_wait_client(s
->listener
);
760 tcp_chr_set_client_ioc_name(chr
, sioc
);
761 tcp_chr_new_client(chr
, sioc
);
762 object_unref(OBJECT(sioc
));
764 sioc
= qio_channel_socket_new();
765 tcp_chr_set_client_ioc_name(chr
, sioc
);
766 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
767 object_unref(OBJECT(sioc
));
770 tcp_chr_new_client(chr
, sioc
);
771 object_unref(OBJECT(sioc
));
778 static void char_socket_finalize(Object
*obj
)
780 Chardev
*chr
= CHARDEV(obj
);
781 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
783 tcp_chr_free_connection(chr
);
785 if (s
->reconnect_timer
) {
786 g_source_remove(s
->reconnect_timer
);
787 s
->reconnect_timer
= 0;
789 qapi_free_SocketAddress(s
->addr
);
791 qio_net_listener_set_client_func(s
->listener
, NULL
, NULL
, NULL
);
792 object_unref(OBJECT(s
->listener
));
795 object_unref(OBJECT(s
->tls_creds
));
798 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
801 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
803 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
804 Chardev
*chr
= CHARDEV(opaque
);
805 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
808 if (qio_task_propagate_error(task
, &err
)) {
809 check_report_connect_error(chr
, err
);
814 s
->connect_err_reported
= false;
815 tcp_chr_new_client(chr
, sioc
);
818 object_unref(OBJECT(sioc
));
821 static gboolean
socket_reconnect_timeout(gpointer opaque
)
823 Chardev
*chr
= CHARDEV(opaque
);
824 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
825 QIOChannelSocket
*sioc
;
827 s
->reconnect_timer
= 0;
833 sioc
= qio_channel_socket_new();
834 tcp_chr_set_client_ioc_name(chr
, sioc
);
835 qio_channel_socket_connect_async(sioc
, s
->addr
,
836 qemu_chr_socket_connected
,
842 static void qmp_chardev_open_socket(Chardev
*chr
,
843 ChardevBackend
*backend
,
847 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
848 ChardevSocket
*sock
= backend
->u
.socket
.data
;
849 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
850 bool is_listen
= sock
->has_server
? sock
->server
: true;
851 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
852 bool is_tn3270
= sock
->has_tn3270
? sock
->tn3270
: false;
853 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
854 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
855 QIOChannelSocket
*sioc
= NULL
;
858 s
->is_listen
= is_listen
;
859 s
->is_telnet
= is_telnet
;
860 s
->is_tn3270
= is_tn3270
;
861 s
->do_nodelay
= do_nodelay
;
862 if (sock
->tls_creds
) {
864 creds
= object_resolve_path_component(
865 object_get_objects_root(), sock
->tls_creds
);
867 error_setg(errp
, "No TLS credentials with id '%s'",
871 s
->tls_creds
= (QCryptoTLSCreds
*)
872 object_dynamic_cast(creds
,
873 TYPE_QCRYPTO_TLS_CREDS
);
875 error_setg(errp
, "Object with id '%s' is not TLS credentials",
879 object_ref(OBJECT(s
->tls_creds
));
881 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
882 error_setg(errp
, "%s",
883 "Expected TLS credentials for server endpoint");
887 if (s
->tls_creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
888 error_setg(errp
, "%s",
889 "Expected TLS credentials for client endpoint");
895 s
->addr
= addr
= socket_address_flatten(sock
->addr
);
897 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
898 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
899 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
900 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
903 /* be isn't opened until we get a connection */
906 update_disconnected_filename(s
);
909 if (is_telnet
|| is_tn3270
) {
912 } else if (reconnect
> 0) {
913 s
->reconnect_time
= reconnect
;
916 if (s
->reconnect_time
) {
917 sioc
= qio_channel_socket_new();
918 tcp_chr_set_client_ioc_name(chr
, sioc
);
919 qio_channel_socket_connect_async(sioc
, s
->addr
,
920 qemu_chr_socket_connected
,
925 s
->listener
= qio_net_listener_new();
927 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
928 qio_net_listener_set_name(s
->listener
, name
);
931 if (qio_net_listener_open_sync(s
->listener
, s
->addr
, errp
) < 0) {
932 object_unref(OBJECT(s
->listener
));
937 qapi_free_SocketAddress(s
->addr
);
938 s
->addr
= socket_local_address(s
->listener
->sioc
[0]->fd
, errp
);
939 update_disconnected_filename(s
);
941 if (is_waitconnect
&&
942 qemu_chr_wait_connected(chr
, errp
) < 0) {
946 qio_net_listener_set_client_func(s
->listener
, tcp_chr_accept
,
949 } else if (qemu_chr_wait_connected(chr
, errp
) < 0) {
958 object_unref(OBJECT(sioc
));
962 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
965 bool is_listen
= qemu_opt_get_bool(opts
, "server", false);
966 bool is_waitconnect
= is_listen
&& qemu_opt_get_bool(opts
, "wait", true);
967 bool is_telnet
= qemu_opt_get_bool(opts
, "telnet", false);
968 bool is_tn3270
= qemu_opt_get_bool(opts
, "tn3270", false);
969 bool do_nodelay
= !qemu_opt_get_bool(opts
, "delay", true);
970 int64_t reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
971 const char *path
= qemu_opt_get(opts
, "path");
972 const char *host
= qemu_opt_get(opts
, "host");
973 const char *port
= qemu_opt_get(opts
, "port");
974 const char *tls_creds
= qemu_opt_get(opts
, "tls-creds");
975 SocketAddressLegacy
*addr
;
978 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
981 error_setg(errp
, "chardev: socket: no host given");
985 error_setg(errp
, "chardev: socket: no port given");
990 error_setg(errp
, "TLS can only be used over TCP socket");
995 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
996 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
998 sock
->has_nodelay
= true;
999 sock
->nodelay
= do_nodelay
;
1000 sock
->has_server
= true;
1001 sock
->server
= is_listen
;
1002 sock
->has_telnet
= true;
1003 sock
->telnet
= is_telnet
;
1004 sock
->has_tn3270
= true;
1005 sock
->tn3270
= is_tn3270
;
1006 sock
->has_wait
= true;
1007 sock
->wait
= is_waitconnect
;
1008 sock
->has_reconnect
= true;
1009 sock
->reconnect
= reconnect
;
1010 sock
->tls_creds
= g_strdup(tls_creds
);
1012 addr
= g_new0(SocketAddressLegacy
, 1);
1014 UnixSocketAddress
*q_unix
;
1015 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_UNIX
;
1016 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1017 q_unix
->path
= g_strdup(path
);
1019 addr
->type
= SOCKET_ADDRESS_LEGACY_KIND_INET
;
1020 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
1021 *addr
->u
.inet
.data
= (InetSocketAddress
) {
1022 .host
= g_strdup(host
),
1023 .port
= g_strdup(port
),
1024 .has_to
= qemu_opt_get(opts
, "to"),
1025 .to
= qemu_opt_get_number(opts
, "to", 0),
1026 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
1027 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
1028 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
1029 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
1036 char_socket_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1037 void *opaque
, Error
**errp
)
1039 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1041 visit_type_SocketAddress(v
, name
, &s
->addr
, errp
);
1045 char_socket_get_connected(Object
*obj
, Error
**errp
)
1047 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1049 return s
->connected
;
1052 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
1054 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1056 cc
->parse
= qemu_chr_parse_socket
;
1057 cc
->open
= qmp_chardev_open_socket
;
1058 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
1059 cc
->chr_write
= tcp_chr_write
;
1060 cc
->chr_sync_read
= tcp_chr_sync_read
;
1061 cc
->chr_disconnect
= tcp_chr_disconnect
;
1062 cc
->get_msgfds
= tcp_get_msgfds
;
1063 cc
->set_msgfds
= tcp_set_msgfds
;
1064 cc
->chr_add_client
= tcp_chr_add_client
;
1065 cc
->chr_add_watch
= tcp_chr_add_watch
;
1066 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1068 object_class_property_add(oc
, "addr", "SocketAddress",
1069 char_socket_get_addr
, NULL
,
1070 NULL
, NULL
, &error_abort
);
1072 object_class_property_add_bool(oc
, "connected", char_socket_get_connected
,
1073 NULL
, &error_abort
);
1076 static const TypeInfo char_socket_type_info
= {
1077 .name
= TYPE_CHARDEV_SOCKET
,
1078 .parent
= TYPE_CHARDEV
,
1079 .instance_size
= sizeof(SocketChardev
),
1080 .instance_finalize
= char_socket_finalize
,
1081 .class_init
= char_socket_class_init
,
1084 static void register_types(void)
1086 type_register_static(&char_socket_type_info
);
1089 type_init(register_types
);