rewrite_sequence.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. * Copyright (c) 2017 Mario D. Santana <tcpreplay at elorangutan dot com> - El Orangutan
  6. *
  7. * The Tcpreplay Suite of tools is free software: you can redistribute it
  8. * and/or modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or with the authors permission any later version.
  11. *
  12. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * This file contains routines to manipulate port maps, in which
  22. * one port number is mapped to another.
  23. */
  24. #include "config.h"
  25. #include "defines.h"
  26. #include "common.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include "tcpreplay.h"
  31. #include "tcpedit.h"
  32. #include "rewrite_sequence.h"
  33. #include "incremental_checksum.h"
  34. /**
  35. * rewrites the TCP sequence and ack numbers
  36. * returns 1 for changes made or 0 for none
  37. */
  38. static int
  39. rewrite_seqs(tcpedit_t *tcpedit, tcp_hdr_t *tcp_hdr)
  40. {
  41. volatile uint32_t newnum;
  42. newnum = ntohl(tcp_hdr->th_seq) + tcpedit->tcp_sequence_adjust;
  43. csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_seq, htonl(newnum));
  44. tcp_hdr->th_seq = htonl(newnum);
  45. /* first packet of 3-way handshake must have an ACK of zero - #450 */
  46. if (!((tcp_hdr->th_flags & TH_SYN) && !(tcp_hdr->th_flags & TH_ACK))) {
  47. newnum = ntohl(tcp_hdr->th_ack) + tcpedit->tcp_sequence_adjust;
  48. csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_ack, htonl(newnum));
  49. tcp_hdr->th_ack = htonl(newnum);
  50. }
  51. return 0;
  52. }
  53. int
  54. rewrite_ipv4_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr)
  55. {
  56. assert(tcpedit);
  57. tcp_hdr_t *tcp_hdr = NULL;
  58. if (*ip_hdr && (*ip_hdr)->ip_p == IPPROTO_TCP) {
  59. tcp_hdr = (tcp_hdr_t *)get_layer4_v4(*ip_hdr, 65536);
  60. return rewrite_seqs(tcpedit, tcp_hdr);
  61. }
  62. return 0;
  63. }
  64. int
  65. rewrite_ipv6_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip6_hdr)
  66. {
  67. assert(tcpedit);
  68. tcp_hdr_t *tcp_hdr = NULL;
  69. if (*ip6_hdr && (*ip6_hdr)->ip_nh == IPPROTO_TCP) {
  70. tcp_hdr = (tcp_hdr_t *)get_layer4_v6(*ip6_hdr, 65535);
  71. return rewrite_seqs(tcpedit, tcp_hdr);
  72. }
  73. return 0;
  74. }