pidgin/pidgin

Add a helper to open ports, respecting user settings.

14 months ago, Elliott Sales de Andrade
ae0175720477
Parents bba8505129d5
Children 8a20b931d967
Add a helper to open ports, respecting user settings.

If opening "any" port, try the ones that the user has configured, if
set.
--- a/libpurple/purple-gio.c Tue Feb 04 03:05:18 2020 +0000
+++ b/libpurple/purple-gio.c Tue Feb 04 02:52:22 2020 -0500
@@ -129,3 +129,29 @@
return client;
}
+guint16
+purple_socket_listener_add_any_inet_port(GSocketListener *listener,
+ GObject *source_object, GError **error)
+{
+ GError *internal_error = NULL;
+ guint16 port, start, end;
+
+ if (!purple_prefs_get_bool("/purple/network/ports_range_use")) {
+ return g_socket_listener_add_any_inet_port(listener, source_object,
+ error);
+ }
+
+ start = purple_prefs_get_int("/purple/network/ports_range_start");
+ end = purple_prefs_get_int("/purple/network/ports_range_end");
+ for (port = start; port <= end; port++) {
+ if (g_socket_listener_add_inet_port(listener, port, source_object,
+ &internal_error)) {
+ return port;
+ } else if (port != end) {
+ g_error_free(internal_error);
+ }
+ }
+
+ g_propagate_error(error, internal_error);
+ return 0;
+}
--- a/libpurple/purple-gio.h Tue Feb 04 03:05:18 2020 +0000
+++ b/libpurple/purple-gio.h Tue Feb 04 02:52:22 2020 -0500
@@ -71,6 +71,23 @@
GSocketClient *
purple_gio_socket_client_new(PurpleAccount *account, GError **error);
+/**
+ * purple_socket_listener_add_any_inet_port:
+ * @listener: A #GSocketListener.
+ * @source_object: (nullable): Optional GObject identifying this source.
+ * @error: A #GError location to store the error occurring, or %NULL to ignore.
+ *
+ * Listens for TCP connections on any available port number for both IPv6 and
+ * IPv4 (if each is available). This is a simple wrapper around
+ * g_socket_listener_add_any_inet_port(), except if the user specified a port
+ * range in the settings, than a port will be chosen from that range.
+ *
+ * Returns: The port number, or 0 in case of failure.
+ */
+guint16 purple_socket_listener_add_any_inet_port(GSocketListener *listener,
+ GObject *source_object,
+ GError **error);
+
G_END_DECLS
#endif /* PURPLE_GIO_H */