portmap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2001-2004 Aaron Turner.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright owners nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * This file contains routines to manipulate port maps, in which
  32. * one port number is mapped to another.
  33. */
  34. #include "config.h"
  35. #include "defines.h"
  36. #include "common.h"
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <errno.h>
  40. #include "tcpreplay.h"
  41. #include "portmap.h"
  42. portmap_t *
  43. new_portmap()
  44. {
  45. portmap_t *newportmap;
  46. newportmap = (portmap_t *)safe_malloc(sizeof(portmap_t));
  47. return (newportmap);
  48. }
  49. /*
  50. * parses a string <port>:<port> and returns a new
  51. * portmap_t datastruct
  52. */
  53. static portmap_t *
  54. ports2PORT(char *ports)
  55. {
  56. portmap_t *portmap = NULL;
  57. char *from_s, *to_s, *badchar;
  58. long from_l, to_l;
  59. char *token = NULL;
  60. /* first split the port numbers */
  61. from_s = strtok_r(ports, ":", &token);
  62. to_s = strtok_r(NULL, ":", &token);
  63. /* if there's anything left, it's a syntax error */
  64. if (strtok_r(NULL, ":", &token) != NULL)
  65. return NULL;
  66. /* if either of the parsed strings is NULL, we have a problem */
  67. if (from_s == NULL || to_s == NULL)
  68. return NULL;
  69. /* convert the strings to longs: if badchar points to anything
  70. * after, then it was a bad string
  71. */
  72. from_l = strtol(from_s, &badchar, 10);
  73. if (strlen(badchar) != 0)
  74. return NULL;
  75. to_l = strtol(to_s, &badchar, 10);
  76. if (strlen(badchar) != 0)
  77. return NULL;
  78. portmap = new_portmap();
  79. /* put the new portmap info into the new node
  80. * while we convert to network-byte order, b/c its better
  81. * to do it once now, rather then each time we have to do a lookup
  82. */
  83. portmap->from = htons(from_l);
  84. portmap->to = htons(to_l);
  85. /* return 1 for success */
  86. return portmap;
  87. }
  88. /*
  89. * Processes a string (ourstr) containing the portmap ("2000:4000" for
  90. * example) and places the data in **portmapdata and finally returns 1 for
  91. * success, 0 for fail.
  92. */
  93. int
  94. parse_portmap(portmap_t ** portmap, char *ourstr)
  95. {
  96. portmap_t *portmap_ptr;
  97. char *substr = NULL, *token = NULL;
  98. /* first iteration of input */
  99. substr = strtok_r(ourstr, ",", &token);
  100. if ((*portmap = ports2PORT(substr)) == NULL)
  101. return 0;
  102. portmap_ptr = *portmap;
  103. while (1) {
  104. substr = strtok_r(NULL, ",", &token);
  105. /* if that was the last one, kick out */
  106. if (substr == NULL)
  107. break;
  108. /* next record */
  109. portmap_ptr->next = ports2PORT(substr);
  110. portmap_ptr = portmap_ptr->next;
  111. }
  112. return 1;
  113. }
  114. /*
  115. * Free's all the memory associated with the given portmap chain
  116. */
  117. void
  118. free_portmap(portmap_t * portmap)
  119. {
  120. /* recursively go down the portmaps */
  121. if (portmap->next != NULL)
  122. free_portmap(portmap->next);
  123. free(portmap);
  124. }
  125. /* This function takes a pointer to a portmap list and prints each node */
  126. void
  127. print_portmap(portmap_t *portmap_data)
  128. {
  129. portmap_t *portmap_ptr;
  130. portmap_ptr = portmap_data;
  131. while (portmap_ptr != NULL) {
  132. printf("from: %ld to: %ld\n", portmap_ptr->from, portmap_ptr->to);
  133. portmap_ptr = portmap_ptr->next;
  134. }
  135. printf("\n");
  136. }
  137. /* This function takes a portmap and a port, and returns the mapped port,
  138. * or the original port if it isn't mapped to anything.
  139. */
  140. long
  141. map_port(portmap_t *portmap_data, long port)
  142. {
  143. portmap_t *portmap_ptr;
  144. long newport;
  145. portmap_ptr = portmap_data;
  146. newport = port;
  147. /* step through the nodes, resetting newport if a match is found */
  148. while (portmap_ptr != NULL) {
  149. if (portmap_ptr->from == port)
  150. newport = portmap_ptr->to;
  151. portmap_ptr = portmap_ptr->next;
  152. }
  153. return(newport);
  154. }
  155. /*
  156. * rewrites the TCP or UDP ports based on a portmap
  157. * returns 1 for changes made or 0 for none
  158. */
  159. int
  160. rewrite_ports(portmap_t * portmap, ip_hdr_t **ip_hdr)
  161. {
  162. tcp_hdr_t *tcp_hdr = NULL;
  163. udp_hdr_t *udp_hdr = NULL;
  164. int changes = 0;
  165. u_int16_t newport;
  166. if (*ip_hdr == NULL) {
  167. return 0;
  168. } else if ((*ip_hdr)->ip_p == IPPROTO_TCP) {
  169. tcp_hdr = (tcp_hdr_t *)get_layer4(*ip_hdr);
  170. /* check if we need to remap the destination port */
  171. newport = map_port(portmap, tcp_hdr->th_dport);
  172. if (newport != tcp_hdr->th_dport) {
  173. tcp_hdr->th_dport = newport;
  174. changes ++;
  175. }
  176. /* check if we need to remap the source port */
  177. newport = map_port(portmap, tcp_hdr->th_sport);
  178. if (newport != tcp_hdr->th_sport) {
  179. tcp_hdr->th_sport = newport;
  180. changes ++;
  181. }
  182. } else if ((*ip_hdr)->ip_p == IPPROTO_UDP) {
  183. udp_hdr = (udp_hdr_t *)get_layer4(*ip_hdr);
  184. /* check if we need to remap the destination port */
  185. newport = map_port(portmap, udp_hdr->uh_dport);
  186. if (newport != udp_hdr->uh_dport) {
  187. udp_hdr->uh_dport = newport;
  188. changes ++;
  189. }
  190. /* check if we need to remap the source port */
  191. newport = map_port(portmap, udp_hdr->uh_sport);
  192. if (newport != udp_hdr->uh_sport) {
  193. udp_hdr->uh_sport = newport;
  194. changes ++;
  195. }
  196. }
  197. return changes;
  198. }
  199. /*
  200. Local Variables:
  201. mode:c
  202. indent-tabs-mode:nil
  203. c-basic-offset:4
  204. End:
  205. */