113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci#include "curl_setup.h" 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include <curl/curl.h> 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#ifdef USE_LIBPSL 3013498266Sopenharmony_ci 3113498266Sopenharmony_ci#include "psl.h" 3213498266Sopenharmony_ci#include "share.h" 3313498266Sopenharmony_ci 3413498266Sopenharmony_ci/* The last 3 #include files should be in this order */ 3513498266Sopenharmony_ci#include "curl_printf.h" 3613498266Sopenharmony_ci#include "curl_memory.h" 3713498266Sopenharmony_ci#include "memdebug.h" 3813498266Sopenharmony_ci 3913498266Sopenharmony_civoid Curl_psl_destroy(struct PslCache *pslcache) 4013498266Sopenharmony_ci{ 4113498266Sopenharmony_ci if(pslcache->psl) { 4213498266Sopenharmony_ci if(pslcache->dynamic) 4313498266Sopenharmony_ci psl_free((psl_ctx_t *) pslcache->psl); 4413498266Sopenharmony_ci pslcache->psl = NULL; 4513498266Sopenharmony_ci pslcache->dynamic = FALSE; 4613498266Sopenharmony_ci } 4713498266Sopenharmony_ci} 4813498266Sopenharmony_ci 4913498266Sopenharmony_cistatic time_t now_seconds(void) 5013498266Sopenharmony_ci{ 5113498266Sopenharmony_ci struct curltime now = Curl_now(); 5213498266Sopenharmony_ci 5313498266Sopenharmony_ci return now.tv_sec; 5413498266Sopenharmony_ci} 5513498266Sopenharmony_ci 5613498266Sopenharmony_ciconst psl_ctx_t *Curl_psl_use(struct Curl_easy *easy) 5713498266Sopenharmony_ci{ 5813498266Sopenharmony_ci struct PslCache *pslcache = easy->psl; 5913498266Sopenharmony_ci const psl_ctx_t *psl; 6013498266Sopenharmony_ci time_t now; 6113498266Sopenharmony_ci 6213498266Sopenharmony_ci if(!pslcache) 6313498266Sopenharmony_ci return NULL; 6413498266Sopenharmony_ci 6513498266Sopenharmony_ci Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED); 6613498266Sopenharmony_ci now = now_seconds(); 6713498266Sopenharmony_ci if(!pslcache->psl || pslcache->expires <= now) { 6813498266Sopenharmony_ci /* Let a chance to other threads to do the job: avoids deadlock. */ 6913498266Sopenharmony_ci Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); 7013498266Sopenharmony_ci 7113498266Sopenharmony_ci /* Update cache: this needs an exclusive lock. */ 7213498266Sopenharmony_ci Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SINGLE); 7313498266Sopenharmony_ci 7413498266Sopenharmony_ci /* Recheck in case another thread did the job. */ 7513498266Sopenharmony_ci now = now_seconds(); 7613498266Sopenharmony_ci if(!pslcache->psl || pslcache->expires <= now) { 7713498266Sopenharmony_ci bool dynamic = FALSE; 7813498266Sopenharmony_ci time_t expires = TIME_T_MAX; 7913498266Sopenharmony_ci 8013498266Sopenharmony_ci#if defined(PSL_VERSION_NUMBER) && PSL_VERSION_NUMBER >= 0x001000 8113498266Sopenharmony_ci psl = psl_latest(NULL); 8213498266Sopenharmony_ci dynamic = psl != NULL; 8313498266Sopenharmony_ci /* Take care of possible time computation overflow. */ 8413498266Sopenharmony_ci expires = now < TIME_T_MAX - PSL_TTL? now + PSL_TTL: TIME_T_MAX; 8513498266Sopenharmony_ci 8613498266Sopenharmony_ci /* Only get the built-in PSL if we do not already have the "latest". */ 8713498266Sopenharmony_ci if(!psl && !pslcache->dynamic) 8813498266Sopenharmony_ci#endif 8913498266Sopenharmony_ci 9013498266Sopenharmony_ci psl = psl_builtin(); 9113498266Sopenharmony_ci 9213498266Sopenharmony_ci if(psl) { 9313498266Sopenharmony_ci Curl_psl_destroy(pslcache); 9413498266Sopenharmony_ci pslcache->psl = psl; 9513498266Sopenharmony_ci pslcache->dynamic = dynamic; 9613498266Sopenharmony_ci pslcache->expires = expires; 9713498266Sopenharmony_ci } 9813498266Sopenharmony_ci } 9913498266Sopenharmony_ci Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); /* Release exclusive lock. */ 10013498266Sopenharmony_ci Curl_share_lock(easy, CURL_LOCK_DATA_PSL, CURL_LOCK_ACCESS_SHARED); 10113498266Sopenharmony_ci } 10213498266Sopenharmony_ci psl = pslcache->psl; 10313498266Sopenharmony_ci if(!psl) 10413498266Sopenharmony_ci Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); 10513498266Sopenharmony_ci return psl; 10613498266Sopenharmony_ci} 10713498266Sopenharmony_ci 10813498266Sopenharmony_civoid Curl_psl_release(struct Curl_easy *easy) 10913498266Sopenharmony_ci{ 11013498266Sopenharmony_ci Curl_share_unlock(easy, CURL_LOCK_DATA_PSL); 11113498266Sopenharmony_ci} 11213498266Sopenharmony_ci 11313498266Sopenharmony_ci#endif /* USE_LIBPSL */ 114