inet.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <arpa/inet.h>
  27. #include <errno.h>
  28. #include <netinet/in.h>
  29. #include <stdio.h>
  30. extern "C" {
  31. const char* inet_ntop(int af, const void* src, char* dst, socklen_t len)
  32. {
  33. if (af != AF_INET) {
  34. errno = EAFNOSUPPORT;
  35. return nullptr;
  36. }
  37. auto* bytes = (const unsigned char*)src;
  38. snprintf(dst, len, "%u.%u.%u.%u", bytes[0], bytes[1], bytes[2], bytes[3]);
  39. return (const char*)dst;
  40. }
  41. int inet_pton(int af, const char* src, void* dst)
  42. {
  43. if (af != AF_INET) {
  44. errno = EAFNOSUPPORT;
  45. return -1;
  46. }
  47. unsigned a;
  48. unsigned b;
  49. unsigned c;
  50. unsigned d;
  51. int count = sscanf(src, "%u.%u.%u.%u", &a, &b, &c, &d);
  52. if (count != 4) {
  53. errno = EINVAL;
  54. return 0;
  55. }
  56. union {
  57. struct {
  58. uint8_t a;
  59. uint8_t b;
  60. uint8_t c;
  61. uint8_t d;
  62. };
  63. uint32_t l;
  64. } u;
  65. u.a = a;
  66. u.b = b;
  67. u.c = c;
  68. u.d = d;
  69. *(uint32_t*)dst = u.l;
  70. return 1;
  71. }
  72. in_addr_t inet_addr(const char* str)
  73. {
  74. in_addr_t tmp {};
  75. int rc = inet_pton(AF_INET, str, &tmp);
  76. if (rc < 0)
  77. return INADDR_NONE;
  78. return tmp;
  79. }
  80. char* inet_ntoa(struct in_addr in)
  81. {
  82. static char buffer[32];
  83. inet_ntop(AF_INET, &in.s_addr, buffer, sizeof(buffer));
  84. return buffer;
  85. }
  86. }