void print_eth_hdr(const uint8_t *packet, unsigned int len) { assert(packet); eth_hdr *eth = (eth_hdr *)packet; printf("Ethernet Packet Header (%d bytes)\n", sizeof(eth_hdr)); indent(1); print_mac_address("Src MAC Address", eth->eth_shost); indent(1); print_mac_address("Dst MAC Address", eth->eth_dhost); indent(1); if(ntohs(eth->eth_type) == ETH_TYPE_IP) { printf("Type = Internet Protocol, Version 4 (IPv4)\n"); } if(ntohs(eth->eth_type) == ETH_TYPE_ARP) { printf("Type = Address Resolution Protocol (ARP)\n"); } }
/** Callback triggered when the ble initialization process has finished */ void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { if (params->error != BLE_ERROR_NONE) { printf("Ble initialization failed."); return; } print_mac_address(); start_advertising(); }
void process_packet(u_char *args,const struct pcap_pkthdr *header,const u_char *packet) { int len,offset,cnt=0; u_char *data; sniff_ethhdr = (HDR_ETHERNET *)packet; // 엔디안을 형식에 알맞게 변환하여 비교를 해야함. if(ntohs(sniff_ethhdr->ether_type)!=IP_V4) return ; sniff_ip = (HDR_IP *)(packet + sizeof(HDR_ETHERNET)); /* * IP_HL(sniff_ip) 가 받아오는 값은 필드의 개수이다. 따라서 byte길이를 구하려면 IP_HL(sniff_ip)*4 를 해주어야 한다. * 추가적으로 IP_HL의 필드 개수로 ip헤더인지 검증을 할 수 있고, optional 헤더의 여부도 알 수 있다. */ len = IP_HL(sniff_ip)*4; if(len<20 || sniff_ip->protocol != PROTO_TCP) return ; sniff_tcp = (HDR_TCP *)(packet + sizeof(HDR_ETHERNET) + len); offset = TH_OFF(sniff_tcp)*4; data = (u_char *)(packet + sizeof(HDR_ETHERNET) + len + offset); print_mac_address("Source Mac : ",sniff_ethhdr->ether_shost); print_mac_address("Dest Mac : ",sniff_ethhdr->ether_dhost); printf("Source IP Address : %s\n",inet_ntoa(sniff_ip->ip_src)); printf("Dest IP Address : %s\n",inet_ntoa(sniff_ip->ip_dst)); printf("Source Port : %d\n",ntohs(sniff_tcp->s_port)); printf("Dest Port : %d\n",ntohs(sniff_tcp->d_port)); printf("========================================== Data ==============================================\n"); while(*data!=NULL) { printf("%c",*data); data++; } printf("==============================================================================================\n"); }
int main(int argc, char *argv[]) { int c; int char_length = 32; int (*char_maker)(int) = numbersCharsAndSymbols; int use_randlib = 0; int mac_address = 0; while ((c = getopt(argc, argv, "ravhn:m")) != -1) { switch (c) { case 'a': char_maker = numbersAndChars; break; case 'v': #ifdef DEVRANDOM fprintf(stderr, "%s using %s\n", PACKAGE_STRING, DEVRANDOM); #else fprintf(stderr, "%s\n", PACKAGE_STRING); #endif exit(0); case 'h': usage(); exit(0); case 'r': use_randlib = 1; break; case 'm': mac_address = 1; break; case 'n': char_length = atoi(optarg); if (char_length < 1) { fprintf(stderr, "Must produce at least one character\n"); exit(1); } break; default: fprintf(stderr, "%s\nUnknown option: %c\n", PACKAGE_STRING, c); usage(); exit(1); } } if (mac_address) print_mac_address(); else if (use_randlib) print_seq_rand_lib(char_length, char_maker); else print_seq(char_length, char_maker); return 0; }
/** Callback triggered when the ble initialization process has finished */ void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { if (params->error != BLE_ERROR_NONE) { printf("Ble initialization failed."); return; } _led_service = new LEDService(_ble, false); _ble.gattServer().onDataWritten(this, &LEDDemo::on_data_written); print_mac_address(); start_advertising(); }
void print_arp_hdr(const uint8_t *packet, unsigned int len) { assert(packet); arp_hdr *arp = get_arp_hdr(packet, len); indent(1); printf("ARP Packet (%d bytes)\n", sizeof(arp_hdr)); indent(2); printf("Hardware Type: "); switch(ntohs(arp->arp_hrd)) { case 1: { printf("Ethernet\n"); break; } default: { printf("%X\n", ntohs(arp->arp_hrd)); break; } } indent(2); printf("Protocol Type = %X (IP)\n", ntohs(arp->arp_pro)); indent(2); printf("Hardware Address Length = %d\n", arp->arp_hln); indent(2); printf("Protocol Address Length = %d\n", arp->arp_pln); indent(2); printf("Opcode = "); switch(ntohs(arp->arp_op)) { case 1: { printf("Request\n"); break; } case 2: { printf("Reply\n"); break; } default: { printf("%X\n", arp->arp_op); break; } } indent(2); print_mac_address("Src Hardware Address", arp->arp_sha); indent(2); print_ip_address("Src Protocol Address", arp->arp_sip); indent(2); print_mac_address("Dst Hardware Address", arp->arp_tha); indent(2); print_ip_address("Dst Protocol Address", arp->arp_tip); }
static int hieth_plat_driver_probe(struct platform_device *pdev) { int ret = -1; struct net_device *ndev = NULL; memset(hieth_devs_save, 0, sizeof(hieth_devs_save)); hieth_sys_init(); if (hieth_mdiobus_driver_init(pdev)) { hieth_error("mdio bus init error!\n"); ret = -ENODEV; goto _error_mdiobus_driver_init; } hieth_platdev_probe_port(pdev, UP_PORT); #ifdef CONFIG_HIETH_DOWNPORT_EN hieth_platdev_probe_port(pdev, DOWN_PORT); #endif phy_quirk(&hieth_mdio_local_device, CONFIG_HIETH_PHYID_U); phy_quirk(&hieth_mdio_local_device, CONFIG_HIETH_PHYID_D); if (hieth_devs_save[UP_PORT]) ndev = hieth_devs_save[UP_PORT]; else if (hieth_devs_save[DOWN_PORT]) ndev = hieth_devs_save[DOWN_PORT]; if (!ndev) { hieth_error("no dev probed!\n"); ret = -ENODEV; goto _error_nodev_exit; } if (!is_valid_ether_addr(macaddr.sa_data)) { print_mac_address(KERN_WARNING "Invalid HW-MAC Address: ", \ macaddr.sa_data, "\n"); random_ether_addr(macaddr.sa_data); print_mac_address(KERN_WARNING "Set Random MAC address: ", \ macaddr.sa_data, "\n"); } hieth_net_set_mac_address(ndev, (void *)&macaddr); ret = request_irq(CONFIG_HIETH_IRQNUM, hieth_net_isr, IRQF_SHARED, \ "hieth", hieth_devs_save); if (ret) { hieth_error("request_irq %d failed!", CONFIG_HIETH_IRQNUM); goto _error_request_irq; } return ret; _error_request_irq: hieth_platdev_remove_port(pdev, UP_PORT); hieth_platdev_remove_port(pdev, DOWN_PORT); _error_nodev_exit: hieth_mdiobus_driver_exit(); _error_mdiobus_driver_init: hieth_sys_exit(); return ret; }
static int process_received_frame ( const int frame_len ) { // At the moment, we can only reply to a single ARP query for our MAC address. // Use a command like this to make this routine generate an answer: // With Ubuntu's arping: // arping -c 1 -f -w 10 -I dpi-tap1 192.168.254.1 // With Thomas Habets' arping: // sudo ./arping -w 10000000 -c 1 -i dpi-tap1 192.168.254.1 const int ARP_FRAME_LENGTH = 42; if ( frame_len < ARP_FRAME_LENGTH ) { uart_print( UART1_BASE_ADDR, "The frame is too short to be the kind of ARP frame we are looking for." EOL ); return 0; } int pos = 0; if ( eth_rx_packet[ pos + 0 ] != BROADCAST_ADDRESS_5 || eth_rx_packet[ pos + 1 ] != BROADCAST_ADDRESS_4 || eth_rx_packet[ pos + 2 ] != BROADCAST_ADDRESS_3 || eth_rx_packet[ pos + 3 ] != BROADCAST_ADDRESS_2 || eth_rx_packet[ pos + 4 ] != BROADCAST_ADDRESS_1 || eth_rx_packet[ pos + 5 ] != BROADCAST_ADDRESS_0 ) { uart_print( UART1_BASE_ADDR, "The target MAC address is not broadcast." EOL ); return 0; } pos += MAC_ADDR_LEN; const int src_mac_addr_pos = pos; uart_print( UART1_BASE_ADDR, "Received broadcast frame from MAC address " ); print_mac_address( ð_rx_packet[ pos ] ); uart_print( UART1_BASE_ADDR, EOL ); pos += MAC_ADDR_LEN; const unsigned char ARP_PROTOCOL_HI = 0x08; const unsigned char ARP_PROTOCOL_LO = 0x06; if ( eth_rx_packet[ pos + 0 ] != ARP_PROTOCOL_HI || eth_rx_packet[ pos + 1 ] != ARP_PROTOCOL_LO ) { uart_print( UART1_BASE_ADDR, "The frame does not contain ARP protocol data." EOL ); return 0; } pos += 2; const unsigned char ETHERNET_HARDWARE_TYPE_HI = 0x00; const unsigned char ETHERNET_HARDWARE_TYPE_LO = 0x01; if ( eth_rx_packet[ pos + 0 ] != ETHERNET_HARDWARE_TYPE_HI || eth_rx_packet[ pos + 1 ] != ETHERNET_HARDWARE_TYPE_LO ) { uart_print( UART1_BASE_ADDR, "The ARP frame does not contain the Ethernet hardware type." EOL ); return 0; } pos += 2; const unsigned char IP_PROTOCOL_HI = 0x08; const unsigned char IP_PROTOCOL_LO = 0x00; if ( eth_rx_packet[ pos + 0 ] != IP_PROTOCOL_HI || eth_rx_packet[ pos + 1 ] != IP_PROTOCOL_LO ) { uart_print( UART1_BASE_ADDR, "The ARP frame is not about the IP protocol." EOL ); return 0; } pos += 2; const unsigned char HARDWARE_SIZE = MAC_ADDR_LEN; if ( eth_rx_packet[ pos ] != HARDWARE_SIZE ) { uart_print( UART1_BASE_ADDR, "The ARP frame has an invalid hardware size." EOL ); return 0; } pos += 1; const unsigned char PROTOCOL_SIZE = IP_ADDR_LEN; if ( eth_rx_packet[ pos ] != PROTOCOL_SIZE ) { uart_print( UART1_BASE_ADDR, "The ARP frame has an invalid protocol size." EOL ); return 0; } pos += 1; const unsigned char OPCODE_REQUEST_HI = 0x00; const unsigned char OPCODE_REQUEST_LO = 0x01; const unsigned char OPCODE_REPLY_HI = 0x00; const unsigned char OPCODE_REPLY_LO = 0x02; if ( eth_rx_packet[ pos + 0 ] != OPCODE_REQUEST_HI || eth_rx_packet[ pos + 1 ] != OPCODE_REQUEST_LO ) { uart_print( UART1_BASE_ADDR, "The ARP frame is not an ARP request." EOL ); return 0; } pos += 2; uart_print( UART1_BASE_ADDR, "The ARP sender MAC address is " ); print_mac_address( ð_rx_packet[ pos ] ); uart_print( UART1_BASE_ADDR, EOL ); pos += MAC_ADDR_LEN; const int src_ip_addr_pos = pos; uart_print( UART1_BASE_ADDR, "The ARP sender IP address is " ); print_ip_address( ð_rx_packet[ pos ] ); uart_print( UART1_BASE_ADDR, EOL ); pos += IP_ADDR_LEN; uart_print( UART1_BASE_ADDR, "The target MAC address is " ); print_mac_address( ð_rx_packet[ pos ] ); uart_print( UART1_BASE_ADDR, EOL ); // Linux uses 0x000000 here, but arping uses 0xFFFFFF. const int is_zero = eth_rx_packet[ pos + 0 ] == 0 && eth_rx_packet[ pos + 1 ] == 0 && eth_rx_packet[ pos + 2 ] == 0 && eth_rx_packet[ pos + 3 ] == 0 && eth_rx_packet[ pos + 4 ] == 0 && eth_rx_packet[ pos + 5 ] == 0; const int is_bcast = eth_rx_packet[ pos + 0 ] == BROADCAST_ADDRESS_5 && eth_rx_packet[ pos + 1 ] == BROADCAST_ADDRESS_4 && eth_rx_packet[ pos + 2 ] == BROADCAST_ADDRESS_3 && eth_rx_packet[ pos + 3 ] == BROADCAST_ADDRESS_2 && eth_rx_packet[ pos + 4 ] == BROADCAST_ADDRESS_1 && eth_rx_packet[ pos + 5 ] == BROADCAST_ADDRESS_0; if ( !is_zero && !is_bcast ) { uart_print( UART1_BASE_ADDR, "The target MAC address is neither zero nor 0xFF." EOL ); return 0; } pos += MAC_ADDR_LEN; uart_print( UART1_BASE_ADDR, "The target IP address is " ); print_ip_address( ð_rx_packet[ pos ] ); uart_print( UART1_BASE_ADDR, EOL ); if ( eth_rx_packet[ pos + 0 ] != OWN_IP_ADDRESS_0 || eth_rx_packet[ pos + 1 ] != OWN_IP_ADDRESS_1 || eth_rx_packet[ pos + 2 ] != OWN_IP_ADDRESS_2 || eth_rx_packet[ pos + 3 ] != OWN_IP_ADDRESS_3 ) { uart_print( UART1_BASE_ADDR, "The target IP address is not ours." EOL ); return 0; } pos += IP_ADDR_LEN; if ( pos != ARP_FRAME_LENGTH ) { uart_print( UART1_BASE_ADDR, "Internal error parsing the frame." EOL ); return 0; } // Build the ARP reply. pos = 0; copy_mac_address( ð_rx_packet[ src_mac_addr_pos ], ð_tx_packet[ pos ] ); pos += MAC_ADDR_LEN; write_own_mac_addr( ð_tx_packet[ pos ] ); pos += MAC_ADDR_LEN; eth_tx_packet[ pos + 0 ] = ARP_PROTOCOL_HI; eth_tx_packet[ pos + 1 ] = ARP_PROTOCOL_LO; pos += 2; eth_tx_packet[ pos + 0 ] = ETHERNET_HARDWARE_TYPE_HI; eth_tx_packet[ pos + 1 ] = ETHERNET_HARDWARE_TYPE_LO; pos += 2; eth_tx_packet[ pos + 0 ] = IP_PROTOCOL_HI; eth_tx_packet[ pos + 1 ] = IP_PROTOCOL_LO; pos += 2; eth_tx_packet[ pos + 0 ] = HARDWARE_SIZE; pos += 1; eth_tx_packet[ pos + 0 ] = PROTOCOL_SIZE; pos += 1; eth_tx_packet[ pos + 0 ] = OPCODE_REPLY_HI; eth_tx_packet[ pos + 1 ] = OPCODE_REPLY_LO; pos += 2; write_own_mac_addr( ð_tx_packet[ pos ] ); pos += MAC_ADDR_LEN; eth_tx_packet[ pos + 0 ] = OWN_IP_ADDRESS_0; eth_tx_packet[ pos + 1 ] = OWN_IP_ADDRESS_1; eth_tx_packet[ pos + 2 ] = OWN_IP_ADDRESS_2; eth_tx_packet[ pos + 3 ] = OWN_IP_ADDRESS_3; pos += IP_ADDR_LEN; copy_mac_address( ð_rx_packet[ src_mac_addr_pos ], ð_tx_packet[ pos ] ); pos += MAC_ADDR_LEN; copy_ip_address( ð_rx_packet[ src_ip_addr_pos ], ð_tx_packet[ pos ] ); pos += IP_ADDR_LEN; if ( pos != ARP_FRAME_LENGTH ) { uart_print( UART1_BASE_ADDR, "Internal error building the ARP reply frame." EOL ); return 0; } // After the ARP information, at the end of the Ethernet packet, comes the dummy CRC, // which should be 4 bytes with value 0xDEADF00D. uart_print( UART1_BASE_ADDR, "Sending the ARP reply..." EOL ); start_ethernet_send( ARP_FRAME_LENGTH ); wait_until_frame_was_sent(); uart_print( UART1_BASE_ADDR, "Reply sent." EOL ); return 1; }