2 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #define NO_BINDING_VERBOSE_MACRO
28 #include "afb-config.h"
31 #include <afb/afb-binding.h>
33 #if !defined(BINDING_INSTALL_DIR)
34 #error "you should define BINDING_INSTALL_DIR"
37 #define AFB_VERSION "0.5"
40 #define DEFLT_CNTX_TIMEOUT 3600 // default Client Connection
42 #define DEFLT_API_TIMEOUT 20 // default Plugin API Timeout [0=NoLimit
44 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache
47 #define DEFLT_AUTH_TOKEN NULL // expect for debug should == NULL
48 #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated
52 // Define command line option
54 #define SET_BACKGROUND 2
55 #define SET_FORGROUND 3
57 #define SET_TCP_PORT 5
58 #define SET_ROOT_DIR 6
59 #define SET_ROOT_BASE 7
60 #define SET_ROOT_API 8
63 #define SET_CACHE_TIMEOUT 10
64 #define SET_SESSION_DIR 11
66 #define SET_AUTH_TOKEN 12
68 #define SET_APITIMEOUT 14
69 #define SET_CNTXTIMEOUT 15
71 #define DISPLAY_VERSION 16
72 #define DISPLAY_HELP 17
75 #define SET_READYFD 19
77 #define DBUS_CLIENT 20
78 #define DBUS_SERVICE 21
81 #define SET_SESSIONMAX 23
86 #define SET_ROOT_HTTP 26
88 #define SET_TRACEREQ 27
90 // Command line structure hold cli --command + help text
92 int val; // command number within application
93 int has_arg; // command number within application
94 char *name; // command as used in --xxxx cli
95 char *help; // help text
99 static AFB_options cliOptions[] = {
101 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
103 {SET_FORGROUND, 0, "foreground", "Get all in foreground mode"},
104 {SET_BACKGROUND, 0, "daemon", "Get all in background mode"},
106 {SET_TCP_PORT, 1, "port", "HTTP listening TCP port [default 1234]"},
107 {SET_ROOT_DIR, 1, "rootdir", "Root Directory [default $HOME/.AFB]"},
108 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default rootdir]"},
109 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
110 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
111 {SET_ALIAS, 1, "alias", "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
113 {SET_APITIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default 10]"},
114 {SET_CNTXTIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
115 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default 3600]"},
117 {SET_SESSION_DIR, 1, "sessiondir", "Sessions file path [default rootdir/sessions]"},
119 {SET_LDPATH, 1, "ldpaths", "Load bindingss from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
120 {SET_AUTH_TOKEN, 1, "token", "Initial Secret [default=no-session, --token= for session without authentication]"},
122 {DISPLAY_VERSION, 0, "version", "Display version and copyright"},
123 {DISPLAY_HELP, 0, "help", "Display this help"},
125 {SET_MODE, 1, "mode", "Set the mode: either local, remote or global"},
126 {SET_READYFD, 1, "readyfd", "Set the #fd to signal when ready"},
128 {DBUS_CLIENT, 1, "dbus-client", "Bind to an afb service through dbus"},
129 {DBUS_SERVICE, 1, "dbus-server", "Provides an afb service through dbus"},
130 {WS_CLIENT, 1, "ws-client", "Bind to an afb service through websocket"},
131 {WS_SERVICE, 1, "ws-server", "Provides an afb service through websockets"},
132 {SO_BINDING, 1, "binding", "Load the binding of path"},
134 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default 10]"},
136 {SET_TRACEREQ, 1, "tracereq", "Log the requests: no, common, extra, all"},
149 static struct enumdesc tracereq_desc[] = {
151 { "common", afb_hook_flags_req_common },
152 { "extra", afb_hook_flags_req_extra },
153 { "all", afb_hook_flags_req_all },
157 static struct enumdesc mode_desc[] = {
158 { "local", AFB_MODE_LOCAL },
159 { "remote", AFB_MODE_REMOTE },
160 { "global", AFB_MODE_GLOBAL },
164 /*----------------------------------------------------------
166 | print version and copyright
167 +--------------------------------------------------------- */
168 static void printVersion(FILE * file)
170 fprintf(file, "\n----------------------------------------- \n");
171 fprintf(file, " AFB [Application Framework Binder] version=%s |\n",
173 fprintf(file, " \n");
175 " Copyright (C) 2015, 2016, 2017 \"IoT.bzh\" [fulup -at- iot.bzh]\n");
176 fprintf(file, " AFB comes with ABSOLUTELY NO WARRANTY.\n");
177 fprintf(file, " Licence Apache 2\n\n");
181 /*----------------------------------------------------------
183 | print information from long option array
184 +--------------------------------------------------------- */
186 static void printHelp(FILE * file, const char *name)
191 fprintf(file, "%s:\nallowed options\n", name);
192 for (ind = 0; cliOptions[ind].name != NULL; ind++) {
193 strcpy(command, cliOptions[ind].name);
194 if (cliOptions[ind].has_arg)
195 strcat(command, "=xxxx");
196 fprintf(file, " --%-15s %s\n", command, cliOptions[ind].help);
199 "Example:\n %s\\\n --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
203 // load config from disk and merge with CLI option
204 static void config_set_default(struct afb_config *config)
207 if (config->httpdPort == 0)
208 config->httpdPort = 1234;
210 // default binding API timeout
211 if (config->apiTimeout == 0)
212 config->apiTimeout = DEFLT_API_TIMEOUT;
214 // default AUTH_TOKEN
215 if (config->token == NULL)
216 config->token = DEFLT_AUTH_TOKEN;
218 // cache timeout default one hour
219 if (config->cacheTimeout == 0)
220 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
222 // cache timeout default one hour
223 if (config->cntxTimeout == 0)
224 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
226 // max count of sessions
227 if (config->nbSessionMax == 0)
228 config->nbSessionMax = CTX_NBCLIENTS;
230 if (config->rootdir == NULL) {
231 config->rootdir = getenv("AFBDIR");
232 if (config->rootdir == NULL) {
233 config->rootdir = malloc(512);
234 strncpy(config->rootdir, getenv("HOME"), 512);
235 strncat(config->rootdir, "/.AFB", 512);
238 // if no Angular/HTML5 rootbase let's try '/' as default
239 if (config->rootbase == NULL)
240 config->rootbase = "/opa";
242 if (config->rootapi == NULL)
243 config->rootapi = "/api";
245 if (config->ldpaths == NULL)
246 config->ldpaths = BINDING_INSTALL_DIR;
248 // if no session dir create a default path from rootdir
249 if (config->sessiondir == NULL) {
250 config->sessiondir = malloc(512);
251 strncpy(config->sessiondir, config->rootdir, 512);
252 strncat(config->sessiondir, "/sessions", 512);
254 // if no config dir create a default path from sessiondir
255 if (config->console == NULL) {
256 config->console = malloc(512);
257 strncpy(config->console, config->sessiondir, 512);
258 strncat(config->console, "/AFB-console.out", 512);
262 /*---------------------------------------------------------
264 | Parse option and launch action
265 +--------------------------------------------------------- */
267 static void list_add(struct afb_config_list **head, char *value)
269 struct afb_config_list *item;
275 while (item != NULL) {
283 item = malloc(sizeof *item);
285 ERROR("out of memory");
297 static char *argvalstr(int index)
300 ERROR("option [--%s] needs a value i.e. --%s=xxx",
301 cliOptions[index].name, cliOptions[index].name);
307 static int argvalenum(int index, struct enumdesc *desc)
311 char *list, *name = argvalstr(index);
314 while(desc[i].name && strcmp(desc[i].name, name))
320 len += strlen(desc[i++].name);
321 list = malloc(len + i + i);
323 ERROR("option [--%s] bad value (found %s)",
324 cliOptions[index].name, name);
327 strcpy(list, desc[i].name ? : "");
328 while(desc[++i].name)
329 strcat(strcat(list, ", "), desc[i].name);
330 ERROR("option [--%s] bad value, only accepts values %s (found %s)",
331 cliOptions[index].name, list, name);
336 return desc[i].value;
339 static int argvalint(int index, int mini, int maxi, int base)
343 beg = argvalstr(index);
344 val = strtol(beg, &end, base);
345 if (*end || end == beg) {
346 ERROR("option [--%s] requires a valid integer (found %s)",
347 cliOptions[index].name, beg);
350 if (val < (long int)mini || val > (long int)maxi) {
351 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
352 cliOptions[index].name, mini, val, maxi);
358 static int argvalintdec(int index, int mini, int maxi)
360 return argvalint(index, mini, maxi, 10);
363 static void noarg(int index)
366 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
371 static void parse_arguments(int argc, char **argv, struct afb_config *config)
373 char *programName = argv[0];
377 struct option *gnuOptions;
379 // ------------------ Process Command Line -----------------------
381 // if no argument print help and return
383 printHelp(stderr, programName);
386 // build GNU getopt info from cliOptions
387 nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
388 gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
389 for (ind = 0; ind < nbcmd; ind++) {
390 gnuOptions[ind].name = cliOptions[ind].name;
391 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
392 gnuOptions[ind].flag = 0;
393 gnuOptions[ind].val = cliOptions[ind].val;
396 // get all options from command line
398 getopt_long(argc, argv, "vsp?", gnuOptions, &optionIndex))
406 config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
410 config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
413 case SET_CNTXTIMEOUT:
414 config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
418 config->rootdir = argvalstr(optionIndex);
419 INFO("Forcing Rootdir=%s", config->rootdir);
423 config->roothttp = argvalstr(optionIndex);
424 INFO("Forcing Root HTTP=%s", config->roothttp);
428 config->rootbase = argvalstr(optionIndex);
429 INFO("Forcing Rootbase=%s", config->rootbase);
433 config->rootapi = argvalstr(optionIndex);
434 INFO("Forcing Rootapi=%s", config->rootapi);
438 list_add(&config->aliases, argvalstr(optionIndex));
442 config->token = argvalstr(optionIndex);
446 config->ldpaths = argvalstr(optionIndex);
449 case SET_SESSION_DIR:
450 config->sessiondir = argvalstr(optionIndex);
453 case SET_CACHE_TIMEOUT:
454 config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
458 config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
463 config->background = 0;
468 config->background = 1;
472 config->mode = argvalenum(optionIndex, mode_desc);
476 config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
480 list_add(&config->dbus_clients, argvalstr(optionIndex));
484 list_add(&config->dbus_servers, argvalstr(optionIndex));
488 list_add(&config->ws_clients, argvalstr(optionIndex));
492 list_add(&config->ws_servers, argvalstr(optionIndex));
496 list_add(&config->so_bindings, argvalstr(optionIndex));
500 config->tracereq = argvalenum(optionIndex, tracereq_desc);
503 case DISPLAY_VERSION:
505 printVersion(stdout);
510 printHelp(stdout, programName);
516 config_set_default(config);
519 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
521 struct afb_config *result;
523 result = calloc(1, sizeof *result);
525 parse_arguments(argc, argv, result);