summaryrefslogtreecommitdiff
path: root/src/pdu.c
blob: 044a87f78fe677551751fa3bb18cc47b9b5395b9 (plain)
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
/*
 * PDU common
 *
 * Copyright (C) 2010 by James Maki
 *
 * Author: James Maki <jamescmaki@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>

#include "config.h"
#include "log.h"
#include "pdu.h"
#include "sms_utils.h"

char strGSMTable[GSM_TABLE_SIZE] = {
        '@','£','$','¥','è','é','ù','ì','ò','Ç','\n','Ø','ø','\r','Å','å',
        'Δ','_','Φ','Γ','Λ','Ω','Π','Ψ','Σ','Θ','Ξ','\x1B','Æ','æ','ß','É',
        ' ','!','\"','#','¤','%','&','\'','(',')','*','+',',','-','.','/',
        '0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?',
        '¡','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
        'P','Q','R','S','T','U','V','W','X','Y','Z','Ä','Ö','Ñ','Ü','\xA7',
        '¿','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
        'p','q','r','s','t','u','v','w','x','y','z','ä','ö','ñ','ü','à'};
char strExtendedTable[GSM_TABLE_SIZE] = {
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ','^',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ','`','{','}',' ',' ',' ',' ',' ','\\',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','[','~',']',' ',
        '|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ','€',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
        ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};

int hex_nibble_scan(const char *buf, size_t len)
{
	static const char digits[] = "0123456789ABCDEF";

	int i;
	int result = 0;

	STRLEN_CHECK(buf, len, -1);

	for (i = 0; i < len; i++) {
		const char *where;

		where = strchr(digits, toupper(buf[i]));
		if (!where) {
			return -1;
		}

		result = (result << 4) + (where - digits);
	}

	return result;
}

int hex_byte_decode(const char *buf)
{
	return hex_nibble_scan(buf, HEX_BYTE_LEN);
}

int hex_byte_encode(char *buf, size_t len, int byte)
{
	if (len < HEX_BYTE_LEN) {
		log_error("buffer is not large enough");
	}

	return snprintf(buf, len, "%02X", byte & 0xFF);
}

int nibble_swap(char *buf, size_t len)
{
	int i;
	char c;

	if (len & 1) {
		BUG("odd buffer size found");
	}

	for (i = 0; i < len; i += HEX_BYTE_LEN) {
		c = buf[i];
		buf[i] = buf[i + 1];
		buf[i + 1] = c;
	}

	return i;
}

int strunpad(char *str, unsigned char c)
{
	char *cp = strchr(str, c);

	if (cp) {
		*cp = '\0';
	}

	return strlen(str);
}

int strpad(char *str, size_t len, unsigned char c)
{
	int i;
	int start = strlen(str);

	for (i = start; i < len; i++) {
		str[i] = c;
	}

	str[i] = '\0';

	return i - start;
}

int pdu_format_timestamp(struct pdu_info *pdu, char *str, size_t len, const char *fmt)
{
	if (len <= 0) {
		return 0;
	}

	switch (pdu->type.msg_type) {
	case PDU_MTI_DELIVER:
	case PDU_MTI_REPORT:
		return strftime(str, len, fmt, &pdu->timestamp);
	default:
		*str = '\0';
		return 0;
	}
}

int pdu_format_vp(struct pdu_info *pdu, char *str, size_t len)
{
	int val;

	if (len <= 0) {
		return 0;
	}

	*str = '\0';

	val = pdu->validity_period;

	switch (pdu->type.msg_type) {
	case PDU_MTI_SUBMIT:
		if (val >= 0 && val <= 143) {
			val = (val + 1) * 5;
		} else if (val >= 144 && val <= 167) {
			val = ((val - 143) * 30) + (12 * 60);
		} else if (val >= 168 && val <= 196) {
			val = (val - 166) * (24 * 60);
		} else {
			val = (val - 192) * (7 * 24 * 60);
		}

		return snprintf(str, len, "%d", val);
	default:
		return 0;
	}
}

int pdu_addr_check(const char *addr_str)
{
	if (addr_str[0] == '+') {
		addr_str++;
	}

	if (!*addr_str) {
		log_error("empty addr");
		return -1;
	}

	int len = strlen(addr_str);
	int bad = strcspn(addr_str, "1234567890");

	if (bad != len) {
		log_error("bad character in addr at offset %d", bad);
		return -1;
	}

	return 0;
}

#define GWRITTEN_SEP(c) ((c) == '-' || (c) == '.')
#define NUM_ADDR "1234567890-."
#define CMD_ADDR "1234567890*#+-."

int pdu_addr_type_infer(const char *str)
{
	if (*str == '+' && strlen(str + 1) == strspn(str + 1, NUM_ADDR)) {
		log_debug("SMS_ADDR_GLOBAL");
		return SMS_ADDR_GLOBAL;
	} else if (strlen(str) == strspn(str, NUM_ADDR)) {
		log_debug("SMS_ADDR_LOCAL");
		return SMS_ADDR_LOCAL;
	} else if (strlen(str) == strspn(str, CMD_ADDR)) {
		log_debug("SMS_ADDR_CMD");
		return SMS_ADDR_CMD;
	} else {
		log_debug("SMS_ADDR_TEXT");
		return SMS_ADDR_TEXT;
	}
}

static int pdu_addr_clean_copy(struct pdu_addr *addr, const char *src)
{
	char *dest = addr->addr;
	int count = 0;
	int c;

	while((c = *src++)) {
		if(isdigit(c)) {
			if(count >= PDU_ADDR_SIZE - 1) {
				log_error("addr exceeds max length");
				return -1;
			}
			dest[count++] = c;
		} else if(GWRITTEN_SEP(c)) {
			continue;
		} else if(c == '+' && !count) {
			continue;
		} else {
			log_error("addr contains invalid char %c at offset %d", c, count);
			return -1;
		}
	}

	dest[count] = '\0';

	if(!count) {
		log_error("empty addr");
		return -1;
	}

	return count;
}

int pdu_addr_fill(struct pdu_addr *addr, const char *addr_str, int type)
{
	int tmp;

	memset(addr, 0 , sizeof(*addr));

	addr->type = pdu_addr_type_infer(addr_str);

	tmp = pdu_addr_clean_copy(addr, addr_str);
	if (tmp < 0) {
		return tmp;
	}

	log_debug("addr: %s", addr->addr);

	if (type) {
		addr->type = type;
	}

	log_debug("addr-type: 0x%02X", addr->type);

	return tmp;
}

int pdu_user_data_read(int fd, struct pdu_info *pdu)
{
	int err;
	char c;
	int total = 0;
	int len;

	if (pdu->data_coding.general.alphabet == PDU_ALPHABET_DEFAULT) {
		len = PDU_UD_7BIT_MAX;
	} else if (pdu->data_coding.general.alphabet == PDU_ALPHABET_EIGHT) {
		len = PDU_UD_8BIT_MAX;
	} else {
		return -1;
	}

	while (1) {
		err = read(fd, &c, 1);
		if (err < 0) {
			log_error("read failed: %m");
			return err;
		} else if (err > 0) {
			if (total < len) {
				pdu->user_data[total++] = c;
			} else {
				log_debug("read max message length %d", total);
				goto done;
			}
		} else {
			log_debug("read eof at %d", total);
			goto done;
		}
	}

done:

	pdu->user_data[total] = '\0';
	pdu->user_data_len = total;

	debug_buffer("message is: ", pdu->user_data, total);

	return total;
}