Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions
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_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.

References curl_open_socket_data::acl, ast_acl_list_is_empty(), ast_apply_acl(), ast_sockaddr_copy_sockaddr(), curl_open_socket_data::debug_info, and S_OR.

Referenced by curler().

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 }
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
Wrapper for an ast_acl linked list.
Definition: acl.h:76
Socket address structure.
Definition: netsock2.h:97
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
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
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
#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