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
24 #include "qemu/osdep.h"
25 #include "sysemu/char.h"
26 #include "io/channel-socket.h"
27 #include "qapi/error.h"
31 /***********************************************************/
37 uint8_t buf
[CHR_READ_BUF_LEN
];
43 #define UDP_CHARDEV(obj) OBJECT_CHECK(UdpChardev, (obj), TYPE_CHARDEV_UDP)
45 /* Called with chr_write_lock held. */
46 static int udp_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
48 UdpChardev
*s
= UDP_CHARDEV(chr
);
50 return qio_channel_write(
51 s
->ioc
, (const char *)buf
, len
, NULL
);
54 static int udp_chr_read_poll(void *opaque
)
56 Chardev
*chr
= CHARDEV(opaque
);
57 UdpChardev
*s
= UDP_CHARDEV(opaque
);
59 s
->max_size
= qemu_chr_be_can_write(chr
);
61 /* If there were any stray characters in the queue process them
64 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
65 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
67 s
->max_size
= qemu_chr_be_can_write(chr
);
72 static gboolean
udp_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
74 Chardev
*chr
= CHARDEV(opaque
);
75 UdpChardev
*s
= UDP_CHARDEV(opaque
);
78 if (s
->max_size
== 0) {
81 ret
= qio_channel_read(
82 s
->ioc
, (char *)s
->buf
, sizeof(s
->buf
), NULL
);
84 remove_fd_in_watch(chr
, NULL
);
90 while (s
->max_size
> 0 && s
->bufptr
< s
->bufcnt
) {
91 qemu_chr_be_write(chr
, &s
->buf
[s
->bufptr
], 1);
93 s
->max_size
= qemu_chr_be_can_write(chr
);
99 static void udp_chr_update_read_handler(Chardev
*chr
,
100 GMainContext
*context
)
102 UdpChardev
*s
= UDP_CHARDEV(chr
);
104 remove_fd_in_watch(chr
, NULL
);
106 chr
->fd_in_tag
= io_add_watch_poll(chr
, s
->ioc
,
113 static void char_udp_finalize(Object
*obj
)
115 Chardev
*chr
= CHARDEV(obj
);
116 UdpChardev
*s
= UDP_CHARDEV(obj
);
118 remove_fd_in_watch(chr
, NULL
);
120 object_unref(OBJECT(s
->ioc
));
122 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
125 static void qemu_chr_parse_udp(QemuOpts
*opts
, ChardevBackend
*backend
,
128 const char *host
= qemu_opt_get(opts
, "host");
129 const char *port
= qemu_opt_get(opts
, "port");
130 const char *localaddr
= qemu_opt_get(opts
, "localaddr");
131 const char *localport
= qemu_opt_get(opts
, "localport");
132 bool has_local
= false;
136 backend
->type
= CHARDEV_BACKEND_KIND_UDP
;
137 if (host
== NULL
|| strlen(host
) == 0) {
140 if (port
== NULL
|| strlen(port
) == 0) {
141 error_setg(errp
, "chardev: udp: remote port not specified");
144 if (localport
== NULL
|| strlen(localport
) == 0) {
149 if (localaddr
== NULL
|| strlen(localaddr
) == 0) {
155 udp
= backend
->u
.udp
.data
= g_new0(ChardevUdp
, 1);
156 qemu_chr_parse_common(opts
, qapi_ChardevUdp_base(udp
));
158 addr
= g_new0(SocketAddress
, 1);
159 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
160 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
161 *addr
->u
.inet
.data
= (InetSocketAddress
) {
162 .host
= g_strdup(host
),
163 .port
= g_strdup(port
),
164 .has_ipv4
= qemu_opt_get(opts
, "ipv4"),
165 .ipv4
= qemu_opt_get_bool(opts
, "ipv4", 0),
166 .has_ipv6
= qemu_opt_get(opts
, "ipv6"),
167 .ipv6
= qemu_opt_get_bool(opts
, "ipv6", 0),
172 udp
->has_local
= true;
173 addr
= g_new0(SocketAddress
, 1);
174 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
175 addr
->u
.inet
.data
= g_new(InetSocketAddress
, 1);
176 *addr
->u
.inet
.data
= (InetSocketAddress
) {
177 .host
= g_strdup(localaddr
),
178 .port
= g_strdup(localport
),
184 static void qmp_chardev_open_udp(Chardev
*chr
,
185 ChardevBackend
*backend
,
189 ChardevUdp
*udp
= backend
->u
.udp
.data
;
190 QIOChannelSocket
*sioc
= qio_channel_socket_new();
192 UdpChardev
*s
= UDP_CHARDEV(chr
);
194 if (qio_channel_socket_dgram_sync(sioc
,
195 udp
->local
, udp
->remote
,
197 object_unref(OBJECT(sioc
));
201 name
= g_strdup_printf("chardev-udp-%s", chr
->label
);
202 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
205 s
->ioc
= QIO_CHANNEL(sioc
);
206 /* be isn't opened until we get a connection */
210 static void char_udp_class_init(ObjectClass
*oc
, void *data
)
212 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
214 cc
->parse
= qemu_chr_parse_udp
;
215 cc
->open
= qmp_chardev_open_udp
;
216 cc
->chr_write
= udp_chr_write
;
217 cc
->chr_update_read_handler
= udp_chr_update_read_handler
;
220 static const TypeInfo char_udp_type_info
= {
221 .name
= TYPE_CHARDEV_UDP
,
222 .parent
= TYPE_CHARDEV
,
223 .instance_size
= sizeof(UdpChardev
),
224 .instance_finalize
= char_udp_finalize
,
225 .class_init
= char_udp_class_init
,
228 static void register_types(void)
230 type_register_static(&char_udp_type_info
);
233 type_init(register_types
);