annotate gen-pdu/message.c @ 12:0fe95ca922c7

sms-gen-tpdu: check the high bit of GSM7 msg input
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 05 Aug 2023 16:55:24 +0000
parents 17dd30989c0b
children d9d722033ff1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements TPDU encoding of actual messages, after all
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * settings have been captured.
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <string.h>
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <strings.h>
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "error.h"
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern int dir_mo, include_sca;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern u_char sc_addr[12], user_addr[12];
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern u_char mr_byte, pid_byte, dcs_byte;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern u_char scts_buf[7];
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 extern int is_septet, scts_is_set;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 extern int input_lineno;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static void
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 emit_first_octet(udhi)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 u_char fo;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (dir_mo)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 fo = 1;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 else
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 fo = 0;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (udhi)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 fo |= 0x40;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 printf("%02X", fo);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 static void
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 make_pdu(udl, ud_buf, ud_octets, udhi)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 u_char *ud_buf;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 unsigned udl, ud_octets;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (include_sca)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 emit_hex_out(sc_addr, sc_addr[0] + 1, stdout);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 emit_first_octet(udhi);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (dir_mo)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 printf("%02X", mr_byte);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 emit_hex_out(user_addr, ((user_addr[0] + 1) >> 1) + 2, stdout);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 printf("%02X", pid_byte);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 printf("%02X", dcs_byte);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (!dir_mo) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (!scts_is_set)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 set_auto_scts();
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 emit_hex_out(scts_buf, 7, stdout);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 printf("%02X", udl);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 emit_hex_out(ud_buf, ud_octets, stdout);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 putchar('\n');
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 static void
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 cmd_msg_common(arg, udhi)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 char *arg;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 u_char input[160], ud7[160], octbuf[140];
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 unsigned input_len, udhl, udhl1, udh_chars, plain_chars;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 unsigned udl, udl_octets;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 for (input_len = 0; ; input_len++) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 while (isspace(*arg))
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 arg++;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (!*arg)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 break;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 fprintf(stderr, ERR_PREFIX "invalid hex string\n",
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 input_lineno);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 exit(1);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (input_len >= 160) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 toolong: fprintf(stderr, ERR_PREFIX "hex string is too long\n",
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 input_lineno);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 exit(1);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 input[input_len] = (decode_hex_digit(arg[0]) << 4) |
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 decode_hex_digit(arg[1]);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 arg += 2;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (!is_septet && input_len > 140)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 goto toolong;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 if (udhi) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 if (!input_len) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 fprintf(stderr, ERR_PREFIX
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 "empty message is invalid with UDHI\n",
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 input_lineno);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 exit(1);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 udhl = input[0];
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 udhl1 = udhl + 1;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if (udhl1 > input_len) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 fprintf(stderr, ERR_PREFIX "UDHL exceeds UD length\n",
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 input_lineno);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 exit(1);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 if (!is_septet) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 make_pdu(input_len, input, input_len, udhi);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 return;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 if (udhi)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 udh_chars = (udhl1 * 8 + 6) / 7;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 else {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 udhl1 = 0;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 udh_chars = 0;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 plain_chars = input_len - udhl1;
12
0fe95ca922c7 sms-gen-tpdu: check the high bit of GSM7 msg input
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
114 if (check_high_bit(input + udhl1, plain_chars) < 0) {
0fe95ca922c7 sms-gen-tpdu: check the high bit of GSM7 msg input
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
115 fprintf(stderr, ERR_PREFIX "high bit set in GSM7 data\n",
0fe95ca922c7 sms-gen-tpdu: check the high bit of GSM7 msg input
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
116 input_lineno);
0fe95ca922c7 sms-gen-tpdu: check the high bit of GSM7 msg input
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
117 exit(1);
0fe95ca922c7 sms-gen-tpdu: check the high bit of GSM7 msg input
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
118 }
9
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 udl = udh_chars + plain_chars;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 if (udl > 160) {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 fprintf(stderr, ERR_PREFIX
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 "message exceeds 160 septets after UDH\n",
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 input_lineno);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 exit(1);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 udl_octets = (udl * 7 + 7) / 8;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 bzero(ud7, 160);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 bcopy(input + udhl1, ud7 + udh_chars, plain_chars);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 gsm7_pack(ud7, octbuf, udl_octets);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 if (udhi)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 bcopy(input, octbuf, udhl1);
10
17dd30989c0b sms-gen-tpdu: fix bug in GSM7 data source
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
132 make_pdu(udl, octbuf, udl_octets, udhi);
9
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 void
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 cmd_msg_plain(argc, argv)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 char **argv;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 cmd_msg_common(argv[1], 0);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 }
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 void
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 cmd_msg_udh(argc, argv)
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 char **argv;
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 {
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 cmd_msg_common(argv[1], 1);
003660a57f99 new program sms-gen-tpdu
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 }