get.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include "../../lib/sll.h"
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #ifdef DEBUG
  31. extern int debug;
  32. #endif
  33. #if defined HAVE_PCAP_VERSION && ! defined HAVE_WIN32
  34. extern const char pcap_version[];
  35. #endif
  36. /**
  37. * Depending on what version of libpcap/WinPcap there are different ways to get
  38. * the version of the libpcap/WinPcap library. This presents a unified way to
  39. * get that information.
  40. */
  41. const char *
  42. get_pcap_version(void)
  43. {
  44. #if defined HAVE_WINPCAP
  45. static char ourver[255];
  46. char *last, *version;
  47. /* WinPcap returns a string like:
  48. * WinPcap version 4.0 (packet.dll version 4.0.0.755), based on libpcap version 0.9.5
  49. */
  50. version = safe_strdup(pcap_lib_version());
  51. strtok_r(version, " ", &last);
  52. strtok_r(NULL, " ", &last);
  53. strlcpy(ourver, strtok_r(NULL, " ", &last), 255);
  54. safe_free(version);
  55. return ourver;
  56. #elif defined HAVE_PCAP_VERSION
  57. return pcap_version;
  58. #else
  59. return pcap_lib_version();
  60. #endif
  61. }
  62. /**
  63. * returns the L2 protocol (IP, ARP, etc)
  64. * or 0 for error
  65. */
  66. uint16_t
  67. get_l2protocol(const u_char *pktdata, const int datalen, const int datalink)
  68. {
  69. eth_hdr_t *eth_hdr;
  70. vlan_hdr_t *vlan_hdr;
  71. hdlc_hdr_t *hdlc_hdr;
  72. sll_hdr_t *sll_hdr;
  73. uint16_t ether_type;
  74. uint16_t eth_hdr_offset = 0;
  75. struct tcpr_pppserial_hdr *ppp;
  76. assert(pktdata);
  77. assert(datalen);
  78. switch (datalink) {
  79. case DLT_RAW:
  80. if ((pktdata[0] >> 4) == 4)
  81. return ETHERTYPE_IP;
  82. else if ((pktdata[0] >> 4) == 6)
  83. return ETHERTYPE_IP6;
  84. break;
  85. case DLT_JUNIPER_ETHER:
  86. if (memcmp(pktdata, "MGC", 3))
  87. warnx("No Magic Number found: %s (0x%x)",
  88. pcap_datalink_val_to_description(datalink), datalink);
  89. if ((pktdata[3] & 0x80) == 0x80) {
  90. eth_hdr_offset = ntohs(*((uint16_t*)&pktdata[4]));
  91. eth_hdr_offset += 6;
  92. } else
  93. eth_hdr_offset = 4; /* no header extensions */
  94. /* fall through */
  95. case DLT_EN10MB:
  96. eth_hdr = (eth_hdr_t *)(pktdata + eth_hdr_offset);
  97. ether_type = ntohs(eth_hdr->ether_type);
  98. switch (ether_type) {
  99. case ETHERTYPE_VLAN: /* 802.1q */
  100. vlan_hdr = (vlan_hdr_t *)pktdata;
  101. return ntohs(vlan_hdr->vlan_len);
  102. default:
  103. return ether_type; /* yes, return it in host byte order */
  104. }
  105. break;
  106. case DLT_PPP_SERIAL:
  107. ppp = (struct tcpr_pppserial_hdr *)pktdata;
  108. if (ntohs(ppp->protocol) == 0x0021)
  109. return htons(ETHERTYPE_IP);
  110. else
  111. return ppp->protocol;
  112. break;
  113. case DLT_C_HDLC:
  114. hdlc_hdr = (hdlc_hdr_t *)pktdata;
  115. return hdlc_hdr->protocol;
  116. break;
  117. case DLT_LINUX_SLL:
  118. sll_hdr = (sll_hdr_t *)pktdata;
  119. return sll_hdr->sll_protocol;
  120. break;
  121. default:
  122. errx(-1, "Unable to process unsupported DLT type: %s (0x%x)",
  123. pcap_datalink_val_to_description(datalink), datalink);
  124. }
  125. return 0;
  126. }
  127. /**
  128. * returns the length in number of bytes of the L2 header, or -1 on error
  129. */
  130. int
  131. get_l2len(const u_char *pktdata, const int datalen, const int datalink)
  132. {
  133. uint16_t ether_type = 0;
  134. vlan_hdr_t *vlan_hdr;
  135. int l2_len = 0;
  136. assert(pktdata);
  137. assert(datalen);
  138. switch (datalink) {
  139. case DLT_RAW:
  140. /* pktdata IS the ip header! */
  141. return 0;
  142. break;
  143. case DLT_JUNIPER_ETHER:
  144. l2_len = 24;
  145. /* fall through */
  146. case DLT_EN10MB:
  147. ether_type = ntohs(((eth_hdr_t*)(pktdata + l2_len))->ether_type);
  148. while (ether_type == ETHERTYPE_VLAN) {
  149. vlan_hdr = (vlan_hdr_t *)(pktdata + l2_len);
  150. ether_type = ntohs(vlan_hdr->vlan_len);
  151. l2_len += 4;
  152. }
  153. l2_len += sizeof(eth_hdr_t);
  154. return l2_len;
  155. break;
  156. case DLT_PPP_SERIAL:
  157. return 4;
  158. break;
  159. case DLT_C_HDLC:
  160. return CISCO_HDLC_LEN;
  161. break;
  162. case DLT_LINUX_SLL:
  163. return SLL_HDR_LEN;
  164. break;
  165. default:
  166. errx(-1, "Unable to process unsupported DLT type: %s (0x%x)",
  167. pcap_datalink_val_to_description(datalink), datalink);
  168. break;
  169. }
  170. return -1; /* we shouldn't get here */
  171. }
  172. /**
  173. * \brief returns a ptr to the ipv4 header + data or NULL if it's not IP
  174. *
  175. * we may use an extra buffer for the IP header (and above)
  176. * on strictly aligned systems where the layer 2 header doesn't
  177. * fall on a 4 byte boundary (like a standard Ethernet header)
  178. *
  179. * Note: you can cast the result as an ip_hdr_t, but you'll be able
  180. * to access data above the header minus any stripped L2 data
  181. */
  182. const u_char *
  183. get_ipv4(const u_char *pktdata, int datalen, int datalink, u_char **newbuff)
  184. {
  185. const u_char *ip_hdr = NULL;
  186. int l2_len = 0;
  187. uint16_t proto;
  188. assert(pktdata);
  189. assert(datalen);
  190. assert(*newbuff);
  191. l2_len = get_l2len(pktdata, datalen, datalink);
  192. /* sanity... datalen must be > l2_len + IP header len*/
  193. if (l2_len + TCPR_IPV4_H > datalen) {
  194. dbg(1, "get_ipv4(): Layer 2 len > total packet len, hence no IP header");
  195. return NULL;
  196. }
  197. proto = get_l2protocol(pktdata, datalen, datalink);
  198. if (proto != ETHERTYPE_IP)
  199. return NULL;
  200. #ifdef FORCE_ALIGN
  201. /*
  202. * copy layer 3 and up to our temp packet buffer
  203. * for now on, we have to edit the packetbuff because
  204. * just before we send the packet, we copy the packetbuff
  205. * back onto the pkt.data + l2len buffer
  206. * we do all this work to prevent byte alignment issues
  207. */
  208. if (l2_len % sizeof(long)) {
  209. memcpy(*newbuff, (pktdata + l2_len), (datalen - l2_len));
  210. ip_hdr = *newbuff;
  211. } else {
  212. /* we don't have to do a memcpy if l2_len lands on a boundry */
  213. ip_hdr = (pktdata + l2_len);
  214. }
  215. #else
  216. /*
  217. * on non-strict byte align systems, don't need to memcpy(),
  218. * just point to l2len bytes into the existing buffer
  219. */
  220. ip_hdr = (pktdata + l2_len);
  221. #endif
  222. return ip_hdr;
  223. }
  224. /**
  225. * \brief returns a ptr to the ipv6 header + data or NULL if it's not IP
  226. *
  227. * we may use an extra buffer for the IP header (and above)
  228. * on strictly aligned systems where the layer 2 header doesn't
  229. * fall on a 4 byte boundary (like a standard Ethernet header)
  230. *
  231. * Note: you can cast the result as an ip_hdr_t, but you'll be able
  232. * to access data above the header minus any stripped L2 data
  233. */
  234. const u_char *
  235. get_ipv6(const u_char *pktdata, int datalen, int datalink, u_char **newbuff)
  236. {
  237. const u_char *ip6_hdr = NULL;
  238. int l2_len = 0;
  239. uint16_t proto;
  240. assert(pktdata);
  241. assert(datalen);
  242. assert(*newbuff);
  243. l2_len = get_l2len(pktdata, datalen, datalink);
  244. /* sanity... datalen must be > l2_len + IP header len*/
  245. if (l2_len + TCPR_IPV6_H > datalen) {
  246. dbg(1, "get_ipv6(): Layer 2 len > total packet len, hence no IPv6 header");
  247. return NULL;
  248. }
  249. proto = get_l2protocol(pktdata, datalen, datalink);
  250. if (proto != ETHERTYPE_IP6)
  251. return NULL;
  252. #ifdef FORCE_ALIGN
  253. /*
  254. * copy layer 3 and up to our temp packet buffer
  255. * for now on, we have to edit the packetbuff because
  256. * just before we send the packet, we copy the packetbuff
  257. * back onto the pkt.data + l2len buffer
  258. * we do all this work to prevent byte alignment issues
  259. */
  260. if (l2_len % sizeof(long)) {
  261. memcpy(*newbuff, (pktdata + l2_len), (datalen - l2_len));
  262. ip6_hdr = *newbuff;
  263. } else {
  264. /* we don't have to do a memcpy if l2_len lands on a boundry */
  265. ip6_hdr = (pktdata + l2_len);
  266. }
  267. #else
  268. /*
  269. * on non-strict byte align systems, don't need to memcpy(),
  270. * just point to l2len bytes into the existing buffer
  271. */
  272. ip6_hdr = (pktdata + l2_len);
  273. #endif
  274. return ip6_hdr;
  275. }
  276. /**
  277. * \brief returns a pointer to the layer 4 header which is just beyond the IPv4 header
  278. *
  279. * If the packet is to short, returns NULL
  280. */
  281. void *
  282. get_layer4_v4(const ipv4_hdr_t *ip_hdr, const int len)
  283. {
  284. void *ptr;
  285. assert(ip_hdr);
  286. ptr = (uint32_t *) ip_hdr + ip_hdr->ip_hl;
  287. /* make sure we don't jump over the end of the buffer */
  288. if ((u_char *)ptr > ((u_char *)ip_hdr + len))
  289. return NULL;
  290. return ((void *)ptr);
  291. }
  292. /**
  293. * returns a pointer to the layer 4 header which is just beyond the IPv6 header
  294. * and any extension headers or NULL when there is none as in the case of
  295. * v6 Frag or ESP header. Function is recursive.
  296. */
  297. void *
  298. get_layer4_v6(const ipv6_hdr_t *ip6_hdr, const int len)
  299. {
  300. struct tcpr_ipv6_ext_hdr_base *next, *exthdr;
  301. uint8_t proto;
  302. uint32_t maxlen;
  303. assert(ip6_hdr);
  304. /* jump to the end of the IPv6 header */
  305. next = (struct tcpr_ipv6_ext_hdr_base *)((u_char *)ip6_hdr + TCPR_IPV6_H);
  306. proto = ip6_hdr->ip_nh;
  307. while (TRUE) {
  308. dbgx(3, "Processing proto: 0x%hx", (uint16_t)proto);
  309. switch (proto) {
  310. /* recurse due to v6-in-v6, need to recast next as an IPv6 Header */
  311. case TCPR_IPV6_NH_IPV6:
  312. dbg(3, "recursing due to v6-in-v6");
  313. return get_layer4_v6((ipv6_hdr_t *)next, len);
  314. break;
  315. /* loop again */
  316. case TCPR_IPV6_NH_AH:
  317. case TCPR_IPV6_NH_ROUTING:
  318. case TCPR_IPV6_NH_DESTOPTS:
  319. case TCPR_IPV6_NH_HBH:
  320. dbgx(3, "Going deeper due to extension header 0x%02X", proto);
  321. maxlen = len - (int)((u_char *)ip6_hdr - (u_char *)next);
  322. exthdr = get_ipv6_next(next, maxlen);
  323. proto = exthdr->ip_nh;
  324. next = exthdr;
  325. break;
  326. /*
  327. * Can't handle. Unparsable IPv6 fragment/encrypted data
  328. */
  329. case TCPR_IPV6_NH_FRAGMENT:
  330. case TCPR_IPV6_NH_ESP:
  331. return NULL;
  332. break;
  333. /*
  334. * no further processing, either TCP, UDP, ICMP, etc...
  335. */
  336. default:
  337. if (proto != ip6_hdr->ip_nh) {
  338. dbgx(3, "Returning byte offset of this ext header: %u",
  339. IPV6_EXTLEN_TO_BYTES(next->ip_len));
  340. return (void *)((u_char *)next + IPV6_EXTLEN_TO_BYTES(next->ip_len));
  341. } else {
  342. dbgx(3, "%s", "Returning end of IPv6 Header");
  343. return next;
  344. }
  345. break;
  346. } /* switch */
  347. } /* while */
  348. }
  349. /**
  350. * returns the next payload or header of the current extention header
  351. * returns NULL for none/ESP.
  352. */
  353. void *
  354. get_ipv6_next(struct tcpr_ipv6_ext_hdr_base *exthdr, const int len)
  355. {
  356. int extlen = 0;
  357. int maxlen;
  358. void *ptr;
  359. assert(exthdr);
  360. maxlen = *((int*)((u_char *)exthdr + len));
  361. dbgx(3, "Jumping to next IPv6 header. Processing 0x%02x", exthdr->ip_nh);
  362. switch (exthdr->ip_nh) {
  363. /* no further processing */
  364. case TCPR_IPV6_NH_NO_NEXT:
  365. case TCPR_IPV6_NH_ESP:
  366. dbg(3, "No-Next or ESP... can't go any further...");
  367. return NULL;
  368. break;
  369. /*
  370. * fragment header is fixed size
  371. * FIXME: Frag header has further ext headers (has a ip_nh field)
  372. * but I don't support it because there's never a full L4 + payload beyond.
  373. */
  374. case TCPR_IPV6_NH_FRAGMENT:
  375. dbg(3, "Looks like were a fragment header. Returning some frag'd data.");
  376. ptr = (void *)((u_char *)exthdr + sizeof(struct tcpr_ipv6_frag_hdr));
  377. if (*(int*)ptr > maxlen)
  378. return NULL;
  379. return ptr;
  380. break;
  381. /* all the rest require us to go deeper using the ip_len field */
  382. case TCPR_IPV6_NH_IPV6:
  383. case TCPR_IPV6_NH_ROUTING:
  384. case TCPR_IPV6_NH_DESTOPTS:
  385. case TCPR_IPV6_NH_HBH:
  386. case TCPR_IPV6_NH_AH:
  387. extlen = IPV6_EXTLEN_TO_BYTES(exthdr->ip_len);
  388. dbgx(3, "Looks like we're an ext header (0x%hhx). Jumping %u bytes"
  389. " to the next", exthdr->ip_nh, extlen);
  390. ptr = (void *)((u_char *)exthdr + extlen);
  391. if (*(int*)ptr > maxlen)
  392. return NULL;
  393. return ptr;
  394. break;
  395. default:
  396. dbg(3, "Must not be a v6 extension header... returning self");
  397. return (void *)exthdr;
  398. break;
  399. }
  400. }
  401. /**
  402. * returns the protocol of the actual layer4 header by processing through
  403. * the extension headers
  404. */
  405. uint8_t
  406. get_ipv6_l4proto(const ipv6_hdr_t *ip6_hdr, const int len)
  407. {
  408. u_char *ptr = (u_char *)ip6_hdr + TCPR_IPV6_H; /* jump to the end of the IPv6 header */
  409. uint8_t proto;
  410. struct tcpr_ipv6_ext_hdr_base *exthdr = NULL;
  411. assert(ip6_hdr);
  412. proto = ip6_hdr->ip_nh;
  413. while (TRUE) {
  414. dbgx(3, "Processing next proto 0x%02X", proto);
  415. switch (proto) {
  416. /* no further processing for IPV6 types with nothing beyond them */
  417. case TCPR_IPV6_NH_FRAGMENT:
  418. case TCPR_IPV6_NH_ESP:
  419. dbg(3, "No-Next or ESP... can't go any further...");
  420. return proto;
  421. break;
  422. /* recurse */
  423. case TCPR_IPV6_NH_IPV6:
  424. dbg(3, "Recursing due to v6 in v6");
  425. return get_ipv6_l4proto((ipv6_hdr_t *)ptr, len);
  426. break;
  427. /* loop again */
  428. case TCPR_IPV6_NH_AH:
  429. case TCPR_IPV6_NH_ROUTING:
  430. case TCPR_IPV6_NH_DESTOPTS:
  431. case TCPR_IPV6_NH_HBH:
  432. dbgx(3, "Jumping to next extension header (0x%hhx)", proto);
  433. exthdr = get_ipv6_next((struct tcpr_ipv6_ext_hdr_base *)ptr, len);
  434. proto = exthdr->ip_nh;
  435. ptr = (u_char *)exthdr;
  436. break;
  437. /* should be TCP, UDP or the like */
  438. default:
  439. dbgx(3, "Selecting next L4 Proto as: 0x%02x", proto);
  440. return proto;
  441. } /* switch */
  442. } /* while */
  443. }
  444. /**
  445. * \brief Converts a human readable IPv4 address to a binary one
  446. *
  447. * stolen from LIBNET since I didn't want to have to deal with
  448. * passing a libnet_t around. Returns 0xFFFFFFFF (255.255.255.255)
  449. * on error
  450. */
  451. uint32_t
  452. get_name2addr4(const char *hostname, bool dnslookup)
  453. {
  454. struct in_addr addr;
  455. #if ! defined HAVE_INET_ATON && defined HAVE_INET_ADDR
  456. struct hostent *host_ent;
  457. #endif
  458. uint32_t m;
  459. u_int val;
  460. int i;
  461. if (dnslookup) {
  462. #ifdef HAVE_INET_ATON
  463. if (inet_aton(hostname, &addr) != 1) {
  464. return(0xffffffff);
  465. }
  466. #elif defined HAVE_INET_ADDR
  467. if ((addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
  468. if (!(host_ent = gethostbyname(hostname))) {
  469. warnx("unable to resolve %s: %s", hostname, strerror(errno));
  470. /* XXX - this is actually 255.255.255.255 */
  471. return (0xffffffff);
  472. }
  473. /* was: host_ent->h_length); */
  474. memcpy(&addr.s_addr, host_ent->h_addr, sizeof(addr.s_addr));
  475. }
  476. #else
  477. warn("Unable to support get_name2addr4 w/ resolve");
  478. /* call ourselves recursively once w/o resolving the hostname */
  479. return get_name2addr4(hostname, DNS_DONT_RESOLVE);
  480. #endif
  481. /* return in network byte order */
  482. return (addr.s_addr);
  483. }
  484. /*
  485. * We only want dots 'n decimals.
  486. */
  487. else {
  488. if (!isdigit(hostname[0])) {
  489. warnx("Expected dotted-quad notation (%s) when DNS lookups are disabled",
  490. hostname);
  491. /* XXX - this is actually 255.255.255.255 */
  492. return (-1);
  493. }
  494. m = 0;
  495. for (i = 0; i < 4; i++) {
  496. m <<= 8;
  497. if (*hostname) {
  498. val = 0;
  499. while (*hostname && *hostname != '.') {
  500. val *= 10;
  501. val += *hostname - '0';
  502. if (val > 255) {
  503. dbgx(4, "value %d > 255 for dotted quad", val);
  504. /* XXX - this is actually 255.255.255.255 */
  505. return (-1);
  506. }
  507. hostname++;
  508. }
  509. m |= val;
  510. if (*hostname) {
  511. hostname++;
  512. }
  513. }
  514. }
  515. /* host byte order */
  516. return (ntohl(m));
  517. }
  518. }
  519. /**
  520. * \brief Converts human readable IPv6 address to binary value
  521. *
  522. * Wrapper around inet_pton
  523. * Returns 1 for valid, 0 for not parsable and -1 for system error.
  524. * Does not support DNS.
  525. */
  526. int
  527. get_name2addr6(const char *hostname, bool dnslookup, struct tcpr_in6_addr *addr)
  528. {
  529. (void)dnslookup; /* prevent warning about unused arg */
  530. #ifdef HAVE_INET_PTON
  531. return inet_pton(AF_INET6, hostname, addr);
  532. #else
  533. #error "Unable to support get_name2addr6: Missing inet_pton() support."
  534. #endif
  535. return -1;
  536. }
  537. /**
  538. * \brief Converts binary IPv4 address to a string.
  539. *
  540. * Generic wrapper around inet_ntop() and inet_ntoa() depending on whichever
  541. * is available on your system. Does not support DNS.
  542. */
  543. const char *
  544. get_addr2name4(const uint32_t ip, bool dnslookup)
  545. {
  546. struct in_addr addr;
  547. static char *new_string = NULL;
  548. if (new_string == NULL)
  549. new_string = (char *)safe_malloc(255);
  550. new_string[0] = '\0';
  551. addr.s_addr = ip;
  552. #ifdef HAVE_INET_NTOP
  553. if (inet_ntop(AF_INET, &addr, new_string, 255) == NULL) {
  554. warnx("Unable to convert 0x%x to a string", ip);
  555. new_string[0] = 0;
  556. }
  557. return new_string;
  558. #elif defined HAVE_INET_NTOA
  559. return inet_ntoa(&addr);
  560. #else
  561. #error "Unable to support get_addr2name4."
  562. #endif
  563. }
  564. /**
  565. * \brief Converts a IPv6 binary address to a string.a
  566. *
  567. * Does not support DNS.
  568. */
  569. const char *
  570. get_addr2name6(const struct tcpr_in6_addr *addr, bool dnslookup)
  571. {
  572. static char *new_string = NULL;
  573. if (new_string == NULL)
  574. new_string = (char *)safe_malloc(255);
  575. new_string[0] = '\0';
  576. #ifdef HAVE_INET_NTOP
  577. if (inet_ntop(AF_INET6, addr, new_string, 255) == NULL) {
  578. warn("Unable to convert addr to a string");
  579. new_string[0] = 0;
  580. }
  581. return new_string;
  582. #else
  583. #error "Unable to support get_addr2name6."
  584. #endif
  585. }
  586. /**
  587. * \brief Converts the binary network address of a tcpr_cidr_t to a string
  588. */
  589. const char *
  590. get_cidr2name(const tcpr_cidr_t *cidr_ptr, bool dnslookup)
  591. {
  592. if (cidr_ptr->family == AF_INET) {
  593. return get_addr2name4(cidr_ptr->u.network, dnslookup);
  594. } else if (cidr_ptr->family == AF_INET6) {
  595. return get_addr2name6(&cidr_ptr->u.network6, dnslookup);
  596. } else {
  597. return NULL;
  598. }
  599. }