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 --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
203 /*----------------------------------------------------------
204 | adds a string to the list
205 +--------------------------------------------------------- */
206 static void list_add(struct afb_config_list **head, char *value)
208 struct afb_config_list *item;
214 while (item != NULL) {
222 item = malloc(sizeof *item);
224 ERROR("out of memory");
236 /*---------------------------------------------------------
237 | helpers for argument scanning
238 +--------------------------------------------------------- */
240 static char *argvalstr(int index)
243 ERROR("option [--%s] needs a value i.e. --%s=xxx",
244 cliOptions[index].name, cliOptions[index].name);
250 static int argvalenum(int index, struct enumdesc *desc)
254 char *list, *name = argvalstr(index);
257 while(desc[i].name && strcmp(desc[i].name, name))
263 len += strlen(desc[i++].name);
264 list = malloc(len + i + i);
266 ERROR("option [--%s] bad value (found %s)",
267 cliOptions[index].name, name);
270 strcpy(list, desc[i].name ? : "");
271 while(desc[++i].name)
272 strcat(strcat(list, ", "), desc[i].name);
273 ERROR("option [--%s] bad value, only accepts values %s (found %s)",
274 cliOptions[index].name, list, name);
279 return desc[i].value;
282 static int argvalint(int index, int mini, int maxi, int base)
286 beg = argvalstr(index);
287 val = strtol(beg, &end, base);
288 if (*end || end == beg) {
289 ERROR("option [--%s] requires a valid integer (found %s)",
290 cliOptions[index].name, beg);
293 if (val < (long int)mini || val > (long int)maxi) {
294 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
295 cliOptions[index].name, mini, val, maxi);
301 static int argvalintdec(int index, int mini, int maxi)
303 return argvalint(index, mini, maxi, 10);
306 static void noarg(int index)
309 ERROR("option [--%s] need no value (found %s)", cliOptions[index].name, optarg);
314 /*---------------------------------------------------------
315 | Parse option and launch action
316 +--------------------------------------------------------- */
318 static void parse_arguments(int argc, char **argv, struct afb_config *config)
320 char *programName = argv[0];
324 struct option *gnuOptions;
326 // ------------------ Process Command Line -----------------------
328 // if no argument print help and return
330 printHelp(stderr, programName);
333 // build GNU getopt info from cliOptions
334 nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
335 gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
336 for (ind = 0; ind < nbcmd; ind++) {
337 gnuOptions[ind].name = cliOptions[ind].name;
338 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
339 gnuOptions[ind].flag = 0;
340 gnuOptions[ind].val = cliOptions[ind].val;
343 // get all options from command line
344 while ((optc = getopt_long(argc, argv, "", gnuOptions, &optionIndex)) != EOF) {
351 config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
355 config->apiTimeout = argvalintdec(optionIndex, 0, INT_MAX);
358 case SET_CNTXTIMEOUT:
359 config->cntxTimeout = argvalintdec(optionIndex, 0, INT_MAX);
363 config->rootdir = argvalstr(optionIndex);
364 INFO("Forcing Rootdir=%s", config->rootdir);
368 config->roothttp = argvalstr(optionIndex);
369 INFO("Forcing Root HTTP=%s", config->roothttp);
373 config->rootbase = argvalstr(optionIndex);
374 INFO("Forcing Rootbase=%s", config->rootbase);
378 config->rootapi = argvalstr(optionIndex);
379 INFO("Forcing Rootapi=%s", config->rootapi);
383 list_add(&config->aliases, argvalstr(optionIndex));
387 config->token = argvalstr(optionIndex);
391 list_add(&config->ldpaths, argvalstr(optionIndex));
394 case SET_SESSION_DIR:
395 config->sessiondir = argvalstr(optionIndex);
398 case SET_CACHE_TIMEOUT:
399 config->cacheTimeout = argvalintdec(optionIndex, 0, INT_MAX);
403 config->nbSessionMax = argvalintdec(optionIndex, 1, INT_MAX);
408 config->background = 0;
413 config->background = 1;
417 config->mode = argvalenum(optionIndex, mode_desc);
421 config->readyfd = argvalintdec(optionIndex, 0, INT_MAX);
425 list_add(&config->dbus_clients, argvalstr(optionIndex));
429 list_add(&config->dbus_servers, argvalstr(optionIndex));
433 list_add(&config->ws_clients, argvalstr(optionIndex));
437 list_add(&config->ws_servers, argvalstr(optionIndex));
441 list_add(&config->so_bindings, argvalstr(optionIndex));
445 config->tracereq = argvalenum(optionIndex, tracereq_desc);
448 case DISPLAY_VERSION:
450 printVersion(stdout);
454 printHelp(stdout, programName);
464 // load config from disk and merge with CLI option
465 static void config_set_default(struct afb_config *config)
468 if (config->httpdPort == 0)
469 config->httpdPort = 1234;
471 // default binding API timeout
472 if (config->apiTimeout == 0)
473 config->apiTimeout = DEFLT_API_TIMEOUT;
475 // default AUTH_TOKEN
476 if (config->token == NULL)
477 config->token = DEFLT_AUTH_TOKEN;
479 // cache timeout default one hour
480 if (config->cacheTimeout == 0)
481 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
483 // cache timeout default one hour
484 if (config->cntxTimeout == 0)
485 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
487 // max count of sessions
488 if (config->nbSessionMax == 0)
489 config->nbSessionMax = CTX_NBCLIENTS;
491 if (config->rootdir == NULL) {
492 config->rootdir = getenv("AFBDIR");
493 if (config->rootdir == NULL) {
494 config->rootdir = malloc(512);
495 strncpy(config->rootdir, getenv("HOME"), 512);
496 strncat(config->rootdir, "/.AFB", 512);
499 // if no Angular/HTML5 rootbase let's try '/' as default
500 if (config->rootbase == NULL)
501 config->rootbase = "/opa";
503 if (config->rootapi == NULL)
504 config->rootapi = "/api";
506 if (config->ldpaths == NULL)
507 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
509 // if no session dir create a default path from rootdir
510 if (config->sessiondir == NULL) {
511 config->sessiondir = malloc(512);
512 strncpy(config->sessiondir, config->rootdir, 512);
513 strncat(config->sessiondir, "/sessions", 512);
515 // if no config dir create a default path from sessiondir
516 if (config->console == NULL) {
517 config->console = malloc(512);
518 strncpy(config->console, config->sessiondir, 512);
519 strncat(config->console, "/AFB-console.out", 512);
523 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
525 struct afb_config *result;
527 result = calloc(1, sizeof *result);
529 parse_arguments(argc, argv, result);
530 config_set_default(result);