hw/display/cirrus_vga: Move "isa-cirrus-vga" device into a separate file
[qemu/ar7.git] / chardev / char-socket.c
bloba75b46d9fe2d2847d1a24f43887a44775bb67d58
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/net-listener.h"
30 #include "qemu/error-report.h"
31 #include "qemu/option.h"
32 #include "qapi/error.h"
33 #include "qapi/clone-visitor.h"
34 #include "qapi/qapi-visit-sockets.h"
36 #include "chardev/char-io.h"
38 /***********************************************************/
39 /* TCP Net console */
41 #define TCP_MAX_FDS 16
43 typedef struct {
44 char buf[21];
45 size_t buflen;
46 } TCPChardevTelnetInit;
48 typedef struct {
49 Chardev parent;
50 QIOChannel *ioc; /* Client I/O channel */
51 QIOChannelSocket *sioc; /* Client master channel */
52 QIONetListener *listener;
53 GSource *hup_source;
54 QCryptoTLSCreds *tls_creds;
55 int connected;
56 int max_size;
57 int do_telnetopt;
58 int do_nodelay;
59 int *read_msgfds;
60 size_t read_msgfds_num;
61 int *write_msgfds;
62 size_t write_msgfds_num;
64 SocketAddress *addr;
65 bool is_listen;
66 bool is_telnet;
67 bool is_tn3270;
68 GSource *telnet_source;
69 TCPChardevTelnetInit *telnet_init;
71 GSource *reconnect_timer;
72 int64_t reconnect_time;
73 bool connect_err_reported;
74 } SocketChardev;
76 #define SOCKET_CHARDEV(obj) \
77 OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
79 static gboolean socket_reconnect_timeout(gpointer opaque);
80 static void tcp_chr_telnet_init(Chardev *chr);
82 static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
84 if (s->reconnect_timer) {
85 g_source_destroy(s->reconnect_timer);
86 g_source_unref(s->reconnect_timer);
87 s->reconnect_timer = NULL;
91 static void qemu_chr_socket_restart_timer(Chardev *chr)
93 SocketChardev *s = SOCKET_CHARDEV(chr);
94 char *name;
96 assert(s->connected == 0);
97 name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
98 s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
99 s->reconnect_time * 1000,
100 socket_reconnect_timeout,
101 chr);
102 g_source_set_name(s->reconnect_timer, name);
103 g_free(name);
106 static void check_report_connect_error(Chardev *chr,
107 Error *err)
109 SocketChardev *s = SOCKET_CHARDEV(chr);
111 if (!s->connect_err_reported) {
112 error_report("Unable to connect character device %s: %s",
113 chr->label, error_get_pretty(err));
114 s->connect_err_reported = true;
116 qemu_chr_socket_restart_timer(chr);
119 static void tcp_chr_accept(QIONetListener *listener,
120 QIOChannelSocket *cioc,
121 void *opaque);
123 static int tcp_chr_read_poll(void *opaque);
124 static void tcp_chr_disconnect(Chardev *chr);
126 /* Called with chr_write_lock held. */
127 static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
129 SocketChardev *s = SOCKET_CHARDEV(chr);
131 if (s->connected) {
132 int ret = io_channel_send_full(s->ioc, buf, len,
133 s->write_msgfds,
134 s->write_msgfds_num);
136 /* free the written msgfds in any cases
137 * other than ret < 0 && errno == EAGAIN
139 if (!(ret < 0 && EAGAIN == errno)
140 && s->write_msgfds_num) {
141 g_free(s->write_msgfds);
142 s->write_msgfds = 0;
143 s->write_msgfds_num = 0;
146 if (ret < 0 && errno != EAGAIN) {
147 if (tcp_chr_read_poll(chr) <= 0) {
148 tcp_chr_disconnect(chr);
149 return len;
150 } /* else let the read handler finish it properly */
153 return ret;
154 } else {
155 /* XXX: indicate an error ? */
156 return len;
160 static int tcp_chr_read_poll(void *opaque)
162 Chardev *chr = CHARDEV(opaque);
163 SocketChardev *s = SOCKET_CHARDEV(opaque);
164 if (!s->connected) {
165 return 0;
167 s->max_size = qemu_chr_be_can_write(chr);
168 return s->max_size;
171 static void tcp_chr_process_IAC_bytes(Chardev *chr,
172 SocketChardev *s,
173 uint8_t *buf, int *size)
175 /* Handle any telnet or tn3270 client's basic IAC options.
176 * For telnet options, it satisfies char by char mode with no echo.
177 * For tn3270 options, it satisfies binary mode with EOR.
178 * All IAC options will be removed from the buf and the do_opt
179 * pointer will be used to track the state of the width of the
180 * IAC information.
182 * RFC854: "All TELNET commands consist of at least a two byte sequence.
183 * The commands dealing with option negotiation are three byte sequences,
184 * the third byte being the code for the option referenced."
185 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
186 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
187 * for tn3270.
188 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
189 * session, and NOP and IP need to be done later.
192 int i;
193 int j = 0;
195 for (i = 0; i < *size; i++) {
196 if (s->do_telnetopt > 1) {
197 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
198 /* Double IAC means send an IAC */
199 if (j != i) {
200 buf[j] = buf[i];
202 j++;
203 s->do_telnetopt = 1;
204 } else {
205 if ((unsigned char)buf[i] == IAC_BREAK
206 && s->do_telnetopt == 2) {
207 /* Handle IAC break commands by sending a serial break */
208 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
209 s->do_telnetopt++;
210 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
211 || (unsigned char)buf[i] == IAC_SB
212 || (unsigned char)buf[i] == IAC_SE)
213 && s->do_telnetopt == 2) {
214 buf[j++] = IAC;
215 buf[j++] = buf[i];
216 s->do_telnetopt++;
217 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
218 || (unsigned char)buf[i] == IAC_NOP)
219 && s->do_telnetopt == 2) {
220 /* TODO: IP and NOP need to be implemented later. */
221 s->do_telnetopt++;
223 s->do_telnetopt++;
225 if (s->do_telnetopt >= 4) {
226 s->do_telnetopt = 1;
228 } else {
229 if ((unsigned char)buf[i] == IAC) {
230 s->do_telnetopt = 2;
231 } else {
232 if (j != i) {
233 buf[j] = buf[i];
235 j++;
239 *size = j;
242 static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
244 SocketChardev *s = SOCKET_CHARDEV(chr);
246 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
248 assert(num <= TCP_MAX_FDS);
250 if (to_copy) {
251 int i;
253 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
255 /* Close unused fds */
256 for (i = to_copy; i < s->read_msgfds_num; i++) {
257 close(s->read_msgfds[i]);
260 g_free(s->read_msgfds);
261 s->read_msgfds = 0;
262 s->read_msgfds_num = 0;
265 return to_copy;
268 static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
270 SocketChardev *s = SOCKET_CHARDEV(chr);
272 /* clear old pending fd array */
273 g_free(s->write_msgfds);
274 s->write_msgfds = NULL;
275 s->write_msgfds_num = 0;
277 if (!s->connected ||
278 !qio_channel_has_feature(s->ioc,
279 QIO_CHANNEL_FEATURE_FD_PASS)) {
280 return -1;
283 if (num) {
284 s->write_msgfds = g_new(int, num);
285 memcpy(s->write_msgfds, fds, num * sizeof(int));
288 s->write_msgfds_num = num;
290 return 0;
293 static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
295 SocketChardev *s = SOCKET_CHARDEV(chr);
296 struct iovec iov = { .iov_base = buf, .iov_len = len };
297 int ret;
298 size_t i;
299 int *msgfds = NULL;
300 size_t msgfds_num = 0;
302 if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
303 ret = qio_channel_readv_full(s->ioc, &iov, 1,
304 &msgfds, &msgfds_num,
305 NULL);
306 } else {
307 ret = qio_channel_readv_full(s->ioc, &iov, 1,
308 NULL, NULL,
309 NULL);
312 if (ret == QIO_CHANNEL_ERR_BLOCK) {
313 errno = EAGAIN;
314 ret = -1;
315 } else if (ret == -1) {
316 errno = EIO;
319 if (msgfds_num) {
320 /* close and clean read_msgfds */
321 for (i = 0; i < s->read_msgfds_num; i++) {
322 close(s->read_msgfds[i]);
325 if (s->read_msgfds_num) {
326 g_free(s->read_msgfds);
329 s->read_msgfds = msgfds;
330 s->read_msgfds_num = msgfds_num;
333 for (i = 0; i < s->read_msgfds_num; i++) {
334 int fd = s->read_msgfds[i];
335 if (fd < 0) {
336 continue;
339 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
340 qemu_set_block(fd);
342 #ifndef MSG_CMSG_CLOEXEC
343 qemu_set_cloexec(fd);
344 #endif
347 return ret;
350 static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
352 SocketChardev *s = SOCKET_CHARDEV(chr);
353 return qio_channel_create_watch(s->ioc, cond);
356 static void remove_hup_source(SocketChardev *s)
358 if (s->hup_source != NULL) {
359 g_source_destroy(s->hup_source);
360 g_source_unref(s->hup_source);
361 s->hup_source = NULL;
365 static void tcp_chr_free_connection(Chardev *chr)
367 SocketChardev *s = SOCKET_CHARDEV(chr);
368 int i;
370 if (s->read_msgfds_num) {
371 for (i = 0; i < s->read_msgfds_num; i++) {
372 close(s->read_msgfds[i]);
374 g_free(s->read_msgfds);
375 s->read_msgfds = NULL;
376 s->read_msgfds_num = 0;
379 remove_hup_source(s);
381 tcp_set_msgfds(chr, NULL, 0);
382 remove_fd_in_watch(chr);
383 object_unref(OBJECT(s->sioc));
384 s->sioc = NULL;
385 object_unref(OBJECT(s->ioc));
386 s->ioc = NULL;
387 g_free(chr->filename);
388 chr->filename = NULL;
389 s->connected = 0;
392 static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr,
393 bool is_listen, bool is_telnet)
395 switch (addr->type) {
396 case SOCKET_ADDRESS_TYPE_INET:
397 return g_strdup_printf("%s%s:%s:%s%s", prefix,
398 is_telnet ? "telnet" : "tcp",
399 addr->u.inet.host,
400 addr->u.inet.port,
401 is_listen ? ",server" : "");
402 break;
403 case SOCKET_ADDRESS_TYPE_UNIX:
404 return g_strdup_printf("%sunix:%s%s", prefix,
405 addr->u.q_unix.path,
406 is_listen ? ",server" : "");
407 break;
408 case SOCKET_ADDRESS_TYPE_FD:
409 return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.str,
410 is_listen ? ",server" : "");
411 break;
412 case SOCKET_ADDRESS_TYPE_VSOCK:
413 return g_strdup_printf("%svsock:%s:%s", prefix,
414 addr->u.vsock.cid,
415 addr->u.vsock.port);
416 default:
417 abort();
421 static void update_disconnected_filename(SocketChardev *s)
423 Chardev *chr = CHARDEV(s);
425 g_free(chr->filename);
426 if (s->addr) {
427 chr->filename = SocketAddress_to_str("disconnected:", s->addr,
428 s->is_listen, s->is_telnet);
429 } else {
430 chr->filename = g_strdup("disconnected:socket");
434 /* NB may be called even if tcp_chr_connect has not been
435 * reached, due to TLS or telnet initialization failure,
436 * so can *not* assume s->connected == true
438 static void tcp_chr_disconnect(Chardev *chr)
440 SocketChardev *s = SOCKET_CHARDEV(chr);
441 bool emit_close = s->connected;
443 tcp_chr_free_connection(chr);
445 if (s->listener) {
446 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
447 chr, NULL, chr->gcontext);
449 update_disconnected_filename(s);
450 if (emit_close) {
451 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
453 if (s->reconnect_time) {
454 qemu_chr_socket_restart_timer(chr);
458 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
460 Chardev *chr = CHARDEV(opaque);
461 SocketChardev *s = SOCKET_CHARDEV(opaque);
462 uint8_t buf[CHR_READ_BUF_LEN];
463 int len, size;
465 if (!s->connected || s->max_size <= 0) {
466 return TRUE;
468 len = sizeof(buf);
469 if (len > s->max_size) {
470 len = s->max_size;
472 size = tcp_chr_recv(chr, (void *)buf, len);
473 if (size == 0 || (size == -1 && errno != EAGAIN)) {
474 /* connection closed */
475 tcp_chr_disconnect(chr);
476 } else if (size > 0) {
477 if (s->do_telnetopt) {
478 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
480 if (size > 0) {
481 qemu_chr_be_write(chr, buf, size);
485 return TRUE;
488 static gboolean tcp_chr_hup(QIOChannel *channel,
489 GIOCondition cond,
490 void *opaque)
492 Chardev *chr = CHARDEV(opaque);
493 tcp_chr_disconnect(chr);
494 return G_SOURCE_REMOVE;
497 static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
499 SocketChardev *s = SOCKET_CHARDEV(chr);
500 int size;
502 if (!s->connected) {
503 return 0;
506 qio_channel_set_blocking(s->ioc, true, NULL);
507 size = tcp_chr_recv(chr, (void *) buf, len);
508 qio_channel_set_blocking(s->ioc, false, NULL);
509 if (size == 0) {
510 /* connection closed */
511 tcp_chr_disconnect(chr);
514 return size;
517 static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
518 struct sockaddr_storage *ps, socklen_t ps_len,
519 bool is_listen, bool is_telnet)
521 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
522 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
523 const char *left = "", *right = "";
525 switch (ss->ss_family) {
526 #ifndef _WIN32
527 case AF_UNIX:
528 return g_strdup_printf("unix:%s%s",
529 ((struct sockaddr_un *)(ss))->sun_path,
530 is_listen ? ",server" : "");
531 #endif
532 case AF_INET6:
533 left = "[";
534 right = "]";
535 /* fall through */
536 case AF_INET:
537 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
538 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
539 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
540 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
541 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
542 is_telnet ? "telnet" : "tcp",
543 left, shost, right, sserv,
544 is_listen ? ",server" : "",
545 left, phost, right, pserv);
547 default:
548 return g_strdup_printf("unknown");
552 static void update_ioc_handlers(SocketChardev *s)
554 Chardev *chr = CHARDEV(s);
556 if (!s->connected) {
557 return;
560 remove_fd_in_watch(chr);
561 chr->gsource = io_add_watch_poll(chr, s->ioc,
562 tcp_chr_read_poll,
563 tcp_chr_read, chr,
564 chr->gcontext);
566 remove_hup_source(s);
567 s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
568 g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
569 chr, NULL);
570 g_source_attach(s->hup_source, chr->gcontext);
573 static void tcp_chr_connect(void *opaque)
575 Chardev *chr = CHARDEV(opaque);
576 SocketChardev *s = SOCKET_CHARDEV(opaque);
578 g_free(chr->filename);
579 chr->filename = sockaddr_to_str(
580 &s->sioc->localAddr, s->sioc->localAddrLen,
581 &s->sioc->remoteAddr, s->sioc->remoteAddrLen,
582 s->is_listen, s->is_telnet);
584 s->connected = 1;
585 update_ioc_handlers(s);
586 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
589 static void tcp_chr_telnet_destroy(SocketChardev *s)
591 if (s->telnet_source) {
592 g_source_destroy(s->telnet_source);
593 g_source_unref(s->telnet_source);
594 s->telnet_source = NULL;
598 static void tcp_chr_update_read_handler(Chardev *chr)
600 SocketChardev *s = SOCKET_CHARDEV(chr);
602 if (s->listener) {
604 * It's possible that chardev context is changed in
605 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
606 * listener if there is.
608 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
609 chr, NULL, chr->gcontext);
612 if (s->telnet_source) {
613 tcp_chr_telnet_init(CHARDEV(s));
616 update_ioc_handlers(s);
619 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
620 GIOCondition cond G_GNUC_UNUSED,
621 gpointer user_data)
623 SocketChardev *s = user_data;
624 Chardev *chr = CHARDEV(s);
625 TCPChardevTelnetInit *init = s->telnet_init;
626 ssize_t ret;
628 assert(init);
630 ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
631 if (ret < 0) {
632 if (ret == QIO_CHANNEL_ERR_BLOCK) {
633 ret = 0;
634 } else {
635 tcp_chr_disconnect(chr);
636 goto end;
639 init->buflen -= ret;
641 if (init->buflen == 0) {
642 tcp_chr_connect(chr);
643 goto end;
646 memmove(init->buf, init->buf + ret, init->buflen);
648 return G_SOURCE_CONTINUE;
650 end:
651 g_free(s->telnet_init);
652 s->telnet_init = NULL;
653 g_source_unref(s->telnet_source);
654 s->telnet_source = NULL;
655 return G_SOURCE_REMOVE;
658 static void tcp_chr_telnet_init(Chardev *chr)
660 SocketChardev *s = SOCKET_CHARDEV(chr);
661 TCPChardevTelnetInit *init;
662 size_t n = 0;
664 /* Destroy existing task */
665 tcp_chr_telnet_destroy(s);
667 if (s->telnet_init) {
668 /* We are possibly during a handshake already */
669 goto cont;
672 s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
673 init = s->telnet_init;
675 #define IACSET(x, a, b, c) \
676 do { \
677 x[n++] = a; \
678 x[n++] = b; \
679 x[n++] = c; \
680 } while (0)
682 if (!s->is_tn3270) {
683 init->buflen = 12;
684 /* Prep the telnet negotion to put telnet in binary,
685 * no echo, single char mode */
686 IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
687 IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
688 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
689 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
690 } else {
691 init->buflen = 21;
692 /* Prep the TN3270 negotion based on RFC1576 */
693 IACSET(init->buf, 0xff, 0xfd, 0x19); /* IAC DO EOR */
694 IACSET(init->buf, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
695 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
696 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
697 IACSET(init->buf, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
698 IACSET(init->buf, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
699 IACSET(init->buf, 0x01, 0xff, 0xf0); /* SEND IAC SE */
702 #undef IACSET
704 cont:
705 s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
706 tcp_chr_telnet_init_io,
707 s, NULL,
708 chr->gcontext);
712 static void tcp_chr_tls_handshake(QIOTask *task,
713 gpointer user_data)
715 Chardev *chr = user_data;
716 SocketChardev *s = user_data;
718 if (qio_task_propagate_error(task, NULL)) {
719 tcp_chr_disconnect(chr);
720 } else {
721 if (s->do_telnetopt) {
722 tcp_chr_telnet_init(chr);
723 } else {
724 tcp_chr_connect(chr);
730 static void tcp_chr_tls_init(Chardev *chr)
732 SocketChardev *s = SOCKET_CHARDEV(chr);
733 QIOChannelTLS *tioc;
734 Error *err = NULL;
735 gchar *name;
737 if (s->is_listen) {
738 tioc = qio_channel_tls_new_server(
739 s->ioc, s->tls_creds,
740 NULL, /* XXX Use an ACL */
741 &err);
742 } else {
743 tioc = qio_channel_tls_new_client(
744 s->ioc, s->tls_creds,
745 s->addr->u.inet.host,
746 &err);
748 if (tioc == NULL) {
749 error_free(err);
750 tcp_chr_disconnect(chr);
751 return;
753 name = g_strdup_printf("chardev-tls-%s-%s",
754 s->is_listen ? "server" : "client",
755 chr->label);
756 qio_channel_set_name(QIO_CHANNEL(tioc), name);
757 g_free(name);
758 object_unref(OBJECT(s->ioc));
759 s->ioc = QIO_CHANNEL(tioc);
761 qio_channel_tls_handshake(tioc,
762 tcp_chr_tls_handshake,
763 chr,
764 NULL,
765 chr->gcontext);
769 static void tcp_chr_set_client_ioc_name(Chardev *chr,
770 QIOChannelSocket *sioc)
772 SocketChardev *s = SOCKET_CHARDEV(chr);
773 char *name;
774 name = g_strdup_printf("chardev-tcp-%s-%s",
775 s->is_listen ? "server" : "client",
776 chr->label);
777 qio_channel_set_name(QIO_CHANNEL(sioc), name);
778 g_free(name);
782 static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
784 SocketChardev *s = SOCKET_CHARDEV(chr);
786 if (s->ioc != NULL) {
787 return -1;
790 s->ioc = QIO_CHANNEL(sioc);
791 object_ref(OBJECT(sioc));
792 s->sioc = sioc;
793 object_ref(OBJECT(sioc));
795 qio_channel_set_blocking(s->ioc, false, NULL);
797 if (s->do_nodelay) {
798 qio_channel_set_delay(s->ioc, false);
800 if (s->listener) {
801 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
802 NULL, chr->gcontext);
805 if (s->tls_creds) {
806 tcp_chr_tls_init(chr);
807 } else {
808 if (s->do_telnetopt) {
809 tcp_chr_telnet_init(chr);
810 } else {
811 tcp_chr_connect(chr);
815 return 0;
819 static int tcp_chr_add_client(Chardev *chr, int fd)
821 int ret;
822 QIOChannelSocket *sioc;
824 sioc = qio_channel_socket_new_fd(fd, NULL);
825 if (!sioc) {
826 return -1;
828 tcp_chr_set_client_ioc_name(chr, sioc);
829 ret = tcp_chr_new_client(chr, sioc);
830 object_unref(OBJECT(sioc));
831 return ret;
834 static void tcp_chr_accept(QIONetListener *listener,
835 QIOChannelSocket *cioc,
836 void *opaque)
838 Chardev *chr = CHARDEV(opaque);
840 tcp_chr_set_client_ioc_name(chr, cioc);
841 tcp_chr_new_client(chr, cioc);
844 static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
846 SocketChardev *s = SOCKET_CHARDEV(chr);
847 QIOChannelSocket *sioc;
849 /* It can't wait on s->connected, since it is set asynchronously
850 * in TLS and telnet cases, only wait for an accepted socket */
851 while (!s->ioc) {
852 if (s->is_listen) {
853 info_report("QEMU waiting for connection on: %s",
854 chr->filename);
855 sioc = qio_net_listener_wait_client(s->listener);
856 tcp_chr_set_client_ioc_name(chr, sioc);
857 tcp_chr_new_client(chr, sioc);
858 object_unref(OBJECT(sioc));
859 } else {
860 sioc = qio_channel_socket_new();
861 tcp_chr_set_client_ioc_name(chr, sioc);
862 if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
863 object_unref(OBJECT(sioc));
864 return -1;
866 tcp_chr_new_client(chr, sioc);
867 object_unref(OBJECT(sioc));
871 return 0;
874 static void char_socket_finalize(Object *obj)
876 Chardev *chr = CHARDEV(obj);
877 SocketChardev *s = SOCKET_CHARDEV(obj);
879 tcp_chr_free_connection(chr);
880 tcp_chr_reconn_timer_cancel(s);
881 qapi_free_SocketAddress(s->addr);
882 tcp_chr_telnet_destroy(s);
883 g_free(s->telnet_init);
884 if (s->listener) {
885 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
886 NULL, chr->gcontext);
887 object_unref(OBJECT(s->listener));
889 if (s->tls_creds) {
890 object_unref(OBJECT(s->tls_creds));
893 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
896 static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
898 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
899 Chardev *chr = CHARDEV(opaque);
900 SocketChardev *s = SOCKET_CHARDEV(chr);
901 Error *err = NULL;
903 if (qio_task_propagate_error(task, &err)) {
904 check_report_connect_error(chr, err);
905 error_free(err);
906 goto cleanup;
909 s->connect_err_reported = false;
910 tcp_chr_new_client(chr, sioc);
912 cleanup:
913 object_unref(OBJECT(sioc));
916 static void tcp_chr_connect_async(Chardev *chr)
918 SocketChardev *s = SOCKET_CHARDEV(chr);
919 QIOChannelSocket *sioc;
921 sioc = qio_channel_socket_new();
922 tcp_chr_set_client_ioc_name(chr, sioc);
923 qio_channel_socket_connect_async(sioc, s->addr,
924 qemu_chr_socket_connected,
925 chr, NULL, chr->gcontext);
928 static gboolean socket_reconnect_timeout(gpointer opaque)
930 Chardev *chr = CHARDEV(opaque);
931 SocketChardev *s = SOCKET_CHARDEV(opaque);
933 g_source_unref(s->reconnect_timer);
934 s->reconnect_timer = NULL;
936 if (chr->be_open) {
937 return false;
940 tcp_chr_connect_async(chr);
942 return false;
945 static void qmp_chardev_open_socket(Chardev *chr,
946 ChardevBackend *backend,
947 bool *be_opened,
948 Error **errp)
950 SocketChardev *s = SOCKET_CHARDEV(chr);
951 ChardevSocket *sock = backend->u.socket.data;
952 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
953 bool is_listen = sock->has_server ? sock->server : true;
954 bool is_telnet = sock->has_telnet ? sock->telnet : false;
955 bool is_tn3270 = sock->has_tn3270 ? sock->tn3270 : false;
956 bool is_waitconnect = sock->has_wait ? sock->wait : false;
957 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
958 QIOChannelSocket *sioc = NULL;
959 SocketAddress *addr;
961 s->is_listen = is_listen;
962 s->is_telnet = is_telnet;
963 s->is_tn3270 = is_tn3270;
964 s->do_nodelay = do_nodelay;
965 if (sock->tls_creds) {
966 Object *creds;
967 creds = object_resolve_path_component(
968 object_get_objects_root(), sock->tls_creds);
969 if (!creds) {
970 error_setg(errp, "No TLS credentials with id '%s'",
971 sock->tls_creds);
972 goto error;
974 s->tls_creds = (QCryptoTLSCreds *)
975 object_dynamic_cast(creds,
976 TYPE_QCRYPTO_TLS_CREDS);
977 if (!s->tls_creds) {
978 error_setg(errp, "Object with id '%s' is not TLS credentials",
979 sock->tls_creds);
980 goto error;
982 object_ref(OBJECT(s->tls_creds));
983 if (is_listen) {
984 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
985 error_setg(errp, "%s",
986 "Expected TLS credentials for server endpoint");
987 goto error;
989 } else {
990 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
991 error_setg(errp, "%s",
992 "Expected TLS credentials for client endpoint");
993 goto error;
998 s->addr = addr = socket_address_flatten(sock->addr);
1000 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
1001 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1002 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1003 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
1006 /* be isn't opened until we get a connection */
1007 *be_opened = false;
1009 update_disconnected_filename(s);
1011 if (is_listen) {
1012 if (is_telnet || is_tn3270) {
1013 s->do_telnetopt = 1;
1015 } else if (reconnect > 0) {
1016 s->reconnect_time = reconnect;
1019 if (s->reconnect_time) {
1020 tcp_chr_connect_async(chr);
1021 } else {
1022 if (s->is_listen) {
1023 char *name;
1024 s->listener = qio_net_listener_new();
1026 name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
1027 qio_net_listener_set_name(s->listener, name);
1028 g_free(name);
1030 if (qio_net_listener_open_sync(s->listener, s->addr, errp) < 0) {
1031 object_unref(OBJECT(s->listener));
1032 s->listener = NULL;
1033 goto error;
1036 qapi_free_SocketAddress(s->addr);
1037 s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
1038 update_disconnected_filename(s);
1040 if (is_waitconnect &&
1041 qemu_chr_wait_connected(chr, errp) < 0) {
1042 return;
1044 if (!s->ioc) {
1045 qio_net_listener_set_client_func_full(s->listener,
1046 tcp_chr_accept,
1047 chr, NULL,
1048 chr->gcontext);
1050 } else if (qemu_chr_wait_connected(chr, errp) < 0) {
1051 goto error;
1055 return;
1057 error:
1058 if (sioc) {
1059 object_unref(OBJECT(sioc));
1063 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
1064 Error **errp)
1066 bool is_listen = qemu_opt_get_bool(opts, "server", false);
1067 bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
1068 bool is_telnet = qemu_opt_get_bool(opts, "telnet", false);
1069 bool is_tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
1070 bool do_nodelay = !qemu_opt_get_bool(opts, "delay", true);
1071 int64_t reconnect = qemu_opt_get_number(opts, "reconnect", 0);
1072 const char *path = qemu_opt_get(opts, "path");
1073 const char *host = qemu_opt_get(opts, "host");
1074 const char *port = qemu_opt_get(opts, "port");
1075 const char *fd = qemu_opt_get(opts, "fd");
1076 const char *tls_creds = qemu_opt_get(opts, "tls-creds");
1077 SocketAddressLegacy *addr;
1078 ChardevSocket *sock;
1080 if ((!!path + !!fd + !!host) != 1) {
1081 error_setg(errp,
1082 "Exactly one of 'path', 'fd' or 'host' required");
1083 return;
1086 backend->type = CHARDEV_BACKEND_KIND_SOCKET;
1087 if (path) {
1088 if (tls_creds) {
1089 error_setg(errp, "TLS can only be used over TCP socket");
1090 return;
1092 } else if (host) {
1093 if (!port) {
1094 error_setg(errp, "chardev: socket: no port given");
1095 return;
1097 } else if (fd) {
1098 /* We don't know what host to validate against when in client mode */
1099 if (tls_creds && !is_listen) {
1100 error_setg(errp, "TLS can not be used with pre-opened client FD");
1101 return;
1103 } else {
1104 g_assert_not_reached();
1107 sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
1108 qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
1110 sock->has_nodelay = true;
1111 sock->nodelay = do_nodelay;
1112 sock->has_server = true;
1113 sock->server = is_listen;
1114 sock->has_telnet = true;
1115 sock->telnet = is_telnet;
1116 sock->has_tn3270 = true;
1117 sock->tn3270 = is_tn3270;
1118 sock->has_wait = true;
1119 sock->wait = is_waitconnect;
1120 sock->has_reconnect = true;
1121 sock->reconnect = reconnect;
1122 sock->tls_creds = g_strdup(tls_creds);
1124 addr = g_new0(SocketAddressLegacy, 1);
1125 if (path) {
1126 UnixSocketAddress *q_unix;
1127 addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
1128 q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
1129 q_unix->path = g_strdup(path);
1130 } else if (host) {
1131 addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
1132 addr->u.inet.data = g_new(InetSocketAddress, 1);
1133 *addr->u.inet.data = (InetSocketAddress) {
1134 .host = g_strdup(host),
1135 .port = g_strdup(port),
1136 .has_to = qemu_opt_get(opts, "to"),
1137 .to = qemu_opt_get_number(opts, "to", 0),
1138 .has_ipv4 = qemu_opt_get(opts, "ipv4"),
1139 .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
1140 .has_ipv6 = qemu_opt_get(opts, "ipv6"),
1141 .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
1143 } else if (fd) {
1144 addr->type = SOCKET_ADDRESS_LEGACY_KIND_FD;
1145 addr->u.fd.data = g_new(String, 1);
1146 addr->u.fd.data->str = g_strdup(fd);
1147 } else {
1148 g_assert_not_reached();
1150 sock->addr = addr;
1153 static void
1154 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
1155 void *opaque, Error **errp)
1157 SocketChardev *s = SOCKET_CHARDEV(obj);
1159 visit_type_SocketAddress(v, name, &s->addr, errp);
1162 static bool
1163 char_socket_get_connected(Object *obj, Error **errp)
1165 SocketChardev *s = SOCKET_CHARDEV(obj);
1167 return s->connected;
1170 static void char_socket_class_init(ObjectClass *oc, void *data)
1172 ChardevClass *cc = CHARDEV_CLASS(oc);
1174 cc->parse = qemu_chr_parse_socket;
1175 cc->open = qmp_chardev_open_socket;
1176 cc->chr_wait_connected = tcp_chr_wait_connected;
1177 cc->chr_write = tcp_chr_write;
1178 cc->chr_sync_read = tcp_chr_sync_read;
1179 cc->chr_disconnect = tcp_chr_disconnect;
1180 cc->get_msgfds = tcp_get_msgfds;
1181 cc->set_msgfds = tcp_set_msgfds;
1182 cc->chr_add_client = tcp_chr_add_client;
1183 cc->chr_add_watch = tcp_chr_add_watch;
1184 cc->chr_update_read_handler = tcp_chr_update_read_handler;
1186 object_class_property_add(oc, "addr", "SocketAddress",
1187 char_socket_get_addr, NULL,
1188 NULL, NULL, &error_abort);
1190 object_class_property_add_bool(oc, "connected", char_socket_get_connected,
1191 NULL, &error_abort);
1194 static const TypeInfo char_socket_type_info = {
1195 .name = TYPE_CHARDEV_SOCKET,
1196 .parent = TYPE_CHARDEV,
1197 .instance_size = sizeof(SocketChardev),
1198 .instance_finalize = char_socket_finalize,
1199 .class_init = char_socket_class_init,
1202 static void register_types(void)
1204 type_register_static(&char_socket_type_info);
1207 type_init(register_types);