recv-pfring.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include "recv.h"
  9. #include "recv-internal.h"
  10. #include "../lib/includes.h"
  11. #include "../lib/logger.h"
  12. #include <errno.h>
  13. #include <unistd.h>
  14. #include <pfring_zc.h>
  15. #include "state.h"
  16. static pfring_zc_pkt_buff *pf_buffer;
  17. static pfring_zc_queue *pf_recv;
  18. void recv_init()
  19. {
  20. // Get the socket and packet handle
  21. pf_recv = zconf.pf.recv;
  22. pf_buffer = pfring_zc_get_packet_handle(zconf.pf.cluster);
  23. if (pf_buffer == NULL) {
  24. log_fatal("recv", "Could not get packet handle: %s",
  25. strerror(errno));
  26. }
  27. }
  28. void recv_cleanup()
  29. {
  30. if (!pf_recv) {
  31. return;
  32. }
  33. pfring_zc_sync_queue(pf_recv, rx_only);
  34. }
  35. void recv_packets()
  36. {
  37. int ret;
  38. // Poll for packets
  39. do {
  40. ret = pfring_zc_recv_pkt(pf_recv, &pf_buffer, 0);
  41. if (ret == 0) {
  42. usleep(1000);
  43. }
  44. } while (ret == 0);
  45. // Handle other errors, by not doing anything and logging
  46. if (ret != 1) {
  47. log_error("recv", "Error: %d", ret);
  48. return;
  49. }
  50. // Successfully got a packet, now handle it
  51. handle_packet(pf_buffer->len, pf_buffer->data);
  52. }
  53. int recv_update_stats(void)
  54. {
  55. if (!pf_recv) {
  56. return EXIT_FAILURE;
  57. }
  58. pfring_zc_stat pfst;
  59. if (pfring_zc_stats(pf_recv, &pfst)) {
  60. log_error("recv", "unable to retrieve pfring statistics");
  61. return EXIT_FAILURE;
  62. } else {
  63. zrecv.pcap_recv = pfst.recv;
  64. zrecv.pcap_drop = pfst.drop;
  65. }
  66. return EXIT_SUCCESS;
  67. }