several improvements
[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 /* other definitions --------------------------------------------------- */
49
50 // Note: because of a bug in libmagic MAGIC_DB NULL should not be used for default
51 #define OPA_INDEX "index.html"
52 #define MAX_ALIAS 10           // max number of aliases
53 #define COOKIE_NAME   "afb-session"
54
55
56 #define DEFLT_CNTX_TIMEOUT  3600   // default Client Connection Timeout
57 #define DEFLT_API_TIMEOUT   0      // default Plugin API Timeout [0=NoLimit for Debug Only]
58 #define DEFLT_API_TIMEOUT   0      // default Plugin API Timeout
59 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
60 #define DEFLT_AUTH_TOKEN    NULL   // expect for debug should == NULL
61 #define DEFLT_HTTP_TIMEOUT  15     // Max MibMicroHttp timeout
62 #define AFB_MAX_PLUGINS     20     // Max number of plugins for a given binder
63
64 typedef int BOOL;
65 #ifndef FALSE
66   #define FALSE 0
67 #endif
68 #ifndef TRUE
69   #define TRUE 1
70 #endif
71
72 #define PUBLIC
73 #define STATIC    static
74 #define FAILED    -1
75
76 #define AUDIO_BUFFER "/tmp/buf"
77
78 extern int verbose;  // this is the only global variable
79
80 // Plugin Type
81 typedef enum  {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321,  AFB_PLUGIN_RAW=987123546} AFB_pluginE;
82
83 // prebuild json error are constructed in config.c
84 typedef enum  { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error;
85
86 #define BANNER "<html><head><title>Application Framework Binder</title></head><body>Application Framework </body></html>"
87 #define JSON_CONTENT  "application/json"
88 #define FORM_CONTENT "multipart/form-data"
89 #define MAX_POST_SIZE  4096   // maximum size for POST data
90 #define CTX_NBCLIENTS   10   // allow a default of 10 authenticated clients
91
92
93 typedef json_object* (*AFB_apiCB)();
94 typedef void (*AFB_freeCtxCB)(void*, void*, char*);
95
96 typedef enum  {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType;
97 typedef enum  {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode;
98
99 // Post Upload File Handle
100 typedef struct {
101    int   fd; 
102    char *path; 
103    int  errcode;
104    json_object* jresp;
105 } AFB_PostCtx;
106
107 typedef  struct {
108     int  len;   // post element size
109     char *data; // post data in raw format
110     AFB_PostType type; // Json type
111 } AFB_PostRequest;
112   
113 // Post handler
114 typedef struct {
115   void*  ctx;               // Application context
116   int    len;               // current len for post
117   int    uid;               // post uid for debug
118   AFB_PostType type;        // JSON or FORM
119   AFB_apiCB  completeCB;    // callback when post is completed
120   char   *privatebuf;       // use internally to keep track or partial buffer
121   struct MHD_PostProcessor *pp; // iterator handle
122 } AFB_PostHandle;
123
124 typedef struct {
125     enum MHD_ValueKind kind; // kind type of the value
126     const char *key;         // key 0-terminated key for the value
127     const char *filename;    // filename of the uploaded file, NULL if not known
128     const char *mimetype;    // content_type mime-type of the data, NULL if not known
129     const char *encoding;    // transfer_encoding encoding of the data, NULL if not known
130     const char *data;        // data pointer to size bytes of data at the specified offset
131     uint64_t   offset;       // offset of data in the overall value
132     size_t     len;          // number of bytes in data available
133 } AFB_PostItem;
134
135 typedef struct {
136   char  path[512];
137   int   fd;
138 } AFB_staticfile;
139
140 typedef struct {
141   char  *url;
142   char  *path;
143   size_t len;
144 } AFB_aliasdir;
145
146 // main config structure
147 typedef struct {
148   char *logname;           // logfile path for info & error log
149   char *console;           // console device name (can be a file or a tty)
150   int  localhostOnly;
151   int   httpdPort;
152   char *ldpaths;           // list of plugins directories
153   char *rootdir;           // base dir for httpd file download
154   char *rootbase;          // Angular HTML5 base URL
155   char *rootapi;           // Base URL for REST APIs
156   char *sessiondir;        // where to store mixer session files
157   char *token;             // initial authentication token [default NULL no session]
158   int  cacheTimeout;
159   int  apiTimeout;
160   int  cntxTimeout;        // Client Session Context timeout
161   int  pluginCount;        // loaded plugins count
162   AFB_Mode mode;           // mode of listening
163   AFB_aliasdir *aliasdir;  // alias mapping for icons,apps,...
164 } AFB_config;
165
166 typedef struct {
167      char    *msg;
168      size_t  len;
169 } AFB_redirect_msg;
170
171 // Enum for Session/Token/Authentication middleware
172 typedef enum  {AFB_SESSION_NONE, AFB_SESSION_CREATE, AFB_SESSION_CLOSE, AFB_SESSION_RENEW, AFB_SESSION_CHECK} AFB_sessionE;
173
174 // API definition
175 typedef struct {
176   char *name;
177   AFB_sessionE session;
178   AFB_apiCB callback;
179   char *info;
180 } AFB_restapi;
181
182 // Plugin definition
183 typedef struct {
184   AFB_pluginE type;  
185   char *info;
186   char *prefix;
187   size_t prefixlen;
188   json_object *jtype;
189   AFB_restapi *apis;
190   void *handle;
191   int  ctxCount;
192   AFB_freeCtxCB freeCtxCB;  // callback to free application context [null for standard free]
193 } AFB_plugin;
194
195
196 // User Client Session Context
197 typedef struct {
198   char uuid[37];        // long term authentication of remote client
199   char token[37];       // short term authentication of remote client
200   time_t timeStamp;     // last time token was refresh
201   int   restfull;       // client does not use cookie
202   void **contexts;      // application specific context [one per plugin]]
203   AFB_plugin **plugins; // we need plugins reference to cleanup session outside of call context
204 } AFB_clientCtx;
205
206 // MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
207 typedef struct {
208   const char *uuid;
209   const char *url;
210   char *prefix;              // plugin convivial name
211   char *api;
212   AFB_PostRequest *post;
213   json_object *jresp;
214   void *context;             // Hold Client Context when using session
215   void *handle;              // provide callback and easy access to plugin
216   int  restfull;             // request is resfull [uuid token provided]
217   int  errcode;              // http error code
218   sigjmp_buf checkPluginCall; // context save for timeout set/longjmp
219   AFB_config *config;         // plugin may need access to config
220   struct MHD_Connection *connection;
221   AFB_plugin **plugins;
222 } AFB_request;
223
224 struct afb_hsrv_handler;
225 struct MHD_Daemon;
226 typedef struct {
227   AFB_config  *config;   // pointer to current config
228   // List of commands to execute
229   int  background;        // run in backround mode
230   int  foreground;        // run in forground mode
231   char *cacheTimeout;     // http require timeout to be a string
232   struct MHD_Daemon *httpd;            // structure for httpd handler
233   int  fakemod;           // respond to GET/POST request without interacting with sndboard
234   int  readyfd;           // a #fd to signal when ready to serve
235   AFB_plugin **plugins;   // pointer to REST/API plugins 
236   magic_t  magic;         // Mime type file magic lib
237   struct afb_hsrv_handler *handlers;
238 } AFB_session;
239
240
241
242 #include "proto-def.h"
243
244 #endif /* LOCAL_DEF_H */