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
53 #define SET_VERBOSE 'v'
54 #define SET_BACKGROUND 2
55 #define SET_FORGROUND 3
58 #define SET_TCP_PORT 5
59 #define SET_ROOT_DIR 6
60 #define SET_ROOT_BASE 7
61 #define SET_ROOT_API 8
64 #define SET_CACHE_TIMEOUT 10
65 #define SET_SESSION_DIR 11
67 #define SET_AUTH_TOKEN 12
69 #define SET_APITIMEOUT 14
70 #define SET_CNTXTIMEOUT 15
72 #define DISPLAY_VERSION 'V'
73 #define DISPLAY_HELP 'h'
76 #define SET_READYFD 19
78 #define DBUS_CLIENT 20
79 #define DBUS_SERVICE 21
82 #define SET_SESSIONMAX 23
87 #define SET_ROOT_HTTP 26
89 #define SET_TRACEREQ 27
91 #define SET_NO_HTTPD 28
95 #define SHORTOPTS "vqhVe"
97 // Command line structure hold cli --command + help text
99 int val; // command number within application
100 int has_arg; // command number within application
101 char *name; // command as used in --xxxx cli
102 char *help; // help text
106 static AFB_options cliOptions[] = {
108 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
109 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
111 {SET_FORGROUND, 0, "foreground", "Get all in foreground mode"},
112 {SET_BACKGROUND, 0, "daemon", "Get all in background mode"},
114 {SET_TCP_PORT, 1, "port", "HTTP listening TCP port [default 1234]"},
115 {SET_ROOT_DIR, 1, "rootdir", "Root Directory [default $HOME/.AFB]"},
116 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default rootdir]"},
117 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
118 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
119 {SET_ALIAS, 1, "alias", "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
121 {SET_APITIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default 10]"},
122 {SET_CNTXTIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
123 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default 3600]"},
125 {SET_SESSION_DIR, 1, "sessiondir", "Sessions file path [default rootdir/sessions]"},
127 {SET_LDPATH, 1, "ldpaths", "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
128 {SET_AUTH_TOKEN, 1, "token", "Initial Secret [default=no-session, --token= for session without authentication]"},
130 {DISPLAY_VERSION, 0, "version", "Display version and copyright"},
131 {DISPLAY_HELP, 0, "help", "Display this help"},
133 {SET_MODE, 1, "mode", "Set the mode: either local, remote or global"},
134 {SET_READYFD, 1, "readyfd", "Set the #fd to signal when ready"},
136 {DBUS_CLIENT, 1, "dbus-client", "Bind to an afb service through dbus"},
137 {DBUS_SERVICE, 1, "dbus-server", "Provides an afb service through dbus"},
138 {WS_CLIENT, 1, "ws-client", "Bind to an afb service through websocket"},
139 {WS_SERVICE, 1, "ws-server", "Provides an afb service through websockets"},
140 {SO_BINDING, 1, "binding", "Load the binding of path"},
142 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default 10]"},
144 {SET_TRACEREQ, 1, "tracereq", "Log the requests: no, common, extra, all"},
146 {SET_NO_HTTPD, 0, "no-httpd", "Forbids HTTP service"},
147 {SET_EXEC, 0, "exec", "Execute the remaining arguments"},
160 static struct enumdesc tracereq_desc[] = {
162 { "common", afb_hook_flags_req_common },
163 { "extra", afb_hook_flags_req_extra },
164 { "all", afb_hook_flags_req_all },
168 static struct enumdesc mode_desc[] = {
169 { "local", AFB_MODE_LOCAL },
170 { "remote", AFB_MODE_REMOTE },
171 { "global", AFB_MODE_GLOBAL },
175 /*----------------------------------------------------------
177 | print version and copyright
178 +--------------------------------------------------------- */
179 static void printVersion(FILE * file)
181 fprintf(file, "\n----------------------------------------- \n");
182 fprintf(file, " AFB [Application Framework Binder] version=%s |\n",
184 fprintf(file, " \n");
186 " Copyright (C) 2015, 2016, 2017 \"IoT.bzh\" [fulup -at- iot.bzh]\n");
187 fprintf(file, " AFB comes with ABSOLUTELY NO WARRANTY.\n");
188 fprintf(file, " Licence Apache 2\n\n");
191 /*----------------------------------------------------------
193 | print information from long option array
194 +--------------------------------------------------------- */
196 static void printHelp(FILE * file, const char *name)
201 fprintf(file, "%s:\nallowed options\n", name);
202 for (ind = 0; cliOptions[ind].name != NULL; ind++) {
203 strcpy(command, cliOptions[ind].name);
204 if (cliOptions[ind].has_arg)
205 strcat(command, "=xxxx");
206 fprintf(file, " --%-15s %s\n", command, cliOptions[ind].help);
209 "Example:\n %s --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
213 /*----------------------------------------------------------
214 | adds a string to the list
215 +--------------------------------------------------------- */
216 static void list_add(struct afb_config_list **head, char *value)
218 struct afb_config_list *item;
224 while (item != NULL) {
232 item = malloc(sizeof *item);
234 ERROR("out of memory");
246 /*---------------------------------------------------------
247 | helpers for argument scanning
248 +--------------------------------------------------------- */
250 static char *argvalstr(int index)
253 ERROR("option [--%s] needs a value i.e. --%s=xxx",
254 cliOptions[index].name, cliOptions[index].name);
260 static int argvalenum(int index, struct enumdesc *desc)
264 char *list, *name = argvalstr(index);
267 while(desc[i].name && strcmp(desc[i].name, name))
273 len += strlen(desc[i++].name);
274 list = malloc(len + i + i);
276 ERROR("option [--%s] bad value (found %s)",
277 cliOptions[index].name, name);
280 strcpy(list, desc[i].name ? : "");
281 while(desc[++i].name)
282 strcat(strcat(list, ", "), desc[i].name);
283 ERROR("option [--%s] bad value, only accepts values %s (found %s)",
284 cliOptions[index].name, list, name);
289 return desc[i].value;
292 static int argvalint(int index, int mini, int maxi, int base)
296 beg = argvalstr(index);
297 val = strtol(beg, &end, base);
298 if (*end || end == beg) {
299 ERROR("option [--%s] requires a valid integer (found %s)",
300 cliOptions[index].name, beg);
303 if (val < (long int)mini || val > (long int)maxi) {
304 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
305 cliOptions[index].name, mini, val, maxi);
311 static int argvalintdec(int index, int mini, int maxi)
313 return argvalint(index, mini, maxi, 10);
316 static void noarg(int index)
319 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
324 /*---------------------------------------------------------
325 | Parse option and launch action
326 +--------------------------------------------------------- */
328 static void parse_arguments(int argc, char **argv, struct afb_config *config)
330 char *programName = argv[0];
334 struct option *gnuOptions;
336 // ------------------ Process Command Line -----------------------
338 // build GNU getopt info from cliOptions
339 nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
340 gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
341 for (ind = 0; ind < nbcmd; ind++) {
342 gnuOptions[ind].name = cliOptions[ind].name;
343 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
344 gnuOptions[ind].flag = 0;
345 gnuOptions[ind].val = cliOptions[ind].val;
348 // get all options from command line
349 while ((optc = getopt_long(argc, argv, SHORTOPTS, gnuOptions, &optionIndex)) != EOF) {
360 config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
364 config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
367 case SET_CNTXTIMEOUT:
368 config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
372 config->rootdir = argvalstr(optionIndex);
373 INFO("Forcing Rootdir=%s", config->rootdir);
377 config->roothttp = argvalstr(optionIndex);
378 INFO("Forcing Root HTTP=%s", config->roothttp);
382 config->rootbase = argvalstr(optionIndex);
383 INFO("Forcing Rootbase=%s", config->rootbase);
387 config->rootapi = argvalstr(optionIndex);
388 INFO("Forcing Rootapi=%s", config->rootapi);
392 list_add(&config->aliases, argvalstr(optionIndex));
396 config->token = argvalstr(optionIndex);
400 list_add(&config->ldpaths, argvalstr(optionIndex));
403 case SET_SESSION_DIR:
404 config->sessiondir = argvalstr(optionIndex);
407 case SET_CACHE_TIMEOUT:
408 config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
412 config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
417 config->background = 0;
422 config->background = 1;
426 config->mode = argvalenum(optionIndex, mode_desc);
430 config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
434 list_add(&config->dbus_clients, argvalstr(optionIndex));
438 list_add(&config->dbus_servers, argvalstr(optionIndex));
442 list_add(&config->ws_clients, argvalstr(optionIndex));
446 list_add(&config->ws_servers, argvalstr(optionIndex));
450 list_add(&config->so_bindings, argvalstr(optionIndex));
454 config->tracereq = argvalenum(optionIndex, tracereq_desc);
463 config->exec = &argv[optind];
467 case DISPLAY_VERSION:
469 printVersion(stdout);
473 printHelp(stdout, programName);
483 // load config from disk and merge with CLI option
484 static void config_set_default(struct afb_config *config)
487 if (config->httpdPort == 0)
488 config->httpdPort = 1234;
490 // default binding API timeout
491 if (config->apiTimeout == 0)
492 config->apiTimeout = DEFLT_API_TIMEOUT;
494 // default AUTH_TOKEN
495 if (config->token == NULL)
496 config->token = DEFLT_AUTH_TOKEN;
498 // cache timeout default one hour
499 if (config->cacheTimeout == 0)
500 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
502 // cache timeout default one hour
503 if (config->cntxTimeout == 0)
504 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
506 // max count of sessions
507 if (config->nbSessionMax == 0)
508 config->nbSessionMax = CTX_NBCLIENTS;
510 if (config->rootdir == NULL) {
511 config->rootdir = getenv("AFBDIR");
512 if (config->rootdir == NULL) {
513 config->rootdir = malloc(512);
514 strncpy(config->rootdir, getenv("HOME"), 512);
515 strncat(config->rootdir, "/.AFB", 512);
518 // if no Angular/HTML5 rootbase let's try '/' as default
519 if (config->rootbase == NULL)
520 config->rootbase = "/opa";
522 if (config->rootapi == NULL)
523 config->rootapi = "/api";
525 if (config->ldpaths == NULL)
526 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
528 // if no session dir create a default path from rootdir
529 if (config->sessiondir == NULL) {
530 config->sessiondir = malloc(512);
531 strncpy(config->sessiondir, config->rootdir, 512);
532 strncat(config->sessiondir, "/sessions", 512);
534 // if no config dir create a default path from sessiondir
535 if (config->console == NULL) {
536 config->console = malloc(512);
537 strncpy(config->console, config->sessiondir, 512);
538 strncat(config->console, "/AFB-console.out", 512);
542 void afb_config_dump(struct afb_config *config)
544 struct afb_config_list *l;
548 #define NN(x) (x)?:""
549 #define P(...) fprintf(stderr, __VA_ARGS__)
550 #define PF(x) P("-- %15s: ", #x)
552 #define S(x) PF(x);P("%s",NN(config->x));PE;
553 #define D(x) PF(x);P("%d",config->x);PE;
554 #define H(x) PF(x);P("%x",config->x);PE;
555 #define B(x) PF(x);P("%s",config->x?"yes":"no");PE;
556 #define L(x) PF(x);l=config->x;if(l){P("%s\n",NN(l->value));for(l=l->next;l;l=l->next)P("-- %15s %s\n","",NN(l->value));}else PE;
557 #define E(x,d) for(e=d;e->name&&e->value!=config->x;e++);if(e->name){PF(x);P("%s",e->name);PE;}else{D(x);}
558 #define V(x) P("-- %15s:", #x);for(v=config->x;v&&*v;v++)P(" %s",*v); PE;
560 P("---BEGIN-OF-CONFIG---\n");
587 E(tracereq,tracereq_desc)
589 P("---END-OF-CONFIG---\n");
604 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
606 struct afb_config *result;
608 result = calloc(1, sizeof *result);
610 parse_arguments(argc, argv, result);
611 config_set_default(result);
613 afb_config_dump(result);