get.c 20 KB

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