202-protocol_api.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. --- a/pcap-linux.c
  2. +++ b/pcap-linux.c
  3. @@ -414,7 +414,7 @@ static int iface_get_id(int fd, const ch
  4. static int iface_get_mtu(int fd, const char *device, char *ebuf);
  5. static int iface_get_arptype(int fd, const char *device, char *ebuf);
  6. #ifdef HAVE_PF_PACKET_SOCKETS
  7. -static int iface_bind(int fd, int ifindex, char *ebuf);
  8. +static int iface_bind(int fd, int ifindex, char *ebuf, unsigned short proto);
  9. #ifdef IW_MODE_MONITOR
  10. static int has_wext(int sock_fd, const char *device, char *ebuf);
  11. #endif /* IW_MODE_MONITOR */
  12. @@ -1028,7 +1028,7 @@ pcap_can_set_rfmon_linux(pcap_t *handle)
  13. * (We assume that if we have Wireless Extensions support
  14. * we also have PF_PACKET support.)
  15. */
  16. - sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  17. + sock_fd = socket(PF_PACKET, SOCK_RAW, p->opt.proto);
  18. if (sock_fd == -1) {
  19. (void)snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  20. "socket: %s", pcap_strerror(errno));
  21. @@ -1337,6 +1337,9 @@ pcap_activate_linux(pcap_t *handle)
  22. handle->read_op = pcap_read_linux;
  23. handle->stats_op = pcap_stats_linux;
  24. + if (handle->opt.proto < 0)
  25. + handle->opt.proto = (int) htons(ETH_P_ALL);
  26. +
  27. /*
  28. * The "any" device is a special device which causes us not
  29. * to bind to a particular device and thus to look at all
  30. @@ -3160,8 +3163,8 @@ activate_new(pcap_t *handle)
  31. * try a SOCK_RAW socket for the raw interface.
  32. */
  33. sock_fd = is_any_device ?
  34. - socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL)) :
  35. - socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  36. + socket(PF_PACKET, SOCK_DGRAM, handle->opt.proto) :
  37. + socket(PF_PACKET, SOCK_RAW, handle->opt.proto);
  38. if (sock_fd == -1) {
  39. if (errno == EINVAL || errno == EAFNOSUPPORT) {
  40. @@ -3279,7 +3282,7 @@ activate_new(pcap_t *handle)
  41. return PCAP_ERROR;
  42. }
  43. sock_fd = socket(PF_PACKET, SOCK_DGRAM,
  44. - htons(ETH_P_ALL));
  45. + handle->opt.proto);
  46. if (sock_fd == -1) {
  47. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  48. "socket: %s", pcap_strerror(errno));
  49. @@ -3343,7 +3346,7 @@ activate_new(pcap_t *handle)
  50. }
  51. if ((err = iface_bind(sock_fd, handlep->ifindex,
  52. - handle->errbuf)) != 1) {
  53. + handle->errbuf, handle->opt.proto)) != 1) {
  54. close(sock_fd);
  55. if (err < 0)
  56. return err;
  57. @@ -5050,7 +5053,7 @@ iface_get_id(int fd, const char *device,
  58. * or a PCAP_ERROR_ value on a hard error.
  59. */
  60. static int
  61. -iface_bind(int fd, int ifindex, char *ebuf)
  62. +iface_bind(int fd, int ifindex, char *ebuf, unsigned short proto)
  63. {
  64. struct sockaddr_ll sll;
  65. int err;
  66. @@ -5059,7 +5062,7 @@ iface_bind(int fd, int ifindex, char *eb
  67. memset(&sll, 0, sizeof(sll));
  68. sll.sll_family = AF_PACKET;
  69. sll.sll_ifindex = ifindex;
  70. - sll.sll_protocol = htons(ETH_P_ALL);
  71. + sll.sll_protocol = proto;
  72. if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
  73. if (errno == ENETDOWN) {
  74. @@ -6049,7 +6052,7 @@ activate_old(pcap_t *handle)
  75. /* Open the socket */
  76. - handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
  77. + handle->fd = socket(PF_INET, SOCK_PACKET, handle->opt.proto);
  78. if (handle->fd == -1) {
  79. snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
  80. "socket: %s", pcap_strerror(errno));
  81. --- a/pcap.c
  82. +++ b/pcap.c
  83. @@ -562,6 +562,7 @@ pcap_create_common(const char *source, c
  84. p->opt.promisc = 0;
  85. p->opt.rfmon = 0;
  86. p->opt.immediate = 0;
  87. + p->opt.proto = -1;
  88. p->opt.tstamp_type = -1; /* default to not setting time stamp type */
  89. p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
  90. @@ -725,6 +726,15 @@ pcap_get_tstamp_precision(pcap_t *p)
  91. }
  92. int
  93. +pcap_set_protocol(pcap_t *p, unsigned short proto)
  94. +{
  95. + if (pcap_check_activated(p))
  96. + return PCAP_ERROR_ACTIVATED;
  97. + p->opt.proto = proto;
  98. + return 0;
  99. +}
  100. +
  101. +int
  102. pcap_activate(pcap_t *p)
  103. {
  104. int status;
  105. --- a/pcap/pcap.h
  106. +++ b/pcap/pcap.h
  107. @@ -66,6 +66,7 @@ extern "C" {
  108. #define PCAP_VERSION_MINOR 4
  109. #define PCAP_ERRBUF_SIZE 256
  110. +#define HAS_PROTO_EXTENSION
  111. /*
  112. * Compatibility for systems that have a bpf.h that
  113. @@ -283,6 +284,7 @@ int pcap_set_timeout(pcap_t *, int);
  114. int pcap_set_tstamp_type(pcap_t *, int);
  115. int pcap_set_immediate_mode(pcap_t *, int);
  116. int pcap_set_buffer_size(pcap_t *, int);
  117. +int pcap_set_protocol(pcap_t *, unsigned short);
  118. int pcap_set_tstamp_precision(pcap_t *, int);
  119. int pcap_get_tstamp_precision(pcap_t *);
  120. int pcap_activate(pcap_t *);
  121. --- a/pcap-int.h
  122. +++ b/pcap-int.h
  123. @@ -109,6 +109,7 @@ struct pcap_opt {
  124. char *source;
  125. int timeout; /* timeout for buffering */
  126. int buffer_size;
  127. + int proto; /* protocol for packet socket (linux) */
  128. int promisc;
  129. int rfmon; /* monitor mode */
  130. int immediate; /* immediate mode - deliver packets as soon as they arrive */