target/mips: Add 'fall through' comments for handling nanoMips' SHXS, SWXS
[qemu/ar7.git] / chardev / char-socket.c
blob7ca5d97af34129988d7099a110d009aecbcd3583
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
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/module.h"
33 #include "qemu/option.h"
34 #include "qapi/error.h"
35 #include "qapi/clone-visitor.h"
36 #include "qapi/qapi-visit-sockets.h"
38 #include "chardev/char-io.h"
40 /***********************************************************/
41 /* TCP Net console */
43 #define TCP_MAX_FDS 16
45 typedef struct {
46 char buf[21];
47 size_t buflen;
48 } TCPChardevTelnetInit;
50 typedef enum {
51 TCP_CHARDEV_STATE_DISCONNECTED,
52 TCP_CHARDEV_STATE_CONNECTING,
53 TCP_CHARDEV_STATE_CONNECTED,
54 } TCPChardevState;
56 typedef struct {
57 Chardev parent;
58 QIOChannel *ioc; /* Client I/O channel */
59 QIOChannelSocket *sioc; /* Client master channel */
60 QIONetListener *listener;
61 GSource *hup_source;
62 QCryptoTLSCreds *tls_creds;
63 char *tls_authz;
64 TCPChardevState state;
65 int max_size;
66 int do_telnetopt;
67 int do_nodelay;
68 int *read_msgfds;
69 size_t read_msgfds_num;
70 int *write_msgfds;
71 size_t write_msgfds_num;
73 SocketAddress *addr;
74 bool is_listen;
75 bool is_telnet;
76 bool is_tn3270;
77 GSource *telnet_source;
78 TCPChardevTelnetInit *telnet_init;
80 bool is_websock;
82 GSource *reconnect_timer;
83 int64_t reconnect_time;
84 bool connect_err_reported;
86 QIOTask *connect_task;
87 } SocketChardev;
89 #define SOCKET_CHARDEV(obj) \
90 OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
92 static gboolean socket_reconnect_timeout(gpointer opaque);
93 static void tcp_chr_telnet_init(Chardev *chr);
95 static void tcp_chr_change_state(SocketChardev *s, TCPChardevState state)
97 switch (state) {
98 case TCP_CHARDEV_STATE_DISCONNECTED:
99 break;
100 case TCP_CHARDEV_STATE_CONNECTING:
101 assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
102 break;
103 case TCP_CHARDEV_STATE_CONNECTED:
104 assert(s->state == TCP_CHARDEV_STATE_CONNECTING);
105 break;
107 s->state = state;
110 static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
112 if (s->reconnect_timer) {
113 g_source_destroy(s->reconnect_timer);
114 g_source_unref(s->reconnect_timer);
115 s->reconnect_timer = NULL;
119 static void qemu_chr_socket_restart_timer(Chardev *chr)
121 SocketChardev *s = SOCKET_CHARDEV(chr);
122 char *name;
124 assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
125 assert(!s->reconnect_timer);
126 name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
127 s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
128 s->reconnect_time * 1000,
129 socket_reconnect_timeout,
130 chr);
131 g_source_set_name(s->reconnect_timer, name);
132 g_free(name);
135 static void check_report_connect_error(Chardev *chr,
136 Error *err)
138 SocketChardev *s = SOCKET_CHARDEV(chr);
140 if (!s->connect_err_reported) {
141 error_report("Unable to connect character device %s: %s",
142 chr->label, error_get_pretty(err));
143 s->connect_err_reported = true;
145 qemu_chr_socket_restart_timer(chr);
148 static void tcp_chr_accept(QIONetListener *listener,
149 QIOChannelSocket *cioc,
150 void *opaque);
152 static int tcp_chr_read_poll(void *opaque);
153 static void tcp_chr_disconnect(Chardev *chr);
155 /* Called with chr_write_lock held. */
156 static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
158 SocketChardev *s = SOCKET_CHARDEV(chr);
160 if (s->state == TCP_CHARDEV_STATE_CONNECTED) {
161 int ret = io_channel_send_full(s->ioc, buf, len,
162 s->write_msgfds,
163 s->write_msgfds_num);
165 /* free the written msgfds in any cases
166 * other than ret < 0 && errno == EAGAIN
168 if (!(ret < 0 && EAGAIN == errno)
169 && s->write_msgfds_num) {
170 g_free(s->write_msgfds);
171 s->write_msgfds = 0;
172 s->write_msgfds_num = 0;
175 if (ret < 0 && errno != EAGAIN) {
176 if (tcp_chr_read_poll(chr) <= 0) {
177 tcp_chr_disconnect(chr);
178 return len;
179 } /* else let the read handler finish it properly */
182 return ret;
183 } else {
184 /* XXX: indicate an error ? */
185 return len;
189 static int tcp_chr_read_poll(void *opaque)
191 Chardev *chr = CHARDEV(opaque);
192 SocketChardev *s = SOCKET_CHARDEV(opaque);
193 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
194 return 0;
196 s->max_size = qemu_chr_be_can_write(chr);
197 return s->max_size;
200 static void tcp_chr_process_IAC_bytes(Chardev *chr,
201 SocketChardev *s,
202 uint8_t *buf, int *size)
204 /* Handle any telnet or tn3270 client's basic IAC options.
205 * For telnet options, it satisfies char by char mode with no echo.
206 * For tn3270 options, it satisfies binary mode with EOR.
207 * All IAC options will be removed from the buf and the do_opt
208 * pointer will be used to track the state of the width of the
209 * IAC information.
211 * RFC854: "All TELNET commands consist of at least a two byte sequence.
212 * The commands dealing with option negotiation are three byte sequences,
213 * the third byte being the code for the option referenced."
214 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
215 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
216 * for tn3270.
217 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
218 * session, and NOP and IP need to be done later.
221 int i;
222 int j = 0;
224 for (i = 0; i < *size; i++) {
225 if (s->do_telnetopt > 1) {
226 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
227 /* Double IAC means send an IAC */
228 if (j != i) {
229 buf[j] = buf[i];
231 j++;
232 s->do_telnetopt = 1;
233 } else {
234 if ((unsigned char)buf[i] == IAC_BREAK
235 && s->do_telnetopt == 2) {
236 /* Handle IAC break commands by sending a serial break */
237 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
238 s->do_telnetopt++;
239 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
240 || (unsigned char)buf[i] == IAC_SB
241 || (unsigned char)buf[i] == IAC_SE)
242 && s->do_telnetopt == 2) {
243 buf[j++] = IAC;
244 buf[j++] = buf[i];
245 s->do_telnetopt++;
246 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
247 || (unsigned char)buf[i] == IAC_NOP)
248 && s->do_telnetopt == 2) {
249 /* TODO: IP and NOP need to be implemented later. */
250 s->do_telnetopt++;
252 s->do_telnetopt++;
254 if (s->do_telnetopt >= 4) {
255 s->do_telnetopt = 1;
257 } else {
258 if ((unsigned char)buf[i] == IAC) {
259 s->do_telnetopt = 2;
260 } else {
261 if (j != i) {
262 buf[j] = buf[i];
264 j++;
268 *size = j;
271 static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
273 SocketChardev *s = SOCKET_CHARDEV(chr);
275 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
277 assert(num <= TCP_MAX_FDS);
279 if (to_copy) {
280 int i;
282 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
284 /* Close unused fds */
285 for (i = to_copy; i < s->read_msgfds_num; i++) {
286 close(s->read_msgfds[i]);
289 g_free(s->read_msgfds);
290 s->read_msgfds = 0;
291 s->read_msgfds_num = 0;
294 return to_copy;
297 static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
299 SocketChardev *s = SOCKET_CHARDEV(chr);
301 /* clear old pending fd array */
302 g_free(s->write_msgfds);
303 s->write_msgfds = NULL;
304 s->write_msgfds_num = 0;
306 if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
307 !qio_channel_has_feature(s->ioc,
308 QIO_CHANNEL_FEATURE_FD_PASS)) {
309 return -1;
312 if (num) {
313 s->write_msgfds = g_new(int, num);
314 memcpy(s->write_msgfds, fds, num * sizeof(int));
317 s->write_msgfds_num = num;
319 return 0;
322 static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
324 SocketChardev *s = SOCKET_CHARDEV(chr);
325 struct iovec iov = { .iov_base = buf, .iov_len = len };
326 int ret;
327 size_t i;
328 int *msgfds = NULL;
329 size_t msgfds_num = 0;
331 if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
332 ret = qio_channel_readv_full(s->ioc, &iov, 1,
333 &msgfds, &msgfds_num,
334 NULL);
335 } else {
336 ret = qio_channel_readv_full(s->ioc, &iov, 1,
337 NULL, NULL,
338 NULL);
341 if (ret == QIO_CHANNEL_ERR_BLOCK) {
342 errno = EAGAIN;
343 ret = -1;
344 } else if (ret == -1) {
345 errno = EIO;
348 if (msgfds_num) {
349 /* close and clean read_msgfds */
350 for (i = 0; i < s->read_msgfds_num; i++) {
351 close(s->read_msgfds[i]);
354 if (s->read_msgfds_num) {
355 g_free(s->read_msgfds);
358 s->read_msgfds = msgfds;
359 s->read_msgfds_num = msgfds_num;
362 for (i = 0; i < s->read_msgfds_num; i++) {
363 int fd = s->read_msgfds[i];
364 if (fd < 0) {
365 continue;
368 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
369 qemu_set_block(fd);
371 #ifndef MSG_CMSG_CLOEXEC
372 qemu_set_cloexec(fd);
373 #endif
376 return ret;
379 static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
381 SocketChardev *s = SOCKET_CHARDEV(chr);
382 return qio_channel_create_watch(s->ioc, cond);
385 static void remove_hup_source(SocketChardev *s)
387 if (s->hup_source != NULL) {
388 g_source_destroy(s->hup_source);
389 g_source_unref(s->hup_source);
390 s->hup_source = NULL;
394 static void tcp_chr_free_connection(Chardev *chr)
396 SocketChardev *s = SOCKET_CHARDEV(chr);
397 int i;
399 if (s->read_msgfds_num) {
400 for (i = 0; i < s->read_msgfds_num; i++) {
401 close(s->read_msgfds[i]);
403 g_free(s->read_msgfds);
404 s->read_msgfds = NULL;
405 s->read_msgfds_num = 0;
408 remove_hup_source(s);
410 tcp_set_msgfds(chr, NULL, 0);
411 remove_fd_in_watch(chr);
412 object_unref(OBJECT(s->sioc));
413 s->sioc = NULL;
414 object_unref(OBJECT(s->ioc));
415 s->ioc = NULL;
416 g_free(chr->filename);
417 chr->filename = NULL;
418 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
421 static const char *qemu_chr_socket_protocol(SocketChardev *s)
423 if (s->is_telnet) {
424 return "telnet";
426 return s->is_websock ? "websocket" : "tcp";
429 static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)
431 switch (s->addr->type) {
432 case SOCKET_ADDRESS_TYPE_INET:
433 return g_strdup_printf("%s%s:%s:%s%s", prefix,
434 qemu_chr_socket_protocol(s),
435 s->addr->u.inet.host,
436 s->addr->u.inet.port,
437 s->is_listen ? ",server" : "");
438 break;
439 case SOCKET_ADDRESS_TYPE_UNIX:
440 return g_strdup_printf("%sunix:%s%s", prefix,
441 s->addr->u.q_unix.path,
442 s->is_listen ? ",server" : "");
443 break;
444 case SOCKET_ADDRESS_TYPE_FD:
445 return g_strdup_printf("%sfd:%s%s", prefix, s->addr->u.fd.str,
446 s->is_listen ? ",server" : "");
447 break;
448 case SOCKET_ADDRESS_TYPE_VSOCK:
449 return g_strdup_printf("%svsock:%s:%s", prefix,
450 s->addr->u.vsock.cid,
451 s->addr->u.vsock.port);
452 default:
453 abort();
457 static void update_disconnected_filename(SocketChardev *s)
459 Chardev *chr = CHARDEV(s);
461 g_free(chr->filename);
462 if (s->addr) {
463 chr->filename = qemu_chr_socket_address(s, "disconnected:");
464 } else {
465 chr->filename = g_strdup("disconnected:socket");
469 /* NB may be called even if tcp_chr_connect has not been
470 * reached, due to TLS or telnet initialization failure,
471 * so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
473 static void tcp_chr_disconnect(Chardev *chr)
475 SocketChardev *s = SOCKET_CHARDEV(chr);
476 bool emit_close = s->state == TCP_CHARDEV_STATE_CONNECTED;
478 tcp_chr_free_connection(chr);
480 if (s->listener) {
481 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
482 chr, NULL, chr->gcontext);
484 update_disconnected_filename(s);
485 if (emit_close) {
486 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
488 if (s->reconnect_time) {
489 qemu_chr_socket_restart_timer(chr);
493 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
495 Chardev *chr = CHARDEV(opaque);
496 SocketChardev *s = SOCKET_CHARDEV(opaque);
497 uint8_t buf[CHR_READ_BUF_LEN];
498 int len, size;
500 if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
501 s->max_size <= 0) {
502 return TRUE;
504 len = sizeof(buf);
505 if (len > s->max_size) {
506 len = s->max_size;
508 size = tcp_chr_recv(chr, (void *)buf, len);
509 if (size == 0 || (size == -1 && errno != EAGAIN)) {
510 /* connection closed */
511 tcp_chr_disconnect(chr);
512 } else if (size > 0) {
513 if (s->do_telnetopt) {
514 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
516 if (size > 0) {
517 qemu_chr_be_write(chr, buf, size);
521 return TRUE;
524 static gboolean tcp_chr_hup(QIOChannel *channel,
525 GIOCondition cond,
526 void *opaque)
528 Chardev *chr = CHARDEV(opaque);
529 tcp_chr_disconnect(chr);
530 return G_SOURCE_REMOVE;
533 static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
535 SocketChardev *s = SOCKET_CHARDEV(chr);
536 int size;
538 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
539 return 0;
542 qio_channel_set_blocking(s->ioc, true, NULL);
543 size = tcp_chr_recv(chr, (void *) buf, len);
544 qio_channel_set_blocking(s->ioc, false, NULL);
545 if (size == 0) {
546 /* connection closed */
547 tcp_chr_disconnect(chr);
550 return size;
553 static char *qemu_chr_compute_filename(SocketChardev *s)
555 struct sockaddr_storage *ss = &s->sioc->localAddr;
556 struct sockaddr_storage *ps = &s->sioc->remoteAddr;
557 socklen_t ss_len = s->sioc->localAddrLen;
558 socklen_t ps_len = s->sioc->remoteAddrLen;
559 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
560 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
561 const char *left = "", *right = "";
563 switch (ss->ss_family) {
564 #ifndef _WIN32
565 case AF_UNIX:
566 return g_strdup_printf("unix:%s%s",
567 ((struct sockaddr_un *)(ss))->sun_path,
568 s->is_listen ? ",server" : "");
569 #endif
570 case AF_INET6:
571 left = "[";
572 right = "]";
573 /* fall through */
574 case AF_INET:
575 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
576 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
577 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
578 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
579 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
580 qemu_chr_socket_protocol(s),
581 left, shost, right, sserv,
582 s->is_listen ? ",server" : "",
583 left, phost, right, pserv);
585 default:
586 return g_strdup_printf("unknown");
590 static void update_ioc_handlers(SocketChardev *s)
592 Chardev *chr = CHARDEV(s);
594 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
595 return;
598 remove_fd_in_watch(chr);
599 chr->gsource = io_add_watch_poll(chr, s->ioc,
600 tcp_chr_read_poll,
601 tcp_chr_read, chr,
602 chr->gcontext);
604 remove_hup_source(s);
605 s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
606 g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
607 chr, NULL);
608 g_source_attach(s->hup_source, chr->gcontext);
611 static void tcp_chr_connect(void *opaque)
613 Chardev *chr = CHARDEV(opaque);
614 SocketChardev *s = SOCKET_CHARDEV(opaque);
616 g_free(chr->filename);
617 chr->filename = qemu_chr_compute_filename(s);
619 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTED);
620 update_ioc_handlers(s);
621 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
624 static void tcp_chr_telnet_destroy(SocketChardev *s)
626 if (s->telnet_source) {
627 g_source_destroy(s->telnet_source);
628 g_source_unref(s->telnet_source);
629 s->telnet_source = NULL;
633 static void tcp_chr_update_read_handler(Chardev *chr)
635 SocketChardev *s = SOCKET_CHARDEV(chr);
637 if (s->listener && s->state == TCP_CHARDEV_STATE_DISCONNECTED) {
639 * It's possible that chardev context is changed in
640 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
641 * listener if there is.
643 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
644 chr, NULL, chr->gcontext);
647 if (s->telnet_source) {
648 tcp_chr_telnet_init(CHARDEV(s));
651 update_ioc_handlers(s);
654 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
655 GIOCondition cond G_GNUC_UNUSED,
656 gpointer user_data)
658 SocketChardev *s = user_data;
659 Chardev *chr = CHARDEV(s);
660 TCPChardevTelnetInit *init = s->telnet_init;
661 ssize_t ret;
663 assert(init);
665 ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
666 if (ret < 0) {
667 if (ret == QIO_CHANNEL_ERR_BLOCK) {
668 ret = 0;
669 } else {
670 tcp_chr_disconnect(chr);
671 goto end;
674 init->buflen -= ret;
676 if (init->buflen == 0) {
677 tcp_chr_connect(chr);
678 goto end;
681 memmove(init->buf, init->buf + ret, init->buflen);
683 return G_SOURCE_CONTINUE;
685 end:
686 g_free(s->telnet_init);
687 s->telnet_init = NULL;
688 g_source_unref(s->telnet_source);
689 s->telnet_source = NULL;
690 return G_SOURCE_REMOVE;
693 static void tcp_chr_telnet_init(Chardev *chr)
695 SocketChardev *s = SOCKET_CHARDEV(chr);
696 TCPChardevTelnetInit *init;
697 size_t n = 0;
699 /* Destroy existing task */
700 tcp_chr_telnet_destroy(s);
702 if (s->telnet_init) {
703 /* We are possibly during a handshake already */
704 goto cont;
707 s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
708 init = s->telnet_init;
710 #define IACSET(x, a, b, c) \
711 do { \
712 x[n++] = a; \
713 x[n++] = b; \
714 x[n++] = c; \
715 } while (0)
717 if (!s->is_tn3270) {
718 init->buflen = 12;
719 /* Prep the telnet negotion to put telnet in binary,
720 * no echo, single char mode */
721 IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
722 IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
723 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
724 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
725 } else {
726 init->buflen = 21;
727 /* Prep the TN3270 negotion based on RFC1576 */
728 IACSET(init->buf, 0xff, 0xfd, 0x19); /* IAC DO EOR */
729 IACSET(init->buf, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
730 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
731 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
732 IACSET(init->buf, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
733 IACSET(init->buf, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
734 IACSET(init->buf, 0x01, 0xff, 0xf0); /* SEND IAC SE */
737 #undef IACSET
739 cont:
740 s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
741 tcp_chr_telnet_init_io,
742 s, NULL,
743 chr->gcontext);
747 static void tcp_chr_websock_handshake(QIOTask *task, gpointer user_data)
749 Chardev *chr = user_data;
750 SocketChardev *s = user_data;
752 if (qio_task_propagate_error(task, NULL)) {
753 tcp_chr_disconnect(chr);
754 } else {
755 if (s->do_telnetopt) {
756 tcp_chr_telnet_init(chr);
757 } else {
758 tcp_chr_connect(chr);
764 static void tcp_chr_websock_init(Chardev *chr)
766 SocketChardev *s = SOCKET_CHARDEV(chr);
767 QIOChannelWebsock *wioc = NULL;
768 gchar *name;
770 wioc = qio_channel_websock_new_server(s->ioc);
772 name = g_strdup_printf("chardev-websocket-server-%s", chr->label);
773 qio_channel_set_name(QIO_CHANNEL(wioc), name);
774 g_free(name);
775 object_unref(OBJECT(s->ioc));
776 s->ioc = QIO_CHANNEL(wioc);
778 qio_channel_websock_handshake(wioc, tcp_chr_websock_handshake, chr, NULL);
782 static void tcp_chr_tls_handshake(QIOTask *task,
783 gpointer user_data)
785 Chardev *chr = user_data;
786 SocketChardev *s = user_data;
788 if (qio_task_propagate_error(task, NULL)) {
789 tcp_chr_disconnect(chr);
790 } else {
791 if (s->is_websock) {
792 tcp_chr_websock_init(chr);
793 } else if (s->do_telnetopt) {
794 tcp_chr_telnet_init(chr);
795 } else {
796 tcp_chr_connect(chr);
802 static void tcp_chr_tls_init(Chardev *chr)
804 SocketChardev *s = SOCKET_CHARDEV(chr);
805 QIOChannelTLS *tioc;
806 Error *err = NULL;
807 gchar *name;
809 if (s->is_listen) {
810 tioc = qio_channel_tls_new_server(
811 s->ioc, s->tls_creds,
812 s->tls_authz,
813 &err);
814 } else {
815 tioc = qio_channel_tls_new_client(
816 s->ioc, s->tls_creds,
817 s->addr->u.inet.host,
818 &err);
820 if (tioc == NULL) {
821 error_free(err);
822 tcp_chr_disconnect(chr);
823 return;
825 name = g_strdup_printf("chardev-tls-%s-%s",
826 s->is_listen ? "server" : "client",
827 chr->label);
828 qio_channel_set_name(QIO_CHANNEL(tioc), name);
829 g_free(name);
830 object_unref(OBJECT(s->ioc));
831 s->ioc = QIO_CHANNEL(tioc);
833 qio_channel_tls_handshake(tioc,
834 tcp_chr_tls_handshake,
835 chr,
836 NULL,
837 chr->gcontext);
841 static void tcp_chr_set_client_ioc_name(Chardev *chr,
842 QIOChannelSocket *sioc)
844 SocketChardev *s = SOCKET_CHARDEV(chr);
845 char *name;
846 name = g_strdup_printf("chardev-tcp-%s-%s",
847 s->is_listen ? "server" : "client",
848 chr->label);
849 qio_channel_set_name(QIO_CHANNEL(sioc), name);
850 g_free(name);
854 static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
856 SocketChardev *s = SOCKET_CHARDEV(chr);
858 if (s->state != TCP_CHARDEV_STATE_CONNECTING) {
859 return -1;
862 s->ioc = QIO_CHANNEL(sioc);
863 object_ref(OBJECT(sioc));
864 s->sioc = sioc;
865 object_ref(OBJECT(sioc));
867 qio_channel_set_blocking(s->ioc, false, NULL);
869 if (s->do_nodelay) {
870 qio_channel_set_delay(s->ioc, false);
872 if (s->listener) {
873 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
874 NULL, chr->gcontext);
877 if (s->tls_creds) {
878 tcp_chr_tls_init(chr);
879 } else if (s->is_websock) {
880 tcp_chr_websock_init(chr);
881 } else if (s->do_telnetopt) {
882 tcp_chr_telnet_init(chr);
883 } else {
884 tcp_chr_connect(chr);
887 return 0;
891 static int tcp_chr_add_client(Chardev *chr, int fd)
893 int ret;
894 QIOChannelSocket *sioc;
895 SocketChardev *s = SOCKET_CHARDEV(chr);
897 if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
898 return -1;
901 sioc = qio_channel_socket_new_fd(fd, NULL);
902 if (!sioc) {
903 return -1;
905 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
906 tcp_chr_set_client_ioc_name(chr, sioc);
907 ret = tcp_chr_new_client(chr, sioc);
908 object_unref(OBJECT(sioc));
909 return ret;
912 static void tcp_chr_accept(QIONetListener *listener,
913 QIOChannelSocket *cioc,
914 void *opaque)
916 Chardev *chr = CHARDEV(opaque);
917 SocketChardev *s = SOCKET_CHARDEV(chr);
919 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
920 tcp_chr_set_client_ioc_name(chr, cioc);
921 tcp_chr_new_client(chr, cioc);
925 static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp)
927 SocketChardev *s = SOCKET_CHARDEV(chr);
928 QIOChannelSocket *sioc = qio_channel_socket_new();
929 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
930 tcp_chr_set_client_ioc_name(chr, sioc);
931 if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
932 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
933 object_unref(OBJECT(sioc));
934 return -1;
936 tcp_chr_new_client(chr, sioc);
937 object_unref(OBJECT(sioc));
938 return 0;
942 static void tcp_chr_accept_server_sync(Chardev *chr)
944 SocketChardev *s = SOCKET_CHARDEV(chr);
945 QIOChannelSocket *sioc;
946 info_report("QEMU waiting for connection on: %s",
947 chr->filename);
948 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
949 sioc = qio_net_listener_wait_client(s->listener);
950 tcp_chr_set_client_ioc_name(chr, sioc);
951 tcp_chr_new_client(chr, sioc);
952 object_unref(OBJECT(sioc));
956 static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
958 SocketChardev *s = SOCKET_CHARDEV(chr);
959 const char *opts[] = { "telnet", "tn3270", "websock", "tls-creds" };
960 bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds };
961 size_t i;
963 QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts) != G_N_ELEMENTS(optset));
964 for (i = 0; i < G_N_ELEMENTS(opts); i++) {
965 if (optset[i]) {
966 error_setg(errp,
967 "'%s' option is incompatible with waiting for "
968 "connection completion", opts[i]);
969 return -1;
973 tcp_chr_reconn_timer_cancel(s);
976 * We expect states to be as follows:
978 * - server
979 * - wait -> CONNECTED
980 * - nowait -> DISCONNECTED
981 * - client
982 * - reconnect == 0 -> CONNECTED
983 * - reconnect != 0 -> CONNECTING
986 if (s->state == TCP_CHARDEV_STATE_CONNECTING) {
987 if (!s->connect_task) {
988 error_setg(errp,
989 "Unexpected 'connecting' state without connect task "
990 "while waiting for connection completion");
991 return -1;
994 * tcp_chr_wait_connected should only ever be run from the
995 * main loop thread associated with chr->gcontext, otherwise
996 * qio_task_wait_thread has a dangerous race condition with
997 * free'ing of the s->connect_task object.
999 * Acquiring the main context doesn't 100% prove we're in
1000 * the main loop thread, but it does at least guarantee
1001 * that the main loop won't be executed by another thread
1002 * avoiding the race condition with the task idle callback.
1004 g_main_context_acquire(chr->gcontext);
1005 qio_task_wait_thread(s->connect_task);
1006 g_main_context_release(chr->gcontext);
1009 * The completion callback (qemu_chr_socket_connected) for
1010 * s->connect_task should have set this to NULL by the time
1011 * qio_task_wait_thread has returned.
1013 assert(!s->connect_task);
1016 * NB we are *not* guaranteed to have "s->state == ..CONNECTED"
1017 * at this point as this first connect may be failed, so
1018 * allow the next loop to run regardless.
1022 while (s->state != TCP_CHARDEV_STATE_CONNECTED) {
1023 if (s->is_listen) {
1024 tcp_chr_accept_server_sync(chr);
1025 } else {
1026 Error *err = NULL;
1027 if (tcp_chr_connect_client_sync(chr, &err) < 0) {
1028 if (s->reconnect_time) {
1029 error_free(err);
1030 g_usleep(s->reconnect_time * 1000ULL * 1000ULL);
1031 } else {
1032 error_propagate(errp, err);
1033 return -1;
1039 return 0;
1042 static void char_socket_finalize(Object *obj)
1044 Chardev *chr = CHARDEV(obj);
1045 SocketChardev *s = SOCKET_CHARDEV(obj);
1047 tcp_chr_free_connection(chr);
1048 tcp_chr_reconn_timer_cancel(s);
1049 qapi_free_SocketAddress(s->addr);
1050 tcp_chr_telnet_destroy(s);
1051 g_free(s->telnet_init);
1052 if (s->listener) {
1053 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
1054 NULL, chr->gcontext);
1055 object_unref(OBJECT(s->listener));
1057 if (s->tls_creds) {
1058 object_unref(OBJECT(s->tls_creds));
1060 g_free(s->tls_authz);
1062 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1065 static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
1067 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1068 Chardev *chr = CHARDEV(opaque);
1069 SocketChardev *s = SOCKET_CHARDEV(chr);
1070 Error *err = NULL;
1072 s->connect_task = NULL;
1074 if (qio_task_propagate_error(task, &err)) {
1075 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
1076 check_report_connect_error(chr, err);
1077 error_free(err);
1078 goto cleanup;
1081 s->connect_err_reported = false;
1082 tcp_chr_new_client(chr, sioc);
1084 cleanup:
1085 object_unref(OBJECT(sioc));
1089 static void tcp_chr_connect_client_task(QIOTask *task,
1090 gpointer opaque)
1092 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1093 SocketAddress *addr = opaque;
1094 Error *err = NULL;
1096 qio_channel_socket_connect_sync(ioc, addr, &err);
1098 qio_task_set_error(task, err);
1102 static void tcp_chr_connect_client_async(Chardev *chr)
1104 SocketChardev *s = SOCKET_CHARDEV(chr);
1105 QIOChannelSocket *sioc;
1107 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
1108 sioc = qio_channel_socket_new();
1109 tcp_chr_set_client_ioc_name(chr, sioc);
1111 * Normally code would use the qio_channel_socket_connect_async
1112 * method which uses a QIOTask + qio_task_set_error internally
1113 * to avoid blocking. The tcp_chr_wait_connected method, however,
1114 * needs a way to synchronize with completion of the background
1115 * connect task which can't be done with the QIOChannelSocket
1116 * async APIs. Thus we must use QIOTask directly to implement
1117 * the non-blocking concept locally.
1119 s->connect_task = qio_task_new(OBJECT(sioc),
1120 qemu_chr_socket_connected,
1121 chr, NULL);
1122 qio_task_run_in_thread(s->connect_task,
1123 tcp_chr_connect_client_task,
1124 s->addr,
1125 NULL,
1126 chr->gcontext);
1129 static gboolean socket_reconnect_timeout(gpointer opaque)
1131 Chardev *chr = CHARDEV(opaque);
1132 SocketChardev *s = SOCKET_CHARDEV(opaque);
1134 g_source_unref(s->reconnect_timer);
1135 s->reconnect_timer = NULL;
1137 if (chr->be_open) {
1138 return false;
1141 tcp_chr_connect_client_async(chr);
1143 return false;
1147 static int qmp_chardev_open_socket_server(Chardev *chr,
1148 bool is_telnet,
1149 bool is_waitconnect,
1150 Error **errp)
1152 SocketChardev *s = SOCKET_CHARDEV(chr);
1153 char *name;
1154 if (is_telnet) {
1155 s->do_telnetopt = 1;
1157 s->listener = qio_net_listener_new();
1159 name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
1160 qio_net_listener_set_name(s->listener, name);
1161 g_free(name);
1163 if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0) {
1164 object_unref(OBJECT(s->listener));
1165 s->listener = NULL;
1166 return -1;
1169 qapi_free_SocketAddress(s->addr);
1170 s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
1171 update_disconnected_filename(s);
1173 if (is_waitconnect) {
1174 tcp_chr_accept_server_sync(chr);
1175 } else {
1176 qio_net_listener_set_client_func_full(s->listener,
1177 tcp_chr_accept,
1178 chr, NULL,
1179 chr->gcontext);
1182 return 0;
1186 static int qmp_chardev_open_socket_client(Chardev *chr,
1187 int64_t reconnect,
1188 Error **errp)
1190 SocketChardev *s = SOCKET_CHARDEV(chr);
1192 if (reconnect > 0) {
1193 s->reconnect_time = reconnect;
1194 tcp_chr_connect_client_async(chr);
1195 return 0;
1196 } else {
1197 return tcp_chr_connect_client_sync(chr, errp);
1202 static bool qmp_chardev_validate_socket(ChardevSocket *sock,
1203 SocketAddress *addr,
1204 Error **errp)
1206 /* Validate any options which have a dependency on address type */
1207 switch (addr->type) {
1208 case SOCKET_ADDRESS_TYPE_FD:
1209 if (sock->has_reconnect) {
1210 error_setg(errp,
1211 "'reconnect' option is incompatible with "
1212 "'fd' address type");
1213 return false;
1215 if (sock->has_tls_creds &&
1216 !(sock->has_server && sock->server)) {
1217 error_setg(errp,
1218 "'tls_creds' option is incompatible with "
1219 "'fd' address type as client");
1220 return false;
1222 break;
1224 case SOCKET_ADDRESS_TYPE_UNIX:
1225 if (sock->has_tls_creds) {
1226 error_setg(errp,
1227 "'tls_creds' option is incompatible with "
1228 "'unix' address type");
1229 return false;
1231 break;
1233 case SOCKET_ADDRESS_TYPE_INET:
1234 break;
1236 case SOCKET_ADDRESS_TYPE_VSOCK:
1237 if (sock->has_tls_creds) {
1238 error_setg(errp,
1239 "'tls_creds' option is incompatible with "
1240 "'vsock' address type");
1241 return false;
1244 default:
1245 break;
1248 if (sock->has_tls_authz && !sock->has_tls_creds) {
1249 error_setg(errp, "'tls_authz' option requires 'tls_creds' option");
1250 return false;
1253 /* Validate any options which have a dependancy on client vs server */
1254 if (!sock->has_server || sock->server) {
1255 if (sock->has_reconnect) {
1256 error_setg(errp,
1257 "'reconnect' option is incompatible with "
1258 "socket in server listen mode");
1259 return false;
1261 } else {
1262 if (sock->has_websocket && sock->websocket) {
1263 error_setg(errp, "%s", "Websocket client is not implemented");
1264 return false;
1266 if (sock->has_wait) {
1267 warn_report("'wait' option is deprecated with "
1268 "socket in client connect mode");
1269 if (sock->wait) {
1270 error_setg(errp, "%s",
1271 "'wait' option is incompatible with "
1272 "socket in client connect mode");
1273 return false;
1278 return true;
1282 static void qmp_chardev_open_socket(Chardev *chr,
1283 ChardevBackend *backend,
1284 bool *be_opened,
1285 Error **errp)
1287 SocketChardev *s = SOCKET_CHARDEV(chr);
1288 ChardevSocket *sock = backend->u.socket.data;
1289 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
1290 bool is_listen = sock->has_server ? sock->server : true;
1291 bool is_telnet = sock->has_telnet ? sock->telnet : false;
1292 bool is_tn3270 = sock->has_tn3270 ? sock->tn3270 : false;
1293 bool is_waitconnect = sock->has_wait ? sock->wait : false;
1294 bool is_websock = sock->has_websocket ? sock->websocket : false;
1295 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
1296 SocketAddress *addr;
1298 s->is_listen = is_listen;
1299 s->is_telnet = is_telnet;
1300 s->is_tn3270 = is_tn3270;
1301 s->is_websock = is_websock;
1302 s->do_nodelay = do_nodelay;
1303 if (sock->tls_creds) {
1304 Object *creds;
1305 creds = object_resolve_path_component(
1306 object_get_objects_root(), sock->tls_creds);
1307 if (!creds) {
1308 error_setg(errp, "No TLS credentials with id '%s'",
1309 sock->tls_creds);
1310 return;
1312 s->tls_creds = (QCryptoTLSCreds *)
1313 object_dynamic_cast(creds,
1314 TYPE_QCRYPTO_TLS_CREDS);
1315 if (!s->tls_creds) {
1316 error_setg(errp, "Object with id '%s' is not TLS credentials",
1317 sock->tls_creds);
1318 return;
1320 object_ref(OBJECT(s->tls_creds));
1321 if (is_listen) {
1322 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
1323 error_setg(errp, "%s",
1324 "Expected TLS credentials for server endpoint");
1325 return;
1327 } else {
1328 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
1329 error_setg(errp, "%s",
1330 "Expected TLS credentials for client endpoint");
1331 return;
1335 s->tls_authz = g_strdup(sock->tls_authz);
1337 s->addr = addr = socket_address_flatten(sock->addr);
1339 if (!qmp_chardev_validate_socket(sock, addr, errp)) {
1340 return;
1343 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
1344 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1345 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1346 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
1349 /* be isn't opened until we get a connection */
1350 *be_opened = false;
1352 update_disconnected_filename(s);
1354 if (s->is_listen) {
1355 if (qmp_chardev_open_socket_server(chr, is_telnet || is_tn3270,
1356 is_waitconnect, errp) < 0) {
1357 return;
1359 } else {
1360 if (qmp_chardev_open_socket_client(chr, reconnect, errp) < 0) {
1361 return;
1366 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
1367 Error **errp)
1369 const char *path = qemu_opt_get(opts, "path");
1370 const char *host = qemu_opt_get(opts, "host");
1371 const char *port = qemu_opt_get(opts, "port");
1372 const char *fd = qemu_opt_get(opts, "fd");
1373 SocketAddressLegacy *addr;
1374 ChardevSocket *sock;
1376 if ((!!path + !!fd + !!host) != 1) {
1377 error_setg(errp,
1378 "Exactly one of 'path', 'fd' or 'host' required");
1379 return;
1382 if (host && !port) {
1383 error_setg(errp, "chardev: socket: no port given");
1384 return;
1387 backend->type = CHARDEV_BACKEND_KIND_SOCKET;
1388 sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
1389 qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
1391 sock->has_nodelay = qemu_opt_get(opts, "delay");
1392 sock->nodelay = !qemu_opt_get_bool(opts, "delay", true);
1394 * We have different default to QMP for 'server', hence
1395 * we can't just check for existence of 'server'
1397 sock->has_server = true;
1398 sock->server = qemu_opt_get_bool(opts, "server", false);
1399 sock->has_telnet = qemu_opt_get(opts, "telnet");
1400 sock->telnet = qemu_opt_get_bool(opts, "telnet", false);
1401 sock->has_tn3270 = qemu_opt_get(opts, "tn3270");
1402 sock->tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
1403 sock->has_websocket = qemu_opt_get(opts, "websocket");
1404 sock->websocket = qemu_opt_get_bool(opts, "websocket", false);
1406 * We have different default to QMP for 'wait' when 'server'
1407 * is set, hence we can't just check for existence of 'wait'
1409 sock->has_wait = qemu_opt_find(opts, "wait") || sock->server;
1410 sock->wait = qemu_opt_get_bool(opts, "wait", true);
1411 sock->has_reconnect = qemu_opt_find(opts, "reconnect");
1412 sock->reconnect = qemu_opt_get_number(opts, "reconnect", 0);
1413 sock->has_tls_creds = qemu_opt_get(opts, "tls-creds");
1414 sock->tls_creds = g_strdup(qemu_opt_get(opts, "tls-creds"));
1415 sock->has_tls_authz = qemu_opt_get(opts, "tls-authz");
1416 sock->tls_authz = g_strdup(qemu_opt_get(opts, "tls-authz"));
1418 addr = g_new0(SocketAddressLegacy, 1);
1419 if (path) {
1420 UnixSocketAddress *q_unix;
1421 addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
1422 q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
1423 q_unix->path = g_strdup(path);
1424 } else if (host) {
1425 addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
1426 addr->u.inet.data = g_new(InetSocketAddress, 1);
1427 *addr->u.inet.data = (InetSocketAddress) {
1428 .host = g_strdup(host),
1429 .port = g_strdup(port),
1430 .has_to = qemu_opt_get(opts, "to"),
1431 .to = qemu_opt_get_number(opts, "to", 0),
1432 .has_ipv4 = qemu_opt_get(opts, "ipv4"),
1433 .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
1434 .has_ipv6 = qemu_opt_get(opts, "ipv6"),
1435 .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
1437 } else if (fd) {
1438 addr->type = SOCKET_ADDRESS_LEGACY_KIND_FD;
1439 addr->u.fd.data = g_new(String, 1);
1440 addr->u.fd.data->str = g_strdup(fd);
1441 } else {
1442 g_assert_not_reached();
1444 sock->addr = addr;
1447 static void
1448 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
1449 void *opaque, Error **errp)
1451 SocketChardev *s = SOCKET_CHARDEV(obj);
1453 visit_type_SocketAddress(v, name, &s->addr, errp);
1456 static bool
1457 char_socket_get_connected(Object *obj, Error **errp)
1459 SocketChardev *s = SOCKET_CHARDEV(obj);
1461 return s->state == TCP_CHARDEV_STATE_CONNECTED;
1464 static void char_socket_class_init(ObjectClass *oc, void *data)
1466 ChardevClass *cc = CHARDEV_CLASS(oc);
1468 cc->parse = qemu_chr_parse_socket;
1469 cc->open = qmp_chardev_open_socket;
1470 cc->chr_wait_connected = tcp_chr_wait_connected;
1471 cc->chr_write = tcp_chr_write;
1472 cc->chr_sync_read = tcp_chr_sync_read;
1473 cc->chr_disconnect = tcp_chr_disconnect;
1474 cc->get_msgfds = tcp_get_msgfds;
1475 cc->set_msgfds = tcp_set_msgfds;
1476 cc->chr_add_client = tcp_chr_add_client;
1477 cc->chr_add_watch = tcp_chr_add_watch;
1478 cc->chr_update_read_handler = tcp_chr_update_read_handler;
1480 object_class_property_add(oc, "addr", "SocketAddress",
1481 char_socket_get_addr, NULL,
1482 NULL, NULL, &error_abort);
1484 object_class_property_add_bool(oc, "connected", char_socket_get_connected,
1485 NULL, &error_abort);
1488 static const TypeInfo char_socket_type_info = {
1489 .name = TYPE_CHARDEV_SOCKET,
1490 .parent = TYPE_CHARDEV,
1491 .instance_size = sizeof(SocketChardev),
1492 .instance_finalize = char_socket_finalize,
1493 .class_init = char_socket_class_init,
1496 static void register_types(void)
1498 type_register_static(&char_socket_type_info);
1501 type_init(register_types);