blob: adb687176226d5ef905c9fca6e18ee14de776595 [file] [log] [blame]
ptrkrysik82bd1172015-01-15 09:35:03 +01001/* -*- c++ -*- */
2/* @file
3 * @author Piotr Krysik <ptrkrysik@gmail.com>
4 * @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);
ptrkrysik258bbb82015-01-15 11:31:47 +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
51 (msg_type==0x21 || msg_type==0x22 || msg_type==0x24) //types corresponding to paging requests
52 )
ptrkrysik82bd1172015-01-15 09:35:03 +010053 {
ptrkrysik03da5c62015-01-18 09:56:26 +010054 switch(msg_type) {
55 case 0x21: //Paging Request Type 1
56 {
57 uint8_t mobile_identity_type = m[5] & 0x07;
58 unsigned int next_element = 0; //position of the next element
59 bool found_id_element = false;
60
61 if(mobile_identity_type == 0x04) //identity type: TMSI
62 {
63 write_tmsi(m+6);
64 dump_file << "-";
65 write_timestamp(now);
66 dump_file << "-0";
67 dump_file << std::endl;
68
69 next_element = 10;
70 found_id_element = true;
71 } else
72 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;
79
80 next_element = 13;
81 found_id_element = true;
82 }
83
84 if(found_id_element == true)
85 {
86 //check if there is additional id element
87 uint8_t element_id = m[next_element];
88 if((next_element < (msg_len+1)) && (element_id == 0x17)){
89 uint8_t element_len = m[next_element+1];
90 mobile_identity_type = m[next_element+2] & 0x07;
91 if(mobile_identity_type == 0x04) //identity type: TMSI
92 {
93 write_tmsi(m+next_element+3); //write starting from position of the TMSI in the message
94 dump_file << "-";
95 write_timestamp(now);
96 dump_file << "-0";
97 dump_file << std::endl;
98 } else
99 if(mobile_identity_type == 0x01) //identity type: IMSI
100 {
101 dump_file << "0-";
102 write_timestamp(now);
103 dump_file << "-";
104 write_imsi(m+next_element+2); //write starting from position of the IMSI in the message
105 dump_file << std::endl;
106 }
107 }
108 int ii;
109 }
110 }
111 break;
112 case 0x22: //Paging Request Type 2
113 break;
114 case 0x24: //Paging Request Type 3
115 break;
116 }
ptrkrysik82bd1172015-01-15 09:35:03 +0100117 }
ptrkrysik03da5c62015-01-18 09:56:26 +0100118}
119
120inline void tmsi_dumper_impl::write_timestamp(tm * now)
121{
122 dump_file << boost::format("%d%02d%02d%02d%02d%02d")
123 % (now->tm_year + 1900) //year
124 % (now->tm_mon + 1) //month
125 % now->tm_mday //day
126 % now->tm_hour % now->tm_min % now->tm_sec; //time of day
127 return;
128}
129
130inline int swap(uint8_t c)
131{
132 uint8_t temp1, temp2;
133 temp1 = c & 0x0F;
134 temp2 = c & 0xF0;
135 temp1=temp1 << 4;
136 temp2=temp2 >> 4;
137 return(temp2|temp1);
138}
139
140void tmsi_dumper_impl::write_imsi(uint8_t * imsi)
141{
142 dump_file << boost::format("%1x%02x%02x%02x%02x%02x%02x%02x")
143 % (swap(imsi[0]) & 0x0f)
144 % swap(imsi[1]) % swap(imsi[2]) % swap(imsi[3]) % swap(imsi[4]) % swap(imsi[5]) % swap(imsi[6]) % swap(imsi[7]);
145 return;
146}
147
148void tmsi_dumper_impl::write_tmsi(uint8_t * tmsi)
149{
150 dump_file << boost::format("%02x%02x%02x%02x")
151 % (int)tmsi[0] % (int)tmsi[1] % (int)tmsi[2] % (int)tmsi[3];
152 return;
153}
154
155
156tmsi_dumper::sptr
157tmsi_dumper::make()
158{
159 return gnuradio::get_initial_sptr
160 (new tmsi_dumper_impl());
161}
162
163/*
164 * The private constructor
165 */
166tmsi_dumper_impl::tmsi_dumper_impl()
167 : gr::block("tmsi_dumper",
168 gr::io_signature::make(0, 0, 0),
169 gr::io_signature::make(0, 0, 0))
170{
171 dump_file.open("tmsicount.txt", std::ios_base::out);
172 message_port_register_in(pmt::mp("msgs"));
173 set_msg_handler(pmt::mp("msgs"), boost::bind(&tmsi_dumper_impl::dump_tmsi, this, _1));
174}
175
176/*
177 * Our virtual destructor.
178 */
179tmsi_dumper_impl::~tmsi_dumper_impl()
180{
181 dump_file.close();
182}
183} /* namespace gsm */
ptrkrysik82bd1172015-01-15 09:35:03 +0100184} /* namespace gr */
185