portmap.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "tcpedit.h"
  42. #include "portmap.h"
  43. tcpedit_portmap_t *
  44. new_portmap()
  45. {
  46. tcpedit_portmap_t *newportmap;
  47. newportmap = (tcpedit_portmap_t *)safe_malloc(sizeof(tcpedit_portmap_t));
  48. return (newportmap);
  49. }
  50. /*
  51. * parses a string <port>:<port> and returns a new
  52. * tcpedit_portmap_t datastruct
  53. */
  54. static tcpedit_portmap_t *
  55. ports2PORT(char *ports)
  56. {
  57. tcpedit_portmap_t *portmap = NULL;
  58. char *from_s, *to_s, *badchar;
  59. long from_l, to_l;
  60. char *token = NULL;
  61. assert(ports);
  62. /* first split the port numbers */
  63. from_s = strtok_r(ports, ":", &token);
  64. to_s = strtok_r(NULL, ":", &token);
  65. /* if there's anything left, it's a syntax error */
  66. if (strtok_r(NULL, ":", &token) != NULL)
  67. return NULL;
  68. /* if either of the parsed strings is NULL, we have a problem */
  69. if (from_s == NULL || to_s == NULL)
  70. return NULL;
  71. /* convert the strings to longs: if badchar points to anything
  72. * after, then it was a bad string
  73. */
  74. from_l = strtol(from_s, &badchar, 10);
  75. if (strlen(badchar) != 0)
  76. return NULL;
  77. to_l = strtol(to_s, &badchar, 10);
  78. if (strlen(badchar) != 0)
  79. return NULL;
  80. portmap = new_portmap();
  81. /* put the new portmap info into the new node
  82. * while we convert to network-byte order, b/c its better
  83. * to do it once now, rather then each time we have to do a lookup
  84. */
  85. portmap->from = htons(from_l);
  86. portmap->to = htons(to_l);
  87. /* return 1 for success */
  88. return portmap;
  89. }
  90. /*
  91. * Processes a string (ourstr) containing the portmap ("2000:4000" for
  92. * example) and places the data in **portmapdata and finally returns 1 for
  93. * success, 0 for fail.
  94. */
  95. int
  96. parse_portmap(tcpedit_portmap_t ** portmap, const char *ourstr)
  97. {
  98. tcpedit_portmap_t *portmap_ptr;
  99. char *substr = NULL, *ourstrcpy = NULL, *token = NULL;
  100. assert(ourstr);
  101. ourstrcpy = safe_strdup(ourstr);
  102. /* first iteration of input */
  103. substr = strtok_r(ourstrcpy, ",", &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(tcpedit_portmap_t * portmap)
  123. {
  124. assert(portmap);
  125. /* recursively go down the portmaps */
  126. if (portmap->next != NULL)
  127. free_portmap(portmap->next);
  128. free(portmap);
  129. }
  130. /* This function takes a pointer to a portmap list and prints each node */
  131. void
  132. print_portmap(tcpedit_portmap_t *portmap_data)
  133. {
  134. tcpedit_portmap_t *portmap_ptr;
  135. assert(portmap_data);
  136. portmap_ptr = portmap_data;
  137. while (portmap_ptr != NULL) {
  138. printf("from: %ld to: %ld\n", portmap_ptr->from, portmap_ptr->to);
  139. portmap_ptr = portmap_ptr->next;
  140. }
  141. printf("\n");
  142. }
  143. /* This function takes a portmap and a port, and returns the mapped port,
  144. * or the original port if it isn't mapped to anything.
  145. */
  146. long
  147. map_port(tcpedit_portmap_t *portmap_data, long port)
  148. {
  149. tcpedit_portmap_t *portmap_ptr;
  150. long newport;
  151. assert(portmap_data);
  152. portmap_ptr = portmap_data;
  153. newport = port;
  154. /* step through the nodes, resetting newport if a match is found */
  155. while (portmap_ptr != NULL) {
  156. if (portmap_ptr->from == port)
  157. newport = portmap_ptr->to;
  158. portmap_ptr = portmap_ptr->next;
  159. }
  160. return(newport);
  161. }
  162. /*
  163. * rewrites the TCP or UDP ports based on a portmap
  164. * returns 1 for changes made or 0 for none
  165. */
  166. int
  167. rewrite_ports(tcpedit_t *tcpedit, ip_hdr_t **ip_hdr)
  168. {
  169. tcp_hdr_t *tcp_hdr = NULL;
  170. udp_hdr_t *udp_hdr = NULL;
  171. int changes = 0;
  172. u_int16_t newport;
  173. tcpedit_portmap_t *portmap;
  174. assert(tcpedit);
  175. assert(tcpedit->portmap);
  176. portmap = tcpedit->portmap;
  177. if (*ip_hdr == NULL) {
  178. return 0;
  179. } else if ((*ip_hdr)->ip_p == IPPROTO_TCP) {
  180. tcp_hdr = (tcp_hdr_t *)get_layer4(*ip_hdr);
  181. /* check if we need to remap the destination port */
  182. newport = map_port(portmap, tcp_hdr->th_dport);
  183. if (newport != tcp_hdr->th_dport) {
  184. tcp_hdr->th_dport = newport;
  185. changes ++;
  186. }
  187. /* check if we need to remap the source port */
  188. newport = map_port(portmap, tcp_hdr->th_sport);
  189. if (newport != tcp_hdr->th_sport) {
  190. tcp_hdr->th_sport = newport;
  191. changes ++;
  192. }
  193. } else if ((*ip_hdr)->ip_p == IPPROTO_UDP) {
  194. udp_hdr = (udp_hdr_t *)get_layer4(*ip_hdr);
  195. /* check if we need to remap the destination port */
  196. newport = map_port(portmap, udp_hdr->uh_dport);
  197. if (newport != udp_hdr->uh_dport) {
  198. udp_hdr->uh_dport = newport;
  199. changes ++;
  200. }
  201. /* check if we need to remap the source port */
  202. newport = map_port(portmap, udp_hdr->uh_sport);
  203. if (newport != udp_hdr->uh_sport) {
  204. udp_hdr->uh_sport = newport;
  205. changes ++;
  206. }
  207. }
  208. return changes;
  209. }
  210. /*
  211. Local Variables:
  212. mode:c
  213. indent-tabs-mode:nil
  214. c-basic-offset:4
  215. End:
  216. */