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).
This commit is contained in:
John-Mark Bell 2023-12-27 19:12:07 +00:00
parent f027a80956
commit e3a6ad7173
3 changed files with 46 additions and 0 deletions

View File

@ -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), "")) {

View File

@ -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;
}

View File

@ -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