PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/include/io/net-listener.h

https://gitlab.com/storedmirrors/qemu
C Header | 186 lines | 43 code | 24 blank | 119 comment | 0 complexity | 78c2436fe224f7702b9424abcc2f39f2 MD5 | raw file
  1. /*
  2. * QEMU network listener
  3. *
  4. * Copyright (c) 2016-2017 Red Hat, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. *
  19. */
  20. #ifndef QIO_NET_LISTENER_H
  21. #define QIO_NET_LISTENER_H
  22. #include "io/channel-socket.h"
  23. #include "qom/object.h"
  24. #define TYPE_QIO_NET_LISTENER "qio-net-listener"
  25. OBJECT_DECLARE_SIMPLE_TYPE(QIONetListener,
  26. QIO_NET_LISTENER)
  27. typedef void (*QIONetListenerClientFunc)(QIONetListener *listener,
  28. QIOChannelSocket *sioc,
  29. gpointer data);
  30. /**
  31. * QIONetListener:
  32. *
  33. * The QIONetListener object encapsulates the management of a
  34. * listening socket. It is able to listen on multiple sockets
  35. * concurrently, to deal with the scenario where IPv4 / IPv6
  36. * needs separate sockets, or there is a need to listen on a
  37. * subset of interface IP addresses, instead of the wildcard
  38. * address.
  39. */
  40. struct QIONetListener {
  41. Object parent;
  42. char *name;
  43. QIOChannelSocket **sioc;
  44. GSource **io_source;
  45. size_t nsioc;
  46. bool connected;
  47. QIONetListenerClientFunc io_func;
  48. gpointer io_data;
  49. GDestroyNotify io_notify;
  50. };
  51. /**
  52. * qio_net_listener_new:
  53. *
  54. * Create a new network listener service, which is not
  55. * listening on any sockets initially.
  56. *
  57. * Returns: the new listener
  58. */
  59. QIONetListener *qio_net_listener_new(void);
  60. /**
  61. * qio_net_listener_set_name:
  62. * @listener: the network listener object
  63. * @name: the listener name
  64. *
  65. * Set the name of the listener. This is used as a debugging
  66. * aid, to set names on any GSource instances associated
  67. * with the listener
  68. */
  69. void qio_net_listener_set_name(QIONetListener *listener,
  70. const char *name);
  71. /**
  72. * qio_net_listener_open_sync:
  73. * @listener: the network listener object
  74. * @addr: the address to listen on
  75. * @num: the amount of expected connections
  76. * @errp: pointer to a NULL initialized error object
  77. *
  78. * Synchronously open a listening connection on all
  79. * addresses associated with @addr. This method may
  80. * also be invoked multiple times, in order to have a
  81. * single listener on multiple distinct addresses.
  82. */
  83. int qio_net_listener_open_sync(QIONetListener *listener,
  84. SocketAddress *addr,
  85. int num,
  86. Error **errp);
  87. /**
  88. * qio_net_listener_add:
  89. * @listener: the network listener object
  90. * @sioc: the socket I/O channel
  91. *
  92. * Associate a listening socket I/O channel with the
  93. * listener. The listener will acquire a new reference
  94. * on @sioc, so the caller should release its own reference
  95. * if it no longer requires the object.
  96. */
  97. void qio_net_listener_add(QIONetListener *listener,
  98. QIOChannelSocket *sioc);
  99. /**
  100. * qio_net_listener_set_client_func_full:
  101. * @listener: the network listener object
  102. * @func: the callback function
  103. * @data: opaque data to pass to @func
  104. * @notify: callback to free @data
  105. * @context: the context that the sources will be bound to. If %NULL,
  106. * the default context will be used.
  107. *
  108. * Register @func to be invoked whenever a new client
  109. * connects to the listener. @func will be invoked
  110. * passing in the QIOChannelSocket instance for the
  111. * client.
  112. */
  113. void qio_net_listener_set_client_func_full(QIONetListener *listener,
  114. QIONetListenerClientFunc func,
  115. gpointer data,
  116. GDestroyNotify notify,
  117. GMainContext *context);
  118. /**
  119. * qio_net_listener_set_client_func:
  120. * @listener: the network listener object
  121. * @func: the callback function
  122. * @data: opaque data to pass to @func
  123. * @notify: callback to free @data
  124. *
  125. * Wrapper of qio_net_listener_set_client_func_full(), only that the
  126. * sources will always be bound to default main context.
  127. */
  128. void qio_net_listener_set_client_func(QIONetListener *listener,
  129. QIONetListenerClientFunc func,
  130. gpointer data,
  131. GDestroyNotify notify);
  132. /**
  133. * qio_net_listener_wait_client:
  134. * @listener: the network listener object
  135. *
  136. * Block execution of the caller until a new client arrives
  137. * on one of the listening sockets. If there was previously
  138. * a callback registered with qio_net_listener_set_client_func
  139. * it will be temporarily disabled, and re-enabled afterwards.
  140. *
  141. * Returns: the new client socket
  142. */
  143. QIOChannelSocket *qio_net_listener_wait_client(QIONetListener *listener);
  144. /**
  145. * qio_net_listener_disconnect:
  146. * @listener: the network listener object
  147. *
  148. * Disconnect the listener, removing all I/O callback
  149. * watches and closing the socket channels.
  150. */
  151. void qio_net_listener_disconnect(QIONetListener *listener);
  152. /**
  153. * qio_net_listener_is_connected:
  154. * @listener: the network listener object
  155. *
  156. * Determine if the listener is connected to any socket
  157. * channels
  158. *
  159. * Returns: true if connected, false otherwise
  160. */
  161. bool qio_net_listener_is_connected(QIONetListener *listener);
  162. #endif /* QIO_NET_LISTENER_H */