print-usb.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2009 Bert Vermeulen <bert@biot.com>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that: (1) source code distributions
  6. * retain the above copyright notice and this paragraph in its entirety, (2)
  7. * distributions including binary code include the above copyright notice and
  8. * this paragraph in its entirety in the documentation or other materials
  9. * provided with the distribution, and (3) all advertising materials mentioning
  10. * features or use of this software display the following acknowledgement:
  11. * ``This product includes software developed by Paolo Abeni.''
  12. * The name of author may not be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  15. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  16. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * Support for USB packets
  19. *
  20. */
  21. /* \summary: USB printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include <netdissect-stdinc.h>
  26. #include "netdissect.h"
  27. #if defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX)
  28. #include <pcap/usb.h>
  29. static const char tstr[] = "[|usb]";
  30. /* returns direction: 1=inbound 2=outbound -1=invalid */
  31. static int
  32. get_direction(int transfer_type, int event_type)
  33. {
  34. int direction;
  35. direction = -1;
  36. switch(transfer_type){
  37. case URB_BULK:
  38. case URB_CONTROL:
  39. case URB_ISOCHRONOUS:
  40. switch(event_type)
  41. {
  42. case URB_SUBMIT:
  43. direction = 2;
  44. break;
  45. case URB_COMPLETE:
  46. case URB_ERROR:
  47. direction = 1;
  48. break;
  49. default:
  50. direction = -1;
  51. }
  52. break;
  53. case URB_INTERRUPT:
  54. switch(event_type)
  55. {
  56. case URB_SUBMIT:
  57. direction = 1;
  58. break;
  59. case URB_COMPLETE:
  60. case URB_ERROR:
  61. direction = 2;
  62. break;
  63. default:
  64. direction = -1;
  65. }
  66. break;
  67. default:
  68. direction = -1;
  69. }
  70. return direction;
  71. }
  72. static void
  73. usb_header_print(netdissect_options *ndo, const pcap_usb_header *uh)
  74. {
  75. int direction;
  76. switch(uh->transfer_type)
  77. {
  78. case URB_ISOCHRONOUS:
  79. ND_PRINT((ndo, "ISOCHRONOUS"));
  80. break;
  81. case URB_INTERRUPT:
  82. ND_PRINT((ndo, "INTERRUPT"));
  83. break;
  84. case URB_CONTROL:
  85. ND_PRINT((ndo, "CONTROL"));
  86. break;
  87. case URB_BULK:
  88. ND_PRINT((ndo, "BULK"));
  89. break;
  90. default:
  91. ND_PRINT((ndo, " ?"));
  92. }
  93. switch(uh->event_type)
  94. {
  95. case URB_SUBMIT:
  96. ND_PRINT((ndo, " SUBMIT"));
  97. break;
  98. case URB_COMPLETE:
  99. ND_PRINT((ndo, " COMPLETE"));
  100. break;
  101. case URB_ERROR:
  102. ND_PRINT((ndo, " ERROR"));
  103. break;
  104. default:
  105. ND_PRINT((ndo, " ?"));
  106. }
  107. direction = get_direction(uh->transfer_type, uh->event_type);
  108. if(direction == 1)
  109. ND_PRINT((ndo, " from"));
  110. else if(direction == 2)
  111. ND_PRINT((ndo, " to"));
  112. ND_PRINT((ndo, " %d:%d:%d", uh->bus_id, uh->device_address, uh->endpoint_number & 0x7f));
  113. }
  114. /*
  115. * This is the top level routine of the printer for captures with a
  116. * 48-byte header.
  117. *
  118. * 'p' points to the header of the packet, 'h->ts' is the timestamp,
  119. * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  120. * is the number of bytes actually captured.
  121. */
  122. u_int
  123. usb_linux_48_byte_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
  124. register const u_char *p)
  125. {
  126. if (h->caplen < sizeof(pcap_usb_header)) {
  127. ND_PRINT((ndo, "%s", tstr));
  128. return(sizeof(pcap_usb_header));
  129. }
  130. usb_header_print(ndo, (const pcap_usb_header *) p);
  131. return(sizeof(pcap_usb_header));
  132. }
  133. #ifdef DLT_USB_LINUX_MMAPPED
  134. /*
  135. * This is the top level routine of the printer for captures with a
  136. * 64-byte header.
  137. *
  138. * 'p' points to the header of the packet, 'h->ts' is the timestamp,
  139. * 'h->len' is the length of the packet off the wire, and 'h->caplen'
  140. * is the number of bytes actually captured.
  141. */
  142. u_int
  143. usb_linux_64_byte_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
  144. register const u_char *p)
  145. {
  146. if (h->caplen < sizeof(pcap_usb_header_mmapped)) {
  147. ND_PRINT((ndo, "%s", tstr));
  148. return(sizeof(pcap_usb_header_mmapped));
  149. }
  150. usb_header_print(ndo, (const pcap_usb_header *) p);
  151. return(sizeof(pcap_usb_header_mmapped));
  152. }
  153. #endif /* DLT_USB_LINUX_MMAPPED */
  154. #endif /* defined(HAVE_PCAP_USB_H) && defined(DLT_USB_LINUX) */