pcap_get_selectable_fd.3pcap 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. .\" Copyright (c) 1994, 1996, 1997
  2. .\" The Regents of the University of California. All rights reserved.
  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 the University of California,
  12. .\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  13. .\" the University nor the names of its contributors may be used to endorse
  14. .\" or promote products derived from this software without specific prior
  15. .\" written permission.
  16. .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  17. .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  18. .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. .\"
  20. .TH PCAP_GET_SELECTABLE_FD 3PCAP "20 January 2018"
  21. .SH NAME
  22. pcap_get_selectable_fd \- get a file descriptor on which a select() can
  23. be done for a live capture
  24. .SH SYNOPSIS
  25. .nf
  26. .ft B
  27. #include <pcap/pcap.h>
  28. .ft
  29. .LP
  30. .ft B
  31. int pcap_get_selectable_fd(pcap_t *p);
  32. .ft
  33. .fi
  34. .SH DESCRIPTION
  35. .B pcap_get_selectable_fd()
  36. returns, on UNIX, a file descriptor number for a file descriptor on
  37. which one can
  38. do a
  39. .BR select() ,
  40. .BR poll() ,
  41. .BR epoll_wait() ,
  42. .BR kevent() ,
  43. or other such call
  44. to wait for it to be possible to read packets without blocking, if such
  45. a descriptor exists, or \-1, if no such descriptor exists.
  46. .PP
  47. Some network devices opened with
  48. .B pcap_create()
  49. and
  50. .BR pcap_activate() ,
  51. or with
  52. .BR pcap_open_live() ,
  53. do not support those calls (for example, regular network devices on
  54. FreeBSD 4.3 and 4.4, and Endace DAG devices), so \-1 is returned for
  55. those devices. In that case, those calls must be given a timeout less
  56. than or equal to the timeout returned by
  57. .B pcap_get_required_select_timeout()
  58. for the device for which
  59. .B pcap_get_selectable_fd()
  60. returned \-1, the device must be put in non-blocking mode with a call to
  61. .BR pcap_setnonblock() ,
  62. and an attempt must always be made to read packets from the device
  63. when the call returns. If
  64. .B pcap_get_required_select_timeout()
  65. returns
  66. .BR NULL ,
  67. it is not possible to wait for packets to arrive on the device in an
  68. event loop.
  69. .PP
  70. Note that a device on which a read can be done without blocking may,
  71. on some platforms, not have any packets to read if the packet buffer
  72. timeout has expired. A call to
  73. .B pcap_dispatch()
  74. or
  75. .B pcap_next_ex()
  76. will return 0 in this case, but will not block.
  77. .PP
  78. Note that in:
  79. .IP
  80. FreeBSD prior to FreeBSD 4.6;
  81. .IP
  82. NetBSD prior to NetBSD 3.0;
  83. .IP
  84. OpenBSD prior to OpenBSD 2.4;
  85. .IP
  86. Mac OS X prior to Mac OS X 10.7;
  87. .PP
  88. .BR select() ,
  89. .BR poll() ,
  90. and
  91. .B kevent()
  92. do not work correctly on BPF devices;
  93. .B pcap_get_selectable_fd()
  94. will return a file descriptor on most of those versions (the exceptions
  95. being FreeBSD 4.3 and 4.4), but a simple
  96. .BR select() ,
  97. .BR poll() ,
  98. or
  99. .B kevent()
  100. call will not indicate that the descriptor is readable until a full
  101. buffer's worth of packets is received, even if the packet timeout
  102. expires before then. To work around this, code that uses
  103. those calls to wait for packets to arrive must put the
  104. .B pcap_t
  105. in non-blocking mode, and must arrange that the call
  106. have a timeout less than or equal to the packet buffer timeout,
  107. and must try to read packets after that timeout expires, regardless of
  108. whether the call indicated that the file descriptor for the
  109. .B pcap_t
  110. is ready to be read or not. (That workaround will not work in FreeBSD
  111. 4.3 and later; however, in FreeBSD 4.6 and later, those calls
  112. work correctly on BPF devices, so the workaround isn't necessary,
  113. although it does no harm.)
  114. .PP
  115. Note also that
  116. .B poll()
  117. and
  118. .B kevent()
  119. doesn't work on character special files, including BPF devices, in Mac
  120. OS X 10.4 and 10.5, so, while
  121. .B select()
  122. can be used on the descriptor returned by
  123. .BR pcap_get_selectable_fd() ,
  124. .B poll()
  125. and
  126. .B kevent()
  127. cannot be used on it those versions of Mac OS X.
  128. .BR poll() ,
  129. but not
  130. .BR kevent() ,
  131. works on that descriptor in Mac OS X releases prior to
  132. 10.4;
  133. .B poll()
  134. and
  135. .B kevent()
  136. work on that descriptor in Mac OS X 10.6 and later.
  137. .PP
  138. .B pcap_get_selectable_fd()
  139. is not available on Windows.
  140. .SH RETURN VALUE
  141. A selectable file descriptor is returned if one exists; otherwise, \-1
  142. is returned.
  143. .SH SEE ALSO
  144. pcap(3PCAP), pcap_get_required_select_timeout(3PCAP),
  145. pcap_setnonblock(3PCAP), select(2), poll(2), epoll_wait(2), kqueue(2)