get_myaddr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * get_myaddress.c
  3. *
  4. * Get client's IP address via ioctl. This avoids using the yellowpages.
  5. * Copyright (c) 2010, Oracle America, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. * * Neither the name of the "Oracle America, Inc." nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  28. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <rpc/types.h>
  35. #include <rpc/clnt.h>
  36. #include <rpc/pmap_prot.h>
  37. #include <sys/socket.h>
  38. #include <stdio.h>
  39. #include <unistd.h>
  40. #include <libintl.h>
  41. #include <net/if.h>
  42. #include <ifaddrs.h>
  43. #include <sys/ioctl.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <shlib-compat.h>
  47. /*
  48. * don't use gethostbyname, which would invoke yellow pages
  49. *
  50. * Avoid loopback interfaces. We return information from a loopback
  51. * interface only if there are no other possible interfaces.
  52. */
  53. void
  54. get_myaddress (struct sockaddr_in *addr)
  55. {
  56. struct ifaddrs *ifa;
  57. if (getifaddrs (&ifa) != 0)
  58. {
  59. perror ("get_myaddress: getifaddrs");
  60. exit (1);
  61. }
  62. int loopback = 0;
  63. struct ifaddrs *run;
  64. again:
  65. run = ifa;
  66. while (run != NULL)
  67. {
  68. if ((run->ifa_flags & IFF_UP)
  69. && run->ifa_addr != NULL
  70. && run->ifa_addr->sa_family == AF_INET
  71. && (!(run->ifa_flags & IFF_LOOPBACK)
  72. || (loopback == 1 && (run->ifa_flags & IFF_LOOPBACK))))
  73. {
  74. *addr = *((struct sockaddr_in *) run->ifa_addr);
  75. addr->sin_port = htons (PMAPPORT);
  76. goto out;
  77. }
  78. run = run->ifa_next;
  79. }
  80. if (loopback == 0)
  81. {
  82. loopback = 1;
  83. goto again;
  84. }
  85. out:
  86. freeifaddrs (ifa);
  87. /* The function is horribly specified. It does not return any error
  88. if no interface is up. Probably this won't happen (at least
  89. loopback is there) but still... */
  90. }
  91. #ifdef EXPORT_RPC_SYMBOLS
  92. libc_hidden_def (get_myaddress)
  93. #else
  94. libc_hidden_nolink_sunrpc (get_myaddress, GLIBC_2_0)
  95. #endif