get.c 21 KB

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