diff --git a/src/libmongoc/src/mongoc/mcd-azure.c b/src/libmongoc/src/mongoc/mcd-azure.c index 18730311db9..e8173edc927 100644 --- a/src/libmongoc/src/mongoc/mcd-azure.c +++ b/src/libmongoc/src/mongoc/mcd-azure.c @@ -20,6 +20,8 @@ #include #include +#include +#include #define AZURE_API_VERSION "2018-02-01" @@ -122,7 +124,7 @@ mcd_azure_access_token_try_init_from_json_str(mcd_azure_access_token *out, // which the token will be valid. strtoll() will saturate on range errors // and return zero on parse errors. char *parse_end; - long long s = strtoll(expires_in_str, &parse_end, 0); + const long long expires_in = strtoll(expires_in_str, &parse_end, 0); if (parse_end != expires_in_str + expires_in_len) { // Did not parse the entire string. Bad _mongoc_set_error(error, @@ -132,7 +134,7 @@ mcd_azure_access_token_try_init_from_json_str(mcd_azure_access_token *out, mlib_in_range(int, expires_in_len) ? (int)expires_in_len : INT_MAX, expires_in_str); } else { - out->expires_in = mcd_seconds(s); + out->expires_in = mlib_duration(expires_in, s); okay = true; } } @@ -174,7 +176,7 @@ mcd_azure_access_token_from_imds(mcd_azure_access_token *const out, mcd_azure_imds_request req = MCD_AZURE_IMDS_REQUEST_INIT; mcd_azure_imds_request_init(&req, opt_imds_host, opt_port, opt_extra_headers); - if (!_mongoc_http_send(&req.req, 3 * 1000, false, NULL, &resp, error)) { + if (!_mongoc_http_send(&req.req, mlib_expires_after(mlib_duration(3, s)), false, NULL, &resp, error)) { goto fail; } diff --git a/src/libmongoc/src/mongoc/mcd-azure.h b/src/libmongoc/src/mongoc/mcd-azure.h index 5db11d72828..0c36d49ab13 100644 --- a/src/libmongoc/src/mongoc/mcd-azure.h +++ b/src/libmongoc/src/mongoc/mcd-azure.h @@ -21,9 +21,10 @@ #include -#include #include +#include + /** * @brief An Azure OAuth2 access token obtained from the Azure API */ @@ -36,7 +37,7 @@ typedef struct mcd_azure_access_token { char *token_type; /// The duration after which it will the token will expires. This is relative /// to the "issue time" of the token. - mcd_duration expires_in; + mlib_duration expires_in; } mcd_azure_access_token; /** diff --git a/src/libmongoc/src/mongoc/mcd-time.h b/src/libmongoc/src/mongoc/mcd-time.h deleted file mode 100644 index 1385bdcddfc..00000000000 --- a/src/libmongoc/src/mongoc/mcd-time.h +++ /dev/null @@ -1,348 +0,0 @@ -/** - * Copyright 2009-present MongoDB, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MCD_TIME_H_INCLUDED -#define MCD_TIME_H_INCLUDED - -#include - -#include - -#include - - -/** - * @brief Represents an abstract point-in-time. - * - * @note This is an *abstract* time point, with the only guarantee that it - * is strictly ordered with every other time point and that the difference - * between any two times will roughly encode actual wall-clock durations. - */ -typedef struct mcd_time_point { - /// The internal representation of the time. - int64_t _rep; -} mcd_time_point; - -/// The latest representable future point-in-time -#define MCD_TIME_POINT_MAX ((mcd_time_point){._rep = INT64_MAX}) -/// The oldest representable past point-in-time -#define MCD_TIME_POINT_MIN ((mcd_time_point){._rep = INT64_MIN}) - -/** - * @brief Represents a (possibly negative) duration of time. - * - * Construct this using one of the duration constructor functions. - * - * @note This encodes real wall-time durations, and may include negative - * durations. It can be compared with other durations and used to offset - * time points. - */ -typedef struct mcd_duration { - /// An internal representation of the duration - int64_t _rep; -} mcd_duration; - -/// The maximum representable duration -#define MCD_DURATION_MAX ((mcd_duration){._rep = INT64_MAX}) -/// The minimal representable (negative) duration -#define MCD_DURATION_MIN ((mcd_duration){._rep = INT64_MIN}) -/// A duration representing zero amount of time -#define MCD_DURATION_ZERO ((mcd_duration){._rep = 0}) - -/** - * @brief Obtain the current time point. This is only an abstract - * monotonically increasing time, and does not necessarily correlate with - * any real-world clock. - */ -static BSON_INLINE mcd_time_point -mcd_now(void) -{ - // Create a time point representing the current time. - return (mcd_time_point){._rep = bson_get_monotonic_time()}; -} - -/** - * @brief Create a duration from a number of microseconds. - * - * @param s A number of microseconds - * @return mcd_duration A duration corresponding to 's' microseconds. - * - * @note Saturates to the min/max duration if the duration is too great in - * magnitude. - */ -static BSON_INLINE mcd_duration -mcd_microseconds(int64_t s) -{ - // 'mcd_duration' is encoded in a number of microseconds, so we don't need to - // do bounds checking here. - return (mcd_duration){._rep = s}; -} - -/** - * @brief Create a duration from a number of milliseconds. - * - * @param s A number of milliseconds - * @return mcd_duration A duration corresponding to 's' milliseconds. - * - * @note Saturates to the min/max duration if the duration is too great in - * magnitude. - */ -static BSON_INLINE mcd_duration -mcd_milliseconds(int64_t s) -{ - // 1'000 microseconds per millisecond: - if (_mcd_i64_mul_would_overflow(s, 1000)) { - return s < 0 ? MCD_DURATION_MIN : MCD_DURATION_MAX; - } - return mcd_microseconds(s * 1000); -} - -/** - * @brief Create a duration from a number of seconds. - * - * @param s A number of seconds - * @return mcd_duration A duration corresponding to 's' seconds. - * - * @note Saturates to the min/max duration if the duration is too great in - * magnitude. - */ -static BSON_INLINE mcd_duration -mcd_seconds(int64_t s) -{ - // 1'000 milliseconds per second: - if (_mcd_i64_mul_would_overflow(s, 1000)) { - return s < 0 ? MCD_DURATION_MIN : MCD_DURATION_MAX; - } - return mcd_milliseconds(s * 1000); -} - -/** - * @brief Create a duration from a number of minutes. - * - * @param m A number of minutes - * @return mcd_duration A duration corresponding to 's' minutes. - * - * @note Saturates to the min/max duration if the duration is too great in - * magnitude. - */ -static BSON_INLINE mcd_duration -mcd_minutes(int64_t m) -{ - // Sixty seconds per minute: - if (_mcd_i64_mul_would_overflow(m, 60)) { - return m < 0 ? MCD_DURATION_MIN : MCD_DURATION_MAX; - } - return mcd_seconds(m * 60); -} - -/** - * @brief Obtain the count of full milliseconds encoded in the given duration - * - * @param d An abstract duration - * @return int64_t The number of milliseconds in 'd' - * - * @note Does not round-trip with `mcd_milliseconds(N)` if N-milliseconds is - * unrepresentable in the duration type. This only occurs in extreme durations - */ -static BSON_INLINE int64_t -mcd_get_milliseconds(mcd_duration d) -{ - return d._rep / 1000; -} - -/** - * @brief Obtain a point-in-time relative to a base time offset by the given - * duration (which may be negative). - * - * @param from The basis of the time offset - * @param delta The amount of time to shift the resulting time point - * @return mcd_time_point If 'delta' is a positive duration, the result is a - * point-in-time *after* 'from'. If 'delta' is a negative duration, the result - * is a point-in-time *before* 'from'. - * - * @note If the resulting point-in-time is unrepresentable, the return value - * will be clamped to MCD_TIME_POINT_MIN or MCD_TIME_POINT_MAX. - */ -static BSON_INLINE mcd_time_point -mcd_later(mcd_time_point from, mcd_duration delta) -{ - if (_mcd_i64_add_would_overflow(from._rep, delta._rep)) { - return delta._rep < 0 ? MCD_TIME_POINT_MIN : MCD_TIME_POINT_MAX; - } else { - from._rep += delta._rep; - return from; - } -} - -/** - * @brief Obtain the duration between two points in time. - * - * @param then The target time - * @param from The base time - * @return mcd_duration The amount of time you would need to wait starting - * at 'from' for the time to become 'then' (the result may be a negative - * duration). - * - * Intuition: If "then" is "in the future" relative to "from", you will - * receive a positive duration, indicating an amount of time to wait - * beginning at 'from' to reach 'then'. If "then" is actually *before* - * "from", you will receive a paradoxical *negative* duration, indicating - * the amount of time needed to time-travel backwards to reach "then." - */ -static BSON_INLINE mcd_duration -mcd_time_difference(mcd_time_point then, mcd_time_point from) -{ - if (_mcd_i64_sub_would_overflow(then._rep, from._rep)) { - if (from._rep < 0) { - // Would overflow past the max - return MCD_DURATION_MAX; - } else { - // Would overflow past the min - return MCD_DURATION_MIN; - } - } else { - int64_t diff = then._rep - from._rep; - // Our time_point encodes the time using a microsecond counter. - return mcd_microseconds(diff); - } -} - -/** - * @brief Compare two time points to create an ordering. - * - * A time point "in the past" is "less than" a time point "in the future". - * - * @retval <0 If 'left' is before 'right' - * @retval >0 If 'right' is before 'left' - * @retval 0 If 'left' and 'right' are equivalent - */ -static BSON_INLINE int -mcd_time_compare(mcd_time_point left, mcd_time_point right) -{ - // Obtain the amount of time needed to wait from 'right' to reach - // 'left' - int64_t diff = mcd_time_difference(left, right)._rep; - if (diff < 0) { - // A negative duration indicates that 'left' is "before" 'right' - return -1; - } else if (diff > 0) { - // A positive duration indicates that 'left' is "after" 'right' - return 1; - } else { - // These time points are equivalent - return 0; - } -} - -/** - * @brief Compare two durations - * - * A duration D1 is "less than" a duration D2 if time-travelling/waiting for D1 - * duration would end in the past relative to time-travelling/waiting for D2. - * - * @retval <0 If left is "less than" right - * @retval >0 If left is "greater than" right - * @retval 0 If left and right are equivalent - */ -static BSON_INLINE int -mcd_duration_compare(mcd_duration left, mcd_duration right) -{ - if (left._rep < right._rep) { - return -1; - } else if (left._rep > right._rep) { - return 1; - } else { - return 0; - } -} - -/** - * @brief Clamp a duration between two other durations - * - * @param dur The duration to transform - * @param min The minimum duration - * @param max The maximum duration - * @retval min If `dur` < `min` - * @retval max If `dur` > `max` - * @retval dur Otherwise - */ -static BSON_INLINE mcd_duration -mcd_duration_clamp(mcd_duration dur, mcd_duration min, mcd_duration max) -{ - BSON_ASSERT(mcd_duration_compare(min, max) <= 0 && "Invalid min-max range given to mcd_duration_clamp()"); - if (mcd_duration_compare(dur, min) < 0) { - // The duration is less than the minimum - return min; - } else if (mcd_duration_compare(dur, max) > 0) { - // The duration is greater than the maximum - return max; - } else { - // The duration is in-bounds - return dur; - } -} - -/// Represents a timer that can be expired -typedef struct mcd_timer { - /// The point in time after which the time will become expired. - mcd_time_point expire_at; -} mcd_timer; - -/// Create a time that will expire at the given time -static BSON_INLINE mcd_timer -mcd_timer_expire_at(mcd_time_point time) -{ - return (mcd_timer){time}; -} - -/** - * @brief Create a timer that will expire after waiting for the given duration - * relative to now - * - * @note If the duration is less-than or equal-to zero, the timer will already - * have expired - */ -static BSON_INLINE mcd_timer -mcd_timer_expire_after(mcd_duration after) -{ - return mcd_timer_expire_at(mcd_later(mcd_now(), after)); -} - -/** - * @brief Obtain the amount of time that one will need to WAIT before the timer - * will be an expired state. - * - * @return mcd_duration A non-negative duration. - * - * @note If the timer is already expired, returns a zero duration. Will never - * return a negative duration. - */ -static BSON_INLINE mcd_duration -mcd_timer_remaining(mcd_timer timer) -{ - // Compute the distance until the expiry time relative to now - mcd_duration remain = mcd_time_difference(timer.expire_at, mcd_now()); - // Compare that duration with a zero duration - if (mcd_duration_compare(remain, mcd_microseconds(0)) < 0) { - // The "remaining" time is less-than zero, which means the timer is - // already expired, so we only need to wait for zero time: - return mcd_microseconds(0); - } - // There is a positive amount of time remaining - return remain; -} - -#endif // MCD_TIME_H_INCLUDED diff --git a/src/libmongoc/src/mongoc/mongoc-cluster-aws-private.h b/src/libmongoc/src/mongoc/mongoc-cluster-aws-private.h index b5cf836a392..3885c0aad0a 100644 --- a/src/libmongoc/src/mongoc/mongoc-cluster-aws-private.h +++ b/src/libmongoc/src/mongoc/mongoc-cluster-aws-private.h @@ -22,10 +22,11 @@ #include // bson_mutex_t #include -#include - #include +#include +#include + bool _mongoc_cluster_auth_node_aws(mongoc_cluster_t *cluster, mongoc_stream_t *stream, @@ -42,7 +43,7 @@ typedef struct { // If expiration.set is false, the credentials do not have a known // expiration. struct { - mcd_timer value; + mlib_timer value; bool set; } expiration; } _mongoc_aws_credentials_t; @@ -51,10 +52,10 @@ typedef struct { (_mongoc_aws_credentials_t) \ { \ .access_key_id = NULL, .secret_access_key = NULL, .session_token = NULL, \ - .expiration = {.value = {.expire_at = {0}}, .set = false}, \ + .expiration = {.value = (mlib_timer){0}, .set = false}, \ } -#define MONGOC_AWS_CREDENTIALS_EXPIRATION_WINDOW_MS 60 * 5 * 1000 +#define MONGOC_AWS_CREDENTIALS_EXPIRATION_WINDOW mlib_duration(5, mn) // _mongoc_aws_credentials_cache_t is a thread-safe global cache of AWS // credentials. diff --git a/src/libmongoc/src/mongoc/mongoc-cluster-aws.c b/src/libmongoc/src/mongoc/mongoc-cluster-aws.c index acdaf7c58b2..4dc3177d3f5 100644 --- a/src/libmongoc/src/mongoc/mongoc-cluster-aws.c +++ b/src/libmongoc/src/mongoc/mongoc-cluster-aws.c @@ -30,7 +30,8 @@ #include #include -#include +#include +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "aws_auth" @@ -139,7 +140,7 @@ _send_http_request(bool use_tls, { mongoc_http_request_t req; mongoc_http_response_t res; - const int socket_timeout_ms = 10000; + const mlib_duration socket_timeout = mlib_duration(10, s); bool ret; mongoc_ssl_opt_t ssl_opt = {0}; @@ -156,7 +157,8 @@ _send_http_request(bool use_tls, if (use_tls) { _mongoc_ssl_opts_copy_to(mongoc_ssl_opt_get_default(), &ssl_opt, true /* copy_internal */); } - ret = _mongoc_http_send(&req, socket_timeout_ms, use_tls /* use_tls */, use_tls ? &ssl_opt : NULL, &res, error); + ret = _mongoc_http_send( + &req, mlib_expires_after(socket_timeout), use_tls /* use_tls */, use_tls ? &ssl_opt : NULL, &res, error); if (ret) { *http_response_headers = bson_strndup(res.headers, res.headers_len); @@ -276,26 +278,25 @@ _obtain_creds_from_env(_mongoc_aws_credentials_t *creds, bson_error_t *error) } // expiration_ms_to_timer converts milliseconds since Unix Epoch into the -// mcd_timer `expiration_timer`. +// mlib_timer `expiration_timer`. static bool -expiration_ms_to_timer(int64_t expiration_ms, mcd_timer *expiration_timer, bson_error_t *error) +expiration_ms_to_timer(int64_t expiration_ms, mlib_timer *expiration_timer, bson_error_t *error) { bool ret = false; - // get current time in milliseconds since Unix Epoch. - int64_t now_ms; + mlib_duration since_unix_epoch; { - struct timeval now; - if (0 != bson_gettimeofday(&now)) { + struct timeval tv; + if (0 != bson_gettimeofday(&tv)) { AUTH_ERROR_AND_FAIL("bson_gettimeofday returned failure. Unable to " "determine expiration."); } else { - now_ms = (1000 * now.tv_sec) + (now.tv_usec / 1000); + since_unix_epoch = mlib_duration((tv.tv_sec, s), plus, (tv.tv_usec, us)); } } - *expiration_timer = - mcd_timer_expire_after(mcd_milliseconds(expiration_ms - now_ms - MONGOC_AWS_CREDENTIALS_EXPIRATION_WINDOW_MS)); + const mlib_duration expires_in = mlib_duration((expiration_ms, ms), minus, since_unix_epoch); + *expiration_timer = mlib_expires_after(expires_in, minus, MONGOC_AWS_CREDENTIALS_EXPIRATION_WINDOW); ret = true; fail: return ret; @@ -306,7 +307,7 @@ expiration_ms_to_timer(int64_t expiration_ms, mcd_timer *expiration_timer, bson_ // string. Example: "2023-02-07T20:04:27Z". On success, `expiration_timer` is // set to the expiration time. static bool -expiration_iso8601_to_timer(const char *expiration_str, mcd_timer *expiration_timer, bson_error_t *error) +expiration_iso8601_to_timer(const char *expiration_str, mlib_timer *expiration_timer, bson_error_t *error) { bool ret = false; @@ -1293,7 +1294,7 @@ check_expired(const _mongoc_aws_credentials_t *creds) if (!creds->expiration.set) { return true; } - return mcd_get_milliseconds(mcd_timer_remaining(creds->expiration.value)) == 0; + return mlib_timer_is_expired(creds->expiration.value); } void diff --git a/src/libmongoc/src/mongoc/mongoc-crypt.c b/src/libmongoc/src/mongoc/mongoc-crypt.c index 183badb41e0..8b2337be576 100644 --- a/src/libmongoc/src/mongoc/mongoc-crypt.c +++ b/src/libmongoc/src/mongoc/mongoc-crypt.c @@ -32,10 +32,10 @@ #include #include -#include #include #include +#include #include #include @@ -138,7 +138,7 @@ struct __mongoc_crypt_t { /// or not yet acquired. mcd_azure_access_token azure_token; /// The time point at which the `azure_token` was acquired. - mcd_time_point azure_token_issued_at; + mlib_time_point azure_token_issued_at; }; static void @@ -835,9 +835,9 @@ _try_add_azure_from_env(_mongoc_crypt_t *crypt, bson_t *out, bson_error_t *error { if (crypt->azure_token.access_token) { // The access-token is non-null, so we may have one cached. - mcd_time_point one_min_from_now = mcd_later(mcd_now(), mcd_minutes(1)); - mcd_time_point expires_at = mcd_later(crypt->azure_token_issued_at, crypt->azure_token.expires_in); - if (mcd_time_compare(expires_at, one_min_from_now) >= 0) { + const mlib_time_point one_min_from_now = mlib_time_add(mlib_now(), mlib_duration(1, mn)); + const mlib_time_point expires_at = mlib_time_add(crypt->azure_token_issued_at, crypt->azure_token.expires_in); + if (mlib_time_cmp(expires_at, >=, one_min_from_now)) { // The token is still valid for at least another minute } else { // The token will expire soon. Destroy it, and below we will below ask @@ -854,7 +854,7 @@ _try_add_azure_from_env(_mongoc_crypt_t *crypt, bson_t *out, bson_error_t *error // number of seconds that the token will be valid relative to its issue // time. Avoid reliance on system clocks by comparing the issue time to an // abstract monotonic "now" - crypt->azure_token_issued_at = mcd_now(); + crypt->azure_token_issued_at = mlib_now(); // Get the token: if (!_request_new_azure_token(&crypt->azure_token, error)) { return false; diff --git a/src/libmongoc/src/mongoc/mongoc-http-private.h b/src/libmongoc/src/mongoc/mongoc-http-private.h index b144d62aa4a..d30d96197c3 100644 --- a/src/libmongoc/src/mongoc/mongoc-http-private.h +++ b/src/libmongoc/src/mongoc/mongoc-http-private.h @@ -21,6 +21,8 @@ #include #include +#include + #ifndef MONGOC_HTTP_PRIVATE_H #define MONGOC_HTTP_PRIVATE_H @@ -74,7 +76,7 @@ _mongoc_http_render_request_head(mcommon_string_append_t *append, const mongoc_h * * @param req The request to send. Uses the "host" attribute to determine the * HTTP peer. - * @param timeout_ms A timeout for the request, in milliseconds + * @param timer A timer for the request. * @param use_tls Whether the connection should use TLS. * @param ssl_opts Options to control TLS (Required only if 'use_tls' is true) * @param res Output parameter for the response. Must be uninitialized. @@ -91,7 +93,7 @@ _mongoc_http_render_request_head(mcommon_string_append_t *append, const mongoc_h */ bool _mongoc_http_send(mongoc_http_request_t const *req, - int timeout_ms, + mlib_timer timer, bool use_tls, mongoc_ssl_opt_t *ssl_opts, mongoc_http_response_t *res, diff --git a/src/libmongoc/src/mongoc/mongoc-http.c b/src/libmongoc/src/mongoc/mongoc-http.c index 5aaf99e22f5..a6cb4d37286 100644 --- a/src/libmongoc/src/mongoc/mongoc-http.c +++ b/src/libmongoc/src/mongoc/mongoc-http.c @@ -22,10 +22,11 @@ #include #include -#include #include #include +#include +#include void _mongoc_http_request_init(mongoc_http_request_t *request) @@ -89,16 +90,16 @@ _mongoc_http_render_request_head(mcommon_string_append_t *append, const mongoc_h } static int32_t -_mongoc_http_msec_remaining(mcd_timer timer) +_mongoc_http_msec_remaining(mlib_timer timer) { - const int64_t msec = mcd_get_milliseconds(mcd_timer_remaining(timer)); + const int64_t msec = mlib_milliseconds_count(mlib_timer_remaining(timer)); BSON_ASSERT(mlib_in_range(int32_t, msec)); return (int32_t)msec; } bool _mongoc_http_send(const mongoc_http_request_t *req, - int timeout_ms, + mlib_timer timer, bool use_tls, mongoc_ssl_opt_t *ssl_opts, mongoc_http_response_t *res, @@ -114,8 +115,6 @@ _mongoc_http_send(const mongoc_http_request_t *req, char *ptr; const char *header_delimiter = "\r\n\r\n"; - const mcd_timer timer = mcd_timer_expire_after(mcd_milliseconds(timeout_ms)); - mcommon_string_append_t http_request; mcommon_string_new_as_append(&http_request); @@ -127,8 +126,8 @@ _mongoc_http_send(const mongoc_http_request_t *req, } stream = mongoc_client_connect_tcp( - // +1 to prevent passing zero as a timeout - _mongoc_http_msec_remaining(timer) + 1, + // Prevent passing zero as a timeout + BSON_MAX(_mongoc_http_msec_remaining(timer), 1), &host_list, error); if (!stream) { diff --git a/src/libmongoc/src/mongoc/mongoc-openssl.c b/src/libmongoc/src/mongoc/mongoc-openssl.c index 6bbcb6da02b..1d503305e06 100644 --- a/src/libmongoc/src/mongoc/mongoc-openssl.c +++ b/src/libmongoc/src/mongoc/mongoc-openssl.c @@ -33,6 +33,8 @@ #include #include +#include +#include #include #include @@ -625,7 +627,7 @@ _get_must_staple(X509 *cert) } #define ERR_STR (ERR_error_string(ERR_get_error(), NULL)) -#define MONGOC_OCSP_REQUEST_TIMEOUT_MS 5000 +#define MONGOC_OCSP_REQUEST_TIMEOUT mlib_duration(5, s) static OCSP_RESPONSE * _contact_ocsp_responder(OCSP_CERTID *id, X509 *peer, mongoc_ssl_opt_t *ssl_opts, int *ocsp_uri_count) @@ -687,7 +689,8 @@ _contact_ocsp_responder(OCSP_CERTID *id, X509 *peer, mongoc_ssl_opt_t *ssl_opts, http_req.port = (int)bson_ascii_strtoll(port, NULL, 10); http_req.body = (const char *)request_der; http_req.body_len = request_der_len; - if (!_mongoc_http_send(&http_req, MONGOC_OCSP_REQUEST_TIMEOUT_MS, ssl != 0, ssl_opts, &http_res, &error)) { + if (!_mongoc_http_send( + &http_req, mlib_expires_after(MONGOC_OCSP_REQUEST_TIMEOUT), ssl != 0, ssl_opts, &http_res, &error)) { MONGOC_DEBUG("Could not send HTTP request: %s", error.message); GOTO(retry); } diff --git a/src/libmongoc/src/mongoc/service-gcp.c b/src/libmongoc/src/mongoc/service-gcp.c index 771d84851dd..5fd80a9aef6 100644 --- a/src/libmongoc/src/mongoc/service-gcp.c +++ b/src/libmongoc/src/mongoc/service-gcp.c @@ -19,6 +19,9 @@ #include #include +#include +#include + #define HOST "metadata.google.internal" static const char *const DEFAULT_METADATA_PATH = "/computeMetadata/v1/instance/service-accounts/default/token"; @@ -143,7 +146,9 @@ gcp_access_token_from_gcp_server(gcp_service_account_token *out, }; gcp_request_init(&req, opt_host, opt_port, opt_extra_headers); - if (!_mongoc_http_send(&req.req, 3 * 1000, false, NULL, &resp, error)) { + const mlib_duration socket_timeout = mlib_duration(3, s); + + if (!_mongoc_http_send(&req.req, mlib_expires_after(socket_timeout), false, NULL, &resp, error)) { goto fail; } diff --git a/src/libmongoc/tests/test-awsauth.c b/src/libmongoc/tests/test-awsauth.c index e709c21ab6c..6e0cd526394 100644 --- a/src/libmongoc/tests/test-awsauth.c +++ b/src/libmongoc/tests/test-awsauth.c @@ -24,6 +24,10 @@ #include +#include +#include +#include + // Ensure stdout and stderr are flushed prior to possible following abort(). #define MONGOC_STDERR_PRINTF(format, ...) \ if (1) { \ @@ -98,7 +102,7 @@ creds_eq(_mongoc_aws_credentials_t *a, _mongoc_aws_credentials_t *b) return false; } if (a->expiration.set) { - if (mcd_time_compare(a->expiration.value.expire_at, b->expiration.value.expire_at) != 0) { + if (mlib_time_cmp(a->expiration.value.expires_at, !=, b->expiration.value.expires_at)) { return false; } } @@ -188,7 +192,7 @@ test_cache(const mongoc_uri_t *uri) ASSERT(mongoc_aws_credentials_cache.cached.set); mongoc_aws_credentials_cache.cached.value.expiration.set = true; mongoc_aws_credentials_cache.cached.value.expiration.value = - mcd_timer_expire_after(mcd_milliseconds(60 * 1000 - MONGOC_AWS_CREDENTIALS_EXPIRATION_WINDOW_MS)); + mlib_expires_after(mlib_duration((1, mn), minus, MONGOC_AWS_CREDENTIALS_EXPIRATION_WINDOW)); _mongoc_aws_credentials_copy_to(&mongoc_aws_credentials_cache.cached.value, &first_cached); } diff --git a/src/libmongoc/tests/test-mongoc-aws.c b/src/libmongoc/tests/test-mongoc-aws.c index 8e3241ada1c..d955573a088 100644 --- a/src/libmongoc/tests/test-mongoc-aws.c +++ b/src/libmongoc/tests/test-mongoc-aws.c @@ -15,6 +15,9 @@ #include +#include +#include + #include #include @@ -265,7 +268,7 @@ test_aws_cache(void *unused) valid_creds.session_token = bson_strdup("session_token"); // Set expiration to one minute from now. valid_creds.expiration.set = true; - valid_creds.expiration.value = mcd_timer_expire_after(mcd_milliseconds(60 * 1000)); + valid_creds.expiration.value = mlib_expires_after(mlib_duration(1, mn)); _mongoc_aws_credentials_t expired_creds = MONGOC_AWS_CREDENTIALS_INIT; expired_creds.access_key_id = bson_strdup("access_key_id"); @@ -273,7 +276,7 @@ test_aws_cache(void *unused) expired_creds.session_token = bson_strdup("session_token"); // Set expiration to one minute before. expired_creds.expiration.set = true; - expired_creds.expiration.value = mcd_timer_expire_after(mcd_milliseconds(-60 * 1000)); + expired_creds.expiration.value = mlib_expires_after(mlib_duration(-1, mn)); _mongoc_aws_credentials_cache_t *cache = &mongoc_aws_credentials_cache; _mongoc_aws_credentials_cache_clear(); diff --git a/src/libmongoc/tests/test-mongoc-client-side-encryption.c b/src/libmongoc/tests/test-mongoc-client-side-encryption.c index f36d2294c1e..e262e6bb10c 100644 --- a/src/libmongoc/tests/test-mongoc-client-side-encryption.c +++ b/src/libmongoc/tests/test-mongoc-client-side-encryption.c @@ -34,6 +34,9 @@ #include #include +#include +#include + /* _mongoc_crypt_get_libmongocrypt_version */ #include @@ -3332,7 +3335,7 @@ reset_failpoints(mongoc_ssl_opt_t *ssl_opts) req.port = failpoint_server_port; req.path = "/reset"; - r = _mongoc_http_send(&req, 10000, true, ssl_opts, &res, &error); + r = _mongoc_http_send(&req, mlib_expires_after(mlib_duration(10, s)), true, ssl_opts, &res, &error); ASSERT_OR_PRINT(r, error); _mongoc_http_response_cleanup(&res); } @@ -3362,7 +3365,7 @@ set_retry_failpoint(mongoc_ssl_opt_t *ssl_opts, bool network, uint32_t count) req.body = count_json; req.body_len = strlen(count_json); - r = _mongoc_http_send(&req, 10000, true, ssl_opts, &res, &error); + r = _mongoc_http_send(&req, mlib_expires_after(mlib_duration(10, s)), true, ssl_opts, &res, &error); ASSERT_OR_PRINT(r, error); _mongoc_http_response_cleanup(&res); } diff --git a/src/libmongoc/tests/test-mongoc-http.c b/src/libmongoc/tests/test-mongoc-http.c index 816b4e94d32..784cecf361a 100644 --- a/src/libmongoc/tests/test-mongoc-http.c +++ b/src/libmongoc/tests/test-mongoc-http.c @@ -18,6 +18,9 @@ #include +#include +#include + #include #include @@ -39,7 +42,7 @@ test_mongoc_http_get(void *unused) req.host = "localhost"; req.path = "get"; req.port = 18000; - r = _mongoc_http_send(&req, 10000, false, NULL, &res, &error); + r = _mongoc_http_send(&req, mlib_expires_after(mlib_duration(10, s)), false, NULL, &res, &error); ASSERT_OR_PRINT(r, error); ASSERT_WITH_MSG(res.status == 200, @@ -71,7 +74,7 @@ test_mongoc_http_post(void *unused) req.host = "localhost"; req.path = "post"; req.port = 18000; - r = _mongoc_http_send(&req, 10000, false, NULL, &res, &error); + r = _mongoc_http_send(&req, mlib_expires_after(mlib_duration(10, s)), false, NULL, &res, &error); ASSERT_OR_PRINT(r, error); ASSERT_WITH_MSG(res.status == 200,