Changeset fc632e8 in mainline
- Timestamp:
 - 2024-12-21T11:37:22Z (11 months ago)
 - Children:
 - 3197b50
 - Parents:
 - 1ea8566
 - File:
 - 
      
- 1 edited
 
- 
          
  uspace/app/pcapcat/main.c (modified) (4 diffs)
 
 
Legend:
- Unmodified
 - Added
 - Removed
 
- 
      
uspace/app/pcapcat/main.c
r1ea8566 rfc632e8 26 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 27 */ 28 //qemu-system-x86_64 -m 256m -nic bridge,id=n1,mac=52:54:00:BE:EF:01,br=br0 -boot order=d -cdrom image.iso -serial stdio 28 29 29 30 #include <stdint.h> … … 44 45 } linktype_parser_t; 45 46 46 47 #define PRINT_IP(msg, ip_addr, spaces) printf("%s IP: %d.%d.%d.%d%s", msg, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3], spaces) 48 #define PRINT_MAC(msg, mac, spaces) printf("%s MAC: %x:%x:%x:%x:%x:%x%s", msg, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], spaces) 49 50 static void parse_arp(unsigned char *byte_source, size_t size) 51 { 52 uint8_t sender_mac[6]; 53 uint8_t sender_ip[4]; 54 uint8_t target_mac[6]; 55 uint8_t target_ip[4]; 56 57 for (int i = 22; i < 28; ++i) { 58 sender_mac[i - 22] = byte_source[i]; 59 } 60 61 for (int i = 28; i < 32; ++i) { 62 sender_ip[i - 28] = byte_source[i]; 63 } 64 65 for (int i = 32; i < 38; ++i) { 66 target_mac[i - 32] = byte_source[i]; 67 } 68 69 for (int i = 38; i < 42; ++i) { 70 target_ip[i - 38] = byte_source[i]; 71 } 72 73 PRINT_MAC("Sender", sender_mac, ", "); 74 PRINT_IP("Sender", sender_ip, " "); 75 PRINT_MAC("Target", target_mac, ", "); 76 PRINT_IP("Target", target_ip, "\n"); 77 78 } 79 80 static void parse_tcp(unsigned char *byte_source, size_t size) 81 { 82 uint16_t src_port = byte_source[34] << 8 | byte_source[35]; 83 uint16_t dst_port = byte_source[36] << 8 | byte_source[37]; 84 printf(" [TCP] source port: %d, destination port: %d\n", src_port, dst_port); 85 } 86 87 static void parse_ip(unsigned char *byte_source, size_t size) 88 { 89 uint16_t total_length; 90 uint8_t header_length; 91 uint16_t payload_length; 92 uint8_t protocol; 93 uint8_t src_ip[4]; 94 uint8_t dst_ip[4]; 95 96 header_length = (byte_source[14] & 0xf) * 4; 97 total_length = byte_source[16] << 8 | byte_source[17]; 98 payload_length = total_length - header_length; 99 protocol = byte_source[23]; 100 101 for (int i = 26; i < 30; ++i) { 102 src_ip[i - 26] = byte_source[i]; 103 } 104 for (int i = 30; i < 34; ++i) { 105 dst_ip[i - 30] = byte_source[i]; 106 } 107 printf("IP header: %dB, payload: %dB, protocol: 0x%x, ", header_length, payload_length, protocol); 108 PRINT_IP("Source", src_ip, ", "); 109 PRINT_IP("Destination", dst_ip, "\n"); 110 111 if (protocol == 0x6) { 112 parse_tcp(byte_source, size); 113 } 114 } 115 116 117 static void parse_packet(void *data, size_t size) 118 { 119 unsigned char* byte_source = (unsigned char*)data; 120 121 // Read the 12th and 13th bytes (indices 11 and 12) 122 unsigned char high_byte = byte_source[12]; // MSB 123 unsigned char low_byte = byte_source[13]; // LSB 124 125 // Combine bytes in big-endian order 126 uint16_t value = (high_byte << 8) | low_byte; 127 128 switch (value){ 129 case 0x0806: 130 printf("[ARP] "); 131 parse_arp(byte_source, size); 132 break; 133 case 0x0800: 134 printf("[IPv4] "); 135 parse_ip(byte_source, size); 136 break; 137 case 0x86DD: 138 printf("IPv6\n"); 139 break; 140 default: 141 printf("0x%x\n", value); 142 break; 143 } 144 //printf("Ethernet Type of packet: 0x%x\n", hdr->etype_len); 145 } 47 146 48 147 static void read_head(FILE *file) … … 77 176 memset(&hdr, 0, sizeof(pcap_packet_header_t)); 78 177 size_t read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), f); 79 178 int index = 1; 80 179 while (read_bytes > 0) 81 180 { … … 84 183 return; 85 184 } 86 printf("0x%x: %d, %d\n", hdr.magic_stamp, hdr.captured_length, hdr.original_length); 185 printf("%04d) ", index); 186 index++; 187 //printf("0x%x: %d, %d\n", hdr.magic_stamp, hdr.captured_length, hdr.original_length); 87 188 void *data = malloc(hdr.captured_length); 88 189 fread(data, 1, (size_t)hdr.captured_length, f); 190 parse_packet(data, (size_t)hdr.captured_length); 89 191 free(data); 90 192 read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), f);  
  Note:
 See   TracChangeset
 for help on using the changeset viewer.
  