cidr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* $Id: cidr.c 767 2004-10-06 12:48:49Z 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 <libnet.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. /* required for inet_aton() */
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #include <arpa/inet.h>
  39. #include "config.h"
  40. #include "tcpreplay.h"
  41. #include "cidr.h"
  42. #include "err.h"
  43. #ifdef DEBUG
  44. extern int debug;
  45. #endif
  46. static CIDR *cidr2CIDR(char *);
  47. /*
  48. * prints to the given fd all the entries in mycidr
  49. */
  50. void
  51. print_cidr(CIDR * mycidr)
  52. {
  53. CIDR *cidr_ptr;
  54. fprintf(stderr, "Cidr List: ");
  55. cidr_ptr = mycidr;
  56. while (cidr_ptr != NULL) {
  57. /* print it */
  58. fprintf(stderr, "%s/%d, ",
  59. libnet_addr2name4(cidr_ptr->network, RESOLVE),
  60. cidr_ptr->masklen);
  61. /* go to the next */
  62. if (cidr_ptr->next != NULL) {
  63. cidr_ptr = cidr_ptr->next;
  64. }
  65. else {
  66. break;
  67. }
  68. }
  69. fprintf(stderr, "\n");
  70. }
  71. /*
  72. * deletes all entries in a cidr and destroys the datastructure
  73. */
  74. void
  75. destroy_cidr(CIDR * cidr)
  76. {
  77. if (cidr != NULL)
  78. if (cidr->next != NULL)
  79. destroy_cidr(cidr->next);
  80. free(cidr);
  81. return;
  82. }
  83. /*
  84. * adds a new CIDR entry to cidrdata
  85. */
  86. void
  87. add_cidr(CIDR * cidrdata, CIDR ** newcidr)
  88. {
  89. CIDR *cidr_ptr;
  90. if (cidrdata == NULL) {
  91. cidrdata = *newcidr;
  92. }
  93. else {
  94. cidr_ptr = cidrdata;
  95. while (cidr_ptr->next != NULL) {
  96. cidr_ptr = cidr_ptr->next;
  97. }
  98. cidr_ptr->next = *newcidr;
  99. }
  100. }
  101. /*
  102. * takes in an IP and masklen, and returns a string in
  103. * cidr format: x.x.x.x/y. This malloc's memory.
  104. */
  105. u_char *
  106. ip2cidr(const unsigned long ip, const int masklen)
  107. {
  108. u_char *network;
  109. char mask[3];
  110. if ((network = (u_char *) malloc(20)) == NULL)
  111. err(1, "malloc");
  112. strncpy((char *)network, (char *)libnet_addr2name4(ip, LIBNET_DONT_RESOLVE),
  113. 19);
  114. strcat((char *)network, "/");
  115. if (masklen < 10) {
  116. snprintf(mask, 1, "%d", masklen);
  117. strncat((char *)network, mask, 1);
  118. }
  119. else {
  120. snprintf(mask, 2, "%d", masklen);
  121. strncat((char *)network, mask, 2);
  122. }
  123. return (network);
  124. }
  125. /*
  126. * Mallocs and sets to sane defaults a CIDR structure
  127. */
  128. CIDR *
  129. new_cidr(void)
  130. {
  131. CIDR *newcidr;
  132. newcidr = (CIDR *) malloc(sizeof(CIDR));
  133. if (newcidr == NULL)
  134. err(1, "unable to malloc memory for new_cidr()");
  135. memset(newcidr, '\0', sizeof(CIDR));
  136. newcidr->masklen = 99;
  137. newcidr->next = NULL;
  138. return (newcidr);
  139. }
  140. CIDRMAP *
  141. new_cidr_map(void)
  142. {
  143. CIDRMAP *new;
  144. new = (CIDRMAP *)malloc(sizeof(CIDRMAP));
  145. if (new == NULL)
  146. err(1, "unable to malloc memory for new_cidr()");
  147. memset(new, '\0', sizeof(CIDRMAP));
  148. new->next = NULL;
  149. return (new);
  150. }
  151. /*
  152. * Converts a single cidr (string) in the form of x.x.x.x/y into a
  153. * CIDR structure. Will malloc the CIDR structure.
  154. */
  155. static CIDR *
  156. cidr2CIDR(char *cidr)
  157. {
  158. int count = 0;
  159. unsigned int octets[4]; /* used in sscanf */
  160. CIDR *newcidr;
  161. char networkip[16], tempoctet[4], ebuf[EBUF_SIZE];
  162. if ((cidr == NULL) || (strlen(cidr) > EBUF_SIZE))
  163. errx(1, "Error parsing: %s", cidr);
  164. newcidr = new_cidr();
  165. /*
  166. * scan it, and make sure it scanned correctly, also copy over the
  167. * masklen
  168. */
  169. count = sscanf(cidr, "%u.%u.%u.%u/%d", &octets[0], &octets[1],
  170. &octets[2], &octets[3], &newcidr->masklen);
  171. if (count == 4) {
  172. newcidr->masklen = 32;
  173. } else if (count != 5) {
  174. goto error;
  175. }
  176. /* masklen better be 0 =< masklen <= 32 */
  177. if (newcidr->masklen > 32)
  178. goto error;
  179. /* copy in the ip address */
  180. memset(networkip, '\0', 16);
  181. for (count = 0; count < 4; count++) {
  182. if (octets[count] > 255)
  183. goto error;
  184. snprintf(tempoctet, sizeof(octets[count]), "%d", octets[count]);
  185. strcat(networkip, tempoctet);
  186. /* we don't want a '.' at the end of the last octet */
  187. if (count < 3)
  188. strcat(networkip, ".");
  189. }
  190. /* copy over the network address and return */
  191. #ifdef INET_ATON
  192. inet_aton(networkip, (struct in_addr *)&newcidr->network);
  193. #elif INET_ADDR
  194. newcidr->network = inet_addr(networkip);
  195. #endif
  196. return (newcidr);
  197. /* we only get here on error parsing input */
  198. error:
  199. memset(ebuf, '\0', EBUF_SIZE);
  200. strncpy(ebuf, "Unable to parse as a vaild CIDR: ", 34);
  201. strncat(ebuf, cidr, (EBUF_SIZE - strlen(ebuf) - 1));
  202. errx(1, "%s", ebuf);
  203. return NULL;
  204. }
  205. /*
  206. * parses a list of CIDR's input from the user which should be in the form
  207. * of x.x.x.x/y,x.x.x.x/y...
  208. * returns 1 for success, or fails to return on failure (exit 1)
  209. * since we use strtok to process cidr, it gets zeroed out.
  210. */
  211. int
  212. parse_cidr(CIDR ** cidrdata, char *cidrin, char *delim)
  213. {
  214. CIDR *cidr_ptr; /* ptr to current cidr record */
  215. char *network = NULL;
  216. char *token = NULL;
  217. /* first itteration of input using strtok */
  218. network = strtok_r(cidrin, delim, &token);
  219. *cidrdata = cidr2CIDR(network);
  220. cidr_ptr = *cidrdata;
  221. /* do the same with the rest of the input */
  222. while (1) {
  223. network = strtok_r(NULL, delim, &token);
  224. /* if that was the last CIDR, then kickout */
  225. if (network == NULL)
  226. break;
  227. /* next record */
  228. cidr_ptr->next = cidr2CIDR(network);
  229. cidr_ptr = cidr_ptr->next;
  230. }
  231. return 1;
  232. }
  233. /*
  234. * parses a pair of IP addresses: <IP1>:<IP2> and processes it like:
  235. * -N 0.0.0.0/0:<IP1> -N 0.0.0.0/0:<IP2>
  236. * returns 1 for success or returns 0 on failure
  237. * since we use strtok to process optarg, it gets zeroed out
  238. */
  239. int
  240. parse_endpoints(CIDRMAP ** cidrmap1, CIDRMAP ** cidrmap2, char *optarg)
  241. {
  242. #define NEWMAP 32
  243. char *map = NULL, newmap[NEWMAP];
  244. char *token = NULL;
  245. memset(newmap, '\0', NEWMAP);
  246. map = strtok_r(optarg, ":", &token);
  247. strcpy(newmap, "0.0.0.0/0:");
  248. strncat(newmap, map, NEWMAP - 1);
  249. if (! parse_cidr_map(cidrmap1, newmap))
  250. return 0;
  251. /* do again with the second IP */
  252. memset(newmap, '\0', NEWMAP);
  253. map = strtok_r(NULL, ":", &token);
  254. strcpy(newmap, "0.0.0.0/0:");
  255. strncat(newmap, map, NEWMAP - 1);
  256. if (! parse_cidr_map(cidrmap2, newmap))
  257. return 0;
  258. return 1; /* success */
  259. }
  260. /*
  261. * parses a list of CIDRMAP's input from the user which should be in the form
  262. * of x.x.x.x/y:x.x.x.x/y,...
  263. * returns 1 for success, or returns 0 on failure
  264. * since we use strtok to process optarg, it gets zeroed out.
  265. */
  266. int
  267. parse_cidr_map(CIDRMAP **cidrmap, char *optarg)
  268. {
  269. CIDR *cidr = NULL;
  270. char *map = NULL;
  271. char *token = NULL;
  272. CIDRMAP *ptr;
  273. /* first iteration */
  274. map = strtok_r(optarg, ",", &token);
  275. if (! parse_cidr(&cidr, map, ":"))
  276. return 0;
  277. /* must return a linked list of two */
  278. if (cidr->next == NULL)
  279. return 0;
  280. /* copy over */
  281. *cidrmap = new_cidr_map();
  282. ptr = *cidrmap;
  283. ptr->from = cidr;
  284. ptr->to = cidr->next;
  285. ptr->from->next = NULL;
  286. /* do the same with the reset of the input */
  287. while(1) {
  288. map = strtok_r(NULL, ",", &token);
  289. if (map == NULL)
  290. break;
  291. if (! parse_cidr(&cidr, map, ":"))
  292. return 0;
  293. /* must return a linked list of two */
  294. if (cidr->next == NULL)
  295. return 0;
  296. /* copy over */
  297. ptr->next = new_cidr_map();
  298. ptr = ptr->next;
  299. ptr->from = cidr;
  300. ptr->to = cidr->next;
  301. ptr->from->next = NULL;
  302. }
  303. return 1; /* success */
  304. }
  305. /*
  306. * checks to see if the ip address is in the cidr
  307. * returns 1 for true, 0 for false
  308. */
  309. int
  310. ip_in_cidr(const CIDR * mycidr, const unsigned long ip)
  311. {
  312. unsigned long ipaddr = 0, network = 0, mask = 0;
  313. /* always return 1 if 0.0.0.0/0 */
  314. if (mycidr->masklen == 0 && mycidr->network == 0)
  315. return 1;
  316. mask = ~0; /* turn on all the bits */
  317. /* shift over by the correct number of bits */
  318. mask = mask << (32 - mycidr->masklen);
  319. /* apply the mask to the network and ip */
  320. ipaddr = ntohl(ip) & mask;
  321. network = htonl(mycidr->network) & mask;
  322. /* if they're the same, then ip is in network */
  323. if (network == ipaddr) {
  324. dbg(1, "The ip %s is inside of %s/%d",
  325. libnet_addr2name4(ip, RESOLVE),
  326. libnet_addr2name4(htonl(network), RESOLVE), mycidr->masklen);
  327. return 1;
  328. }
  329. else {
  330. dbg(1, "The ip %s is not inside of %s/%d",
  331. libnet_addr2name4(ip, RESOLVE),
  332. libnet_addr2name4(htonl(network), RESOLVE), mycidr->masklen);
  333. return 0;
  334. }
  335. }
  336. /*
  337. * iterates over cidrdata to find if a given ip matches
  338. * returns 1 for true, 0 for false
  339. */
  340. int
  341. check_ip_CIDR(CIDR * cidrdata, const unsigned long ip)
  342. {
  343. CIDR *mycidr;
  344. /* if we have no cidrdata, of course it isn't in there */
  345. if (cidrdata == NULL)
  346. return 0;
  347. mycidr = cidrdata;
  348. /* loop through cidr */
  349. while (1) {
  350. /* if match, return 1 */
  351. if (ip_in_cidr(mycidr, ip)) {
  352. return 1;
  353. }
  354. /* check for next record */
  355. if (mycidr->next != NULL) {
  356. mycidr = mycidr->next;
  357. }
  358. else {
  359. break;
  360. }
  361. }
  362. /* if we get here, no match */
  363. return 0;
  364. }
  365. /*
  366. * cidr2ip takes a CIDR and a delimiter
  367. * and returns a string which lists all the IP addresses in the cidr
  368. * deliminated by the given char
  369. */
  370. char *
  371. cidr2iplist(CIDR * cidr, char delim)
  372. {
  373. char *list = NULL;
  374. char ipaddr[16];
  375. unsigned long size, i;
  376. unsigned long first, last, numips;
  377. struct in_addr in;
  378. /*
  379. * 16 bytes per IP + delim
  380. * # of IP's = 2^(32-masklen)
  381. */
  382. numips = 2;
  383. for (i = 2; i <= (32 - cidr->masklen); i++) {
  384. numips *= 2;
  385. }
  386. size = 16 * numips;
  387. if ((list = (char *)malloc(size)) == NULL)
  388. errx(1, "Unable to malloc %d bytes! Aborting...", size);
  389. memset(list, 0, size);
  390. /* first and last should not include network or broadcast */
  391. first = ntohl(cidr->network) + 1;
  392. last = first + numips - 3;
  393. dbg(1, "First: %u\t\tLast: %u", first, last);
  394. /* loop through all but the last one */
  395. for (i = first; i < last; i++) {
  396. in.s_addr = htonl(i);
  397. snprintf(ipaddr, 17, "%s%c", inet_ntoa(in), delim);
  398. dbg(2, "%s", ipaddr);
  399. strncat(list, ipaddr, size);
  400. }
  401. /* last is a special case, end in \0 */
  402. in.s_addr = htonl(i);
  403. snprintf(ipaddr, 16, "%s", inet_ntoa(in));
  404. strncat(list, ipaddr, size);
  405. return list;
  406. }