0bf0294d2d7cbd81ff2fbffb28c0f64f9819a942
[src/app-framework-binder.git] / include / local-def.h
1 /*
2    local-def.h -- provide a REST/HTTP interface
3
4    Copyright (C) 2015, Fulup Ar Foll
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21 #ifndef LOCAL_DEF_H
22 #define LOCAL_DEF_H
23
24 #ifndef _GNU_SOURCE
25   #define _GNU_SOURCE
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <termios.h>
35 #include <sys/ioctl.h>
36 #include <sys/signal.h>
37 #include <sys/types.h>
38 #include <time.h>
39 #include <json.h>
40 #include <microhttpd.h>
41 #include <magic.h>
42 #include <setjmp.h>
43 #include <signal.h>
44 #include <uuid/uuid.h>
45
46
47
48
49 #define AJQ_VERSION "0.1"
50
51 /* other definitions --------------------------------------------------- */
52
53 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
54 #define MAGIC_DB "/usr/share/misc/magic.mgc"
55 #define OPA_INDEX "index.html"
56 #define MAX_ALIAS 10           // max number of aliases
57 #define COOKIE_NAME   "afb-session"
58
59
60 #define DEFLT_CNTX_TIMEOUT  3600   // default Client Connection Timeout
61 #define DEFLT_API_TIMEOUT   0      // default Plugin API Timeout [0=NoLimit for Debug Only]
62 #define DEFLT_API_TIMEOUT   0      // default Plugin API Timeout
63 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
64 #define DEFLT_AUTH_TOKEN    NULL   // expect for debug should == NULL
65 #define DEFLT_HTTP_TIMEOUT  15     // Max MibMicroHttp timeout
66 #define AFB_MAX_PLUGINS     20     // Max number of plugins for a given binder
67
68 typedef int BOOL;
69 #ifndef FALSE
70   #define FALSE 0
71 #endif
72 #ifndef TRUE
73   #define TRUE 1
74 #endif
75
76 #define PUBLIC
77 #define STATIC    static
78 #define FAILED    -1
79
80 #define AUDIO_BUFFER "/tmp/buf"
81
82 extern int verbose;  // this is the only global variable
83
84 // Plugin Type
85 typedef enum  {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321,  AFB_PLUGIN_RAW=987123546} AFB_pluginE;
86
87 // prebuild json error are constructed in config.c
88 typedef enum  { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
89
90 extern char *ERROR_LABEL[];
91 #define ERROR_LABEL_DEF {"false", "true", "fatal", "fail", "warning", "empty", "success"}
92
93 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
94 #define JSON_CONTENT  "application/json"
95 #define FORM_CONTENT "multipart/form-data"
96 #define MAX_POST_SIZE  4096   // maximum size for POST data
97 #define CTX_NBCLIENTS   10   // allow a default of 10 authenticated clients
98
99
100 typedef json_object* (*AFB_apiCB)();
101 typedef void (*AFB_freeCtxCB)(void*, void*, char*);
102
103 // Error code are requested through function to manage json usage count
104 typedef struct {
105   int   level;
106   char* label;
107   json_object *json;
108 } AFB_errorT;
109
110 typedef enum  {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
111 typedef enum  {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode;
112
113 // Post Upload File Handle
114 typedef struct {
115    int   fd; 
116    char *path; 
117    int  errcode;
118    json_object* jresp;
119 } AFB_PostCtx;
120
121 typedef  struct {
122     int  len;   // post element size
123     char *data; // post data in raw format
124     AFB_PostType type; // Json type
125 } AFB_PostRequest;
126   
127 // Post handler
128 typedef struct {
129   void*  ctx;               // Application context
130   int    len;               // current len for post
131   int    uid;               // post uid for debug
132   AFB_PostType type;        // JSON or FORM
133   AFB_apiCB  completeCB;    // callback when post is completed
134   char   *privatebuf;       // use internally to keep track or partial buffer
135   struct MHD_PostProcessor *pp; // iterator handle
136 } AFB_PostHandle;
137
138 typedef struct {
139     enum MHD_ValueKind kind; // kind type of the value
140     const char *key;         // key 0-terminated key for the value
141     const char *filename;    // filename of the uploaded file, NULL if not known
142     const char *mimetype;    // content_type mime-type of the data, NULL if not known
143     const char *encoding;    // transfer_encoding encoding of the data, NULL if not known
144     const char *data;        // data pointer to size bytes of data at the specified offset
145     uint64_t   offset;       // offset of data in the overall value
146     size_t     len;          // number of bytes in data available
147 } AFB_PostItem;
148
149 typedef struct {
150   char  path[512];
151   int   fd;
152 } AFB_staticfile;
153
154 typedef struct {
155   char  *url;
156   char  *path;
157   size_t len;
158 } AFB_aliasdir;
159
160 // Command line structure hold cli --command + help text
161 typedef struct {
162   int  val;        // command number within application
163   int  has_arg;    // command number within application
164   char *name;      // command as used in --xxxx cli
165   char *help;      // help text
166 } AFB_options;
167
168 // main config structure
169 typedef struct {
170   char *logname;           // logfile path for info & error log
171   char *console;           // console device name (can be a file or a tty)
172   int  localhostOnly;
173   int   httpdPort;
174   char *ldpaths;           // list of plugins directories
175   char *rootdir;           // base dir for httpd file download
176   char *rootbase;          // Angular HTML5 base URL
177   char *rootapi;           // Base URL for REST APIs
178   char *pidfile;           // where to store pid when running background
179   char *sessiondir;        // where to store mixer session files
180   char *configfile;        // where to store configuration on gateway exit
181   char *setuid;            // downgrade uid to username
182   char *token;             // initial authentication token [default NULL no session]
183   int  cacheTimeout;
184   int  apiTimeout;
185   int  cntxTimeout;        // Client Session Context timeout
186   int  pluginCount;        // loaded plugins count
187   AFB_Mode mode;           // mode of listening
188   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
189 } AFB_config;
190
191 typedef struct {
192   int  len;        // command number within application
193   json_object *jtype;
194 } AFB_privateApi;
195
196
197 typedef struct {
198      char    *msg;
199      size_t  len;
200 } AFB_redirect_msg;
201
202 // Enum for Session/Token/Authentication middleware
203 typedef enum  {AFB_SESSION_NONE, AFB_SESSION_CREATE, AFB_SESSION_CLOSE, AFB_SESSION_RENEW, AFB_SESSION_CHECK} AFB_sessionE;
204
205 // API definition
206 typedef struct {
207   char *name;
208   AFB_sessionE session;
209   AFB_apiCB callback;
210   char *info;
211   AFB_privateApi *privateapi;
212 } AFB_restapi;
213
214 // Plugin definition
215 typedef struct {
216   AFB_pluginE type;  
217   char *info;
218   char *prefix;
219   size_t prefixlen;
220   json_object *jtype;
221   AFB_restapi *apis;
222   void *handle;
223   int  ctxCount;
224   AFB_freeCtxCB freeCtxCB;  // callback to free application context [null for standard free]
225 } AFB_plugin;
226
227
228 // User Client Session Context
229 typedef struct {
230   char uuid[37];        // long term authentication of remote client
231   char token[37];       // short term authentication of remote client
232   time_t timeStamp;     // last time token was refresh
233   int   restfull;       // client does not use cookie
234   void **contexts;      // application specific context [one per plugin]]
235   AFB_plugin **plugins; // we need plugins reference to cleanup session outside of call context
236 } AFB_clientCtx;
237
238 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
239 typedef struct {
240   const char *uuid;
241   const char *url;
242   char *prefix;              // plugin convivial name
243   char *api;
244   AFB_PostRequest *post;
245   json_object *jresp;
246   void *context;             // Hold Client Context when using session
247   void *handle;              // provide callback and easy access to plugin
248   int  restfull;             // request is resfull [uuid token provided]
249   int  errcode;              // http error code
250   sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
251   AFB_config *config;         // plugin may need access to config
252   struct MHD_Connection *connection;
253   AFB_plugin **plugins;
254 } AFB_request;
255
256
257 typedef struct {
258   AFB_config  *config;   // pointer to current config
259   // List of commands to execute
260   int  killPrevious;
261   int  background;        // run in backround mode
262   int  foreground;        // run in forground mode
263   int  configsave;        // Save config on disk on start
264   char *cacheTimeout;     // http require timeout to be a string
265   void *httpd;            // anonymous structure for httpd handler
266   int  fakemod;           // respond to GET/POST request without interacting with sndboard
267   int  forceexit;         // when autoconfig from script force exit before starting server
268   int  readyfd;           // a #fd to signal when ready to serve
269   AFB_plugin **plugins;   // pointer to REST/API plugins 
270   magic_t  magic;         // Mime type file magic lib
271   sigjmp_buf restartCkpt; // context save for restart set/longjmp
272 } AFB_session;
273
274
275
276 #include "proto-def.h"
277
278 #endif /* LOCAL_DEF_H */