ip.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file
  3. * Common IPv4 and IPv6 code
  4. *
  5. * @defgroup ip IP
  6. * @ingroup callbackstyle_api
  7. *
  8. * @defgroup ip4 IPv4
  9. * @ingroup ip
  10. *
  11. * @defgroup ip6 IPv6
  12. * @ingroup ip
  13. *
  14. * @defgroup ipaddr IP address handling
  15. * @ingroup infrastructure
  16. *
  17. * @defgroup ip4addr IPv4 only
  18. * @ingroup ipaddr
  19. *
  20. * @defgroup ip6addr IPv6 only
  21. * @ingroup ipaddr
  22. */
  23. /*
  24. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or without modification,
  28. * are permitted provided that the following conditions are met:
  29. *
  30. * 1. Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above copyright notice,
  33. * this list of conditions and the following disclaimer in the documentation
  34. * and/or other materials provided with the distribution.
  35. * 3. The name of the author may not be used to endorse or promote products
  36. * derived from this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  41. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  42. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  43. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  44. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  46. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  47. * OF SUCH DAMAGE.
  48. *
  49. * This file is part of the lwIP TCP/IP stack.
  50. *
  51. * Author: Adam Dunkels <adam@sics.se>
  52. *
  53. */
  54. #include "lwip/opt.h"
  55. #if LWIP_IPV4 || LWIP_IPV6
  56. #include "lwip/ip_addr.h"
  57. #include "lwip/ip.h"
  58. /** Global data for both IPv4 and IPv6 */
  59. struct ip_globals ip_data;
  60. #if LWIP_IPV4 && LWIP_IPV6
  61. const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;
  62. /**
  63. * @ingroup ipaddr
  64. * Convert IP address string (both versions) to numeric.
  65. * The version is auto-detected from the string.
  66. *
  67. * @param cp IP address string to convert
  68. * @param addr conversion result is stored here
  69. * @return 1 on success, 0 on error
  70. */
  71. int
  72. ipaddr_aton(const char *cp, ip_addr_t *addr)
  73. {
  74. if (cp != NULL) {
  75. const char* c;
  76. for (c = cp; *c != 0; c++) {
  77. if (*c == ':') {
  78. /* contains a colon: IPv6 address */
  79. if (addr) {
  80. IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6);
  81. }
  82. return ip6addr_aton(cp, ip_2_ip6(addr));
  83. } else if (*c == '.') {
  84. /* contains a dot: IPv4 address */
  85. break;
  86. }
  87. }
  88. /* call ip4addr_aton as fallback or if IPv4 was found */
  89. if (addr) {
  90. IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4);
  91. }
  92. return ip4addr_aton(cp, ip_2_ip4(addr));
  93. }
  94. return 0;
  95. }
  96. /**
  97. * @ingroup lwip_nosys
  98. * If both IP versions are enabled, this function can dispatch packets to the correct one.
  99. * Don't call directly, pass to netif_add() and call netif->input().
  100. */
  101. err_t
  102. ip_input(struct pbuf *p, struct netif *inp)
  103. {
  104. if (p != NULL) {
  105. if (IP_HDR_GET_VERSION(p->payload) == 6) {
  106. return ip6_input(p, inp);
  107. }
  108. return ip4_input(p, inp);
  109. }
  110. return ERR_VAL;
  111. }
  112. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  113. #endif /* LWIP_IPV4 || LWIP_IPV6 */