e956656d1a52ef20662c73a26b334945df68856f
[kamailio] / src / modules / smsops / smsops_impl.c
1 /*
2  * Copyright (C) 2015 Carsten Bock, ng-voice GmbH
3  *
4  * This file is part of Kamailio, a free SIP server.
5  *
6  * Kamailio 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  * Kamailio 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 /*! \file
23  * \brief Support for transformations
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32
33 #include "../../core/dprint.h"
34 #include "../../core/mem/mem.h"
35 #include "../../core/ut.h"
36 #include "../../core/trim.h"
37 #include "../../core/pvapi.h"
38 #include "../../core/dset.h"
39 #include "../../core/basex.h"
40
41 #include "../../core/strutils.h"
42 #include "../../core/parser/parse_content.h"
43
44
45 #include "smsops_impl.h"
46
47 // Types of RP-DATA
48 typedef enum _rp_message_type {
49         RP_DATA_MS_TO_NETWORK = 0x00,
50         RP_DATA_NETWORK_TO_MS = 0x01,
51         RP_ACK_MS_TO_NETWORK  = 0x02,
52         RP_ACK_NETWORK_TO_MS  = 0x03,
53 } rp_message_type_t;
54
55 enum SMS_DATA {
56         SMS_ALL,
57         SMS_RPDATA_TYPE,
58         SMS_RPDATA_REFERENCE,
59         SMS_RPDATA_ORIGINATOR,
60         SMS_RPDATA_DESTINATION,
61         SMS_TPDU_TYPE,
62         SMS_TPDU_FLAGS,
63         SMS_TPDU_CODING,
64         SMS_TPDU_PAYLOAD,
65         SMS_TPDU_PROTOCOL,
66         SMS_TPDU_VALIDITY,
67         SMS_TPDU_REFERENCE,
68         SMS_TPDU_ORIGINATING_ADDRESS,
69         SMS_TPDU_DESTINATION,
70         SMS_UDH_CONCATSM_REF,
71         SMS_UDH_CONCATSM_MAX_NUM_SM,
72         SMS_UDH_CONCATSM_SEQ
73 };
74
75 // Types of the PDU-Message
76 typedef enum _pdu_message_type {
77         DELIVER = 0x00,
78         SUBMIT = 0x01,
79         COMMAND = 0x02,
80         ANY = 0x03,
81 } pdu_message_type_t;
82
83 #define TP_RD 0x4;
84 #define TP_VPF 0x16;
85 #define TP_SRR 0x32;
86 #define TP_UDHI 0x64;
87 #define TP_RP 0x128;
88
89 // Information element identifiers and corresponding structs.
90 // Only the supported ones are listed.
91 // Defined in TS 23.040, Sec. 9.2.3.24 and 9.2.3.24.1
92 #define TP_UDH_IE_CONCAT_SM_8BIT_REF 0x00       // 9.2.3.24
93 struct ie_concat_sm_8bit_ref {
94         unsigned char ref;                      //Concatenated short message reference number
95         unsigned char max_num_sm;       //Maximum number of short messages in the concatenated short message.
96         unsigned char seq;                      //Sequence number of the current short message
97 };
98
99 // Information element in User Data Header
100 typedef struct _tp_udh_inf_element tp_udh_inf_element_t;
101 struct _tp_udh_inf_element {
102         unsigned char identifier;
103         union {
104                 str data;
105                 struct ie_concat_sm_8bit_ref concat_sm_8bit_ref;
106         };
107
108         tp_udh_inf_element_t* next;
109 };
110
111 // TS 23.040, Sec. 9.2.3.24
112 typedef struct _tp_user_data {
113         tp_udh_inf_element_t* header;
114         str sm;
115 } tp_user_data_t;
116
117 // PDU (GSM 03.40) of the SMS
118 typedef struct _sms_pdu {
119         pdu_message_type_t msg_type;
120         unsigned char reference;
121         unsigned char flags;
122         unsigned char pid;
123         unsigned char coding;
124         unsigned char validity;
125         str originating_address;
126         str destination;
127         tp_user_data_t payload;
128 } sms_pdu_t;
129
130 // RP-Data of the message
131 typedef struct _sms_rp_data {
132         rp_message_type_t msg_type;
133         unsigned char reference;
134         str originator;
135         str destination;
136         unsigned char pdu_len;
137         sms_pdu_t pdu;
138 } sms_rp_data_t;
139
140 // Pointer to current parsed rp_data/tpdu
141 static sms_rp_data_t * rp_data = NULL;
142 // Pointer to rp_data/tpdu used for sending
143 static sms_rp_data_t * rp_send_data = NULL;
144
145 // ID of current message
146 static unsigned int current_msg_id = 0;
147
148 /*******************************************************************
149  * Helper Functions
150  *******************************************************************/
151
152 // Frees internal pointers within the SMS-PDU-Type:
153 void freeRP_DATA(sms_rp_data_t * rpdata) {
154         if (rpdata) {
155                 if (rpdata->originator.s) pkg_free(rpdata->originator.s);
156                 if (rpdata->destination.s) pkg_free(rpdata->destination.s);
157                 if (rpdata->pdu.originating_address.s) pkg_free(rpdata->pdu.originating_address.s);
158                 if (rpdata->pdu.destination.s) pkg_free(rpdata->pdu.destination.s);
159                 while (rpdata->pdu.payload.header) {
160                         tp_udh_inf_element_t* next = rpdata->pdu.payload.header->next;
161                         if(rpdata->pdu.payload.header->identifier != TP_UDH_IE_CONCAT_SM_8BIT_REF) {
162                                 if(rpdata->pdu.payload.header->data.s) pkg_free(rpdata->pdu.payload.header->data.s);
163                         }
164                         pkg_free(rpdata->pdu.payload.header);
165                         rpdata->pdu.payload.header = next;
166                 }
167                 if (rpdata->pdu.payload.sm.s) pkg_free(rpdata->pdu.payload.sm.s);
168         }
169 }
170
171 #define BITMASK_7BITS 0x7F
172 #define BITMASK_8BITS 0xFF
173 #define BITMASK_HIGH_4BITS 0xF0
174 #define BITMASK_LOW_4BITS 0x0F
175 #define BITMASK_TP_UDHI 0x40
176
177 // Encode SMS-Message by merging 7 bit ASCII characters into 8 bit octets.
178 static int ascii_to_gsm(str sms, char * output_buffer, int buffer_size) {
179         // Check if output buffer is big enough.
180         if ((sms.len * 7 + 7) / 8 > buffer_size)
181                 return -1;
182
183         int output_buffer_length = 0;
184         int carry_on_bits = 1;
185         int i = 0;
186
187         for (; i < sms.len; ++i) {
188                 output_buffer[output_buffer_length++] =
189                         ((sms.s[i] & BITMASK_7BITS) >> (carry_on_bits - 1)) |
190                         ((sms.s[i + 1] & BITMASK_7BITS) << (8 - carry_on_bits));
191                 carry_on_bits++;
192                 if (carry_on_bits == 8) {
193                         carry_on_bits = 1;
194                         ++i;
195                 }
196         }
197
198         if (i < sms.len)
199                 output_buffer[output_buffer_length++] = (sms.s[i] & BITMASK_7BITS) >> (carry_on_bits - 1);
200
201         return output_buffer_length;
202 }
203
204 // Decode 7bit encoded message by splitting 8 bit encoded buffer into 7 bit ASCII characters.
205 int gsm_to_ascii(char* buffer, int buffer_length, str sms, const int fill_bits) {
206                 int output_text_length = 0;
207
208                 if(buffer_length <= 2)
209                         return 0;
210
211                 // How many bits we have carried from the next octet. This number can be positive or negative:
212                 // positive: We have carried n bits FROM the next octet.
213                 // negative: We have to carry n bits TO the next octet.
214                 // 0: Nothing carried. Default value!
215                 int carry_on_bits = 0;
216
217                 // Used to iterate over buffer. Declared here, because if there are fill_bits it have to be incremented
218                 int i = 0;
219
220                 // First remove the fill bits, if any
221                 if(fill_bits) {
222                         // We need 7 bits in the first octet, so if there is only 1 fill bit, we don't have to
223                         // carry from the next octet.
224
225                         // cmask stands for carry mask or how many bits to carry from the 2nd octet
226                         unsigned char cmask = (1 << (fill_bits - 1)) - 1;
227
228                         sms.s[output_text_length++] = ( (buffer[0] >> fill_bits) |      // remove the fill bits from the first octet
229                                                                                         (buffer[1] & cmask << (8 - fill_bits)) // mask the required number of bits
230                                                                                                                                                                         //and shift them accordingly
231                                                                                 ) & BITMASK_7BITS;      // mask just 7 bits from the first octet
232
233                         carry_on_bits = fill_bits - 1;
234                         i++;
235                 }
236
237
238                 for (; i < buffer_length; ++i) {
239                         if(carry_on_bits > 0) {
240                                 unsigned char cmask = (1 << (carry_on_bits - 1)) - 1;   //mask for the rightmost carry_on_bits
241                                                                                                                                                 //E.g. carry_on_bits=3 -> _ _ _ _ _ X X X
242                                 sms.s[output_text_length++] = ( (buffer[i] >> carry_on_bits) | //shift right to remove carried bits
243                                                                                                 (buffer[i+1] & cmask) << (8 - carry_on_bits)    // carry from the next
244                                                                                                                                                                                                 // and shift accordingly
245                                                                                                 ) & BITMASK_7BITS;      // mask just 7 bits from the first octet
246                         }
247                         else if(carry_on_bits < 0) {
248                                 carry_on_bits = carry_on_bits * -1;     //make carry_on_bits positive for the bitwise ops
249                                 unsigned char cmask = ((1 << carry_on_bits) - 1) << (8 - carry_on_bits);        //mask for the leftmost carry_on_bits.
250                                                                                                                                                                                 //E.g. carry_on_bits=3 -> X X X _ _ _ _ _
251                                 sms.s[output_text_length++] = ( (buffer[i] << carry_on_bits) | //shift left to make space for the carried bits
252                                                                                                 (buffer[i-1] & cmask) >> (8 - carry_on_bits)    // get the bits from the previous octet
253                                                                                                                                                                                                 // and shift accordingly
254                                                                                                 ) & BITMASK_7BITS;      // mask just 7 bits from the first octet
255
256                                 carry_on_bits = carry_on_bits * -1;     //return the original value
257                         }
258                         else {// carry_on_bits == 0
259                                 sms.s[output_text_length++] = buffer[i] & BITMASK_7BITS;
260                         }
261
262                         //Update carry_on bits. It is always decremented, because we iterate over octests but read just septets
263                         carry_on_bits--;
264
265                         if (output_text_length == sms.len) break;
266
267                         if (carry_on_bits == -8) {
268                                 carry_on_bits = -1;
269                                 sms.s[output_text_length++] = buffer[i] & BITMASK_7BITS;
270                                 if (output_text_length == sms.len) break;
271                         }
272
273                         if(carry_on_bits > 0 && (i + 2 >= buffer_length)) {
274                                 //carry_on_bits is positive, which means thah we have to borrow from the next octet on next iteration
275                                 //However i + 2 >= buffer_length so there is no next octet. This is error.
276                                 break;
277                         }
278                 }
279
280                 if (output_text_length < sms.len)  // Add last remainder.
281                         sms.s[output_text_length++] = buffer[i - 1] >> (8 - carry_on_bits);
282
283                 return output_text_length;
284 }
285
286 // Decode UCS2 message by splitting the buffer into utf8 characters
287 int ucs2_to_utf8 (int ucs2, char * utf8) {
288         if (ucs2 < 0x80) {
289                 utf8[0] = ucs2;
290         utf8[1] = 0;
291                 return 1;
292         }
293         if (ucs2 >= 0x80  && ucs2 < 0x800) {
294                 utf8[0] = (ucs2 >> 6)   | 0xC0;
295                 utf8[1] = (ucs2 & 0x3F) | 0x80;
296                 return 2;
297         }
298         if (ucs2 >= 0x800 && ucs2 < 0xFFFF) {
299                 if (ucs2 >= 0xD800 && ucs2 <= 0xDFFF) return -1;
300                 utf8[0] = ((ucs2 >> 12)       ) | 0xE0;
301                 utf8[1] = ((ucs2 >> 6 ) & 0x3F) | 0x80;
302                 utf8[2] = ((ucs2      ) & 0x3F) | 0x80;
303                 return 3;
304         }
305         if (ucs2 >= 0x10000 && ucs2 < 0x10FFFF) {
306         utf8[0] = 0xF0 | (ucs2 >> 18);
307         utf8[1] = 0x80 | ((ucs2 >> 12) & 0x3F);
308         utf8[2] = 0x80 | ((ucs2 >> 6) & 0x3F);
309         utf8[3] = 0x80 | ((ucs2 & 0x3F));
310                 return 4;
311         }
312         return -1;
313 }
314
315 // Decode UTF8 to UCS2
316 int utf8_to_ucs2 (const unsigned char * input, const unsigned char ** end_ptr) {
317         *end_ptr = input;
318         if (input[0] == 0)
319                 return -1;
320         if (input[0] < 0x80) {
321                 * end_ptr = input + 1;
322                 return input[0];
323         }
324         if ((input[0] & 0xE0) == 0xE0) {
325                 if (input[1] == 0 || input[2] == 0) return -1;
326                 *end_ptr = input + 3;
327                 return (input[0] & 0x0F) << 12 | (input[1] & 0x3F) << 6  | (input[2] & 0x3F);
328         }
329         if ((input[0] & 0xC0) == 0xC0) {
330                 if (input[1] == 0) return -1;
331                 * end_ptr = input + 2;
332                 return (input[0] & 0x1F) << 6 | (input[1] & 0x3F);
333         }
334         return -1;
335 }
336
337 // Encode a digit based phone number for SMS based format.
338 static int EncodePhoneNumber(str phone, char * output_buffer, int buffer_size) {
339         int output_buffer_length = 0;
340         // Check if the output buffer is big enough.
341         if ((phone.len + 1) / 2 > buffer_size)
342                 return -1;
343
344         int i = 0;
345         for (; i < phone.len; ++i) {
346                 if (phone.s[i] < '0' && phone.s[i] > '9')
347                         return -1;
348                 if (i % 2 == 0) {
349                         output_buffer[output_buffer_length++] = BITMASK_HIGH_4BITS | (phone.s[i] - '0');
350                 } else {
351                         output_buffer[output_buffer_length - 1] = (output_buffer[output_buffer_length - 1] & BITMASK_LOW_4BITS) | ((phone.s[i] - '0') << 4);
352                 }
353         }
354
355         return output_buffer_length;
356 }
357
358 // Decode a digit based phone number for SMS based format.
359 static int DecodePhoneNumber(char* buffer, int len, str phone) {
360         int i = 0;
361         for (; i < len; ++i) {
362                 if (i % 2 == 0)
363                         phone.s[i] = (buffer[i / 2] & BITMASK_LOW_4BITS) + '0';
364                 else
365                         phone.s[i] = ((buffer[i / 2] & BITMASK_HIGH_4BITS) >> 4) + '0';
366         }
367         return i;
368 }
369
370 // Generate a 7 Byte Long Time
371 static void EncodeTime(char * buffer) {
372         time_t ts;
373         struct tm now;
374         int i = 0;
375
376         time(&ts);
377         /* Get GMT time */
378         gmtime_r(&ts, &now);
379
380         i = now.tm_year % 100;
381         buffer[0] = (unsigned char)((((i % 10) << 4) | (i / 10)) & 0xff);
382         i = now.tm_mon + 1;
383         buffer[1] = (unsigned char)((((i % 10) << 4) | (i / 10)) & 0xff);
384         i = now.tm_mday;
385         buffer[2] = (unsigned char)((((i % 10) << 4) | (i / 10)) & 0xff);
386         i = now.tm_hour;
387         buffer[3] = (unsigned char)((((i % 10) << 4) | (i / 10)) & 0xff);
388         i = now.tm_min;
389         buffer[4] = (unsigned char)((((i % 10) << 4) | (i / 10)) & 0xff);
390         i = now.tm_sec;
391         buffer[5] = (unsigned char)((((i % 10) << 4) | (i / 10)) & 0xff);
392         buffer[6] = 0; // Timezone, we use no time offset.
393 }
394
395 //The function is called GetXXX but it actually creates the IE if it doesn't exist
396 static struct ie_concat_sm_8bit_ref* GetConcatShortMsg8bitRefIE(sms_rp_data_t* rp_data)
397 {
398         tp_udh_inf_element_t* ie = rp_data->pdu.payload.header;
399         tp_udh_inf_element_t* prev = rp_data->pdu.payload.header;
400         //Look for Concatenated SM 8bit Reference IE
401         while(ie) {
402                 if(ie->identifier == TP_UDH_IE_CONCAT_SM_8BIT_REF)
403                         break;
404                 prev = ie;
405                 ie = ie->next;
406         }
407
408         if(ie == NULL) {
409                 //If not found - create it
410                 ie = pkg_malloc(sizeof(tp_udh_inf_element_t));
411                 if(ie == NULL) {
412                         LM_ERR("no more pkg\n");
413                         return NULL;
414                 }
415                 memset(ie, 0, sizeof(tp_udh_inf_element_t));
416                 ie->identifier = TP_UDH_IE_CONCAT_SM_8BIT_REF;
417
418                 if(prev) {
419                         //If the previous IE is not NULL - link to it
420                         prev->next = ie;
421                 }
422                 else {
423                         //There are not IEs at all
424                         rp_data->pdu.payload.header = ie;
425                         //Set TP-UDHI flag to 1
426                         rp_data->pdu.flags |= BITMASK_TP_UDHI;
427                 }
428         }
429
430         return &(ie->concat_sm_8bit_ref);
431 }
432
433 // Decode SMS-Body into the given structure:
434 int decode_3gpp_sms(struct sip_msg *msg) {
435         str body;
436         int len, blen, j, p = 0;
437         // Parse only the body again, if the mesage differs from the last call:
438         if (msg->id != current_msg_id) {
439                 // Extract Message-body and length: taken from RTPEngine's code
440                 body.s = get_body(msg);
441                 if (body.s == 0) {
442                         LM_ERR("failed to get the message body\n");
443                         return -1;
444                 }
445
446                 /*
447                  * Better use the content-len value - no need of any explicit
448                  * parcing as get_body() parsed all headers and Conten-Length
449                  * body header is automaticaly parsed when found.
450                  */
451                 if (msg->content_length==0) {
452                         LM_ERR("failed to get the content length in message\n");
453                         return -1;
454                 }
455
456                 body.len = get_content_length(msg);
457                 if (body.len==0) {
458                         LM_ERR("message body has length zero\n");
459                         return -1;
460                 }
461
462                 if (body.len + body.s > msg->buf + msg->len) {
463                         LM_ERR("content-length exceeds packet-length by %d\n",
464                                         (int)((body.len + body.s) - (msg->buf + msg->len)));
465                         return -1;
466                 }
467
468                 // Get structure for RP-DATA:
469                 if (!rp_data) {
470                         rp_data = (sms_rp_data_t*)pkg_malloc(sizeof(struct _sms_rp_data));
471                         if (!rp_data) {
472                                 LM_ERR("Error allocating %lu bytes!\n", (unsigned long)sizeof(struct _sms_rp_data));
473                                 return -1;
474                         }
475                 } else {
476                         freeRP_DATA(rp_data);
477                 }
478
479                 // Initialize structure:
480                 memset(rp_data, 0, sizeof(struct _sms_rp_data));
481
482                 ////////////////////////////////////////////////
483                 // RP-Data
484                 ////////////////////////////////////////////////
485                 rp_data->msg_type = (unsigned char)body.s[p++];
486                 rp_data->reference = (unsigned char)body.s[p++];
487                 if ((rp_data->msg_type == RP_DATA_MS_TO_NETWORK) || (rp_data->msg_type == RP_DATA_NETWORK_TO_MS)) {
488                         len = body.s[p++];
489                         if (len > 0) {
490                                 p++; // Type of Number, we assume E164, thus ignored
491                                 len--; // Deduct the type of number from the len
492                                 rp_data->originator.s = pkg_malloc(len * 2);
493                                 rp_data->originator.len = DecodePhoneNumber(&body.s[p], len * 2, rp_data->originator);
494                                 p += len;
495                         }
496                         len = body.s[p++];
497                         if (len > 0) {
498                                 p++; // Type of Number, we assume E164, thus ignored
499                                 len--; // Deduct the type of number from the len
500                                 rp_data->destination.s = pkg_malloc(len * 2);
501                                 rp_data->destination.len = DecodePhoneNumber(&body.s[p], len * 2, rp_data->destination);
502                                 p += len;
503                         }
504
505                         ////////////////////////////////////////////////
506                         // TPDU
507                         ////////////////////////////////////////////////
508                         rp_data->pdu_len = body.s[p++];
509                         if (rp_data->pdu_len > 0) {
510                                 rp_data->pdu.flags = (unsigned char)body.s[p++];
511                                 rp_data->pdu.msg_type = (unsigned char)rp_data->pdu.flags & 0x03;
512                                 rp_data->pdu.reference = (unsigned char)body.s[p++];
513                                 // TP-DA
514                                 rp_data->pdu.destination.len = body.s[p++];
515                                 if (rp_data->pdu.destination.len > 0) {
516                                         p++; // Type of Number, we assume E164, thus ignored
517                                         rp_data->pdu.destination.s = pkg_malloc(rp_data->pdu.destination.len);
518                                         DecodePhoneNumber(&body.s[p], rp_data->pdu.destination.len, rp_data->pdu.destination);
519                                         if (rp_data->pdu.destination.len % 2 == 0) {
520                                                 p += rp_data->pdu.destination.len/2;
521                                         } else {
522                                                 p += (rp_data->pdu.destination.len/2)+1;
523                                         }
524
525                                 }
526                                 rp_data->pdu.pid = (unsigned char)body.s[p++];
527                                 rp_data->pdu.coding = (unsigned char)body.s[p++];
528                                 rp_data->pdu.validity = (unsigned char)body.s[p++];
529
530                                 //TP-User-Data-Length and TP-User-Data
531                                 len = (unsigned char)body.s[p++];
532                                 int fill_bits = 0;
533                                 if (len > 0) {
534                                         if((unsigned char)rp_data->pdu.flags & BITMASK_TP_UDHI) { //TP-UDHI
535                                                 int udh_len = (unsigned char)body.s[p++];
536                                                 int udh_read = 0;
537
538                                                 if(rp_data->pdu.coding == 0) {
539                                                         //calcucate padding size for 7bit coding
540                                                         //udh_len + 1, because the length field itself should be included
541                                                         fill_bits = (7 - (udh_len + 1) % 7) % 7; //padding size is in bits!
542                                                 }
543
544                                                 // Check for malicious length, which might cause buffer overflow
545                                                 if(udh_len > body.len - p) {
546                                                         LM_ERR("TP-User-Data-Length is bigger than the remaining message buffer!\n");
547                                                         return -1;
548                                                 }
549
550                                                 //User-Data-Header
551                                                 tp_udh_inf_element_t* prev_ie = NULL;
552                                                 // IE 'Concatenated short messages, 8-bit reference number' should not be repeated
553                                                 int contains_8bit_refnum = 0;
554                                                 while(udh_read < udh_len) {
555                                                         tp_udh_inf_element_t* ie = pkg_malloc(sizeof(tp_udh_inf_element_t));
556                                                         if(ie == NULL) {
557                                                                 LM_ERR("no more pkg\n");
558                                                                 return -1;
559                                                         }
560                                                         memset(ie, 0, sizeof(tp_udh_inf_element_t));
561
562                                                         ie->identifier = (unsigned char)body.s[p++];
563                                                         ie->data.len = (unsigned char)body.s[p++];
564
565                                                         // Check for malicious length, which might cause buffer overflow
566                                                         if(udh_read + ie->data.len + 2 /* two octets are read so far */ > udh_len) {
567                                                                 LM_ERR("IE Length for IE id %d is bigger than the remaining User-Data element!\n",
568                                                                                                                                                                                                         ie->identifier);
569                                                                 pkg_free(ie);
570                                                                 return -1;
571                                                         }
572
573                                                         if(ie->identifier == TP_UDH_IE_CONCAT_SM_8BIT_REF) {
574                                                                 if(contains_8bit_refnum) {
575                                                                         pkg_free(ie);
576                                                                         LM_ERR("IE Concatenated Short Message 8bit Reference occurred more than once in UDH\n");
577                                                                         return -1;
578                                                                 }
579
580                                                                 ie->concat_sm_8bit_ref.ref = body.s[p++];
581                                                                 ie->concat_sm_8bit_ref.max_num_sm = body.s[p++];
582                                                                 ie->concat_sm_8bit_ref.seq = body.s[p++];
583
584                                                                 contains_8bit_refnum = 1;
585                                                         }
586                                                         else { /* Unsupported IE, save it as binary */
587                                                                 ie->data.s = pkg_malloc(ie->data.len);
588                                                                 if(ie->data.s == NULL) {
589                                                                         pkg_free(ie);
590                                                                         LM_ERR("no more pkg\n");
591                                                                         return -1;
592                                                                 }
593                                                                 memset(ie->data.s, 0, ie->data.len);
594                                                                 memcpy(ie->data.s, &body.s[p], ie->data.len);
595                                                                 p += ie->data.len;
596                                                         }
597
598                                                         if(prev_ie == NULL) {
599                                                                 rp_data->pdu.payload.header = ie;
600                                                         }
601                                                         else {
602                                                                 prev_ie->next = ie;
603                                                         }
604
605                                                         prev_ie = ie;
606                                                         udh_read += (1 /* IE ID */ + 1 /* IE Len */ + ie->data.len /* IE data */);
607                                                 }
608
609                                                 // TS 23.040, Sec. 9.2.3.16
610                                                 // Coding: 7 Bit
611                                                 if (rp_data->pdu.coding == 0x00) {
612                                                         int udh_bit_len = (1 + udh_len) * 8; // add 1 octet for the udh length
613                                                         udh_bit_len += (7 - (udh_bit_len % 7));
614                                                         len -= (udh_bit_len / 7);
615                                                 }else{
616                                                         len -= (1 + udh_len); // add 1 octet for the udh length
617                                                 }
618                                         }
619
620                                         blen = 2 + len*4;
621                                         rp_data->pdu.payload.sm.s = pkg_malloc(blen);
622                                         if(rp_data->pdu.payload.sm.s==NULL) {
623                                                 LM_ERR("no more pkg\n");
624                                                 return -1;
625                                         }
626                                         memset(rp_data->pdu.payload.sm.s, 0, blen);
627                                         // Coding: 7 Bit
628                                         if (rp_data->pdu.coding == 0x00) {
629                                                 // We don't care about the extra used bytes here.
630                                                 rp_data->pdu.payload.sm.len = len;
631                                                 rp_data->pdu.payload.sm.len = gsm_to_ascii(&body.s[p], len, rp_data->pdu.payload.sm, fill_bits);
632                                         } else {
633                                                 // Length is worst-case 2 * len (UCS2 is 2 Bytes, UTF8 is worst-case 4 Bytes)
634                                                 rp_data->pdu.payload.sm.len = 0;
635                                                 while (len > 0) {
636                                                         j = (body.s[p] << 8) + body.s[p + 1];
637                                                         p += 2;
638                                                         rp_data->pdu.payload.sm.len += ucs2_to_utf8(j, &rp_data->pdu.payload.sm.s[rp_data->pdu.payload.sm.len]);
639                                                         len -= 2;
640                                                 }
641                                         }
642                                 }
643                         }
644                 }
645         }
646
647         return 1;
648 }
649
650 int dumpRPData(sms_rp_data_t * rpdata, int level) {
651         if (rpdata) {
652                 LOG(level, "SMS-Message\n");
653                 LOG(level, "------------------------\n");
654                 LOG(level, "RP-Data\n");
655                 LOG(level, "  Type:                       %x\n", rpdata->msg_type);
656                 LOG(level, "  Reference:                  %x (%i)\n", rpdata->reference, rpdata->reference);
657                 LOG(level, "  Originator:                 %.*s (%i)\n", rpdata->originator.len, rpdata->originator.s, rpdata->originator.len);
658                 LOG(level, "  Destination:                %.*s (%i)\n", rpdata->destination.len, rpdata->destination.s, rpdata->destination.len);
659                 LOG(level, "T-PDU\n");
660                 LOG(level, "  Type:                       %x\n", rpdata->pdu.msg_type);
661                 LOG(level, "  Flags:                      %x (%i)\n", rpdata->pdu.flags, rpdata->pdu.flags);
662                 LOG(level, "  Reference:                  %x (%i)\n", rpdata->pdu.reference, rpdata->pdu.reference);
663
664                 LOG(level, "  Originating-Address:        %.*s (%i)\n", rpdata->pdu.originating_address.len, rpdata->pdu.originating_address.s, rpdata->pdu.originating_address.len);
665                 LOG(level, "  Destination:                %.*s (%i)\n", rpdata->pdu.destination.len, rpdata->pdu.destination.s, rpdata->pdu.destination.len);
666
667                 LOG(level, "  Protocol:                   %x (%i)\n", rpdata->pdu.pid, rpdata->pdu.pid);
668                 LOG(level, "  Coding:                     %x (%i)\n", rpdata->pdu.coding, rpdata->pdu.coding);
669                 LOG(level, "  Validity:                   %x (%i)\n", rpdata->pdu.validity, rpdata->pdu.validity);
670
671                 LOG(level, "  Payload:                    %.*s (%i)\n", rpdata->pdu.payload.sm.len, rpdata->pdu.payload.sm.s, rpdata->pdu.payload.sm.len);
672         }
673         return 1;
674 }
675
676 /*******************************************************************
677  * Implementation
678  *******************************************************************/
679
680 /*
681  * Creates the body for SMS-ACK from the current message
682  */
683 int pv_sms_ack(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) {
684         str rp_data_ack = {0, 0};
685
686         // Decode the 3GPP-SMS:
687         if (decode_3gpp_sms(msg) != 1) {
688                 LM_ERR("Error getting/decoding RP-Data from request!\n");
689                 return -1;
690         }
691
692         // RP-Type (1) + RP-Ref (1) + RP-User-Data (Element-ID (1) + Length (9 => Msg-Type (1) + Parameter (1) + Service-Centre-Time (7)) = 13;
693         rp_data_ack.len = 13;
694         rp_data_ack.s = (char*)pkg_malloc(rp_data_ack.len);
695         if (!rp_data_ack.s) {
696                 LM_ERR("Error allocating %d bytes!\n", rp_data_ack.len);
697                 return -1;
698         }
699
700         // Encode the data (RP-Data)
701         // Always ACK NETWORK to MS
702         rp_data_ack.s[0] = RP_ACK_NETWORK_TO_MS;
703         // Take the reference from request:
704         rp_data_ack.s[1] = rp_data->reference;
705         // RP-Data-Element-ID
706         rp_data_ack.s[2] = 0x41;
707         // Length
708         rp_data_ack.s[3] = 9;
709         // PDU
710         // SMS-SUBMIT-Report
711         rp_data_ack.s[4] = SUBMIT;
712         // Parameters (none)
713         rp_data_ack.s[5] = 0x0;
714
715         EncodeTime(&rp_data_ack.s[6]);
716
717         return pv_get_strval(msg, param, res, &rp_data_ack);
718 }
719
720
721 /*
722  * Creates the body for SMS-ACK from the current message
723  */
724 int pv_sms_body(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) {
725         dumpRPData(rp_send_data, L_DBG);
726
727         str sms_body = {0, 0};
728         int buffer_size = 1024, lenpos = 0, i = 0;
729
730         // We assume a maximum size of 1024 Bytes, to be verified.
731         sms_body.s = (char*)pkg_malloc(buffer_size);
732         if (!sms_body.s) {
733                 LM_ERR("Error allocating %i bytes!\n", buffer_size);
734                 return -1;
735         }
736
737         // Encode the data (RP-Data)
738         sms_body.s[sms_body.len++] = rp_send_data->msg_type;
739         sms_body.s[sms_body.len++] = rp_send_data->reference;
740         lenpos = sms_body.len;
741         sms_body.s[sms_body.len++] = 0x00;
742         if (rp_send_data->originator.len > 0) {
743                 sms_body.s[sms_body.len++] = 0x91; // Type of number: ISDN/Telephony Numbering (E164), no extension
744                 i = EncodePhoneNumber(rp_send_data->originator, &sms_body.s[sms_body.len], buffer_size - sms_body.len);
745                 sms_body.s[lenpos] = i + 1;
746                 sms_body.len += i;
747         }
748         lenpos = sms_body.len;
749         sms_body.s[sms_body.len++] = 0x00;
750         if (rp_send_data->destination.len > 0) {
751                 sms_body.s[sms_body.len++] = 0x91; // Type of number: ISDN/Telephony Numbering (E164), no extension
752                 i = EncodePhoneNumber(rp_send_data->destination, &sms_body.s[sms_body.len], buffer_size - sms_body.len);
753                 sms_body.s[lenpos] = i + 1;
754                 sms_body.len += i;
755         }
756         // Store the position of the length for later usage:
757         lenpos = sms_body.len;
758         sms_body.s[sms_body.len++] = 0x00;
759
760         ///////////////////////////////////////////////////
761         // T-PDU
762         ///////////////////////////////////////////////////
763         sms_body.s[sms_body.len++] = rp_send_data->pdu.msg_type | rp_send_data->pdu.flags | 0x4; // We've always got no more messages to send.
764         // Originating Address:
765         sms_body.s[sms_body.len++] = rp_send_data->pdu.originating_address.len;
766         sms_body.s[sms_body.len++] = 0x91; // Type of number: ISDN/Telephony Numbering (E164), no extension
767         sms_body.len += EncodePhoneNumber(rp_send_data->pdu.originating_address, &sms_body.s[sms_body.len], buffer_size - sms_body.len);
768         // Protocol ID
769         sms_body.s[sms_body.len++] = rp_send_data->pdu.pid;
770         // Encoding (0 => default 7 Bit)
771         sms_body.s[sms_body.len++] = rp_send_data->pdu.coding;
772         // Service-Center-Timestamp (always 7 octets)
773         EncodeTime(&sms_body.s[sms_body.len]);
774         sms_body.len += 7;
775         sms_body.s[sms_body.len++] = rp_send_data->pdu.payload.sm.len;
776         i = ascii_to_gsm(rp_send_data->pdu.payload.sm, &sms_body.s[sms_body.len], buffer_size - sms_body.len);
777         sms_body.len += i - 1;
778
779         // Update the len of the PDU
780         sms_body.s[lenpos] = (unsigned char)(sms_body.len - lenpos - 1);
781
782         return pv_get_strval(msg, param, res, &sms_body);
783 }
784
785 int pv_get_sms(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) {
786         if (param==NULL) return -1;
787
788         // Decode the 3GPP-SMS:
789         if (decode_3gpp_sms(msg) != 1) {
790                 LM_ERR("Error getting/decoding RP-Data from request!\n");
791                 return -1;
792         }
793
794         switch(param->pvn.u.isname.name.n) {
795                 case SMS_RPDATA_TYPE:
796                         return pv_get_sintval(msg, param, res, (int)rp_data->msg_type);
797                 case SMS_RPDATA_REFERENCE:
798                         return pv_get_sintval(msg, param, res, (int)rp_data->reference);
799                 case SMS_RPDATA_ORIGINATOR:
800                         return pv_get_strval(msg, param, res, &rp_data->originator);
801                 case SMS_RPDATA_DESTINATION:
802                         return pv_get_strval(msg, param, res, &rp_data->destination);
803                 case SMS_TPDU_TYPE:
804                         return  pv_get_sintval(msg, param, res, (int)rp_data->pdu.msg_type);
805                 case SMS_TPDU_FLAGS:
806                         return  pv_get_sintval(msg, param, res, (int)rp_data->pdu.flags);
807                 case SMS_TPDU_CODING:
808                         return  pv_get_sintval(msg, param, res, (int)rp_data->pdu.coding);
809                 case SMS_TPDU_PROTOCOL:
810                         return  pv_get_sintval(msg, param, res, (int)rp_data->pdu.pid);
811                 case SMS_TPDU_VALIDITY:
812                         return  pv_get_sintval(msg, param, res, (int)rp_data->pdu.validity);
813                 case SMS_TPDU_REFERENCE:
814                         return  pv_get_sintval(msg, param, res, (int)rp_data->pdu.reference);
815                 case SMS_TPDU_PAYLOAD:
816                         return pv_get_strval(msg, param, res, &rp_data->pdu.payload.sm);
817                 case SMS_TPDU_DESTINATION:
818                         return pv_get_strval(msg, param, res, &rp_data->pdu.destination);
819                 case SMS_TPDU_ORIGINATING_ADDRESS:
820                         return pv_get_strval(msg, param, res, &rp_data->pdu.originating_address);
821                 case SMS_UDH_CONCATSM_REF: {
822                         tp_udh_inf_element_t* ie = rp_data->pdu.payload.header;
823                         while(ie) {
824                                 if(ie->identifier == TP_UDH_IE_CONCAT_SM_8BIT_REF)
825                                         return pv_get_uintval(msg, param, res, (unsigned int)ie->concat_sm_8bit_ref.ref);
826                                 ie = ie->next;
827                         }
828                         return -1;
829                 }
830                 case SMS_UDH_CONCATSM_MAX_NUM_SM: {
831                         tp_udh_inf_element_t* ie = rp_data->pdu.payload.header;
832                         while(ie) {
833                                 if(ie->identifier == TP_UDH_IE_CONCAT_SM_8BIT_REF)
834                                         return pv_get_uintval(msg, param, res, (unsigned int)ie->concat_sm_8bit_ref.max_num_sm);
835                                 ie = ie->next;
836                         }
837                         return -1;
838                 }
839                 case SMS_UDH_CONCATSM_SEQ: {
840                         tp_udh_inf_element_t* ie = rp_data->pdu.payload.header;
841                         while(ie) {
842                                 if(ie->identifier == TP_UDH_IE_CONCAT_SM_8BIT_REF)
843                                         return pv_get_uintval(msg, param, res, (unsigned int)ie->concat_sm_8bit_ref.seq);
844                                 ie = ie->next;
845                         }
846                         return -1;
847                 }
848         }
849         return 0;
850 }
851
852 int pv_set_sms(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) {
853         if (param==NULL)
854                 return -1;
855
856         if (!rp_send_data) {
857                 rp_send_data = (sms_rp_data_t*)pkg_malloc(sizeof(struct _sms_rp_data));
858                 if (!rp_send_data) {
859                         LM_ERR("Error allocating %lu bytes!\n", (unsigned long)sizeof(struct _sms_rp_data));
860                         return -1;
861                 }
862                 // Initialize structure:
863                 memset(rp_send_data, 0, sizeof(struct _sms_rp_data));
864         }
865
866         switch(param->pvn.u.isname.name.n) {
867                 case SMS_ALL:
868                         freeRP_DATA(rp_send_data);
869                         // Initialize structure:
870                         memset(rp_send_data, 0, sizeof(struct _sms_rp_data));
871                         break;
872                 case SMS_RPDATA_TYPE:
873                         if (val == NULL) {
874                                 rp_send_data->msg_type = 0;
875                                 return 0;
876                         }
877                         if (!(val->flags & PV_VAL_INT)) {
878                                 LM_ERR("Invalid value type\n");
879                                 return -1;
880                         }
881                         rp_send_data->msg_type = (unsigned char)val->ri;
882                         break;
883                 case SMS_RPDATA_REFERENCE:
884                         if (val == NULL) {
885                                 rp_send_data->reference = 0;
886                                 return 0;
887                         }
888                         if (!(val->flags & PV_VAL_INT)) {
889                                 LM_ERR("Invalid value type\n");
890                                 return -1;
891                         }
892                         rp_send_data->reference = (unsigned char)val->ri;
893                         break;
894                 case SMS_RPDATA_ORIGINATOR:
895                         if (rp_send_data->originator.s) {
896                                 pkg_free(rp_send_data->originator.s);
897                                 rp_send_data->originator.s = 0;
898                                 rp_send_data->originator.len = 0;
899                         }
900                         if (val == NULL)
901                                 return 0;
902                         if (!(val->flags&PV_VAL_STR)) {
903                                 LM_ERR("Invalid type\n");
904                                 return -1;
905                         }
906                         rp_send_data->originator.s = pkg_malloc(val->rs.len);
907                         if(rp_send_data->originator.s==NULL) {
908                                 LM_ERR("no more pkg\n");
909                                 return -1;
910                         }
911                         rp_send_data->originator.len = val->rs.len;
912                         memcpy(rp_send_data->originator.s, val->rs.s, val->rs.len);
913                         break;
914                 case SMS_RPDATA_DESTINATION:
915                         if (rp_send_data->destination.s) {
916                                 pkg_free(rp_send_data->destination.s);
917                                 rp_send_data->destination.s = 0;
918                                 rp_send_data->destination.len = 0;
919                         }
920                         if (val == NULL)
921                                 return 0;
922                         if (!(val->flags&PV_VAL_STR)) {
923                                 LM_ERR("Invalid type\n");
924                                 return -1;
925                         }
926                         rp_send_data->destination.s = pkg_malloc(val->rs.len);
927                         if(rp_send_data->destination.s==NULL) {
928                                 LM_ERR("no more pkg\n");
929                                 return -1;
930                         }
931                         rp_send_data->destination.len = val->rs.len;
932                         memcpy(rp_send_data->destination.s, val->rs.s, val->rs.len);
933                         break;
934                 case SMS_TPDU_TYPE:
935                         if (val == NULL) {
936                                 rp_send_data->pdu.msg_type = 0;
937                                 return 0;
938                         }
939                         if (!(val->flags & PV_VAL_INT)) {
940                                 LM_ERR("Invalid value type\n");
941                                 return -1;
942                         }
943                         rp_send_data->pdu.msg_type = (unsigned char)val->ri;
944                         break;
945
946                 case SMS_TPDU_FLAGS:
947                         if (val == NULL) {
948                                 rp_send_data->pdu.flags = 0;
949                                 return 0;
950                         }
951                         if (!(val->flags & PV_VAL_INT)) {
952                                 LM_ERR("Invalid value type\n");
953                                 return -1;
954                         }
955                         rp_send_data->pdu.flags = (unsigned char)val->ri;
956                         break;
957                 case SMS_TPDU_CODING:
958                         if (val == NULL) {
959                                 rp_send_data->pdu.coding = 0;
960                                 return 0;
961                         }
962                         if (!(val->flags & PV_VAL_INT)) {
963                                 LM_ERR("Invalid value type\n");
964                                 return -1;
965                         }
966                         rp_send_data->pdu.coding = (unsigned char)val->ri;
967                         break;
968                 case SMS_TPDU_PROTOCOL:
969                         if (val == NULL) {
970                                 rp_send_data->pdu.pid = 0;
971                                 return 0;
972                         }
973                         if (!(val->flags & PV_VAL_INT)) {
974                                 LM_ERR("Invalid value type\n");
975                                 return -1;
976                         }
977                         rp_send_data->pdu.pid = (unsigned char)val->ri;
978                         break;
979                 case SMS_TPDU_REFERENCE:
980                         if (val == NULL) {
981                                 rp_send_data->pdu.reference = 0;
982                                 return 0;
983                         }
984                         if (!(val->flags & PV_VAL_INT)) {
985                                 LM_ERR("Invalid value type\n");
986                                 return -1;
987                         }
988                         rp_send_data->pdu.reference = (unsigned char)val->ri;
989                         break;
990                 case SMS_TPDU_VALIDITY:
991                         if (val == NULL) {
992                                 rp_send_data->pdu.validity = 0;
993                                 return 0;
994                         }
995                         if (!(val->flags & PV_VAL_INT)) {
996                                 LM_ERR("Invalid value type\n");
997                                 return -1;
998                         }
999                         rp_send_data->pdu.validity = (unsigned char)val->ri;
1000                         break;
1001                 case SMS_TPDU_PAYLOAD:
1002                         if (rp_send_data->pdu.payload.sm.s) {
1003                                 pkg_free(rp_send_data->pdu.payload.sm.s);
1004                                 rp_send_data->pdu.payload.sm.s = 0;
1005                                 rp_send_data->pdu.payload.sm.len = 0;
1006                         }
1007                         if (val == NULL)
1008                                 return 0;
1009                         if (!(val->flags&PV_VAL_STR)) {
1010                                 LM_ERR("Invalid type\n");
1011                                 return -1;
1012                         }
1013                         rp_send_data->pdu.payload.sm.s = pkg_malloc(val->rs.len);
1014                         if(rp_send_data->pdu.payload.sm.s==NULL) {
1015                                 LM_ERR("no more pkg\n");
1016                                 return -1;
1017                         }
1018                         rp_send_data->pdu.payload.sm.len = val->rs.len;
1019                         memcpy(rp_send_data->pdu.payload.sm.s, val->rs.s, val->rs.len);
1020                         break;
1021                 case SMS_TPDU_DESTINATION:
1022                         if (rp_send_data->pdu.destination.s) {
1023                                 pkg_free(rp_send_data->pdu.destination.s);
1024                                 rp_send_data->pdu.destination.s = 0;
1025                                 rp_send_data->pdu.destination.len = 0;
1026                         }
1027                         if (val == NULL)
1028                                 return 0;
1029                         if (!(val->flags&PV_VAL_STR)) {
1030                                 LM_ERR("Invalid type\n");
1031                                 return -1;
1032                         }
1033                         rp_send_data->pdu.destination.s = pkg_malloc(val->rs.len);
1034                         if(rp_send_data->pdu.destination.s==NULL) {
1035                                 LM_ERR("no more pkg\n");
1036                                 return -1;
1037                         }
1038                         rp_send_data->pdu.destination.len = val->rs.len;
1039                         memcpy(rp_send_data->pdu.destination.s, val->rs.s, val->rs.len);
1040                         break;
1041                 case SMS_TPDU_ORIGINATING_ADDRESS:
1042                         if (rp_send_data->pdu.originating_address.s) {
1043                                 pkg_free(rp_send_data->pdu.originating_address.s);
1044                                 rp_send_data->pdu.originating_address.s = 0;
1045                                 rp_send_data->pdu.originating_address.len = 0;
1046                         }
1047                         if (val == NULL)
1048                                 return 0;
1049                         if (!(val->flags&PV_VAL_STR)) {
1050                                 LM_ERR("Invalid type\n");
1051                                 return -1;
1052                         }
1053                         rp_send_data->pdu.originating_address.s = pkg_malloc(val->rs.len);
1054                         if(rp_send_data->pdu.originating_address.s==NULL) {
1055                                 LM_ERR("no more pkg\n");
1056                                 return -1;
1057                         }
1058                         rp_send_data->pdu.originating_address.len = val->rs.len;
1059                         memcpy(rp_send_data->pdu.originating_address.s, val->rs.s, val->rs.len);
1060                         break;
1061                 case SMS_UDH_CONCATSM_REF: {
1062                         if (val == NULL)
1063                                 return 0;
1064                         if (!(val->flags&PV_VAL_INT)) {
1065                                 LM_ERR("Invalid type\n");
1066                                 return -1;
1067                         }
1068                         struct ie_concat_sm_8bit_ref* concat = GetConcatShortMsg8bitRefIE(rp_data);
1069                         if(concat == NULL)
1070                                 return -1;
1071
1072                         concat->ref = (unsigned char)val->ri;
1073                         break;
1074                 }
1075                 case SMS_UDH_CONCATSM_MAX_NUM_SM: {
1076                         if (val == NULL)
1077                                 return 0;
1078                         if (!(val->flags&PV_VAL_INT)) {
1079                                 LM_ERR("Invalid type\n");
1080                                 return -1;
1081                         }
1082                         struct ie_concat_sm_8bit_ref* concat = GetConcatShortMsg8bitRefIE(rp_data);
1083                         if(concat == NULL)
1084                                 return -1;
1085
1086                         concat->max_num_sm = (unsigned char)val->ri;
1087                         break;
1088                 }
1089                 case SMS_UDH_CONCATSM_SEQ: {
1090                         if (val == NULL)
1091                                 return 0;
1092                         if (!(val->flags&PV_VAL_INT)) {
1093                                 LM_ERR("Invalid type\n");
1094                                 return -1;
1095                         }
1096                         struct ie_concat_sm_8bit_ref* concat = GetConcatShortMsg8bitRefIE(rp_data);
1097                         if(concat == NULL)
1098                                 return -1;
1099
1100                         concat->seq = (unsigned char)val->ri;
1101                         break;
1102                 }
1103         }
1104         return 0;
1105 }
1106
1107 int pv_parse_rpdata_name(pv_spec_p sp, str *in) {
1108         if (sp==NULL || in==NULL || in->len<=0) return -1;
1109
1110         switch(in->len) {
1111                 case 3:
1112                         if (strncmp(in->s, "all", 3) == 0) sp->pvp.pvn.u.isname.name.n = SMS_ALL;
1113                         else goto error;
1114                         break;
1115                 case 4:
1116                         if (strncmp(in->s, "type", 4) == 0) sp->pvp.pvn.u.isname.name.n = SMS_RPDATA_TYPE;
1117                         else goto error;
1118                         break;
1119                 case 9:
1120                         if (strncmp(in->s, "reference", 9) == 0) sp->pvp.pvn.u.isname.name.n = SMS_RPDATA_REFERENCE;
1121                         else goto error;
1122                         break;
1123                 case 10:
1124                         if (strncmp(in->s, "originator", 10) == 0) sp->pvp.pvn.u.isname.name.n = SMS_RPDATA_ORIGINATOR;
1125                         else goto error;
1126                         break;
1127                 case 11:
1128                         if (strncmp(in->s, "destination", 11) == 0) sp->pvp.pvn.u.isname.name.n = SMS_RPDATA_DESTINATION;
1129                         else goto error;
1130                         break;
1131                 default:
1132                         goto error;
1133         }
1134         sp->pvp.pvn.type = PV_NAME_INTSTR;
1135         sp->pvp.pvn.u.isname.type = 0;
1136
1137         return 0;
1138
1139 error:
1140         LM_ERR("unknown uac_req name %.*s\n", in->len, in->s);
1141         return -1;
1142 }
1143
1144 int pv_parse_tpdu_name(pv_spec_p sp, str *in) {
1145         if (sp==NULL || in==NULL || in->len<=0) return -1;
1146
1147         switch(in->len) {
1148                 case 3:
1149                         if (strncmp(in->s, "all", 3) == 0) sp->pvp.pvn.u.isname.name.n = SMS_ALL;
1150                         else goto error;
1151                         break;
1152                 case 4:
1153                         if (strncmp(in->s, "type", 4) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_TYPE;
1154                         else goto error;
1155                         break;
1156                 case 5:
1157                         if (strncmp(in->s, "flags", 5) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_FLAGS;
1158                         else if (strncmp(in->s, "mp_id", 5) == 0) sp->pvp.pvn.u.isname.name.n = SMS_UDH_CONCATSM_REF;
1159                         else goto error;
1160                         break;
1161                 case 6:
1162                         if (strncmp(in->s, "coding", 6) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_CODING;
1163                         else if (strncmp(in->s, "origen", 6) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_ORIGINATING_ADDRESS;
1164                         else goto error;
1165                         break;
1166                 case 7:
1167                         if (strncmp(in->s, "payload", 7) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_PAYLOAD;
1168                         else goto error;
1169                         break;
1170                 case 8:
1171                         if (strncmp(in->s, "protocol", 8) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_PROTOCOL;
1172                         else if (strncmp(in->s, "validity", 8) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_VALIDITY;
1173                         else if (strncmp(in->s, "mp_parts", 8) == 0) sp->pvp.pvn.u.isname.name.n = SMS_UDH_CONCATSM_MAX_NUM_SM;
1174                         else goto error;
1175                         break;
1176                 case 9:
1177                         if (strncmp(in->s, "reference", 9) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_REFERENCE;
1178                         else goto error;
1179                         break;
1180                 case 11:
1181                         if (strncmp(in->s, "destination", 11) == 0) sp->pvp.pvn.u.isname.name.n = SMS_TPDU_DESTINATION;
1182                         else if (strncmp(in->s, "mp_part_num", 11) == 0) sp->pvp.pvn.u.isname.name.n = SMS_UDH_CONCATSM_SEQ;
1183                         else goto error;
1184                         break;
1185                 default:
1186                         goto error;
1187         }
1188         sp->pvp.pvn.type = PV_NAME_INTSTR;
1189         sp->pvp.pvn.u.isname.type = 0;
1190
1191         return 0;
1192
1193 error:
1194         LM_ERR("unknown pdu name %.*s\n", in->len, in->s);
1195         return -1;
1196 }
1197
1198 /*
1199  * Dumps the content of the SMS-Message:
1200  */
1201 int smsdump(struct sip_msg *msg, char *str1, char *str2) {
1202         // Decode the 3GPP-SMS:
1203         if (decode_3gpp_sms(msg) != 1) {
1204                 LM_ERR("Error getting/decoding RP-Data from request!\n");
1205                 return -1;
1206         }
1207
1208         return dumpRPData(rp_data, L_DBG);
1209 }
1210
1211 int isRPDATA(struct sip_msg *msg, char *str1, char *str2) {
1212         // Decode the 3GPP-SMS:
1213         if (decode_3gpp_sms(msg) != 1) {
1214                 LM_ERR("Error getting/decoding RP-Data from request!\n");
1215                 return -1;
1216         }
1217         if ((rp_data->msg_type == RP_DATA_MS_TO_NETWORK) || (rp_data->msg_type == RP_DATA_NETWORK_TO_MS))
1218                 return 1;
1219         else
1220                 return -1;
1221 }
1222