2 * QEMU network listener
4 * Copyright (c) 2016-2017 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io/net-listener.h"
23 #include "io/dns-resolver.h"
24 #include "qapi/error.h"
26 QIONetListener
*qio_net_listener_new(void)
30 ret
= QIO_NET_LISTENER(object_new(TYPE_QIO_NET_LISTENER
));
35 void qio_net_listener_set_name(QIONetListener
*listener
,
38 g_free(listener
->name
);
39 listener
->name
= g_strdup(name
);
43 static gboolean
qio_net_listener_channel_func(QIOChannel
*ioc
,
44 GIOCondition condition
,
47 QIONetListener
*listener
= QIO_NET_LISTENER(opaque
);
48 QIOChannelSocket
*sioc
;
50 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
56 if (listener
->io_func
) {
57 listener
->io_func(listener
, sioc
, listener
->io_data
);
60 object_unref(OBJECT(sioc
));
66 int qio_net_listener_open_sync(QIONetListener
*listener
,
70 QIODNSResolver
*resolver
= qio_dns_resolver_get_instance();
71 SocketAddress
**resaddrs
;
77 if (qio_dns_resolver_lookup_sync(resolver
,
85 for (i
= 0; i
< nresaddrs
; i
++) {
86 QIOChannelSocket
*sioc
= qio_channel_socket_new();
88 if (qio_channel_socket_listen_sync(sioc
, resaddrs
[i
],
89 err
? NULL
: &err
) == 0) {
92 qio_net_listener_add(listener
, sioc
);
95 qapi_free_SocketAddress(resaddrs
[i
]);
96 object_unref(OBJECT(sioc
));
104 error_propagate(errp
, err
);
110 void qio_net_listener_add(QIONetListener
*listener
,
111 QIOChannelSocket
*sioc
)
113 if (listener
->name
) {
114 char *name
= g_strdup_printf("%s-listen", listener
->name
);
115 qio_channel_set_name(QIO_CHANNEL(sioc
), name
);
119 listener
->sioc
= g_renew(QIOChannelSocket
*, listener
->sioc
,
120 listener
->nsioc
+ 1);
121 listener
->io_tag
= g_renew(gulong
, listener
->io_tag
, listener
->nsioc
+ 1);
122 listener
->sioc
[listener
->nsioc
] = sioc
;
123 listener
->io_tag
[listener
->nsioc
] = 0;
125 object_ref(OBJECT(sioc
));
126 listener
->connected
= true;
128 if (listener
->io_func
!= NULL
) {
129 object_ref(OBJECT(listener
));
130 listener
->io_tag
[listener
->nsioc
] = qio_channel_add_watch(
131 QIO_CHANNEL(listener
->sioc
[listener
->nsioc
]), G_IO_IN
,
132 qio_net_listener_channel_func
,
133 listener
, (GDestroyNotify
)object_unref
);
140 void qio_net_listener_set_client_func(QIONetListener
*listener
,
141 QIONetListenerClientFunc func
,
143 GDestroyNotify notify
)
147 if (listener
->io_notify
) {
148 listener
->io_notify(listener
->io_data
);
150 listener
->io_func
= func
;
151 listener
->io_data
= data
;
152 listener
->io_notify
= notify
;
154 for (i
= 0; i
< listener
->nsioc
; i
++) {
155 if (listener
->io_tag
[i
]) {
156 g_source_remove(listener
->io_tag
[i
]);
157 listener
->io_tag
[i
] = 0;
161 if (listener
->io_func
!= NULL
) {
162 for (i
= 0; i
< listener
->nsioc
; i
++) {
163 object_ref(OBJECT(listener
));
164 listener
->io_tag
[i
] = qio_channel_add_watch(
165 QIO_CHANNEL(listener
->sioc
[i
]), G_IO_IN
,
166 qio_net_listener_channel_func
,
167 listener
, (GDestroyNotify
)object_unref
);
173 struct QIONetListenerClientWaitData
{
174 QIOChannelSocket
*sioc
;
179 static gboolean
qio_net_listener_wait_client_func(QIOChannel
*ioc
,
180 GIOCondition condition
,
183 struct QIONetListenerClientWaitData
*data
= opaque
;
184 QIOChannelSocket
*sioc
;
186 sioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
193 object_unref(OBJECT(sioc
));
196 g_main_loop_quit(data
->loop
);
202 QIOChannelSocket
*qio_net_listener_wait_client(QIONetListener
*listener
)
204 GMainContext
*ctxt
= g_main_context_new();
205 GMainLoop
*loop
= g_main_loop_new(ctxt
, TRUE
);
207 struct QIONetListenerClientWaitData data
= {
213 for (i
= 0; i
< listener
->nsioc
; i
++) {
214 if (listener
->io_tag
[i
]) {
215 g_source_remove(listener
->io_tag
[i
]);
216 listener
->io_tag
[i
] = 0;
220 sources
= g_new0(GSource
*, listener
->nsioc
);
221 for (i
= 0; i
< listener
->nsioc
; i
++) {
222 sources
[i
] = qio_channel_create_watch(QIO_CHANNEL(listener
->sioc
[i
]),
225 g_source_set_callback(sources
[i
],
226 (GSourceFunc
)qio_net_listener_wait_client_func
,
229 g_source_attach(sources
[i
], ctxt
);
232 g_main_loop_run(loop
);
234 for (i
= 0; i
< listener
->nsioc
; i
++) {
235 g_source_unref(sources
[i
]);
237 g_main_loop_unref(loop
);
238 g_main_context_unref(ctxt
);
240 if (listener
->io_func
!= NULL
) {
241 for (i
= 0; i
< listener
->nsioc
; i
++) {
242 object_ref(OBJECT(listener
));
243 listener
->io_tag
[i
] = qio_channel_add_watch(
244 QIO_CHANNEL(listener
->sioc
[i
]), G_IO_IN
,
245 qio_net_listener_channel_func
,
246 listener
, (GDestroyNotify
)object_unref
);
253 void qio_net_listener_disconnect(QIONetListener
*listener
)
257 if (!listener
->connected
) {
261 for (i
= 0; i
< listener
->nsioc
; i
++) {
262 if (listener
->io_tag
[i
]) {
263 g_source_remove(listener
->io_tag
[i
]);
264 listener
->io_tag
[i
] = 0;
266 qio_channel_close(QIO_CHANNEL(listener
->sioc
[i
]), NULL
);
268 listener
->connected
= false;
272 bool qio_net_listener_is_connected(QIONetListener
*listener
)
274 return listener
->connected
;
277 static void qio_net_listener_finalize(Object
*obj
)
279 QIONetListener
*listener
= QIO_NET_LISTENER(obj
);
282 qio_net_listener_disconnect(listener
);
284 for (i
= 0; i
< listener
->nsioc
; i
++) {
285 object_unref(OBJECT(listener
->sioc
[i
]));
287 g_free(listener
->io_tag
);
288 g_free(listener
->sioc
);
289 g_free(listener
->name
);
292 static const TypeInfo qio_net_listener_info
= {
293 .parent
= TYPE_OBJECT
,
294 .name
= TYPE_QIO_NET_LISTENER
,
295 .instance_size
= sizeof(QIONetListener
),
296 .instance_finalize
= qio_net_listener_finalize
,
297 .class_size
= sizeof(QIONetListenerClass
),
301 static void qio_net_listener_register_types(void)
303 type_register_static(&qio_net_listener_info
);
307 type_init(qio_net_listener_register_types
);