MHEG5  18.9.0
MHEG5 Documentation
mh5access.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2010 Ocean Blue Software Ltd
4  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
25 /*---includes for this file--------------------------------------------------*/
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 
31 #include "mh5profile.h"
32 #include "mh5gate.h"
33 #include "mh5memory.h"
34 #include "mh5base.h"
35 #include "mh5access.h"
36 #include "mh5fileorm.h"
37 #include "mh5misc.h"
38 
39 #ifndef CI_PLUS_ONLY
40 #include "glue_dsmcc.h"
41 #endif
42 #include "stb_os.h"
43 
44 /*---constant definitions for this file--------------------------------------*/
45 
46 
47 #define SERVER_AUTH_FILE "DSM://auth.servers"
48 #define ACCESS_FILE_TIMEOUT_MS (1000 * 60 * 60 * 4)
49 #define MAX_CALLBACKS 5
50 
51 /*---local typedef structs for this file-------------------------------------*/
52 
53 typedef enum
54 {
61 
62 
63 /*---local (static) variable declarations for this file----------------------*/
64 static AccessFileState_t accessFileState = ACCESS_FILE_NOTLOADED;
65 
66 static U32BIT lastAccessUpdate = 0;
67 static MHEG5String accessFile = {0, NULL};
68 static U16BIT callbackCount = 0;
69 
70 static void(*fileLoadedCallback[MAX_CALLBACKS]) (void);
71 
72 /*---local function prototypes for this file---------------------------------*/
73 static MHEG5ServerAccess_t CheckUrlPermitted(U8BIT *url);
74 static void AuthFileRetrieved( void *userData, S_CONTENT *content );
75 static void AuthFileRetrievalFailed( void *userData );
76 static int GetLineLength(U8BIT *string, U8BIT **remaining, int max);
77 static void NotifyServerAccessReady(void);
78 
82 static void MHEG5UpdateServerAccess(void)
83 {
84  MHEG5String authFilename;
85 
86  accessFileState = ACCESS_FILE_LOADING_SYNC;
87 
88  lastAccessUpdate = STB_OSGetClockMilliseconds();
89 
90  authFilename.data = (U8BIT *)SERVER_AUTH_FILE;
91  authFilename.len = strlen(SERVER_AUTH_FILE);
92 
93  (void)MHEG5FileOrmGet( authFilename, FRP_CACHE_DEFAULT, NULL,
94  AuthFileRetrieved, AuthFileRetrievalFailed );
95 
96  if (accessFileState == ACCESS_FILE_LOADING_SYNC)
97  {
98  accessFileState = ACCESS_FILE_LOADING_ASYNC;
99  }
100 }
101 
112 {
113  U32BIT currentTime;
114  BOOLEAN timeToUpdate;
115  MHEG5ServerAccess_t permission;
116 
117  currentTime = STB_OSGetClockMilliseconds();
118  timeToUpdate = FALSE;
119 
120  permission = SERVER_ACCESS_PENDING;
121 
122  if (accessFileState == ACCESS_FILE_NOTLOADED)
123  {
124  timeToUpdate = TRUE;
125  }
126  else if ((currentTime > lastAccessUpdate) &&
127  (currentTime - lastAccessUpdate) > ACCESS_FILE_TIMEOUT_MS)
128  {
129  timeToUpdate = TRUE;
130  }
131  else if (currentTime < lastAccessUpdate)
132  {
133  /* overflow */
134  timeToUpdate = TRUE;
135  }
136 
137  if (timeToUpdate)
138  {
139  MHEG5UpdateServerAccess();
140  }
141 
142  switch (accessFileState)
143  {
144  case ACCESS_FILE_LOADED:
145  permission = CheckUrlPermitted(url);
146  break;
147  case ACCESS_FILE_MISSING:
148  permission = SERVER_ACCESS_BLOCKED;
149  break;
151  permission = SERVER_ACCESS_PENDING;
152  break;
153  default:
154  assert(0);
155  }
156 
157  return permission;
158 }
159 
165 static MHEG5ServerAccess_t CheckUrlPermitted(U8BIT *url)
166 {
167  U8BIT *fileBuffer;
168  U8BIT *line;
169  U8BIT *nextLine;
170  int lineLength;
171  int remainingChars;
172  MHEG5ServerAccess_t permission;
173  MHEG5ServerAccess_t entryAuthRequired;
174  int differ;
175 
176  permission = SERVER_ACCESS_BLOCKED;
177  if (accessFileState == ACCESS_FILE_LOADED)
178  {
179  fileBuffer = accessFile.data;
180 
181  /*get first line*/
182  line = fileBuffer;
183  remainingChars = accessFile.len;
184  lineLength = GetLineLength(line, &nextLine, remainingChars);
185  while (lineLength >= 0)
186  {
187  if (*line == '!')
188  {
189  /*remove the '!'*/
190  line++;
191  lineLength--;
192 
193  entryAuthRequired = SERVER_ACCESS_PERMITTED_NO_AUTH;
194  }
195  else
196  {
197  entryAuthRequired = SERVER_ACCESS_PERMITTED_AUTH;
198  }
199 
200  if (lineLength == 0)
201  {
202  /*ignore blank lines*/
203  ;
204  }
205  else if (*line == '#')
206  {
207  /*ignore lines beginning with #*/
208  ;
209  }
210  else if (strlen((char *)url) <= (size_t)lineLength)
211  {
212  /*url must be longer than line*/
213  ;
214  }
215  else
216  {
217  /*check the url against the access file entry*/
218  differ = MHEG5strncmp( line, url, lineLength );
219 
220  if (!differ)
221  {
222  /*check that either the server-list line ends with '/'
223  or the next character in the url is one*/
224  if (line[lineLength - 1] == '/' || url[lineLength] == '/')
225  {
226  permission = entryAuthRequired;
227  break;
228  }
229  }
230  }
231  /*get next line*/
232  line = nextLine;
233  remainingChars -= (lineLength + 1);
234  lineLength = GetLineLength(line, &nextLine, remainingChars);
235  }
236  }
237  return permission;
238 }
239 
248 static void AuthFileRetrieved(void *userData, S_CONTENT *content )
249 {
250  BOOLEAN call;
251 
252  USE_UNWANTED_PARAM(userData);
253 
254  /*free any file already loaded*/
255  if (accessFile.data)
256  {
257  MHEG5freeMem(accessFile.data);
258  }
259 
260  accessFile.data = MHEG5getMem(content->size + 1);
261  if (accessFile.data != NULL)
262  {
263  accessFile.len = content->size;
264 
265  memcpy(accessFile.data, content->data, content->size);
266  accessFile.data[accessFile.len] = '\0';
267 
268  call = FALSE;
269  if (accessFileState == ACCESS_FILE_LOADING_ASYNC)
270  {
271  call = TRUE;
272  }
273 
274  accessFileState = ACCESS_FILE_LOADED;
275 
276  /*call the provided callback if one was provided*/
277  if (call)
278  {
279  NotifyServerAccessReady();
280  }
281  }
282 }
283 
290 static void AuthFileRetrievalFailed( void *userData )
291 {
292  BOOLEAN call;
293 
294  USE_UNWANTED_PARAM(userData);
295 
296  call = FALSE;
297  if (accessFileState == ACCESS_FILE_LOADING_ASYNC)
298  {
299  call = TRUE;
300  }
301 
302  accessFileState = ACCESS_FILE_MISSING;
303 
304  /*free any loaded file*/
305  accessFile.len = 0;
306  if (accessFile.data)
307  {
308  MHEG5freeMem(accessFile.data);
309  accessFile.data = NULL;
310  }
311 
312  /*call the provided callback if one was provided*/
313  if (call)
314  {
315  NotifyServerAccessReady();
316  }
317 }
318 
324 {
325  if (accessFileState != ACCESS_FILE_LOADING_ASYNC)
326  {
327  accessFile.len = 0;
328  if (accessFile.data)
329  {
330  MHEG5freeMem(accessFile.data);
331  accessFile.data = NULL;
332  }
333  }
334 
335  accessFileState = ACCESS_FILE_NOTLOADED;
336 }
337 
344 void MHEG5AddServerAccessCallback(void (*LoadNotifyCallback)(void))
345 {
346  if (callbackCount < MAX_CALLBACKS)
347  {
348  fileLoadedCallback[callbackCount] = LoadNotifyCallback;
349  callbackCount++;
350  }
351 }
352 
360 static int GetLineLength(U8BIT *string, U8BIT **remaining, int max)
361 {
362  int i = -1;
363 
364  *remaining = NULL;
365 
366  if (string != NULL && max > 0)
367  {
368  /*check each character*/
369  for (i = 0; i < max; i++)
370  {
371  /*stop on newline or end of text*/
372  if (string[i] == '\n')
373  {
374  /*set remainder to start of next line*/
375  *remaining = string + i + 1;
376  break;
377  }
378  else if (string[i] == '\0')
379  {
380  /*no remaining text*/
381  break;
382  }
383  }
384  }
385  /*return the number of characters*/
386  return i;
387 }
388 
393 static void NotifyServerAccessReady(void)
394 {
395  U16BIT i;
396 
397  for (i = 0; i < callbackCount; i++)
398  {
399  if (fileLoadedCallback[i] != NULL)
400  {
401  fileLoadedCallback[i]();
402  }
403  }
404 }
405 
U32BIT STB_OSGetClockMilliseconds(void)
Get Current Computer Clock Time.
Basis MHEG5 data types.
U32BIT size
Definition: fs_types.h:54
Interface functions to DSM-CC instance for MHEG5.
MHEG5ServerAccess_t
Definition: mh5access.h:36
AccessFileState_t
Definition: mh5access.c:53
#define MHEG5getMem
Definition: glue_memory.h:93
void * MHEG5FileOrmGet(MHEG5String name, U16BIT priority, void *userData, F_CB_Good cbGood, F_CB_Fail cbFail)
Get a file. The file will be loaded and one of the callback functions called when request is resolved...
Definition: mh5fileorm.c:1179
Miscellaneous.
#define FRP_CACHE_DEFAULT
Definition: mh5fileorm.h:38
MHEG5ServerAccess_t MHEG5CheckServerPermitted(U8BIT *url)
Definition: mh5access.c:111
uint8_t U8BIT
Definition: techtype.h:82
#define MHEG5freeMem
Definition: glue_memory.h:94
This file defines the profile for the MHEG engine.
Implement Functions to support Service Gateways. Functions for standarizing several GroupIDs like +DS...
#define ACCESS_FILE_TIMEOUT_MS
Definition: mh5access.c:48
Functions relating to HTTPS Server Access.
MHEG5Byte * data
Definition: mh5base.h:85
#define MHEG5strncmp(a, b, n)
Definition: mh5base.h:52
uint16_t U16BIT
Definition: techtype.h:84
File interface functions to DSMCC component.
void MHEG5AddServerAccessCallback(void(*LoadNotifyCallback)(void))
Add callback function to be called when the server access permission file is loaded.
Definition: mh5access.c:344
#define MAX_CALLBACKS
Definition: mh5access.c:49
redirection include
#define FALSE
Definition: techtype.h:68
MHEG5Int len
Definition: mh5base.h:84
U8BIT * data
Definition: fs_types.h:55
U8BIT BOOLEAN
Definition: techtype.h:99
#define USE_UNWANTED_PARAM(param)
Definition: techtype.h:48
#define TRUE
Definition: techtype.h:69
uint32_t U32BIT
Definition: techtype.h:86
#define SERVER_AUTH_FILE
Definition: mh5access.c:47
void MHEG5ResetServerAccess(void)
Reset the cachedserver access permission file.
Definition: mh5access.c:323
Header file - Function prototypes for operating system.