From b18ed67d16e60b0cfe25b5d3b5ff3b62a2b467dc Mon Sep 17 00:00:00 2001 From: Mark Michelson Date: Mon, 9 Dec 2013 16:10:05 +0000 Subject: Switch PJSIP auth to use a vector. Since Asterisk has a vector API now, places where arrays are manually resized don't really make sense any more. Since the auth work in PJSIP was freshly-written, it was easy to reform it to use a vector. Review: https://reviewboard.asterisk.org/r/3044 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@403499 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- res/res_pjsip.c | 2 +- res/res_pjsip/config_auth.c | 11 +++-- res/res_pjsip/pjsip_configuration.c | 67 +++++++++++++-------------- res/res_pjsip/pjsip_distributor.c | 2 +- res/res_pjsip_authenticator_digest.c | 17 ++++--- res/res_pjsip_outbound_authenticator_digest.c | 17 +++---- res/res_pjsip_outbound_registration.c | 31 ++++++------- 7 files changed, 72 insertions(+), 75 deletions(-) (limited to 'res') diff --git a/res/res_pjsip.c b/res/res_pjsip.c index d5b870f1f..52a949257 100644 --- a/res/res_pjsip.c +++ b/res/res_pjsip.c @@ -1194,7 +1194,7 @@ void ast_sip_unregister_outbound_authenticator(struct ast_sip_outbound_authentic ast_module_unref(ast_module_info->self); } -int ast_sip_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge, +int ast_sip_create_request_with_auth(const struct ast_sip_auth_vector *auths, pjsip_rx_data *challenge, pjsip_transaction *tsx, pjsip_tx_data **new_request) { if (!registered_outbound_authenticator) { diff --git a/res/res_pjsip/config_auth.c b/res/res_pjsip/config_auth.c index afa26867e..047e9337d 100644 --- a/res/res_pjsip/config_auth.c +++ b/res/res_pjsip/config_auth.c @@ -118,19 +118,20 @@ static int auth_apply(const struct ast_sorcery *sorcery, void *obj) return res; } -int ast_sip_for_each_auth(const struct ast_sip_auth_array *array, +int ast_sip_for_each_auth(const struct ast_sip_auth_vector *vector, ao2_callback_fn on_auth, void *arg) { int i; - if (!array || !array->num) { + if (!vector || !AST_VECTOR_SIZE(vector)) { return 0; } - for (i = 0; i < array->num; ++i) { + for (i = 0; i < AST_VECTOR_SIZE(vector); ++i) { + /* AST_VECTOR_GET is safe to use since the vector is immutable */ RAII_VAR(struct ast_sip_auth *, auth, ast_sorcery_retrieve_by_id( ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, - array->names[i]), ao2_cleanup); + AST_VECTOR_GET(vector,i)), ao2_cleanup); if (!auth) { continue; @@ -175,7 +176,7 @@ static int format_ami_auth_handler(void *obj, void *arg, int flags) return 0; } -int ast_sip_format_auths_ami(const struct ast_sip_auth_array *auths, +int ast_sip_format_auths_ami(const struct ast_sip_auth_vector *auths, struct ast_sip_ami *ami) { return ast_sip_for_each_auth(auths, format_ami_auth_handler, ami); diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c index 8ee907ba7..3b9bfd6a7 100644 --- a/res/res_pjsip/pjsip_configuration.c +++ b/res/res_pjsip/pjsip_configuration.c @@ -331,55 +331,47 @@ static int timers_to_str(const void *obj, const intptr_t *args, char **buf) return 0; } -void ast_sip_auth_array_destroy(struct ast_sip_auth_array *auths) +void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths) { int i; + size_t size; if (!auths) { return; } - for (i = 0; i < auths->num; ++i) { - ast_free((char *) auths->names[i]); + size = AST_VECTOR_SIZE(auths); + + for (i = 0; i < size; ++i) { + const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0); + ast_free((char *) name); } - ast_free(auths->names); - auths->names = NULL; - auths->num = 0; + AST_VECTOR_FREE(auths); } -#define AUTH_INCREMENT 4 - -int ast_sip_auth_array_init(struct ast_sip_auth_array *auths, const char *value) +int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value) { char *auth_names = ast_strdupa(value); char *val; - int num_alloced = 0; - const char **alloced_auths; ast_assert(auths != NULL); - ast_assert(auths->names == NULL); - ast_assert(!auths->num); + ast_assert(AST_VECTOR_SIZE(auths) == 0); + + AST_VECTOR_INIT(auths, 1); while ((val = strsep(&auth_names, ","))) { - if (auths->num >= num_alloced) { - num_alloced += AUTH_INCREMENT; - alloced_auths = ast_realloc(auths->names, num_alloced * sizeof(char *)); - if (!alloced_auths) { - goto failure; - } - auths->names = alloced_auths; - } val = ast_strdup(val); if (!val) { goto failure; } - auths->names[auths->num] = val; - ++auths->num; + if (AST_VECTOR_APPEND(auths, val)) { + goto failure; + } } return 0; failure: - ast_sip_auth_array_destroy(auths); + ast_sip_auth_vector_destroy(auths); return -1; } @@ -387,19 +379,19 @@ static int inbound_auth_handler(const struct aco_option *opt, struct ast_variabl { struct ast_sip_endpoint *endpoint = obj; - return ast_sip_auth_array_init(&endpoint->inbound_auths, var->value); + return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value); } static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) { struct ast_sip_endpoint *endpoint = obj; - return ast_sip_auth_array_init(&endpoint->outbound_auths, var->value); + return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value); } -int ast_sip_auths_to_str(const struct ast_sip_auth_array *auths, char **buf) +int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf) { - if (!auths || !auths->num) { + if (!auths || !AST_VECTOR_SIZE(auths)) { return 0; } @@ -407,7 +399,8 @@ int ast_sip_auths_to_str(const struct ast_sip_auth_array *auths, char **buf) return -1; } - ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->names, auths->num, ','); + /* I feel like accessing the vector's elem array directly is cheating...*/ + ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ','); return 0; } @@ -1171,7 +1164,7 @@ static int ami_show_endpoint(struct mansession *s, const struct message *m) return 0; } -static int format_str_append_auth(const struct ast_sip_auth_array *auths, +static int format_str_append_auth(const struct ast_sip_auth_vector *auths, struct ast_str **buf) { char *str = NULL; @@ -1473,8 +1466,8 @@ static void endpoint_destructor(void* obj) subscription_configuration_destroy(&endpoint->subscription); info_configuration_destroy(&endpoint->info); media_configuration_destroy(&endpoint->media); - ast_sip_auth_array_destroy(&endpoint->inbound_auths); - ast_sip_auth_array_destroy(&endpoint->outbound_auths); + ast_sip_auth_vector_destroy(&endpoint->inbound_auths); + ast_sip_auth_vector_destroy(&endpoint->outbound_auths); ast_party_id_free(&endpoint->id.self); endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups); endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups); @@ -1535,14 +1528,16 @@ struct ao2_container *ast_sip_get_endpoints(void) return endpoints; } -int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_sip_auth **out) +int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out) { int i; - for (i = 0; i < auths->num; ++i) { - out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, auths->names[i]); + for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) { + /* Using AST_VECTOR_GET is safe since the vector is immutable */ + const char *name = AST_VECTOR_GET(auths, i); + out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name); if (!out[i]) { - ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", auths->names[i]); + ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name); return -1; } } diff --git a/res/res_pjsip/pjsip_distributor.c b/res/res_pjsip/pjsip_distributor.c index 5e53cb0fd..e59305ec5 100644 --- a/res/res_pjsip/pjsip_distributor.c +++ b/res/res_pjsip/pjsip_distributor.c @@ -227,7 +227,7 @@ static int create_artificial_endpoint(void) return -1; } - artificial_endpoint->inbound_auths.num = 1; + AST_VECTOR_INIT(&artificial_endpoint->inbound_auths, 1); return 0; } diff --git a/res/res_pjsip_authenticator_digest.c b/res/res_pjsip_authenticator_digest.c index 30da26cfd..4a84a402a 100644 --- a/res/res_pjsip_authenticator_digest.c +++ b/res/res_pjsip_authenticator_digest.c @@ -41,7 +41,7 @@ AO2_GLOBAL_OBJ_STATIC(entity_id); */ static int digest_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) { - return endpoint->inbound_auths.num > 0; + return AST_VECTOR_SIZE(&endpoint->inbound_auths) > 0; } static void auth_store_cleanup(void *data) @@ -384,12 +384,15 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint enum ast_sip_check_auth_result res; int i; int failures = 0; + size_t auth_size; RAII_VAR(struct ast_sip_endpoint *, artificial_endpoint, ast_sip_get_artificial_endpoint(), ao2_cleanup); - auths = ast_alloca(endpoint->inbound_auths.num * sizeof(*auths)); - verify_res = ast_alloca(endpoint->inbound_auths.num * sizeof(*verify_res)); + auth_size = AST_VECTOR_SIZE(&endpoint->inbound_auths); + + auths = ast_alloca(auth_size * sizeof(*auths)); + verify_res = ast_alloca(auth_size * sizeof(*verify_res)); if (!auths) { return AST_SIP_AUTHENTICATION_ERROR; @@ -402,7 +405,7 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint goto cleanup; } - for (i = 0; i < endpoint->inbound_auths.num; ++i) { + for (i = 0; i < auth_size; ++i) { if (ast_strlen_zero(auths[i]->realm)) { ast_string_field_set(auths[i], realm, "asterisk"); } @@ -416,18 +419,18 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint } } - for (i = 0; i < endpoint->inbound_auths.num; ++i) { + for (i = 0; i < auth_size; ++i) { challenge(auths[i]->realm, tdata, rdata, verify_res[i] == AUTH_STALE); } - if (failures == endpoint->inbound_auths.num) { + if (failures == auth_size) { res = AST_SIP_AUTHENTICATION_FAILED; } else { res = AST_SIP_AUTHENTICATION_CHALLENGE; } cleanup: - ast_sip_cleanup_auths(auths, endpoint->inbound_auths.num); + ast_sip_cleanup_auths(auths, auth_size); return res; } diff --git a/res/res_pjsip_outbound_authenticator_digest.c b/res/res_pjsip_outbound_authenticator_digest.c index e7e77dbd5..1e411f1c5 100644 --- a/res/res_pjsip_outbound_authenticator_digest.c +++ b/res/res_pjsip_outbound_authenticator_digest.c @@ -50,15 +50,16 @@ static pjsip_www_authenticate_hdr *get_auth_header(pjsip_rx_data *challenge) { } static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_sess, - const struct ast_sip_auth_array *array, pjsip_rx_data *challenge) + const struct ast_sip_auth_vector *auth_vector, pjsip_rx_data *challenge) { - struct ast_sip_auth **auths = ast_alloca(array->num * sizeof(*auths)); - pjsip_cred_info *auth_creds = ast_alloca(array->num * sizeof(*auth_creds)); + size_t auth_size = AST_VECTOR_SIZE(auth_vector); + struct ast_sip_auth **auths = ast_alloca(auth_size * sizeof(*auths)); + pjsip_cred_info *auth_creds = ast_alloca(auth_size * sizeof(*auth_creds)); pjsip_www_authenticate_hdr *auth_hdr = NULL; int res = 0; int i; - if (ast_sip_retrieve_auths(array, auths)) { + if (ast_sip_retrieve_auths(auth_vector, auths)) { res = -1; goto cleanup; } @@ -70,7 +71,7 @@ static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_ses goto cleanup; } - for (i = 0; i < array->num; ++i) { + for (i = 0; i < auth_size; ++i) { if (ast_strlen_zero(auths[i]->realm)) { auth_creds[i].realm = auth_hdr->challenge.common.realm; } else { @@ -93,14 +94,14 @@ static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_ses } } - pjsip_auth_clt_set_credentials(auth_sess, array->num, auth_creds); + pjsip_auth_clt_set_credentials(auth_sess, auth_size, auth_creds); cleanup: - ast_sip_cleanup_auths(auths, array->num); + ast_sip_cleanup_auths(auths, auth_size); return res; } -static int digest_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge, +static int digest_create_request_with_auth(const struct ast_sip_auth_vector *auths, pjsip_rx_data *challenge, pjsip_transaction *tsx, pjsip_tx_data **new_request) { pjsip_auth_clt_sess auth_sess; diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c index b2ccd3b95..725822709 100644 --- a/res/res_pjsip_outbound_registration.c +++ b/res/res_pjsip_outbound_registration.c @@ -192,9 +192,7 @@ struct sip_outbound_registration_client_state { /*! \brief Serializer for stuff and things */ struct ast_taskprocessor *serializer; /*! \brief Configured authentication credentials */ - struct ast_sip_auth_array outbound_auths; - /*! \brief Number of configured auths */ - size_t num_outbound_auths; + struct ast_sip_auth_vector outbound_auths; /*! \brief Registration should be destroyed after completion of transaction */ unsigned int destroy:1; }; @@ -235,9 +233,7 @@ struct sip_outbound_registration { /*! \brief Outbound registration state */ struct sip_outbound_registration_state *state; /*! \brief Configured authentication credentials */ - struct ast_sip_auth_array outbound_auths; - /*! \brief Number of configured auths */ - size_t num_outbound_auths; + struct ast_sip_auth_vector outbound_auths; }; /*! \brief Helper function which cancels the timer on a client */ @@ -334,7 +330,7 @@ static int handle_client_state_destruction(void *data) pjsip_regc_destroy(client_state->client); client_state->status = SIP_REGISTRATION_STOPPED; - ast_sip_auth_array_destroy(&client_state->outbound_auths); + ast_sip_auth_vector_destroy(&client_state->outbound_auths); return 0; } @@ -564,7 +560,7 @@ static void sip_outbound_registration_destroy(void *obj) struct sip_outbound_registration *registration = obj; ao2_cleanup(registration->state); - ast_sip_auth_array_destroy(®istration->outbound_auths); + ast_sip_auth_vector_destroy(®istration->outbound_auths); ast_string_field_free_memory(registration); } @@ -657,13 +653,14 @@ static int can_reuse_registration(struct sip_outbound_registration *existing, st if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->client_uri, applied->client_uri) || strcmp(existing->transport, applied->transport) || strcmp(existing->contact_user, applied->contact_user) || - strcmp(existing->outbound_proxy, applied->outbound_proxy) || existing->num_outbound_auths != applied->num_outbound_auths || + strcmp(existing->outbound_proxy, applied->outbound_proxy) || + AST_VECTOR_SIZE(&existing->outbound_auths) != AST_VECTOR_SIZE(&applied->outbound_auths) || existing->auth_rejection_permanent != applied->auth_rejection_permanent) { return 0; } - for (i = 0; i < existing->num_outbound_auths; ++i) { - if (strcmp(existing->outbound_auths.names[i], applied->outbound_auths.names[i])) { + for (i = 0; i < AST_VECTOR_SIZE(&existing->outbound_auths); ++i) { + if (strcmp(AST_VECTOR_GET(&existing->outbound_auths, i), AST_VECTOR_GET(&applied->outbound_auths, i))) { return 0; } } @@ -764,13 +761,13 @@ static int sip_outbound_registration_perform(void *data) size_t i; /* Just in case the client state is being reused for this registration, free the auth information */ - ast_sip_auth_array_destroy(®istration->state->client_state->outbound_auths); + ast_sip_auth_vector_destroy(®istration->state->client_state->outbound_auths); - registration->state->client_state->outbound_auths.names = ast_calloc(registration->outbound_auths.num, sizeof(char *)); - for (i = 0; i < registration->outbound_auths.num; ++i) { - registration->state->client_state->outbound_auths.names[i] = ast_strdup(registration->outbound_auths.names[i]); + AST_VECTOR_INIT(®istration->state->client_state->outbound_auths, AST_VECTOR_SIZE(®istration->outbound_auths)); + for (i = 0; i < AST_VECTOR_SIZE(®istration->outbound_auths); ++i) { + const char *name = ast_strdup(AST_VECTOR_GET(®istration->outbound_auths, i)); + AST_VECTOR_APPEND(®istration->state->client_state->outbound_auths, name); } - registration->state->client_state->outbound_auths.num = registration->outbound_auths.num; registration->state->client_state->retry_interval = registration->retry_interval; registration->state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval; registration->state->client_state->max_retries = registration->max_retries; @@ -808,7 +805,7 @@ static int outbound_auth_handler(const struct aco_option *opt, struct ast_variab { struct sip_outbound_registration *registration = obj; - return ast_sip_auth_array_init(®istration->outbound_auths, var->value); + return ast_sip_auth_vector_init(®istration->outbound_auths, var->value); } static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf) -- cgit v1.2.3