packet.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Name: packet.c
  2. *
  3. * Slitheen - a decoy routing system for censorship resistance
  4. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 3.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Additional permission under GNU GPL version 3 section 7
  19. *
  20. * If you modify this Program, or any covered work, by linking or combining
  21. * it with the OpenSSL library (or a modified version of that library),
  22. * containing parts covered by the terms of the OpenSSL Licence and the
  23. * SSLeay license, the licensors of this Program grant you additional
  24. * permission to convey the resulting work. Corresponding Source for a
  25. * non-source form of such a combination shall include the source code
  26. * for the parts of the OpenSSL library used as well as that of the covered
  27. * work.
  28. */
  29. #include <pcap.h>
  30. #include "packet.h"
  31. #include "util.h"
  32. /** Copies a packet_info structure and returns a pointer to the duplicate.
  33. */
  34. struct packet_info *copy_packet_info(struct packet_info *src_info){
  35. struct packet_info *dst_info = emalloc(sizeof(struct packet_info));
  36. dst_info->ip_hdr = src_info->ip_hdr;
  37. dst_info->tcp_hdr = src_info->tcp_hdr;
  38. dst_info->size_tcp_hdr = src_info->size_tcp_hdr;
  39. dst_info->size_ip_hdr = src_info->size_ip_hdr;
  40. dst_info->app_data = src_info->app_data;
  41. dst_info->app_data_len = src_info->app_data_len;
  42. return dst_info;
  43. }
  44. /*
  45. * Injects a packet back out the opposite interface
  46. */
  47. void inject_packet(struct inject_args *iargs, const struct pcap_pkthdr *header, uint8_t *packet){
  48. pcap_t *handle = iargs->write_dev;
  49. //write back out to the MAC ADDR it came in on
  50. //memmove(packet, packet+ETHER_ADDR_LEN, ETHER_ADDR_LEN);
  51. //memcpy(packet+ETHER_ADDR_LEN, iargs->mac_addr, ETHER_ADDR_LEN);
  52. if((pcap_inject(handle, packet, header->len)) < 0 ){
  53. fprintf(stderr, "Error: %s\n", pcap_geterr(handle));
  54. printf("Length: %d\n", header->len);
  55. }
  56. #ifdef DEBUG_EXTRA
  57. fprintf(stderr, "injected the following packet:\n");
  58. for(int i=0; i< header->len; i++){
  59. fprintf(stderr, "%02x ", packet[i]);
  60. }
  61. fprintf(stderr, "\n");
  62. #endif
  63. free(packet);
  64. }