clibdocker  0.1.0-alpha.0
A Docker API for C
Data Structures | Macros | Typedefs | Enumerations | Functions
docker_connection_util.h File Reference

Docker Connection Utils. More...

#include <coll_arraylist.h>
#include <coll_arraylist_map.h>
#include <stdlib.h>
#include "docker_common.h"
#include <stdbool.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#include <json-c/linkhash.h>
#include <curl/curl.h>
#include "docker_result.h"
Include dependency graph for docker_connection_util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  docker_context_t
 A docker context for a specific docker server. More...
 
struct  docker_call_t
 internal datastructure representing a Docker Call object. More...
 

Macros

#define DOCKER_API_VERSION_1_39   "1.39"
 Currently supported Docker API Version.
 
#define DOCKER_DEFAULT_UNIX_SOCKET   "/var/run/docker.sock"
 Docker UNIX Socket URL.
 
#define DOCKER_NPIPE_URL_PREFIX   "npipe://"
 Window NPIPE URL Prefix.
 
#define DOCKER_DEFAULT_WINDOWS_NAMED_PIPE   "npipe:////./pipe/docker_engine"
 Default Windows Docker NPIPE URL.
 
#define DOCKER_DEFAULT_LOCALHOST_URL   "http://localhost:2375/"
 Default Docker HTTP URL.
 
#define HEADER_JSON   "Content-Type: application/json"
 JSON Content Type Header.
 
#define HEADER_TAR   "Content-Type: application/x-tar"
 TAR Content Type Header.
 

Typedefs

typedef void() docker_result_handler_fn(struct docker_context_t *ctx, docker_result *result)
 Defines a docker result handler function. More...
 
typedef struct docker_context_t docker_context
 A docker context for a specific docker server.
 
typedef void() status_callback(char *msg, void *cbargs, void *client_cbargs)
 Status callback function type. This is used to get a status callback from docker calls.
 
typedef struct docker_call_t docker_call
 internal datastructure representing a Docker Call object. More...
 

Enumerations

enum  docker_object_type {
  NONE = 0 , CONTAINER = 1 , IMAGE = 2 , SYSTEM = 3 ,
  NETWORK = 4 , VOLUME = 5
}
 Docker Object type in the Docker API call JSON.
 

Functions

bool is_http_url (char *url)
 Check if the given string input is a HTTP(s) URL. More...
 
bool is_unix_socket (char *url)
 Check if the given string input is a UNIX socket URL. More...
 
bool is_npipe (char *url)
 Check if the given string input is an NPIPE URL. More...
 
char * npipe_url_only (char *url)
 Get the docker api url part of the npipe url. Removes the npipe:// prefix from the url. More...
 
d_err_t docker_api_init ()
 
void docker_api_cleanup ()
 
d_err_t make_docker_context_url (docker_context **ctx, const char *url)
 
d_err_t make_docker_context_default_local (docker_context **ctx)
 
d_err_t docker_context_result_handler_set (docker_context *ctx, docker_result_handler_fn *result_handler_fn)
 
docker_result_handler_fndocker_context_result_handler_get (docker_context *ctx)
 Get the docker context result handler function. More...
 
d_err_t docker_context_client_args_set (docker_context *ctx, void *client_args)
 Set the client args for the docker context. More...
 
void * docker_context_client_args_get (docker_context *ctx)
 Get the client args for the docker context. More...
 
d_err_t free_docker_context (docker_context **ctx)
 
d_err_t make_docker_call (docker_call **dcall, char *site_url, docker_object_type object, const char *id, const char *method)
 Create a new generic Docker API Call object. More...
 
void docker_call_request_method_set (docker_call *dcall, char *method)
 Set the docker request HTTP method. More...
 
char * docker_call_request_method_get (docker_call *dcall)
 Get the docker request HTTP method. More...
 
void docker_call_content_type_header_set (docker_call *dcall, char *content_type_header)
 Set the docker call content type header. More...
 
char * docker_call_content_type_header_get (docker_call *dcall)
 Get the docker request content type header. More...
 
void docker_call_request_data_set (docker_call *dcall, char *request_data)
 Set the docker request data. More...
 
char * docker_call_request_data_get (docker_call *dcall)
 Get the docker request data. More...
 
void docker_call_request_data_len_set (docker_call *dcall, size_t request_data_len)
 Set the docker request data length. More...
 
size_t docker_call_request_data_len_get (docker_call *dcall)
 Get the docker request data length. More...
 
char * docker_call_response_data_get (docker_call *dcall)
 Get the docker response data. More...
 
size_t docker_call_response_data_length (docker_call *dcall)
 Get the docker response data length. More...
 
int docker_call_http_code_get (docker_call *dcall)
 Get the docker response HTTP code. More...
 
void docker_call_http_code_set (docker_call *dcall, int http_code)
 Set the docker respose HTTP code. More...
 
void docker_call_status_cb_set (docker_call *dcall, status_callback *status_callback)
 Set the docker call callback function. More...
 
status_callbackdocker_call_status_cb_get (docker_call *dcall)
 Get the docker call callback function. More...
 
void docker_call_cb_args_set (docker_call *dcall, void *cb_args)
 Set the docker call callback function callback args. More...
 
void * docker_call_cb_args_get (docker_call *dcall)
 Get the docker call callback function callback args. More...
 
void docker_call_client_cb_args_set (docker_call *dcall, void *client_cb_args)
 Set the docker call client callback args. More...
 
void * docker_call_client_cb_args_get (docker_call *dcall)
 Get the docker call client callback args. More...
 
void free_docker_call (docker_call *dcall)
 Free the docker call object. More...
 
int docker_call_params_add (docker_call *dcall, char *param, char *value)
 Add a string key/value pair to the docker call parameters. More...
 
int docker_call_params_add_boolean (docker_call *dcall, char *param, int value)
 Add a string key/boolean value pair to the docker call parameters. More...
 
char * docker_call_get_url (docker_call *dcall)
 Get the docker request HTTP url. More...
 
char * docker_call_get_svc_url (docker_call *dcall)
 Get the docker request service url. More...
 
d_err_t docker_call_exec (docker_context *ctx, docker_call *dcall, json_object **response)
 Execute the Docker Call i.e. send the request to the server and get response. More...
 

Detailed Description

Docker Connection Utils.

Typedef Documentation

◆ docker_call

typedef struct docker_call_t docker_call

internal datastructure representing a Docker Call object.

All Docker API call implementations internally use this object to represent a call to the Docker API.

◆ docker_result_handler_fn

typedef void() docker_result_handler_fn(struct docker_context_t *ctx, docker_result *result)

Defines a docker result handler function.

This function type is used to define a result handler by a client program. The result handler is called when the docker API call returns.

Function Documentation

◆ docker_api_cleanup()

void docker_api_cleanup ( )

Global cleanup of the docker API, should be called once when all api usage is complete.

◆ docker_api_init()

d_err_t docker_api_init ( )

Global initialization of the docker API, should be called only once in an application

Returns
error code 0 if initialization was ok, non-zero otherwise

◆ docker_call_cb_args_get()

void* docker_call_cb_args_get ( docker_call dcall)

Get the docker call callback function callback args.

Parameters
dcalldocker call object
Returns
void* callback arguments

References docker_call_t::cb_args.

◆ docker_call_cb_args_set()

void docker_call_cb_args_set ( docker_call dcall,
void *  cb_args 
)

Set the docker call callback function callback args.

Parameters
dcalldocker call object
cb_argscallback args (argument to callback function)

References docker_call_t::cb_args.

◆ docker_call_client_cb_args_get()

void* docker_call_client_cb_args_get ( docker_call dcall)

Get the docker call client callback args.

Parameters
dcalldocker call object
Returns
void* client callback args

References docker_call_t::client_cb_args.

◆ docker_call_client_cb_args_set()

void docker_call_client_cb_args_set ( docker_call dcall,
void *  client_cb_args 
)

Set the docker call client callback args.

Parameters
dcalldocker call object
client_cb_argsargs set by the client of the api

References docker_call_t::client_cb_args.

◆ docker_call_content_type_header_get()

char* docker_call_content_type_header_get ( docker_call dcall)

Get the docker request content type header.

Parameters
dcalldocker call object
Returns
char* content type header

References docker_call_t::content_type_header.

◆ docker_call_content_type_header_set()

void docker_call_content_type_header_set ( docker_call dcall,
char *  content_type_header 
)

Set the docker call content type header.

Parameters
dcalldocker call object
content_type_headercontent type header

References docker_call_t::content_type_header, and str_clone().

◆ docker_call_exec()

d_err_t docker_call_exec ( docker_context ctx,
docker_call dcall,
json_object **  response 
)

Execute the Docker Call i.e. send the request to the server and get response.

Parameters
ctxdocker context
dcalldocker call object
responsejson response object to be set
Returns
d_err_t error code

◆ docker_call_get_svc_url()

char* docker_call_get_svc_url ( docker_call dcall)

Get the docker request service url.

Parameters
dcalldocker call object
Returns
char* service url

References docker_call_t::id, docker_call_t::method, docker_call_t::object, docker_call_t::params, and str_clone().

◆ docker_call_get_url()

char* docker_call_get_url ( docker_call dcall)

Get the docker request HTTP url.

Parameters
dcalldocker call object
Returns
char* http url

References docker_call_t::id, docker_call_t::method, docker_call_t::object, docker_call_t::params, docker_call_t::site_url, and str_clone().

◆ docker_call_http_code_get()

int docker_call_http_code_get ( docker_call dcall)

Get the docker response HTTP code.

Parameters
dcalldocker call object
Returns
int HTTP response code

References docker_call_t::http_error_code.

◆ docker_call_http_code_set()

void docker_call_http_code_set ( docker_call dcall,
int  http_code 
)

Set the docker respose HTTP code.

Parameters
dcalldocker call object
http_codehttp response code

References docker_call_t::http_error_code.

◆ docker_call_params_add()

int docker_call_params_add ( docker_call dcall,
char *  param,
char *  value 
)

Add a string key/value pair to the docker call parameters.

Parameters
dcalldocker call object
paramparameter key
valueparameter value
Returns
int error code

References docker_call_t::params, and str_clone().

Referenced by docker_call_params_add_boolean().

◆ docker_call_params_add_boolean()

int docker_call_params_add_boolean ( docker_call dcall,
char *  param,
int  value 
)

Add a string key/boolean value pair to the docker call parameters.

Parameters
dcalldocker call object
paramparameter key
valueparameter value
Returns
int error code

References docker_call_params_add().

◆ docker_call_request_data_get()

char* docker_call_request_data_get ( docker_call dcall)

Get the docker request data.

Parameters
dcalldocker call object
Returns
char* request data

References docker_call_t::request_data.

◆ docker_call_request_data_len_get()

size_t docker_call_request_data_len_get ( docker_call dcall)

Get the docker request data length.

Parameters
dcalldocker call object
Returns
size_t request data length

References docker_call_t::request_data_len.

◆ docker_call_request_data_len_set()

void docker_call_request_data_len_set ( docker_call dcall,
size_t  request_data_len 
)

Set the docker request data length.

Parameters
dcalldocker call object
request_data_lenrequest data length

References docker_call_t::request_data_len.

Referenced by docker_call_request_data_set().

◆ docker_call_request_data_set()

void docker_call_request_data_set ( docker_call dcall,
char *  request_data 
)

Set the docker request data.

Parameters
dcalldocker call object
request_datajson request data

References docker_call_request_data_len_set(), docker_call_t::request_data, and str_clone().

◆ docker_call_request_method_get()

char* docker_call_request_method_get ( docker_call dcall)

Get the docker request HTTP method.

Parameters
dcalldocker call object
Returns
char* http method

References docker_call_t::request_method.

◆ docker_call_request_method_set()

void docker_call_request_method_set ( docker_call dcall,
char *  method 
)

Set the docker request HTTP method.

Parameters
dcalldocker call object
methodhttp method

References docker_call_t::request_method, and str_clone().

◆ docker_call_response_data_get()

char* docker_call_response_data_get ( docker_call dcall)

Get the docker response data.

Parameters
dcalldocker call object
Returns
char* response data

References docker_call_t::memory.

◆ docker_call_response_data_length()

size_t docker_call_response_data_length ( docker_call dcall)

Get the docker response data length.

Parameters
dcalldocker call object
Returns
size_t response data length

References docker_call_t::size.

◆ docker_call_status_cb_get()

status_callback* docker_call_status_cb_get ( docker_call dcall)

Get the docker call callback function.

Parameters
dcalldocker call object
Returns
statuc_callback* callback function

References docker_call_t::status_cb.

◆ docker_call_status_cb_set()

void docker_call_status_cb_set ( docker_call dcall,
status_callback status_callback 
)

Set the docker call callback function.

Parameters
dcalldocker call object
status_callback*status callback function for the docker call

References docker_call_t::status_cb.

◆ docker_context_client_args_get()

void* docker_context_client_args_get ( docker_context ctx)

Get the client args for the docker context.

Parameters
ctxdocker context
Returns
void* client args

References docker_context_t::client_args.

◆ docker_context_client_args_set()

d_err_t docker_context_client_args_set ( docker_context ctx,
void *  client_args 
)

Set the client args for the docker context.

Parameters
ctxdocker context
client_argsa client args value
Returns
d_err_t error code

References docker_context_t::client_args.

◆ docker_context_result_handler_get()

docker_result_handler_fn* docker_context_result_handler_get ( docker_context ctx)

Get the docker context result handler function.

Parameters
ctxdocker context
Returns
docker_result_handler_fn* result handler fn

References docker_context_t::result_handler_fn.

◆ docker_context_result_handler_set()

d_err_t docker_context_result_handler_set ( docker_context ctx,
docker_result_handler_fn result_handler_fn 
)

Set the result handler function for the docker context. Every API call's result will be passed to the result handler, once the call is complete. NOTE: the result handler will be freed before the API call returns, so if a copy of any of the data in the result is to be retained, caller must make a copy.

Parameters
ctxthe docker context
result_handler_fncallback which receives the docker_result object.
Returns
error code

References docker_context_t::result_handler_fn.

◆ free_docker_call()

void free_docker_call ( docker_call dcall)

Free the docker call object.

Parameters
dcalldocker call object

References docker_call_t::memory, and docker_call_t::params.

◆ free_docker_context()

d_err_t free_docker_context ( docker_context **  ctx)

Free docker context memory.

◆ is_http_url()

bool is_http_url ( char *  url)

Check if the given string input is a HTTP(s) URL.

Parameters
urlinput string
Returns
bool whether string is an http url

◆ is_npipe()

bool is_npipe ( char *  url)

Check if the given string input is an NPIPE URL.

Parameters
urlinput string
Returns
bool whether string is an Npipe url.

References DOCKER_NPIPE_URL_PREFIX.

Referenced by npipe_url_only().

◆ is_unix_socket()

bool is_unix_socket ( char *  url)

Check if the given string input is a UNIX socket URL.

Parameters
urlinput string
Returns
bool whether string is a Unix socket.

◆ make_docker_call()

d_err_t make_docker_call ( docker_call **  dcall,
char *  site_url,
docker_object_type  object,
const char *  id,
const char *  method 
)

Create a new generic Docker API Call object.

The Docker Call object has getters and setters of the form docker_call_<member>_get , and docker_call_<member>_set.

Parameters
dcallpointer to docker_call* to create and return
site_urlrequest url
objectthe docker object type whose endpoint is being called
iddocker object id
methoddocker request http method
Returns
d_err_t error code

Referenced by docker_container_attach_default(), docker_container_changes(), docker_container_list(), docker_container_logs(), docker_create_container(), docker_image_build_cb(), docker_images_list(), docker_inspect_container(), docker_kill_container(), docker_networks_list(), docker_pause_container(), docker_ping(), docker_process_list_container(), docker_remove_container(), docker_rename_container(), docker_restart_container(), docker_start_container(), docker_stop_container(), docker_system_df(), docker_system_events_cb(), docker_system_info(), docker_system_version(), docker_unpause_container(), docker_volume_create(), docker_volume_delete(), docker_volumes_delete_unused(), docker_volumes_list(), and docker_wait_container().

◆ make_docker_context_default_local()

d_err_t make_docker_context_default_local ( docker_context **  ctx)

Connect to the default local docker.

Parameters
ctxcontext to create
Returns
error code

References DOCKER_DEFAULT_UNIX_SOCKET, DOCKER_DEFAULT_WINDOWS_NAMED_PIPE, and make_docker_context_url().

◆ make_docker_context_url()

d_err_t make_docker_context_url ( docker_context **  ctx,
const char *  url 
)

Create a new docker context with the given url.

The method makes a copy of the given string for use, so that it can be safely freed by the calling program.

Referenced by make_docker_context_default_local().

◆ npipe_url_only()

char* npipe_url_only ( char *  url)

Get the docker api url part of the npipe url. Removes the npipe:// prefix from the url.

Parameters
urlinput string
Returns
char* a new string with the docker url part

References DOCKER_NPIPE_URL_PREFIX, and is_npipe().