aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-server/interceptors.c
blob: 9e66ce65b17533c28bcc5715099471ccd5299650 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
 * mds — A micro-display server
 * Copyright © 2014, 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "interceptors.h"

#include "globals.h"
#include "interception-condition.h"
#include "client.h"
#include "queued-interception.h"

#include <libmdsserver/macros.h>
#include <libmdsserver/hash-help.h>

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>


/**
 * Remove interception condition by index
 * 
 * @param  client  The intercepting client
 * @param  index   The index of the condition
 */
static void remove_intercept_condition(client_t* client, size_t index)
{
  interception_condition_t* conds = client->interception_conditions;
  size_t n = client->interception_conditions_count;
  
  /* Remove the condition from the list. */
  free(conds[index].condition);
  memmove(conds + index, conds + index + 1, --n - index);
  client->interception_conditions_count--;
  
  /* Shrink the list. */
  if (client->interception_conditions_count == 0)
    {
      free(conds);
      client->interception_conditions = NULL;
    }
  else
    if (xrealloc(conds, n, interception_condition_t))
      xperror(*argv);
    else
      client->interception_conditions = conds;
}


/**
 * Add an interception condition for a client
 * 
 * @param  client     The client
 * @param  condition  The header, optionally with value, to look for, or empty (not NULL) for all messages
 * @param  priority   Interception priority
 * @param  modifying  Whether the client may modify the messages
 * @param  stop       Whether the condition should be removed rather than added
 */
void add_intercept_condition(client_t* client, char* condition, int64_t priority, int modifying, int stop)
{
  size_t n = client->interception_conditions_count;
  interception_condition_t* conds = client->interception_conditions;
  ssize_t nonmodifying = -1;
  char* header = condition;
  char* colon = NULL;
  char* value;
  size_t hash;
  size_t i;
  
  /* Split header and value apart. */
  if ((value = strchr(header, ':')) != NULL)
    {
      *value = '\0'; /* NUL-terminate header. */
      colon = value; /* End of header. */
      value += 2;    /* Skip over delimiter.  */
    }
  
  /* Calcuate header hash (comparison optimisation) */
  hash = string_hash(header);
  /* Undo header–value splitting. */
  if (colon != NULL)
    *colon = ':';
  
  /* Remove of update condition of already registered,
     also look for non-modifying condition to swap position
     with for optimisation. */
  for (i = 0; i < n; i++)
    {
      if ((conds[i].header_hash != hash) || !strequals(conds[i].condition, condition))
	{
	  /* Look for the first non-modifying, this is a part of the
	     optimisation where we put all modifying conditions at the
	     beginning. */
	  if ((nonmodifying < 0) && conds[i].modifying)
	    nonmodifying = (ssize_t)i;
	  continue;
	}
      
      if (stop)
	remove_intercept_condition(client, i);
      else
	{
	  /* Update parameters. */
	  conds[i].priority = priority;
	  conds[i].modifying = modifying;
	  
	  if (modifying && (nonmodifying >= 0))
	    {
	      /* Optimisation: put conditions that are modifying
		 at the beginning. When a client is intercepting
		 we most know if any satisfying condition is
		 modifying. With this optimisation the first
		 satisfying condition will tell us if there is
		 any satisfying condition that is modifying. */
	      interception_condition_t temp = conds[nonmodifying];
	      conds[nonmodifying] = conds[i];
	      conds[i] = temp;
	    }
	}
      return;
    }
  
  if (stop)
    eprint("client tried to stop intercepting messages that it does not intercept.");
  else
    {
      /* Duplicate condition string. */
      fail_if (xstrdup(condition, condition));
      
      /* Grow the interception condition list. */
      fail_if (xrealloc(conds, n + 1, interception_condition_t));
      client->interception_conditions = conds; 
      /* Store condition. */
      client->interception_conditions_count++;
      conds[n].condition = condition;
      conds[n].header_hash = hash;
      conds[n].priority = priority;
      conds[n].modifying = modifying;
      
      if (modifying && (nonmodifying >= 0))
	{
	  /* Optimisation: put conditions that are modifying
	     at the beginning. When a client is intercepting
	     we most know if any satisfying condition is
	     modifying. With this optimisation the first
	     satisfying condition will tell us if there is
	     any satisfying condition that is modifying. */
	  interception_condition_t temp = conds[nonmodifying];
	  conds[nonmodifying] = conds[n];
	  conds[n] = temp;
	}
    }
  
  return;
 fail:
  xperror(*argv);
  free(condition);
  return;
}


/**
 * Check if a condition matches any of a set of accepted patterns
 * 
 * @param   cond     The condition
 * @param   hashes   The hashes of the accepted header names
 * @param   keys     The header names
 * @param   headers  The header name–value pairs
 * @param   count    The number of accepted patterns
 * @return           Evaluates to true if and only if a matching pattern was found
 */
int is_condition_matching(interception_condition_t* cond, size_t* hashes,
			  char** keys, char** headers, size_t count)
{
  size_t i;
  for (i = 0; i < count; i++)
    if (*(cond->condition) == '\0')
      return 1;
    else if ((cond->header_hash == hashes[i]) &&
	     (strequals(cond->condition, keys[i]) ||
	      strequals(cond->condition, headers[i])))
      return 1;
  return 0;
}


/**
 * Find a matching condition to any of a set of acceptable conditions
 * 
 * @param   client            The intercepting client
 * @param   hashes            The hashes of the accepted header names
 * @param   keys              The header names
 * @param   headers           The header name–value pairs
 * @param   count             The number of accepted patterns
 * @param   interception_out  Storage slot for found interception
 * @return                    -1 on error, otherwise: evalutes to true iff a matching condition was found
 */
int find_matching_condition(client_t* client, size_t* hashes, char** keys, char** headers,
			    size_t count, queued_interception_t* interception_out)
{
  pthread_mutex_t mutex = client->mutex;
  interception_condition_t* conds = client->interception_conditions;
  size_t n = 0, i;
  
  fail_if ((errno = pthread_mutex_lock(&(mutex))));
  
  /* Look for a matching condition. */
  if (client->open)
    n = client->interception_conditions_count;
  for (i = 0; i < n; i++)
    if (is_condition_matching(conds + i, hashes, keys, headers, count))
      {
	/* Report matching condition. */
	interception_out->client    = client;
	interception_out->priority  = conds[i].priority;
	interception_out->modifying = conds[i].modifying;
	break;
      }
  
  pthread_mutex_unlock(&(mutex));
  
  return i < n;
 fail:
  return -1;
}


/**
 * Get all interceptors who have at least one condition matching any of a set of acceptable patterns
 * 
 * @param   sender                   The original sender of the message
 * @param   hashes                   The hashes of the accepted header names
 * @param   keys                     The header names
 * @param   headers                  The header name–value pairs
 * @param   count                    The number of accepted patterns
 * @param   interceptions_count_out  Slot at where to store the number of found interceptors
 * @return                           The found interceptors, `NULL` on error
 */
queued_interception_t* get_interceptors(client_t* sender, size_t* hashes, char** keys, char** headers,
					size_t count, size_t* interceptions_count_out)
{
  queued_interception_t* interceptions = NULL;
  size_t interceptions_count = 0, n = 0;
  ssize_t node;
  int saved_errno;
  
  /* Count clients. */
  foreach_linked_list_node (client_list, node)
    n++;
  
  /* Allocate interceptor list. */
  fail_if (xmalloc(interceptions, n, queued_interception_t));
  
  /* Search clients. */
  foreach_linked_list_node (client_list, node)
    {
      client_t* client = (client_t*)(void*)(client_list.values[node]);
      
      /* Look for and list a matching condition. */
      if (client->open && (client != sender))
	{
	  int r = find_matching_condition(client, hashes, keys, headers, count,
					  interceptions + interceptions_count);
	  fail_if (r == -1);
	  if (r)
	    /* List client of there was a matching condition. */
	    interceptions_count++;
	}
    }
  
  *interceptions_count_out = interceptions_count;
  return interceptions;
  
 fail:
  saved_errno = errno;
  free(interceptions);
  return errno = saved_errno, NULL;
}