/** Transform input. * @param txt_in: input text format. * @param wire1: output wireformat in hex (txt_in converted to wire). * @param txt_out: output text format (converted from wire_out). * @param wire2: output wireformat in hex, txt_out converted back to wireformat. * @param bufs: size of the text buffers. */ static void rr_transform(char* txt_in, char* wire1, char* txt_out, char* wire2, size_t bufs) { uint8_t b[65536]; size_t len; int err; len = sizeof(b); err = sldns_str2wire_rr_buf(txt_in, b, &len, NULL, 3600, NULL, 0, NULL, 0); if(err != 0) { if(vbmp) printf("sldns_str2wire_rr_buf, pos %d: %s\n", LDNS_WIREPARSE_OFFSET(err), sldns_get_errorstr_parse(err)); } unit_assert(err == 0); buf_to_hex(b, len, wire1, bufs); if(vbmp) printf("wire1: %s", wire1); err = sldns_wire2str_rr_buf(b, len, txt_out, bufs); unit_assert(err < (int)bufs && err > 0); if(vbmp) printf("txt: %s", txt_out); len = sizeof(b); err = sldns_str2wire_rr_buf(txt_out, b, &len, NULL, 3600, NULL, 0, NULL, 0); if(err != 0) { if(vbmp) printf("sldns_str2wire_rr_buf-2, pos %d: %s\n", LDNS_WIREPARSE_OFFSET(err), sldns_get_errorstr_parse(err)); } unit_assert(err == 0); buf_to_hex(b, len, wire2, bufs); if(vbmp) printf("wire2: %s", wire2); }
void process_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) { #ifdef LINUX pcap_usb_header *usb_header = (pcap_usb_header *) packet; if(usb_header->device_address != device_number) return; // TODO: have a hash table here? static int last_urb_id = 0; // Used for mapping request with it's response unsigned int last_request_type; if(usb_header->setup.bmRequestType == 128 && usb_header->setup.bRequest == 0x06) { // This one seems being requested each time // when we start a data capture. // I don't think that we are insterested in it's contents, // so just display a message. printf("# GET DESCRIPTOR Request DEVICE\n"); // Mark as awaiting response last_urb_id = usb_header->id; last_request_type = usb_header->setup.bmRequestType; return; } if(last_urb_id && last_request_type == 128) { // We do not need it's response as well printf("# GET DESCRIPTOR Response DEVICE\n"); last_urb_id = last_request_type = 0; return; } // Transfer type char transfer_type[5], direction[4]; switch(usb_header->transfer_type) { case URB_ISOCHRONOUS: sprintf(transfer_type, "ISOC"); break; case URB_INTERRUPT: sprintf(transfer_type, "INTR"); break; case URB_CONTROL: sprintf(transfer_type, "CTRL"); break; case URB_BULK: sprintf(transfer_type, "BULK"); break; } if( usb_header->endpoint_number & URB_TRANSFER_IN ) sprintf(direction, "IN"); else sprintf(direction, "OUT"); // Output char prefix[19], hex[MAX_PACKET_SIZE * 2]; if(usb_header->data_len > MAX_PACKET_SIZE) { printf("# Large data block omitted\n"); if(out) fprintf(out, "# Large data block omitted\n"); } else if(usb_header->data_len) { /* Printing transfer type & endpoint*/ sprintf(prefix, "%s_%s(%d.%d):", transfer_type, direction, 0, // avoid redundancy usb_header->endpoint_number & 0x7F); OUT("%-14s", prefix); /* Printing packet's setup */ if(usb_header->transfer_type == URB_CONTROL) { pcap_usb_setup *setup = &(usb_header->setup); OUT("%02x:%02x:%04x:%04x:", setup->bmRequestType, setup->bRequest, setup->wValue, setup->wIndex); } /* Printing payload */ const unsigned char *raw_data = packet + header->len - usb_header->data_len; void (*print_packet)(FILE *out, pcap_usb_header *usb_header, const unsigned char *raw_data); buf_to_hex(raw_data, usb_header->data_len, hex); OUT("%s", hex); /* Printing timestamp */ long double time_diff = track_time(usb_header->ts_sec, usb_header->ts_usec); OUT(" # %.16Lf\n", time_diff); } #endif }