flowkey.c 4.3 KB

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