aboutsummaryrefslogtreecommitdiff
path: root/misc/tde.c
blob: 97590f66e801e43515dbb10f69eaf59ca2f16fc6 (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
326
327
328
329
330
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <CSTAapdu.h>
#include <CSTAServices.h>
#include <malloc.h>
#include <CSTAEventReportArgument.h>
#include <MonitorStartArgument.h>

/* Определяем порт и адрес АТС. */
#define PORTNUM 33333
#define SERVER "192.168.0.101"
int s;
int write_out(const void *buffer, size_t size, void *app_key);

typedef struct buffer_s {
	char	*buf;
	int	len;
} buffer_t;

void parseEventReport(char *data, int length)
{
	CSTAEventReportArgument_t *rep = 0; /* Type to decode */
	asn_dec_rval_t rval; /* Decoder return value	*/
	size_t size;		 /* Number of bytes read	*/
 
	/* Decode the input buffer */
	rval = ber_decode(0, &asn_DEF_CSTAEventReportArgument,
		(void **)&rep, data, length);
	if(rval.code != RC_OK)
		fprintf(stderr, "Broken encoding at byte %ld\n", (long)rval.consumed);
 
	/* Print the decoded Rectangle type as XML */
	xer_fprint(stdout, &asn_DEF_CSTAEventReportArgument, rep);
 
	return 0; /* Decoding finished successfully */
}

void dump(unsigned char *buf, int len) {
    int i, j;

    for (i = 0; i < len; i+=16) {
        for (j = 0; j < 16; j++)
            if (i+j < len)
            printf("%02X ", (unsigned char)buf[i+j]);
            else
            printf("   ");
            for (j = 0; j < 16; j++)
            if (i+j < len) {
            if (buf[i+j] >= 0x20)
                printf("%c", (unsigned char)buf[i+j]);
            else
                printf(".");
        }
        printf("\n");
    }
}

void *memmem(const void *haystack, size_t haystacklen,
                     const void *needle, size_t needlelen)
{
    register const char *pn;
    register const char *ph;
    const char *plast;
    size_t n;
 
    if (needlelen == 0) {
            return (void *) haystack;
    }
 
    if (haystacklen >= needlelen) {
            ph = (const char *) haystack;
            pn = (const char *) needle;
            plast = ph + (haystacklen - needlelen);
 
            do {
                    n = 0;
                    while (ph[n] == pn[n]) {
                            if (++n == needlelen) {
                                    return (void *) ph;
                            }
                    }
            } while (++ph <= plast);
    }
 
    return NULL;
}

#define	READ	0
#define WRITE	1

void timestamp(int dir)
{
	time_t	t;
	struct tm *l;
	
	t = time(NULL);
	l = localtime(&t);
	printf("\n[%02d:%02d:%02d %02d.%02d.%4d] ", l->tm_hour, l->tm_min, l->tm_sec, l->tm_mday, l->tm_mon+1, l->tm_year+1900);
	
	if (dir == READ)
		printf("---- PBX -> PC ----\n");
	else
		printf("---- PC -> PBX ----\n");
}

void pktdump(char *buf, int len)
{
	int	i, j;
	char	id[256], *cp, type, tmp[256], *c, *orig;
	long	seq, l, origlen;
	CSTAapdu_t *apdu = 0; /* Type to decode */
	RORSapdu_t *rors;
	asn_dec_rval_t rval; /* Decoder return value	*/
	size_t size;		 /* Number of bytes read	*/
	buffer_t *out;
	
	buf += 2;
	len -= 2;
	timestamp(READ);
 
	/* Decode the input buffer */
	rval = ber_decode(0, &asn_DEF_CSTAapdu,	(void **)&apdu, buf, len);
	if(rval.code != RC_OK) {
		fprintf(stderr, "Broken encoding at byte %ld\n", (long)rval.consumed);
		return 1; 
	}
	switch (apdu->present) {
		case CSTAapdu_PR_svcRequest:
			printf("invokeID %d, serviceID %d\n", apdu->choice.svcRequest.invokeID, apdu->choice.svcRequest.serviceID);
			switch (apdu->choice.svcRequest.serviceID) {
 				case 211:
					printf("SystemStatus Request\n");
					//send SystemStatus responce
					rors = calloc(1, sizeof(RORSapdu_t));
					rors->invokeID = apdu->choice.svcRequest.invokeID;
					rors->result.serviceID = apdu->choice.svcRequest.serviceID;
					
					out = calloc(1, sizeof(buffer_t));
					out->buf = malloc(1024);
					der_encode(&asn_DEF_RORSapdu, rors, write_out, out);
					timestamp(WRITE);
					printf("invokeID %d, serviceID %d\n", rors->invokeID, rors->result.serviceID);					
					printf("SystemStatus Responce\n");
					//xer_fprint(stdout, &asn_DEF_RORSapdu, rors);
					
					memmove(out->buf+2, out->buf, out->len);
					out->buf[0] = 0x00;
					out->buf[1] = out->len;
					out->len += 2;
					send(s, out->buf, out->len, 0);
					free(out->buf);
					break;
				case CSTAServices_cSTAeventReportSID:
					parseEventReport(apdu->choice.svcRequest.serviceArgs.buf, apdu->choice.svcRequest.serviceArgs.size);
					break;
				default:			
					printf("Unknown svcRequest:\n");
					dump(apdu->choice.svcRequest.serviceArgs.buf, apdu->choice.svcRequest.serviceArgs.size);
					break;
			}
			break;
		case CSTAapdu_PR_svcResult:
		case CSTAapdu_PR_svcError:
		case CSTAapdu_PR_svcReject:
		default:
			xer_fprint(stdout, &asn_DEF_CSTAapdu, apdu);
			break;
	}
}

int write_out(const void *buffer, size_t size, void *app_key) 
{
	buffer_t *dest = app_key;
	u_char *buf = buffer;
	int i;

	for (i = 0; i < size; i++)
		dest->buf[dest->len+i] = buf[i];
	dest->len += size;
	return 0;
}

int sendID = 1;
                       
void start_monitor(char *numer)
{
	ROIVapdu_t *pdu;
	MonitorStartArgument_t *rep;
	int i;
	buffer_t *buf;
	
	rep = calloc(1, sizeof(MonitorStartArgument_t));
	rep->monitorObject.present = CSTAObject_PR_deviceObject;
	rep->monitorObject.choice.deviceObject.deviceIdentifier.present = DeviceID__deviceIdentifier_PR_dialingNumber;
	rep->monitorObject.choice.deviceObject.deviceIdentifier.choice.dialingNumber.buf = numer;
	rep->monitorObject.choice.deviceObject.deviceIdentifier.choice.dialingNumber.size = strlen(numer);

	pdu = calloc(1, sizeof(ROIVapdu_t));
	pdu->invokeID = sendID++;
	pdu->serviceID = CSTAServices_monitorStartSID;
	pdu->serviceArgs.buf = malloc(1024);

	buf = calloc(1, sizeof(buffer_t));
	buf->buf = pdu->serviceArgs.buf;
	der_encode(&asn_DEF_MonitorStartArgument, rep, write_out, buf);
	pdu->serviceArgs.size = buf->len;

	buf->len = 0;
	buf->buf = malloc(1024);
	der_encode(&asn_DEF_ROIVapdu, pdu, write_out, buf);

	timestamp(WRITE);
	xer_fprint(stdout, &asn_DEF_MonitorStartArgument, rep);
	
	memmove(buf->buf+2, buf->buf, buf->len);
	buf->buf[0] = 0x00;
	buf->buf[1] = buf->len;
	buf->len += 2;
	send(s, buf->buf, buf->len, 0);

	free(buf->buf);
	free(pdu->serviceArgs.buf);
	//read_pkt();
}

int read_pkt(int s, unsigned char *buf)
{
    int nbytes, total = 0, length;
    while (total < 2 )
    {
		nbytes = recv( s, &buf[total], 2-total, 0);
		if (nbytes == 0) return 0; // соединение закрыто удаленно
		if (nbytes < 0 ) return -1;// ошибка чтения из сокета
		total += nbytes;
	}
	if (buf[0] != 0) return -2; // испорченный пакет
	if (buf[1] == 0) return 2; // прочли 2 байта - пустой пакет
	length = ((unsigned char *) buf)[1] + 2;
	while (total < length)
    {
		nbytes = recv( s, &buf[total], length - total, 0);
		if (nbytes == 0) return 0; // соединение закрыто удаленно
		if (nbytes < 0 ) return -1; // ошибка чтения из сокета
		total += nbytes;
	}
	return total;
}
main(argc,argv)
	int argc;
	char *argv[];
{
	int nbytes;
	int pid;
	int nport;

  /* Начало программы. */
	struct sockaddr_in serv_addr;
	unsigned char buf[242]={0x00, 0x25, 0x60, 0x23, 0x80, 0x02, 0x07, 0x80, 0xA1, 0x07, 0x06, 0x05, 0x2B, 0x0C, 0x00, 0x81, 0x5A, 0xBE, 0x14, 0x28, 0x12, 0x06, 0x07, 0x2B, 0x0C, 0x00, 0x82, 0x1D, 0x81, 0x48, 0xA0, 0x07, 0xA0, 0x05, 0x03, 0x03, 0x00, 0x08, 0x00};
	int addrlen;
	unsigned long addr;

	nport=htons((u_short)PORTNUM);

	if( (s=socket(AF_INET, SOCK_STREAM, 0) ) == -1 ) {
		perror("Error calling socket()");
		return 1;
	}

  /* В serv_addr заносим адрес и порт АТС. */
	bzero(&serv_addr, sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=inet_addr(SERVER);
	serv_addr.sin_port=nport;
 
	if( connect(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr) ) == -1) {
		perror("Error calling connect()"); 
		return 1;
    }
    
//посылаем #A-ASSOCIATE Request
//  dump(buf, 39);
    send(s, buf, 39, 0);
  
 // Получаем ответ от АТС. 
    nbytes=recv(s, buf, sizeof(buf), 0);
 // printf("%d", nbytes)
    dump(buf, nbytes);   
  
    if (nbytes<0) {
		perror("Error calling recv()"); 
		return 1;
    }
   //проверяем на rejected-permanent
    const unsigned char seek[] = {0xA2, 0x03, 0x02, 0x01, 0x01};
    unsigned char *off_ptr = memmem (buf, sizeof(buf), seek, sizeof(seek));
    if (off_ptr) { 
		printf("rejected-permanent\n");
		close(s);
		return 0;
    }
   //шлём #StartMonitor
	start_monitor("101");
	start_monitor("102");
	start_monitor("111");
	start_monitor("112");
	start_monitor("113");
	start_monitor("114");
	start_monitor("115");
	
	while (1) { //цикл
		nbytes = read_pkt (s, buf);
		if (nbytes<0) {
			perror("Error calling recv()"); 
			return 1;
		} else if (nbytes==0) {
			printf("socket closed\n");
			return 1;
		} 
	//	dump(buf, nbytes);
		pktdump(buf, nbytes);
	}
    close(s);
    return 0;
}