portmap.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include "config.h"
  38. #include "tcpreplay.h"
  39. #include "utils.h"
  40. #include "err.h"
  41. #include "portmap.h"
  42. #define EBUF_SIZE 256
  43. PORTMAP *
  44. new_portmap()
  45. {
  46. PORTMAP *newportmap;
  47. newportmap = (PORTMAP *) malloc(sizeof(PORTMAP));
  48. if (newportmap == NULL)
  49. errx(1, "unable to malloc memory for new_portmap()");
  50. memset(newportmap, 0, sizeof(PORTMAP));
  51. return (newportmap);
  52. }
  53. /*
  54. * parses a string <port>:<port> and returns a new
  55. * PORTMAP datastruct
  56. */
  57. static PORTMAP *
  58. ports2PORT(char *ports)
  59. {
  60. PORTMAP *portmap = NULL;
  61. char *from_s, *to_s, *badchar;
  62. long from_l, to_l;
  63. char *token = NULL;
  64. /* first split the port numbers */
  65. from_s = strtok_r(ports, ":", &token);
  66. to_s = strtok_r(NULL, ":", &token);
  67. /* if there's anything left, it's a syntax error */
  68. if (strtok_r(NULL, ":", &token) != NULL)
  69. return NULL;
  70. /* if either of the parsed strings is NULL, we have a problem */
  71. if (from_s == NULL || to_s == NULL)
  72. return NULL;
  73. /* convert the strings to longs: if badchar points to anything
  74. * after, then it was a bad string
  75. */
  76. from_l = strtol(from_s, &badchar, 10);
  77. if (strlen(badchar) != 0)
  78. return NULL;
  79. to_l = strtol(to_s, &badchar, 10);
  80. if (strlen(badchar) != 0)
  81. return NULL;
  82. portmap = new_portmap();
  83. /* put the new portmap info into the new node
  84. * while we convert to network-byte order, b/c its better
  85. * to do it once now, rather then each time we have to do a lookup
  86. */
  87. portmap->from = htons(from_l);
  88. portmap->to = htons(to_l);
  89. /* return 1 for success */
  90. return portmap;
  91. }
  92. /*
  93. * Processes a string (ourstr) containing the portmap ("2000:4000" for
  94. * example) and places the data in **portmapdata and finally returns 1 for
  95. * success, 0 for fail.
  96. */
  97. int
  98. parse_portmap(PORTMAP ** portmap, char *ourstr)
  99. {
  100. PORTMAP *portmap_ptr;
  101. char *substr = NULL, *token = NULL;
  102. /* first iteration of input */
  103. substr = strtok_r(ourstr, ",", &token);
  104. if ((*portmap = ports2PORT(substr)) == NULL)
  105. return 0;
  106. portmap_ptr = *portmap;
  107. while (1) {
  108. substr = strtok_r(NULL, ",", &token);
  109. /* if that was the last one, kick out */
  110. if (substr == NULL)
  111. break;
  112. /* next record */
  113. portmap_ptr->next = ports2PORT(substr);
  114. portmap_ptr = portmap_ptr->next;
  115. }
  116. return 1;
  117. }
  118. /*
  119. * Free's all the memory associated with the given portmap chain
  120. */
  121. void
  122. free_portmap(PORTMAP * portmap)
  123. {
  124. /* recursively go down the portmaps */
  125. if (portmap->next != NULL)
  126. free_portmap(portmap->next);
  127. free(portmap);
  128. }
  129. /* This function takes a pointer to a portmap list and prints each node */
  130. void
  131. print_portmap(PORTMAP *portmap_data)
  132. {
  133. PORTMAP *portmap_ptr;
  134. portmap_ptr = portmap_data;
  135. while (portmap_ptr != NULL) {
  136. printf("from: %ld to: %ld\n", portmap_ptr->from, portmap_ptr->to);
  137. portmap_ptr = portmap_ptr->next;
  138. }
  139. printf("\n");
  140. }
  141. /* This function takes a portmap and a port, and returns the mapped port,
  142. * or the original port if it isn't mapped to anything.
  143. */
  144. long
  145. map_port(PORTMAP *portmap_data, long port)
  146. {
  147. PORTMAP *portmap_ptr;
  148. long newport;
  149. portmap_ptr = portmap_data;
  150. newport = port;
  151. /* step through the nodes, resetting newport if a match is found */
  152. while (portmap_ptr != NULL) {
  153. if (portmap_ptr->from == port)
  154. newport = portmap_ptr->to;
  155. portmap_ptr = portmap_ptr->next;
  156. }
  157. return(newport);
  158. }
  159. /*
  160. * rewrites the TCP or UDP ports based on a portmap
  161. * returns 1 for changes made or 0 for none
  162. */
  163. int
  164. rewrite_ports(PORTMAP * portmap, ip_hdr_t **ip_hdr)
  165. {
  166. tcp_hdr_t *tcp_hdr = NULL;
  167. udp_hdr_t *udp_hdr = NULL;
  168. int changes = 0;
  169. u_int16_t newport;
  170. if (*ip_hdr == NULL) {
  171. return 0;
  172. } else if ((*ip_hdr)->ip_p == IPPROTO_TCP) {
  173. tcp_hdr = (tcp_hdr_t *)get_layer4(*ip_hdr);
  174. /* check if we need to remap the destination port */
  175. newport = map_port(portmap, tcp_hdr->th_dport);
  176. if (newport != tcp_hdr->th_dport) {
  177. tcp_hdr->th_dport = newport;
  178. changes ++;
  179. }
  180. /* check if we need to remap the source port */
  181. newport = map_port(portmap, tcp_hdr->th_sport);
  182. if (newport != tcp_hdr->th_sport) {
  183. tcp_hdr->th_sport = newport;
  184. changes ++;
  185. }
  186. } else if ((*ip_hdr)->ip_p == IPPROTO_UDP) {
  187. udp_hdr = (udp_hdr_t *)get_layer4(*ip_hdr);
  188. /* check if we need to remap the destination port */
  189. newport = map_port(portmap, udp_hdr->uh_dport);
  190. if (newport != udp_hdr->uh_dport) {
  191. udp_hdr->uh_dport = newport;
  192. changes ++;
  193. }
  194. /* check if we need to remap the source port */
  195. newport = map_port(portmap, udp_hdr->uh_sport);
  196. if (newport != udp_hdr->uh_sport) {
  197. udp_hdr->uh_sport = newport;
  198. changes ++;
  199. }
  200. }
  201. return changes;
  202. }