ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* @file |
| 3 | * @author Piotr Krysik <ptrkrysik@gmail.com> |
| 4 | * @section LICENSE |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 5 | * |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 6 | * 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. |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 10 | * |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 11 | * 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. |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 15 | * |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 16 | * 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. |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 20 | * |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 21 | */ |
| 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" |
ptrkrysik | 258bbb8 | 2015-01-15 11:31:47 +0100 | [diff] [blame] | 29 | #include "grgsm/gsmtap.h" |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 30 | #include <iostream> |
| 31 | #include <boost/format.hpp> |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 32 | |
| 33 | namespace gr { |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 34 | namespace gsm { |
banjaxbanjo | 27ca869 | 2015-01-16 09:01:49 +0000 | [diff] [blame] | 35 | |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 36 | void tmsi_dumper_impl::dump_tmsi(pmt::pmt_t msg) |
| 37 | { |
| 38 | time_t t = time(0); |
| 39 | tm *now = localtime(&t); |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 40 | |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 41 | 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 |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 45 | |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 46 | uint8_t msg_len = m[0]; |
| 47 | uint8_t direction_and_protocol = m[1]; |
| 48 | uint8_t msg_type = m[2]; |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 49 | |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 50 | if( direction_and_protocol == 0x06 && //direction from originating site, transaction id==0, Radio Resouce Management protocol |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 51 | (msg_type==0x21 || msg_type==0x22 || msg_type==0x24) //types corresponding to paging requests |
| 52 | ) |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 53 | { |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 54 | //write timestamp |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 55 | switch(msg_type) { |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 56 | 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 |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 63 | { |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 64 | write_tmsi(m+6); |
| 65 | dump_file << "-"; |
| 66 | write_timestamp(now); |
| 67 | dump_file << "-0"; |
| 68 | dump_file << std::endl; |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 69 | |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 70 | 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; |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 79 | |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 80 | next_element_index = 13; |
| 81 | found_id_element = true; |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 82 | } |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 83 | |
| 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 | } |
banjaxbanjo | 9e661ca | 2015-01-18 10:19:29 +0100 | [diff] [blame] | 112 | break; |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 113 | 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; |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 163 | } |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 164 | } |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | inline void tmsi_dumper_impl::write_timestamp(tm * now) |
| 168 | { |
| 169 | dump_file << boost::format("%d%02d%02d%02d%02d%02d") |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 170 | % (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 |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
| 177 | inline int swap(uint8_t c) |
| 178 | { |
| 179 | uint8_t temp1, temp2; |
| 180 | temp1 = c & 0x0F; |
| 181 | temp2 = c & 0xF0; |
| 182 | temp1=temp1 << 4; |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 183 | temp2=temp2 >> 4; |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 184 | return(temp2|temp1); |
| 185 | } |
| 186 | |
| 187 | void 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 | |
| 195 | void 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 | |
| 203 | tmsi_dumper::sptr |
| 204 | tmsi_dumper::make() |
| 205 | { |
| 206 | return gnuradio::get_initial_sptr |
| 207 | (new tmsi_dumper_impl()); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * The private constructor |
| 212 | */ |
| 213 | tmsi_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 | { |
ptrkrysik | 8c6e6ac | 2015-01-18 10:00:36 +0100 | [diff] [blame] | 218 | dump_file.open("tmsicount.txt", std::ios_base::app); |
ptrkrysik | 03da5c6 | 2015-01-18 09:56:26 +0100 | [diff] [blame] | 219 | message_port_register_in(pmt::mp("msgs")); |
| 220 | set_msg_handler(pmt::mp("msgs"), boost::bind(&tmsi_dumper_impl::dump_tmsi, this, _1)); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Our virtual destructor. |
| 225 | */ |
| 226 | tmsi_dumper_impl::~tmsi_dumper_impl() |
| 227 | { |
| 228 | dump_file.close(); |
| 229 | } |
| 230 | } /* namespace gsm */ |
ptrkrysik | 82bd117 | 2015-01-15 09:35:03 +0100 | [diff] [blame] | 231 | } /* namespace gr */ |
| 232 | |