dns_resolve.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * fs/cifs/dns_resolve.c
  3. *
  4. * Copyright (c) 2007 Igor Mammedov
  5. * Author(s): Igor Mammedov (niallain@gmail.com)
  6. * Steve French (sfrench@us.ibm.com)
  7. * Wang Lei (wang840925@gmail.com)
  8. * David Howells (dhowells@redhat.com)
  9. *
  10. * Contains the CIFS DFS upcall routines used for hostname to
  11. * IP address translation.
  12. *
  13. * This library is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as published
  15. * by the Free Software Foundation; either version 2.1 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  21. * the GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/slab.h>
  28. #include <linux/dns_resolver.h>
  29. #include "dns_resolve.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. /**
  34. * dns_resolve_server_name_to_ip - Resolve UNC server name to ip address.
  35. * @unc: UNC path specifying the server (with '/' as delimiter)
  36. * @ip_addr: Where to return the IP address.
  37. *
  38. * The IP address will be returned in string form, and the caller is
  39. * responsible for freeing it.
  40. *
  41. * Returns length of result on success, -ve on error.
  42. */
  43. int
  44. dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
  45. {
  46. struct sockaddr_storage ss;
  47. const char *hostname, *sep;
  48. char *name;
  49. int len, rc;
  50. if (!ip_addr || !unc)
  51. return -EINVAL;
  52. len = strlen(unc);
  53. if (len < 3) {
  54. cifs_dbg(FYI, "%s: unc is too short: %s\n", __func__, unc);
  55. return -EINVAL;
  56. }
  57. /* Discount leading slashes for cifs */
  58. len -= 2;
  59. hostname = unc + 2;
  60. /* Search for server name delimiter */
  61. sep = memchr(hostname, '/', len);
  62. if (sep)
  63. len = sep - hostname;
  64. else
  65. cifs_dbg(FYI, "%s: probably server name is whole unc: %s\n",
  66. __func__, unc);
  67. /* Try to interpret hostname as an IPv4 or IPv6 address */
  68. rc = cifs_convert_address((struct sockaddr *)&ss, hostname, len);
  69. if (rc > 0)
  70. goto name_is_IP_address;
  71. /* Perform the upcall */
  72. rc = dns_query(current->nsproxy->net_ns, NULL, hostname, len,
  73. NULL, ip_addr, NULL, false);
  74. if (rc < 0)
  75. cifs_dbg(FYI, "%s: unable to resolve: %*.*s\n",
  76. __func__, len, len, hostname);
  77. else
  78. cifs_dbg(FYI, "%s: resolved: %*.*s to %s\n",
  79. __func__, len, len, hostname, *ip_addr);
  80. return rc;
  81. name_is_IP_address:
  82. name = kmalloc(len + 1, GFP_KERNEL);
  83. if (!name)
  84. return -ENOMEM;
  85. memcpy(name, hostname, len);
  86. name[len] = 0;
  87. cifs_dbg(FYI, "%s: unc is IP, skipping dns upcall: %s\n",
  88. __func__, name);
  89. *ip_addr = name;
  90. return 0;
  91. }