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 SHORTOPTS "vqhV"
93 // Command line structure hold cli --command + help text
95 int val; // command number within application
96 int has_arg; // command number within application
97 char *name; // command as used in --xxxx cli
98 char *help; // help text
102 static AFB_options cliOptions[] = {
104 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
105 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
107 {SET_FORGROUND, 0, "foreground", "Get all in foreground mode"},
108 {SET_BACKGROUND, 0, "daemon", "Get all in background mode"},
110 {SET_TCP_PORT, 1, "port", "HTTP listening TCP port [default 1234]"},
111 {SET_ROOT_DIR, 1, "rootdir", "Root Directory [default $HOME/.AFB]"},
112 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default rootdir]"},
113 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
114 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
115 {SET_ALIAS, 1, "alias", "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
117 {SET_APITIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default 10]"},
118 {SET_CNTXTIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
119 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default 3600]"},
121 {SET_SESSION_DIR, 1, "sessiondir", "Sessions file path [default rootdir/sessions]"},
123 {SET_LDPATH, 1, "ldpaths", "Load bindingss from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
124 {SET_AUTH_TOKEN, 1, "token", "Initial Secret [default=no-session, --token= for session without authentication]"},
126 {DISPLAY_VERSION, 0, "version", "Display version and copyright"},
127 {DISPLAY_HELP, 0, "help", "Display this help"},
129 {SET_MODE, 1, "mode", "Set the mode: either local, remote or global"},
130 {SET_READYFD, 1, "readyfd", "Set the #fd to signal when ready"},
132 {DBUS_CLIENT, 1, "dbus-client", "Bind to an afb service through dbus"},
133 {DBUS_SERVICE, 1, "dbus-server", "Provides an afb service through dbus"},
134 {WS_CLIENT, 1, "ws-client", "Bind to an afb service through websocket"},
135 {WS_SERVICE, 1, "ws-server", "Provides an afb service through websockets"},
136 {SO_BINDING, 1, "binding", "Load the binding of path"},
138 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default 10]"},
140 {SET_TRACEREQ, 1, "tracereq", "Log the requests: no, common, extra, all"},
153 static struct enumdesc tracereq_desc[] = {
155 { "common", afb_hook_flags_req_common },
156 { "extra", afb_hook_flags_req_extra },
157 { "all", afb_hook_flags_req_all },
161 static struct enumdesc mode_desc[] = {
162 { "local", AFB_MODE_LOCAL },
163 { "remote", AFB_MODE_REMOTE },
164 { "global", AFB_MODE_GLOBAL },
168 /*----------------------------------------------------------
170 | print version and copyright
171 +--------------------------------------------------------- */
172 static void printVersion(FILE * file)
174 fprintf(file, "\n----------------------------------------- \n");
175 fprintf(file, " AFB [Application Framework Binder] version=%s |\n",
177 fprintf(file, " \n");
179 " Copyright (C) 2015, 2016, 2017 \"IoT.bzh\" [fulup -at- iot.bzh]\n");
180 fprintf(file, " AFB comes with ABSOLUTELY NO WARRANTY.\n");
181 fprintf(file, " Licence Apache 2\n\n");
184 /*----------------------------------------------------------
186 | print information from long option array
187 +--------------------------------------------------------- */
189 static void printHelp(FILE * file, const char *name)
194 fprintf(file, "%s:\nallowed options\n", name);
195 for (ind = 0; cliOptions[ind].name != NULL; ind++) {
196 strcpy(command, cliOptions[ind].name);
197 if (cliOptions[ind].has_arg)
198 strcat(command, "=xxxx");
199 fprintf(file, " --%-15s %s\n", command, cliOptions[ind].help);
202 "Example:\n %s --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
206 /*----------------------------------------------------------
207 | adds a string to the list
208 +--------------------------------------------------------- */
209 static void list_add(struct afb_config_list **head, char *value)
211 struct afb_config_list *item;
217 while (item != NULL) {
225 item = malloc(sizeof *item);
227 ERROR("out of memory");
239 /*---------------------------------------------------------
240 | helpers for argument scanning
241 +--------------------------------------------------------- */
243 static char *argvalstr(int index)
246 ERROR("option [--%s] needs a value i.e. --%s=xxx",
247 cliOptions[index].name, cliOptions[index].name);
253 static int argvalenum(int index, struct enumdesc *desc)
257 char *list, *name = argvalstr(index);
260 while(desc[i].name && strcmp(desc[i].name, name))
266 len += strlen(desc[i++].name);
267 list = malloc(len + i + i);
269 ERROR("option [--%s] bad value (found %s)",
270 cliOptions[index].name, name);
273 strcpy(list, desc[i].name ? : "");
274 while(desc[++i].name)
275 strcat(strcat(list, ", "), desc[i].name);
276 ERROR("option [--%s] bad value, only accepts values %s (found %s)",
277 cliOptions[index].name, list, name);
282 return desc[i].value;
285 static int argvalint(int index, int mini, int maxi, int base)
289 beg = argvalstr(index);
290 val = strtol(beg, &end, base);
291 if (*end || end == beg) {
292 ERROR("option [--%s] requires a valid integer (found %s)",
293 cliOptions[index].name, beg);
296 if (val < (long int)mini || val > (long int)maxi) {
297 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
298 cliOptions[index].name, mini, val, maxi);
304 static int argvalintdec(int index, int mini, int maxi)
306 return argvalint(index, mini, maxi, 10);
309 static void noarg(int index)
312 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
317 /*---------------------------------------------------------
318 | Parse option and launch action
319 +--------------------------------------------------------- */
321 static void parse_arguments(int argc, char **argv, struct afb_config *config)
323 char *programName = argv[0];
327 struct option *gnuOptions;
329 // ------------------ Process Command Line -----------------------
331 // build GNU getopt info from cliOptions
332 nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
333 gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
334 for (ind = 0; ind < nbcmd; ind++) {
335 gnuOptions[ind].name = cliOptions[ind].name;
336 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
337 gnuOptions[ind].flag = 0;
338 gnuOptions[ind].val = cliOptions[ind].val;
341 // get all options from command line
342 while ((optc = getopt_long(argc, argv, SHORTOPTS, gnuOptions, &optionIndex)) != EOF) {
353 config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
357 config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
360 case SET_CNTXTIMEOUT:
361 config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
365 config->rootdir = argvalstr(optionIndex);
366 INFO("Forcing Rootdir=%s", config->rootdir);
370 config->roothttp = argvalstr(optionIndex);
371 INFO("Forcing Root HTTP=%s", config->roothttp);
375 config->rootbase = argvalstr(optionIndex);
376 INFO("Forcing Rootbase=%s", config->rootbase);
380 config->rootapi = argvalstr(optionIndex);
381 INFO("Forcing Rootapi=%s", config->rootapi);
385 list_add(&config->aliases, argvalstr(optionIndex));
389 config->token = argvalstr(optionIndex);
393 list_add(&config->ldpaths, argvalstr(optionIndex));
396 case SET_SESSION_DIR:
397 config->sessiondir = argvalstr(optionIndex);
400 case SET_CACHE_TIMEOUT:
401 config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
405 config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
410 config->background = 0;
415 config->background = 1;
419 config->mode = argvalenum(optionIndex, mode_desc);
423 config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
427 list_add(&config->dbus_clients, argvalstr(optionIndex));
431 list_add(&config->dbus_servers, argvalstr(optionIndex));
435 list_add(&config->ws_clients, argvalstr(optionIndex));
439 list_add(&config->ws_servers, argvalstr(optionIndex));
443 list_add(&config->so_bindings, argvalstr(optionIndex));
447 config->tracereq = argvalenum(optionIndex, tracereq_desc);
450 case DISPLAY_VERSION:
452 printVersion(stdout);
456 printHelp(stdout, programName);
466 // load config from disk and merge with CLI option
467 static void config_set_default(struct afb_config *config)
470 if (config->httpdPort == 0)
471 config->httpdPort = 1234;
473 // default binding API timeout
474 if (config->apiTimeout == 0)
475 config->apiTimeout = DEFLT_API_TIMEOUT;
477 // default AUTH_TOKEN
478 if (config->token == NULL)
479 config->token = DEFLT_AUTH_TOKEN;
481 // cache timeout default one hour
482 if (config->cacheTimeout == 0)
483 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
485 // cache timeout default one hour
486 if (config->cntxTimeout == 0)
487 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
489 // max count of sessions
490 if (config->nbSessionMax == 0)
491 config->nbSessionMax = CTX_NBCLIENTS;
493 if (config->rootdir == NULL) {
494 config->rootdir = getenv("AFBDIR");
495 if (config->rootdir == NULL) {
496 config->rootdir = malloc(512);
497 strncpy(config->rootdir, getenv("HOME"), 512);
498 strncat(config->rootdir, "/.AFB", 512);
501 // if no Angular/HTML5 rootbase let's try '/' as default
502 if (config->rootbase == NULL)
503 config->rootbase = "/opa";
505 if (config->rootapi == NULL)
506 config->rootapi = "/api";
508 if (config->ldpaths == NULL)
509 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
511 // if no session dir create a default path from rootdir
512 if (config->sessiondir == NULL) {
513 config->sessiondir = malloc(512);
514 strncpy(config->sessiondir, config->rootdir, 512);
515 strncat(config->sessiondir, "/sessions", 512);
517 // if no config dir create a default path from sessiondir
518 if (config->console == NULL) {
519 config->console = malloc(512);
520 strncpy(config->console, config->sessiondir, 512);
521 strncat(config->console, "/AFB-console.out", 512);
525 void afb_config_dump(struct afb_config *config)
527 struct afb_config_list *l;
530 #define NN(x) (x)?:""
531 #define P(...) fprintf(stderr, __VA_ARGS__)
532 #define PF(x) P("-- %15s: ", #x)
534 #define S(x) PF(x);P("%s",NN(config->x));PE;
535 #define D(x) PF(x);P("%d",config->x);PE;
536 #define H(x) PF(x);P("%x",config->x);PE;
537 #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;
538 #define E(x,d) for(e=d;e->name&&e->value!=config->x;e++);PF(x);if(e->name)P("%s",e->name);else P("%d",config->x);PE;
540 P("-- BEGIN OF CONFIG --\n");
565 E(tracereq,tracereq_desc)
566 P("-- END OF CONFIG --\n");
579 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
581 struct afb_config *result;
583 result = calloc(1, sizeof *result);
585 parse_arguments(argc, argv, result);
586 config_set_default(result);
588 afb_config_dump(result);