inet.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file net/inet.c
  38. * \brief Internet address helper functions.
  39. *
  40. * \verbatim
  41. * $Id: inet.c 4473 2012-08-20 15:12:45Z haraldkipp $
  42. * \endverbatim
  43. */
  44. /*!
  45. * \addtogroup xgIP
  46. */
  47. /*@{*/
  48. #include <arpa/inet.h>
  49. /*!
  50. * \brief Convert decimal dotted ASCII representation into numeric IP address.
  51. *
  52. * \param str String containing the ASCII representation.
  53. *
  54. * \return IP address in network byte order.
  55. */
  56. uint32_t inet_addr(const char * str)
  57. {
  58. uint_fast16_t num;
  59. uint32_t addr = 0;
  60. uint_fast8_t parts = 0;
  61. uint8_t *ap;
  62. ap = (uint8_t *) & addr;
  63. while (parts < 4) {
  64. if (*str < '0' || *str > '9')
  65. break;
  66. for (num = 0; num <= 255;) {
  67. num = (num * 10) + (*str - '0');
  68. if (*++str < '0' || *str > '9')
  69. break;
  70. }
  71. if (num > 255)
  72. break;
  73. parts++;
  74. *ap++ = (uint8_t) num;
  75. if (*str != '.') {
  76. if (parts == 4)
  77. return addr;
  78. break;
  79. }
  80. str++;
  81. }
  82. return -1;
  83. }
  84. /*!
  85. * \brief Convert numeric IP address into decimal dotted
  86. * ASCII representation.
  87. *
  88. * \note This function is not thread safe. Each subsequent
  89. * call will destroy the previous result. Applications
  90. * should locally store the result before calling the
  91. * function again or allowing other threads to call it.
  92. *
  93. * \param addr IP address in network byte order.
  94. *
  95. * \return Pointer to a static buffer containing the
  96. * ASCII representation.
  97. */
  98. char *inet_ntoa(uint32_t addr)
  99. {
  100. static char str[16];
  101. char inv[3];
  102. char *rp;
  103. uint8_t *ap;
  104. uint8_t rem;
  105. uint_fast8_t n;
  106. uint_fast8_t i;
  107. rp = str;
  108. ap = (uint8_t *) & addr;
  109. for (n = 0; n < 4; n++) {
  110. i = 0;
  111. do {
  112. rem = *ap % (uint8_t) 10;
  113. *ap /= (uint8_t) 10;
  114. inv[i++] = '0' + rem;
  115. } while (*ap);
  116. while (i--)
  117. *rp++ = inv[i];
  118. *rp++ = '.';
  119. ap++;
  120. }
  121. *--rp = 0;
  122. return str;
  123. }
  124. /*!
  125. * \brief Convert numeric MAC address array into double dotted
  126. * ASCII representation.
  127. *
  128. * \note This function is not thread safe. Each subsequent
  129. * call will destroy the previous result. Applications
  130. * should locally store the result before calling the
  131. * function again or allowing other threads to call it.
  132. *
  133. * \param mac Pointer to MAC address array.
  134. *
  135. * \return Pointer to a static buffer containing the
  136. * ASCII representation.
  137. */
  138. char *inet_mtoa(uint8_t *mac)
  139. {
  140. static char str[18];
  141. char hex[16]="0123456789ABCDEF";
  142. int i, p;
  143. p=0;
  144. for(i=0;i<6;i++)
  145. {
  146. str[p++]=hex[mac[i]>>4];
  147. str[p++]=hex[mac[i]&0xf];
  148. if(i<5) str[p++]=':'; else str[p++]='\0';
  149. }
  150. return str;
  151. }
  152. /*@}*/