forked from tvntsr/push
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush.c
376 lines (282 loc) · 8.39 KB
/
push.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <arpa/inet.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../mem/mem.h"
#include "../../parser/parse_to.h"
#include "../../lib/cds/list.h"
#include "../../lib/srdb1/db.h"
#include "../../lib/srdb1/db_val.h"
#include "push.h"
// Create a new char* chunk.
// Return NULL in case of error
// chunk should be released by free()
static char* make_frame_msg(APNS_Frame* frame);
// print item to buffer accroding to the spec:
// id:len:item
static int print_item(char* buff_to, int item_id, char* from, size_t from_len);
// prints poayload to buffer
// return 0 on success,
// on error:
// -1 small buffer
// -2 payload string too long (limit is 256 chars)
static int print_payload_msg(APNS_Payload* payload, char* buff, size_t size);
static int notification_add_frame(APNS_Notification* to, APNS_Frame* frame);
static APNS_Frame* create_frame(APNS_Item* item);
static void destroy_frame(APNS_Frame* );
static int print_payload_msg(APNS_Payload* payload, char* buff, size_t size)
{
// :TODO: only alert is used
// CallId should be used too
// it cannot be compound value, only message
if (payload == NULL)
return -1;
if (payload->alert == NULL)
return -1;
int printed = payload->custom_param == NULL ?
snprintf(buff, size,
// "{\"aps\":{\"alert\":\"%s\"},\"call-id\":\"%s\"}",
"{\"aps\":{\"alert\":{\"body\":\"%s\"}}}",
payload->alert):
snprintf(buff, size,
"{\"aps\":{\"alert\":{\"body\":\"%s\"}}, %s}",
payload->alert, payload->custom_param);
LM_DBG("Payload: [%s], total %d\n", buff, printed);
return printed;
}
static int print_item(char* buff_to, int item_id, char* from, size_t from_len)
{
char* ptr = buff_to;
char ID = item_id & 0xff;
uint16_t len = htons(from_len);
int res;
memmove(ptr, &ID, 1);
ptr += 1;
memmove(ptr, &len, sizeof(len));
ptr += sizeof(len);
memmove(ptr, from, from_len);
res = from_len + 1 + sizeof(len);
LM_DBG("printed item %d, result len %d\n", item_id, res);
return res;
}
static int print_item_msg(APNS_Item* payload, char* buff, size_t size)
{
int printed = 0;
int ret;
uint32_t t;
char msg_buf[PAYLOAD_MAX_LEN + 1];
if (payload == NULL)
return 0;
if (buff == NULL)
return 0;
if (size == 0)
return 0;
if (size < DEVICE_TOKEN_LEN_BIN)
return 0;
LM_DBG("print items\n");
printed += print_item(buff + printed, DeviceTokenID, payload->token, DEVICE_TOKEN_LEN_BIN);
ret = print_payload_msg(payload->payload, msg_buf, PAYLOAD_MAX_LEN);
if (ret == 0)
return 0;
printed += print_item(buff + printed, PayloadID, msg_buf, ret);
if (size < printed + sizeof(t) + sizeof(payload->expiration) + 1)
return 0;
t = htonl(payload->identifier);
printed += print_item(buff + printed, NotificationID, (char*)&t, sizeof(t));
printed += print_item(buff + printed, ExpDateID, (char*)&payload->expiration, sizeof(payload->expiration));
printed += print_item(buff + printed, PriorityID, (char*)&payload->priority, sizeof(payload->priority));
LM_DBG("Priority: %02X, id: %04X, printed total: %d\n", ((unsigned)payload->priority) & 0xff, payload->identifier, printed +1);
return printed;
}
// Create a new char* chunk.
// Return NULL in case of error
// chunk should be released by free()
static char* make_frame_msg(APNS_Frame* frame)
{
// size of the chunk:
// payload 256
// token 32
// id 4
// expire 4
// prio 1
// total 297
// Frame: + (1 (number) + 2 (length))* frame count
// Total: 312
#define FRAME_BUFFER_SIZE 312
if (frame == NULL)
return NULL;
LM_DBG("Making the frame\n");
char * chunk = (char*)malloc(FRAME_BUFFER_SIZE);
if (chunk != NULL)
{
chunk[0] = frame->number;
frame->length = print_item_msg(frame->data,
chunk,
FRAME_BUFFER_SIZE);
if (frame->length == 0)
{
free(chunk);
chunk = NULL;
}
}
if (frame->_chunk)
free(frame->_chunk);
frame->_chunk = chunk;
return chunk;
}
char* make_push_msg(APNS_Notification* notification)
{
#define NOTIFICATION_COMMAND (char)2
#define NOTIFICATION_OFFSET 5
char* buffer;
APNS_Frame* frame = NULL;
int printed = 0;
uint32_t count = 1;
if (notification == NULL)
return NULL;
LM_DBG("Making push message\n");
notification->length = 0;
frame = notification->data;
// Calculate the buffer
while(frame != NULL)
{
char* chunk = NULL;
if (frame->number != count)
{
// Fixup:
frame->number = count;
}
chunk = make_frame_msg(frame);
if (chunk == NULL)
{
// ::FIXME::
LM_ERR("Cannot create frame, abort push\n");
return NULL;
}
notification->length += frame->length;
LM_DBG("Chunk %d, length %d, total: %d\n", count, frame->length, notification->length);
frame = frame->next;
++count ;
}
LM_DBG("Push message length calculated (%u bytes), total %d frames, printing it\n",
notification->length, count-1);
// Fill the buffer
buffer = (char*)malloc(notification->length+NOTIFICATION_OFFSET);
if (buffer == NULL)
{
LM_ERR("Cannot allocate memory\n");
return NULL;
}
buffer[0] = NOTIFICATION_COMMAND;
count = htonl(notification->length);
memcpy(buffer+1, &count, sizeof(count));
printed += NOTIFICATION_OFFSET;
LM_DBG("Base header filled in, add body\n");
frame = notification->data;
while(frame != NULL)
{
LM_DBG("Adding %d frame\n", frame->number);
memmove(buffer+printed, frame->_chunk, frame->length);
printed += frame->length;
frame = frame->next;
}
LM_DBG("Done here\n");
notification->length+=NOTIFICATION_OFFSET;
return buffer;
}
APNS_Notification* create_notification()
{
APNS_Notification* notification;
notification = (APNS_Notification*)calloc(1, sizeof(APNS_Notification));
return notification;
}
void destroy_notification(APNS_Notification* notification)
{
if (notification == NULL)
return;
destroy_frame(notification->data);
free(notification);
}
int notification_add_item(APNS_Notification* to, APNS_Item* data)
{
APNS_Frame* frame = create_frame(data);
return notification_add_frame(to, frame);
}
static int notification_add_frame(APNS_Notification* to, APNS_Frame* frame)
{
APNS_Frame* last = NULL;
uint32_t count = 1;
if (to == NULL)
return -1;
if (frame == NULL)
return -1;
LM_DBG("adding the frame\n");
if (to->data == NULL)
{
to->data = frame;
to->data->number = 1;
to->data->next = to->data->prev = NULL;
LM_DBG("First frame\n");
return 0;
}
LM_DBG("Already one exists, adding the next\n");
while(last->next != NULL)
{
last = last->next;
++count ;
LM_DBG("Current %d\n", count);
}
frame->number = count;
DOUBLE_LINKED_LIST_ADD(to->data,last, frame);
return 0;
}
static APNS_Frame* create_frame(APNS_Item* item)
{
APNS_Frame* frame = (APNS_Frame*)calloc(1, sizeof(APNS_Frame));
if (frame)
{
frame->data = item;
}
return frame;
}
static void destroy_frame(APNS_Frame* frame)
{
APNS_Frame* last = frame;
APNS_Frame* curr;
while(last != NULL)
{
destroy_item(last->data);
curr = last;
last = last->next;
if (curr->_chunk)
free(curr->_chunk);
free(curr);
}
}
APNS_Item* create_item(APNS_Payload* payload, unsigned char prio)
{
APNS_Item* item = (APNS_Item*)calloc(1, sizeof(APNS_Item));
if (item)
{
item->payload = payload;
item->priority = prio;
}
return item;
}
void destroy_item(APNS_Item* item)
{
if (item == NULL)
return;
destroy_payload(item->payload);
free(item);
}
void destroy_payload(APNS_Payload* payload)
{
if (payload == NULL)
return;
free(payload->alert);
free(payload->sound);
}