Asterisk - The Open Source Telephony Project GIT-master-abe0018
Data Structures | Functions
Open Socket Callback
Collaboration diagram for Open Socket Callback:

Data Structures

struct  curl_open_socket_data
 Context structure passed to ast_curl_open_socket_default_cb. More...
 

Functions

curl_socket_t curl_open_socket_cb (void *client_data, curlsocktype purpose, struct curl_sockaddr *address)
 A default implementation of an open socket callback. More...
 
void curl_open_socket_data_free (void *obj)
 

Detailed Description

If you need to allocate the socket curl uses to make the request yourself or you need to do some checking on the request's resolved IP address, this is the callback for you.

Your callback must follow the specification defined for CURLOPT_OPENSOCKETFUNCTION and implement the 'curl_opensocket_callback' prototype.

The following ast_open_socket objects compose a default implementation that will not allow requests to servers not whitelisted in the provided ast_acl_list.

Function Documentation

◆ curl_open_socket_cb()

curl_socket_t curl_open_socket_cb ( void *  client_data,
curlsocktype  purpose,
struct curl_sockaddr *  address 
)

A default implementation of an open socket callback.

This is an implementation of the function described by CURLOPT_OPENSOCKETFUNCTION that checks the request's IP address against a user-supplied ast_acl_list and either rejects the request if the IP address isn't allowed, or opens a socket and returns it to curl. See the CURLOPT_OPENSOCKETFUNCTION documentation for more info.

Parameters
client_dataA pointer to whatever structure you passed to ast_curler in the curl_write_data parameter.
purposeWill always be CURLSOCKTYPE_IPCXN
addressThe request server's resolved IP address
Returns
A socket opened by socket() or -1 to signal an error.

Definition at line 205 of file curl_utils.c.

207{
208 struct curl_open_socket_data *cb_data = client_data;
209 char *debug_info = S_OR(cb_data->debug_info, "");
210 SCOPE_ENTER(5, "'%s': Opening socket\n", debug_info);
211
212 if (!ast_acl_list_is_empty((struct ast_acl_list *)cb_data->acl)) {
213 struct ast_sockaddr ast_address = { {0,} };
214
215 ast_sockaddr_copy_sockaddr(&ast_address, &address->addr, address->addrlen);
216
217 if (ast_apply_acl((struct ast_acl_list *)cb_data->acl, &ast_address, NULL) != AST_SENSE_ALLOW) {
218 SCOPE_EXIT_LOG_RTN_VALUE(CURL_SOCKET_BAD, LOG_WARNING,
219 "'%s': Unable to apply acl\n", debug_info);
220 }
221 }
222
223 cb_data->sockfd = socket(address->family, address->socktype, address->protocol);
224 if (cb_data->sockfd < 0) {
225 SCOPE_EXIT_LOG_RTN_VALUE(CURL_SOCKET_BAD, LOG_WARNING,
226 "'%s': Failed to open socket: %s\n", debug_info, strerror(errno));
227 }
228
229 SCOPE_EXIT_RTN_VALUE(cb_data->sockfd, "Success");
230}
enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
Apply a set of rules to a given IP address.
Definition: acl.c:799
@ AST_SENSE_ALLOW
Definition: acl.h:38
int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
Determines if an ACL is empty or if it contains entries.
Definition: acl.c:540
char * address
Definition: f2c.h:59
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
#define SCOPE_ENTER(level,...)
#define LOG_WARNING
int errno
static void ast_sockaddr_copy_sockaddr(struct ast_sockaddr *dst, struct sockaddr *src, socklen_t len)
Copies the data from a sockaddr to an ast_sockaddr.
Definition: netsock2.h:151
#define NULL
Definition: resample.c:96
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
Wrapper for an ast_acl linked list.
Definition: acl.h:76
Socket address structure.
Definition: netsock2.h:97
Context structure passed to ast_curl_open_socket_default_cb.
Definition: curl_utils.h:341
const struct ast_acl_list * acl
Definition: curl_utils.h:346
curl_socket_t sockfd
Definition: curl_utils.h:355

References curl_open_socket_data::acl, ast_acl_list_is_empty(), ast_apply_acl(), AST_SENSE_ALLOW, ast_sockaddr_copy_sockaddr(), curl_open_socket_data::debug_info, errno, LOG_WARNING, NULL, S_OR, SCOPE_ENTER, SCOPE_EXIT_LOG_RTN_VALUE, SCOPE_EXIT_RTN_VALUE, and curl_open_socket_data::sockfd.

Referenced by curler().

◆ curl_open_socket_data_free()

void curl_open_socket_data_free ( void *  obj)

Definition at line 193 of file curl_utils.c.

194{
195 struct curl_open_socket_data *cb_data = obj;
196 if (!cb_data) {
197 return;
198 }
199 if (cb_data->debug_info) {
200 ast_free(cb_data->debug_info);
201 }
202 ast_free(cb_data);
203}
#define ast_free(a)
Definition: astmm.h:180

References ast_free, and curl_open_socket_data::debug_info.

Referenced by retrieve_cert_from_url().