tcg: Add generic vector ops for multiplication
[qemu/ar7.git] / io / net-listener.c
blob77a4e2831caeed6ce48d3aa1468ef9e9ab172978
1 /*
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)
28 QIONetListener *ret;
30 ret = QIO_NET_LISTENER(object_new(TYPE_QIO_NET_LISTENER));
32 return ret;
35 void qio_net_listener_set_name(QIONetListener *listener,
36 const char *name)
38 g_free(listener->name);
39 listener->name = g_strdup(name);
43 static gboolean qio_net_listener_channel_func(QIOChannel *ioc,
44 GIOCondition condition,
45 gpointer opaque)
47 QIONetListener *listener = QIO_NET_LISTENER(opaque);
48 QIOChannelSocket *sioc;
50 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
51 NULL);
52 if (!sioc) {
53 return TRUE;
56 if (listener->io_func) {
57 listener->io_func(listener, sioc, listener->io_data);
60 object_unref(OBJECT(sioc));
62 return TRUE;
66 int qio_net_listener_open_sync(QIONetListener *listener,
67 SocketAddress *addr,
68 Error **errp)
70 QIODNSResolver *resolver = qio_dns_resolver_get_instance();
71 SocketAddress **resaddrs;
72 size_t nresaddrs;
73 size_t i;
74 Error *err = NULL;
75 bool success = false;
77 if (qio_dns_resolver_lookup_sync(resolver,
78 addr,
79 &nresaddrs,
80 &resaddrs,
81 errp) < 0) {
82 return -1;
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) {
90 success = true;
92 qio_net_listener_add(listener, sioc);
95 qapi_free_SocketAddress(resaddrs[i]);
96 object_unref(OBJECT(sioc));
98 g_free(resaddrs);
100 if (success) {
101 error_free(err);
102 return 0;
103 } else {
104 error_propagate(errp, err);
105 return -1;
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);
116 g_free(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);
136 listener->nsioc++;
140 void qio_net_listener_set_client_func(QIONetListener *listener,
141 QIONetListenerClientFunc func,
142 gpointer data,
143 GDestroyNotify notify)
145 size_t i;
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;
175 GMainLoop *loop;
179 static gboolean qio_net_listener_wait_client_func(QIOChannel *ioc,
180 GIOCondition condition,
181 gpointer opaque)
183 struct QIONetListenerClientWaitData *data = opaque;
184 QIOChannelSocket *sioc;
186 sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
187 NULL);
188 if (!sioc) {
189 return TRUE;
192 if (data->sioc) {
193 object_unref(OBJECT(sioc));
194 } else {
195 data->sioc = sioc;
196 g_main_loop_quit(data->loop);
199 return TRUE;
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);
206 GSource **sources;
207 struct QIONetListenerClientWaitData data = {
208 .sioc = NULL,
209 .loop = loop
211 size_t i;
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]),
223 G_IO_IN);
225 g_source_set_callback(sources[i],
226 (GSourceFunc)qio_net_listener_wait_client_func,
227 &data,
228 NULL);
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);
250 return data.sioc;
253 void qio_net_listener_disconnect(QIONetListener *listener)
255 size_t i;
257 if (!listener->connected) {
258 return;
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);
280 size_t i;
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);