arm tcg cpus: Fix Lesser GPL version number
[qemu.git] / chardev / char-socket.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
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"
37
38 #include "chardev/char-io.h"
39 #include "qom/object.h"
40
41 /***********************************************************/
42 /* TCP Net console */
43
44 #define TCP_MAX_FDS 16
45
46 typedef struct {
47 char buf[21];
48 size_t buflen;
49 } TCPChardevTelnetInit;
50
51 typedef enum {
52 TCP_CHARDEV_STATE_DISCONNECTED,
53 TCP_CHARDEV_STATE_CONNECTING,
54 TCP_CHARDEV_STATE_CONNECTED,
55 } TCPChardevState;
56
57 struct SocketChardev {
58 Chardev parent;
59 QIOChannel *ioc; /* Client I/O channel */
60 QIOChannelSocket *sioc; /* Client master channel */
61 QIONetListener *listener;
62 GSource *hup_source;
63 QCryptoTLSCreds *tls_creds;
64 char *tls_authz;
65 TCPChardevState state;
66 int max_size;
67 int do_telnetopt;
68 int do_nodelay;
69 int *read_msgfds;
70 size_t read_msgfds_num;
71 int *write_msgfds;
72 size_t write_msgfds_num;
73
74 SocketAddress *addr;
75 bool is_listen;
76 bool is_telnet;
77 bool is_tn3270;
78 GSource *telnet_source;
79 TCPChardevTelnetInit *telnet_init;
80
81 bool is_websock;
82
83 GSource *reconnect_timer;
84 int64_t reconnect_time;
85 bool connect_err_reported;
86
87 QIOTask *connect_task;
88 };
89 typedef struct SocketChardev SocketChardev;
90
91 DECLARE_INSTANCE_CHECKER(SocketChardev, SOCKET_CHARDEV,
92 TYPE_CHARDEV_SOCKET)
93
94 static gboolean socket_reconnect_timeout(gpointer opaque);
95 static void tcp_chr_telnet_init(Chardev *chr);
96
97 static void tcp_chr_change_state(SocketChardev *s, TCPChardevState state)
98 {
99 switch (state) {
100 case TCP_CHARDEV_STATE_DISCONNECTED:
101 break;
102 case TCP_CHARDEV_STATE_CONNECTING:
103 assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
104 break;
105 case TCP_CHARDEV_STATE_CONNECTED:
106 assert(s->state == TCP_CHARDEV_STATE_CONNECTING);
107 break;
108 }
109 s->state = state;
110 }
111
112 static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
113 {
114 if (s->reconnect_timer) {
115 g_source_destroy(s->reconnect_timer);
116 g_source_unref(s->reconnect_timer);
117 s->reconnect_timer = NULL;
118 }
119 }
120
121 static void qemu_chr_socket_restart_timer(Chardev *chr)
122 {
123 SocketChardev *s = SOCKET_CHARDEV(chr);
124 char *name;
125
126 assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
127 assert(!s->reconnect_timer);
128 name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
129 s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
130 s->reconnect_time * 1000,
131 socket_reconnect_timeout,
132 chr);
133 g_source_set_name(s->reconnect_timer, name);
134 g_free(name);
135 }
136
137 static void check_report_connect_error(Chardev *chr,
138 Error *err)
139 {
140 SocketChardev *s = SOCKET_CHARDEV(chr);
141
142 if (!s->connect_err_reported) {
143 error_reportf_err(err,
144 "Unable to connect character device %s: ",
145 chr->label);
146 s->connect_err_reported = true;
147 } else {
148 error_free(err);
149 }
150 qemu_chr_socket_restart_timer(chr);
151 }
152
153 static void tcp_chr_accept(QIONetListener *listener,
154 QIOChannelSocket *cioc,
155 void *opaque);
156
157 static int tcp_chr_read_poll(void *opaque);
158 static void tcp_chr_disconnect_locked(Chardev *chr);
159
160 /* Called with chr_write_lock held. */
161 static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
162 {
163 SocketChardev *s = SOCKET_CHARDEV(chr);
164
165 if (s->state == TCP_CHARDEV_STATE_CONNECTED) {
166 int ret = io_channel_send_full(s->ioc, buf, len,
167 s->write_msgfds,
168 s->write_msgfds_num);
169
170 /* free the written msgfds in any cases
171 * other than ret < 0 && errno == EAGAIN
172 */
173 if (!(ret < 0 && EAGAIN == errno)
174 && s->write_msgfds_num) {
175 g_free(s->write_msgfds);
176 s->write_msgfds = 0;
177 s->write_msgfds_num = 0;
178 }
179
180 if (ret < 0 && errno != EAGAIN) {
181 if (tcp_chr_read_poll(chr) <= 0) {
182 /* Perform disconnect and return error. */
183 tcp_chr_disconnect_locked(chr);
184 } /* else let the read handler finish it properly */
185 }
186
187 return ret;
188 } else {
189 /* Indicate an error. */
190 errno = EIO;
191 return -1;
192 }
193 }
194
195 static int tcp_chr_read_poll(void *opaque)
196 {
197 Chardev *chr = CHARDEV(opaque);
198 SocketChardev *s = SOCKET_CHARDEV(opaque);
199 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
200 return 0;
201 }
202 s->max_size = qemu_chr_be_can_write(chr);
203 return s->max_size;
204 }
205
206 static void tcp_chr_process_IAC_bytes(Chardev *chr,
207 SocketChardev *s,
208 uint8_t *buf, int *size)
209 {
210 /* Handle any telnet or tn3270 client's basic IAC options.
211 * For telnet options, it satisfies char by char mode with no echo.
212 * For tn3270 options, it satisfies binary mode with EOR.
213 * All IAC options will be removed from the buf and the do_opt
214 * pointer will be used to track the state of the width of the
215 * IAC information.
216 *
217 * RFC854: "All TELNET commands consist of at least a two byte sequence.
218 * The commands dealing with option negotiation are three byte sequences,
219 * the third byte being the code for the option referenced."
220 * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
221 * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
222 * for tn3270.
223 * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
224 * session, and NOP and IP need to be done later.
225 */
226
227 int i;
228 int j = 0;
229
230 for (i = 0; i < *size; i++) {
231 if (s->do_telnetopt > 1) {
232 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
233 /* Double IAC means send an IAC */
234 if (j != i) {
235 buf[j] = buf[i];
236 }
237 j++;
238 s->do_telnetopt = 1;
239 } else {
240 if ((unsigned char)buf[i] == IAC_BREAK
241 && s->do_telnetopt == 2) {
242 /* Handle IAC break commands by sending a serial break */
243 qemu_chr_be_event(chr, CHR_EVENT_BREAK);
244 s->do_telnetopt++;
245 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
246 || (unsigned char)buf[i] == IAC_SB
247 || (unsigned char)buf[i] == IAC_SE)
248 && s->do_telnetopt == 2) {
249 buf[j++] = IAC;
250 buf[j++] = buf[i];
251 s->do_telnetopt++;
252 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
253 || (unsigned char)buf[i] == IAC_NOP)
254 && s->do_telnetopt == 2) {
255 /* TODO: IP and NOP need to be implemented later. */
256 s->do_telnetopt++;
257 }
258 s->do_telnetopt++;
259 }
260 if (s->do_telnetopt >= 4) {
261 s->do_telnetopt = 1;
262 }
263 } else {
264 if ((unsigned char)buf[i] == IAC) {
265 s->do_telnetopt = 2;
266 } else {
267 if (j != i) {
268 buf[j] = buf[i];
269 }
270 j++;
271 }
272 }
273 }
274 *size = j;
275 }
276
277 static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
278 {
279 SocketChardev *s = SOCKET_CHARDEV(chr);
280
281 int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
282
283 assert(num <= TCP_MAX_FDS);
284
285 if (to_copy) {
286 int i;
287
288 memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
289
290 /* Close unused fds */
291 for (i = to_copy; i < s->read_msgfds_num; i++) {
292 close(s->read_msgfds[i]);
293 }
294
295 g_free(s->read_msgfds);
296 s->read_msgfds = 0;
297 s->read_msgfds_num = 0;
298 }
299
300 return to_copy;
301 }
302
303 static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
304 {
305 SocketChardev *s = SOCKET_CHARDEV(chr);
306
307 /* clear old pending fd array */
308 g_free(s->write_msgfds);
309 s->write_msgfds = NULL;
310 s->write_msgfds_num = 0;
311
312 if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
313 !qio_channel_has_feature(s->ioc,
314 QIO_CHANNEL_FEATURE_FD_PASS)) {
315 return -1;
316 }
317
318 if (num) {
319 s->write_msgfds = g_new(int, num);
320 memcpy(s->write_msgfds, fds, num * sizeof(int));
321 }
322
323 s->write_msgfds_num = num;
324
325 return 0;
326 }
327
328 static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
329 {
330 SocketChardev *s = SOCKET_CHARDEV(chr);
331 struct iovec iov = { .iov_base = buf, .iov_len = len };
332 int ret;
333 size_t i;
334 int *msgfds = NULL;
335 size_t msgfds_num = 0;
336
337 if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
338 ret = qio_channel_readv_full(s->ioc, &iov, 1,
339 &msgfds, &msgfds_num,
340 NULL);
341 } else {
342 ret = qio_channel_readv_full(s->ioc, &iov, 1,
343 NULL, NULL,
344 NULL);
345 }
346
347 if (ret == QIO_CHANNEL_ERR_BLOCK) {
348 errno = EAGAIN;
349 ret = -1;
350 } else if (ret == -1) {
351 errno = EIO;
352 }
353
354 if (msgfds_num) {
355 /* close and clean read_msgfds */
356 for (i = 0; i < s->read_msgfds_num; i++) {
357 close(s->read_msgfds[i]);
358 }
359
360 if (s->read_msgfds_num) {
361 g_free(s->read_msgfds);
362 }
363
364 s->read_msgfds = msgfds;
365 s->read_msgfds_num = msgfds_num;
366 }
367
368 for (i = 0; i < s->read_msgfds_num; i++) {
369 int fd = s->read_msgfds[i];
370 if (fd < 0) {
371 continue;
372 }
373
374 /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
375 qemu_set_block(fd);
376
377 #ifndef MSG_CMSG_CLOEXEC
378 qemu_set_cloexec(fd);
379 #endif
380 }
381
382 return ret;
383 }
384
385 static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
386 {
387 SocketChardev *s = SOCKET_CHARDEV(chr);
388 return qio_channel_create_watch(s->ioc, cond);
389 }
390
391 static void remove_hup_source(SocketChardev *s)
392 {
393 if (s->hup_source != NULL) {
394 g_source_destroy(s->hup_source);
395 g_source_unref(s->hup_source);
396 s->hup_source = NULL;
397 }
398 }
399
400 static void tcp_chr_free_connection(Chardev *chr)
401 {
402 SocketChardev *s = SOCKET_CHARDEV(chr);
403 int i;
404
405 if (s->read_msgfds_num) {
406 for (i = 0; i < s->read_msgfds_num; i++) {
407 close(s->read_msgfds[i]);
408 }
409 g_free(s->read_msgfds);
410 s->read_msgfds = NULL;
411 s->read_msgfds_num = 0;
412 }
413
414 remove_hup_source(s);
415
416 tcp_set_msgfds(chr, NULL, 0);
417 remove_fd_in_watch(chr);
418 object_unref(OBJECT(s->sioc));
419 s->sioc = NULL;
420 object_unref(OBJECT(s->ioc));
421 s->ioc = NULL;
422 g_free(chr->filename);
423 chr->filename = NULL;
424 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
425 }
426
427 static const char *qemu_chr_socket_protocol(SocketChardev *s)
428 {
429 if (s->is_telnet) {
430 return "telnet";
431 }
432 return s->is_websock ? "websocket" : "tcp";
433 }
434
435 static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)
436 {
437 switch (s->addr->type) {
438 case SOCKET_ADDRESS_TYPE_INET:
439 return g_strdup_printf("%s%s:%s:%s%s", prefix,
440 qemu_chr_socket_protocol(s),
441 s->addr->u.inet.host,
442 s->addr->u.inet.port,
443 s->is_listen ? ",server" : "");
444 break;
445 case SOCKET_ADDRESS_TYPE_UNIX:
446 {
447 const char *tight = "", *abstract = "";
448 UnixSocketAddress *sa = &s->addr->u.q_unix;
449
450 #ifdef CONFIG_LINUX
451 if (sa->has_abstract && sa->abstract) {
452 abstract = ",abstract";
453 if (sa->has_tight && sa->tight) {
454 tight = ",tight";
455 }
456 }
457 #endif
458
459 return g_strdup_printf("%sunix:%s%s%s%s", prefix, sa->path,
460 abstract, tight,
461 s->is_listen ? ",server" : "");
462 break;
463 }
464 case SOCKET_ADDRESS_TYPE_FD:
465 return g_strdup_printf("%sfd:%s%s", prefix, s->addr->u.fd.str,
466 s->is_listen ? ",server" : "");
467 break;
468 case SOCKET_ADDRESS_TYPE_VSOCK:
469 return g_strdup_printf("%svsock:%s:%s", prefix,
470 s->addr->u.vsock.cid,
471 s->addr->u.vsock.port);
472 default:
473 abort();
474 }
475 }
476
477 static void update_disconnected_filename(SocketChardev *s)
478 {
479 Chardev *chr = CHARDEV(s);
480
481 g_free(chr->filename);
482 if (s->addr) {
483 chr->filename = qemu_chr_socket_address(s, "disconnected:");
484 } else {
485 chr->filename = g_strdup("disconnected:socket");
486 }
487 }
488
489 /* NB may be called even if tcp_chr_connect has not been
490 * reached, due to TLS or telnet initialization failure,
491 * so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
492 * This must be called with chr->chr_write_lock held.
493 */
494 static void tcp_chr_disconnect_locked(Chardev *chr)
495 {
496 SocketChardev *s = SOCKET_CHARDEV(chr);
497 bool emit_close = s->state == TCP_CHARDEV_STATE_CONNECTED;
498
499 tcp_chr_free_connection(chr);
500
501 if (s->listener) {
502 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
503 chr, NULL, chr->gcontext);
504 }
505 update_disconnected_filename(s);
506 if (emit_close) {
507 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
508 }
509 if (s->reconnect_time && !s->reconnect_timer) {
510 qemu_chr_socket_restart_timer(chr);
511 }
512 }
513
514 static void tcp_chr_disconnect(Chardev *chr)
515 {
516 qemu_mutex_lock(&chr->chr_write_lock);
517 tcp_chr_disconnect_locked(chr);
518 qemu_mutex_unlock(&chr->chr_write_lock);
519 }
520
521 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
522 {
523 Chardev *chr = CHARDEV(opaque);
524 SocketChardev *s = SOCKET_CHARDEV(opaque);
525 uint8_t buf[CHR_READ_BUF_LEN];
526 int len, size;
527
528 if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
529 s->max_size <= 0) {
530 return TRUE;
531 }
532 len = sizeof(buf);
533 if (len > s->max_size) {
534 len = s->max_size;
535 }
536 size = tcp_chr_recv(chr, (void *)buf, len);
537 if (size == 0 || (size == -1 && errno != EAGAIN)) {
538 /* connection closed */
539 tcp_chr_disconnect(chr);
540 } else if (size > 0) {
541 if (s->do_telnetopt) {
542 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
543 }
544 if (size > 0) {
545 qemu_chr_be_write(chr, buf, size);
546 }
547 }
548
549 return TRUE;
550 }
551
552 static gboolean tcp_chr_hup(QIOChannel *channel,
553 GIOCondition cond,
554 void *opaque)
555 {
556 Chardev *chr = CHARDEV(opaque);
557 tcp_chr_disconnect(chr);
558 return G_SOURCE_REMOVE;
559 }
560
561 static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
562 {
563 SocketChardev *s = SOCKET_CHARDEV(chr);
564 int size;
565
566 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
567 return 0;
568 }
569
570 qio_channel_set_blocking(s->ioc, true, NULL);
571 size = tcp_chr_recv(chr, (void *) buf, len);
572 if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
573 qio_channel_set_blocking(s->ioc, false, NULL);
574 }
575 if (size == 0) {
576 /* connection closed */
577 tcp_chr_disconnect(chr);
578 }
579
580 return size;
581 }
582
583 static char *qemu_chr_compute_filename(SocketChardev *s)
584 {
585 struct sockaddr_storage *ss = &s->sioc->localAddr;
586 struct sockaddr_storage *ps = &s->sioc->remoteAddr;
587 socklen_t ss_len = s->sioc->localAddrLen;
588 socklen_t ps_len = s->sioc->remoteAddrLen;
589 char shost[NI_MAXHOST], sserv[NI_MAXSERV];
590 char phost[NI_MAXHOST], pserv[NI_MAXSERV];
591 const char *left = "", *right = "";
592
593 switch (ss->ss_family) {
594 #ifndef _WIN32
595 case AF_UNIX:
596 return g_strdup_printf("unix:%s%s",
597 ((struct sockaddr_un *)(ss))->sun_path,
598 s->is_listen ? ",server" : "");
599 #endif
600 case AF_INET6:
601 left = "[";
602 right = "]";
603 /* fall through */
604 case AF_INET:
605 getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
606 sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
607 getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
608 pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
609 return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
610 qemu_chr_socket_protocol(s),
611 left, shost, right, sserv,
612 s->is_listen ? ",server" : "",
613 left, phost, right, pserv);
614
615 default:
616 return g_strdup_printf("unknown");
617 }
618 }
619
620 static void update_ioc_handlers(SocketChardev *s)
621 {
622 Chardev *chr = CHARDEV(s);
623
624 if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
625 return;
626 }
627
628 remove_fd_in_watch(chr);
629 chr->gsource = io_add_watch_poll(chr, s->ioc,
630 tcp_chr_read_poll,
631 tcp_chr_read, chr,
632 chr->gcontext);
633
634 remove_hup_source(s);
635 s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
636 g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
637 chr, NULL);
638 g_source_attach(s->hup_source, chr->gcontext);
639 }
640
641 static void tcp_chr_connect(void *opaque)
642 {
643 Chardev *chr = CHARDEV(opaque);
644 SocketChardev *s = SOCKET_CHARDEV(opaque);
645
646 g_free(chr->filename);
647 chr->filename = qemu_chr_compute_filename(s);
648
649 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTED);
650 update_ioc_handlers(s);
651 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
652 }
653
654 static void tcp_chr_telnet_destroy(SocketChardev *s)
655 {
656 if (s->telnet_source) {
657 g_source_destroy(s->telnet_source);
658 g_source_unref(s->telnet_source);
659 s->telnet_source = NULL;
660 }
661 }
662
663 static void tcp_chr_update_read_handler(Chardev *chr)
664 {
665 SocketChardev *s = SOCKET_CHARDEV(chr);
666
667 if (s->listener && s->state == TCP_CHARDEV_STATE_DISCONNECTED) {
668 /*
669 * It's possible that chardev context is changed in
670 * qemu_chr_be_update_read_handlers(). Reset it for QIO net
671 * listener if there is.
672 */
673 qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
674 chr, NULL, chr->gcontext);
675 }
676
677 if (s->telnet_source) {
678 tcp_chr_telnet_init(CHARDEV(s));
679 }
680
681 update_ioc_handlers(s);
682 }
683
684 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
685 GIOCondition cond G_GNUC_UNUSED,
686 gpointer user_data)
687 {
688 SocketChardev *s = user_data;
689 Chardev *chr = CHARDEV(s);
690 TCPChardevTelnetInit *init = s->telnet_init;
691 ssize_t ret;
692
693 assert(init);
694
695 ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
696 if (ret < 0) {
697 if (ret == QIO_CHANNEL_ERR_BLOCK) {
698 ret = 0;
699 } else {
700 tcp_chr_disconnect(chr);
701 goto end;
702 }
703 }
704 init->buflen -= ret;
705
706 if (init->buflen == 0) {
707 tcp_chr_connect(chr);
708 goto end;
709 }
710
711 memmove(init->buf, init->buf + ret, init->buflen);
712
713 return G_SOURCE_CONTINUE;
714
715 end:
716 g_free(s->telnet_init);
717 s->telnet_init = NULL;
718 g_source_unref(s->telnet_source);
719 s->telnet_source = NULL;
720 return G_SOURCE_REMOVE;
721 }
722
723 static void tcp_chr_telnet_init(Chardev *chr)
724 {
725 SocketChardev *s = SOCKET_CHARDEV(chr);
726 TCPChardevTelnetInit *init;
727 size_t n = 0;
728
729 /* Destroy existing task */
730 tcp_chr_telnet_destroy(s);
731
732 if (s->telnet_init) {
733 /* We are possibly during a handshake already */
734 goto cont;
735 }
736
737 s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
738 init = s->telnet_init;
739
740 #define IACSET(x, a, b, c) \
741 do { \
742 x[n++] = a; \
743 x[n++] = b; \
744 x[n++] = c; \
745 } while (0)
746
747 if (!s->is_tn3270) {
748 init->buflen = 12;
749 /* Prep the telnet negotion to put telnet in binary,
750 * no echo, single char mode */
751 IACSET(init->buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
752 IACSET(init->buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
753 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
754 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
755 } else {
756 init->buflen = 21;
757 /* Prep the TN3270 negotion based on RFC1576 */
758 IACSET(init->buf, 0xff, 0xfd, 0x19); /* IAC DO EOR */
759 IACSET(init->buf, 0xff, 0xfb, 0x19); /* IAC WILL EOR */
760 IACSET(init->buf, 0xff, 0xfd, 0x00); /* IAC DO BINARY */
761 IACSET(init->buf, 0xff, 0xfb, 0x00); /* IAC WILL BINARY */
762 IACSET(init->buf, 0xff, 0xfd, 0x18); /* IAC DO TERMINAL TYPE */
763 IACSET(init->buf, 0xff, 0xfa, 0x18); /* IAC SB TERMINAL TYPE */
764 IACSET(init->buf, 0x01, 0xff, 0xf0); /* SEND IAC SE */
765 }
766
767 #undef IACSET
768
769 cont:
770 s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
771 tcp_chr_telnet_init_io,
772 s, NULL,
773 chr->gcontext);
774 }
775
776
777 static void tcp_chr_websock_handshake(QIOTask *task, gpointer user_data)
778 {
779 Chardev *chr = user_data;
780 SocketChardev *s = user_data;
781
782 if (qio_task_propagate_error(task, NULL)) {
783 tcp_chr_disconnect(chr);
784 } else {
785 if (s->do_telnetopt) {
786 tcp_chr_telnet_init(chr);
787 } else {
788 tcp_chr_connect(chr);
789 }
790 }
791 }
792
793
794 static void tcp_chr_websock_init(Chardev *chr)
795 {
796 SocketChardev *s = SOCKET_CHARDEV(chr);
797 QIOChannelWebsock *wioc = NULL;
798 gchar *name;
799
800 wioc = qio_channel_websock_new_server(s->ioc);
801
802 name = g_strdup_printf("chardev-websocket-server-%s", chr->label);
803 qio_channel_set_name(QIO_CHANNEL(wioc), name);
804 g_free(name);
805 object_unref(OBJECT(s->ioc));
806 s->ioc = QIO_CHANNEL(wioc);
807
808 qio_channel_websock_handshake(wioc, tcp_chr_websock_handshake, chr, NULL);
809 }
810
811
812 static void tcp_chr_tls_handshake(QIOTask *task,
813 gpointer user_data)
814 {
815 Chardev *chr = user_data;
816 SocketChardev *s = user_data;
817
818 if (qio_task_propagate_error(task, NULL)) {
819 tcp_chr_disconnect(chr);
820 } else {
821 if (s->is_websock) {
822 tcp_chr_websock_init(chr);
823 } else if (s->do_telnetopt) {
824 tcp_chr_telnet_init(chr);
825 } else {
826 tcp_chr_connect(chr);
827 }
828 }
829 }
830
831
832 static void tcp_chr_tls_init(Chardev *chr)
833 {
834 SocketChardev *s = SOCKET_CHARDEV(chr);
835 QIOChannelTLS *tioc;
836 gchar *name;
837
838 if (s->is_listen) {
839 tioc = qio_channel_tls_new_server(
840 s->ioc, s->tls_creds,
841 s->tls_authz,
842 NULL);
843 } else {
844 tioc = qio_channel_tls_new_client(
845 s->ioc, s->tls_creds,
846 s->addr->u.inet.host,
847 NULL);
848 }
849 if (tioc == NULL) {
850 tcp_chr_disconnect(chr);
851 return;
852 }
853 name = g_strdup_printf("chardev-tls-%s-%s",
854 s->is_listen ? "server" : "client",
855 chr->label);
856 qio_channel_set_name(QIO_CHANNEL(tioc), name);
857 g_free(name);
858 object_unref(OBJECT(s->ioc));
859 s->ioc = QIO_CHANNEL(tioc);
860
861 qio_channel_tls_handshake(tioc,
862 tcp_chr_tls_handshake,
863 chr,
864 NULL,
865 chr->gcontext);
866 }
867
868
869 static void tcp_chr_set_client_ioc_name(Chardev *chr,
870 QIOChannelSocket *sioc)
871 {
872 SocketChardev *s = SOCKET_CHARDEV(chr);
873 char *name;
874 name = g_strdup_printf("chardev-tcp-%s-%s",
875 s->is_listen ? "server" : "client",
876 chr->label);
877 qio_channel_set_name(QIO_CHANNEL(sioc), name);
878 g_free(name);
879
880 }
881
882 static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
883 {
884 SocketChardev *s = SOCKET_CHARDEV(chr);
885
886 if (s->state != TCP_CHARDEV_STATE_CONNECTING) {
887 return -1;
888 }
889
890 s->ioc = QIO_CHANNEL(sioc);
891 object_ref(OBJECT(sioc));
892 s->sioc = sioc;
893 object_ref(OBJECT(sioc));
894
895 qio_channel_set_blocking(s->ioc, false, NULL);
896
897 if (s->do_nodelay) {
898 qio_channel_set_delay(s->ioc, false);
899 }
900 if (s->listener) {
901 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
902 NULL, chr->gcontext);
903 }
904
905 if (s->tls_creds) {
906 tcp_chr_tls_init(chr);
907 } else if (s->is_websock) {
908 tcp_chr_websock_init(chr);
909 } else if (s->do_telnetopt) {
910 tcp_chr_telnet_init(chr);
911 } else {
912 tcp_chr_connect(chr);
913 }
914
915 return 0;
916 }
917
918
919 static int tcp_chr_add_client(Chardev *chr, int fd)
920 {
921 int ret;
922 QIOChannelSocket *sioc;
923 SocketChardev *s = SOCKET_CHARDEV(chr);
924
925 if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
926 return -1;
927 }
928
929 sioc = qio_channel_socket_new_fd(fd, NULL);
930 if (!sioc) {
931 return -1;
932 }
933 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
934 tcp_chr_set_client_ioc_name(chr, sioc);
935 ret = tcp_chr_new_client(chr, sioc);
936 object_unref(OBJECT(sioc));
937 return ret;
938 }
939
940 static void tcp_chr_accept(QIONetListener *listener,
941 QIOChannelSocket *cioc,
942 void *opaque)
943 {
944 Chardev *chr = CHARDEV(opaque);
945 SocketChardev *s = SOCKET_CHARDEV(chr);
946
947 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
948 tcp_chr_set_client_ioc_name(chr, cioc);
949 tcp_chr_new_client(chr, cioc);
950 }
951
952
953 static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp)
954 {
955 SocketChardev *s = SOCKET_CHARDEV(chr);
956 QIOChannelSocket *sioc = qio_channel_socket_new();
957 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
958 tcp_chr_set_client_ioc_name(chr, sioc);
959 if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
960 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
961 object_unref(OBJECT(sioc));
962 return -1;
963 }
964 tcp_chr_new_client(chr, sioc);
965 object_unref(OBJECT(sioc));
966 return 0;
967 }
968
969
970 static void tcp_chr_accept_server_sync(Chardev *chr)
971 {
972 SocketChardev *s = SOCKET_CHARDEV(chr);
973 QIOChannelSocket *sioc;
974 info_report("QEMU waiting for connection on: %s",
975 chr->filename);
976 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
977 sioc = qio_net_listener_wait_client(s->listener);
978 tcp_chr_set_client_ioc_name(chr, sioc);
979 tcp_chr_new_client(chr, sioc);
980 object_unref(OBJECT(sioc));
981 }
982
983
984 static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
985 {
986 SocketChardev *s = SOCKET_CHARDEV(chr);
987 const char *opts[] = { "telnet", "tn3270", "websock", "tls-creds" };
988 bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds };
989 size_t i;
990
991 QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts) != G_N_ELEMENTS(optset));
992 for (i = 0; i < G_N_ELEMENTS(opts); i++) {
993 if (optset[i]) {
994 error_setg(errp,
995 "'%s' option is incompatible with waiting for "
996 "connection completion", opts[i]);
997 return -1;
998 }
999 }
1000
1001 tcp_chr_reconn_timer_cancel(s);
1002
1003 /*
1004 * We expect states to be as follows:
1005 *
1006 * - server
1007 * - wait -> CONNECTED
1008 * - nowait -> DISCONNECTED
1009 * - client
1010 * - reconnect == 0 -> CONNECTED
1011 * - reconnect != 0 -> CONNECTING
1012 *
1013 */
1014 if (s->state == TCP_CHARDEV_STATE_CONNECTING) {
1015 if (!s->connect_task) {
1016 error_setg(errp,
1017 "Unexpected 'connecting' state without connect task "
1018 "while waiting for connection completion");
1019 return -1;
1020 }
1021 /*
1022 * tcp_chr_wait_connected should only ever be run from the
1023 * main loop thread associated with chr->gcontext, otherwise
1024 * qio_task_wait_thread has a dangerous race condition with
1025 * free'ing of the s->connect_task object.
1026 *
1027 * Acquiring the main context doesn't 100% prove we're in
1028 * the main loop thread, but it does at least guarantee
1029 * that the main loop won't be executed by another thread
1030 * avoiding the race condition with the task idle callback.
1031 */
1032 g_main_context_acquire(chr->gcontext);
1033 qio_task_wait_thread(s->connect_task);
1034 g_main_context_release(chr->gcontext);
1035
1036 /*
1037 * The completion callback (qemu_chr_socket_connected) for
1038 * s->connect_task should have set this to NULL by the time
1039 * qio_task_wait_thread has returned.
1040 */
1041 assert(!s->connect_task);
1042
1043 /*
1044 * NB we are *not* guaranteed to have "s->state == ..CONNECTED"
1045 * at this point as this first connect may be failed, so
1046 * allow the next loop to run regardless.
1047 */
1048 }
1049
1050 while (s->state != TCP_CHARDEV_STATE_CONNECTED) {
1051 if (s->is_listen) {
1052 tcp_chr_accept_server_sync(chr);
1053 } else {
1054 Error *err = NULL;
1055 if (tcp_chr_connect_client_sync(chr, &err) < 0) {
1056 if (s->reconnect_time) {
1057 error_free(err);
1058 g_usleep(s->reconnect_time * 1000ULL * 1000ULL);
1059 } else {
1060 error_propagate(errp, err);
1061 return -1;
1062 }
1063 }
1064 }
1065 }
1066
1067 return 0;
1068 }
1069
1070 static void char_socket_finalize(Object *obj)
1071 {
1072 Chardev *chr = CHARDEV(obj);
1073 SocketChardev *s = SOCKET_CHARDEV(obj);
1074
1075 tcp_chr_free_connection(chr);
1076 tcp_chr_reconn_timer_cancel(s);
1077 qapi_free_SocketAddress(s->addr);
1078 tcp_chr_telnet_destroy(s);
1079 g_free(s->telnet_init);
1080 if (s->listener) {
1081 qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
1082 NULL, chr->gcontext);
1083 object_unref(OBJECT(s->listener));
1084 }
1085 if (s->tls_creds) {
1086 object_unref(OBJECT(s->tls_creds));
1087 }
1088 g_free(s->tls_authz);
1089
1090 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1091 }
1092
1093 static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
1094 {
1095 QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1096 Chardev *chr = CHARDEV(opaque);
1097 SocketChardev *s = SOCKET_CHARDEV(chr);
1098 Error *err = NULL;
1099
1100 s->connect_task = NULL;
1101
1102 if (qio_task_propagate_error(task, &err)) {
1103 tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
1104 check_report_connect_error(chr, err);
1105 goto cleanup;
1106 }
1107
1108 s->connect_err_reported = false;
1109 tcp_chr_new_client(chr, sioc);
1110
1111 cleanup:
1112 object_unref(OBJECT(sioc));
1113 }
1114
1115
1116 static void tcp_chr_connect_client_task(QIOTask *task,
1117 gpointer opaque)
1118 {
1119 QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1120 SocketAddress *addr = opaque;
1121 Error *err = NULL;
1122
1123 qio_channel_socket_connect_sync(ioc, addr, &err);
1124
1125 qio_task_set_error(task, err);
1126 }
1127
1128
1129 static void tcp_chr_connect_client_async(Chardev *chr)
1130 {
1131 SocketChardev *s = SOCKET_CHARDEV(chr);
1132 QIOChannelSocket *sioc;
1133
1134 tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
1135 sioc = qio_channel_socket_new();
1136 tcp_chr_set_client_ioc_name(chr, sioc);
1137 /*
1138 * Normally code would use the qio_channel_socket_connect_async
1139 * method which uses a QIOTask + qio_task_set_error internally
1140 * to avoid blocking. The tcp_chr_wait_connected method, however,
1141 * needs a way to synchronize with completion of the background
1142 * connect task which can't be done with the QIOChannelSocket
1143 * async APIs. Thus we must use QIOTask directly to implement
1144 * the non-blocking concept locally.
1145 */
1146 s->connect_task = qio_task_new(OBJECT(sioc),
1147 qemu_chr_socket_connected,
1148 object_ref(OBJECT(chr)),
1149 (GDestroyNotify)object_unref);
1150 qio_task_run_in_thread(s->connect_task,
1151 tcp_chr_connect_client_task,
1152 s->addr,
1153 NULL,
1154 chr->gcontext);
1155 }
1156
1157 static gboolean socket_reconnect_timeout(gpointer opaque)
1158 {
1159 Chardev *chr = CHARDEV(opaque);
1160 SocketChardev *s = SOCKET_CHARDEV(opaque);
1161
1162 qemu_mutex_lock(&chr->chr_write_lock);
1163 g_source_unref(s->reconnect_timer);
1164 s->reconnect_timer = NULL;
1165 qemu_mutex_unlock(&chr->chr_write_lock);
1166
1167 if (chr->be_open) {
1168 return false;
1169 }
1170
1171 tcp_chr_connect_client_async(chr);
1172
1173 return false;
1174 }
1175
1176
1177 static int qmp_chardev_open_socket_server(Chardev *chr,
1178 bool is_telnet,
1179 bool is_waitconnect,
1180 Error **errp)
1181 {
1182 SocketChardev *s = SOCKET_CHARDEV(chr);
1183 char *name;
1184 if (is_telnet) {
1185 s->do_telnetopt = 1;
1186 }
1187 s->listener = qio_net_listener_new();
1188
1189 name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
1190 qio_net_listener_set_name(s->listener, name);
1191 g_free(name);
1192
1193 if (qio_net_listener_open_sync(s->listener, s->addr, 1, errp) < 0) {
1194 object_unref(OBJECT(s->listener));
1195 s->listener = NULL;
1196 return -1;
1197 }
1198
1199 qapi_free_SocketAddress(s->addr);
1200 s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
1201 update_disconnected_filename(s);
1202
1203 if (is_waitconnect) {
1204 tcp_chr_accept_server_sync(chr);
1205 } else {
1206 qio_net_listener_set_client_func_full(s->listener,
1207 tcp_chr_accept,
1208 chr, NULL,
1209 chr->gcontext);
1210 }
1211
1212 return 0;
1213 }
1214
1215
1216 static int qmp_chardev_open_socket_client(Chardev *chr,
1217 int64_t reconnect,
1218 Error **errp)
1219 {
1220 SocketChardev *s = SOCKET_CHARDEV(chr);
1221
1222 if (reconnect > 0) {
1223 s->reconnect_time = reconnect;
1224 tcp_chr_connect_client_async(chr);
1225 return 0;
1226 } else {
1227 return tcp_chr_connect_client_sync(chr, errp);
1228 }
1229 }
1230
1231
1232 static bool qmp_chardev_validate_socket(ChardevSocket *sock,
1233 SocketAddress *addr,
1234 Error **errp)
1235 {
1236 /* Validate any options which have a dependency on address type */
1237 switch (addr->type) {
1238 case SOCKET_ADDRESS_TYPE_FD:
1239 if (sock->has_reconnect) {
1240 error_setg(errp,
1241 "'reconnect' option is incompatible with "
1242 "'fd' address type");
1243 return false;
1244 }
1245 if (sock->has_tls_creds &&
1246 !(sock->has_server && sock->server)) {
1247 error_setg(errp,
1248 "'tls_creds' option is incompatible with "
1249 "'fd' address type as client");
1250 return false;
1251 }
1252 break;
1253
1254 case SOCKET_ADDRESS_TYPE_UNIX:
1255 if (sock->has_tls_creds) {
1256 error_setg(errp,
1257 "'tls_creds' option is incompatible with "
1258 "'unix' address type");
1259 return false;
1260 }
1261 break;
1262
1263 case SOCKET_ADDRESS_TYPE_INET:
1264 break;
1265
1266 case SOCKET_ADDRESS_TYPE_VSOCK:
1267 if (sock->has_tls_creds) {
1268 error_setg(errp,
1269 "'tls_creds' option is incompatible with "
1270 "'vsock' address type");
1271 return false;
1272 }
1273
1274 default:
1275 break;
1276 }
1277
1278 if (sock->has_tls_authz && !sock->has_tls_creds) {
1279 error_setg(errp, "'tls_authz' option requires 'tls_creds' option");
1280 return false;
1281 }
1282
1283 /* Validate any options which have a dependancy on client vs server */
1284 if (!sock->has_server || sock->server) {
1285 if (sock->has_reconnect) {
1286 error_setg(errp,
1287 "'reconnect' option is incompatible with "
1288 "socket in server listen mode");
1289 return false;
1290 }
1291 } else {
1292 if (sock->has_websocket && sock->websocket) {
1293 error_setg(errp, "%s", "Websocket client is not implemented");
1294 return false;
1295 }
1296 if (sock->has_wait) {
1297 warn_report("'wait' option is deprecated with "
1298 "socket in client connect mode");
1299 if (sock->wait) {
1300 error_setg(errp, "%s",
1301 "'wait' option is incompatible with "
1302 "socket in client connect mode");
1303 return false;
1304 }
1305 }
1306 }
1307
1308 return true;
1309 }
1310
1311
1312 static void qmp_chardev_open_socket(Chardev *chr,
1313 ChardevBackend *backend,
1314 bool *be_opened,
1315 Error **errp)
1316 {
1317 SocketChardev *s = SOCKET_CHARDEV(chr);
1318 ChardevSocket *sock = backend->u.socket.data;
1319 bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
1320 bool is_listen = sock->has_server ? sock->server : true;
1321 bool is_telnet = sock->has_telnet ? sock->telnet : false;
1322 bool is_tn3270 = sock->has_tn3270 ? sock->tn3270 : false;
1323 bool is_waitconnect = sock->has_wait ? sock->wait : false;
1324 bool is_websock = sock->has_websocket ? sock->websocket : false;
1325 int64_t reconnect = sock->has_reconnect ? sock->reconnect : 0;
1326 SocketAddress *addr;
1327
1328 s->is_listen = is_listen;
1329 s->is_telnet = is_telnet;
1330 s->is_tn3270 = is_tn3270;
1331 s->is_websock = is_websock;
1332 s->do_nodelay = do_nodelay;
1333 if (sock->tls_creds) {
1334 Object *creds;
1335 creds = object_resolve_path_component(
1336 object_get_objects_root(), sock->tls_creds);
1337 if (!creds) {
1338 error_setg(errp, "No TLS credentials with id '%s'",
1339 sock->tls_creds);
1340 return;
1341 }
1342 s->tls_creds = (QCryptoTLSCreds *)
1343 object_dynamic_cast(creds,
1344 TYPE_QCRYPTO_TLS_CREDS);
1345 if (!s->tls_creds) {
1346 error_setg(errp, "Object with id '%s' is not TLS credentials",
1347 sock->tls_creds);
1348 return;
1349 }
1350 object_ref(OBJECT(s->tls_creds));
1351 if (is_listen) {
1352 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
1353 error_setg(errp, "%s",
1354 "Expected TLS credentials for server endpoint");
1355 return;
1356 }
1357 } else {
1358 if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
1359 error_setg(errp, "%s",
1360 "Expected TLS credentials for client endpoint");
1361 return;
1362 }
1363 }
1364 }
1365 s->tls_authz = g_strdup(sock->tls_authz);
1366
1367 s->addr = addr = socket_address_flatten(sock->addr);
1368
1369 if (!qmp_chardev_validate_socket(sock, addr, errp)) {
1370 return;
1371 }
1372
1373 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
1374 /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1375 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1376 qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
1377 }
1378
1379 /* be isn't opened until we get a connection */
1380 *be_opened = false;
1381
1382 update_disconnected_filename(s);
1383
1384 if (s->is_listen) {
1385 if (qmp_chardev_open_socket_server(chr, is_telnet || is_tn3270,
1386 is_waitconnect, errp) < 0) {
1387 return;
1388 }
1389 } else {
1390 if (qmp_chardev_open_socket_client(chr, reconnect, errp) < 0) {
1391 return;
1392 }
1393 }
1394 }
1395
1396 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
1397 Error **errp)
1398 {
1399 const char *path = qemu_opt_get(opts, "path");
1400 const char *host = qemu_opt_get(opts, "host");
1401 const char *port = qemu_opt_get(opts, "port");
1402 const char *fd = qemu_opt_get(opts, "fd");
1403 #ifdef CONFIG_LINUX
1404 bool tight = qemu_opt_get_bool(opts, "tight", true);
1405 bool abstract = qemu_opt_get_bool(opts, "abstract", false);
1406 #endif
1407 SocketAddressLegacy *addr;
1408 ChardevSocket *sock;
1409
1410 if ((!!path + !!fd + !!host) != 1) {
1411 error_setg(errp,
1412 "Exactly one of 'path', 'fd' or 'host' required");
1413 return;
1414 }
1415
1416 if (host && !port) {
1417 error_setg(errp, "chardev: socket: no port given");
1418 return;
1419 }
1420
1421 backend->type = CHARDEV_BACKEND_KIND_SOCKET;
1422 sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
1423 qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
1424
1425 sock->has_nodelay = qemu_opt_get(opts, "delay");
1426 sock->nodelay = !qemu_opt_get_bool(opts, "delay", true);
1427 /*
1428 * We have different default to QMP for 'server', hence
1429 * we can't just check for existence of 'server'
1430 */
1431 sock->has_server = true;
1432 sock->server = qemu_opt_get_bool(opts, "server", false);
1433 sock->has_telnet = qemu_opt_get(opts, "telnet");
1434 sock->telnet = qemu_opt_get_bool(opts, "telnet", false);
1435 sock->has_tn3270 = qemu_opt_get(opts, "tn3270");
1436 sock->tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
1437 sock->has_websocket = qemu_opt_get(opts, "websocket");
1438 sock->websocket = qemu_opt_get_bool(opts, "websocket", false);
1439 /*
1440 * We have different default to QMP for 'wait' when 'server'
1441 * is set, hence we can't just check for existence of 'wait'
1442 */
1443 sock->has_wait = qemu_opt_find(opts, "wait") || sock->server;
1444 sock->wait = qemu_opt_get_bool(opts, "wait", true);
1445 sock->has_reconnect = qemu_opt_find(opts, "reconnect");
1446 sock->reconnect = qemu_opt_get_number(opts, "reconnect", 0);
1447 sock->has_tls_creds = qemu_opt_get(opts, "tls-creds");
1448 sock->tls_creds = g_strdup(qemu_opt_get(opts, "tls-creds"));
1449 sock->has_tls_authz = qemu_opt_get(opts, "tls-authz");
1450 sock->tls_authz = g_strdup(qemu_opt_get(opts, "tls-authz"));
1451
1452 addr = g_new0(SocketAddressLegacy, 1);
1453 if (path) {
1454 UnixSocketAddress *q_unix;
1455 addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
1456 q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
1457 q_unix->path = g_strdup(path);
1458 #ifdef CONFIG_LINUX
1459 q_unix->has_tight = true;
1460 q_unix->tight = tight;
1461 q_unix->has_abstract = true;
1462 q_unix->abstract = abstract;
1463 #endif
1464 } else if (host) {
1465 addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
1466 addr->u.inet.data = g_new(InetSocketAddress, 1);
1467 *addr->u.inet.data = (InetSocketAddress) {
1468 .host = g_strdup(host),
1469 .port = g_strdup(port),
1470 .has_to = qemu_opt_get(opts, "to"),
1471 .to = qemu_opt_get_number(opts, "to", 0),
1472 .has_ipv4 = qemu_opt_get(opts, "ipv4"),
1473 .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
1474 .has_ipv6 = qemu_opt_get(opts, "ipv6"),
1475 .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
1476 };
1477 } else if (fd) {
1478 addr->type = SOCKET_ADDRESS_LEGACY_KIND_FD;
1479 addr->u.fd.data = g_new(String, 1);
1480 addr->u.fd.data->str = g_strdup(fd);
1481 } else {
1482 g_assert_not_reached();
1483 }
1484 sock->addr = addr;
1485 }
1486
1487 static void
1488 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
1489 void *opaque, Error **errp)
1490 {
1491 SocketChardev *s = SOCKET_CHARDEV(obj);
1492
1493 visit_type_SocketAddress(v, name, &s->addr, errp);
1494 }
1495
1496 static bool
1497 char_socket_get_connected(Object *obj, Error **errp)
1498 {
1499 SocketChardev *s = SOCKET_CHARDEV(obj);
1500
1501 return s->state == TCP_CHARDEV_STATE_CONNECTED;
1502 }
1503
1504 static void char_socket_class_init(ObjectClass *oc, void *data)
1505 {
1506 ChardevClass *cc = CHARDEV_CLASS(oc);
1507
1508 cc->parse = qemu_chr_parse_socket;
1509 cc->open = qmp_chardev_open_socket;
1510 cc->chr_wait_connected = tcp_chr_wait_connected;
1511 cc->chr_write = tcp_chr_write;
1512 cc->chr_sync_read = tcp_chr_sync_read;
1513 cc->chr_disconnect = tcp_chr_disconnect;
1514 cc->get_msgfds = tcp_get_msgfds;
1515 cc->set_msgfds = tcp_set_msgfds;
1516 cc->chr_add_client = tcp_chr_add_client;
1517 cc->chr_add_watch = tcp_chr_add_watch;
1518 cc->chr_update_read_handler = tcp_chr_update_read_handler;
1519
1520 object_class_property_add(oc, "addr", "SocketAddress",
1521 char_socket_get_addr, NULL,
1522 NULL, NULL);
1523
1524 object_class_property_add_bool(oc, "connected", char_socket_get_connected,
1525 NULL);
1526 }
1527
1528 static const TypeInfo char_socket_type_info = {
1529 .name = TYPE_CHARDEV_SOCKET,
1530 .parent = TYPE_CHARDEV,
1531 .instance_size = sizeof(SocketChardev),
1532 .instance_finalize = char_socket_finalize,
1533 .class_init = char_socket_class_init,
1534 };
1535
1536 static void register_types(void)
1537 {
1538 type_register_static(&char_socket_type_info);
1539 }
1540
1541 type_init(register_types);