flowkey.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* $Id: flowkey.c 1542 2006-07-29 06:19:22Z 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 "defines.h"
  33. #include "common.h"
  34. #include "flowreplay.h"
  35. #include "flowkey.h"
  36. #include <string.h>
  37. /*
  38. * takes in a packet from the IP header on, and generates a unique key
  39. * for the redblack tree. Uses the following formula:
  40. * char key[12] = highip + lowip + highport + lowport
  41. * returns 1 on success, 0 on fail
  42. */
  43. int
  44. rbkeygen(ipv4_hdr_t * ip, u_char proto, void *l4, u_char * key)
  45. {
  46. tcp_hdr_t *tcp = NULL;
  47. udp_hdr_t *udp = NULL;
  48. /* copy over the IP addresses, high then low */
  49. if (ip->ip_src.s_addr > ip->ip_dst.s_addr) {
  50. memcpy(key, &ip->ip_src.s_addr, 4);
  51. memcpy(&key[4], &ip->ip_dst.s_addr, 4);
  52. }
  53. else {
  54. memcpy(key, &ip->ip_dst.s_addr, 4);
  55. memcpy(&key[4], &ip->ip_src.s_addr, 4);
  56. }
  57. /* copy over the port, high then low */
  58. if (proto == IPPROTO_TCP) {
  59. tcp = (tcp_hdr_t *) l4;
  60. if (tcp->th_sport > tcp->th_dport) {
  61. memcpy(&key[8], &tcp->th_sport, 2);
  62. memcpy(&key[10], &tcp->th_dport, 2);
  63. }
  64. else {
  65. memcpy(&key[8], &tcp->th_dport, 2);
  66. memcpy(&key[10], &tcp->th_sport, 2);
  67. }
  68. dbgx(3, "rbkeygen TCP: %s:%hu > %s:%hu => 0x%llx",
  69. get_addr2name4(ip->ip_src.s_addr, RESOLVE),
  70. ntohs(tcp->th_sport),
  71. get_addr2name4(ip->ip_dst.s_addr, RESOLVE),
  72. ntohs(tcp->th_dport), pkeygen(key));
  73. }
  74. else if (proto == IPPROTO_UDP) {
  75. udp = (udp_hdr_t *) l4;
  76. if (udp->uh_sport > udp->uh_dport) {
  77. memcpy(&key[8], &udp->uh_sport, 2);
  78. memcpy(&key[10], &udp->uh_dport, 2);
  79. }
  80. else {
  81. memcpy(&key[8], &udp->uh_dport, 2);
  82. memcpy(&key[10], &udp->uh_sport, 2);
  83. }
  84. dbgx(3, "rbkeygen UDP: %s:%u > %s:%u => 0x%llx",
  85. get_addr2name4(ip->ip_src.s_addr, RESOLVE),
  86. ntohs(udp->uh_sport),
  87. get_addr2name4(ip->ip_dst.s_addr, RESOLVE),
  88. ntohs(udp->uh_dport), pkeygen(key));
  89. }
  90. else {
  91. warn("You tried to rbkeygen() for a non-TCP/UDP packet!");
  92. return (0);
  93. }
  94. return (1);
  95. }
  96. /*
  97. * pseudo-key gen. Generates a 64bit key suitable for printing via 0x%llx
  98. * since we can't print the real 12 byte rbkey
  99. */
  100. u_int64_t
  101. pkeygen(u_char key[])
  102. {
  103. u_int32_t ip1, ip2;
  104. u_int16_t port1, port2;
  105. u_int64_t result = 0, temp = 0;
  106. memcpy(&ip1, &key, 4);
  107. memcpy(&ip2, &key[4], 4);
  108. memcpy(&port1, &key[8], 2);
  109. memcpy(&port2, &key[10], 2);
  110. result = ip1 ^ ip2;
  111. temp = (port1 << 16) | port2;
  112. result = (temp << 32) | result;
  113. return (result);
  114. }
  115. /*
  116. Local Variables:
  117. mode:c
  118. indent-tabs-mode:nil
  119. c-basic-offset:4
  120. End:
  121. */