PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/network/getaddrinfo.c

https://github.com/boliu/synchronicity
C | 191 lines | 114 code | 20 blank | 57 comment | 24 complexity | 7533d0a237294168186edfd9a1f54730 MD5 | raw file
  1. /*****************************************************************************
  2. * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
  3. *****************************************************************************
  4. * Copyright (C) 2005 VLC authors and VideoLAN
  5. * Copyright (C) 2002-2007 Rémi Denis-Courmont
  6. * $Id: 0d5cdf873792f36e0b29574145a777045c73535e $
  7. *
  8. * Author: Rémi Denis-Courmont <rem # videolan.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23. *****************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. # include "config.h"
  26. #endif
  27. #include <vlc_common.h>
  28. #include <vlc_charset.h>
  29. #include <stddef.h> /* size_t */
  30. #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
  31. #include <stdlib.h> /* malloc(), free(), strtoul() */
  32. #include <errno.h>
  33. #include <assert.h>
  34. #include <sys/types.h>
  35. #include <vlc_network.h>
  36. #ifndef AF_UNSPEC
  37. # define AF_UNSPEC 0
  38. #endif
  39. int vlc_getnameinfo( const struct sockaddr *sa, int salen,
  40. char *host, int hostlen, int *portnum, int flags )
  41. {
  42. char psz_servbuf[6], *psz_serv;
  43. int i_servlen, i_val;
  44. flags |= NI_NUMERICSERV;
  45. if( portnum != NULL )
  46. {
  47. psz_serv = psz_servbuf;
  48. i_servlen = sizeof( psz_servbuf );
  49. }
  50. else
  51. {
  52. psz_serv = NULL;
  53. i_servlen = 0;
  54. }
  55. i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
  56. if( portnum != NULL )
  57. *portnum = atoi( psz_serv );
  58. return i_val;
  59. }
  60. /**
  61. * Resolves a host name to a list of socket addresses (like getaddrinfo()).
  62. *
  63. * @param p_this a VLC object
  64. * @param node host name to resolve (encoded as UTF-8), or NULL
  65. * @param i_port port number for the socket addresses
  66. * @param p_hints parameters (see getaddrinfo() manual page)
  67. * @param res pointer set to the resulting chained list.
  68. * @return 0 on success, a getaddrinfo() error otherwise.
  69. * On failure, *res is undefined. On success, it must be freed with
  70. * freeaddrinfo().
  71. */
  72. int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
  73. int i_port, const struct addrinfo *p_hints,
  74. struct addrinfo **res )
  75. {
  76. struct addrinfo hints;
  77. char psz_buf[NI_MAXHOST], psz_service[6];
  78. /*
  79. * In VLC, we always use port number as integer rather than strings
  80. * for historical reasons (and portability).
  81. */
  82. if( ( i_port > 65535 ) || ( i_port < 0 ) )
  83. {
  84. msg_Err( p_this, "invalid port number %d specified", i_port );
  85. return EAI_SERVICE;
  86. }
  87. /* cannot overflow */
  88. snprintf( psz_service, 6, "%d", i_port );
  89. /* Check if we have to force ipv4 or ipv6 */
  90. memset (&hints, 0, sizeof (hints));
  91. if (p_hints != NULL)
  92. {
  93. const int safe_flags =
  94. AI_PASSIVE |
  95. AI_CANONNAME |
  96. AI_NUMERICHOST |
  97. AI_NUMERICSERV |
  98. #ifdef AI_ALL
  99. AI_ALL |
  100. #endif
  101. #ifdef AI_ADDRCONFIG
  102. AI_ADDRCONFIG |
  103. #endif
  104. #ifdef AI_V4MAPPED
  105. AI_V4MAPPED |
  106. #endif
  107. 0;
  108. hints.ai_family = p_hints->ai_family;
  109. hints.ai_socktype = p_hints->ai_socktype;
  110. hints.ai_protocol = p_hints->ai_protocol;
  111. /* Unfortunately, some flags chang the layout of struct addrinfo, so
  112. * they cannot be copied blindly from p_hints to &hints. Therefore, we
  113. * only copy flags that we know for sure are "safe".
  114. */
  115. hints.ai_flags = p_hints->ai_flags & safe_flags;
  116. }
  117. /* We only ever use port *numbers* */
  118. hints.ai_flags |= AI_NUMERICSERV;
  119. /*
  120. * VLC extensions :
  121. * - accept "" as NULL
  122. * - ignore square brackets
  123. */
  124. if (node != NULL)
  125. {
  126. if (node[0] == '[')
  127. {
  128. size_t len = strlen (node + 1);
  129. if ((len <= sizeof (psz_buf)) && (node[len] == ']'))
  130. {
  131. assert (len > 0);
  132. memcpy (psz_buf, node + 1, len - 1);
  133. psz_buf[len - 1] = '\0';
  134. node = psz_buf;
  135. }
  136. }
  137. if (node[0] == '\0')
  138. node = NULL;
  139. }
  140. int ret;
  141. node = ToLocale (node);
  142. #ifdef WIN32
  143. /*
  144. * Winsock tries to resolve numerical IPv4 addresses as AAAA
  145. * and IPv6 addresses as A... There comes the bug-to-bug fix.
  146. */
  147. if ((hints.ai_flags & AI_NUMERICHOST) == 0)
  148. {
  149. hints.ai_flags |= AI_NUMERICHOST;
  150. ret = getaddrinfo (node, psz_service, &hints, res);
  151. if (ret == 0)
  152. goto out;
  153. hints.ai_flags &= ~AI_NUMERICHOST;
  154. }
  155. #endif
  156. #ifdef AI_IDN
  157. /* Run-time I18n Domain Names support */
  158. hints.ai_flags |= AI_IDN;
  159. ret = getaddrinfo (node, psz_service, &hints, res);
  160. if (ret != EAI_BADFLAGS)
  161. goto out;
  162. /* IDN not available: disable and retry without it */
  163. hints.ai_flags &= ~AI_IDN;
  164. #endif
  165. ret = getaddrinfo (node, psz_service, &hints, res);
  166. #if defined(AI_IDN) || defined(WIN32)
  167. out:
  168. #endif
  169. LocaleFree (node);
  170. return ret;
  171. }