1
0

flowkey.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* $Id: flowkey.c 767 2004-10-06 12:48:49Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "flowreplay.h"
  33. #include "flowkey.h"
  34. #include "err.h"
  35. /*
  36. * takes in a packet from the IP header on, and generates a unique key
  37. * for the redblack tree. Uses the following formula:
  38. * char key[12] = highip + lowip + highport + lowport
  39. * returns 1 on success, 0 on fail
  40. */
  41. int
  42. rbkeygen(ip_hdr_t * ip, u_char proto, void *l4, u_char * key)
  43. {
  44. tcp_hdr_t *tcp = NULL;
  45. udp_hdr_t *udp = NULL;
  46. /* copy over the IP addresses, high then low */
  47. if (ip->ip_src.s_addr > ip->ip_dst.s_addr) {
  48. memcpy(key, &ip->ip_src.s_addr, 4);
  49. memcpy(&key[4], &ip->ip_dst.s_addr, 4);
  50. }
  51. else {
  52. memcpy(key, &ip->ip_dst.s_addr, 4);
  53. memcpy(&key[4], &ip->ip_src.s_addr, 4);
  54. }
  55. /* copy over the port, high then low */
  56. if (proto == IPPROTO_TCP) {
  57. tcp = (tcp_hdr_t *) l4;
  58. if (tcp->th_sport > tcp->th_dport) {
  59. memcpy(&key[8], &tcp->th_sport, 2);
  60. memcpy(&key[10], &tcp->th_dport, 2);
  61. }
  62. else {
  63. memcpy(&key[8], &tcp->th_dport, 2);
  64. memcpy(&key[10], &tcp->th_sport, 2);
  65. }
  66. dbg(3, "rbkeygen TCP: %s:%hu > %s:%hu => 0x%llx",
  67. libnet_addr2name4(ip->ip_src.s_addr, LIBNET_DONT_RESOLVE),
  68. ntohs(tcp->th_sport),
  69. libnet_addr2name4(ip->ip_dst.s_addr, LIBNET_DONT_RESOLVE),
  70. ntohs(tcp->th_dport), pkeygen(key));
  71. }
  72. else if (proto == IPPROTO_UDP) {
  73. udp = (udp_hdr_t *) l4;
  74. if (udp->uh_sport > udp->uh_dport) {
  75. memcpy(&key[8], &udp->uh_sport, 2);
  76. memcpy(&key[10], &udp->uh_dport, 2);
  77. }
  78. else {
  79. memcpy(&key[8], &udp->uh_dport, 2);
  80. memcpy(&key[10], &udp->uh_sport, 2);
  81. }
  82. dbg(3, "rbkeygen UDP: %s:%u > %s:%u => 0x%llx",
  83. libnet_addr2name4(ip->ip_src.s_addr, LIBNET_DONT_RESOLVE),
  84. ntohs(udp->uh_sport),
  85. libnet_addr2name4(ip->ip_dst.s_addr, LIBNET_DONT_RESOLVE),
  86. ntohs(udp->uh_dport), pkeygen(key));
  87. }
  88. else {
  89. warnx("You tried to rbkeygen() for a non-TCP/UDP packet!");
  90. return (0);
  91. }
  92. return (1);
  93. }
  94. /*
  95. * pseudo-key gen. Generates a 64bit key suitable for printing via 0x%llx
  96. * since we can't print the real 12 byte rbkey
  97. */
  98. u_int64_t
  99. pkeygen(u_char key[])
  100. {
  101. u_int32_t ip1, ip2;
  102. u_int16_t port1, port2;
  103. u_int64_t result = 0, temp = 0;
  104. memcpy(&ip1, &key, 4);
  105. memcpy(&ip2, &key[4], 4);
  106. memcpy(&port1, &key[8], 2);
  107. memcpy(&port2, &key[10], 2);
  108. result = ip1 ^ ip2;
  109. temp = (port1 << 16) | port2;
  110. result = (temp << 32) | result;
  111. return (result);
  112. }