const is fine on ares__channel_[un]lock (#758)

at https://github.com/c-ares/c-ares/pull/601#issuecomment-1801935063 you
chose not to scatter `const` on the public interface because of the plan
- now realised - to add threading to c-ares, and in the expectation that
even read operations would need to lock the mutex.

But the threading implementation has a _pointer_ to a mutex inside the
ares channel and as I understand it, that means that it is just fine to
mark `ares__channel_lock` (and `ares__channel_unlock`) as taking a
`const` channel. It is the pointed-to mutex that is not constant, but C
does not propagate `const`-ness through pointers.

This PR sprinkles const where appropriate on public interfaces.

Fix By: David Hotham (@dimbleby)
pull/759/head
David Hotham 6 months ago committed by GitHub
parent 85fe3d5a73
commit 2f200b9170
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      docs/ares_dup.3
  2. 2
      docs/ares_fds.3
  3. 4
      docs/ares_get_servers.3
  4. 2
      docs/ares_getsock.3
  5. 2
      docs/ares_queue.3
  6. 2
      docs/ares_save_options.3
  7. 2
      docs/ares_set_servers_csv.3
  8. 2
      docs/ares_timeout.3
  9. 32
      include/ares.h
  10. 4
      src/lib/ares__threads.c
  11. 2
      src/lib/ares_fds.c
  12. 2
      src/lib/ares_getsock.c
  13. 2
      src/lib/ares_init.c
  14. 18
      src/lib/ares_options.c
  15. 4
      src/lib/ares_private.h
  16. 2
      src/lib/ares_send.c
  17. 4
      src/lib/ares_timeout.c
  18. 11
      src/lib/ares_update_servers.c

@ -9,7 +9,7 @@ ares_dup \- Duplicate a resolver channel
.nf .nf
#include <ares.h> #include <ares.h>
int ares_dup(ares_channel_t **\fIdest\fP, ares_channel_t *\fIsource\fP) int ares_dup(ares_channel_t **\fIdest\fP, const ares_channel_t *\fIsource\fP)
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION
The \fBares_dup(3)\fP function duplicates an existing communications channel The \fBares_dup(3)\fP function duplicates an existing communications channel
@ -23,4 +23,3 @@ handle when the channel is no longer needed.
.BR ares_library_init (3) .BR ares_library_init (3)
.SH AVAILABILITY .SH AVAILABILITY
\fIares_dup(3)\fP was added in c-ares 1.6.0 \fIares_dup(3)\fP was added in c-ares 1.6.0

@ -9,7 +9,7 @@ ares_fds \- return file descriptors to select on (deprecated)
.nf .nf
#include <ares.h> #include <ares.h>
int ares_fds(ares_channel_t *\fIchannel\fP, int ares_fds(const ares_channel_t *\fIchannel\fP,
fd_set *\fIread_fds\fP, fd_set *\fIread_fds\fP,
fd_set *\fIwrite_fds\fP) fd_set *\fIwrite_fds\fP)
.fi .fi

@ -10,10 +10,10 @@ ares_get_servers, ares_get_servers_ports \- Retrieve name servers from an initia
.nf .nf
#include <ares.h> #include <ares.h>
int ares_get_servers(ares_channel_t *\fIchannel\fP, int ares_get_servers(const ares_channel_t *\fIchannel\fP,
struct ares_addr_node **\fIservers\fP) struct ares_addr_node **\fIservers\fP)
int ares_get_servers_ports(ares_channel_t *\fIchannel\fP, int ares_get_servers_ports(const ares_channel_t *\fIchannel\fP,
struct ares_addr_port_node **\fIservers\fP) struct ares_addr_port_node **\fIservers\fP)
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION

@ -9,7 +9,7 @@ ares_getsock \- get socket descriptors to wait on (deprecated)
.nf .nf
#include <ares.h> #include <ares.h>
int ares_getsock(ares_channel_t *\fIchannel\fP, ares_socket_t *\fIsocks\fP, int ares_getsock(const ares_channel_t *\fIchannel\fP, ares_socket_t *\fIsocks\fP,
int \fInumsocks\fP); int \fInumsocks\fP);
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION

@ -10,7 +10,7 @@ c-ares queue status
.nf .nf
#include <ares.h> #include <ares.h>
size_t ares_queue_active_queries(ares_channel_t *channel); size_t ares_queue_active_queries(const ares_channel_t *channel);
ares_status_t ares_queue_wait_empty(ares_channel_t *channel, ares_status_t ares_queue_wait_empty(ares_channel_t *channel,
int timeout_ms); int timeout_ms);

@ -9,7 +9,7 @@ ares_save_options \- Save configuration values obtained from initialized ares_ch
.nf .nf
#include <ares.h> #include <ares.h>
int ares_save_options(ares_channel_t *\fIchannel\fP, int ares_save_options(const ares_channel_t *\fIchannel\fP,
struct ares_options *\fIoptions\fP, int *\fIoptmask\fP) struct ares_options *\fIoptions\fP, int *\fIoptmask\fP)
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION

@ -14,7 +14,7 @@ int ares_set_servers_csv(ares_channel_t *\fIchannel\fP, const char* \fIservers\f
int ares_set_servers_ports_csv(ares_channel_t *\fIchannel\fP, const char* \fIservers\fP) int ares_set_servers_ports_csv(ares_channel_t *\fIchannel\fP, const char* \fIservers\fP)
char *ares_get_servers_csv(ares_channel_t *\fIchannel\fP) char *ares_get_servers_csv(const ares_channel_t *\fIchannel\fP)
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION
The \fBares_set_servers_csv\fP and \fBares_set_servers_ports_csv\fP functions set The \fBares_set_servers_csv\fP and \fBares_set_servers_ports_csv\fP functions set

@ -9,7 +9,7 @@ ares_timeout \- return maximum time to wait
.nf .nf
#include <ares.h> #include <ares.h>
struct timeval *ares_timeout(ares_channel_t *\fIchannel\fP, struct timeval *ares_timeout(const ares_channel_t *\fIchannel\fP,
struct timeval *\fImaxtv\fP, struct timeval *\fImaxtv\fP,
struct timeval *\fItv\fP) struct timeval *\fItv\fP)
.fi .fi

@ -477,12 +477,12 @@ CARES_EXTERN int ares_init_options(ares_channel_t **channelp
const struct ares_options *options, const struct ares_options *options,
int optmask); int optmask);
CARES_EXTERN int ares_save_options(ares_channel_t *channel, CARES_EXTERN int ares_save_options(const ares_channel_t *channel,
struct ares_options *options, int *optmask); struct ares_options *options, int *optmask);
CARES_EXTERN void ares_destroy_options(struct ares_options *options); CARES_EXTERN void ares_destroy_options(struct ares_options *options);
CARES_EXTERN int ares_dup(ares_channel_t **dest, ares_channel_t *src); CARES_EXTERN int ares_dup(ares_channel_t **dest, const ares_channel_t *src);
CARES_EXTERN ares_status_t ares_reinit(ares_channel_t *channel); CARES_EXTERN ares_status_t ares_reinit(ares_channel_t *channel);
@ -512,8 +512,10 @@ CARES_EXTERN void ares_set_socket_callback(ares_channel_t *ch
CARES_EXTERN void ares_set_socket_configure_callback( CARES_EXTERN void ares_set_socket_configure_callback(
ares_channel_t *channel, ares_sock_config_callback callback, void *user_data); ares_channel_t *channel, ares_sock_config_callback callback, void *user_data);
CARES_EXTERN void ares_set_server_state_callback( CARES_EXTERN void
ares_channel_t *channel, ares_server_state_callback callback, void *user_data); ares_set_server_state_callback(ares_channel_t *channel,
ares_server_state_callback callback,
void *user_data);
CARES_EXTERN int ares_set_sortlist(ares_channel_t *channel, CARES_EXTERN int ares_set_sortlist(ares_channel_t *channel,
const char *sortstr); const char *sortstr);
@ -632,15 +634,15 @@ CARES_EXTERN void ares_getnameinfo(ares_channel_t *channel,
CARES_EXTERN CARES_DEPRECATED_FOR( CARES_EXTERN CARES_DEPRECATED_FOR(
ARES_OPT_EVENT_THREAD or ARES_OPT_EVENT_THREAD or
ARES_OPT_SOCK_STATE_CB) int ares_fds(ares_channel_t *channel, ARES_OPT_SOCK_STATE_CB) int ares_fds(const ares_channel_t *channel,
fd_set *read_fds, fd_set *write_fds); fd_set *read_fds, fd_set *write_fds);
CARES_EXTERN CARES_DEPRECATED_FOR( CARES_EXTERN CARES_DEPRECATED_FOR(
ARES_OPT_EVENT_THREAD or ARES_OPT_EVENT_THREAD or
ARES_OPT_SOCK_STATE_CB) int ares_getsock(ares_channel_t *channel, ARES_OPT_SOCK_STATE_CB) int ares_getsock(const ares_channel_t *channel,
ares_socket_t *socks, int numsocks); ares_socket_t *socks, int numsocks);
CARES_EXTERN struct timeval *ares_timeout(ares_channel_t *channel, CARES_EXTERN struct timeval *ares_timeout(const ares_channel_t *channel,
struct timeval *maxtv, struct timeval *maxtv,
struct timeval *tv); struct timeval *tv);
@ -866,22 +868,24 @@ CARES_EXTERN CARES_DEPRECATED_FOR(ares_set_servers_csv) int ares_set_servers(
ares_channel_t *channel, const struct ares_addr_node *servers); ares_channel_t *channel, const struct ares_addr_node *servers);
CARES_EXTERN CARES_EXTERN
CARES_DEPRECATED_FOR(ares_set_servers_ports_csv) int ares_set_servers_ports( CARES_DEPRECATED_FOR(ares_set_servers_ports_csv)
ares_channel_t *channel, const struct ares_addr_port_node *servers); int ares_set_servers_ports(ares_channel_t *channel,
const struct ares_addr_port_node *servers);
/* Incoming string format: host[:port][,host[:port]]... */ /* Incoming string format: host[:port][,host[:port]]... */
CARES_EXTERN int ares_set_servers_csv(ares_channel_t *channel, CARES_EXTERN int ares_set_servers_csv(ares_channel_t *channel,
const char *servers); const char *servers);
CARES_EXTERN int ares_set_servers_ports_csv(ares_channel_t *channel, CARES_EXTERN int ares_set_servers_ports_csv(ares_channel_t *channel,
const char *servers); const char *servers);
CARES_EXTERN char *ares_get_servers_csv(ares_channel_t *channel); CARES_EXTERN char *ares_get_servers_csv(const ares_channel_t *channel);
CARES_EXTERN CARES_DEPRECATED_FOR(ares_get_servers_csv) int ares_get_servers( CARES_EXTERN CARES_DEPRECATED_FOR(ares_get_servers_csv) int ares_get_servers(
ares_channel_t *channel, struct ares_addr_node **servers); const ares_channel_t *channel, struct ares_addr_node **servers);
CARES_EXTERN CARES_EXTERN
CARES_DEPRECATED_FOR(ares_get_servers_ports_csv) int ares_get_servers_ports( CARES_DEPRECATED_FOR(ares_get_servers_ports_csv)
ares_channel_t *channel, struct ares_addr_port_node **servers); int ares_get_servers_ports(const ares_channel_t *channel,
struct ares_addr_port_node **servers);
CARES_EXTERN const char *ares_inet_ntop(int af, const void *src, char *dst, CARES_EXTERN const char *ares_inet_ntop(int af, const void *src, char *dst,
ares_socklen_t size); ares_socklen_t size);
@ -915,7 +919,7 @@ CARES_EXTERN ares_status_t ares_queue_wait_empty(ares_channel_t *channel,
* \param[in] channel Initialized ares channel * \param[in] channel Initialized ares channel
* \return Number of active queries to servers * \return Number of active queries to servers
*/ */
CARES_EXTERN size_t ares_queue_active_queries(ares_channel_t *channel); CARES_EXTERN size_t ares_queue_active_queries(const ares_channel_t *channel);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -537,12 +537,12 @@ void ares__channel_threading_destroy(ares_channel_t *channel)
channel->cond_empty = NULL; channel->cond_empty = NULL;
} }
void ares__channel_lock(ares_channel_t *channel) void ares__channel_lock(const ares_channel_t *channel)
{ {
ares__thread_mutex_lock(channel->lock); ares__thread_mutex_lock(channel->lock);
} }
void ares__channel_unlock(ares_channel_t *channel) void ares__channel_unlock(const ares_channel_t *channel)
{ {
ares__thread_mutex_unlock(channel->lock); ares__thread_mutex_unlock(channel->lock);
} }

@ -30,7 +30,7 @@
#include "ares.h" #include "ares.h"
#include "ares_private.h" #include "ares_private.h"
int ares_fds(ares_channel_t *channel, fd_set *read_fds, fd_set *write_fds) int ares_fds(const ares_channel_t *channel, fd_set *read_fds, fd_set *write_fds)
{ {
ares_socket_t nfds; ares_socket_t nfds;
ares__slist_node_t *snode; ares__slist_node_t *snode;

@ -29,7 +29,7 @@
#include "ares.h" #include "ares.h"
#include "ares_private.h" #include "ares_private.h"
int ares_getsock(ares_channel_t *channel, ares_socket_t *socks, int ares_getsock(const ares_channel_t *channel, ares_socket_t *socks,
int numsocks) /* size of the 'socks' array */ int numsocks) /* size of the 'socks' array */
{ {
ares__slist_node_t *snode; ares__slist_node_t *snode;

@ -426,7 +426,7 @@ ares_status_t ares_reinit(ares_channel_t *channel)
/* ares_dup() duplicates a channel handle with all its options and returns a /* ares_dup() duplicates a channel handle with all its options and returns a
new channel handle */ new channel handle */
int ares_dup(ares_channel_t **dest, ares_channel_t *src) int ares_dup(ares_channel_t **dest, const ares_channel_t *src)
{ {
struct ares_options opts; struct ares_options opts;
ares_status_t rc; ares_status_t rc;

@ -53,7 +53,7 @@ void ares_destroy_options(struct ares_options *options)
ares_free(options->hosts_path); ares_free(options->hosts_path);
} }
static struct in_addr *ares_save_opt_servers(ares_channel_t *channel, static struct in_addr *ares_save_opt_servers(const ares_channel_t *channel,
int *nservers) int *nservers)
{ {
ares__slist_node_t *snode; ares__slist_node_t *snode;
@ -82,8 +82,8 @@ static struct in_addr *ares_save_opt_servers(ares_channel_t *channel,
} }
/* Save options from initialized channel */ /* Save options from initialized channel */
int ares_save_options(ares_channel_t *channel, struct ares_options *options, int ares_save_options(const ares_channel_t *channel,
int *optmask) struct ares_options *options, int *optmask)
{ {
size_t i; size_t i;
@ -231,10 +231,8 @@ int ares_save_options(ares_channel_t *channel, struct ares_options *options,
/* Set options for server failover behavior */ /* Set options for server failover behavior */
if (channel->optmask & ARES_OPT_SERVER_FAILOVER) { if (channel->optmask & ARES_OPT_SERVER_FAILOVER) {
options->server_failover_opts.retry_chance = options->server_failover_opts.retry_chance = channel->server_retry_chance;
channel->server_retry_chance; options->server_failover_opts.retry_delay = channel->server_retry_delay;
options->server_failover_opts.retry_delay =
channel->server_retry_delay;
} }
*optmask = (int)channel->optmask; *optmask = (int)channel->optmask;
@ -484,10 +482,8 @@ ares_status_t ares__init_by_options(ares_channel_t *channel,
/* Set fields for server failover behavior */ /* Set fields for server failover behavior */
if (optmask & ARES_OPT_SERVER_FAILOVER) { if (optmask & ARES_OPT_SERVER_FAILOVER) {
channel->server_retry_chance = channel->server_retry_chance = options->server_failover_opts.retry_chance;
options->server_failover_opts.retry_chance; channel->server_retry_delay = options->server_failover_opts.retry_delay;
channel->server_retry_delay =
options->server_failover_opts.retry_delay;
} }
channel->optmask = (unsigned int)optmask; channel->optmask = (unsigned int)optmask;

@ -642,8 +642,8 @@ ares_status_t ares_qcache_fetch(ares_channel_t *channel,
ares_status_t ares__channel_threading_init(ares_channel_t *channel); ares_status_t ares__channel_threading_init(ares_channel_t *channel);
void ares__channel_threading_destroy(ares_channel_t *channel); void ares__channel_threading_destroy(ares_channel_t *channel);
void ares__channel_lock(ares_channel_t *channel); void ares__channel_lock(const ares_channel_t *channel);
void ares__channel_unlock(ares_channel_t *channel); void ares__channel_unlock(const ares_channel_t *channel);
struct ares_event_thread; struct ares_event_thread;
typedef struct ares_event_thread ares_event_thread_t; typedef struct ares_event_thread ares_event_thread_t;

@ -200,7 +200,7 @@ void ares_send(ares_channel_t *channel, const unsigned char *qbuf, int qlen,
ares_dns_record_destroy(dnsrec); ares_dns_record_destroy(dnsrec);
} }
size_t ares_queue_active_queries(ares_channel_t *channel) size_t ares_queue_active_queries(const ares_channel_t *channel)
{ {
size_t len; size_t len;

@ -55,8 +55,8 @@ void ares__timeval_remaining(struct timeval *remaining,
} }
} }
struct timeval *ares_timeout(ares_channel_t *channel, struct timeval *maxtv, struct timeval *ares_timeout(const ares_channel_t *channel,
struct timeval *tvbuf) struct timeval *maxtv, struct timeval *tvbuf)
{ {
const struct query *query; const struct query *query;
ares__slist_node_t *node; ares__slist_node_t *node;

@ -370,7 +370,6 @@ static ares_status_t ares__sconfig_linklocal(ares_sconfig_t *s,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
ares_status_t ares__sconfig_append(ares__llist_t **sconfig, ares_status_t ares__sconfig_append(ares__llist_t **sconfig,
const struct ares_addr *addr, const struct ares_addr *addr,
unsigned short udp_port, unsigned short udp_port,
@ -973,7 +972,8 @@ ares_status_t ares_get_server_addr(const struct server_state *server,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
int ares_get_servers(ares_channel_t *channel, struct ares_addr_node **servers) int ares_get_servers(const ares_channel_t *channel,
struct ares_addr_node **servers)
{ {
struct ares_addr_node *srvr_head = NULL; struct ares_addr_node *srvr_head = NULL;
struct ares_addr_node *srvr_last = NULL; struct ares_addr_node *srvr_last = NULL;
@ -1027,7 +1027,7 @@ int ares_get_servers(ares_channel_t *channel, struct ares_addr_node **servers)
return (int)status; return (int)status;
} }
int ares_get_servers_ports(ares_channel_t *channel, int ares_get_servers_ports(const ares_channel_t *channel,
struct ares_addr_port_node **servers) struct ares_addr_port_node **servers)
{ {
struct ares_addr_port_node *srvr_head = NULL; struct ares_addr_port_node *srvr_head = NULL;
@ -1175,7 +1175,7 @@ int ares_set_servers_ports_csv(ares_channel_t *channel, const char *_csv)
return (int)set_servers_csv(channel, _csv); return (int)set_servers_csv(channel, _csv);
} }
char *ares_get_servers_csv(ares_channel_t *channel) char *ares_get_servers_csv(const ares_channel_t *channel)
{ {
ares__buf_t *buf = NULL; ares__buf_t *buf = NULL;
char *out = NULL; char *out = NULL;
@ -1216,8 +1216,7 @@ done:
} }
void ares_set_server_state_callback(ares_channel_t *channel, void ares_set_server_state_callback(ares_channel_t *channel,
ares_server_state_callback cb, ares_server_state_callback cb, void *data)
void *data)
{ {
if (channel == NULL) { if (channel == NULL) {
return; return;

Loading…
Cancel
Save