blob: 3809eee24aac971e403e03b2631b6b2283992529 [file] [log] [blame]
ptrkrysik82bd1172015-01-15 09:35:03 +01001/* -*- c++ -*- */
2/* @file
Piotr Krysika6268a52017-08-23 16:02:19 +02003 * @author (C) 2015 by Piotr Krysik <ptrkrysik@gmail.com>
ptrkrysik82bd1172015-01-15 09:35:03 +01004 * @section LICENSE
ptrkrysik03da5c62015-01-18 09:56:26 +01005 *
ptrkrysik82bd1172015-01-15 09:35:03 +01006 * Gr-gsm 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 3, or (at your option)
9 * any later version.
ptrkrysik03da5c62015-01-18 09:56:26 +010010 *
ptrkrysik82bd1172015-01-15 09:35:03 +010011 * Gr-gsm 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.
ptrkrysik03da5c62015-01-18 09:56:26 +010015 *
ptrkrysik82bd1172015-01-15 09:35:03 +010016 * You should have received a copy of the GNU General Public License
17 * along with gr-gsm; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Boston, MA 02110-1301, USA.
ptrkrysik03da5c62015-01-18 09:56:26 +010020 *
ptrkrysik82bd1172015-01-15 09:35:03 +010021 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <gnuradio/io_signature.h>
28#include "tmsi_dumper_impl.h"
ptrkrysik258bbb82015-01-15 11:31:47 +010029#include "grgsm/gsmtap.h"
ptrkrysik03da5c62015-01-18 09:56:26 +010030#include <iostream>
31#include <boost/format.hpp>
ptrkrysik82bd1172015-01-15 09:35:03 +010032
33namespace gr {
ptrkrysik03da5c62015-01-18 09:56:26 +010034namespace gsm {
banjaxbanjo27ca8692015-01-16 09:01:49 +000035
ptrkrysik03da5c62015-01-18 09:56:26 +010036void tmsi_dumper_impl::dump_tmsi(pmt::pmt_t msg)
37{
38 time_t t = time(0);
39 tm *now = localtime(&t);
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010040
ptrkrysik03da5c62015-01-18 09:56:26 +010041 pmt::pmt_t message_plus_header_blob = pmt::cdr(msg);
42 uint8_t * message_plus_header = (uint8_t *)pmt::blob_data(message_plus_header_blob);
43 gsmtap_hdr * header = (gsmtap_hdr *)message_plus_header;
44 uint8_t * m = (uint8_t *)(message_plus_header+sizeof(gsmtap_hdr)); //message content
ptrkrysik82bd1172015-01-15 09:35:03 +010045
ptrkrysik03da5c62015-01-18 09:56:26 +010046 uint8_t msg_len = m[0];
47 uint8_t direction_and_protocol = m[1];
48 uint8_t msg_type = m[2];
ptrkrysik82bd1172015-01-15 09:35:03 +010049
ptrkrysik03da5c62015-01-18 09:56:26 +010050 if( direction_and_protocol == 0x06 && //direction from originating site, transaction id==0, Radio Resouce Management protocol
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010051 (msg_type==0x21 || msg_type==0x22 || msg_type==0x24) //types corresponding to paging requests
52 )
ptrkrysik82bd1172015-01-15 09:35:03 +010053 {
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010054 //write timestamp
ptrkrysik03da5c62015-01-18 09:56:26 +010055 switch(msg_type) {
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010056 case 0x21: //Paging Request Type 1
57 {
58 uint8_t mobile_identity_type = m[5] & 0x07;
59 unsigned int next_element_index = 0; //position of the next element
60 bool found_id_element = false;
61
62 if(mobile_identity_type == 0x04) //identity type: TMSI
ptrkrysik03da5c62015-01-18 09:56:26 +010063 {
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010064 write_tmsi(m+6);
65 dump_file << "-";
66 write_timestamp(now);
67 dump_file << "-0";
68 dump_file << std::endl;
ptrkrysik03da5c62015-01-18 09:56:26 +010069
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010070 next_element_index = 10;
71 found_id_element = true;
72 } else if(mobile_identity_type == 0x01) //identity type: IMSI
73 {
74 dump_file << "0-";
75 write_timestamp(now);
76 dump_file << "-";
77 write_imsi(m+5);
78 dump_file << std::endl;
ptrkrysik03da5c62015-01-18 09:56:26 +010079
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010080 next_element_index = 13;
81 found_id_element = true;
ptrkrysik03da5c62015-01-18 09:56:26 +010082 }
ptrkrysik8c6e6ac2015-01-18 10:00:36 +010083
84 if(found_id_element == true)
85 {
86 //check if there is additional id element
87 uint8_t element_id = m[next_element_index];
88 if((next_element_index < (msg_len+1)) && (element_id == 0x17)) {
89 //check if there is another element
90 uint8_t element_len = m[next_element_index+1];
91 mobile_identity_type = m[next_element_index+2] & 0x07;
92
93 if(mobile_identity_type == 0x04) //identity type: TMSI
94 {
95 write_tmsi(m+next_element_index+3); //write starting from position of the TMSI in the message
96 dump_file << "-";
97 write_timestamp(now);
98 dump_file << "-0";
99 dump_file << std::endl;
100 } else if(mobile_identity_type == 0x01) //identity type: IMSI
101 {
102 dump_file << "0-";
103 write_timestamp(now);
104 dump_file << "-";
105 write_imsi(m+next_element_index+2); //write starting from position of the IMSI in the message
106 dump_file << std::endl;
107 }
108 }
109 int ii;
110 }
111 }
banjaxbanjo9e661ca2015-01-18 10:19:29 +0100112 break;
ptrkrysik8c6e6ac2015-01-18 10:00:36 +0100113 case 0x22: //Paging Request Type 2
114 {
115 uint8_t mobile_identity_type = m[14] & 0x07;
116
117 write_tmsi(m+4);//1st tmsi location
118 dump_file << "-";
119 write_timestamp(now);
120 dump_file << "-0";
121 dump_file << std::endl;
122
123 write_tmsi(m+8);//2nd tmsi location
124 dump_file << "-";
125 write_timestamp(now);
126 dump_file << "-0";
127 dump_file << std::endl;
128
129 if(mobile_identity_type == 0x04) //identity type: TMSI
130 {
131 write_tmsi(m+15);
132 dump_file << "-";
133 write_timestamp(now);
134 dump_file << "-0";
135 dump_file << std::endl;
136
137 } else if(mobile_identity_type == 0x01) //identity type: IMSI
138 {
139 dump_file << "0-";
140 write_timestamp(now);
141 dump_file << "-";
142 write_imsi(m+14);
143 dump_file << std::endl;
144
145 }
146 }
147 break;
148 case 0x24: //Paging Request Type 3
149 {
150 int TMSI_INDEX[4] = {4,8,12,16}; // indexes of the 4 tmsi's
151
152 for(int x =0; x < 4; x++)
153 {
154 write_tmsi(m+TMSI_INDEX[x]);
155 dump_file << "-";
156 write_timestamp(now);
157 dump_file << "-0";
158 dump_file << std::endl;
159 }
160
161 }
162 break;
ptrkrysik03da5c62015-01-18 09:56:26 +0100163 }
ptrkrysik82bd1172015-01-15 09:35:03 +0100164 }
ptrkrysik03da5c62015-01-18 09:56:26 +0100165}
166
167inline void tmsi_dumper_impl::write_timestamp(tm * now)
168{
169 dump_file << boost::format("%d%02d%02d%02d%02d%02d")
ptrkrysik8c6e6ac2015-01-18 10:00:36 +0100170 % (now->tm_year + 1900-2000) //year -2000 here after the 1900 leaves you with 15 instead of 2015 (delivery reports format is 150112223501)
171 % (now->tm_mon + 1) //month
172 % now->tm_mday //day
173 % now->tm_hour % now->tm_min % now->tm_sec; //time of day
ptrkrysik03da5c62015-01-18 09:56:26 +0100174 return;
175}
176
177inline int swap(uint8_t c)
178{
179 uint8_t temp1, temp2;
180 temp1 = c & 0x0F;
181 temp2 = c & 0xF0;
182 temp1=temp1 << 4;
ptrkrysik8c6e6ac2015-01-18 10:00:36 +0100183 temp2=temp2 >> 4;
ptrkrysik03da5c62015-01-18 09:56:26 +0100184 return(temp2|temp1);
185}
186
187void tmsi_dumper_impl::write_imsi(uint8_t * imsi)
188{
189 dump_file << boost::format("%1x%02x%02x%02x%02x%02x%02x%02x")
190 % (swap(imsi[0]) & 0x0f)
191 % swap(imsi[1]) % swap(imsi[2]) % swap(imsi[3]) % swap(imsi[4]) % swap(imsi[5]) % swap(imsi[6]) % swap(imsi[7]);
192 return;
193}
194
195void tmsi_dumper_impl::write_tmsi(uint8_t * tmsi)
196{
197 dump_file << boost::format("%02x%02x%02x%02x")
198 % (int)tmsi[0] % (int)tmsi[1] % (int)tmsi[2] % (int)tmsi[3];
199 return;
200}
201
202
203tmsi_dumper::sptr
204tmsi_dumper::make()
205{
206 return gnuradio::get_initial_sptr
207 (new tmsi_dumper_impl());
208}
209
210/*
211 * The private constructor
212 */
213tmsi_dumper_impl::tmsi_dumper_impl()
214 : gr::block("tmsi_dumper",
215 gr::io_signature::make(0, 0, 0),
216 gr::io_signature::make(0, 0, 0))
217{
ptrkrysik8c6e6ac2015-01-18 10:00:36 +0100218 dump_file.open("tmsicount.txt", std::ios_base::app);
ptrkrysik03da5c62015-01-18 09:56:26 +0100219 message_port_register_in(pmt::mp("msgs"));
Vadim Yanitskiya1169202021-05-03 19:00:43 +0200220 set_msg_handler(pmt::mp("msgs"), boost::bind(&tmsi_dumper_impl::dump_tmsi, this, boost::placeholders::_1));
ptrkrysik03da5c62015-01-18 09:56:26 +0100221}
222
223/*
224 * Our virtual destructor.
225 */
226tmsi_dumper_impl::~tmsi_dumper_impl()
227{
228 dump_file.close();
229}
230} /* namespace gsm */
ptrkrysik82bd1172015-01-15 09:35:03 +0100231} /* namespace gr */
232