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-websock.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qapi/error.h"
33 #include "qapi/clone-visitor.h"
34 #include "qapi/qapi-visit-sockets.h"
35 #include "qemu/yank.h"
37 #include "chardev/char-io.h"
38 #include "chardev/char-socket.h"
40 static gboolean
socket_reconnect_timeout(gpointer opaque
);
41 static void tcp_chr_telnet_init(Chardev
*chr
);
43 static void tcp_chr_change_state(SocketChardev
*s
, TCPChardevState state
)
46 case TCP_CHARDEV_STATE_DISCONNECTED
:
48 case TCP_CHARDEV_STATE_CONNECTING
:
49 assert(s
->state
== TCP_CHARDEV_STATE_DISCONNECTED
);
51 case TCP_CHARDEV_STATE_CONNECTED
:
52 assert(s
->state
== TCP_CHARDEV_STATE_CONNECTING
);
58 static void tcp_chr_reconn_timer_cancel(SocketChardev
*s
)
60 if (s
->reconnect_timer
) {
61 g_source_destroy(s
->reconnect_timer
);
62 g_source_unref(s
->reconnect_timer
);
63 s
->reconnect_timer
= NULL
;
67 static void qemu_chr_socket_restart_timer(Chardev
*chr
)
69 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
72 assert(s
->state
== TCP_CHARDEV_STATE_DISCONNECTED
);
73 assert(!s
->reconnect_timer
);
74 name
= g_strdup_printf("chardev-socket-reconnect-%s", chr
->label
);
75 s
->reconnect_timer
= qemu_chr_timeout_add_ms(chr
,
76 s
->reconnect_time
* 1000,
77 socket_reconnect_timeout
,
79 g_source_set_name(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_reportf_err(err
,
90 "Unable to connect character device %s: ",
92 s
->connect_err_reported
= true;
96 qemu_chr_socket_restart_timer(chr
);
99 static void tcp_chr_accept(QIONetListener
*listener
,
100 QIOChannelSocket
*cioc
,
103 static int tcp_chr_read_poll(void *opaque
);
104 static void tcp_chr_disconnect_locked(Chardev
*chr
);
106 /* Called with chr_write_lock held. */
107 static int tcp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
109 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
111 if (s
->state
== TCP_CHARDEV_STATE_CONNECTED
) {
112 int ret
= io_channel_send_full(s
->ioc
, buf
, len
,
114 s
->write_msgfds_num
);
116 /* free the written msgfds in any cases
117 * other than ret < 0 && errno == EAGAIN
119 if (!(ret
< 0 && EAGAIN
== errno
)
120 && s
->write_msgfds_num
) {
121 g_free(s
->write_msgfds
);
123 s
->write_msgfds_num
= 0;
126 if (ret
< 0 && errno
!= EAGAIN
) {
127 if (tcp_chr_read_poll(chr
) <= 0) {
128 /* Perform disconnect and return error. */
129 tcp_chr_disconnect_locked(chr
);
130 } /* else let the read handler finish it properly */
135 /* Indicate an error. */
141 static int tcp_chr_read_poll(void *opaque
)
143 Chardev
*chr
= CHARDEV(opaque
);
144 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
145 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
148 s
->max_size
= qemu_chr_be_can_write(chr
);
152 static void tcp_chr_process_IAC_bytes(Chardev
*chr
,
154 uint8_t *buf
, int *size
)
156 /* Handle any telnet or tn3270 client's basic IAC options.
157 * For telnet options, it satisfies char by char mode with no echo.
158 * For tn3270 options, it satisfies binary mode with EOR.
159 * All IAC options will be removed from the buf and the do_opt
160 * pointer will be used to track the state of the width of the
163 * RFC854: "All TELNET commands consist of at least a two byte sequence.
164 * The commands dealing with option negotiation are three byte sequences,
165 * the third byte being the code for the option referenced."
166 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
167 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
169 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
170 * session, and NOP and IP need to be done later.
176 for (i
= 0; i
< *size
; i
++) {
177 if (s
->do_telnetopt
> 1) {
178 if ((unsigned char)buf
[i
] == IAC
&& s
->do_telnetopt
== 2) {
179 /* Double IAC means send an IAC */
186 if ((unsigned char)buf
[i
] == IAC_BREAK
187 && s
->do_telnetopt
== 2) {
188 /* Handle IAC break commands by sending a serial break */
189 qemu_chr_be_event(chr
, CHR_EVENT_BREAK
);
191 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_EOR
192 || (unsigned char)buf
[i
] == IAC_SB
193 || (unsigned char)buf
[i
] == IAC_SE
)
194 && s
->do_telnetopt
== 2) {
198 } else if (s
->is_tn3270
&& ((unsigned char)buf
[i
] == IAC_IP
199 || (unsigned char)buf
[i
] == IAC_NOP
)
200 && s
->do_telnetopt
== 2) {
201 /* TODO: IP and NOP need to be implemented later. */
206 if (s
->do_telnetopt
>= 4) {
210 if ((unsigned char)buf
[i
] == IAC
) {
223 static int tcp_get_msgfds(Chardev
*chr
, int *fds
, int num
)
225 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
227 int to_copy
= (s
->read_msgfds_num
< num
) ? s
->read_msgfds_num
: num
;
229 assert(num
<= TCP_MAX_FDS
);
234 memcpy(fds
, s
->read_msgfds
, to_copy
* sizeof(int));
236 /* Close unused fds */
237 for (i
= to_copy
; i
< s
->read_msgfds_num
; i
++) {
238 close(s
->read_msgfds
[i
]);
241 g_free(s
->read_msgfds
);
243 s
->read_msgfds_num
= 0;
249 static int tcp_set_msgfds(Chardev
*chr
, int *fds
, int num
)
251 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
253 /* clear old pending fd array */
254 g_free(s
->write_msgfds
);
255 s
->write_msgfds
= NULL
;
256 s
->write_msgfds_num
= 0;
258 if ((s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) ||
259 !qio_channel_has_feature(s
->ioc
,
260 QIO_CHANNEL_FEATURE_FD_PASS
)) {
265 s
->write_msgfds
= g_new(int, num
);
266 memcpy(s
->write_msgfds
, fds
, num
* sizeof(int));
269 s
->write_msgfds_num
= num
;
274 static ssize_t
tcp_chr_recv(Chardev
*chr
, char *buf
, size_t len
)
276 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
277 struct iovec iov
= { .iov_base
= buf
, .iov_len
= len
};
281 size_t msgfds_num
= 0;
283 if (qio_channel_has_feature(s
->ioc
, QIO_CHANNEL_FEATURE_FD_PASS
)) {
284 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 1,
285 &msgfds
, &msgfds_num
,
288 ret
= qio_channel_readv_full(s
->ioc
, &iov
, 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 */
314 qemu_socket_set_block(fd
);
316 #ifndef MSG_CMSG_CLOEXEC
317 qemu_set_cloexec(fd
);
321 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
324 } else if (ret
== -1) {
331 static GSource
*tcp_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
333 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
337 return qio_channel_create_watch(s
->ioc
, cond
);
340 static void remove_hup_source(SocketChardev
*s
)
342 if (s
->hup_source
!= NULL
) {
343 g_source_destroy(s
->hup_source
);
344 g_source_unref(s
->hup_source
);
345 s
->hup_source
= NULL
;
349 static void char_socket_yank_iochannel(void *opaque
)
351 QIOChannel
*ioc
= QIO_CHANNEL(opaque
);
353 qio_channel_shutdown(ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
356 static void tcp_chr_free_connection(Chardev
*chr
)
358 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
361 if (s
->read_msgfds_num
) {
362 for (i
= 0; i
< s
->read_msgfds_num
; i
++) {
363 close(s
->read_msgfds
[i
]);
365 g_free(s
->read_msgfds
);
366 s
->read_msgfds
= NULL
;
367 s
->read_msgfds_num
= 0;
370 remove_hup_source(s
);
372 tcp_set_msgfds(chr
, NULL
, 0);
373 remove_fd_in_watch(chr
);
374 if (s
->registered_yank
&&
375 (s
->state
== TCP_CHARDEV_STATE_CONNECTING
376 || s
->state
== TCP_CHARDEV_STATE_CONNECTED
)) {
377 yank_unregister_function(CHARDEV_YANK_INSTANCE(chr
->label
),
378 char_socket_yank_iochannel
,
379 QIO_CHANNEL(s
->sioc
));
381 object_unref(OBJECT(s
->sioc
));
383 object_unref(OBJECT(s
->ioc
));
385 g_free(chr
->filename
);
386 chr
->filename
= NULL
;
387 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_DISCONNECTED
);
390 static const char *qemu_chr_socket_protocol(SocketChardev
*s
)
395 return s
->is_websock
? "websocket" : "tcp";
398 static char *qemu_chr_socket_address(SocketChardev
*s
, const char *prefix
)
400 switch (s
->addr
->type
) {
401 case SOCKET_ADDRESS_TYPE_INET
:
402 return g_strdup_printf("%s%s:%s:%s%s", prefix
,
403 qemu_chr_socket_protocol(s
),
404 s
->addr
->u
.inet
.host
,
405 s
->addr
->u
.inet
.port
,
406 s
->is_listen
? ",server=on" : "");
408 case SOCKET_ADDRESS_TYPE_UNIX
:
410 const char *tight
= "", *abstract
= "";
411 UnixSocketAddress
*sa
= &s
->addr
->u
.q_unix
;
414 if (sa
->has_abstract
&& sa
->abstract
) {
415 abstract
= ",abstract=on";
416 if (sa
->has_tight
&& sa
->tight
) {
422 return g_strdup_printf("%sunix:%s%s%s%s", prefix
, sa
->path
,
424 s
->is_listen
? ",server=on" : "");
427 case SOCKET_ADDRESS_TYPE_FD
:
428 return g_strdup_printf("%sfd:%s%s", prefix
, s
->addr
->u
.fd
.str
,
429 s
->is_listen
? ",server=on" : "");
431 case SOCKET_ADDRESS_TYPE_VSOCK
:
432 return g_strdup_printf("%svsock:%s:%s", prefix
,
433 s
->addr
->u
.vsock
.cid
,
434 s
->addr
->u
.vsock
.port
);
440 static void update_disconnected_filename(SocketChardev
*s
)
442 Chardev
*chr
= CHARDEV(s
);
444 g_free(chr
->filename
);
446 chr
->filename
= qemu_chr_socket_address(s
, "disconnected:");
448 chr
->filename
= g_strdup("disconnected:socket");
452 /* NB may be called even if tcp_chr_connect has not been
453 * reached, due to TLS or telnet initialization failure,
454 * so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
455 * This must be called with chr->chr_write_lock held.
457 static void tcp_chr_disconnect_locked(Chardev
*chr
)
459 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
460 bool emit_close
= s
->state
== TCP_CHARDEV_STATE_CONNECTED
;
462 tcp_chr_free_connection(chr
);
465 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
466 chr
, NULL
, chr
->gcontext
);
468 update_disconnected_filename(s
);
470 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
472 if (s
->reconnect_time
&& !s
->reconnect_timer
) {
473 qemu_chr_socket_restart_timer(chr
);
477 static void tcp_chr_disconnect(Chardev
*chr
)
479 qemu_mutex_lock(&chr
->chr_write_lock
);
480 tcp_chr_disconnect_locked(chr
);
481 qemu_mutex_unlock(&chr
->chr_write_lock
);
484 static gboolean
tcp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
486 Chardev
*chr
= CHARDEV(opaque
);
487 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
488 uint8_t buf
[CHR_READ_BUF_LEN
];
491 if ((s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) ||
496 if (len
> s
->max_size
) {
499 size
= tcp_chr_recv(chr
, (void *)buf
, len
);
500 if (size
== 0 || (size
== -1 && errno
!= EAGAIN
)) {
501 /* connection closed */
502 tcp_chr_disconnect(chr
);
503 } else if (size
> 0) {
504 if (s
->do_telnetopt
) {
505 tcp_chr_process_IAC_bytes(chr
, s
, buf
, &size
);
508 qemu_chr_be_write(chr
, buf
, size
);
515 static gboolean
tcp_chr_hup(QIOChannel
*channel
,
519 Chardev
*chr
= CHARDEV(opaque
);
520 tcp_chr_disconnect(chr
);
521 return G_SOURCE_REMOVE
;
524 static int tcp_chr_sync_read(Chardev
*chr
, const uint8_t *buf
, int len
)
526 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
530 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
534 qio_channel_set_blocking(s
->ioc
, true, NULL
);
535 size
= tcp_chr_recv(chr
, (void *) buf
, len
);
537 if (s
->state
!= TCP_CHARDEV_STATE_DISCONNECTED
) {
538 qio_channel_set_blocking(s
->ioc
, false, NULL
);
541 /* connection closed */
542 tcp_chr_disconnect(chr
);
549 static char *qemu_chr_compute_filename(SocketChardev
*s
)
551 struct sockaddr_storage
*ss
= &s
->sioc
->localAddr
;
552 struct sockaddr_storage
*ps
= &s
->sioc
->remoteAddr
;
553 socklen_t ss_len
= s
->sioc
->localAddrLen
;
554 socklen_t ps_len
= s
->sioc
->remoteAddrLen
;
555 char shost
[NI_MAXHOST
], sserv
[NI_MAXSERV
];
556 char phost
[NI_MAXHOST
], pserv
[NI_MAXSERV
];
557 const char *left
= "", *right
= "";
559 switch (ss
->ss_family
) {
562 return g_strdup_printf("unix:%s%s",
563 ((struct sockaddr_un
*)(ss
))->sun_path
,
564 s
->is_listen
? ",server=on" : "");
571 getnameinfo((struct sockaddr
*) ss
, ss_len
, shost
, sizeof(shost
),
572 sserv
, sizeof(sserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
573 getnameinfo((struct sockaddr
*) ps
, ps_len
, phost
, sizeof(phost
),
574 pserv
, sizeof(pserv
), NI_NUMERICHOST
| NI_NUMERICSERV
);
575 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
576 qemu_chr_socket_protocol(s
),
577 left
, shost
, right
, sserv
,
578 s
->is_listen
? ",server=on" : "",
579 left
, phost
, right
, pserv
);
582 return g_strdup_printf("unknown");
586 static void update_ioc_handlers(SocketChardev
*s
)
588 Chardev
*chr
= CHARDEV(s
);
590 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
594 remove_fd_in_watch(chr
);
595 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc
,
600 remove_hup_source(s
);
601 s
->hup_source
= qio_channel_create_watch(s
->ioc
, G_IO_HUP
);
602 g_source_set_callback(s
->hup_source
, (GSourceFunc
)tcp_chr_hup
,
604 g_source_attach(s
->hup_source
, chr
->gcontext
);
607 static void tcp_chr_connect(void *opaque
)
609 Chardev
*chr
= CHARDEV(opaque
);
610 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
612 g_free(chr
->filename
);
613 chr
->filename
= qemu_chr_compute_filename(s
);
615 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTED
);
616 update_ioc_handlers(s
);
617 qemu_chr_be_event(chr
, CHR_EVENT_OPENED
);
620 static void tcp_chr_telnet_destroy(SocketChardev
*s
)
622 if (s
->telnet_source
) {
623 g_source_destroy(s
->telnet_source
);
624 g_source_unref(s
->telnet_source
);
625 s
->telnet_source
= NULL
;
629 static void tcp_chr_update_read_handler(Chardev
*chr
)
631 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
633 if (s
->listener
&& s
->state
== TCP_CHARDEV_STATE_DISCONNECTED
) {
635 * It's possible that chardev context is changed in
636 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
637 * listener if there is.
639 qio_net_listener_set_client_func_full(s
->listener
, tcp_chr_accept
,
640 chr
, NULL
, chr
->gcontext
);
643 if (s
->telnet_source
) {
644 tcp_chr_telnet_init(CHARDEV(s
));
647 update_ioc_handlers(s
);
650 static gboolean
tcp_chr_telnet_init_io(QIOChannel
*ioc
,
651 GIOCondition cond G_GNUC_UNUSED
,
654 SocketChardev
*s
= user_data
;
655 Chardev
*chr
= CHARDEV(s
);
656 TCPChardevTelnetInit
*init
= s
->telnet_init
;
661 ret
= qio_channel_write(ioc
, init
->buf
, init
->buflen
, NULL
);
663 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
666 tcp_chr_disconnect(chr
);
672 if (init
->buflen
== 0) {
673 tcp_chr_connect(chr
);
677 memmove(init
->buf
, init
->buf
+ ret
, init
->buflen
);
679 return G_SOURCE_CONTINUE
;
682 g_free(s
->telnet_init
);
683 s
->telnet_init
= NULL
;
684 g_source_unref(s
->telnet_source
);
685 s
->telnet_source
= NULL
;
686 return G_SOURCE_REMOVE
;
689 static void tcp_chr_telnet_init(Chardev
*chr
)
691 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
692 TCPChardevTelnetInit
*init
;
695 /* Destroy existing task */
696 tcp_chr_telnet_destroy(s
);
698 if (s
->telnet_init
) {
699 /* We are possibly during a handshake already */
703 s
->telnet_init
= g_new0(TCPChardevTelnetInit
, 1);
704 init
= s
->telnet_init
;
706 #define IACSET(x, a, b, c) \
715 /* Prep the telnet negotion to put telnet in binary,
716 * no echo, single char mode */
717 IACSET(init
->buf
, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
718 IACSET(init
->buf
, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
719 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
720 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO Binary */
723 /* Prep the TN3270 negotion based on RFC1576 */
724 IACSET(init
->buf
, 0xff, 0xfd, 0x19); /* IAC DO EOR */
725 IACSET(init
->buf
, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
726 IACSET(init
->buf
, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
727 IACSET(init
->buf
, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
728 IACSET(init
->buf
, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
729 IACSET(init
->buf
, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
730 IACSET(init
->buf
, 0x01, 0xff, 0xf0); /* SEND IAC SE */
736 s
->telnet_source
= qio_channel_add_watch_source(s
->ioc
, G_IO_OUT
,
737 tcp_chr_telnet_init_io
,
743 static void tcp_chr_websock_handshake(QIOTask
*task
, gpointer user_data
)
745 Chardev
*chr
= user_data
;
746 SocketChardev
*s
= user_data
;
748 if (qio_task_propagate_error(task
, NULL
)) {
749 tcp_chr_disconnect(chr
);
751 if (s
->do_telnetopt
) {
752 tcp_chr_telnet_init(chr
);
754 tcp_chr_connect(chr
);
760 static void tcp_chr_websock_init(Chardev
*chr
)
762 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
763 QIOChannelWebsock
*wioc
= NULL
;
766 wioc
= qio_channel_websock_new_server(s
->ioc
);
768 name
= g_strdup_printf("chardev-websocket-server-%s", chr
->label
);
769 qio_channel_set_name(QIO_CHANNEL(wioc
), name
);
771 object_unref(OBJECT(s
->ioc
));
772 s
->ioc
= QIO_CHANNEL(wioc
);
774 qio_channel_websock_handshake(wioc
, tcp_chr_websock_handshake
, chr
, NULL
);
778 static void tcp_chr_tls_handshake(QIOTask
*task
,
781 Chardev
*chr
= user_data
;
782 SocketChardev
*s
= user_data
;
784 if (qio_task_propagate_error(task
, NULL
)) {
785 tcp_chr_disconnect(chr
);
788 tcp_chr_websock_init(chr
);
789 } else if (s
->do_telnetopt
) {
790 tcp_chr_telnet_init(chr
);
792 tcp_chr_connect(chr
);
798 static void tcp_chr_tls_init(Chardev
*chr
)
800 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
805 tioc
= qio_channel_tls_new_server(
806 s
->ioc
, s
->tls_creds
,
810 tioc
= qio_channel_tls_new_client(
811 s
->ioc
, s
->tls_creds
,
812 s
->addr
->u
.inet
.host
,
816 tcp_chr_disconnect(chr
);
819 name
= g_strdup_printf("chardev-tls-%s-%s",
820 s
->is_listen
? "server" : "client",
822 qio_channel_set_name(QIO_CHANNEL(tioc
), name
);
824 object_unref(OBJECT(s
->ioc
));
825 s
->ioc
= QIO_CHANNEL(tioc
);
827 qio_channel_tls_handshake(tioc
,
828 tcp_chr_tls_handshake
,
835 static void tcp_chr_set_client_ioc_name(Chardev
*chr
,
836 QIOChannelSocket
*sioc
)
838 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
840 name
= g_strdup_printf("chardev-tcp-%s-%s",
841 s
->is_listen
? "server" : "client",
843 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
848 static int tcp_chr_new_client(Chardev
*chr
, QIOChannelSocket
*sioc
)
850 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
852 if (s
->state
!= TCP_CHARDEV_STATE_CONNECTING
) {
856 s
->ioc
= QIO_CHANNEL(sioc
);
857 object_ref(OBJECT(sioc
));
859 object_ref(OBJECT(sioc
));
861 qio_channel_set_blocking(s
->ioc
, false, NULL
);
864 qio_channel_set_delay(s
->ioc
, false);
867 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
868 NULL
, chr
->gcontext
);
872 tcp_chr_tls_init(chr
);
873 } else if (s
->is_websock
) {
874 tcp_chr_websock_init(chr
);
875 } else if (s
->do_telnetopt
) {
876 tcp_chr_telnet_init(chr
);
878 tcp_chr_connect(chr
);
885 static int tcp_chr_add_client(Chardev
*chr
, int fd
)
888 QIOChannelSocket
*sioc
;
889 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
891 if (s
->state
!= TCP_CHARDEV_STATE_DISCONNECTED
) {
895 sioc
= qio_channel_socket_new_fd(fd
, NULL
);
899 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
900 tcp_chr_set_client_ioc_name(chr
, sioc
);
901 if (s
->registered_yank
) {
902 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
903 char_socket_yank_iochannel
,
906 ret
= tcp_chr_new_client(chr
, sioc
);
907 object_unref(OBJECT(sioc
));
911 static void tcp_chr_accept(QIONetListener
*listener
,
912 QIOChannelSocket
*cioc
,
915 Chardev
*chr
= CHARDEV(opaque
);
916 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
918 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
919 tcp_chr_set_client_ioc_name(chr
, cioc
);
920 if (s
->registered_yank
) {
921 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
922 char_socket_yank_iochannel
,
925 tcp_chr_new_client(chr
, cioc
);
929 static int tcp_chr_connect_client_sync(Chardev
*chr
, Error
**errp
)
931 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
932 QIOChannelSocket
*sioc
= qio_channel_socket_new();
933 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
934 tcp_chr_set_client_ioc_name(chr
, sioc
);
935 if (qio_channel_socket_connect_sync(sioc
, s
->addr
, errp
) < 0) {
936 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_DISCONNECTED
);
937 object_unref(OBJECT(sioc
));
940 if (s
->registered_yank
) {
941 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
942 char_socket_yank_iochannel
,
945 tcp_chr_new_client(chr
, sioc
);
946 object_unref(OBJECT(sioc
));
951 static void tcp_chr_accept_server_sync(Chardev
*chr
)
953 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
954 QIOChannelSocket
*sioc
;
955 info_report("QEMU waiting for connection on: %s",
957 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
958 sioc
= qio_net_listener_wait_client(s
->listener
);
959 tcp_chr_set_client_ioc_name(chr
, sioc
);
960 if (s
->registered_yank
) {
961 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
962 char_socket_yank_iochannel
,
965 tcp_chr_new_client(chr
, sioc
);
966 object_unref(OBJECT(sioc
));
970 static int tcp_chr_wait_connected(Chardev
*chr
, Error
**errp
)
972 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
973 const char *opts
[] = { "telnet", "tn3270", "websock", "tls-creds" };
974 bool optset
[] = { s
->is_telnet
, s
->is_tn3270
, s
->is_websock
, s
->tls_creds
};
977 QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts
) != G_N_ELEMENTS(optset
));
978 for (i
= 0; i
< G_N_ELEMENTS(opts
); i
++) {
981 "'%s' option is incompatible with waiting for "
982 "connection completion", opts
[i
]);
987 tcp_chr_reconn_timer_cancel(s
);
990 * We expect states to be as follows:
993 * - wait -> CONNECTED
994 * - nowait -> DISCONNECTED
996 * - reconnect == 0 -> CONNECTED
997 * - reconnect != 0 -> CONNECTING
1000 if (s
->state
== TCP_CHARDEV_STATE_CONNECTING
) {
1001 if (!s
->connect_task
) {
1003 "Unexpected 'connecting' state without connect task "
1004 "while waiting for connection completion");
1008 * tcp_chr_wait_connected should only ever be run from the
1009 * main loop thread associated with chr->gcontext, otherwise
1010 * qio_task_wait_thread has a dangerous race condition with
1011 * free'ing of the s->connect_task object.
1013 * Acquiring the main context doesn't 100% prove we're in
1014 * the main loop thread, but it does at least guarantee
1015 * that the main loop won't be executed by another thread
1016 * avoiding the race condition with the task idle callback.
1018 g_main_context_acquire(chr
->gcontext
);
1019 qio_task_wait_thread(s
->connect_task
);
1020 g_main_context_release(chr
->gcontext
);
1023 * The completion callback (qemu_chr_socket_connected) for
1024 * s->connect_task should have set this to NULL by the time
1025 * qio_task_wait_thread has returned.
1027 assert(!s
->connect_task
);
1030 * NB we are *not* guaranteed to have "s->state == ..CONNECTED"
1031 * at this point as this first connect may be failed, so
1032 * allow the next loop to run regardless.
1036 while (s
->state
!= TCP_CHARDEV_STATE_CONNECTED
) {
1038 tcp_chr_accept_server_sync(chr
);
1041 if (tcp_chr_connect_client_sync(chr
, &err
) < 0) {
1042 if (s
->reconnect_time
) {
1044 g_usleep(s
->reconnect_time
* 1000ULL * 1000ULL);
1046 error_propagate(errp
, err
);
1056 static void char_socket_finalize(Object
*obj
)
1058 Chardev
*chr
= CHARDEV(obj
);
1059 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1061 tcp_chr_free_connection(chr
);
1062 tcp_chr_reconn_timer_cancel(s
);
1063 qapi_free_SocketAddress(s
->addr
);
1064 tcp_chr_telnet_destroy(s
);
1065 g_free(s
->telnet_init
);
1067 qio_net_listener_set_client_func_full(s
->listener
, NULL
, NULL
,
1068 NULL
, chr
->gcontext
);
1069 object_unref(OBJECT(s
->listener
));
1072 object_unref(OBJECT(s
->tls_creds
));
1074 g_free(s
->tls_authz
);
1075 if (s
->registered_yank
) {
1077 * In the chardev-change special-case, we shouldn't unregister the yank
1078 * instance, as it still may be needed.
1080 if (!chr
->handover_yank_instance
) {
1081 yank_unregister_instance(CHARDEV_YANK_INSTANCE(chr
->label
));
1085 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
1088 static void qemu_chr_socket_connected(QIOTask
*task
, void *opaque
)
1090 QIOChannelSocket
*sioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
1091 Chardev
*chr
= CHARDEV(opaque
);
1092 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1095 s
->connect_task
= NULL
;
1097 if (qio_task_propagate_error(task
, &err
)) {
1098 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_DISCONNECTED
);
1099 if (s
->registered_yank
) {
1100 yank_unregister_function(CHARDEV_YANK_INSTANCE(chr
->label
),
1101 char_socket_yank_iochannel
,
1104 check_report_connect_error(chr
, err
);
1108 s
->connect_err_reported
= false;
1109 tcp_chr_new_client(chr
, sioc
);
1112 object_unref(OBJECT(sioc
));
1116 static void tcp_chr_connect_client_task(QIOTask
*task
,
1119 QIOChannelSocket
*ioc
= QIO_CHANNEL_SOCKET(qio_task_get_source(task
));
1120 SocketAddress
*addr
= opaque
;
1123 qio_channel_socket_connect_sync(ioc
, addr
, &err
);
1125 qio_task_set_error(task
, err
);
1129 static void tcp_chr_connect_client_async(Chardev
*chr
)
1131 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1132 QIOChannelSocket
*sioc
;
1134 tcp_chr_change_state(s
, TCP_CHARDEV_STATE_CONNECTING
);
1135 sioc
= qio_channel_socket_new();
1136 tcp_chr_set_client_ioc_name(chr
, sioc
);
1137 if (s
->registered_yank
) {
1138 yank_register_function(CHARDEV_YANK_INSTANCE(chr
->label
),
1139 char_socket_yank_iochannel
,
1143 * Normally code would use the qio_channel_socket_connect_async
1144 * method which uses a QIOTask + qio_task_set_error internally
1145 * to avoid blocking. The tcp_chr_wait_connected method, however,
1146 * needs a way to synchronize with completion of the background
1147 * connect task which can't be done with the QIOChannelSocket
1148 * async APIs. Thus we must use QIOTask directly to implement
1149 * the non-blocking concept locally.
1151 s
->connect_task
= qio_task_new(OBJECT(sioc
),
1152 qemu_chr_socket_connected
,
1153 object_ref(OBJECT(chr
)),
1154 (GDestroyNotify
)object_unref
);
1155 qio_task_run_in_thread(s
->connect_task
,
1156 tcp_chr_connect_client_task
,
1162 static gboolean
socket_reconnect_timeout(gpointer opaque
)
1164 Chardev
*chr
= CHARDEV(opaque
);
1165 SocketChardev
*s
= SOCKET_CHARDEV(opaque
);
1167 qemu_mutex_lock(&chr
->chr_write_lock
);
1168 g_source_unref(s
->reconnect_timer
);
1169 s
->reconnect_timer
= NULL
;
1170 qemu_mutex_unlock(&chr
->chr_write_lock
);
1176 tcp_chr_connect_client_async(chr
);
1182 static int qmp_chardev_open_socket_server(Chardev
*chr
,
1184 bool is_waitconnect
,
1187 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1190 s
->do_telnetopt
= 1;
1192 s
->listener
= qio_net_listener_new();
1194 name
= g_strdup_printf("chardev-tcp-listener-%s", chr
->label
);
1195 qio_net_listener_set_name(s
->listener
, name
);
1198 if (s
->addr
->type
== SOCKET_ADDRESS_TYPE_FD
&& !*s
->addr
->u
.fd
.str
) {
1202 if (qio_net_listener_open_sync(s
->listener
, s
->addr
, 1, errp
) < 0) {
1203 object_unref(OBJECT(s
->listener
));
1208 qapi_free_SocketAddress(s
->addr
);
1209 s
->addr
= socket_local_address(s
->listener
->sioc
[0]->fd
, errp
);
1212 update_disconnected_filename(s
);
1214 if (is_waitconnect
) {
1215 tcp_chr_accept_server_sync(chr
);
1217 qio_net_listener_set_client_func_full(s
->listener
,
1227 static int qmp_chardev_open_socket_client(Chardev
*chr
,
1231 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1233 if (reconnect
> 0) {
1234 s
->reconnect_time
= reconnect
;
1235 tcp_chr_connect_client_async(chr
);
1238 return tcp_chr_connect_client_sync(chr
, errp
);
1243 static bool qmp_chardev_validate_socket(ChardevSocket
*sock
,
1244 SocketAddress
*addr
,
1247 /* Validate any options which have a dependency on address type */
1248 switch (addr
->type
) {
1249 case SOCKET_ADDRESS_TYPE_FD
:
1250 if (sock
->has_reconnect
) {
1252 "'reconnect' option is incompatible with "
1253 "'fd' address type");
1256 if (sock
->has_tls_creds
&&
1257 !(sock
->has_server
&& sock
->server
)) {
1259 "'tls_creds' option is incompatible with "
1260 "'fd' address type as client");
1265 case SOCKET_ADDRESS_TYPE_UNIX
:
1266 if (sock
->has_tls_creds
) {
1268 "'tls_creds' option is incompatible with "
1269 "'unix' address type");
1274 case SOCKET_ADDRESS_TYPE_INET
:
1277 case SOCKET_ADDRESS_TYPE_VSOCK
:
1278 if (sock
->has_tls_creds
) {
1280 "'tls_creds' option is incompatible with "
1281 "'vsock' address type");
1289 if (sock
->has_tls_authz
&& !sock
->has_tls_creds
) {
1290 error_setg(errp
, "'tls_authz' option requires 'tls_creds' option");
1294 /* Validate any options which have a dependancy on client vs server */
1295 if (!sock
->has_server
|| sock
->server
) {
1296 if (sock
->has_reconnect
) {
1298 "'reconnect' option is incompatible with "
1299 "socket in server listen mode");
1303 if (sock
->has_websocket
&& sock
->websocket
) {
1304 error_setg(errp
, "%s", "Websocket client is not implemented");
1307 if (sock
->has_wait
) {
1308 error_setg(errp
, "%s",
1309 "'wait' option is incompatible with "
1310 "socket in client connect mode");
1319 static void qmp_chardev_open_socket(Chardev
*chr
,
1320 ChardevBackend
*backend
,
1324 SocketChardev
*s
= SOCKET_CHARDEV(chr
);
1325 ChardevSocket
*sock
= backend
->u
.socket
.data
;
1326 bool do_nodelay
= sock
->has_nodelay
? sock
->nodelay
: false;
1327 bool is_listen
= sock
->has_server
? sock
->server
: true;
1328 bool is_telnet
= sock
->has_telnet
? sock
->telnet
: false;
1329 bool is_tn3270
= sock
->has_tn3270
? sock
->tn3270
: false;
1330 bool is_waitconnect
= sock
->has_wait
? sock
->wait
: false;
1331 bool is_websock
= sock
->has_websocket
? sock
->websocket
: false;
1332 int64_t reconnect
= sock
->has_reconnect
? sock
->reconnect
: 0;
1333 SocketAddress
*addr
;
1335 s
->is_listen
= is_listen
;
1336 s
->is_telnet
= is_telnet
;
1337 s
->is_tn3270
= is_tn3270
;
1338 s
->is_websock
= is_websock
;
1339 s
->do_nodelay
= do_nodelay
;
1340 if (sock
->tls_creds
) {
1342 creds
= object_resolve_path_component(
1343 object_get_objects_root(), sock
->tls_creds
);
1345 error_setg(errp
, "No TLS credentials with id '%s'",
1349 s
->tls_creds
= (QCryptoTLSCreds
*)
1350 object_dynamic_cast(creds
,
1351 TYPE_QCRYPTO_TLS_CREDS
);
1352 if (!s
->tls_creds
) {
1353 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1357 object_ref(OBJECT(s
->tls_creds
));
1358 if (!qcrypto_tls_creds_check_endpoint(s
->tls_creds
,
1360 ? QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
1361 : QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
1366 s
->tls_authz
= g_strdup(sock
->tls_authz
);
1368 s
->addr
= addr
= socket_address_flatten(sock
->addr
);
1370 if (!qmp_chardev_validate_socket(sock
, addr
, errp
)) {
1374 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_RECONNECTABLE
);
1375 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1376 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
1377 qemu_chr_set_feature(chr
, QEMU_CHAR_FEATURE_FD_PASS
);
1381 * In the chardev-change special-case, we shouldn't register a new yank
1382 * instance, as there already may be one.
1384 if (!chr
->handover_yank_instance
) {
1385 if (!yank_register_instance(CHARDEV_YANK_INSTANCE(chr
->label
), errp
)) {
1389 s
->registered_yank
= true;
1391 /* be isn't opened until we get a connection */
1394 update_disconnected_filename(s
);
1397 if (qmp_chardev_open_socket_server(chr
, is_telnet
|| is_tn3270
,
1398 is_waitconnect
, errp
) < 0) {
1402 if (qmp_chardev_open_socket_client(chr
, reconnect
, errp
) < 0) {
1408 static void qemu_chr_parse_socket(QemuOpts
*opts
, ChardevBackend
*backend
,
1411 const char *path
= qemu_opt_get(opts
, "path");
1412 const char *host
= qemu_opt_get(opts
, "host");
1413 const char *port
= qemu_opt_get(opts
, "port");
1414 const char *fd
= qemu_opt_get(opts
, "fd");
1416 bool tight
= qemu_opt_get_bool(opts
, "tight", true);
1417 bool abstract
= qemu_opt_get_bool(opts
, "abstract", false);
1419 SocketAddressLegacy
*addr
;
1420 ChardevSocket
*sock
;
1422 if ((!!path
+ !!fd
+ !!host
) > 1) {
1424 "None or one of 'path', 'fd' or 'host' option required.");
1428 if (host
&& !port
) {
1429 error_setg(errp
, "chardev: socket: no port given");
1433 backend
->type
= CHARDEV_BACKEND_KIND_SOCKET
;
1434 sock
= backend
->u
.socket
.data
= g_new0(ChardevSocket
, 1);
1435 qemu_chr_parse_common(opts
, qapi_ChardevSocket_base(sock
));
1437 if (qemu_opt_get(opts
, "delay") && qemu_opt_get(opts
, "nodelay")) {
1438 error_setg(errp
, "'delay' and 'nodelay' are mutually exclusive");
1442 qemu_opt_get(opts
, "delay") ||
1443 qemu_opt_get(opts
, "nodelay");
1445 !qemu_opt_get_bool(opts
, "delay", true) ||
1446 qemu_opt_get_bool(opts
, "nodelay", false);
1449 * We have different default to QMP for 'server', hence
1450 * we can't just check for existence of 'server'
1452 sock
->has_server
= true;
1453 sock
->server
= qemu_opt_get_bool(opts
, "server", false);
1454 sock
->has_telnet
= qemu_opt_get(opts
, "telnet");
1455 sock
->telnet
= qemu_opt_get_bool(opts
, "telnet", false);
1456 sock
->has_tn3270
= qemu_opt_get(opts
, "tn3270");
1457 sock
->tn3270
= qemu_opt_get_bool(opts
, "tn3270", false);
1458 sock
->has_websocket
= qemu_opt_get(opts
, "websocket");
1459 sock
->websocket
= qemu_opt_get_bool(opts
, "websocket", false);
1461 * We have different default to QMP for 'wait' when 'server'
1462 * is set, hence we can't just check for existence of 'wait'
1464 sock
->has_wait
= qemu_opt_find(opts
, "wait") || sock
->server
;
1465 sock
->wait
= qemu_opt_get_bool(opts
, "wait", true);
1466 sock
->has_reconnect
= qemu_opt_find(opts
, "reconnect");
1467 sock
->reconnect
= qemu_opt_get_number(opts
, "reconnect", 0);
1468 sock
->has_tls_creds
= qemu_opt_get(opts
, "tls-creds");
1469 sock
->tls_creds
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
1470 sock
->has_tls_authz
= qemu_opt_get(opts
, "tls-authz");
1471 sock
->tls_authz
= g_strdup(qemu_opt_get(opts
, "tls-authz"));
1473 addr
= g_new0(SocketAddressLegacy
, 1);
1475 UnixSocketAddress
*q_unix
;
1476 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1477 q_unix
= addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1478 q_unix
->path
= g_strdup(path
);
1480 q_unix
->has_tight
= true;
1481 q_unix
->tight
= tight
;
1482 q_unix
->has_abstract
= true;
1483 q_unix
->abstract
= abstract
;
1486 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1487 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
1488 *addr
->u
.inet
.data
= (InetSocketAddress
) {
1489 .host
= g_strdup(host
),
1490 .port
= g_strdup(port
),
1491 .has_to
= qemu_opt_get(opts
, "to"),
1492 .to
= qemu_opt_get_number(opts
, "to", 0),
1493 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
1494 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
1495 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
1496 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
1499 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1500 addr
->u
.fd
.data
= g_new(String
, 1);
1501 addr
->u
.fd
.data
->str
= g_strdup(fd
);
1507 char_socket_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1508 void *opaque
, Error
**errp
)
1510 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1512 visit_type_SocketAddress(v
, name
, &s
->addr
, errp
);
1516 char_socket_get_connected(Object
*obj
, Error
**errp
)
1518 SocketChardev
*s
= SOCKET_CHARDEV(obj
);
1520 return s
->state
== TCP_CHARDEV_STATE_CONNECTED
;
1523 static void char_socket_class_init(ObjectClass
*oc
, void *data
)
1525 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
1527 cc
->supports_yank
= true;
1529 cc
->parse
= qemu_chr_parse_socket
;
1530 cc
->open
= qmp_chardev_open_socket
;
1531 cc
->chr_wait_connected
= tcp_chr_wait_connected
;
1532 cc
->chr_write
= tcp_chr_write
;
1533 cc
->chr_sync_read
= tcp_chr_sync_read
;
1534 cc
->chr_disconnect
= tcp_chr_disconnect
;
1535 cc
->get_msgfds
= tcp_get_msgfds
;
1536 cc
->set_msgfds
= tcp_set_msgfds
;
1537 cc
->chr_add_client
= tcp_chr_add_client
;
1538 cc
->chr_add_watch
= tcp_chr_add_watch
;
1539 cc
->chr_update_read_handler
= tcp_chr_update_read_handler
;
1541 object_class_property_add(oc
, "addr", "SocketAddress",
1542 char_socket_get_addr
, NULL
,
1545 object_class_property_add_bool(oc
, "connected", char_socket_get_connected
,
1549 static const TypeInfo char_socket_type_info
= {
1550 .name
= TYPE_CHARDEV_SOCKET
,
1551 .parent
= TYPE_CHARDEV
,
1552 .instance_size
= sizeof(SocketChardev
),
1553 .instance_finalize
= char_socket_finalize
,
1554 .class_init
= char_socket_class_init
,
1557 static void register_types(void)
1559 type_register_static(&char_socket_type_info
);
1562 type_init(register_types
);