/src/java/net/sourceforge/jpcap/capture/PacketCaptureCapable.java

https://github.com/jpcap/jpcap · Java · 186 lines · 31 code · 25 blank · 130 comment · 0 complexity · 995133a53ce19799194bc306c4db1be2 MD5 · raw file

  1. // $Id: PacketCaptureCapable.java,v 1.4 2003/06/24 20:30:18 pcharles Exp $
  2. /***************************************************************************
  3. * Copyright (C) 2001, Patrick Charles and Jonas Lehmann *
  4. * Distributed under the Mozilla Public License *
  5. * http://www.mozilla.org/NPL/MPL-1.1.txt *
  6. ***************************************************************************/
  7. package net.sourceforge.jpcap.capture;
  8. /**
  9. * This is the packet capture interface. It is implemented by both the
  10. * packet capture system (PacketCapture) and the simulator
  11. * (PacketCaptureSimulator).
  12. * <p>
  13. * The interface has two major components: methods that a client uses
  14. * to register for packet events and methods that a client
  15. * calls in order to setup and initiate packet capture.
  16. *
  17. * @author Patrick Charles and Jonas Lehmann
  18. * @version $Revision: 1.4 $
  19. * @lastModifiedBy $Author: pcharles $
  20. * @lastModifiedAt $Date: 2003/06/24 20:30:18 $
  21. */
  22. public interface PacketCaptureCapable
  23. {
  24. /**
  25. * Snapshot length. Maximum number of bytes per packet to capture.
  26. * For IPv4, 96 bytes guarantees that at least the headers of most
  27. * packet types and protocols will get captured.
  28. * For IPv6, 68 is a better value?
  29. */
  30. int DEFAULT_SNAPLEN = 96;
  31. /**
  32. * Default capture timeout in milliseconds.
  33. */
  34. int DEFAULT_TIMEOUT = 1000;
  35. // capture component of interface
  36. /**
  37. * Open a network device for data capture. Throws an exception if
  38. * the device name specified is invalid. Uses default values for
  39. * the capture timeout and snaplen.
  40. *
  41. * @param device the name of the network device.
  42. * Examples of valid network devices on linux are 'eth0' and 'ppp0'.
  43. * @param promiscuous whether or not the device should be opened in
  44. * promiscuous mode.
  45. */
  46. void open(String device, boolean promiscuous)
  47. throws CaptureDeviceOpenException;
  48. /**
  49. * Open a network device for data capture.
  50. *
  51. * @param device the name of the network device.
  52. * Examples of valid network devices on linux are 'eth0' and 'ppp0'.
  53. * @param snaplen the 'snapshot' length. Defines the maximum number of
  54. * bytes to save from each captured packet.
  55. * @param promiscuous whether or not the device should be opened in
  56. * promiscuous mode.
  57. * @param timeout the packet capture timeout in milliseconds.
  58. */
  59. void open(String device, int snaplen, boolean promiscuous, int timeout)
  60. throws CaptureDeviceOpenException;
  61. /**
  62. * Open a tcpdump-formatted savefile.
  63. *
  64. * @param fileName the name of the savefile.
  65. */
  66. void openOffline(String fileName)
  67. throws CaptureFileOpenException;
  68. /**
  69. * Create, compile and activate a filter from a filter expression.
  70. *
  71. * @param filterExpression the filter expression. For example,
  72. * the expression "host techno" would filter only packets sent or
  73. * arriving at the host named techno.
  74. * @param optimize whether or not the resulting bpf code is optimized
  75. * internally by libpcap.
  76. */
  77. void setFilter(String filterExpression, boolean optimize)
  78. throws InvalidFilterException;
  79. /**
  80. * Capture packets.
  81. *
  82. * @param count the number of packets to capture.
  83. * If count is negative, capture will block forever, unless an exception
  84. * is thrown.
  85. */
  86. void capture(int count)
  87. throws CapturePacketException;
  88. /**
  89. * Fetch statistics on captured packets.
  90. * This method should not be called unless capture() was previously called.
  91. *
  92. * @return packet capture statistics.
  93. */
  94. CaptureStatistics getStatistics();
  95. /**
  96. * Close the capture device.
  97. */
  98. void close();
  99. // static native methods to fetch capture device and network information
  100. /**
  101. * Detect a network device suitable for packet capture.
  102. *
  103. * @return a string describing the network device. if no device can be
  104. * found, null is returned.
  105. */
  106. String findDevice()
  107. throws CaptureDeviceNotFoundException;
  108. /**
  109. * Fetch the network number for the specified device.
  110. *
  111. * @param device the name of the network device.
  112. * @return the network address
  113. */
  114. int getNetwork(String device)
  115. throws CaptureConfigurationException;
  116. /**
  117. * Fetch the network mask for the specified device.
  118. *
  119. * @param device the name of the network device.
  120. * @return the netmask address
  121. */
  122. int getNetmask(String device)
  123. throws CaptureConfigurationException;
  124. /**
  125. * Fetch the link layer type for the specified device.
  126. *
  127. * @return the link layer type code.
  128. */
  129. int getLinkLayerType()
  130. throws CaptureConfigurationException;
  131. /**
  132. * Get the snapshot length given that network device is open.
  133. *
  134. * @return the packet snapshot length.
  135. */
  136. int getSnapshotLength();
  137. // listener registration component of interface
  138. /**
  139. * Register a raw packet listener with this capture system.
  140. * @param rawListener the raw packet listener to add to the notification
  141. * list.
  142. */
  143. void addRawPacketListener(RawPacketListener rawListener);
  144. /**
  145. * Deregister a raw packet listener from this capture system.
  146. * @param rawListener the raw packet listener to remove from the
  147. * notification list.
  148. */
  149. void removeRawPacketListener(RawPacketListener rawListener);
  150. /**
  151. * Register a packet object listener with this capture system.
  152. * @param objListener the packet listener to add to the notification list.
  153. */
  154. void addPacketListener(PacketListener objListener);
  155. /**
  156. * Deregister a packet object listener from this capture system.
  157. * @param objListener the packet listener to remove from the
  158. * notification list.
  159. */
  160. void removePacketListener(PacketListener objListener);
  161. }