Address reviews and improve code style

This commit is contained in:
Federico Stazi 2020-08-18 10:44:53 +00:00
parent f47e1cc6ac
commit 9291231275
16 changed files with 187 additions and 207 deletions

5
.gitmodules vendored
View File

@ -4,7 +4,6 @@
[submodule "oss-internship-2020/pffft/master"] [submodule "oss-internship-2020/pffft/master"]
path = oss-internship-2020/pffft/master path = oss-internship-2020/pffft/master
url = https://bitbucket.org/jpommier/pffft/src/master/ url = https://bitbucket.org/jpommier/pffft/src/master/
[submodule "oss-internship-2020/curl/custom_curl/curl"] [submodule "oss-internship-2020/curl/curl_wrapper/curl"]
path = oss-internship-2020/curl/custom_curl/curl path = oss-internship-2020/curl/curl_wrapper/curl
url = https://github.com/curl/curl url = https://github.com/curl/curl

View File

@ -48,7 +48,7 @@ The pointers can then be obtained using an `RPCChannel` object, as shown in `exa
## Examples ## Examples
The `examples` directory contains the sandboxed versions of example source codes taken from [this page](https://curl.haxx.se/libcurl/c/example.html) on curl's website. The `examples` directory contains the sandboxed versions of example source codes taken from [this page](https://curl.haxx.se/libcurl/c/example.html) on curl's website. More information about each example can be found in the examples' [README](examples/README.md).
To build these examples when building the library, the cmake variable `CURL_SAPI_ENABLE_EXAMPLES` must be set to `ON`. To build these examples when building the library, the cmake variable `CURL_SAPI_ENABLE_EXAMPLES` must be set to `ON`.

View File

@ -12,16 +12,15 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Wrapper library including curl, # Wrapper library including curl, wrappers for some methods and callbacks
# adds the explicit versions of the variadic methods and the callbacks
# The CURL_SAPI_CALLBACKS variable should contain the absolute paths of # The CURL_SAPI_CALLBACKS variable should contain the absolute paths of
# all the files implementing the callbacks # all the files implementing the callbacks
add_library(custom_curl_and_callbacks OBJECT add_library(curl_wrapper_and_callbacks OBJECT
custom_curl.h curl_wrapper.h
custom_curl.cc curl_wrapper.cc
"${CURL_SAPI_CALLBACKS}" "${CURL_SAPI_CALLBACKS}"
) )
set_target_properties(custom_curl_and_callbacks set_target_properties(curl_wrapper_and_callbacks
PROPERTIES LINKER_LANGUAGE C PROPERTIES LINKER_LANGUAGE C
) )
@ -31,6 +30,6 @@ set(BUILD_SHARED_LIBS OFF)
# Link the wrapper to the original curl library # Link the wrapper to the original curl library
add_subdirectory(curl) add_subdirectory(curl)
target_link_libraries(custom_curl_and_callbacks target_link_libraries(curl_wrapper_and_callbacks
CURL::libcurl CURL::libcurl
) )

@ -0,0 +1 @@
Subproject commit 6332f65714d14b3f19662fd0607c7b59af400b9b

View File

@ -12,83 +12,78 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "custom_curl.h" #include "curl_wrapper.h"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option, CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option,
void* parameter) { void* parameter) {
return curl_easy_setopt(handle, option, parameter); return curl_easy_setopt(handle, option, parameter);
} }
CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option, CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option,
long parameter) { long parameter) {
return curl_easy_setopt(handle, option, parameter); return curl_easy_setopt(handle, option, parameter);
} }
CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option, CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option,
curl_off_t parameter) { curl_off_t parameter) {
return curl_easy_setopt(handle, option, parameter); return curl_easy_setopt(handle, option, parameter);
} }
CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option, CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option, void* parameter) {
void* parameter) {
return curl_easy_getinfo(handle, option, parameter); return curl_easy_getinfo(handle, option, parameter);
} }
time_t_sapi curl_getdate_sapi(char *datestring, time_t_sapi *now ) { time_t_sapi curl_getdate_sapi(char* datestring, time_t_sapi* now) {
return curl_getdate(datestring, now); return curl_getdate(datestring, now);
} }
CURLMcode curl_multi_fdset_sapi(CURLM *multi_handle, CURLMcode curl_multi_fdset_sapi(CURLM* multi_handle, fd_set_sapi* read_fd_set,
fd_set_sapi *read_fd_set, fd_set_sapi* write_fd_set,
fd_set_sapi *write_fd_set, fd_set_sapi* exc_fd_set, int* max_fd) {
fd_set_sapi *exc_fd_set, return curl_multi_fdset(multi_handle, read_fd_set, write_fd_set, exc_fd_set,
int *max_fd) {
return curl_multi_fdset(multi_handle, read_fd_set, write_fd_set, exc_fd_set,
max_fd); max_fd);
} }
CURLMcode curl_mutti_setopt_ptr(CURLM* handle, CURLMoption option, CURLMcode curl_multi_setopt_ptr(CURLM* handle, CURLMoption option,
void* parameter) { void* parameter) {
return curl_multi_setopt(handle, option, parameter); return curl_multi_setopt(handle, option, parameter);
} }
CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option, CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option,
long parameter) { long parameter) {
return curl_multi_setopt(handle, option, parameter); return curl_multi_setopt(handle, option, parameter);
} }
CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle, CURLMoption option, CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle, CURLMoption option,
curl_off_t parameter) { curl_off_t parameter) {
return curl_multi_setopt(handle, option, parameter); return curl_multi_setopt(handle, option, parameter);
} }
CURLMcode curl_multi_poll_sapi(CURLM *multi_handle, CURLMcode curl_multi_poll_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds, struct curl_waitfd* extra_fds,
unsigned int extra_nfds, unsigned int extra_nfds, int timeout_ms,
int timeout_ms, int* numfds) {
int *numfds) { return curl_multi_poll(multi_handle, extra_fds, extra_nfds, timeout_ms,
return curl_multi_poll(multi_handle, extra_fds, extra_nfds, timeout_ms,
numfds); numfds);
} }
CURLMcode curl_multi_wait_sapi(CURLM *multi_handle, CURLMcode curl_multi_wait_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds, struct curl_waitfd* extra_fds,
unsigned int extra_nfds, unsigned int extra_nfds, int timeout_ms,
int timeout_ms, int* numfds) {
int *numfds) { return curl_multi_wait(multi_handle, extra_fds, extra_nfds, timeout_ms,
return curl_multi_wait(multi_handle, extra_fds, extra_nfds, timeout_ms,
numfds); numfds);
} }
CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option, CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option,
void* parameter) { void* parameter) {
return curl_share_setopt(handle, option, parameter); return curl_share_setopt(handle, option, parameter);
} }
CURLSHcode curl_share_setopt_long(CURLSH* handle, CURLSHoption option, CURLSHcode curl_share_setopt_long(CURLSH* handle, CURLSHoption option,
long parameter) { long parameter) {
return curl_share_setopt(handle, option, parameter); return curl_share_setopt(handle, option, parameter);
} }

View File

@ -14,17 +14,17 @@
// Wrapper for curl library // Wrapper for curl library
#ifndef CUSTOM_CURL_H #ifndef CURL_WRAPPER_H
#define CUSTOM_CURL_H #define CURL_WRAPPER_H
#include <curl/curl.h> #include <curl/curl.h>
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option, extern "C" CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option,
void* parameter); void* parameter);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option, extern "C" CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option,
long parameter); long parameter);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
@ -32,56 +32,55 @@ extern "C" CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option,
curl_off_t parameter); curl_off_t parameter);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option, extern "C" CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option,
void* parameter); void* parameter);
// The typedef and wrapper method are needed because the original method has // The typedef and wrapper method are needed because the original method has
// some conflicts in curl_sapi.sapi.h // some conflicts in curl_sapi.sapi.h
extern "C" typedef time_t time_t_sapi; extern "C" typedef time_t time_t_sapi;
extern "C" time_t_sapi curl_getdate_sapi(char *datestring, time_t_sapi *now ); extern "C" time_t_sapi curl_getdate_sapi(char* datestring, time_t_sapi* now);
// The typedef and wrapper method are needed because the original method has // The typedef and wrapper method are needed because the original method has
// some conflicts in curl_sapi.sapi.h // some conflicts in curl_sapi.sapi.h
extern "C" typedef fd_set fd_set_sapi; extern "C" typedef fd_set fd_set_sapi;
extern "C" CURLMcode curl_multi_fdset_sapi(CURLM *multi_handle, extern "C" CURLMcode curl_multi_fdset_sapi(CURLM* multi_handle,
fd_set_sapi *read_fd_set, fd_set_sapi* read_fd_set,
fd_set_sapi *write_fd_set, fd_set_sapi* write_fd_set,
fd_set_sapi *exc_fd_set, fd_set_sapi* exc_fd_set,
int *max_fd); int* max_fd);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLMcode curl_multi_setopt_ptr(CURLM* handle, CURLMoption option, extern "C" CURLMcode curl_multi_setopt_ptr(CURLM* handle, CURLMoption option,
void* parameter); void* parameter);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option, extern "C" CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option,
long parameter); long parameter);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle, extern "C" CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle,
CURLMoption option, CURLMoption option,
curl_off_t parameter); curl_off_t parameter);
// The wrapper method is needed because incomplete array type is not supported // The wrapper method is needed because incomplete array type is not supported
extern "C" CURLMcode curl_multi_poll_sapi(CURLM *multi_handle, extern "C" CURLMcode curl_multi_poll_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds, struct curl_waitfd* extra_fds,
unsigned int extra_nfds, unsigned int extra_nfds,
int timeout_ms, int timeout_ms, int* numfds);
int *numfds);
// The wrapper method is needed because incomplete array type is not supported // The wrapper method is needed because incomplete array type is not supported
extern "C" CURLMcode curl_multi_wait_sapi(CURLM *multi_handle, extern "C" CURLMcode curl_multi_wait_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds, struct curl_waitfd* extra_fds,
unsigned int extra_nfds, unsigned int extra_nfds,
int timeout_ms, int timeout_ms, int* numfds);
int *numfds);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option, extern "C" CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option,
void* parameter); void* parameter);
// The wrapper method is needed to make the variadic argument explicit // The wrapper method is needed to make the variadic argument explicit
extern "C" CURLSHcode curl_share_setopt_long(CURLSH* handle, CURLSHoption option, extern "C" CURLSHcode curl_share_setopt_long(CURLSH* handle,
CURLSHoption option,
long parameter); long parameter);
#endif // CUSTOM_CURL_H #endif // CURL_WRAPPER_H

@ -1 +0,0 @@
Subproject commit a0c461434c747ad6daff0c85c0a8bbe9cfc44d75

View File

@ -16,11 +16,11 @@
# (https://curl.haxx.se/libcurl/c/example.html) # (https://curl.haxx.se/libcurl/c/example.html)
# Example 1: simple.c # Example 1: simple.c
add_executable(example1 add_executable(example1
example1.cc example1.cc
) )
target_link_libraries(example1 PRIVATE target_link_libraries(example1 PRIVATE
curl_sapi curl_sapi
sapi::sapi sapi::sapi
) )
@ -58,4 +58,4 @@ add_executable(example5
target_link_libraries(example5 PRIVATE target_link_libraries(example5 PRIVATE
curl_sapi curl_sapi
sapi::sapi sapi::sapi
) )

View File

@ -0,0 +1,11 @@
# Curl Sandboxed Examples
Each example in this folder is the sandboxed version of a code snippet from [this page](https://curl.haxx.se/libcurl/c/example.html) on curl's website. These examples perform some basic tasks using libcurl, and can be useful both to understand how to use Curl Sandboxed, but also to get an idea of how regular and sandboxed code compare to each other.
This is the list of the examples:
- **example1**: sandboxed version of [simple.c](https://curl.haxx.se/libcurl/c/simple.html). Really simple HTTP request, downloads and prints out the page at [example.com](http://example.com).
- **example2**: sandboxed version of [getinmemory.c](https://curl.haxx.se/libcurl/c/getinmemory.html). Same HTTP request as example1. The difference is that this example uses a callback to save the page directly in memory. Only the page size is printed out.
- **example3**: sandboxed version of [simplessl.c](https://curl.haxx.se/libcurl/c/simplessl.html). HTTPS request of the [example.com](https://example.com) page, using SSL authentication. This script takes 4 arguments (SSL certificates file, SSL keys file, SSL keys password and CA certificates files), and prints out the page.
- **example4**: sandboxed version of [multi-poll.c](https://curl.haxx.se/libcurl/c/multi-poll.html). Same HTTP request as example1, with the addition of a polling method that can be used to track the status of the request. The page is printed out after it is downloaded.
- **example5**: sandboxed version of [multithread.c](https://curl.haxx.se/libcurl/c/multithread.html). Four HTTP request of the pages [example.com](http://example.com), [example.edu](http://example.edu), [example.net](http://example.net), [example.org](http://example.org) pages, performed at the same time using libcurl's multithreading methods. The threads' status and the pages are printed out.

View File

@ -17,16 +17,15 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
// Function taken from curl's getinmemory.c
size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp) {
// Function taken from curl's getinmemory.c
size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp) {
size_t realsize = size * nmemb; size_t realsize = size * nmemb;
struct MemoryStruct* mem = (struct MemoryStruct*)userp; struct MemoryStruct* mem = (struct MemoryStruct*)userp;
char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1); char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(ptr == NULL) { // Out of memory if (ptr == NULL) { // Out of memory
std::cout << "not enough memory (realloc returned NULL)\n"; std::cout << "not enough memory (realloc returned NULL)\n";
return 0; return 0;
} }
@ -35,7 +34,6 @@ size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
memcpy(&(mem->memory[mem->size]), contents, realsize); memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize; mem->size += realsize;
mem->memory[mem->size] = 0; mem->memory[mem->size] = 0;
return realsize;
return realsize;
} }

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#ifndef CALLBACKS_H #ifndef EXAMPLES_CALLBACKS_H
#define CALLBACKS_H #define EXAMPLES_CALLBACKS_H
#include <curl/curl.h> #include <curl/curl.h>
@ -22,7 +22,7 @@ extern "C" struct MemoryStruct {
size_t size; size_t size;
}; };
extern "C" size_t WriteMemoryCallback(void* contents, size_t size, extern "C" size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
size_t nmemb, void* userp); void* userp);
#endif // CALLBACKS_H #endif // EXAMPLES_CALLBACKS_H

View File

@ -22,26 +22,25 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx1 : public CurlSandbox { class CurlApiSandboxEx1 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
absl::Status status; absl::Status status;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
// Initialize sandbox2 and sapi // Initialize sandbox2 and sapi
CurlApiSandboxEx1 sandbox; CurlApiSandboxEx1 sandbox;
status = sandbox.Init(); status = sandbox.Init();
assert(status.ok()); assert(status.ok());
CurlApi api(&sandbox); CurlApi api(&sandbox);
@ -53,7 +52,7 @@ int main(int argc, char* argv[]) {
assert(curl.GetValue()); // Checking curl != nullptr assert(curl.GetValue()); // Checking curl != nullptr
// Specify URL to get // Specify URL to get
sapi::v::ConstCStr url("http://example.com"); sapi::v::ConstCStr url("http://example.com");
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore()); status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -63,7 +62,7 @@ int main(int argc, char* argv[]) {
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
status_or_int = api.curl_easy_setopt_long(&curl, CURLOPT_SSL_VERIFYPEER, 0l); status_or_int = api.curl_easy_setopt_long(&curl, CURLOPT_SSL_VERIFYPEER, 0l);
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -78,5 +77,4 @@ int main(int argc, char* argv[]) {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -27,20 +27,19 @@ struct MemoryStruct {
}; };
class CurlApiSandboxEx2 : public CurlSandbox { class CurlApiSandboxEx2 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
int main() { int main() {
absl::Status status; absl::Status status;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
@ -71,21 +70,21 @@ int main() {
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Set WriteMemoryCallback as the write function // Set WriteMemoryCallback as the write function
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEFUNCTION, status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEFUNCTION,
&remote_function_ptr); &remote_function_ptr);
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Pass 'chunk' struct to the callback function // Pass 'chunk' struct to the callback function
sapi::v::Struct<MemoryStruct> chunk; sapi::v::Struct<MemoryStruct> chunk;
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEDATA, status_or_int =
chunk.PtrBoth()); api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEDATA, chunk.PtrBoth());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Set a user agent // Set a user agent
sapi::v::ConstCStr user_agent("libcurl-agent/1.0"); sapi::v::ConstCStr user_agent("libcurl-agent/1.0");
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_USERAGENT, status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_USERAGENT,
user_agent.PtrBefore()); user_agent.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -107,5 +106,4 @@ int main() {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -22,38 +22,33 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx3 : public CurlSandbox { class CurlApiSandboxEx3 : public CurlSandbox {
public:
CurlApiSandboxEx3(std::string ssl_certificate, std::string ssl_key,
std::string ca_certificates)
: ssl_certificate(ssl_certificate),
ssl_key(ssl_key),
ca_certificates(ca_certificates) {}
public: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.AddFile(ssl_certificate)
.AddFile(ssl_key)
.AddFile(ca_certificates)
.BuildOrDie();
}
CurlApiSandboxEx3(std::string ssl_certificate, std::string ssl_key, std::string ssl_certificate;
std::string ca_certificates) std::string ssl_key;
: ssl_certificate(ssl_certificate), std::string ca_certificates;
ssl_key(ssl_key),
ca_certificates(ca_certificates) {}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.AddFile(ssl_certificate)
.AddFile(ssl_key)
.AddFile(ca_certificates)
.BuildOrDie();
}
std::string ssl_certificate;
std::string ssl_key;
std::string ca_certificates;
}; };
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
absl::Status status; absl::Status status;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
@ -70,7 +65,7 @@ int main(int argc, char* argv[]) {
status = sandbox.Init(); status = sandbox.Init();
assert(status.ok()); assert(status.ok());
CurlApi api(&sandbox); CurlApi api(&sandbox);
// Initialize curl (CURL_GLOBAL_DEFAULT = 3) // Initialize curl (CURL_GLOBAL_DEFAULT = 3)
status_or_int = api.curl_global_init(3l); status_or_int = api.curl_global_init(3l);
assert(status_or_int.ok()); assert(status_or_int.ok());
@ -84,29 +79,28 @@ int main(int argc, char* argv[]) {
// Specify URL to get (using HTTPS) // Specify URL to get (using HTTPS)
sapi::v::ConstCStr url("https://example.com"); sapi::v::ConstCStr url("https://example.com");
status_or_int = status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Set the SSL certificate type to "PEM" // Set the SSL certificate type to "PEM"
sapi::v::ConstCStr ssl_cert_type("PEM"); sapi::v::ConstCStr ssl_cert_type("PEM");
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERTTYPE, status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERTTYPE,
ssl_cert_type.PtrBefore()); ssl_cert_type.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Set the certificate for client authentication // Set the certificate for client authentication
sapi::v::ConstCStr sapi_ssl_certificate(ssl_certificate.c_str()); sapi::v::ConstCStr sapi_ssl_certificate(ssl_certificate.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERT, status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERT,
sapi_ssl_certificate.PtrBefore()); sapi_ssl_certificate.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Set the private key for client authentication // Set the private key for client authentication
sapi::v::ConstCStr sapi_ssl_key(ssl_key.c_str()); sapi::v::ConstCStr sapi_ssl_key(ssl_key.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLKEY, status_or_int =
sapi_ssl_key.PtrBefore()); api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLKEY, sapi_ssl_key.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -119,7 +113,7 @@ int main(int argc, char* argv[]) {
// Set the file with the certificates vaildating the server // Set the file with the certificates vaildating the server
sapi::v::ConstCStr sapi_ca_certificates(ca_certificates.c_str()); sapi::v::ConstCStr sapi_ca_certificates(ca_certificates.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_CAINFO, status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_CAINFO,
sapi_ca_certificates.PtrBefore()); sapi_ca_certificates.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -141,7 +135,6 @@ int main(int argc, char* argv[]) {
// Cleanup curl // Cleanup curl
status = api.curl_global_cleanup(); status = api.curl_global_cleanup();
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS;
return EXIT_SUCCESS;
} }

View File

@ -22,20 +22,19 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx4 : public CurlSandbox { class CurlApiSandboxEx4 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
int main() { int main() {
absl::Status status; absl::Status status;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
@ -60,11 +59,11 @@ int main() {
assert(status_or_curl.ok()); assert(status_or_curl.ok());
sapi::v::RemotePtr http_handle(status_or_curl.value()); sapi::v::RemotePtr http_handle(status_or_curl.value());
assert(http_handle.GetValue()); // Checking http_handle != nullptr assert(http_handle.GetValue()); // Checking http_handle != nullptr
// Specify URL to get // Specify URL to get
sapi::v::ConstCStr url("http://example.com"); sapi::v::ConstCStr url("http://example.com");
status_or_int = status_or_int =
api.curl_easy_setopt_ptr(&http_handle, CURLOPT_URL, url.PtrBefore()); api.curl_easy_setopt_ptr(&http_handle, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -79,20 +78,19 @@ int main() {
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
while (still_running.GetValue()) { while (still_running.GetValue()) {
sapi::v::Int numfds(0); sapi::v::Int numfds(0);
// Perform the request // Perform the request
status_or_int = api.curl_multi_perform(&multi_handle, status_or_int =
still_running.PtrBoth()); api.curl_multi_perform(&multi_handle, still_running.PtrBoth());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
if (still_running.GetValue()) { if (still_running.GetValue()) {
// Wait for an event or timeout // Wait for an event or timeout
sapi::v::NullPtr null_ptr; sapi::v::NullPtr null_ptr;
status_or_int = api.curl_multi_poll_sapi(&multi_handle, &null_ptr, 0, status_or_int = api.curl_multi_poll_sapi(&multi_handle, &null_ptr, 0,
1000, numfds.PtrBoth()); 1000, numfds.PtrBoth());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLM_OK); assert(status_or_int.value() == CURLM_OK);
@ -116,7 +114,6 @@ int main() {
// Cleanup curl // Cleanup curl
status = api.curl_global_cleanup(); status = api.curl_global_cleanup();
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -24,27 +24,26 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx5 : public CurlSandbox { class CurlApiSandboxEx5 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
struct thread_args { struct thread_args {
const char* url; const char* url;
CurlApi* api; CurlApi* api;
}; };
constexpr int kThreadsnumber = 4; constexpr int kThreadsnumber = 4;
void *pull_one_url(void* args) { void* pull_one_url(void* args) {
absl::Status status; absl::Status status;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
@ -58,9 +57,9 @@ void *pull_one_url(void* args) {
assert(curl.GetValue()); // Checking curl != nullptr assert(curl.GetValue()); // Checking curl != nullptr
// Specify URL to get // Specify URL to get
sapi::v::ConstCStr sapi_url(((thread_args*)args)->url); sapi::v::ConstCStr sapi_url(((thread_args*)args)->url);
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, status_or_int =
sapi_url.PtrBefore()); api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, sapi_url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -74,18 +73,13 @@ void *pull_one_url(void* args) {
assert(status.ok()); assert(status.ok());
return NULL; return NULL;
} }
const char * const urls[kThreadsnumber] = { const char* const urls[kThreadsnumber] = {
"http://example.com", "http://example.com", "http://example.edu", "http://example.net",
"http://example.edu", "http://example.org"};
"http://example.net",
"http://example.org"
};
int main(int argc, char **argv) {
int main(int argc, char** argv) {
pthread_t tid[kThreadsnumber]; pthread_t tid[kThreadsnumber];
absl::Status status; absl::Status status;
@ -96,22 +90,22 @@ int main(int argc, char **argv) {
status = sandbox.Init(); status = sandbox.Init();
assert(status.ok()); assert(status.ok());
CurlApi api(&sandbox); CurlApi api(&sandbox);
// Initialize curl (CURL_GLOBAL_DEFAULT = 3) // Initialize curl (CURL_GLOBAL_DEFAULT = 3)
status_or_int = api.curl_global_init(3l); status_or_int = api.curl_global_init(3l);
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Create the threads // Create the threads
for(int i = 0; i < kThreadsnumber; ++i) { for (int i = 0; i < kThreadsnumber; ++i) {
thread_args args = {urls[i], &api}; thread_args args = {urls[i], &api};
int error = pthread_create(&tid[i], NULL, pull_one_url, (void*)&args); int error = pthread_create(&tid[i], NULL, pull_one_url, (void*)&args);
assert(!error); assert(!error);
std::cout << "Thread "<< i << " gets " << urls[i] << std::endl; std::cout << "Thread " << i << " gets " << urls[i] << std::endl;
} }
// Join the threads // Join the threads
for(int i = 0; i< kThreadsnumber; i++) { for (int i = 0; i < kThreadsnumber; i++) {
pthread_join(tid[i], NULL); pthread_join(tid[i], NULL);
std::cout << "Thread " << i << " terminated" << std::endl; std::cout << "Thread " << i << " terminated" << std::endl;
} }
@ -119,7 +113,6 @@ int main(int argc, char **argv) {
// Cleanup curl // Cleanup curl
status = api.curl_global_cleanup(); status = api.curl_global_cleanup();
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS;
return EXIT_SUCCESS;
} }