share.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #include <curl/curl.h>
00026 #include "urldata.h"
00027 #include "share.h"
00028 #include "vtls/vtls.h"
00029 #include "curl_memory.h"
00030 
00031 /* The last #include file should be: */
00032 #include "memdebug.h"
00033 
00034 struct Curl_share *
00035 curl_share_init(void)
00036 {
00037   struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
00038   if(share) {
00039     share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
00040 
00041     if(Curl_mk_dnscache(&share->hostcache)) {
00042       free(share);
00043       return NULL;
00044     }
00045   }
00046 
00047   return share;
00048 }
00049 
00050 #undef curl_share_setopt
00051 CURLSHcode
00052 curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
00053 {
00054   va_list param;
00055   int type;
00056   curl_lock_function lockfunc;
00057   curl_unlock_function unlockfunc;
00058   void *ptr;
00059   CURLSHcode res = CURLSHE_OK;
00060 
00061   if(share->dirty)
00062     /* don't allow setting options while one or more handles are already
00063        using this share */
00064     return CURLSHE_IN_USE;
00065 
00066   va_start(param, option);
00067 
00068   switch(option) {
00069   case CURLSHOPT_SHARE:
00070     /* this is a type this share will share */
00071     type = va_arg(param, int);
00072     share->specifier |= (1<<type);
00073     switch(type) {
00074     case CURL_LOCK_DATA_DNS:
00075       break;
00076 
00077     case CURL_LOCK_DATA_COOKIE:
00078 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
00079       if(!share->cookies) {
00080         share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE);
00081         if(!share->cookies)
00082           res = CURLSHE_NOMEM;
00083       }
00084 #else   /* CURL_DISABLE_HTTP */
00085       res = CURLSHE_NOT_BUILT_IN;
00086 #endif
00087       break;
00088 
00089     case CURL_LOCK_DATA_SSL_SESSION:
00090 #ifdef USE_SSL
00091       if(!share->sslsession) {
00092         share->max_ssl_sessions = 8;
00093         share->sslsession = calloc(share->max_ssl_sessions,
00094                                    sizeof(struct curl_ssl_session));
00095         share->sessionage = 0;
00096         if(!share->sslsession)
00097           res = CURLSHE_NOMEM;
00098       }
00099 #else
00100       res = CURLSHE_NOT_BUILT_IN;
00101 #endif
00102       break;
00103 
00104     case CURL_LOCK_DATA_CONNECT:     /* not supported (yet) */
00105       break;
00106 
00107     default:
00108       res = CURLSHE_BAD_OPTION;
00109     }
00110     break;
00111 
00112   case CURLSHOPT_UNSHARE:
00113     /* this is a type this share will no longer share */
00114     type = va_arg(param, int);
00115     share->specifier &= ~(1<<type);
00116     switch(type) {
00117     case CURL_LOCK_DATA_DNS:
00118       break;
00119 
00120     case CURL_LOCK_DATA_COOKIE:
00121 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
00122       if(share->cookies) {
00123         Curl_cookie_cleanup(share->cookies);
00124         share->cookies = NULL;
00125       }
00126 #else   /* CURL_DISABLE_HTTP */
00127       res = CURLSHE_NOT_BUILT_IN;
00128 #endif
00129       break;
00130 
00131     case CURL_LOCK_DATA_SSL_SESSION:
00132 #ifdef USE_SSL
00133       Curl_safefree(share->sslsession);
00134 #else
00135       res = CURLSHE_NOT_BUILT_IN;
00136 #endif
00137       break;
00138 
00139     case CURL_LOCK_DATA_CONNECT:
00140       break;
00141 
00142     default:
00143       res = CURLSHE_BAD_OPTION;
00144       break;
00145     }
00146     break;
00147 
00148   case CURLSHOPT_LOCKFUNC:
00149     lockfunc = va_arg(param, curl_lock_function);
00150     share->lockfunc = lockfunc;
00151     break;
00152 
00153   case CURLSHOPT_UNLOCKFUNC:
00154     unlockfunc = va_arg(param, curl_unlock_function);
00155     share->unlockfunc = unlockfunc;
00156     break;
00157 
00158   case CURLSHOPT_USERDATA:
00159     ptr = va_arg(param, void *);
00160     share->clientdata = ptr;
00161     break;
00162 
00163   default:
00164     res = CURLSHE_BAD_OPTION;
00165     break;
00166   }
00167 
00168   va_end(param);
00169 
00170   return res;
00171 }
00172 
00173 CURLSHcode
00174 curl_share_cleanup(struct Curl_share *share)
00175 {
00176   if(share == NULL)
00177     return CURLSHE_INVALID;
00178 
00179   if(share->lockfunc)
00180     share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
00181                     share->clientdata);
00182 
00183   if(share->dirty) {
00184     if(share->unlockfunc)
00185       share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
00186     return CURLSHE_IN_USE;
00187   }
00188 
00189   Curl_hash_destroy(&share->hostcache);
00190 
00191 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
00192   Curl_cookie_cleanup(share->cookies);
00193 #endif
00194 
00195 #ifdef USE_SSL
00196   if(share->sslsession) {
00197     size_t i;
00198     for(i = 0; i < share->max_ssl_sessions; i++)
00199       Curl_ssl_kill_session(&(share->sslsession[i]));
00200     free(share->sslsession);
00201   }
00202 #endif
00203 
00204   if(share->unlockfunc)
00205     share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
00206   free(share);
00207 
00208   return CURLSHE_OK;
00209 }
00210 
00211 
00212 CURLSHcode
00213 Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
00214                 curl_lock_access accesstype)
00215 {
00216   struct Curl_share *share = data->share;
00217 
00218   if(share == NULL)
00219     return CURLSHE_INVALID;
00220 
00221   if(share->specifier & (1<<type)) {
00222     if(share->lockfunc) /* only call this if set! */
00223       share->lockfunc(data, type, accesstype, share->clientdata);
00224   }
00225   /* else if we don't share this, pretend successful lock */
00226 
00227   return CURLSHE_OK;
00228 }
00229 
00230 CURLSHcode
00231 Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
00232 {
00233   struct Curl_share *share = data->share;
00234 
00235   if(share == NULL)
00236     return CURLSHE_INVALID;
00237 
00238   if(share->specifier & (1<<type)) {
00239     if(share->unlockfunc) /* only call this if set! */
00240       share->unlockfunc (data, type, share->clientdata);
00241   }
00242 
00243   return CURLSHE_OK;
00244 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06