cleaned up code and program output, identify alice and bob by their BD_ADDR

This commit is contained in:
matthias.ringwald 2009-08-25 19:44:27 +00:00
parent 9edc874259
commit bbed6963f5

View File

@ -15,19 +15,20 @@
#define EIR_LEN 240 #define EIR_LEN 240
bd_addr_t addr; // there is the target: BOB
bd_addr_t temp_addr; bd_addr_t bob_addr;
uint8_t bob_EIR[EIR_LEN];
uint8_t got_EIR = 0;
uint8_t bob_EIR[EIR_LEN];
hci_con_handle_t bob_handle = 0; hci_con_handle_t bob_handle = 0;
uint16_t bob_clock_offset;
uint8_t bob_page_scan_repetition_mode;
uint8_t bob_got_EIR = 0;
// here's ALICE who wants to talk to BOB
hci_con_handle_t alice_handle = 0; hci_con_handle_t alice_handle = 0;
uint16_t clock_offset;
uint8_t page_scan_repetition_mode; //
bd_addr_t temp_addr;
uint8_t inquiry_done = 0; uint8_t inquiry_done = 0;
hci_con_handle_t con_handle;
uint16_t source_cid_interrupt;
uint16_t source_cid_control;
void data_handler(uint8_t *packet, uint16_t size){ void data_handler(uint8_t *packet, uint16_t size){
hci_con_handle_t in = READ_ACL_CONNECTION_HANDLE(packet); hci_con_handle_t in = READ_ACL_CONNECTION_HANDLE(packet);
@ -80,17 +81,17 @@ void event_handler(uint8_t *packet, uint16_t size){
// enable capure // enable capure
bt_send_cmd(&btstack_set_acl_capture_mode, 1); bt_send_cmd(&btstack_set_acl_capture_mode, 1);
printf("Starting inquiry to get EIR from BOB\n"); printf("1. Started inquiry.\n");
bt_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, 15, 0); bt_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, 15, 0);
} }
// process EIR responses // process EIR responses
if (packet[0] == HCI_EVENT_EXTENDED_INQUIRY_RESPONSE && packet[17] && !got_EIR) { if (packet[0] == HCI_EVENT_EXTENDED_INQUIRY_RESPONSE && packet[17] && !bob_got_EIR) {
printf("Got EIR from BOB\n"); printf("2. Got BOB's EIR.\n");
memcpy(bob_EIR, &packet[17], EIR_LEN); memcpy(bob_EIR, &packet[17], EIR_LEN);
got_EIR = 1; bob_got_EIR = 1;
clock_offset = READ_BT_16(packet, 14); bob_clock_offset = READ_BT_16(packet, 14);
page_scan_repetition_mode = packet[9]; bob_page_scan_repetition_mode = packet[9];
// stop inquiry // stop inquiry
bt_send_cmd(&hci_inquiry_cancel); bt_send_cmd(&hci_inquiry_cancel);
} }
@ -99,46 +100,51 @@ void event_handler(uint8_t *packet, uint16_t size){
if (packet[0] == HCI_EVENT_INQUIRY_COMPLETE || COMMAND_COMPLETE_EVENT(packet, hci_inquiry_cancel)){ if (packet[0] == HCI_EVENT_INQUIRY_COMPLETE || COMMAND_COMPLETE_EVENT(packet, hci_inquiry_cancel)){
if (!inquiry_done){ if (!inquiry_done){
inquiry_done = 1; inquiry_done = 1;
printf("Inquiry Complete, got EIR %u\n", got_EIR); printf("3. Inquiry Complete\n", bob_got_EIR);
if (got_EIR){ if (bob_got_EIR){
printf("Set own EIR to Bob's.\n"); printf("4. Set own EIR to Bob's.\n");
bt_send_cmd(&hci_write_extended_inquiry_response, 0, bob_EIR); bt_send_cmd(&hci_write_extended_inquiry_response, 0, bob_EIR);
} else {
// failed to get BOB's EIR
} }
} }
} }
// Connect to BOB // Connect to BOB
if ( COMMAND_COMPLETE_EVENT(packet, hci_write_extended_inquiry_response) ) { if ( COMMAND_COMPLETE_EVENT(packet, hci_write_extended_inquiry_response) ) {
printf("Now start Alice!...\n"); printf("5. Waiting for Alice!...\n");
// bt_send_cmd(&hci_create_connection, &addr, 0x18, page_scan_repetition_mode, 0, 0x8000 || clock_offset, 0); // bt_send_cmd(&hci_create_connection, &addr, 0x18, page_scan_repetition_mode, 0, 0x8000 || clock_offset, 0);
} }
// accept incoming connections // accept incoming connections
if (packet[0] == HCI_EVENT_CONNECTION_REQUEST){ if (packet[0] == HCI_EVENT_CONNECTION_REQUEST){
printf("Connection request from "); printf("-> Connection request from ");
bt_flip_addr(temp_addr, &packet[2]); bt_flip_addr(temp_addr, &packet[2]);
print_bd_addr(temp_addr); print_bd_addr(temp_addr);
printf("\n"); printf(", sending accept.\n");
bt_send_cmd(&hci_accept_connection_request, &temp_addr, 1); bt_send_cmd(&hci_accept_connection_request, &temp_addr, 1);
} }
// handle connections // handle connections
if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) {
bt_flip_addr(temp_addr, &packet[5]);
if (packet[2] == 0){ if (packet[2] == 0){
if (!alice_handle) { hci_con_handle_t incoming_handle = READ_BT_16(packet, 3);
alice_handle = READ_BT_16(packet, 3); if (BD_ADDR_CMP(temp_addr, bob_addr)){
printf("Alice connected (handle %u). Connecting BOB!\n", alice_handle); bob_handle = incoming_handle;
bt_send_cmd(&hci_create_connection, &addr, 0x18, page_scan_repetition_mode, 0, 0x8000 || clock_offset, 0); printf("7. Connected to BOB (handle %u). Relaying data!\n", bob_handle);
} else { } else {
bob_handle = READ_BT_16(packet, 3); alice_handle = incoming_handle;
printf("Connected to BOB (handle %u). Relayaing data!\n", bob_handle); printf("6. Alice connected (handle %u). Connecting to BOB.\n", alice_handle);
bt_send_cmd(&hci_create_connection, &bob_addr, 0x18, bob_page_scan_repetition_mode, 0, 0x8000 || bob_clock_offset, 0);
} }
} else { } else {
printf("Connection complete status %u\n", packet[2]); printf("Connection complete status %u for connection", packet[2]);
print_bd_addr(temp_addr);
printf("\n");
} }
} }
// inform about pin code request // inform about pin code request
if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){ if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
printf("Please enter PIN 1234 on remote device\n"); printf("Please enter PIN 1234 on remote device\n");
@ -155,7 +161,7 @@ int main (int argc, const char * argv[]){
// parse addr of Bob // parse addr of Bob
uint8_t ok = 0; uint8_t ok = 0;
if (argc >= 2) { if (argc >= 2) {
ok = sscan_bd_addr((uint8_t *) argv[1], addr); ok = sscan_bd_addr((uint8_t *) argv[1], bob_addr);
} }
if (!ok) { if (!ok) {
printf("Usage: mitm 12:34:56:78:9A:BC\n"); printf("Usage: mitm 12:34:56:78:9A:BC\n");
@ -170,7 +176,7 @@ int main (int argc, const char * argv[]){
} }
printf("BTstack-in-the-Middle started, will pretend to be BOB ("); printf("BTstack-in-the-Middle started, will pretend to be BOB (");
print_bd_addr(addr); print_bd_addr(bob_addr);
printf(")\n"); printf(")\n");
bt_register_event_packet_handler(event_handler); bt_register_event_packet_handler(event_handler);