char-socket: return -1 in case of disconnect during tcp_chr_write
[qemu/ar7.git] / chardev / char-socket.c
blob18e762643bb6d3b60fdf41f365a02c4bc04cda0c
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_reportf_err(err,
142 "Unable to connect character device %s: ",
143 chr->label);
144 s->connect_err_reported = true;
146 qemu_chr_socket_restart_timer(chr);
149 static void tcp_chr_accept(QIONetListener *listener,
150 QIOChannelSocket *cioc,
151 void *opaque);
153 static int tcp_chr_read_poll(void *opaque);
154 static void tcp_chr_disconnect_locked(Chardev *chr);
156 /* Called with chr_write_lock held. */
157 static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
159 SocketChardev *s = SOCKET_CHARDEV(chr);
161 if (s->state == TCP_CHARDEV_STATE_CONNECTED) {
162 int ret = io_channel_send_full(s->ioc, buf, len,
163 s->write_msgfds,
164 s->write_msgfds_num);
166 /* free the written msgfds in any cases
167 * other than ret < 0 && errno == EAGAIN
169 if (!(ret < 0 && EAGAIN == errno)
170 && s->write_msgfds_num) {
171 g_free(s->write_msgfds);
172 s->write_msgfds = 0;
173 s->write_msgfds_num = 0;
176 if (ret < 0 && errno != EAGAIN) {
177 if (tcp_chr_read_poll(chr) <= 0) {
178 /* Perform disconnect and return error. */
179 tcp_chr_disconnect_locked(chr);
180 } /* else let the read handler finish it properly */
183 return ret;
184 } else {
185 /* Indicate an error. */
186 errno = EIO;
187 return -1;
191 static int tcp_chr_read_poll(void *opaque)
193 Chardev *chr = CHARDEV(opaque);
194 SocketChardev *s = SOCKET_CHARDEV(opaque);
195 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
196 return 0;
198 s->max_size = qemu_chr_be_can_write(chr);
199 return s->max_size;
202 static void tcp_chr_process_IAC_bytes(Chardev *chr,
203 SocketChardev *s,
204 uint8_t *buf, int *size)
206 /* Handle any telnet or tn3270 client's basic IAC options.
207 * For telnet options, it satisfies char by char mode with no echo.
208 * For tn3270 options, it satisfies binary mode with EOR.
209 * All IAC options will be removed from the buf and the do_opt
210 * pointer will be used to track the state of the width of the
211 * IAC information.
213 * RFC854: "All TELNET commands consist of at least a two byte sequence.
214 * The commands dealing with option negotiation are three byte sequences,
215 * the third byte being the code for the option referenced."
216 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
217 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
218 * for tn3270.
219 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
220 * session, and NOP and IP need to be done later.
223 int i;
224 int j = 0;
226 for (i = 0; i < *size; i++) {
227 if (s->do_telnetopt > 1) {
228 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
229 /* Double IAC means send an IAC */
230 if (j != i) {
231 buf[j] = buf[i];
233 j++;
234 s->do_telnetopt = 1;
235 } else {
236 if ((unsigned char)buf[i] == IAC_BREAK
237 && s->do_telnetopt == 2) {
238 /* Handle IAC break commands by sending a serial break */
239 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
240 s->do_telnetopt++;
241 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
242 || (unsigned char)buf[i] == IAC_SB
243 || (unsigned char)buf[i] == IAC_SE)
244 && s->do_telnetopt == 2) {
245 buf[j++] = IAC;
246 buf[j++] = buf[i];
247 s->do_telnetopt++;
248 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
249 || (unsigned char)buf[i] == IAC_NOP)
250 && s->do_telnetopt == 2) {
251 /* TODO: IP and NOP need to be implemented later. */
252 s->do_telnetopt++;
254 s->do_telnetopt++;
256 if (s->do_telnetopt >= 4) {
257 s->do_telnetopt = 1;
259 } else {
260 if ((unsigned char)buf[i] == IAC) {
261 s->do_telnetopt = 2;
262 } else {
263 if (j != i) {
264 buf[j] = buf[i];
266 j++;
270 *size = j;
273 static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
275 SocketChardev *s = SOCKET_CHARDEV(chr);
277 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
279 assert(num <= TCP_MAX_FDS);
281 if (to_copy) {
282 int i;
284 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
286 /* Close unused fds */
287 for (i = to_copy; i < s->read_msgfds_num; i++) {
288 close(s->read_msgfds[i]);
291 g_free(s->read_msgfds);
292 s->read_msgfds = 0;
293 s->read_msgfds_num = 0;
296 return to_copy;
299 static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
301 SocketChardev *s = SOCKET_CHARDEV(chr);
303 /* clear old pending fd array */
304 g_free(s->write_msgfds);
305 s->write_msgfds = NULL;
306 s->write_msgfds_num = 0;
308 if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
309 !qio_channel_has_feature(s->ioc,
310 QIO_CHANNEL_FEATURE_FD_PASS)) {
311 return -1;
314 if (num) {
315 s->write_msgfds = g_new(int, num);
316 memcpy(s->write_msgfds, fds, num * sizeof(int));
319 s->write_msgfds_num = num;
321 return 0;
324 static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
326 SocketChardev *s = SOCKET_CHARDEV(chr);
327 struct iovec iov = { .iov_base = buf, .iov_len = len };
328 int ret;
329 size_t i;
330 int *msgfds = NULL;
331 size_t msgfds_num = 0;
333 if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
334 ret = qio_channel_readv_full(s->ioc, &iov, 1,
335 &msgfds, &msgfds_num,
336 NULL);
337 } else {
338 ret = qio_channel_readv_full(s->ioc, &iov, 1,
339 NULL, NULL,
340 NULL);
343 if (ret == QIO_CHANNEL_ERR_BLOCK) {
344 errno = EAGAIN;
345 ret = -1;
346 } else if (ret == -1) {
347 errno = EIO;
350 if (msgfds_num) {
351 /* close and clean read_msgfds */
352 for (i = 0; i < s->read_msgfds_num; i++) {
353 close(s->read_msgfds[i]);
356 if (s->read_msgfds_num) {
357 g_free(s->read_msgfds);
360 s->read_msgfds = msgfds;
361 s->read_msgfds_num = msgfds_num;
364 for (i = 0; i < s->read_msgfds_num; i++) {
365 int fd = s->read_msgfds[i];
366 if (fd < 0) {
367 continue;
370 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
371 qemu_set_block(fd);
373 #ifndef MSG_CMSG_CLOEXEC
374 qemu_set_cloexec(fd);
375 #endif
378 return ret;
381 static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
383 SocketChardev *s = SOCKET_CHARDEV(chr);
384 return qio_channel_create_watch(s->ioc, cond);
387 static void remove_hup_source(SocketChardev *s)
389 if (s->hup_source != NULL) {
390 g_source_destroy(s->hup_source);
391 g_source_unref(s->hup_source);
392 s->hup_source = NULL;
396 static void tcp_chr_free_connection(Chardev *chr)
398 SocketChardev *s = SOCKET_CHARDEV(chr);
399 int i;
401 if (s->read_msgfds_num) {
402 for (i = 0; i < s->read_msgfds_num; i++) {
403 close(s->read_msgfds[i]);
405 g_free(s->read_msgfds);
406 s->read_msgfds = NULL;
407 s->read_msgfds_num = 0;
410 remove_hup_source(s);
412 tcp_set_msgfds(chr, NULL, 0);
413 remove_fd_in_watch(chr);
414 object_unref(OBJECT(s->sioc));
415 s->sioc = NULL;
416 object_unref(OBJECT(s->ioc));
417 s->ioc = NULL;
418 g_free(chr->filename);
419 chr->filename = NULL;
420 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
423 static const char *qemu_chr_socket_protocol(SocketChardev *s)
425 if (s->is_telnet) {
426 return "telnet";
428 return s->is_websock ? "websocket" : "tcp";
431 static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)
433 switch (s->addr->type) {
434 case SOCKET_ADDRESS_TYPE_INET:
435 return g_strdup_printf("%s%s:%s:%s%s", prefix,
436 qemu_chr_socket_protocol(s),
437 s->addr->u.inet.host,
438 s->addr->u.inet.port,
439 s->is_listen ? ",server" : "");
440 break;
441 case SOCKET_ADDRESS_TYPE_UNIX:
442 return g_strdup_printf("%sunix:%s%s", prefix,
443 s->addr->u.q_unix.path,
444 s->is_listen ? ",server" : "");
445 break;
446 case SOCKET_ADDRESS_TYPE_FD:
447 return g_strdup_printf("%sfd:%s%s", prefix, s->addr->u.fd.str,
448 s->is_listen ? ",server" : "");
449 break;
450 case SOCKET_ADDRESS_TYPE_VSOCK:
451 return g_strdup_printf("%svsock:%s:%s", prefix,
452 s->addr->u.vsock.cid,
453 s->addr->u.vsock.port);
454 default:
455 abort();
459 static void update_disconnected_filename(SocketChardev *s)
461 Chardev *chr = CHARDEV(s);
463 g_free(chr->filename);
464 if (s->addr) {
465 chr->filename = qemu_chr_socket_address(s, "disconnected:");
466 } else {
467 chr->filename = g_strdup("disconnected:socket");
471 /* NB may be called even if tcp_chr_connect has not been
472 * reached, due to TLS or telnet initialization failure,
473 * so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
474 * This must be called with chr->chr_write_lock held.
476 static void tcp_chr_disconnect_locked(Chardev *chr)
478 SocketChardev *s = SOCKET_CHARDEV(chr);
479 bool emit_close = s->state == TCP_CHARDEV_STATE_CONNECTED;
481 tcp_chr_free_connection(chr);
483 if (s->listener) {
484 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
485 chr, NULL, chr->gcontext);
487 update_disconnected_filename(s);
488 if (emit_close) {
489 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
491 if (s->reconnect_time) {
492 qemu_chr_socket_restart_timer(chr);
496 static void tcp_chr_disconnect(Chardev *chr)
498 qemu_mutex_lock(&chr->chr_write_lock);
499 tcp_chr_disconnect_locked(chr);
500 qemu_mutex_unlock(&chr->chr_write_lock);
503 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
505 Chardev *chr = CHARDEV(opaque);
506 SocketChardev *s = SOCKET_CHARDEV(opaque);
507 uint8_t buf[CHR_READ_BUF_LEN];
508 int len, size;
510 if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
511 s->max_size <= 0) {
512 return TRUE;
514 len = sizeof(buf);
515 if (len > s->max_size) {
516 len = s->max_size;
518 size = tcp_chr_recv(chr, (void *)buf, len);
519 if (size == 0 || (size == -1 && errno != EAGAIN)) {
520 /* connection closed */
521 tcp_chr_disconnect(chr);
522 } else if (size > 0) {
523 if (s->do_telnetopt) {
524 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
526 if (size > 0) {
527 qemu_chr_be_write(chr, buf, size);
531 return TRUE;
534 static gboolean tcp_chr_hup(QIOChannel *channel,
535 GIOCondition cond,
536 void *opaque)
538 Chardev *chr = CHARDEV(opaque);
539 tcp_chr_disconnect(chr);
540 return G_SOURCE_REMOVE;
543 static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
545 SocketChardev *s = SOCKET_CHARDEV(chr);
546 int size;
548 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
549 return 0;
552 qio_channel_set_blocking(s->ioc, true, NULL);
553 size = tcp_chr_recv(chr, (void *) buf, len);
554 qio_channel_set_blocking(s->ioc, false, NULL);
555 if (size == 0) {
556 /* connection closed */
557 tcp_chr_disconnect(chr);
560 return size;
563 static char *qemu_chr_compute_filename(SocketChardev *s)
565 struct sockaddr_storage *ss = &s->sioc->localAddr;
566 struct sockaddr_storage *ps = &s->sioc->remoteAddr;
567 socklen_t ss_len = s->sioc->localAddrLen;
568 socklen_t ps_len = s->sioc->remoteAddrLen;
569 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
570 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
571 const char *left = "", *right = "";
573 switch (ss->ss_family) {
574 #ifndef _WIN32
575 case AF_UNIX:
576 return g_strdup_printf("unix:%s%s",
577 ((struct sockaddr_un *)(ss))->sun_path,
578 s->is_listen ? ",server" : "");
579 #endif
580 case AF_INET6:
581 left = "[";
582 right = "]";
583 /* fall through */
584 case AF_INET:
585 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
586 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
587 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
588 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
589 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
590 qemu_chr_socket_protocol(s),
591 left, shost, right, sserv,
592 s->is_listen ? ",server" : "",
593 left, phost, right, pserv);
595 default:
596 return g_strdup_printf("unknown");
600 static void update_ioc_handlers(SocketChardev *s)
602 Chardev *chr = CHARDEV(s);
604 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
605 return;
608 remove_fd_in_watch(chr);
609 chr->gsource = io_add_watch_poll(chr, s->ioc,
610 tcp_chr_read_poll,
611 tcp_chr_read, chr,
612 chr->gcontext);
614 remove_hup_source(s);
615 s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
616 g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
617 chr, NULL);
618 g_source_attach(s->hup_source, chr->gcontext);
621 static void tcp_chr_connect(void *opaque)
623 Chardev *chr = CHARDEV(opaque);
624 SocketChardev *s = SOCKET_CHARDEV(opaque);
626 g_free(chr->filename);
627 chr->filename = qemu_chr_compute_filename(s);
629 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTED);
630 update_ioc_handlers(s);
631 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
634 static void tcp_chr_telnet_destroy(SocketChardev *s)
636 if (s->telnet_source) {
637 g_source_destroy(s->telnet_source);
638 g_source_unref(s->telnet_source);
639 s->telnet_source = NULL;
643 static void tcp_chr_update_read_handler(Chardev *chr)
645 SocketChardev *s = SOCKET_CHARDEV(chr);
647 if (s->listener && s->state == TCP_CHARDEV_STATE_DISCONNECTED) {
649 * It's possible that chardev context is changed in
650 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
651 * listener if there is.
653 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
654 chr, NULL, chr->gcontext);
657 if (s->telnet_source) {
658 tcp_chr_telnet_init(CHARDEV(s));
661 update_ioc_handlers(s);
664 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
665 GIOCondition cond G_GNUC_UNUSED,
666 gpointer user_data)
668 SocketChardev *s = user_data;
669 Chardev *chr = CHARDEV(s);
670 TCPChardevTelnetInit *init = s->telnet_init;
671 ssize_t ret;
673 assert(init);
675 ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
676 if (ret < 0) {
677 if (ret == QIO_CHANNEL_ERR_BLOCK) {
678 ret = 0;
679 } else {
680 tcp_chr_disconnect(chr);
681 goto end;
684 init->buflen -= ret;
686 if (init->buflen == 0) {
687 tcp_chr_connect(chr);
688 goto end;
691 memmove(init->buf, init->buf + ret, init->buflen);
693 return G_SOURCE_CONTINUE;
695 end:
696 g_free(s->telnet_init);
697 s->telnet_init = NULL;
698 g_source_unref(s->telnet_source);
699 s->telnet_source = NULL;
700 return G_SOURCE_REMOVE;
703 static void tcp_chr_telnet_init(Chardev *chr)
705 SocketChardev *s = SOCKET_CHARDEV(chr);
706 TCPChardevTelnetInit *init;
707 size_t n = 0;
709 /* Destroy existing task */
710 tcp_chr_telnet_destroy(s);
712 if (s->telnet_init) {
713 /* We are possibly during a handshake already */
714 goto cont;
717 s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
718 init = s->telnet_init;
720 #define IACSET(x, a, b, c) \
721 do { \
722 x[n++] = a; \
723 x[n++] = b; \
724 x[n++] = c; \
725 } while (0)
727 if (!s->is_tn3270) {
728 init->buflen = 12;
729 /* Prep the telnet negotion to put telnet in binary,
730 * no echo, single char mode */
731 IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
732 IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
733 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
734 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
735 } else {
736 init->buflen = 21;
737 /* Prep the TN3270 negotion based on RFC1576 */
738 IACSET(init->buf, 0xff, 0xfd, 0x19); /* IAC DO EOR */
739 IACSET(init->buf, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
740 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
741 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
742 IACSET(init->buf, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
743 IACSET(init->buf, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
744 IACSET(init->buf, 0x01, 0xff, 0xf0); /* SEND IAC SE */
747 #undef IACSET
749 cont:
750 s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
751 tcp_chr_telnet_init_io,
752 s, NULL,
753 chr->gcontext);
757 static void tcp_chr_websock_handshake(QIOTask *task, gpointer user_data)
759 Chardev *chr = user_data;
760 SocketChardev *s = user_data;
762 if (qio_task_propagate_error(task, NULL)) {
763 tcp_chr_disconnect(chr);
764 } else {
765 if (s->do_telnetopt) {
766 tcp_chr_telnet_init(chr);
767 } else {
768 tcp_chr_connect(chr);
774 static void tcp_chr_websock_init(Chardev *chr)
776 SocketChardev *s = SOCKET_CHARDEV(chr);
777 QIOChannelWebsock *wioc = NULL;
778 gchar *name;
780 wioc = qio_channel_websock_new_server(s->ioc);
782 name = g_strdup_printf("chardev-websocket-server-%s", chr->label);
783 qio_channel_set_name(QIO_CHANNEL(wioc), name);
784 g_free(name);
785 object_unref(OBJECT(s->ioc));
786 s->ioc = QIO_CHANNEL(wioc);
788 qio_channel_websock_handshake(wioc, tcp_chr_websock_handshake, chr, NULL);
792 static void tcp_chr_tls_handshake(QIOTask *task,
793 gpointer user_data)
795 Chardev *chr = user_data;
796 SocketChardev *s = user_data;
798 if (qio_task_propagate_error(task, NULL)) {
799 tcp_chr_disconnect(chr);
800 } else {
801 if (s->is_websock) {
802 tcp_chr_websock_init(chr);
803 } else if (s->do_telnetopt) {
804 tcp_chr_telnet_init(chr);
805 } else {
806 tcp_chr_connect(chr);
812 static void tcp_chr_tls_init(Chardev *chr)
814 SocketChardev *s = SOCKET_CHARDEV(chr);
815 QIOChannelTLS *tioc;
816 Error *err = NULL;
817 gchar *name;
819 if (s->is_listen) {
820 tioc = qio_channel_tls_new_server(
821 s->ioc, s->tls_creds,
822 s->tls_authz,
823 &err);
824 } else {
825 tioc = qio_channel_tls_new_client(
826 s->ioc, s->tls_creds,
827 s->addr->u.inet.host,
828 &err);
830 if (tioc == NULL) {
831 error_free(err);
832 tcp_chr_disconnect(chr);
833 return;
835 name = g_strdup_printf("chardev-tls-%s-%s",
836 s->is_listen ? "server" : "client",
837 chr->label);
838 qio_channel_set_name(QIO_CHANNEL(tioc), name);
839 g_free(name);
840 object_unref(OBJECT(s->ioc));
841 s->ioc = QIO_CHANNEL(tioc);
843 qio_channel_tls_handshake(tioc,
844 tcp_chr_tls_handshake,
845 chr,
846 NULL,
847 chr->gcontext);
851 static void tcp_chr_set_client_ioc_name(Chardev *chr,
852 QIOChannelSocket *sioc)
854 SocketChardev *s = SOCKET_CHARDEV(chr);
855 char *name;
856 name = g_strdup_printf("chardev-tcp-%s-%s",
857 s->is_listen ? "server" : "client",
858 chr->label);
859 qio_channel_set_name(QIO_CHANNEL(sioc), name);
860 g_free(name);
864 static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
866 SocketChardev *s = SOCKET_CHARDEV(chr);
868 if (s->state != TCP_CHARDEV_STATE_CONNECTING) {
869 return -1;
872 s->ioc = QIO_CHANNEL(sioc);
873 object_ref(OBJECT(sioc));
874 s->sioc = sioc;
875 object_ref(OBJECT(sioc));
877 qio_channel_set_blocking(s->ioc, false, NULL);
879 if (s->do_nodelay) {
880 qio_channel_set_delay(s->ioc, false);
882 if (s->listener) {
883 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
884 NULL, chr->gcontext);
887 if (s->tls_creds) {
888 tcp_chr_tls_init(chr);
889 } else if (s->is_websock) {
890 tcp_chr_websock_init(chr);
891 } else if (s->do_telnetopt) {
892 tcp_chr_telnet_init(chr);
893 } else {
894 tcp_chr_connect(chr);
897 return 0;
901 static int tcp_chr_add_client(Chardev *chr, int fd)
903 int ret;
904 QIOChannelSocket *sioc;
905 SocketChardev *s = SOCKET_CHARDEV(chr);
907 if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
908 return -1;
911 sioc = qio_channel_socket_new_fd(fd, NULL);
912 if (!sioc) {
913 return -1;
915 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
916 tcp_chr_set_client_ioc_name(chr, sioc);
917 ret = tcp_chr_new_client(chr, sioc);
918 object_unref(OBJECT(sioc));
919 return ret;
922 static void tcp_chr_accept(QIONetListener *listener,
923 QIOChannelSocket *cioc,
924 void *opaque)
926 Chardev *chr = CHARDEV(opaque);
927 SocketChardev *s = SOCKET_CHARDEV(chr);
929 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
930 tcp_chr_set_client_ioc_name(chr, cioc);
931 tcp_chr_new_client(chr, cioc);
935 static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp)
937 SocketChardev *s = SOCKET_CHARDEV(chr);
938 QIOChannelSocket *sioc = qio_channel_socket_new();
939 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
940 tcp_chr_set_client_ioc_name(chr, sioc);
941 if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
942 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
943 object_unref(OBJECT(sioc));
944 return -1;
946 tcp_chr_new_client(chr, sioc);
947 object_unref(OBJECT(sioc));
948 return 0;
952 static void tcp_chr_accept_server_sync(Chardev *chr)
954 SocketChardev *s = SOCKET_CHARDEV(chr);
955 QIOChannelSocket *sioc;
956 info_report("QEMU waiting for connection on: %s",
957 chr->filename);
958 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
959 sioc = qio_net_listener_wait_client(s->listener);
960 tcp_chr_set_client_ioc_name(chr, sioc);
961 tcp_chr_new_client(chr, sioc);
962 object_unref(OBJECT(sioc));
966 static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
968 SocketChardev *s = SOCKET_CHARDEV(chr);
969 const char *opts[] = { "telnet", "tn3270", "websock", "tls-creds" };
970 bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds };
971 size_t i;
973 QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts) != G_N_ELEMENTS(optset));
974 for (i = 0; i < G_N_ELEMENTS(opts); i++) {
975 if (optset[i]) {
976 error_setg(errp,
977 "'%s' option is incompatible with waiting for "
978 "connection completion", opts[i]);
979 return -1;
983 tcp_chr_reconn_timer_cancel(s);
986 * We expect states to be as follows:
988 * - server
989 * - wait -> CONNECTED
990 * - nowait -> DISCONNECTED
991 * - client
992 * - reconnect == 0 -> CONNECTED
993 * - reconnect != 0 -> CONNECTING
996 if (s->state == TCP_CHARDEV_STATE_CONNECTING) {
997 if (!s->connect_task) {
998 error_setg(errp,
999 "Unexpected 'connecting' state without connect task "
1000 "while waiting for connection completion");
1001 return -1;
1004 * tcp_chr_wait_connected should only ever be run from the
1005 * main loop thread associated with chr->gcontext, otherwise
1006 * qio_task_wait_thread has a dangerous race condition with
1007 * free'ing of the s->connect_task object.
1009 * Acquiring the main context doesn't 100% prove we're in
1010 * the main loop thread, but it does at least guarantee
1011 * that the main loop won't be executed by another thread
1012 * avoiding the race condition with the task idle callback.
1014 g_main_context_acquire(chr->gcontext);
1015 qio_task_wait_thread(s->connect_task);
1016 g_main_context_release(chr->gcontext);
1019 * The completion callback (qemu_chr_socket_connected) for
1020 * s->connect_task should have set this to NULL by the time
1021 * qio_task_wait_thread has returned.
1023 assert(!s->connect_task);
1026 * NB we are *not* guaranteed to have "s->state == ..CONNECTED"
1027 * at this point as this first connect may be failed, so
1028 * allow the next loop to run regardless.
1032 while (s->state != TCP_CHARDEV_STATE_CONNECTED) {
1033 if (s->is_listen) {
1034 tcp_chr_accept_server_sync(chr);
1035 } else {
1036 Error *err = NULL;
1037 if (tcp_chr_connect_client_sync(chr, &err) < 0) {
1038 if (s->reconnect_time) {
1039 error_free(err);
1040 g_usleep(s->reconnect_time * 1000ULL * 1000ULL);
1041 } else {
1042 error_propagate(errp, err);
1043 return -1;
1049 return 0;
1052 static void char_socket_finalize(Object *obj)
1054 Chardev *chr = CHARDEV(obj);
1055 SocketChardev *s = SOCKET_CHARDEV(obj);
1057 tcp_chr_free_connection(chr);
1058 tcp_chr_reconn_timer_cancel(s);
1059 qapi_free_SocketAddress(s->addr);
1060 tcp_chr_telnet_destroy(s);
1061 g_free(s->telnet_init);
1062 if (s->listener) {
1063 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
1064 NULL, chr->gcontext);
1065 object_unref(OBJECT(s->listener));
1067 if (s->tls_creds) {
1068 object_unref(OBJECT(s->tls_creds));
1070 g_free(s->tls_authz);
1072 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1075 static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
1077 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1078 Chardev *chr = CHARDEV(opaque);
1079 SocketChardev *s = SOCKET_CHARDEV(chr);
1080 Error *err = NULL;
1082 s->connect_task = NULL;
1084 if (qio_task_propagate_error(task, &err)) {
1085 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
1086 check_report_connect_error(chr, err);
1087 error_free(err);
1088 goto cleanup;
1091 s->connect_err_reported = false;
1092 tcp_chr_new_client(chr, sioc);
1094 cleanup:
1095 object_unref(OBJECT(sioc));
1099 static void tcp_chr_connect_client_task(QIOTask *task,
1100 gpointer opaque)
1102 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1103 SocketAddress *addr = opaque;
1104 Error *err = NULL;
1106 qio_channel_socket_connect_sync(ioc, addr, &err);
1108 qio_task_set_error(task, err);
1112 static void tcp_chr_connect_client_async(Chardev *chr)
1114 SocketChardev *s = SOCKET_CHARDEV(chr);
1115 QIOChannelSocket *sioc;
1117 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
1118 sioc = qio_channel_socket_new();
1119 tcp_chr_set_client_ioc_name(chr, sioc);
1121 * Normally code would use the qio_channel_socket_connect_async
1122 * method which uses a QIOTask + qio_task_set_error internally
1123 * to avoid blocking. The tcp_chr_wait_connected method, however,
1124 * needs a way to synchronize with completion of the background
1125 * connect task which can't be done with the QIOChannelSocket
1126 * async APIs. Thus we must use QIOTask directly to implement
1127 * the non-blocking concept locally.
1129 s->connect_task = qio_task_new(OBJECT(sioc),
1130 qemu_chr_socket_connected,
1131 chr, NULL);
1132 qio_task_run_in_thread(s->connect_task,
1133 tcp_chr_connect_client_task,
1134 s->addr,
1135 NULL,
1136 chr->gcontext);
1139 static gboolean socket_reconnect_timeout(gpointer opaque)
1141 Chardev *chr = CHARDEV(opaque);
1142 SocketChardev *s = SOCKET_CHARDEV(opaque);
1144 qemu_mutex_lock(&chr->chr_write_lock);
1145 g_source_unref(s->reconnect_timer);
1146 s->reconnect_timer = NULL;
1147 qemu_mutex_unlock(&chr->chr_write_lock);
1149 if (chr->be_open) {
1150 return false;
1153 tcp_chr_connect_client_async(chr);
1155 return false;
1159 static int qmp_chardev_open_socket_server(Chardev *chr,
1160 bool is_telnet,
1161 bool is_waitconnect,
1162 Error **errp)
1164 SocketChardev *s = SOCKET_CHARDEV(chr);
1165 char *name;
1166 if (is_telnet) {
1167 s->do_telnetopt = 1;
1169 s->listener = qio_net_listener_new();
1171 name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
1172 qio_net_listener_set_name(s->listener, name);
1173 g_free(name);
1175 if (qio_net_listener_open_sync(s->listener, s->addr, 1, errp) < 0) {
1176 object_unref(OBJECT(s->listener));
1177 s->listener = NULL;
1178 return -1;
1181 qapi_free_SocketAddress(s->addr);
1182 s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
1183 update_disconnected_filename(s);
1185 if (is_waitconnect) {
1186 tcp_chr_accept_server_sync(chr);
1187 } else {
1188 qio_net_listener_set_client_func_full(s->listener,
1189 tcp_chr_accept,
1190 chr, NULL,
1191 chr->gcontext);
1194 return 0;
1198 static int qmp_chardev_open_socket_client(Chardev *chr,
1199 int64_t reconnect,
1200 Error **errp)
1202 SocketChardev *s = SOCKET_CHARDEV(chr);
1204 if (reconnect > 0) {
1205 s->reconnect_time = reconnect;
1206 tcp_chr_connect_client_async(chr);
1207 return 0;
1208 } else {
1209 return tcp_chr_connect_client_sync(chr, errp);
1214 static bool qmp_chardev_validate_socket(ChardevSocket *sock,
1215 SocketAddress *addr,
1216 Error **errp)
1218 /* Validate any options which have a dependency on address type */
1219 switch (addr->type) {
1220 case SOCKET_ADDRESS_TYPE_FD:
1221 if (sock->has_reconnect) {
1222 error_setg(errp,
1223 "'reconnect' option is incompatible with "
1224 "'fd' address type");
1225 return false;
1227 if (sock->has_tls_creds &&
1228 !(sock->has_server && sock->server)) {
1229 error_setg(errp,
1230 "'tls_creds' option is incompatible with "
1231 "'fd' address type as client");
1232 return false;
1234 break;
1236 case SOCKET_ADDRESS_TYPE_UNIX:
1237 if (sock->has_tls_creds) {
1238 error_setg(errp,
1239 "'tls_creds' option is incompatible with "
1240 "'unix' address type");
1241 return false;
1243 break;
1245 case SOCKET_ADDRESS_TYPE_INET:
1246 break;
1248 case SOCKET_ADDRESS_TYPE_VSOCK:
1249 if (sock->has_tls_creds) {
1250 error_setg(errp,
1251 "'tls_creds' option is incompatible with "
1252 "'vsock' address type");
1253 return false;
1256 default:
1257 break;
1260 if (sock->has_tls_authz && !sock->has_tls_creds) {
1261 error_setg(errp, "'tls_authz' option requires 'tls_creds' option");
1262 return false;
1265 /* Validate any options which have a dependancy on client vs server */
1266 if (!sock->has_server || sock->server) {
1267 if (sock->has_reconnect) {
1268 error_setg(errp,
1269 "'reconnect' option is incompatible with "
1270 "socket in server listen mode");
1271 return false;
1273 } else {
1274 if (sock->has_websocket && sock->websocket) {
1275 error_setg(errp, "%s", "Websocket client is not implemented");
1276 return false;
1278 if (sock->has_wait) {
1279 warn_report("'wait' option is deprecated with "
1280 "socket in client connect mode");
1281 if (sock->wait) {
1282 error_setg(errp, "%s",
1283 "'wait' option is incompatible with "
1284 "socket in client connect mode");
1285 return false;
1290 return true;
1294 static void qmp_chardev_open_socket(Chardev *chr,
1295 ChardevBackend *backend,
1296 bool *be_opened,
1297 Error **errp)
1299 SocketChardev *s = SOCKET_CHARDEV(chr);
1300 ChardevSocket *sock = backend->u.socket.data;
1301 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
1302 bool is_listen = sock->has_server ? sock->server : true;
1303 bool is_telnet = sock->has_telnet ? sock->telnet : false;
1304 bool is_tn3270 = sock->has_tn3270 ? sock->tn3270 : false;
1305 bool is_waitconnect = sock->has_wait ? sock->wait : false;
1306 bool is_websock = sock->has_websocket ? sock->websocket : false;
1307 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
1308 SocketAddress *addr;
1310 s->is_listen = is_listen;
1311 s->is_telnet = is_telnet;
1312 s->is_tn3270 = is_tn3270;
1313 s->is_websock = is_websock;
1314 s->do_nodelay = do_nodelay;
1315 if (sock->tls_creds) {
1316 Object *creds;
1317 creds = object_resolve_path_component(
1318 object_get_objects_root(), sock->tls_creds);
1319 if (!creds) {
1320 error_setg(errp, "No TLS credentials with id '%s'",
1321 sock->tls_creds);
1322 return;
1324 s->tls_creds = (QCryptoTLSCreds *)
1325 object_dynamic_cast(creds,
1326 TYPE_QCRYPTO_TLS_CREDS);
1327 if (!s->tls_creds) {
1328 error_setg(errp, "Object with id '%s' is not TLS credentials",
1329 sock->tls_creds);
1330 return;
1332 object_ref(OBJECT(s->tls_creds));
1333 if (is_listen) {
1334 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
1335 error_setg(errp, "%s",
1336 "Expected TLS credentials for server endpoint");
1337 return;
1339 } else {
1340 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
1341 error_setg(errp, "%s",
1342 "Expected TLS credentials for client endpoint");
1343 return;
1347 s->tls_authz = g_strdup(sock->tls_authz);
1349 s->addr = addr = socket_address_flatten(sock->addr);
1351 if (!qmp_chardev_validate_socket(sock, addr, errp)) {
1352 return;
1355 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
1356 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1357 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1358 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
1361 /* be isn't opened until we get a connection */
1362 *be_opened = false;
1364 update_disconnected_filename(s);
1366 if (s->is_listen) {
1367 if (qmp_chardev_open_socket_server(chr, is_telnet || is_tn3270,
1368 is_waitconnect, errp) < 0) {
1369 return;
1371 } else {
1372 if (qmp_chardev_open_socket_client(chr, reconnect, errp) < 0) {
1373 return;
1378 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
1379 Error **errp)
1381 const char *path = qemu_opt_get(opts, "path");
1382 const char *host = qemu_opt_get(opts, "host");
1383 const char *port = qemu_opt_get(opts, "port");
1384 const char *fd = qemu_opt_get(opts, "fd");
1385 bool tight = qemu_opt_get_bool(opts, "tight", true);
1386 bool abstract = qemu_opt_get_bool(opts, "abstract", false);
1387 SocketAddressLegacy *addr;
1388 ChardevSocket *sock;
1390 if ((!!path + !!fd + !!host) != 1) {
1391 error_setg(errp,
1392 "Exactly one of 'path', 'fd' or 'host' required");
1393 return;
1396 if (host && !port) {
1397 error_setg(errp, "chardev: socket: no port given");
1398 return;
1401 backend->type = CHARDEV_BACKEND_KIND_SOCKET;
1402 sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
1403 qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
1405 sock->has_nodelay = qemu_opt_get(opts, "delay");
1406 sock->nodelay = !qemu_opt_get_bool(opts, "delay", true);
1408 * We have different default to QMP for 'server', hence
1409 * we can't just check for existence of 'server'
1411 sock->has_server = true;
1412 sock->server = qemu_opt_get_bool(opts, "server", false);
1413 sock->has_telnet = qemu_opt_get(opts, "telnet");
1414 sock->telnet = qemu_opt_get_bool(opts, "telnet", false);
1415 sock->has_tn3270 = qemu_opt_get(opts, "tn3270");
1416 sock->tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
1417 sock->has_websocket = qemu_opt_get(opts, "websocket");
1418 sock->websocket = qemu_opt_get_bool(opts, "websocket", false);
1420 * We have different default to QMP for 'wait' when 'server'
1421 * is set, hence we can't just check for existence of 'wait'
1423 sock->has_wait = qemu_opt_find(opts, "wait") || sock->server;
1424 sock->wait = qemu_opt_get_bool(opts, "wait", true);
1425 sock->has_reconnect = qemu_opt_find(opts, "reconnect");
1426 sock->reconnect = qemu_opt_get_number(opts, "reconnect", 0);
1427 sock->has_tls_creds = qemu_opt_get(opts, "tls-creds");
1428 sock->tls_creds = g_strdup(qemu_opt_get(opts, "tls-creds"));
1429 sock->has_tls_authz = qemu_opt_get(opts, "tls-authz");
1430 sock->tls_authz = g_strdup(qemu_opt_get(opts, "tls-authz"));
1432 addr = g_new0(SocketAddressLegacy, 1);
1433 if (path) {
1434 UnixSocketAddress *q_unix;
1435 addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
1436 q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
1437 q_unix->path = g_strdup(path);
1438 q_unix->tight = tight;
1439 q_unix->abstract = abstract;
1440 } else if (host) {
1441 addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
1442 addr->u.inet.data = g_new(InetSocketAddress, 1);
1443 *addr->u.inet.data = (InetSocketAddress) {
1444 .host = g_strdup(host),
1445 .port = g_strdup(port),
1446 .has_to = qemu_opt_get(opts, "to"),
1447 .to = qemu_opt_get_number(opts, "to", 0),
1448 .has_ipv4 = qemu_opt_get(opts, "ipv4"),
1449 .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
1450 .has_ipv6 = qemu_opt_get(opts, "ipv6"),
1451 .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
1453 } else if (fd) {
1454 addr->type = SOCKET_ADDRESS_LEGACY_KIND_FD;
1455 addr->u.fd.data = g_new(String, 1);
1456 addr->u.fd.data->str = g_strdup(fd);
1457 } else {
1458 g_assert_not_reached();
1460 sock->addr = addr;
1463 static void
1464 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
1465 void *opaque, Error **errp)
1467 SocketChardev *s = SOCKET_CHARDEV(obj);
1469 visit_type_SocketAddress(v, name, &s->addr, errp);
1472 static bool
1473 char_socket_get_connected(Object *obj, Error **errp)
1475 SocketChardev *s = SOCKET_CHARDEV(obj);
1477 return s->state == TCP_CHARDEV_STATE_CONNECTED;
1480 static void char_socket_class_init(ObjectClass *oc, void *data)
1482 ChardevClass *cc = CHARDEV_CLASS(oc);
1484 cc->parse = qemu_chr_parse_socket;
1485 cc->open = qmp_chardev_open_socket;
1486 cc->chr_wait_connected = tcp_chr_wait_connected;
1487 cc->chr_write = tcp_chr_write;
1488 cc->chr_sync_read = tcp_chr_sync_read;
1489 cc->chr_disconnect = tcp_chr_disconnect;
1490 cc->get_msgfds = tcp_get_msgfds;
1491 cc->set_msgfds = tcp_set_msgfds;
1492 cc->chr_add_client = tcp_chr_add_client;
1493 cc->chr_add_watch = tcp_chr_add_watch;
1494 cc->chr_update_read_handler = tcp_chr_update_read_handler;
1496 object_class_property_add(oc, "addr", "SocketAddress",
1497 char_socket_get_addr, NULL,
1498 NULL, NULL);
1500 object_class_property_add_bool(oc, "connected", char_socket_get_connected,
1501 NULL);
1504 static const TypeInfo char_socket_type_info = {
1505 .name = TYPE_CHARDEV_SOCKET,
1506 .parent = TYPE_CHARDEV,
1507 .instance_size = sizeof(SocketChardev),
1508 .instance_finalize = char_socket_finalize,
1509 .class_init = char_socket_class_init,
1512 static void register_types(void)
1514 type_register_static(&char_socket_type_info);
1517 type_init(register_types);