summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn-Mark Bell <jmb@netsurf-browser.org>2023-12-27 19:12:07 +0000
committerJohn-Mark Bell <jmb@netsurf-browser.org>2024-03-13 23:03:34 +0000
commite3a6ad7173d8746af0fde87a1ead69c1c7d61bfa (patch)
tree5fe6098aab43d1f47869d12754c802df1e07320f
parentf027a8095654e204f608ad9db5c9231646059128 (diff)
downloadnetsurf-e3a6ad7173d8746af0fde87a1ead69c1c7d61bfa.tar.gz
netsurf-e3a6ad7173d8746af0fde87a1ead69c1c7d61bfa.tar.bz2
Fetch/curl: expose socket open/close via fetch vtable
This allows frontends to customise the behaviour of sockets. The default implementation simply maps to socket(2)/close(2).
-rw-r--r--content/fetchers/curl.c20
-rw-r--r--desktop/gui_factory.c9
-rw-r--r--include/netsurf/fetch.h17
3 files changed, 46 insertions, 0 deletions
diff --git a/content/fetchers/curl.c b/content/fetchers/curl.c
index b1907448e..680e60456 100644
--- a/content/fetchers/curl.c
+++ b/content/fetchers/curl.c
@@ -1792,6 +1792,24 @@ fetch_curl_debug(CURL *handle,
}
+static curl_socket_t fetch_curl_socket_open(void *clientp,
+ curlsocktype purpose, struct curl_sockaddr *address)
+{
+ (void) clientp;
+ (void) purpose;
+
+ return (curl_socket_t) guit->fetch->socket_open(
+ address->family, address->socktype,
+ address->protocol);
+}
+
+static int fetch_curl_socket_close(void *clientp, curl_socket_t item)
+{
+ (void) clientp;
+
+ return guit->fetch->socket_close((int) item);
+}
+
/**
* Callback function for cURL.
*/
@@ -2047,6 +2065,8 @@ nserror fetch_curl_register(void)
SETOPT(CURLOPT_LOW_SPEED_TIME, 180L);
SETOPT(CURLOPT_NOSIGNAL, 1L);
SETOPT(CURLOPT_CONNECTTIMEOUT, nsoption_uint(curl_fetch_timeout));
+ SETOPT(CURLOPT_OPENSOCKETFUNCTION, fetch_curl_socket_open);
+ SETOPT(CURLOPT_CLOSESOCKETFUNCTION, fetch_curl_socket_close);
if (nsoption_charp(ca_bundle) &&
strcmp(nsoption_charp(ca_bundle), "")) {
diff --git a/desktop/gui_factory.c b/desktop/gui_factory.c
index 89f2e373f..c9dcd8b2b 100644
--- a/desktop/gui_factory.c
+++ b/desktop/gui_factory.c
@@ -20,6 +20,9 @@
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
#include "utils/config.h"
#include "utils/errors.h"
@@ -498,6 +501,12 @@ static nserror verify_fetch_register(struct gui_fetch_table *gft)
if (gft->mimetype == NULL) {
gft->mimetype = gui_default_mimetype;
}
+ if (gft->socket_open == NULL) {
+ gft->socket_open = socket;
+ }
+ if (gft->socket_close == NULL) {
+ gft->socket_close = close;
+ }
return NSERROR_OK;
}
diff --git a/include/netsurf/fetch.h b/include/netsurf/fetch.h
index 30b204868..156f4d1ef 100644
--- a/include/netsurf/fetch.h
+++ b/include/netsurf/fetch.h
@@ -99,6 +99,23 @@ struct gui_fetch_table {
*/
char *(*mimetype)(const char *ro_path);
+ /**
+ * Open a socket
+ *
+ * \param domain Communication domain
+ * \param type Socket type
+ * \param protocol Protocol
+ * \return Socket descriptor on success, -1 on error and errno set
+ */
+ int (*socket_open)(int domain, int type, int protocol);
+
+ /**
+ * Close a socket
+ *
+ * \param socket Socket descriptor
+ * \return 0 on success, -1 on error and errno set
+ */
+ int (*socket_close)(int socket);
};
#endif