/Simple_Capture/packet_handlers.c

https://github.com/saahil/Network-Traffic-Monitor · C · 144 lines · 96 code · 27 blank · 21 comment · 2 complexity · 1ee642427fb83204de07b1a364869aea MD5 · raw file

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pcap.h>
  4. #include "globals.h"
  5. void packet_handler_stdio_udp(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) {
  6. struct tm pktime;
  7. char strtime[16];
  8. time_t local_tv_sec;
  9. ip_header *ih;
  10. udp_header *uh;
  11. u_short sport, dport;
  12. u_int ip_len;
  13. FILE *out_file;
  14. u_int i;
  15. u_char *udp_data;
  16. out_file = fopen("C:/simple_out.dump", "w");
  17. /*Into the file*/
  18. fprintf(out_file, "%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
  19. /*Get the timestamp from the header*/
  20. local_tv_sec = header->ts.tv_sec;
  21. localtime_s(&pktime, &local_tv_sec);
  22. strftime(strtime, sizeof(strtime), "%H:%M:%S", &pktime);
  23. fprintf(out_file, "%s\t%.6d\t%d\n", strtime, header->ts.tv_usec, header->len);
  24. /*For position of the ip header*/
  25. ih = (ip_header*)(pkt_data+14); /*Length of the ethernet header*/
  26. /*For the position of the UDP header*/
  27. ip_len = (ih->ver_ihl & 0xf)*4;
  28. uh = (udp_header*)((u_char*)ih+ip_len);
  29. sport = ntohs(uh->sport);
  30. dport = ntohs(uh->dport);
  31. /*Printing IP addresses and UDP ports on source and destination*/
  32. fprintf(out_file, "%d.%d.%d.%d:%d->%d.%d.%d.%d:%d\n",
  33. ih->saddr.byte1,
  34. ih->saddr.byte2,
  35. ih->saddr.byte3,
  36. ih->saddr.byte4,
  37. sport,
  38. ih->daddr.byte1,
  39. ih->daddr.byte2,
  40. ih->daddr.byte3,
  41. ih->daddr.byte4,
  42. dport);
  43. /*Reach the UDP data*/
  44. udp_data = (u_char*)uh+8;
  45. fprintf(out_file, "UDP data length: %d\n", uh->len);
  46. /*Print the packet*/
  47. for(i=1; i<uh->len; i++) {
  48. fprintf(out_file, "%c", udp_data[i-1]);
  49. //if((i%16)==0) fprintf(out_file, "\n");
  50. }
  51. fprintf(out_file, "\n\n");
  52. }
  53. void packet_handler_stdio_tcp(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) {
  54. FILE *out_file;
  55. out_file = fopen("C:/simple_out.dump", "w");
  56. /*Into the file*/
  57. //fprintf(out_file, "%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
  58. extract_tcp_info(out_file, header, pkt_data);
  59. fclose(out_file);
  60. Sleep(1000);
  61. printf("\n\n");
  62. }
  63. void packet_handler_file_dump(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) {
  64. /*Dump the contents into the pcap file*/
  65. printf("Reading now...\n");
  66. pcap_dump(param, header, pkt_data);
  67. printf("Done recording for this packet.\n");
  68. }
  69. void extract_tcp_info(FILE *out_file, const struct pcap_pkthdr *header, const u_char *pkt_data) {
  70. struct tm pktime;
  71. char strtime[16];
  72. time_t local_tv_sec;
  73. ip_header *ih;
  74. tcp_header *th;
  75. u_short sport, dport;
  76. u_int ip_len;
  77. u_int i;
  78. u_char *tcp_data;
  79. u_int data_offset;
  80. /*Get the timestamp from the header*/
  81. local_tv_sec = header->ts.tv_sec;
  82. localtime_s(&pktime, &local_tv_sec);
  83. strftime(strtime, sizeof(strtime), "%H:%M:%S", &pktime);
  84. fprintf(out_file, "%s\t%d\n", strtime, header->len);
  85. /*For position of the ip header*/
  86. ih = (ip_header*)(pkt_data+14); /*Length of the ethernet header*/
  87. /*For the position of the TCP header*/
  88. ip_len = (ih->ver_ihl & 0xf)*4;
  89. th = (tcp_header*)((u_char*)ih+ip_len);
  90. sport = ntohs(th->sport);
  91. dport = ntohs(th->dport);
  92. /*Printing IP addresses and TCP ports on source and destination*/
  93. fprintf(out_file, "%d.%d.%d.%d:%d->%d.%d.%d.%d:%d\n",
  94. ih->saddr.byte1,
  95. ih->saddr.byte2,
  96. ih->saddr.byte3,
  97. ih->saddr.byte4,
  98. sport,
  99. ih->daddr.byte1,
  100. ih->daddr.byte2,
  101. ih->daddr.byte3,
  102. ih->daddr.byte4,
  103. dport);
  104. /*Skip the header options*/
  105. data_offset = th->data_offset & 0xf0;
  106. data_offset = data_offset >> 4;
  107. /*Reach the tcp data*/
  108. tcp_data = (u_char*)th+(data_offset*4);
  109. /*Print the packet*/
  110. for(i=1; i<(ih->tlen-ip_len-data_offset*4); i++) {
  111. fprintf(out_file, "%c", tcp_data[i-1]);
  112. /*if((i%16)==0) {
  113. fprintf(out_file, "\n");
  114. }*/
  115. }
  116. fprintf(out_file, "\n\n");
  117. }