/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/http-host.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · C · 95 lines · 70 code · 6 blank · 19 comment · 22 complexity · 544aad444c1f9c7aefebf2aef7e9b259 MD5 · raw file

  1. /*
  2. http-host.c - thread-safe host lookup
  3. Copyright (C) 2008 siliconforks.com
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <config.h>
  17. #include "http-server.h"
  18. #include "util.h"
  19. int xgethostbyname(const char * host, struct in_addr * a) {
  20. #if defined(__CYGWIN__) || defined(__MINGW32__)
  21. /* gethostbyname is thread-safe */
  22. struct hostent * p = gethostbyname(host);
  23. if (p == NULL || p->h_addrtype != AF_INET) {
  24. return -1;
  25. }
  26. *a = *((struct in_addr *) p->h_addr);
  27. return 0;
  28. #elif HAVE_GETADDRINFO
  29. struct addrinfo hints;
  30. hints.ai_flags = 0;
  31. hints.ai_family = PF_INET;
  32. hints.ai_socktype = 0;
  33. hints.ai_protocol = 0;
  34. hints.ai_addrlen = 0;
  35. hints.ai_addr = NULL;
  36. hints.ai_canonname = NULL;
  37. hints.ai_next = NULL;
  38. struct addrinfo * p;
  39. int result = getaddrinfo(host, NULL, &hints, &p);
  40. if (result != 0 || p == NULL) {
  41. return -1;
  42. }
  43. if (p->ai_family != PF_INET) {
  44. freeaddrinfo(p);
  45. return -1;
  46. }
  47. struct sockaddr_in * address_and_port = (struct sockaddr_in *) p->ai_addr;
  48. *a = address_and_port->sin_addr;
  49. freeaddrinfo(p);
  50. return 0;
  51. #elif HAVE_GETHOSTBYNAME_R
  52. struct hostent h;
  53. struct hostent * p;
  54. char * buffer;
  55. size_t buffer_size;
  56. int error;
  57. int result;
  58. buffer_size = 1024;
  59. buffer = xmalloc(buffer_size);
  60. while ((result = gethostbyname_r(host, &h, buffer, buffer_size, &p, &error)) == ERANGE) {
  61. buffer_size = mulst(buffer_size, 2);
  62. buffer = xrealloc(buffer, buffer_size);
  63. }
  64. if (result != 0 || p == NULL || p->h_addrtype != AF_INET) {
  65. free(buffer);
  66. return -1;
  67. }
  68. *a = *((struct in_addr *) p->h_addr);
  69. free(buffer);
  70. return 0;
  71. #else
  72. #error "No thread-safe host lookup available"
  73. #endif
  74. }
  75. #ifndef HAVE_INET_ATON
  76. int inet_aton(const char * name, struct in_addr * a) {
  77. unsigned long result = inet_addr(name);
  78. if (result == INADDR_NONE) {
  79. return 0;
  80. }
  81. else {
  82. a->s_addr = result;
  83. return 1;
  84. }
  85. }
  86. #endif