netutil.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "netutil.h"
  5. /**
  6. * Convert a 32bit Address into a Dotted Decimal Format string.
  7. *
  8. * @param addr 32bit address.
  9. * @return Dotted Decimal Format string.
  10. */
  11. int8_t* inet_ntoa(uint32_t addr)
  12. {
  13. static int8_t addr_str[16];
  14. memset(addr_str,0,16);
  15. sprintf((char*)addr_str,"%d.%d.%d.%d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF));
  16. return addr_str;
  17. }
  18. /**
  19. * Convert a 32bit Address into a Dotted Decimal Format string.
  20. * This is differ from inet_ntoa in fixed length.
  21. *
  22. * @param addr 32bit address.
  23. * @return Dotted Decimal Format string.
  24. */
  25. int8_t* inet_ntoa_pad(uint32_t addr)
  26. {
  27. static int8_t addr_str[16];
  28. memset(addr_str,0,16);
  29. sprintf((char*)addr_str,"%03d.%03d.%03d.%03d",(int32_t)(addr>>24 & 0xFF),(int32_t)(addr>>16 & 0xFF),(int32_t)(addr>>8 & 0xFF),(int32_t)(addr & 0xFF));
  30. return addr_str;
  31. }
  32. /**
  33. * Converts a string containing an (Ipv4) Internet Protocol decimal dotted address into a 32bit address.
  34. *
  35. * @param addr Dotted Decimal Format string.
  36. * @return 32bit address.
  37. */
  38. uint32_t inet_addr(uint8_t* addr)
  39. {
  40. int8_t i;
  41. uint32_t inetaddr = 0;
  42. int8_t taddr[30];
  43. int8_t * nexttok;
  44. int32_t num;
  45. strcpy((char*)taddr,(char*)addr);
  46. nexttok = taddr;
  47. for(i = 0; i < 4 ; i++)
  48. {
  49. nexttok = (int8_t*)strtok((char*)nexttok,".");
  50. if(nexttok[0] == '0' && nexttok[1] == 'x') num = strtol((char*)nexttok+2, NULL, 16);
  51. else num = strtol((char*)nexttok, NULL, 10);
  52. inetaddr = inetaddr << 8;
  53. inetaddr |= (num & 0xFF);
  54. nexttok = NULL;
  55. }
  56. return inetaddr;
  57. }
  58. /**
  59. * Swap the byte order of 16bit(short) wide variable.
  60. *
  61. * @param i 16bit value to swap
  62. * @return Swapped value
  63. */
  64. uint16_t swaps(uint16_t i)
  65. {
  66. uint16_t ret=0;
  67. ret = (i & 0xFF) << 8;
  68. ret |= ((i >> 8)& 0xFF);
  69. return ret;
  70. }
  71. /**
  72. * Swap the byte order of 32bit(long) wide variable.
  73. *
  74. * @param l 32bit value to convert
  75. * @return Swapped value
  76. */
  77. uint32_t swapl(uint32_t l)
  78. {
  79. uint32_t ret=0;
  80. ret = (l & 0xFF) << 24;
  81. ret |= ((l >> 8) & 0xFF) << 16;
  82. ret |= ((l >> 16) & 0xFF) << 8;
  83. ret |= ((l >> 24) & 0xFF);
  84. return ret;
  85. }
  86. /**
  87. * htons function converts a unsigned short from host to TCP/IP network byte order (which is big-endian).
  88. *
  89. * @param hostshort The value to convert.
  90. * @return The value in TCP/IP network byte order.
  91. */
  92. uint16_t htons(uint16_t hostshort)
  93. {
  94. #ifdef SYSTEM_LITTLE_ENDIAN
  95. return swaps(hostshort);
  96. #else
  97. return hostshort;
  98. #endif
  99. }
  100. /**
  101. * htonl function converts a unsigned long from host to TCP/IP network byte order (which is big-endian).
  102. *
  103. * @param hostlong The value to convert.
  104. * @return The value in TCP/IP network byte order.
  105. */
  106. uint32_t htonl(uint32_t hostlong)
  107. {
  108. #ifdef SYSTEM_LITTLE_ENDIAN
  109. return swapl(hostlong);
  110. #else
  111. return hostlong;
  112. #endif
  113. }
  114. /**
  115. * ntohs function converts a unsigned short from TCP/IP network byte order
  116. * to host byte order (which is little-endian on Intel processors).
  117. *
  118. * @param netshort The value to convert.
  119. * @return A 16-bit number in host byte order
  120. */
  121. uint32_t ntohs(uint16_t netshort)
  122. {
  123. #ifdef SYSTEM_LITTLE_ENDIAN
  124. return htons(netshort);
  125. #else
  126. return netshort;
  127. #endif
  128. }
  129. /**
  130. * converts a unsigned long from TCP/IP network byte order to host byte order
  131. * (which is little-endian on Intel processors).
  132. *
  133. * @param netlong The value to convert.
  134. * @return A 16-bit number in host byte order
  135. */
  136. uint32_t ntohl(uint32_t netlong)
  137. {
  138. #ifdef SYSTEM_LITTLE_ENDIAN
  139. return swapl(netlong);
  140. #else
  141. return netlong;
  142. #endif
  143. }
  144. /**
  145. * @}
  146. */