blob: 6d2a8c9bfd12ac75e430c42cfc8faea301ad862e [file] [log] [blame]
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +02001/*
2 * (C) 2010 by Holger Hans Peter Freyther
3 * (C) 2010 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010022#include <osmocom/gsm/gsm0480.h>
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27static const uint8_t ussd_request[] = {
28 0x0b, 0x7b, 0x1c, 0x15, 0xa1, 0x13, 0x02, 0x01,
29 0x03, 0x02, 0x01, 0x3b, 0x30, 0x0b, 0x04, 0x01,
30 0x0f, 0x04, 0x06, 0x2a, 0xd5, 0x4c, 0x16, 0x1b,
31 0x01, 0x7f, 0x01, 0x00
32};
33
34static int parse_ussd(const uint8_t *_data, int len)
35{
36 uint8_t *data;
37 int rc;
38 struct ussd_request req;
39 struct gsm48_hdr *hdr;
40
41 data = malloc(len);
42 memcpy(data, _data, len);
43 hdr = (struct gsm48_hdr *) &data[0];
44 rc = gsm0480_decode_ussd_request(hdr, len, &req);
45 free(data);
46
47 return rc;
48}
49
Holger Hans Peter Freytherc88a44f2010-10-11 08:21:00 +020050static int parse_mangle_ussd(const uint8_t *_data, int len)
51{
52 uint8_t *data;
53 int rc;
54 struct ussd_request req;
55 struct gsm48_hdr *hdr;
56
57 data = malloc(len);
58 memcpy(data, _data, len);
59 hdr = (struct gsm48_hdr *) &data[0];
60 hdr->data[1] = len - sizeof(*hdr) - 2;
61 rc = gsm0480_decode_ussd_request(hdr, len, &req);
62 free(data);
63
64 return rc;
65}
66
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020067int main(int argc, char **argv)
68{
Holger Hans Peter Freytherf6323712010-10-11 08:49:27 +020069 struct ussd_request req;
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020070 const int size = sizeof(ussd_request);
71 int i;
72
Holger Hans Peter Freytherf6323712010-10-11 08:49:27 +020073 gsm0480_decode_ussd_request((struct gsm48_hdr *) ussd_request, size, &req);
74 printf("Tested if it still works. Text was: %s\n", req.text);
75
76
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020077 printf("Testing parsing a USSD request and truncated versions\n");
78
79 for (i = size; i > sizeof(struct gsm48_hdr); --i) {
80 int rc = parse_ussd(&ussd_request[0], i);
81 printf("Result for %d is %d\n", rc, i);
82 }
83
Holger Hans Peter Freytherc88a44f2010-10-11 08:21:00 +020084 printf("Mangling the container now\n");
85 for (i = size; i > sizeof(struct gsm48_hdr) + 2; --i) {
86 int rc = parse_mangle_ussd(&ussd_request[0], i);
87 printf("Result for %d is %d\n", rc, i);
88 }
89
Holger Hans Peter Freytherdaa653f2010-10-11 07:56:06 +020090 return 0;
91}