clibdocker  0.1.0-alpha.0
A Docker API for C
docker_connection_util.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2018-2022 Abhishek Mishra
4  *
5  * This file is part of clibdocker.
6  *
7  * clibdocker is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation,
10  * either version 3 of the License, or (at your option)
11  * any later version.
12  *
13  * clibdocker is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with clibdocker.
20  * If not, see <https://www.gnu.org/licenses/>.
21  *
22  */
23 
29 #ifndef DOCKER_CONNECTION_UTIL_H_
30 #define DOCKER_CONNECTION_UTIL_H_
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <coll_arraylist.h>
37 #include <coll_arraylist_map.h>
38 #include <stdlib.h>
39 #include "docker_common.h"
40 #include <stdbool.h>
41 #include <json-c/json_object.h>
42 #include <json-c/json_tokener.h>
43 #include <json-c/linkhash.h>
44 #include <curl/curl.h>
45 #include "docker_result.h"
46 
50 #define DOCKER_API_VERSION_1_39 "1.39"
51 
55 #define DOCKER_DEFAULT_UNIX_SOCKET "/var/run/docker.sock"
56 
60 #define DOCKER_NPIPE_URL_PREFIX "npipe://"
61 
65 #define DOCKER_DEFAULT_WINDOWS_NAMED_PIPE "npipe:////./pipe/docker_engine"
66 
70 #define DOCKER_DEFAULT_LOCALHOST_URL "http://localhost:2375/"
71 
72 #if defined(_WIN32)
73 #define DOCKER_DEFAULT_SOCKET DOCKER_DEFAULT_WINDOWS_NAMED_PIPE
74 #endif
75 
76 #if defined(unix) || defined(__unix__) || defined(__unix)
77 #define DOCKER_DEFAULT_SOCKET DOCKER_DEFAULT_UNIX_SOCKET
78 #endif
79 
83 #define HEADER_JSON "Content-Type: application/json"
84 
88 #define HEADER_TAR "Content-Type: application/x-tar"
89 
96 MODULE_API bool is_http_url(char* url);
97 
104 MODULE_API bool is_unix_socket(char* url);
105 
112 MODULE_API bool is_npipe(char* url);
113 
121 MODULE_API char* npipe_url_only(char* url);
122 
126 typedef enum {
127  NONE = 0, CONTAINER = 1, IMAGE = 2, SYSTEM = 3, NETWORK = 4, VOLUME = 5
129 
136 typedef void (docker_result_handler_fn) (struct docker_context_t* ctx, docker_result* result);
137 
141 typedef struct docker_context_t {
142  char* url;
143  char* api_version;
145  void* client_args;
147 
154 MODULE_API d_err_t docker_api_init();
155 
156 
162 MODULE_API void docker_api_cleanup();
163 
170 MODULE_API d_err_t make_docker_context_url(docker_context** ctx, const char* url);
171 
179 
193 
201 
209 MODULE_API d_err_t docker_context_client_args_set(docker_context* ctx, void* client_args);
210 
217 MODULE_API void* docker_context_client_args_get(docker_context* ctx);
218 
222 MODULE_API d_err_t free_docker_context(docker_context** ctx);
223 
224 // BEGIN: Docker API Calls HTTP Utils V2
225 
229 typedef void (status_callback)(char* msg, void* cbargs, void* client_cbargs);
230 
237 typedef struct docker_call_t {
238  // URL Parts
239  char* site_url;
241  char* id;
242  char* method;
243  coll_al_map* params;
244 
245  // HTTP
248 
249  // Request object
250  char* request_data;
252 
253  // Response data
254  // char* response_data;
256 
257  // HTTP Response Internals
258  char* memory;
259  size_t capacity;
260  size_t size;
261  size_t flush_end;
262 
263  // Callback Config
265  void* cb_args;
268 
283 MODULE_API d_err_t make_docker_call(docker_call** dcall, char* site_url, docker_object_type object,
284  const char* id, const char* method);
285 
292 MODULE_API void docker_call_request_method_set(docker_call* dcall, char* method);
293 
300 MODULE_API char* docker_call_request_method_get(docker_call* dcall);
301 
308 MODULE_API void docker_call_content_type_header_set(docker_call* dcall, char* content_type_header);
309 
316 MODULE_API char* docker_call_content_type_header_get(docker_call* dcall);
317 
324 MODULE_API void docker_call_request_data_set(docker_call* dcall, char* request_data);
325 
332 MODULE_API char* docker_call_request_data_get(docker_call* dcall);
333 
340 MODULE_API void docker_call_request_data_len_set(docker_call* dcall, size_t request_data_len);
341 
348 MODULE_API size_t docker_call_request_data_len_get(docker_call* dcall);
349 
350 //void docker_call_response_data_set(docker_call* dcall, char* response_data);
351 
358 MODULE_API char* docker_call_response_data_get(docker_call* dcall);
359 
366 MODULE_API size_t docker_call_response_data_length(docker_call* dcall);
367 
374 MODULE_API int docker_call_http_code_get(docker_call* dcall);
375 
382 MODULE_API void docker_call_http_code_set(docker_call* dcall, int http_code);
383 
391 
399 
406 MODULE_API void docker_call_cb_args_set(docker_call* dcall, void* cb_args);
407 
414 MODULE_API void* docker_call_cb_args_get(docker_call* dcall);
415 
422 MODULE_API void docker_call_client_cb_args_set(docker_call* dcall, void* client_cb_args);
423 
430 MODULE_API void* docker_call_client_cb_args_get(docker_call* dcall);
431 
437 MODULE_API void free_docker_call(docker_call* dcall);
438 
447 MODULE_API int docker_call_params_add(docker_call* dcall, char* param, char* value);
448 
457 MODULE_API int docker_call_params_add_boolean(docker_call* dcall, char* param, int value);
458 
465 MODULE_API char* docker_call_get_url(docker_call* dcall);
466 
473 MODULE_API char* docker_call_get_svc_url(docker_call* dcall);
474 
483 MODULE_API d_err_t docker_call_exec(docker_context* ctx, docker_call* dcall, json_object** response);
484 
485 // END: Docker API Calls HTTP Utils V2
486 
487 // BEGIN: Windows Named Pipe Support
488 
489 #ifdef _WIN32
490 #include <windows.h>
491 #include <stdio.h>
492 #include <conio.h>
493 #include <tchar.h>
494 
495 #define NPIPE_READ_BUFSIZE 1024
496 
497 #endif
498 
499 // END: Windows Named Pipe Support
500 
501 #ifdef __cplusplus
502 }
503 #endif
504 
505 #endif /* DOCKER_CONNECTION_UTIL_H_ */
Some common declarations.
void docker_call_request_data_set(docker_call *dcall, char *request_data)
Set the docker request data.
Definition: docker_connection_util.c:387
void docker_call_request_data_len_set(docker_call *dcall, size_t request_data_len)
Set the docker request data length.
Definition: docker_connection_util.c:405
d_err_t make_docker_context_default_local(docker_context **ctx)
Definition: docker_connection_util.c:165
void() docker_result_handler_fn(struct docker_context_t *ctx, docker_result *result)
Defines a docker result handler function.
Definition: docker_connection_util.h:136
void docker_call_request_method_set(docker_call *dcall, char *method)
Set the docker request HTTP method.
Definition: docker_connection_util.c:353
docker_result_handler_fn * docker_context_result_handler_get(docker_context *ctx)
Get the docker context result handler function.
Definition: docker_connection_util.c:189
size_t docker_call_request_data_len_get(docker_call *dcall)
Get the docker request data length.
Definition: docker_connection_util.c:413
char * docker_call_content_type_header_get(docker_call *dcall)
Get the docker request content type header.
Definition: docker_connection_util.c:378
status_callback * docker_call_status_cb_get(docker_call *dcall)
Get the docker call callback function.
Definition: docker_connection_util.c:465
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.
Definition: docker_connection_util.h:229
size_t docker_call_response_data_length(docker_call *dcall)
Get the docker response data length.
Definition: docker_connection_util.c:431
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.
Definition: docker_connection_util.c:743
bool is_npipe(char *url)
Check if the given string input is an NPIPE URL.
Definition: docker_connection_util.c:78
struct docker_context_t docker_context
A docker context for a specific docker server.
d_err_t free_docker_context(docker_context **ctx)
Definition: docker_connection_util.c:219
void free_docker_call(docker_call *dcall)
Free the docker call object.
Definition: docker_connection_util.c:520
void docker_call_content_type_header_set(docker_call *dcall, char *content_type_header)
Set the docker call content type header.
Definition: docker_connection_util.c:370
void docker_call_cb_args_set(docker_call *dcall, void *cb_args)
Set the docker call callback function callback args.
Definition: docker_connection_util.c:474
void docker_api_cleanup()
Definition: docker_connection_util.c:114
char * docker_call_response_data_get(docker_call *dcall)
Get the docker response data.
Definition: docker_connection_util.c:422
char * npipe_url_only(char *url)
Get the docker api url part of the npipe url. Removes the npipe:// prefix from the url.
Definition: docker_connection_util.c:90
docker_object_type
Docker Object type in the Docker API call JSON.
Definition: docker_connection_util.h:126
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.
Definition: docker_connection_util.c:543
d_err_t docker_context_result_handler_set(docker_context *ctx, docker_result_handler_fn *result_handler_fn)
Definition: docker_connection_util.c:180
char * docker_call_request_method_get(docker_call *dcall)
Get the docker request HTTP method.
Definition: docker_connection_util.c:361
char * docker_call_get_svc_url(docker_call *dcall)
Get the docker request service url.
Definition: docker_connection_util.c:608
void docker_call_client_cb_args_set(docker_call *dcall, void *client_cb_args)
Set the docker call client callback args.
Definition: docker_connection_util.c:491
int docker_call_http_code_get(docker_call *dcall)
Get the docker response HTTP code.
Definition: docker_connection_util.c:440
d_err_t docker_context_client_args_set(docker_context *ctx, void *client_args)
Set the client args for the docker context.
Definition: docker_connection_util.c:198
void * docker_call_cb_args_get(docker_call *dcall)
Get the docker call callback function callback args.
Definition: docker_connection_util.c:482
char * docker_call_get_url(docker_call *dcall)
Get the docker request HTTP url.
Definition: docker_connection_util.c:555
void * docker_call_client_cb_args_get(docker_call *dcall)
Get the docker call client callback args.
Definition: docker_connection_util.c:499
void * docker_context_client_args_get(docker_context *ctx)
Get the client args for the docker context.
Definition: docker_connection_util.c:207
int docker_call_params_add(docker_call *dcall, char *param, char *value)
Add a string key/value pair to the docker call parameters.
Definition: docker_connection_util.c:534
struct docker_call_t docker_call
internal datastructure representing a Docker Call object.
bool is_unix_socket(char *url)
Check if the given string input is a UNIX socket URL.
Definition: docker_connection_util.c:66
char * docker_call_request_data_get(docker_call *dcall)
Get the docker request data.
Definition: docker_connection_util.c:396
void docker_call_http_code_set(docker_call *dcall, int http_code)
Set the docker respose HTTP code.
Definition: docker_connection_util.c:449
d_err_t docker_api_init()
Definition: docker_connection_util.c:103
d_err_t make_docker_context_url(docker_context **ctx, const char *url)
Definition: docker_connection_util.c:119
void docker_call_status_cb_set(docker_call *dcall, status_callback *status_callback)
Set the docker call callback function.
Definition: docker_connection_util.c:457
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.
Definition: docker_connection_util.c:306
bool is_http_url(char *url)
Check if the given string input is a HTTP(s) URL.
Definition: docker_connection_util.c:54
Docker Results.
enum _config_error d_err_t
Definition: docker_result.h:60
internal datastructure representing a Docker Call object.
Definition: docker_connection_util.h:237
status_callback * status_cb
the status callback method
Definition: docker_connection_util.h:264
docker_object_type object
docker object type enum value
Definition: docker_connection_util.h:240
void * cb_args
callback args for internal usage
Definition: docker_connection_util.h:265
coll_al_map * params
docker request parameters map
Definition: docker_connection_util.h:243
size_t size
used size of the storage
Definition: docker_connection_util.h:260
size_t flush_end
size of storage already flushed
Definition: docker_connection_util.h:261
size_t capacity
total capacity of internal response storage
Definition: docker_connection_util.h:259
int http_error_code
http response code
Definition: docker_connection_util.h:255
char * memory
internal memory used for response
Definition: docker_connection_util.h:258
void * client_cb_args
callback args provided by client
Definition: docker_connection_util.h:266
char * id
docker object id if applicable
Definition: docker_connection_util.h:241
char * content_type_header
http request content type header value
Definition: docker_connection_util.h:247
char * request_data
http request data
Definition: docker_connection_util.h:250
char * site_url
site url
Definition: docker_connection_util.h:239
char * request_method
http request method
Definition: docker_connection_util.h:246
char * method
docker api request method
Definition: docker_connection_util.h:242
size_t request_data_len
http request data length
Definition: docker_connection_util.h:251
A docker context for a specific docker server.
Definition: docker_connection_util.h:141
docker_result_handler_fn * result_handler_fn
Result handler for all responses.
Definition: docker_connection_util.h:144
void * client_args
Client args passed to callback functions.
Definition: docker_connection_util.h:145
char * api_version
API version expected.
Definition: docker_connection_util.h:143
char * url
Url of the docker server.
Definition: docker_connection_util.h:142
Definition: docker_result.h:93